1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
8 * \brief Convert in_addr and in6_addr to and from strings.
11 #include "lib/net/inaddr.h"
13 #include "lib/cc/torint.h"
14 #include "lib/log/util_bug.h"
15 #include "lib/net/inaddr_st.h"
16 #include "lib/string/compat_ctype.h"
17 #include "lib/string/compat_string.h"
18 #include "lib/string/printf.h"
19 #include "lib/string/scanf.h"
20 #include "lib/string/util_string.h"
22 #ifdef HAVE_ARPA_INET_H
23 #include <arpa/inet.h>
33 /** Set *addr to the IP address (in dotted-quad notation) stored in *str.
34 * Return 1 on success, 0 if *str is badly formatted.
35 * (Like inet_aton(str,addr), but works on Windows and Solaris.)
38 tor_inet_aton(const char *str
, struct in_addr
* addr
)
42 if (tor_sscanf(str
, "%3u.%3u.%3u.%3u%c", &a
,&b
,&c
,&d
,&more
) != 4)
44 if (a
> 255) return 0;
45 if (b
> 255) return 0;
46 if (c
> 255) return 0;
47 if (d
> 255) return 0;
48 addr
->s_addr
= htonl((a
<<24) | (b
<<16) | (c
<<8) | d
);
52 /** Given an IPv4 in_addr struct *<b>in</b> (in network order, as usual),
53 * write it as a string into the <b>buf_len</b>-byte buffer in
54 * <b>buf</b>. Returns a non-negative integer on success.
55 * Returns -1 on failure.
58 tor_inet_ntoa(const struct in_addr
*in
, char *buf
, size_t buf_len
)
60 uint32_t a
= ntohl(in
->s_addr
);
61 return tor_snprintf(buf
, buf_len
, "%d.%d.%d.%d",
62 (int)(uint8_t)((a
>>24)&0xff),
63 (int)(uint8_t)((a
>>16)&0xff),
64 (int)(uint8_t)((a
>>8 )&0xff),
65 (int)(uint8_t)((a
)&0xff));
68 /** Given <b>af</b>==AF_INET and <b>src</b> a struct in_addr, or
69 * <b>af</b>==AF_INET6 and <b>src</b> a struct in6_addr, try to format the
70 * address and store it in the <b>len</b>-byte buffer <b>dst</b>. Returns
71 * <b>dst</b> on success, NULL on failure.
73 * (Like inet_ntop(af,src,dst,len), but works on platforms that don't have it:
74 * Tor sometimes needs to format ipv6 addresses even on platforms without ipv6
77 tor_inet_ntop(int af
, const void *src
, char *dst
, size_t len
)
80 if (tor_inet_ntoa(src
, dst
, len
) < 0)
84 } else if (af
== AF_INET6
) {
85 const struct in6_addr
*addr
= src
;
87 int longestGapLen
= 0, longestGapPos
= -1, i
,
88 curGapPos
= -1, curGapLen
= 0;
90 for (i
= 0; i
< 8; ++i
) {
91 words
[i
] = (((uint16_t)addr
->s6_addr
[2*i
])<<8) + addr
->s6_addr
[2*i
+1];
93 if (words
[0] == 0 && words
[1] == 0 && words
[2] == 0 && words
[3] == 0 &&
94 words
[4] == 0 && ((words
[5] == 0 && words
[6] && words
[7]) ||
95 (words
[5] == 0xffff))) {
96 /* This is an IPv4 address. */
98 tor_snprintf(buf
, sizeof(buf
), "::%d.%d.%d.%d",
99 addr
->s6_addr
[12], addr
->s6_addr
[13],
100 addr
->s6_addr
[14], addr
->s6_addr
[15]);
102 tor_snprintf(buf
, sizeof(buf
), "::%x:%d.%d.%d.%d", words
[5],
103 addr
->s6_addr
[12], addr
->s6_addr
[13],
104 addr
->s6_addr
[14], addr
->s6_addr
[15]);
106 if ((strlen(buf
) + 1) > len
) /* +1 for \0 */
108 strlcpy(dst
, buf
, len
);
116 while (i
<8 && words
[i
] == 0) {
119 if (curGapLen
> longestGapLen
) {
120 longestGapPos
= curGapPos
;
121 longestGapLen
= curGapLen
;
127 if (longestGapLen
<=1)
131 for (i
= 0; i
< 8; ++i
) {
132 if (words
[i
] == 0 && longestGapPos
== i
) {
136 while (i
< 8 && words
[i
] == 0)
138 --i
; /* to compensate for loop increment. */
140 tor_snprintf(cp
, sizeof(buf
)-(cp
-buf
), "%x", (unsigned)words
[i
]);
147 if ((strlen(buf
) + 1) > len
) /* +1 for \0 */
149 strlcpy(dst
, buf
, len
);
156 /** Given <b>af</b>==AF_INET or <b>af</b>==AF_INET6, and a string <b>src</b>
157 * encoding an IPv4 address or IPv6 address correspondingly, try to parse the
158 * address and store the result in <b>dst</b> (which must have space for a
159 * struct in_addr or a struct in6_addr, as appropriate). Return 1 on success,
160 * 0 on a bad parse, and -1 on a bad <b>af</b>.
162 * (Like inet_pton(af,src,dst) but works on platforms that don't have it: Tor
163 * sometimes needs to format ipv6 addresses even on platforms without ipv6
166 tor_inet_pton(int af
, const char *src
, void *dst
)
169 return tor_inet_aton(src
, dst
);
170 } else if (af
== AF_INET6
) {
171 ssize_t len
= strlen(src
);
173 /* Reject if src has needless trailing ':'. */
174 if (len
> 2 && src
[len
- 1] == ':' && src
[len
- 2] != ':') {
178 struct in6_addr
*out
= dst
;
180 int gapPos
= -1, i
, setWords
=0;
181 const char *dot
= strchr(src
, '.');
182 const char *eow
; /* end of words. */
183 memset(words
, 0xf8, sizeof(words
));
187 eow
= src
+strlen(src
);
189 unsigned byte1
,byte2
,byte3
,byte4
;
191 for (eow
= dot
-1; eow
> src
&& TOR_ISDIGIT(*eow
); --eow
)
197 /* We use "scanf" because some platform inet_aton()s are too lax
198 * about IPv4 addresses of the form "1.2.3" */
199 if (tor_sscanf(eow
, "%3u.%3u.%3u.%3u%c",
200 &byte1
,&byte2
,&byte3
,&byte4
,&more
) != 4)
203 if (byte1
> 255 || byte2
> 255 || byte3
> 255 || byte4
> 255)
206 words
[6] = (byte1
<<8) | byte2
;
207 words
[7] = (byte3
<<8) | byte4
;
215 if (TOR_ISXDIGIT(*src
)) {
217 long r
= strtol(src
, &next
, 16);
218 if (next
== NULL
|| next
== src
) {
219 /* The 'next == src' error case can happen on versions of openbsd
220 * which treat "0xfoo" as an error, rather than as "0" followed by
225 len
= *next
== '\0' ? eow
- src
: next
- src
;
228 if (len
> 1 && !TOR_ISXDIGIT(src
[1]))
229 return 0; /* 0x is not valid */
232 tor_assert(r
< 65536);
233 words
[i
++] = (uint16_t)r
;
236 if (*src
!= ':' && src
!= eow
)
239 } else if (*src
== ':' && i
> 0 && gapPos
== -1) {
242 } else if (*src
== ':' && i
== 0 && src
+1 < eow
&& src
[1] == ':' &&
252 (setWords
== 8 && gapPos
!= -1) ||
253 (setWords
< 8 && gapPos
== -1))
257 int nToMove
= setWords
- (dot
? 2 : 0) - gapPos
;
258 int gapLen
= 8 - setWords
;
259 tor_assert(nToMove
>= 0);
260 memmove(&words
[gapPos
+gapLen
], &words
[gapPos
],
261 sizeof(uint16_t)*nToMove
);
262 memset(&words
[gapPos
], 0, sizeof(uint16_t)*gapLen
);
264 for (i
= 0; i
< 8; ++i
) {
265 out
->s6_addr
[2*i
] = words
[i
] >> 8;
266 out
->s6_addr
[2*i
+1] = words
[i
] & 0xff;