mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2025-02-23 22:47:12 +01:00
Change from inet_ntoa to a threadproof tor_inet_ntoa.
svn:r3656
This commit is contained in:
parent
e8da6b26b6
commit
6e6d95b3db
14 changed files with 75 additions and 23 deletions
|
@ -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
|
||||
* ===== */
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue