FreeSWITCH API Documentation  1.7.0
inet_pton.c
Go to the documentation of this file.
1 /* This is from the BIND 4.9.4 release, modified to compile by itself */
2 
3 /* Copyright (c) 1996 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16  * SOFTWARE.
17  */
18 
19 #ifndef HAVE_INET_PTON
20 
21 #ifdef HAVE_SYS_PARAM_H
22 #include <sys/param.h>
23 #endif
24 #ifdef HAVE_SYS_TYPES_H
25 #include <sys/types.h>
26 #endif
27 #ifdef HAVE_SYS_SOCKET_H
28 #include <sys/socket.h>
29 #endif
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
35 #endif
36 #include <string.h>
37 #include <errno.h>
38 
39 #define IN6ADDRSZ 16
40 #define INADDRSZ 4
41 #define INT16SZ 2
42 
43 #ifdef WIN32
44 #include <winsock2.h>
45 #define ENABLE_IPV6
46 #if _MSC_VER < 1600
47 #define EAFNOSUPPORT WSAEAFNOSUPPORT
48 #endif
49 #endif
50 
51 #include <switch.h>
52 
53 /*
54  * WARNING: Don't even consider trying to compile this on a system where
55  * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
56  */
57 
58 static int inet_pton4(const char *src, unsigned char *dst);
59 #ifdef ENABLE_IPV6
60 static int inet_pton6(const char *src, unsigned char *dst);
61 #endif
62 
63 /* int
64  * inet_pton(af, src, dst)
65  * convert from presentation format (which usually means ASCII printable)
66  * to network format (which is usually some kind of binary format).
67  * return:
68  * 1 if the address was valid for the specified address family
69  * 0 if the address wasn't valid (`dst' is untouched in this case)
70  * -1 if some other error occurred (`dst' is untouched in this case, too)
71  * author:
72  * Paul Vixie, 1996.
73  */
74 SWITCH_DECLARE(int) switch_inet_pton(int af, const char *src, void *dst)
75 {
76  switch (af) {
77  case AF_INET:
78  return (inet_pton4(src, (unsigned char *) dst));
79 #ifdef ENABLE_IPV6
80 #ifndef AF_INET6
81 #define AF_INET6 (AF_MAX+1) /* just to let this compile */
82 #endif
83  case AF_INET6:
84  return (inet_pton6(src, (unsigned char *) dst));
85 #endif
86  default:
87  errno = EAFNOSUPPORT;
88  return (-1);
89  }
90  /* NOTREACHED */
91 }
92 
93 /* int
94  * inet_pton4(src, dst)
95  * like inet_aton() but without all the hexadecimal and shorthand.
96  * return:
97  * 1 if `src' is a valid dotted quad, else 0.
98  * notice:
99  * does not touch `dst' unless it's returning 1.
100  * author:
101  * Paul Vixie, 1996.
102  */
103 static int inet_pton4(const char *src, unsigned char *dst)
104 {
105  static const char digits[] = "0123456789";
106  int saw_digit, octets, ch;
107  unsigned char tmp[INADDRSZ], *tp;
108 
109  saw_digit = 0;
110  octets = 0;
111  tp = tmp;
112  *tp = 0;
113  while ((ch = *src++) != '\0') {
114  const char *pch;
115 
116  if ((pch = strchr(digits, ch)) != NULL) {
117  unsigned int val = *tp * 10 + (unsigned int) (pch - digits);
118 
119  if (val > 255)
120  return (0);
121  *tp = (unsigned char) val;
122  if (!saw_digit) {
123  if (++octets > 4)
124  return (0);
125  saw_digit = 1;
126  }
127  } else if (ch == '.' && saw_digit) {
128  if (octets == 4)
129  return (0);
130  *++tp = 0;
131  saw_digit = 0;
132  } else
133  return (0);
134  }
135  if (octets < 4)
136  return (0);
137  /* bcopy(tmp, dst, INADDRSZ); */
138  memcpy(dst, tmp, INADDRSZ);
139  return (1);
140 }
141 
142 #ifdef ENABLE_IPV6
143 /* int
144  * inet_pton6(src, dst)
145  * convert presentation level address to network order binary form.
146  * return:
147  * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
148  * notice:
149  * (1) does not touch `dst' unless it's returning 1.
150  * (2) :: in a full address is silently ignored.
151  * credit:
152  * inspired by Mark Andrews.
153  * author:
154  * Paul Vixie, 1996.
155  */
156 static int inet_pton6(const char *src, unsigned char *dst)
157 {
158  static const char xdigits_l[] = "0123456789abcdef", xdigits_u[] = "0123456789ABCDEF";
159  unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
160  const char *xdigits, *curtok;
161  int ch, saw_xdigit;
162  unsigned int val;
163 
164  memset((tp = tmp), 0, IN6ADDRSZ);
165  endp = tp + IN6ADDRSZ;
166  colonp = NULL;
167  /* Leading :: requires some special handling. */
168  if (*src == ':')
169  if (*++src != ':')
170  return (0);
171  curtok = src;
172  saw_xdigit = 0;
173  val = 0;
174  while ((ch = *src++) != '\0') {
175  const char *pch;
176 
177  if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
178  pch = strchr((xdigits = xdigits_u), ch);
179  if (pch != NULL) {
180  val <<= 4;
181  val |= (pch - xdigits);
182  if (val > 0xffff)
183  return (0);
184  saw_xdigit = 1;
185  continue;
186  }
187  if (ch == ':') {
188  curtok = src;
189  if (!saw_xdigit) {
190  if (colonp)
191  return (0);
192  colonp = tp;
193  continue;
194  }
195  if (tp + INT16SZ > endp)
196  return (0);
197  *tp++ = (unsigned char) (val >> 8) & 0xff;
198  *tp++ = (unsigned char) val & 0xff;
199  saw_xdigit = 0;
200  val = 0;
201  continue;
202  }
203  if (ch == '.' && ((tp + INADDRSZ) <= endp) && inet_pton4(curtok, tp) > 0) {
204  tp += INADDRSZ;
205  saw_xdigit = 0;
206  break; /* '\0' was seen by inet_pton4(). */
207  }
208  return (0);
209  }
210  if (saw_xdigit) {
211  if (tp + INT16SZ > endp)
212  return (0);
213  *tp++ = (unsigned char) (val >> 8) & 0xff;
214  *tp++ = (unsigned char) val & 0xff;
215  }
216  if (colonp != NULL) {
217  /*
218  * Since some memmove()'s erroneously fail to handle
219  * overlapping regions, we'll do the shift by hand.
220  */
221  const int n = tp - colonp;
222  int i;
223 
224  for (i = 1; i <= n; i++) {
225  endp[-i] = colonp[n - i];
226  colonp[n - i] = 0;
227  }
228  tp = endp;
229  }
230  if (tp != endp)
231  return (0);
232  /* bcopy(tmp, dst, IN6ADDRSZ); */
233  memcpy(dst, tmp, IN6ADDRSZ);
234  return (1);
235 }
236 #endif /* ENABLE_IPV6 */
237 
238 #endif /* HAVE_INET_PTON */
239 
240 /* For Emacs:
241  * Local Variables:
242  * mode:c
243  * indent-tabs-mode:t
244  * tab-width:4
245  * c-basic-offset:4
246  * End:
247  * For VIM:
248  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
249  */
#define INADDRSZ
Definition: inet_pton.c:40
int switch_inet_pton(int af, const char *src, void *dst)
Definition: inet_pton.c:74
static int inet_pton4(const char *src, unsigned char *dst)
Definition: inet_pton.c:103
#define IN6ADDRSZ
Definition: inet_pton.c:39
#define INT16SZ
Definition: inet_pton.c:41
Main Library Header.
#define SWITCH_DECLARE(type)
memset(buf, 0, buflen)