/[cvs]/eggdrop1.8/src/compat/inet_pton.c
ViewVC logotype

Contents of /eggdrop1.8/src/compat/inet_pton.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Mon Aug 23 21:27:40 2010 UTC (9 years ago) by pseudo
Branch: MAIN
CVS Tags: HEAD
Branch point for: gettext
Changes since 1.1: +5 -3 lines
File MIME type: text/x-chdr
Modified src/compat/ replacements of gethostbyname2() and inet_ntop() to not compile when IPv6 is disabled.
Added a missing header preventing gethostbyname2() from compiling on FreeBSD.
Fixed few lines with wrong indentation.

1 /*
2 * inet_pton.c -- provides inet_pton() if necessary
3 *
4 * $Id: inet_pton.c,v 1.1 2010/08/05 18:12:05 pseudo Exp $
5 */
6 /*
7 * Portions Copyright (C) 2010 Eggheads Development Team
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #include "inet_pton.h"
25
26 #ifndef HAVE_INET_PTON
27 /*
28 * Copyright (c) 1996,1999 by Internet Software Consortium.
29 *
30 * Permission to use, copy, modify, and distribute this software for any
31 * purpose with or without fee is hereby granted, provided that the above
32 * copyright notice and this permission notice appear in all copies.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
35 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
36 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
37 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
38 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
39 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
40 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
41 * SOFTWARE.
42 */
43
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <ctype.h>
47 #include <string.h>
48
49 #define NS_INADDRSZ 4 /* IPv4 T_A */
50 #define NS_IN6ADDRSZ 16 /* IPv6 T_AAAA */
51 #define NS_INT16SZ 2 /* #/bytes of data in a u_int16_t */
52
53 static int inet_pton4 (const char *src, u_char *dst);
54 #ifdef IPV6
55 static int inet_pton6 (const char *src, u_char *dst);
56 #endif
57
58 /* int
59 * inet_pton(af, src, dst)
60 * convert from presentation format (which usually means ASCII printable)
61 * to network format (which is usually some kind of binary format).
62 * return:
63 * 1 if the address was valid for the specified address family
64 * 0 if the address wasn't valid (`dst' is untouched in this case)
65 * -1 if some other error occurred (`dst' is untouched in this case, too)
66 * author:
67 * Paul Vixie, 1996.
68 */
69 int inet_pton(af, src, dst)
70 int af;
71 const char *src;
72 void *dst;
73 {
74 switch (af) {
75 case AF_INET:
76 return (inet_pton4(src, dst));
77 #ifdef IPV6
78 case AF_INET6:
79 return (inet_pton6(src, dst));
80 #endif
81 default:
82 return -1;
83 }
84 /* NOTREACHED */
85 }
86
87 /* int
88 * inet_pton4(src, dst)
89 * like inet_aton() but without all the hexadecimal and shorthand.
90 * return:
91 * 1 if `src' is a valid dotted quad, else 0.
92 * notice:
93 * does not touch `dst' unless it's returning 1.
94 * author:
95 * Paul Vixie, 1996.
96 */
97 static int
98 inet_pton4(src, dst)
99 const char *src;
100 u_char *dst;
101 {
102 int saw_digit, octets, ch;
103 u_char tmp[NS_INADDRSZ], *tp;
104
105 saw_digit = 0;
106 octets = 0;
107 *(tp = tmp) = 0;
108 while ((ch = *src++) != '\0') {
109
110 if (ch >= '0' && ch <= '9') {
111 u_int new = *tp * 10 + (ch - '0');
112
113 if (new > 255)
114 return (0);
115 *tp = new;
116 if (! saw_digit) {
117 if (++octets > 4)
118 return (0);
119 saw_digit = 1;
120 }
121 } else if (ch == '.' && saw_digit) {
122 if (octets == 4)
123 return (0);
124 *++tp = 0;
125 saw_digit = 0;
126 } else
127 return (0);
128 }
129 if (octets < 4)
130 return (0);
131 memcpy(dst, tmp, NS_INADDRSZ);
132 return (1);
133 }
134
135 /* int
136 * inet_pton6(src, dst)
137 * convert presentation level address to network order binary form.
138 * return:
139 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
140 * notice:
141 * (1) does not touch `dst' unless it's returning 1.
142 * (2) :: in a full address is silently ignored.
143 * credit:
144 * inspired by Mark Andrews.
145 * author:
146 * Paul Vixie, 1996.
147 */
148 #ifdef IPV6
149 static int
150 inet_pton6(src, dst)
151 const char *src;
152 u_char *dst;
153 {
154 static const char xdigits[] = "0123456789abcdef";
155 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
156 const char *curtok;
157 int ch, saw_xdigit;
158 u_int val;
159
160 tp = memset(tmp, '\0', NS_IN6ADDRSZ);
161 endp = tp + NS_IN6ADDRSZ;
162 colonp = NULL;
163 /* Leading :: requires some special handling. */
164 if (*src == ':')
165 if (*++src != ':')
166 return (0);
167 curtok = src;
168 saw_xdigit = 0;
169 val = 0;
170 while ((ch = tolower (*src++)) != '\0') {
171 const char *pch;
172
173 pch = strchr(xdigits, ch);
174 if (pch != NULL) {
175 val <<= 4;
176 val |= (pch - xdigits);
177 if (val > 0xffff)
178 return (0);
179 saw_xdigit = 1;
180 continue;
181 }
182 if (ch == ':') {
183 curtok = src;
184 if (!saw_xdigit) {
185 if (colonp)
186 return (0);
187 colonp = tp;
188 continue;
189 } else if (*src == '\0') {
190 return (0);
191 }
192 if (tp + NS_INT16SZ > endp)
193 return (0);
194 *tp++ = (u_char) (val >> 8) & 0xff;
195 *tp++ = (u_char) val & 0xff;
196 saw_xdigit = 0;
197 val = 0;
198 continue;
199 }
200 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
201 inet_pton4(curtok, tp) > 0) {
202 tp += NS_INADDRSZ;
203 saw_xdigit = 0;
204 break; /* '\0' was seen by inet_pton4(). */
205 }
206 return (0);
207 }
208 if (saw_xdigit) {
209 if (tp + NS_INT16SZ > endp)
210 return (0);
211 *tp++ = (u_char) (val >> 8) & 0xff;
212 *tp++ = (u_char) val & 0xff;
213 }
214 if (colonp != NULL) {
215 /*
216 * Since some memmove()'s erroneously fail to handle
217 * overlapping regions, we'll do the shift by hand.
218 */
219 const int n = tp - colonp;
220 int i;
221
222 if (tp == endp)
223 return (0);
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 memcpy(dst, tmp, NS_IN6ADDRSZ);
233 return (1);
234 }
235 #endif /* IPV6 */
236
237 #endif /* HAVE_INET_PTON */

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23