grrr...fix reverse chronological order
[dragonfly.git] / lib / libcr / net / inet_net_ntop.c
1 /*
2  * Copyright (c) 1996 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * $FreeBSD: src/lib/libc/net/inet_net_ntop.c,v 1.5 1999/08/28 00:00:10 peter Exp $
18  * $DragonFly: src/lib/libcr/net/Attic/inet_net_ntop.c,v 1.3 2003/11/12 20:21:28 eirikn Exp $
19  */
20
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #ifdef SPRINTF_CHAR
32 # define SPRINTF(x) strlen(sprintf/**/x)
33 #else
34 # define SPRINTF(x) ((size_t)sprintf x)
35 #endif
36
37 static char *   inet_net_ntop_ipv4 (const u_char *src, int bits,
38                                         char *dst, size_t size);
39
40 /*
41  * char *
42  * inet_net_ntop(af, src, bits, dst, size)
43  *      convert network number from network to presentation format.
44  *      generates CIDR style result always.
45  * return:
46  *      pointer to dst, or NULL if an error occurred (check errno).
47  * author:
48  *      Paul Vixie (ISC), July 1996
49  */
50 char *
51 inet_net_ntop(af, src, bits, dst, size)
52         int af;
53         const void *src;
54         int bits;
55         char *dst;
56         size_t size;
57 {
58         switch (af) {
59         case AF_INET:
60                 return (inet_net_ntop_ipv4(src, bits, dst, size));
61         default:
62                 errno = EAFNOSUPPORT;
63                 return (NULL);
64         }
65 }
66
67 /*
68  * static char *
69  * inet_net_ntop_ipv4(src, bits, dst, size)
70  *      convert IPv4 network number from network to presentation format.
71  *      generates CIDR style result always.
72  * return:
73  *      pointer to dst, or NULL if an error occurred (check errno).
74  * note:
75  *      network byte order assumed.  this means 192.5.5.240/28 has
76  *      0x11110000 in its fourth octet.
77  * author:
78  *      Paul Vixie (ISC), July 1996
79  */
80 static char *
81 inet_net_ntop_ipv4(src, bits, dst, size)
82         const u_char *src;
83         int bits;
84         char *dst;
85         size_t size;
86 {
87         char *odst = dst;
88         char *t;
89         u_int m;
90         int b;
91
92         if (bits < 0 || bits > 32) {
93                 errno = EINVAL;
94                 return (NULL);
95         }
96         if (bits == 0) {
97                 if (size < sizeof "0")
98                         goto emsgsize;
99                 *dst++ = '0';
100                 *dst = '\0';
101         }
102
103         /* Format whole octets. */
104         for (b = bits / 8; b > 0; b--) {
105                 if (size < sizeof "255.")
106                         goto emsgsize;
107                 t = dst;
108                 dst += SPRINTF((dst, "%u", *src++));
109                 if (b > 1) {
110                         *dst++ = '.';
111                         *dst = '\0';
112                 }
113                 size -= (size_t)(dst - t);
114         }
115
116         /* Format partial octet. */
117         b = bits % 8;
118         if (b > 0) {
119                 if (size < sizeof ".255")
120                         goto emsgsize;
121                 t = dst;
122                 if (dst != odst)
123                         *dst++ = '.';
124                 m = ((1 << b) - 1) << (8 - b);
125                 dst += SPRINTF((dst, "%u", *src & m));
126                 size -= (size_t)(dst - t);
127         }
128
129         /* Format CIDR /width. */
130         if (size < sizeof "/32")
131                 goto emsgsize;
132         dst += SPRINTF((dst, "/%u", bits));
133         return (odst);
134
135  emsgsize:
136         errno = EMSGSIZE;
137         return (NULL);
138 }
139
140 /*
141  * Weak aliases for applications that use certain private entry points,
142  * and fail to include <arpa/inet.h>.
143  */
144 #undef inet_net_ntop
145 __weak_reference(__inet_net_ntop, inet_net_ntop);