/[svn]/hopm/branches/1.0.x/src/libopm/src/inet.c
ViewVC logotype

Annotation of /hopm/branches/1.0.x/src/libopm/src/inet.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5154 - (hide annotations)
Fri Dec 26 17:03:43 2014 UTC (5 years, 2 months ago) by michael
File MIME type: text/x-chdr
File size: 6598 byte(s)
- Removed inet_ntop related bloat

1 michael 5052 /*
2     Copyright (C) 2002 by the past and present ircd coders, and others.
3    
4     This program is free software; you can redistribute it and/or
5     modify it under the terms of the GNU General Public License
6     as published by the Free Software Foundation; either version 2
7     of the License, or (at your option) any later version.
8    
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13    
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to
16    
17     The Free Software Foundation, Inc.
18     59 Temple Place - Suite 330
19     Boston, MA 02111-1307, USA.
20    
21     */
22    
23     /*
24     * This code is borrowed from ircd-hybrid version 7
25     * -TimeMr14C
26     */
27    
28     #include "setup.h"
29    
30     #include <errno.h>
31     #include <stdio.h>
32     #include <assert.h>
33     #if STDC_HEADERS
34     # include <string.h>
35     # include <stdlib.h>
36     #endif
37     #ifdef HAVE_STRINGS_H
38     # include <strings.h>
39     #endif
40     #include <sys/types.h>
41     #include <sys/socket.h>
42     #include <netinet/in.h>
43     #include <netdb.h>
44    
45     #include "inet.h"
46     #include "opm.h"
47    
48     #ifndef INADDRSZ
49     #define INADDRSZ 4
50     #endif
51    
52     #ifdef IPV6
53     #ifndef IN6ADDRSZ
54     #define IN6ADDRSZ 16
55     #endif
56     #endif
57    
58    
59     #ifndef INT16SZ
60     #define INT16SZ 2
61     #endif
62    
63     #ifdef IPV6
64     #define HOSTIPLEN 53
65     #else
66     #define HOSTIPLEN 15
67     #endif
68    
69    
70     extern const unsigned char ToLowerTab[];
71     #define ToLower(c) (ToLowerTab[(unsigned char)(c)])
72    
73     /*
74     * WARNING: Don't even consider trying to compile this on a system where
75     * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
76     */
77    
78     /* int
79     * inet_pton(af, src, dst)
80     * convert from presentation format (which usually means ASCII printable)
81     * to network format (which is usually some kind of binary format).
82     * return:
83     * 1 if the address was valid for the specified address family
84     * 0 if the address wasn't valid (`dst' is untouched in this case)
85     * -1 if some other error occurred (`dst' is untouched in this case, too)
86     * author:
87     * Paul Vixie, 1996.
88     */
89    
90     /* int
91     * inet_pton4(src, dst)
92     * like inet_aton() but without all the hexadecimal and shorthand.
93     * return:
94     * 1 if `src' is a valid dotted quad, else 0.
95     * notice:
96     * does not touch `dst' unless it's returning 1.
97     * author:
98     * Paul Vixie, 1996.
99     */
100    
101     #ifndef HAVE_INET_PTON
102    
103     static int inet_pton4(src, dst)
104     const char *src;
105     unsigned char *dst;
106     {
107     int saw_digit, octets, ch;
108     unsigned char tmp[INADDRSZ], *tp;
109    
110     saw_digit = 0;
111     octets = 0;
112     *(tp = tmp) = 0;
113     while ((ch = *src++) != '\0') {
114    
115     if (ch >= '0' && ch <= '9') {
116     unsigned int new = *tp * 10 + (ch - '0');
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     memcpy(dst, tmp, INADDRSZ);
137     return (1);
138     }
139    
140     #ifdef IPV6
141     /* int
142     * inet_pton6(src, dst)
143     * convert presentation level address to network order binary form.
144     * return:
145     * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
146     * notice:
147     * (1) does not touch `dst' unless it's returning 1.
148     * (2) :: in a full address is silently ignored.
149     * credit:
150     * inspired by Mark Andrews.
151     * author:
152     * Paul Vixie, 1996.
153     */
154    
155     static int inet_pton6(src, dst)
156     const char *src;
157     unsigned char *dst;
158     {
159     static const char xdigits[] = "0123456789abcdef";
160     unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
161     const char *curtok;
162     int ch, saw_xdigit;
163     unsigned int val;
164    
165     tp = memset(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 = ToLower(*src++)) != '\0') {
176     const char *pch;
177    
178     pch = strchr(xdigits, 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     } else if (*src == '\0') {
195     return (0);
196     }
197     if (tp + INT16SZ > endp)
198     return (0);
199     *tp++ = (unsigned char) (val >> 8) & 0xff;
200     *tp++ = (unsigned char) val & 0xff;
201     saw_xdigit = 0;
202     val = 0;
203     continue;
204     }
205    
206     if (*src != '\0' && ch == '.') {
207     if (((tp + INADDRSZ) <= endp) && inet_pton4(curtok, tp) > 0) {
208     tp += INADDRSZ;
209     saw_xdigit = 0;
210     break; /* '\0' was seen by inet_pton4(). */
211     }
212     } else
213     continue;
214     return (0);
215     }
216     if (saw_xdigit) {
217     if (tp + INT16SZ > endp)
218     return (0);
219     *tp++ = (unsigned char) (val >> 8) & 0xff;
220     *tp++ = (unsigned char) val & 0xff;
221     }
222     if (colonp != NULL) {
223     /*
224     * Since some memmove()'s erroneously fail to handle
225     * overlapping regions, we'll do the shift by hand.
226     */
227     const int n = tp - colonp;
228     int i;
229    
230     if (tp == endp)
231     return (0);
232     for (i = 1; i <= n; i++) {
233     endp[-i] = colonp[n - i];
234     colonp[n - i] = 0;
235     }
236     tp = endp;
237     }
238     if (tp != endp)
239     return (0);
240     memcpy(dst, tmp, IN6ADDRSZ);
241     return (1);
242     }
243     #endif /* IPv6 */
244    
245     int inet_pton(af, src, dst)
246     int af;
247     const char *src;
248     void *dst;
249     {
250     switch (af) {
251     case AF_INET:
252     return (inet_pton4(src, dst));
253     #ifdef IPV6
254     case AF_INET6:
255     /* Somebody might have passed as an IPv4 address this is sick but it works */
256     if (inet_pton4(src, dst)) {
257     char tmp[HOSTIPLEN];
258     sprintf(tmp, "::ffff:%s", src);
259     return (inet_pton6(tmp, dst));
260     } else
261     return (inet_pton6(src, dst));
262     #endif /* IPv6 */
263     default:
264     return (-1);
265     }
266     /* NOTREACHED */
267     }
268    
269     #endif

Properties

Name Value
svn:eol-style native
svn:keywords Id

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.26