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