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
* ===== */
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);
*/
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;
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);
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));
* 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;
struct hostent *rent;
char hostname[256];
int explicit_ip=1;
+ char tmpbuf[INET_NTOA_BUF_LEN];
tor_assert(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;
}
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)) {
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;
}
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;
}
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);
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);
}
(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;
}
}
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
*/
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
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);
}
}
}
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;
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;
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;
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;
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);
&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;
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);
}
int n_args;
struct in_addr a;
uint32_t result;
+ char buf[INET_NTOA_BUF_LEN];
arg = &argv[1];
n_args = argc-1;
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;
}