24 #ifdef HAVE_ARPA_INET_H
25 #include <arpa/inet.h>
44 bool is_octal =
false;
47 if (
tor_sscanf(str,
"%3u.%3u.%3u.%3u%c", &a, &b, &c, &d, &more) != 4)
54 is_octal = (strlen(octet) > 1 && octet[0] ==
'0');
65 if (a > 255)
return 0;
66 if (b > 255)
return 0;
67 if (c > 255)
return 0;
68 if (d > 255)
return 0;
69 addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d);
81 uint32_t a = ntohl(in->s_addr);
83 (
int)(uint8_t)((a>>24)&0xff),
84 (
int)(uint8_t)((a>>16)&0xff),
85 (
int)(uint8_t)((a>>8 )&0xff),
86 (
int)(uint8_t)((a )&0xff));
105 }
else if (af == AF_INET6) {
108 int longestGapLen = 0, longestGapPos = -1, i,
109 curGapPos = -1, curGapLen = 0;
111 for (i = 0; i < 8; ++i) {
112 words[i] = (((uint16_t)addr->s6_addr[2*i])<<8) + addr->s6_addr[2*i+1];
114 if (words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 &&
115 words[4] == 0 && ((words[5] == 0 && words[6] && words[7]) ||
116 (words[5] == 0xffff))) {
120 addr->s6_addr[12], addr->s6_addr[13],
121 addr->s6_addr[14], addr->s6_addr[15]);
123 tor_snprintf(buf,
sizeof(buf),
"::%x:%d.%d.%d.%d", words[5],
124 addr->s6_addr[12], addr->s6_addr[13],
125 addr->s6_addr[14], addr->s6_addr[15]);
127 if ((strlen(buf) + 1) > len)
129 strlcpy(dst, buf, len);
137 while (i<8 && words[i] == 0) {
140 if (curGapLen > longestGapLen) {
141 longestGapPos = curGapPos;
142 longestGapLen = curGapLen;
148 if (longestGapLen<=1)
152 for (i = 0; i < 8; ++i) {
153 if (words[i] == 0 && longestGapPos == i) {
157 while (i < 8 && words[i] == 0)
161 tor_snprintf(cp,
sizeof(buf)-(cp-buf),
"%x", (
unsigned)words[i]);
168 if ((strlen(buf) + 1) > len)
170 strlcpy(dst, buf, len);
191 }
else if (af == AF_INET6) {
192 ssize_t len = strlen(src);
195 if (len > 2 && src[len - 1] ==
':' && src[len - 2] !=
':') {
201 int gapPos = -1, i, setWords=0;
202 const char *dot = strchr(src,
'.');
204 memset(words, 0xf8,
sizeof(words));
208 eow = src+strlen(src);
210 unsigned byte1,byte2,byte3,byte4;
212 for (eow = dot-1; eow > src && TOR_ISDIGIT(*eow); --eow)
221 &byte1,&byte2,&byte3,&byte4,&more) != 4)
224 if (byte1 > 255 || byte2 > 255 || byte3 > 255 || byte4 > 255)
227 words[6] = (byte1<<8) | byte2;
228 words[7] = (byte3<<8) | byte4;
236 if (TOR_ISXDIGIT(*src)) {
238 long r = strtol(src, &next, 16);
239 if (next == NULL || next == src) {
246 len = *next ==
'\0' ? eow - src : next - src;
249 if (len > 1 && !TOR_ISXDIGIT(src[1]))
254 words[i++] = (uint16_t)r;
257 if (*src !=
':' && src != eow)
260 }
else if (*src ==
':' && i > 0 && gapPos == -1) {
263 }
else if (*src ==
':' && i == 0 && src+1 < eow && src[1] ==
':' &&
273 (setWords == 8 && gapPos != -1) ||
274 (setWords < 8 && gapPos == -1))
278 int nToMove = setWords - (dot ? 2 : 0) - gapPos;
279 int gapLen = 8 - setWords;
281 memmove(&words[gapPos+gapLen], &words[gapPos],
282 sizeof(uint16_t)*nToMove);
283 memset(&words[gapPos], 0,
sizeof(uint16_t)*gapLen);
285 for (i = 0; i < 8; ++i) {
286 out->s6_addr[2*i ] = words[i] >> 8;
287 out->s6_addr[2*i+1] = words[i] & 0xff;
Locale-independent character-type inspection (header)
Header for compat_string.c.
int tor_inet_ntoa(const struct in_addr *in, char *buf, size_t buf_len)
int tor_inet_aton(const char *str, struct in_addr *addr)
int tor_inet_pton(int af, const char *src, void *dst)
const char * tor_inet_ntop(int af, const void *src, char *dst, size_t len)
Define in6_addr, its members, and related types on platforms that lack it.
Headers for util_malloc.c.
int tor_snprintf(char *str, size_t size, const char *format,...)
int tor_sscanf(const char *buf, const char *pattern,...)
smartlist_t * smartlist_new(void)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)
Integer definitions used throughout Tor.
Macros to manage assertions, fatal and non-fatal.
Header for util_string.c.