Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdint.h> #include <string.h> #include <errno.h> #include <sys/socket.h> #define MAX_V4_ADDR_LEN 16 #define MAX_V6_ADDR_LEN 64 static const char *hexchars = "0123456789abcdef"; const char * inet_ntop6(const struct in6_addr *addr, char *dst, socklen_t size) { char hexa[8][5], tmp[MAX_V6_ADDR_LEN]; int zr[8]; socklen_t len; int32_t i, j, k, skip; uint8_t x8, hx8; uint16_t x16; struct in_addr a4; if (addr == NULL) { errno = EAFNOSUPPORT; return NULL; } if (dst == NULL) { errno = ENOSPC; return NULL; } memset(tmp, 0, MAX_V6_ADDR_LEN); /* check for mapped or compat addresses */ i = IN6_IS_ADDR_V4MAPPED(addr); j = IN6_IS_ADDR_V4COMPAT(addr); if ((i != 0) || (j != 0)) { a4.s_addr = addr->__u6_addr.__u6_addr32[3]; sprintf(tmp, "::%s%s", (i != 0) ? "ffff:" : "", inet_ntoa(a4)); len = strlen(tmp) + 1; if (len > size) { errno = ENOSPC; return NULL; } memcpy(dst, tmp, len); return dst; } k = 0; for (i = 0; i < 16; i += 2) { j = 0; skip = 1; memset(hexa[k], 0, 5); x8 = addr->__u6_addr.__u6_addr8[i]; hx8 = x8 >> 4; if (hx8 != 0) { skip = 0; hexa[k][j++] = hexchars[hx8]; } hx8 = x8 & 0x0f; if ((skip == 0) || ((skip == 1) && (hx8 != 0))) { skip = 0; hexa[k][j++] = hexchars[hx8]; } x8 = addr->__u6_addr.__u6_addr8[i + 1]; hx8 = x8 >> 4; if ((skip == 0) || ((skip == 1) && (hx8 != 0))) { hexa[k][j++] = hexchars[hx8]; } hx8 = x8 & 0x0f; hexa[k][j++] = hexchars[hx8]; k++; } /* find runs of zeros for :: convention */ j = 0; for (i = 7; i >= 0; i--) { zr[i] = j; x16 = addr->__u6_addr.__u6_addr16[i]; if (x16 == 0) j++; else j = 0; zr[i] = j; } /* find longest run of zeros */ k = -1; j = 0; for(i = 0; i < 8; i++) { if (zr[i] > j) { k = i; j = zr[i]; } } for(i = 0; i < 8; i++) { if (i != k) zr[i] = 0; } len = 0; for (i = 0; i < 8; i++) { if (zr[i] != 0) { /* check for leading zero */ if (i == 0) tmp[len++] = ':'; tmp[len++] = ':'; i += (zr[i] - 1); continue; } for (j = 0; hexa[i][j] != '\0'; j++) tmp[len++] = hexa[i][j]; if (i != 7) tmp[len++] = ':'; } /* trailing NULL */ len++; if (len > size) { errno = ENOSPC; return NULL; } memcpy(dst, tmp, len); return dst; } const char * inet_ntop4(const struct in_addr *addr, char *dst, socklen_t size) { char tmp[MAX_V4_ADDR_LEN], *p; const u_int8_t *ap = (u_int8_t *)&addr->s_addr; int i, ql, len; if (addr == NULL) { errno = EAFNOSUPPORT; return NULL; } if (dst == NULL) { errno = ENOSPC; return NULL; } memset(tmp, 0, MAX_V4_ADDR_LEN); /* 3 dots, trailing nul */ len = 4; p = tmp; for (i = 0; i < 4; i++, ap++) { snprintf(p, 4, "%d", *ap); ql = strlen(p); len += ql; p += ql; if (i < 3) *p++ = '.'; } if (len > size) { errno = ENOSPC; return NULL; } memcpy(dst, tmp, len); return dst; } const char * inet_ntop(int af, const void *addr, char *buf, socklen_t len) { if (af == AF_INET6) return inet_ntop6(addr, buf, len); if (af == AF_INET) return inet_ntop4(addr, buf, len); errno = EAFNOSUPPORT; return NULL; } |