summaryrefslogtreecommitdiff
path: root/legacy/evil
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2011-10-05 20:23:24 +0000
committerVincent Torri <vincent.torri@gmail.com>2011-10-05 20:23:24 +0000
commit2c6a86fb6d39b46d10cd61f5baa7e409b3cecd8e (patch)
tree33d7743353292699dfdb093b8f1016c9b52b1537 /legacy/evil
parent6ecdda512122eb5d405eb81434800875533ba21d (diff)
Evil: add inet_ntop function from c-ares
* src/lib/evil_inet.c: * src/lib/evil_inet.h: * AUTHORS: add inet_ntop() function. Implementation from c-ares library. See AUTHORS. SVN revision: 63843
Diffstat (limited to 'legacy/evil')
-rw-r--r--legacy/evil/AUTHORS4
-rw-r--r--legacy/evil/ChangeLog10
-rw-r--r--legacy/evil/src/lib/evil_inet.c195
-rw-r--r--legacy/evil/src/lib/evil_inet.h5
4 files changed, 209 insertions, 5 deletions
diff --git a/legacy/evil/AUTHORS b/legacy/evil/AUTHORS
index 125597df1f..e73fed1fdf 100644
--- a/legacy/evil/AUTHORS
+++ b/legacy/evil/AUTHORS
@@ -3,5 +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) 6 Paul Vixie (ISC) (inet_pton and inet_ntop functions)
7 Daniel Stenberg (inet_pton function) 7 Daniel Stenberg (inet_pton and inet_ntop functions)
diff --git a/legacy/evil/ChangeLog b/legacy/evil/ChangeLog
index 4dce120be6..632eb44f86 100644
--- a/legacy/evil/ChangeLog
+++ b/legacy/evil/ChangeLog
@@ -1,3 +1,11 @@
12011-10-05 Vincent Torri <doursse at users dot sf dot net>
2
3 * src/lib/evil_inet.c:
4 * src/lib/evil_inet.h:
5 * AUTHORS:
6 add inet_ntop() function. Implementation from c-ares library.
7 See AUTHORS.
8
12011-09-14 Vincent Torri <doursse at users dot sf dot net> 92011-09-14 Vincent Torri <doursse at users dot sf dot net>
2 10
3 * src/lib/dlfcn/dlfcn.c: 11 * src/lib/dlfcn/dlfcn.c:
@@ -13,7 +21,7 @@
13 * src/lib/Evil.h: 21 * src/lib/Evil.h:
14 * src/lib/Makefile.am: 22 * src/lib/Makefile.am:
15 * AUTHORS: 23 * AUTHORS:
16 add inet_pton() function. Implementation from curl library. 24 add inet_pton() function. Implementation from c-ares library.
17 See AUTHORS. 25 See AUTHORS.
18 26
192011-02-27 Vincent Torri <doursse at users dot sf dot net> 272011-02-27 Vincent Torri <doursse at users dot sf dot net>
diff --git a/legacy/evil/src/lib/evil_inet.c b/legacy/evil/src/lib/evil_inet.c
index fa3732a72f..46a0f6afb3 100644
--- a/legacy/evil/src/lib/evil_inet.c
+++ b/legacy/evil/src/lib/evil_inet.c
@@ -39,6 +39,8 @@
39#include "evil_private.h" 39#include "evil_private.h"
40 40
41 41
42# define SPRINTF(x) ((size_t)sprintf x)
43
42#define ERRNO ((int)GetLastError()) 44#define ERRNO ((int)GetLastError())
43#define SET_ERRNO(x) (SetLastError((DWORD)(x))) 45#define SET_ERRNO(x) (SetLastError((DWORD)(x)))
44 46
@@ -433,7 +435,8 @@ ares_inet_net_pton(int af, const char *src, void *dst, size_t size)
433 } 435 }
434} 436}
435 437
436int evil_inet_pton(int af, const char *src, void *dst) 438int
439evil_inet_pton(int af, const char *src, void *dst)
437{ 440{
438 int result; 441 int result;
439 size_t size; 442 size_t size;
@@ -448,7 +451,195 @@ int evil_inet_pton(int af, const char *src, void *dst)
448 return -1; 451 return -1;
449 } 452 }
450 result = ares_inet_net_pton(af, src, dst, size); 453 result = ares_inet_net_pton(af, src, dst, size);
451 if (result == -1 && ERRNO == ENOENT) 454 if ((result == -1) && (ERRNO == ENOENT))
452 return 0; 455 return 0;
453 return (result > -1 ? 1 : -1); 456 return (result > -1 ? 1 : -1);
454} 457}
458
459
460/* const char *
461 * inet_ntop4(src, dst, size)
462 * format an IPv4 address, more or less like inet_ntoa()
463 * return:
464 * `dst' (as a const)
465 * notes:
466 * (1) uses no statics
467 * (2) takes a unsigned char* not an in_addr as input
468 * author:
469 * Paul Vixie, 1996.
470 */
471static const char *
472inet_ntop4(const unsigned char *src, char *dst, size_t size)
473{
474 static const char fmt[] = "%u.%u.%u.%u";
475 char tmp[sizeof "255.255.255.255"];
476
477 if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size)
478 {
479 SET_ERRNO(ENOSPC);
480 return (NULL);
481 }
482 strcpy(dst, tmp);
483 return (dst);
484}
485
486/* const char *
487 * inet_ntop6(src, dst, size)
488 * convert IPv6 binary address into presentation (printable) format
489 * author:
490 * Paul Vixie, 1996.
491 */
492static const char *
493inet_ntop6(const unsigned char *src, char *dst, size_t size)
494{
495 /*
496 * Note that int32_t and int16_t need only be "at least" large enough
497 * to contain a value of the specified size. On some systems, like
498 * Crays, there is no such thing as an integer variable with 16 bits.
499 * Keep this in mind if you think this function should have been coded
500 * to use pointer overlays. All the world's not a VAX.
501 */
502 char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
503 char *tp;
504 struct {
505 long base;
506 long len;
507 } best, cur;
508 unsigned long words[NS_IN6ADDRSZ / NS_INT16SZ];
509 int i;
510
511 /*
512 * Preprocess:
513 * Copy the input (bytewise) array into a wordwise array.
514 * Find the longest run of 0x00's in src[] for :: shorthanding.
515 */
516 memset(words, '\0', sizeof(words));
517 for (i = 0; i < NS_IN6ADDRSZ; i++)
518 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
519
520 best.base = -1;
521 cur.base = -1;
522 best.len = 0;
523 cur.len = 0;
524
525 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
526 {
527 if (words[i] == 0)
528 {
529 if (cur.base == -1)
530 cur.base = i, cur.len = 1;
531 else
532 cur.len++;
533 }
534 else
535 {
536 if (cur.base != -1)
537 {
538 if (best.base == -1 || cur.len > best.len)
539 best = cur;
540 cur.base = -1;
541 }
542 }
543 }
544 if (cur.base != -1)
545 {
546 if (best.base == -1 || cur.len > best.len)
547 best = cur;
548 }
549 if (best.base != -1 && best.len < 2)
550 best.base = -1;
551
552 /*
553 * Format the result.
554 */
555 tp = tmp;
556 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
557 {
558 /* Are we inside the best run of 0x00's? */
559 if (best.base != -1 && i >= best.base &&
560 i < (best.base + best.len))
561 {
562 if (i == best.base)
563 *tp++ = ':';
564 continue;
565 }
566 /* Are we following an initial run of 0x00s or any real hex? */
567 if (i != 0)
568 *tp++ = ':';
569 /* Is this address an encapsulated IPv4? */
570 if (i == 6 && best.base == 0 &&
571 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
572 {
573 if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
574 return (NULL);
575 tp += strlen(tp);
576 break;
577 }
578 tp += SPRINTF((tp, "%lx", words[i]));
579 }
580
581 /* Was it a trailing run of 0x00's? */
582 if (best.base != -1 && (best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ))
583 *tp++ = ':';
584 *tp++ = '\0';
585
586 /*
587 * Check for overflow, copy, and we're done.
588 */
589 if ((size_t)(tp - tmp) > size)
590 {
591 SET_ERRNO(ENOSPC);
592 return (NULL);
593 }
594 strcpy(dst, tmp);
595 return (dst);
596}
597
598/* char *
599 * inet_ntop(af, src, dst, size)
600 * convert a network format address to presentation format.
601 * return:
602 * pointer to presentation format address (`dst'), or NULL (see errno).
603 * note:
604 * On Windows we store the error in the thread errno, not
605 * in the winsock error code. This is to avoid loosing the
606 * actual last winsock error. So use macro ERRNO to fetch the
607 * errno this funtion sets when returning NULL, not SOCKERRNO.
608 * author:
609 * Paul Vixie, 1996.
610 */
611static const char *
612ares_inet_ntop(int af, const void *src, char *dst, size_t size)
613{
614 switch (af)
615 {
616 case AF_INET:
617 return (inet_ntop4(src, dst, size));
618 case AF_INET6:
619 return (inet_ntop6(src, dst, size));
620 default:
621 SET_ERRNO(EAFNOSUPPORT);
622 return (NULL);
623 }
624 /* NOTREACHED */
625}
626
627const char *evil_inet_ntop(int af, const char *src, void *dst)
628{
629 const char *result;
630 size_t size;
631
632 if (af == AF_INET)
633 size = sizeof(struct in_addr);
634 else if (af == AF_INET6)
635 size = sizeof(struct ares_in6_addr);
636 else
637 {
638 SET_ERRNO(EAFNOSUPPORT);
639 return NULL;
640 }
641 result = ares_inet_ntop(af, src, dst, size);
642 if ((result == NULL) && (ERRNO == ENOSPC))
643 return NULL;
644 return result;
645}
diff --git a/legacy/evil/src/lib/evil_inet.h b/legacy/evil/src/lib/evil_inet.h
index abc59478c1..46e0c8be90 100644
--- a/legacy/evil/src/lib/evil_inet.h
+++ b/legacy/evil/src/lib/evil_inet.h
@@ -32,6 +32,11 @@ EAPI int evil_inet_pton(int af, const char *src, void *dst);
32 32
33#define inet_pton(x,y,z) evil_inet_pton(x,y,z) 33#define inet_pton(x,y,z) evil_inet_pton(x,y,z)
34 34
35EAPI const char *evil_inet_ntop(int af, const char *src, void *dst);
36
37#define inet_ntop(x,y,z) evil_inet_ntop(x,y,z)
38
39
35 40
36/** 41/**
37 * @} 42 * @}