doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #include "setup.h"
20 
21 #ifndef HAVE_INET_PTON
22 
23 #ifdef HAVE_SYS_PARAM_H
24 #include <sys/param.h>
25 #endif
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
31 #endif
32 #ifdef HAVE_NETINET_IN_H
33 #include <netinet/in.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 #include <arpa/inet.h>
37 #endif
38 #include <string.h>
39 #include <errno.h>
40 
41 #include "inet_pton.h"
42 
43 #define IN6ADDRSZ 16
44 #define INADDRSZ 4
45 #define INT16SZ 2
46 
47 #ifdef WIN32
48 #define EAFNOSUPPORT WSAEAFNOSUPPORT
49 #endif
50 
51 /*
52  * WARNING: Don't even consider trying to compile this on a system where
53  * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
54  */
55 
56 static int inet_pton4(const char *src, unsigned char *dst);
57 #ifdef ENABLE_IPV6
58 static int inet_pton6(const char *src, unsigned char *dst);
59 #endif
60 
61 /* int
62  * inet_pton(af, src, dst)
63  * convert from presentation format (which usually means ASCII printable)
64  * to network format (which is usually some kind of binary format).
65  * return:
66  * 1 if the address was valid for the specified address family
67  * 0 if the address wasn't valid (`dst' is untouched in this case)
68  * -1 if some other error occurred (`dst' is untouched in this case, too)
69  * author:
70  * Paul Vixie, 1996.
71  */
72 int
73 Curl_inet_pton(int af, const char *src, void *dst)
74 {
75  switch (af) {
76  case AF_INET:
77  return (inet_pton4(src, dst));
78 #ifdef ENABLE_IPV6
79 #ifndef AF_INET6
80 #define AF_INET6 AF_MAX+1 /* just to let this compile */
81 #endif
82  case AF_INET6:
83  return (inet_pton6(src, dst));
84 #endif
85  default:
86  errno = EAFNOSUPPORT;
87  return (-1);
88  }
89  /* NOTREACHED */
90 }
91 
92 /* int
93  * inet_pton4(src, dst)
94  * like inet_aton() but without all the hexadecimal and shorthand.
95  * return:
96  * 1 if `src' is a valid dotted quad, else 0.
97  * notice:
98  * does not touch `dst' unless it's returning 1.
99  * author:
100  * Paul Vixie, 1996.
101  */
102 static int
103 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) = 0;
112  while ((ch = *src++) != '\0') {
113  const char *pch;
114 
115  if ((pch = strchr(digits, ch)) != NULL) {
116  u_int new = *tp * 10 + (pch - digits);
117 
118  if (new > 255)
119  return (0);
120  *tp = new;
121  if (! saw_digit) {
122  if (++octets > 4)
123  return (0);
124  saw_digit = 1;
125  }
126  } else if (ch == '.' && saw_digit) {
127  if (octets == 4)
128  return (0);
129  *++tp = 0;
130  saw_digit = 0;
131  } else
132  return (0);
133  }
134  if (octets < 4)
135  return (0);
136  /* bcopy(tmp, dst, INADDRSZ); */
137  memcpy(dst, tmp, INADDRSZ);
138  return (1);
139 }
140 
141 #ifdef ENABLE_IPV6
142 /* int
143  * inet_pton6(src, dst)
144  * convert presentation level address to network order binary form.
145  * return:
146  * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
147  * notice:
148  * (1) does not touch `dst' unless it's returning 1.
149  * (2) :: in a full address is silently ignored.
150  * credit:
151  * inspired by Mark Andrews.
152  * author:
153  * Paul Vixie, 1996.
154  */
155 static int
156 inet_pton6(const char *src, unsigned char *dst)
157 {
158  static const char xdigits_l[] = "0123456789abcdef",
159  xdigits_u[] = "0123456789ABCDEF";
160  unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
161  const char *xdigits, *curtok;
162  int ch, saw_xdigit;
163  u_int val;
164 
165  memset((tp = tmp), 0, IN6ADDRSZ);
166  endp = tp + IN6ADDRSZ;
167  colonp = NULL;
168  /* Leading :: requires some special handling. */
169  if (*src == ':')
170  if (*++src != ':')
171  return (0);
172  curtok = src;
173  saw_xdigit = 0;
174  val = 0;
175  while ((ch = *src++) != '\0') {
176  const char *pch;
177 
178  if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
179  pch = strchr((xdigits = xdigits_u), ch);
180  if (pch != NULL) {
181  val <<= 4;
182  val |= (pch - xdigits);
183  if (val > 0xffff)
184  return (0);
185  saw_xdigit = 1;
186  continue;
187  }
188  if (ch == ':') {
189  curtok = src;
190  if (!saw_xdigit) {
191  if (colonp)
192  return (0);
193  colonp = tp;
194  continue;
195  }
196  if (tp + INT16SZ > endp)
197  return (0);
198  *tp++ = (unsigned char) (val >> 8) & 0xff;
199  *tp++ = (unsigned char) val & 0xff;
200  saw_xdigit = 0;
201  val = 0;
202  continue;
203  }
204  if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
205  inet_pton4(curtok, tp) > 0) {
206  tp += INADDRSZ;
207  saw_xdigit = 0;
208  break; /* '\0' was seen by inet_pton4(). */
209  }
210  return (0);
211  }
212  if (saw_xdigit) {
213  if (tp + INT16SZ > endp)
214  return (0);
215  *tp++ = (unsigned char) (val >> 8) & 0xff;
216  *tp++ = (unsigned char) val & 0xff;
217  }
218  if (colonp != NULL) {
219  /*
220  * Since some memmove()'s erroneously fail to handle
221  * overlapping regions, we'll do the shift by hand.
222  */
223  const int n = tp - colonp;
224  int i;
225 
226  for (i = 1; i <= n; i++) {
227  endp[- i] = colonp[n - i];
228  colonp[n - i] = 0;
229  }
230  tp = endp;
231  }
232  if (tp != endp)
233  return (0);
234  /* bcopy(tmp, dst, IN6ADDRSZ); */
235  memcpy(dst, tmp, IN6ADDRSZ);
236  return (1);
237 }
238 #endif /* ENABLE_IPV6 */
239 
240 #endif /* HAVE_INET_PTON */
#define INADDRSZ
Definition: inet_pton.c:44
GLenum GLsizei n
Definition: glext.h:3705
GLuint src
Definition: glext.h:5390
int i
Definition: process.py:33
GLuint dst
Definition: glext.h:5285
#define NULL
Definition: Lib.h:88
int Curl_inet_pton(int af, const char *src, void *dst)
Definition: inet_pton.c:73
#define IN6ADDRSZ
Definition: inet_pton.c:43
#define INT16SZ
Definition: inet_pton.c:45