aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2005-02-22 08:18:36 +0000
committerNick Mathewson <nickm@torproject.org>2005-02-22 08:18:36 +0000
commit6e6d95b3db091d4ae107c8d4f03354ccadd20757 (patch)
tree1c5799dcfb490b15934adffa42a564d9a47f8a91 /src
parente8da6b26b62475114937077e88905be921937554 (diff)
downloadtor-6e6d95b3db091d4ae107c8d4f03354ccadd20757.tar.gz
tor-6e6d95b3db091d4ae107c8d4f03354ccadd20757.zip
Change from inet_ntoa to a threadproof tor_inet_ntoa.
svn:r3656
Diffstat (limited to 'src')
-rw-r--r--src/common/util.c11
-rw-r--r--src/common/util.h2
-rw-r--r--src/or/buffers.c6
-rw-r--r--src/or/circuitbuild.c4
-rw-r--r--src/or/config.c6
-rw-r--r--src/or/connection.c12
-rw-r--r--src/or/connection_edge.c9
-rw-r--r--src/or/connection_or.c4
-rw-r--r--src/or/cpuworker.c4
-rw-r--r--src/or/rendservice.c4
-rw-r--r--src/or/router.c11
-rw-r--r--src/or/routerparse.c13
-rw-r--r--src/or/test.c8
-rw-r--r--src/tools/tor-resolve.c4
14 files changed, 75 insertions, 23 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 4b17454e80..339572e449 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -1255,6 +1255,17 @@ parse_addr_and_port_range(const char *s, uint32_t *addr_out,
return -1;
}
+int
+tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len)
+{
+ uint32_t a = ntohl(in->s_addr);
+ return tor_snprintf(buf, buf_len, "%d.%d.%d.%d",
+ (int)(uint8_t)((a>>24)&0xff),
+ (int)(uint8_t)((a>>16)&0xff),
+ (int)(uint8_t)((a>>8 )&0xff),
+ (int)(uint8_t)((a )&0xff));
+}
+
/* =====
* Process helpers
* ===== */
diff --git a/src/common/util.h b/src/common/util.h
index 2f139515af..fde2557f58 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -126,6 +126,8 @@ int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
int parse_addr_and_port_range(const char *s, uint32_t *addr_out,
uint32_t *mask_out, uint16_t *port_min_out,
uint16_t *port_max_out);
+#define INET_NTOA_BUF_LEN 16
+int tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len);
/* Process helpers */
void start_daemon(const char *desired_cwd);
diff --git a/src/or/buffers.c b/src/or/buffers.c
index 990fc94b15..0abcd9a828 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -448,7 +448,7 @@ int fetch_from_buf_http(buf_t *buf,
*/
int fetch_from_buf_socks(buf_t *buf, socks_request_t *req) {
unsigned char len;
- char *tmpbuf=NULL;
+ char tmpbuf[INET_NTOA_BUF_LEN];
uint32_t destip;
enum {socks4, socks4a} socks4_prot = socks4a;
char *next, *startaddr;
@@ -505,7 +505,7 @@ int fetch_from_buf_socks(buf_t *buf, socks_request_t *req) {
destip = ntohl(*(uint32_t*)(buf->mem+4));
in.s_addr = htonl(destip);
- tmpbuf = inet_ntoa(in);
+ tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
log_fn(LOG_WARN,"socks5 IP takes %d bytes, which doesn't fit in %d. Rejecting.",
(int)strlen(tmpbuf)+1,(int)MAX_SOCKS_ADDR_LEN);
@@ -565,7 +565,7 @@ int fetch_from_buf_socks(buf_t *buf, socks_request_t *req) {
if (destip >> 8) {
log_fn(LOG_DEBUG,"socks4: destip not in form 0.0.0.x.");
in.s_addr = htonl(destip);
- tmpbuf = inet_ntoa(in);
+ tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
log_fn(LOG_WARN,"socks4 addr (%d bytes) too long. Rejecting.",
(int)strlen(tmpbuf));
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index edf76da110..b10d0b1732 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -503,9 +503,11 @@ int circuit_extend(cell_t *cell, circuit_t *circ) {
* router twice in a row in the path. I think that's ok.
*/
struct in_addr in;
+ char tmpbuf[INET_NTOA_BUF_LEN];
in.s_addr = htonl(circ->n_addr);
+ tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
log_fn(LOG_INFO,"Next router (%s:%d) not connected. Connecting.",
- inet_ntoa(in), circ->n_port);
+ tmpbuf, circ->n_port);
memcpy(circ->onionskin, onionskin, ONIONSKIN_CHALLENGE_LEN);
circ->state = CIRCUIT_STATE_OR_WAIT;
diff --git a/src/or/config.c b/src/or/config.c
index aeaa538177..2e69de2602 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -928,6 +928,7 @@ resolve_my_address(const char *address, uint32_t *addr)
struct hostent *rent;
char hostname[256];
int explicit_ip=1;
+ char tmpbuf[INET_NTOA_BUF_LEN];
tor_assert(addr);
@@ -957,14 +958,15 @@ resolve_my_address(const char *address, uint32_t *addr)
memcpy(&in.s_addr, rent->h_addr, rent->h_length);
}
+ tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
if (!explicit_ip && is_internal_IP(htonl(in.s_addr))) {
log_fn(LOG_WARN,"Address '%s' resolves to private IP '%s'. "
"Please set the Address config option to be the IP you want to use.",
- hostname, inet_ntoa(in));
+ hostname, tmpbuf);
return -1;
}
- log_fn(LOG_DEBUG, "Resolved Address to %s.", inet_ntoa(in));
+ log_fn(LOG_DEBUG, "Resolved Address to %s.", tmpbuf);
*addr = ntohl(in.s_addr);
return 0;
}
diff --git a/src/or/connection.c b/src/or/connection.c
index bad34746c2..ccf629179d 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -462,6 +462,7 @@ static int connection_handle_listener_read(connection_t *conn, int new_type) {
struct sockaddr_in remote;
/* length of the remote address. Must be an int, since accept() needs that. */
int remotelen = sizeof(struct sockaddr_in);
+ char tmpbuf[INET_NTOA_BUF_LEN];
news = accept(conn->s,(struct sockaddr *)&remote,&remotelen);
if (!SOCKET_IS_POLLABLE(news)) {
@@ -495,8 +496,9 @@ static int connection_handle_listener_read(connection_t *conn, int new_type) {
if (new_type == CONN_TYPE_AP) {
/* check sockspolicy to see if we should accept it */
if (socks_policy_permits_address(ntohl(remote.sin_addr.s_addr)) == 0) {
+ tor_inet_ntoa(&remote.sin_addr, tmpbuf, sizeof(tmpbuf));
log_fn(LOG_NOTICE,"Denying socks connection from untrusted address %s.",
- inet_ntoa(remote.sin_addr));
+ tmpbuf);
tor_close_socket(news);
return 0;
}
@@ -504,8 +506,9 @@ static int connection_handle_listener_read(connection_t *conn, int new_type) {
if (new_type == CONN_TYPE_DIR) {
/* check dirpolicy to see if we should accept it */
if (dir_policy_permits_address(ntohl(remote.sin_addr.s_addr)) == 0) {
+ tor_inet_ntoa(&remote.sin_addr, tmpbuf, sizeof(tmpbuf));
log_fn(LOG_NOTICE,"Denying dir connection from address %s.",
- inet_ntoa(remote.sin_addr));
+ tmpbuf);
tor_close_socket(news);
return 0;
}
@@ -514,7 +517,10 @@ static int connection_handle_listener_read(connection_t *conn, int new_type) {
newconn = connection_new(new_type);
newconn->s = news;
- newconn->address = tor_strdup(inet_ntoa(remote.sin_addr)); /* remember the remote address */
+ /* remember the remote address */
+ newconn->address = tor_malloc(INET_NTOA_BUF_LEN);
+ tor_inet_ntoa(&remote.sin_addr, newconn->address, INET_NTOA_BUF_LEN);
+
newconn->addr = ntohl(remote.sin_addr.s_addr);
newconn->port = ntohs(remote.sin_port);
diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c
index 826a91fcc2..632a93e497 100644
--- a/src/or/connection_edge.c
+++ b/src/or/connection_edge.c
@@ -482,13 +482,16 @@ int client_dns_incr_failures(const char *address)
void client_dns_set_addressmap(const char *address, uint32_t val)
{
struct in_addr in;
+ char *addr;
tor_assert(address); tor_assert(val);
if (tor_inet_aton(address, &in))
return; /* don't set an addressmap back to ourselves! */
in.s_addr = htonl(val);
- addressmap_register(address, strdup(inet_ntoa(in)),
+ addr = tor_malloc(INET_NTOA_BUF_LEN);
+ tor_inet_ntoa(&in,addr,INET_NTOA_BUF_LEN);
+ addressmap_register(address, addr,
time(NULL) + MAX_DNS_ENTRY_AGE);
}
@@ -1109,11 +1112,13 @@ connection_exit_connect(connection_t *conn) {
(r->port_min <= port) && (port <= r->port_max)) {
struct in_addr in;
if (r->is_redirect) {
+ char tmpbuf[INET_NTOA_BUF_LEN];
addr = r->addr_dest;
port = r->port_dest;
in.s_addr = htonl(addr);
+ tor_inet_ntoa(&in, tmpbuf, sizeof(tmpbuf));
log_fn(LOG_DEBUG, "Redirecting connection from %s:%d to %s:%d",
- conn->address, conn->port, inet_ntoa(in), port);
+ conn->address, conn->port, tmpbuf, port);
}
break;
}
diff --git a/src/or/connection_or.c b/src/or/connection_or.c
index aa0e624efc..7286de869e 100644
--- a/src/or/connection_or.c
+++ b/src/or/connection_or.c
@@ -157,7 +157,9 @@ connection_or_init_conn_from_address(connection_t *conn,
}
tor_free(conn->address);
in.s_addr = htonl(addr);
- conn->address = tor_strdup(inet_ntoa(in));
+
+ conn->address = tor_malloc(INET_NTOA_BUF_LEN);
+ tor_inet_ntoa(&in,conn->address,INET_NTOA_BUF_LEN);
}
void
diff --git a/src/or/cpuworker.c b/src/or/cpuworker.c
index 62a99d589f..5b65835ecc 100644
--- a/src/or/cpuworker.c
+++ b/src/or/cpuworker.c
@@ -67,13 +67,15 @@ static void tag_pack(char *tag, uint32_t addr, uint16_t port, uint16_t circ_id)
*/
static void tag_unpack(const char *tag, uint32_t *addr, uint16_t *port, uint16_t *circ_id) {
struct in_addr in;
+ char addrbuf[INET_NTOA_BUF_LEN];
*addr = *(const uint32_t *)tag;
*port = *(const uint16_t *)(tag+4);
*circ_id = *(const uint16_t *)(tag+6);
in.s_addr = htonl(*addr);
- log_fn(LOG_DEBUG,"onion was from %s:%d, circ_id %d.", inet_ntoa(in), *port, *circ_id);
+ tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
+ log_fn(LOG_DEBUG,"onion was from %s:%d, circ_id %d.", addrbuf, *port, *circ_id);
}
/** Called when the onion key has changed and we need to spawn new
diff --git a/src/or/rendservice.c b/src/or/rendservice.c
index 1093760968..f9c9c3e378 100644
--- a/src/or/rendservice.c
+++ b/src/or/rendservice.c
@@ -113,10 +113,12 @@ static void add_service(rend_service_t *service)
smartlist_add(rend_service_list, service);
log_fn(LOG_DEBUG,"Configuring service with directory %s",service->directory);
for (i = 0; i < smartlist_len(service->ports); ++i) {
+ char addrbuf[INET_NTOA_BUF_LEN];
p = smartlist_get(service->ports, i);
addr.s_addr = htonl(p->real_address);
+ tor_inet_ntoa(&addr, addrbuf, sizeof(addrbuf));
log_fn(LOG_DEBUG,"Service maps port %d to %s:%d",
- p->virtual_port, inet_ntoa(addr), p->real_port);
+ p->virtual_port, addrbuf, p->real_port);
}
}
}
diff --git a/src/or/router.c b/src/or/router.c
index 0cb45def30..ebcf7b56c1 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -541,6 +541,7 @@ int router_rebuild_descriptor(int force) {
struct in_addr in;
int hibernating = we_are_hibernating();
or_options_t *options = get_options();
+ char addrbuf[INET_NTOA_BUF_LEN];
if (!desc_is_dirty && !force)
return 0;
@@ -552,7 +553,8 @@ int router_rebuild_descriptor(int force) {
ri = tor_malloc_zero(sizeof(routerinfo_t));
in.s_addr = htonl(addr);
- ri->address = tor_strdup(inet_ntoa(in));
+ tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
+ ri->address = tor_strdup(addrbuf);
ri->nickname = tor_strdup(options->Nickname);
ri->addr = addr;
ri->or_port = options->ORPort;
@@ -628,6 +630,7 @@ int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
char published[32];
char fingerprint[FINGERPRINT_LEN+1];
struct in_addr in;
+ char addrbuf[INET_NTOA_BUF_LEN];
size_t onion_pkeylen, identity_pkeylen;
size_t written;
int result=0;
@@ -729,16 +732,18 @@ int router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
for (tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
in.s_addr = htonl(tmpe->addr);
/* Write: "accept 1.2.3.4" */
+ tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
result = tor_snprintf(s+written, maxlen-written, "%s %s",
tmpe->policy_type == ADDR_POLICY_ACCEPT ? "accept" : "reject",
- tmpe->msk == 0 ? "*" : inet_ntoa(in));
+ tmpe->msk == 0 ? "*" : addrbuf);
if (result < 0)
return -1;
written += result;
if (tmpe->msk != 0xFFFFFFFFu && tmpe->msk != 0) {
/* Write "/255.255.0.0" */
+ tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
in.s_addr = htonl(tmpe->msk);
- result = tor_snprintf(s+written, maxlen-written, "/%s", inet_ntoa(in));
+ result = tor_snprintf(s+written, maxlen-written, "/%s", addrbuf);
if (result<0)
return -1;
written += result;
diff --git a/src/or/routerparse.c b/src/or/routerparse.c
index 07ecf44cd4..0cb996c17b 100644
--- a/src/or/routerparse.c
+++ b/src/or/routerparse.c
@@ -1054,8 +1054,10 @@ static addr_policy_t *
router_parse_addr_policy(directory_token_t *tok) {
addr_policy_t *newe;
- struct in_addr in;
- char *arg, *address;
+// struct in_addr in;
+ char *arg;
+// char *address;
+// char buf[INET_NTOA_BUF_LEN];
tor_assert(tok->tp == K_REJECT || tok->tp == K_ACCEPT);
@@ -1076,13 +1078,14 @@ router_parse_addr_policy(directory_token_t *tok) {
&newe->prt_min, &newe->prt_max))
goto policy_read_failed;
- in.s_addr = htonl(newe->addr);
- address = tor_strdup(inet_ntoa(in));
+// in.s_addr = htonl(newe->addr);
+// tor_inet_ntoa(&in, buf, sizeof(buf));
+// address = tor_strdup(buf);
// in.s_addr = htonl(newe->msk);
// log_fn(LOG_DEBUG,"%s %s/%s:%d-%d",
// newe->policy_type == ADDR_POLICY_REJECT ? "reject" : "accept",
// address, inet_ntoa(in), newe->prt_min, newe->prt_max);
- tor_free(address);
+// tor_free(address);
return newe;
diff --git a/src/or/test.c b/src/or/test.c
index cf3505c0a8..a30d43e969 100644
--- a/src/or/test.c
+++ b/src/or/test.c
@@ -791,6 +791,14 @@ test_util(void) {
test_assert(strcmpend("abcdef", "dee")>0);
test_assert(strcmpend("ab", "abb")<0);
+ {
+ char tmpbuf[INET_NTOA_BUF_LEN];
+ struct in_addr in;
+ tor_inet_aton("18.244.0.188",&in);
+ tor_inet_ntoa(&in, tmpbuf, sizeof(tmpbuf));
+ test_streq(tmpbuf, "18.244.0.188");
+ }
+
/* XXXX test older functions. */
smartlist_free(sl);
}
diff --git a/src/tools/tor-resolve.c b/src/tools/tor-resolve.c
index 65a6300cfa..090503045e 100644
--- a/src/tools/tor-resolve.c
+++ b/src/tools/tor-resolve.c
@@ -174,6 +174,7 @@ main(int argc, char **argv)
int n_args;
struct in_addr a;
uint32_t result;
+ char buf[INET_NTOA_BUF_LEN];
arg = &argv[1];
n_args = argc-1;
@@ -225,6 +226,7 @@ main(int argc, char **argv)
return 1;
a.s_addr = htonl(result);
- printf("%s\n", inet_ntoa(a));
+ tor_inet_ntoa(&a, buf, sizeof(buf));
+ printf("%s\n", buf);
return 0;
}