| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <arpa/inet.h> | 
|  | 2 | #include <errno.h> | 
|  | 3 | #include <netinet/in.h> | 
|  | 4 | #include <stdio.h> | 
|  | 5 | #include <string.h> | 
|  | 6 |  | 
|  | 7 | static int | 
|  | 8 | do_test (void) | 
|  | 9 | { | 
|  | 10 | struct in_addr addr4; | 
|  | 11 | struct in6_addr addr6; | 
|  | 12 | char buf[64]; | 
|  | 13 | int result = 0; | 
|  | 14 |  | 
|  | 15 | addr4.s_addr = 0xe0e0e0e0; | 
|  | 16 | addr6.s6_addr16[0] = 0; | 
|  | 17 | addr6.s6_addr16[1] = 0; | 
|  | 18 | addr6.s6_addr16[2] = 0; | 
|  | 19 | addr6.s6_addr16[3] = 0; | 
|  | 20 | addr6.s6_addr16[4] = 0; | 
|  | 21 | addr6.s6_addr16[5] = 0xffff; | 
|  | 22 | addr6.s6_addr32[3] = 0xe0e0e0e0; | 
|  | 23 | memset (buf, 'x', sizeof buf); | 
|  | 24 |  | 
|  | 25 | if (inet_ntop (AF_INET, &addr4, buf, 15) != NULL) | 
|  | 26 | { | 
|  | 27 | puts ("1st inet_ntop returned non-NULL"); | 
|  | 28 | result++; | 
|  | 29 | } | 
|  | 30 | else if (errno != ENOSPC) | 
|  | 31 | { | 
|  | 32 | puts ("1st inet_ntop didn't fail with ENOSPC"); | 
|  | 33 | result++; | 
|  | 34 | } | 
|  | 35 | if (buf[15] != 'x') | 
|  | 36 | { | 
|  | 37 | puts ("1st inet_ntop wrote past the end of buffer"); | 
|  | 38 | result++; | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | if (inet_ntop (AF_INET, &addr4, buf, 16) != buf) | 
|  | 42 | { | 
|  | 43 | puts ("2nd inet_ntop did not return buf"); | 
|  | 44 | result++; | 
|  | 45 | } | 
|  | 46 | if (memcmp (buf, "224.224.224.224\0" "xxxxxxxx", 24) != 0) | 
|  | 47 | { | 
|  | 48 | puts ("2nd inet_ntop wrote past the end of buffer"); | 
|  | 49 | result++; | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | if (inet_ntop (AF_INET6, &addr6, buf, 22) != NULL) | 
|  | 53 | { | 
|  | 54 | puts ("3rd inet_ntop returned non-NULL"); | 
|  | 55 | result++; | 
|  | 56 | } | 
|  | 57 | else if (errno != ENOSPC) | 
|  | 58 | { | 
|  | 59 | puts ("3rd inet_ntop didn't fail with ENOSPC"); | 
|  | 60 | result++; | 
|  | 61 | } | 
|  | 62 | if (buf[22] != 'x') | 
|  | 63 | { | 
|  | 64 | puts ("3rd inet_ntop wrote past the end of buffer"); | 
|  | 65 | result++; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | if (inet_ntop (AF_INET6, &addr6, buf, 23) != buf) | 
|  | 69 | { | 
|  | 70 | puts ("4th inet_ntop did not return buf"); | 
|  | 71 | result++; | 
|  | 72 | } | 
|  | 73 | if (memcmp (buf, "::ffff:224.224.224.224\0" "xxxxxxxx", 31) != 0) | 
|  | 74 | { | 
|  | 75 | puts ("4th inet_ntop wrote past the end of buffer"); | 
|  | 76 | result++; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | memset (&addr6.s6_addr, 0xe0, sizeof (addr6.s6_addr)); | 
|  | 80 |  | 
|  | 81 | if (inet_ntop (AF_INET6, &addr6, buf, 39) != NULL) | 
|  | 82 | { | 
|  | 83 | puts ("5th inet_ntop returned non-NULL"); | 
|  | 84 | result++; | 
|  | 85 | } | 
|  | 86 | else if (errno != ENOSPC) | 
|  | 87 | { | 
|  | 88 | puts ("5th inet_ntop didn't fail with ENOSPC"); | 
|  | 89 | result++; | 
|  | 90 | } | 
|  | 91 | if (buf[39] != 'x') | 
|  | 92 | { | 
|  | 93 | puts ("5th inet_ntop wrote past the end of buffer"); | 
|  | 94 | result++; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | if (inet_ntop (AF_INET6, &addr6, buf, 40) != buf) | 
|  | 98 | { | 
|  | 99 | puts ("6th inet_ntop did not return buf"); | 
|  | 100 | result++; | 
|  | 101 | } | 
|  | 102 | if (memcmp (buf, "e0e0:e0e0:e0e0:e0e0:e0e0:e0e0:e0e0:e0e0\0" | 
|  | 103 | "xxxxxxxx", 48) != 0) | 
|  | 104 | { | 
|  | 105 | puts ("6th inet_ntop wrote past the end of buffer"); | 
|  | 106 | result++; | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 |  | 
|  | 110 | return result; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | #define TEST_FUNCTION do_test () | 
|  | 114 | #include "../test-skeleton.c" |