summaryrefslogtreecommitdiff
path: root/legacy/evil
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2011-05-19 12:00:06 +0000
committerVincent Torri <vincent.torri@gmail.com>2011-05-19 12:00:06 +0000
commit43955a6d53e7088157e8a3cc05a795bc9795ea6b (patch)
treeb849f68091b9f2be840b36f85713ddd549b73ae7 /legacy/evil
parent4dd1d5cf62e51e81e4eac3a9f71a42fe1819600b (diff)
Evil:
* src/lib/dlfcn/dlfcn.c: change '/' to '\' in paths * src/lib/evil_inet.c: * src/lib/evil_inet.h: * src/lib/Evil.h: * src/lib/Makefile.am: * AUTHORS: add inet_pton() function. Implementation from curl library. See AUTHORS. SVN revision: 59530
Diffstat (limited to 'legacy/evil')
-rw-r--r--legacy/evil/AUTHORS2
-rw-r--r--legacy/evil/ChangeLog13
-rw-r--r--legacy/evil/src/lib/Evil.h1
-rw-r--r--legacy/evil/src/lib/Makefile.am2
-rw-r--r--legacy/evil/src/lib/dlfcn/dlfcn.c4
-rw-r--r--legacy/evil/src/lib/evil_inet.c454
-rw-r--r--legacy/evil/src/lib/evil_inet.h41
7 files changed, 515 insertions, 2 deletions
diff --git a/legacy/evil/AUTHORS b/legacy/evil/AUTHORS
index 226283dce1..125597df1f 100644
--- a/legacy/evil/AUTHORS
+++ b/legacy/evil/AUTHORS
@@ -3,3 +3,5 @@ Main Authors:
3 Tor Lillqvist <tml at iki dot fi> (pipe function) 3 Tor Lillqvist <tml at iki dot fi> (pipe function)
4 Lance Fetters 4 Lance Fetters
5 Vincent Richomme <vrichomme at smartmobili dot com> 5 Vincent Richomme <vrichomme at smartmobili dot com>
6 Paul Vixie (ISC) (inet_pton function)
7 Daniel Stenberg (inet_pton function)
diff --git a/legacy/evil/ChangeLog b/legacy/evil/ChangeLog
index cea9615013..6cf49e2f03 100644
--- a/legacy/evil/ChangeLog
+++ b/legacy/evil/ChangeLog
@@ -1,3 +1,16 @@
12011-05-19 Vincent Torri <doursse at users dot sf dot net>
2
3 * src/lib/dlfcn/dlfcn.c:
4 change '/' to '\' in paths
5
6 * src/lib/evil_inet.c:
7 * src/lib/evil_inet.h:
8 * src/lib/Evil.h:
9 * src/lib/Makefile.am:
10 * AUTHORS:
11 add inet_pton() function. Implementation from curl library.
12 See AUTHORS.
13
12011-04-20 Vincent Torri <doursse at users dot sf dot net> 142011-04-20 Vincent Torri <doursse at users dot sf dot net>
2 15
3 * evil_stdlib.c: 16 * evil_stdlib.c:
diff --git a/legacy/evil/src/lib/Evil.h b/legacy/evil/src/lib/Evil.h
index ad8198116b..6a8087506e 100644
--- a/legacy/evil/src/lib/Evil.h
+++ b/legacy/evil/src/lib/Evil.h
@@ -119,6 +119,7 @@ typedef unsigned long gid_t;
119 119
120 120
121#include "evil_fcntl.h" 121#include "evil_fcntl.h"
122#include "evil_inet.h"
122#include "evil_langinfo.h" 123#include "evil_langinfo.h"
123#include "evil_libgen.h" 124#include "evil_libgen.h"
124#include "evil_main.h" 125#include "evil_main.h"
diff --git a/legacy/evil/src/lib/Makefile.am b/legacy/evil/src/lib/Makefile.am
index bca6a65032..a48bcfec48 100644
--- a/legacy/evil/src/lib/Makefile.am
+++ b/legacy/evil/src/lib/Makefile.am
@@ -8,6 +8,7 @@ lib_LTLIBRARIES = libevil.la
8include_HEADERS = \ 8include_HEADERS = \
9Evil.h \ 9Evil.h \
10evil_fcntl.h \ 10evil_fcntl.h \
11evil_inet.h \
11evil_langinfo.h \ 12evil_langinfo.h \
12evil_libgen.h \ 13evil_libgen.h \
13evil_main.h \ 14evil_main.h \
@@ -31,6 +32,7 @@ evil_dirent.c \
31evil_fcntl.c \ 32evil_fcntl.c \
32evil_fnmatch.c \ 33evil_fnmatch.c \
33evil_fnmatch_list_of_states.c \ 34evil_fnmatch_list_of_states.c \
35evil_inet.c \
34evil_langinfo.c \ 36evil_langinfo.c \
35evil_libgen.c \ 37evil_libgen.c \
36evil_main.c \ 38evil_main.c \
diff --git a/legacy/evil/src/lib/dlfcn/dlfcn.c b/legacy/evil/src/lib/dlfcn/dlfcn.c
index 8f4d387be3..782d61c6a2 100644
--- a/legacy/evil/src/lib/dlfcn/dlfcn.c
+++ b/legacy/evil/src/lib/dlfcn/dlfcn.c
@@ -180,11 +180,11 @@ dladdr (const void *addr __UNUSED__, Dl_info *info)
180 path[PATH_MAX - 1] = '\0'; 180 path[PATH_MAX - 1] = '\0';
181 } 181 }
182 182
183 /* replace '\' by '/' */ 183 /* replace '/' by '\' */
184 tmp = path; 184 tmp = path;
185 while (*tmp) 185 while (*tmp)
186 { 186 {
187 if (*tmp == '\\') *tmp = '/'; 187 if (*tmp == '/') *tmp = '\\';
188 tmp++; 188 tmp++;
189 } 189 }
190 190
diff --git a/legacy/evil/src/lib/evil_inet.c b/legacy/evil/src/lib/evil_inet.c
new file mode 100644
index 0000000000..fa3732a72f
--- /dev/null
+++ b/legacy/evil/src/lib/evil_inet.c
@@ -0,0 +1,454 @@
1
2/*
3 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
4 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19/*
20 * Modifications: Vincent Torri, for the integration in Evil
21 * - modification of the name of some functions
22 * * modification of the management of the error
23 */
24
25#ifdef HAVE_CONFIG_H
26# include "config.h"
27#endif /* HAVE_CONFIG_H */
28
29#include <errno.h>
30#include <ctype.h>
31
32#ifndef WIN32_LEAN_AND_MEAN
33# define WIN32_LEAN_AND_MEAN
34#endif
35#include <winsock2.h>
36#undef WIN32_LEAN_AND_MEAN
37
38#include "Evil.h"
39#include "evil_private.h"
40
41
42#define ERRNO ((int)GetLastError())
43#define SET_ERRNO(x) (SetLastError((DWORD)(x)))
44
45#define ISDIGIT(x) (isdigit((int) ((unsigned char)x)))
46#define ISXDIGIT(x) (isxdigit((int) ((unsigned char)x)))
47#define ISUPPER(x) (isupper((int) ((unsigned char)x)))
48
49#define NS_IN6ADDRSZ 16
50#define NS_INT16SZ 2
51#define NS_INADDRSZ sizeof(IN_ADDR)
52
53
54struct ares_in6_addr {
55 union {
56 unsigned char _S6_u8[16];
57 } _S6_un;
58};
59
60const struct ares_in6_addr ares_in6addr_any = { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
61
62
63/*
64 * static int
65 * inet_net_pton_ipv4(src, dst, size)
66 * convert IPv4 network number from presentation to network format.
67 * accepts hex octets, hex strings, decimal octets, and /CIDR.
68 * "size" is in bytes and describes "dst".
69 * return:
70 * number of bits, either imputed classfully or specified with /CIDR,
71 * or -1 if some failure occurred (check errno). ENOENT means it was
72 * not an IPv4 network specification.
73 * note:
74 * network byte order assumed. this means 192.5.5.240/28 has
75 * 0b11110000 in its fourth octet.
76 * note:
77 * On Windows we store the error in the thread errno, not
78 * in the winsock error code. This is to avoid loosing the
79 * actual last winsock error. So use macro ERRNO to fetch the
80 * errno this funtion sets when returning (-1), not SOCKERRNO.
81 * author:
82 * Paul Vixie (ISC), June 1996
83 */
84static int
85inet_net_pton_ipv4(const char *src, unsigned char *dst, size_t size)
86{
87 static const char xdigits[] = "0123456789abcdef";
88 static const char digits[] = "0123456789";
89 int n, ch, tmp = 0, dirty, bits;
90 const unsigned char *odst = dst;
91
92 ch = *src++;
93 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
94 && ISXDIGIT(src[1])) {
95 /* Hexadecimal: Eat nybble string. */
96 if (!size)
97 goto emsgsize;
98 dirty = 0;
99 src++; /* skip x or X. */
100 while ((ch = *src++) != '\0' && ISXDIGIT(ch)) {
101 if (ISUPPER(ch))
102 ch = tolower(ch);
103 n = (int)(strchr(xdigits, ch) - xdigits);
104 if (dirty == 0)
105 tmp = n;
106 else
107 tmp = (tmp << 4) | n;
108 if (++dirty == 2) {
109 if (!size--)
110 goto emsgsize;
111 *dst++ = (unsigned char) tmp;
112 dirty = 0;
113 }
114 }
115 if (dirty) { /* Odd trailing nybble? */
116 if (!size--)
117 goto emsgsize;
118 *dst++ = (unsigned char) (tmp << 4);
119 }
120 } else if (ISDIGIT(ch)) {
121 /* Decimal: eat dotted digit string. */
122 for (;;) {
123 tmp = 0;
124 do {
125 n = (int)(strchr(digits, ch) - digits);
126 tmp *= 10;
127 tmp += n;
128 if (tmp > 255)
129 goto enoent;
130 } while ((ch = *src++) != '\0' &&
131 ISDIGIT(ch));
132 if (!size--)
133 goto emsgsize;
134 *dst++ = (unsigned char) tmp;
135 if (ch == '\0' || ch == '/')
136 break;
137 if (ch != '.')
138 goto enoent;
139 ch = *src++;
140 if (!ISDIGIT(ch))
141 goto enoent;
142 }
143 } else
144 goto enoent;
145
146 bits = -1;
147 if (ch == '/' &&
148 ISDIGIT(src[0]) && dst > odst) {
149 /* CIDR width specifier. Nothing can follow it. */
150 ch = *src++; /* Skip over the /. */
151 bits = 0;
152 do {
153 n = (int)(strchr(digits, ch) - digits);
154 bits *= 10;
155 bits += n;
156 } while ((ch = *src++) != '\0' && ISDIGIT(ch));
157 if (ch != '\0')
158 goto enoent;
159 if (bits > 32)
160 goto emsgsize;
161 }
162
163 /* Firey death and destruction unless we prefetched EOS. */
164 if (ch != '\0')
165 goto enoent;
166
167 /* If nothing was written to the destination, we found no address. */
168 if (dst == odst)
169 goto enoent;
170 /* If no CIDR spec was given, infer width from net class. */
171 if (bits == -1) {
172 if (*odst >= 240) /* Class E */
173 bits = 32;
174 else if (*odst >= 224) /* Class D */
175 bits = 8;
176 else if (*odst >= 192) /* Class C */
177 bits = 24;
178 else if (*odst >= 128) /* Class B */
179 bits = 16;
180 else /* Class A */
181 bits = 8;
182 /* If imputed mask is narrower than specified octets, widen. */
183 if (bits < ((dst - odst) * 8))
184 bits = (int)(dst - odst) * 8;
185 /*
186 * If there are no additional bits specified for a class D
187 * address adjust bits to 4.
188 */
189 if (bits == 8 && *odst == 224)
190 bits = 4;
191 }
192 /* Extend network to cover the actual mask. */
193 while (bits > ((dst - odst) * 8)) {
194 if (!size--)
195 goto emsgsize;
196 *dst++ = '\0';
197 }
198 return (bits);
199
200 enoent:
201 SET_ERRNO(ENOENT);
202 return (-1);
203
204 emsgsize:
205 SET_ERRNO(EMSGSIZE);
206 return (-1);
207}
208
209static int
210getbits(const char *src, int *bitsp)
211{
212 static const char digits[] = "0123456789";
213 int n;
214 int val;
215 char ch;
216
217 val = 0;
218 n = 0;
219 while ((ch = *src++) != '\0') {
220 const char *pch;
221
222 pch = strchr(digits, ch);
223 if (pch != NULL) {
224 if (n++ != 0 && val == 0) /* no leading zeros */
225 return (0);
226 val *= 10;
227 val += (pch - digits);
228 if (val > 128) /* range */
229 return (0);
230 continue;
231 }
232 return (0);
233 }
234 if (n == 0)
235 return (0);
236 *bitsp = val;
237 return (1);
238}
239
240static int
241getv4(const char *src, unsigned char *dst, int *bitsp)
242{
243 static const char digits[] = "0123456789";
244 unsigned char *odst = dst;
245 int n;
246 unsigned int val;
247 char ch;
248
249 val = 0;
250 n = 0;
251 while ((ch = *src++) != '\0') {
252 const char *pch;
253
254 pch = strchr(digits, ch);
255 if (pch != NULL) {
256 if (n++ != 0 && val == 0) /* no leading zeros */
257 return (0);
258 val *= 10;
259 val += (pch - digits);
260 if (val > 255) /* range */
261 return (0);
262 continue;
263 }
264 if (ch == '.' || ch == '/') {
265 if (dst - odst > 3) /* too many octets? */
266 return (0);
267 *dst++ = (unsigned char)val;
268 if (ch == '/')
269 return (getbits(src, bitsp));
270 val = 0;
271 n = 0;
272 continue;
273 }
274 return (0);
275 }
276 if (n == 0)
277 return (0);
278 if (dst - odst > 3) /* too many octets? */
279 return (0);
280 *dst++ = (unsigned char)val;
281 return (1);
282}
283
284static int
285inet_net_pton_ipv6(const char *src, unsigned char *dst, size_t size)
286{
287 static const char xdigits_l[] = "0123456789abcdef",
288 xdigits_u[] = "0123456789ABCDEF";
289 unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
290 const char *xdigits, *curtok;
291 int ch, saw_xdigit;
292 unsigned int val;
293 int digits;
294 int bits;
295 size_t bytes;
296 int words;
297 int ipv4;
298
299 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
300 endp = tp + NS_IN6ADDRSZ;
301 colonp = NULL;
302 /* Leading :: requires some special handling. */
303 if (*src == ':')
304 if (*++src != ':')
305 goto enoent;
306 curtok = src;
307 saw_xdigit = 0;
308 val = 0;
309 digits = 0;
310 bits = -1;
311 ipv4 = 0;
312 while ((ch = *src++) != '\0') {
313 const char *pch;
314
315 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
316 pch = strchr((xdigits = xdigits_u), ch);
317 if (pch != NULL) {
318 val <<= 4;
319 val |= (pch - xdigits);
320 if (++digits > 4)
321 goto enoent;
322 saw_xdigit = 1;
323 continue;
324 }
325 if (ch == ':') {
326 curtok = src;
327 if (!saw_xdigit) {
328 if (colonp)
329 goto enoent;
330 colonp = tp;
331 continue;
332 } else if (*src == '\0')
333 goto enoent;
334 if (tp + NS_INT16SZ > endp)
335 return (0);
336 *tp++ = (unsigned char)((val >> 8) & 0xff);
337 *tp++ = (unsigned char)(val & 0xff);
338 saw_xdigit = 0;
339 digits = 0;
340 val = 0;
341 continue;
342 }
343 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
344 getv4(curtok, tp, &bits) > 0) {
345 tp += NS_INADDRSZ;
346 saw_xdigit = 0;
347 ipv4 = 1;
348 break; /* '\0' was seen by inet_pton4(). */
349 }
350 if (ch == '/' && getbits(src, &bits) > 0)
351 break;
352 goto enoent;
353 }
354 if (saw_xdigit) {
355 if (tp + NS_INT16SZ > endp)
356 goto enoent;
357 *tp++ = (unsigned char)((val >> 8) & 0xff);
358 *tp++ = (unsigned char)(val & 0xff);
359 }
360 if (bits == -1)
361 bits = 128;
362
363 words = (bits + 15) / 16;
364 if (words < 2)
365 words = 2;
366 if (ipv4)
367 words = 8;
368 endp = tmp + 2 * words;
369
370 if (colonp != NULL) {
371 /*
372 * Since some memmove()'s erroneously fail to handle
373 * overlapping regions, we'll do the shift by hand.
374 */
375 const ssize_t n = tp - colonp;
376 ssize_t i;
377
378 if (tp == endp)
379 goto enoent;
380 for (i = 1; i <= n; i++) {
381 *(endp - i) = *(colonp + n - i);
382 *(colonp + n - i) = 0;
383 }
384 tp = endp;
385 }
386 if (tp != endp)
387 goto enoent;
388
389 bytes = (bits + 7) / 8;
390 if (bytes > size)
391 goto emsgsize;
392 memcpy(dst, tmp, bytes);
393 return (bits);
394
395 enoent:
396 SET_ERRNO(ENOENT);
397 return (-1);
398
399 emsgsize:
400 SET_ERRNO(EMSGSIZE);
401 return (-1);
402}
403
404/*
405 * int
406 * inet_net_pton(af, src, dst, size)
407 * convert network number from presentation to network format.
408 * accepts hex octets, hex strings, decimal octets, and /CIDR.
409 * "size" is in bytes and describes "dst".
410 * return:
411 * number of bits, either imputed classfully or specified with /CIDR,
412 * or -1 if some failure occurred (check errno). ENOENT means it was
413 * not a valid network specification.
414 * note:
415 * On Windows we store the error in the thread errno, not
416 * in the winsock error code. This is to avoid loosing the
417 * actual last winsock error. So use macro ERRNO to fetch the
418 * errno this funtion sets when returning (-1), not SOCKERRNO.
419 * author:
420 * Paul Vixie (ISC), June 1996
421 */
422static int
423ares_inet_net_pton(int af, const char *src, void *dst, size_t size)
424{
425 switch (af) {
426 case AF_INET:
427 return (inet_net_pton_ipv4(src, dst, size));
428 case AF_INET6:
429 return (inet_net_pton_ipv6(src, dst, size));
430 default:
431 SET_ERRNO(EAFNOSUPPORT);
432 return (-1);
433 }
434}
435
436int evil_inet_pton(int af, const char *src, void *dst)
437{
438 int result;
439 size_t size;
440
441 if (af == AF_INET)
442 size = sizeof(struct in_addr);
443 else if (af == AF_INET6)
444 size = sizeof(struct ares_in6_addr);
445 else
446 {
447 SET_ERRNO(EAFNOSUPPORT);
448 return -1;
449 }
450 result = ares_inet_net_pton(af, src, dst, size);
451 if (result == -1 && ERRNO == ENOENT)
452 return 0;
453 return (result > -1 ? 1 : -1);
454}
diff --git a/legacy/evil/src/lib/evil_inet.h b/legacy/evil/src/lib/evil_inet.h
new file mode 100644
index 0000000000..abc59478c1
--- /dev/null
+++ b/legacy/evil/src/lib/evil_inet.h
@@ -0,0 +1,41 @@
1
2/* Copyright (C) 2005 by Daniel Stenberg
3 *
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of M.I.T. not be used in
10 * advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.
12 * M.I.T. makes no representations about the suitability of
13 * this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
15 */
16
17#ifndef __EVIL_INET_H__
18#define __EVIL_INET_H__
19
20
21/**
22 * @file evil_inet.h
23 * @brief The file that provides functions ported from Unix in arpa/inet.h.
24 * @defgroup Evil_Inet_Group Inet.h functions
25 *
26 * This header provides functions ported from Unix in stdio.h.
27 *
28 * @{
29 */
30
31EAPI int evil_inet_pton(int af, const char *src, void *dst);
32
33#define inet_pton(x,y,z) evil_inet_pton(x,y,z)
34
35
36/**
37 * @}
38 */
39
40
41#endif /* __EVIL_INET_H__ */