Use parse_addr_port() function instead of ad-hoc variants

svn:r2441
This commit is contained in:
Nick Mathewson 2004-10-12 15:52:09 +00:00
parent 8b037509f3
commit 0383afac9a
3 changed files with 39 additions and 64 deletions

View File

@ -319,30 +319,20 @@ void connection_expire_held_open(void)
static int connection_create_listener(const char *bindaddress, uint16_t bindport, int type) { static int connection_create_listener(const char *bindaddress, uint16_t bindport, int type) {
struct sockaddr_in bindaddr; /* where to bind */ struct sockaddr_in bindaddr; /* where to bind */
connection_t *conn; connection_t *conn;
char *hostname, *cp; uint16_t usePort;
int usePort;
int s; /* the socket we're going to make */ int s; /* the socket we're going to make */
int one=1; int one=1;
cp = strchr(bindaddress, ':');
if (cp) {
hostname = tor_strndup(bindaddress, cp-bindaddress);
usePort = atoi(cp+1);
} else {
hostname = tor_strdup(bindaddress);
usePort = bindport;
}
memset(&bindaddr,0,sizeof(struct sockaddr_in)); memset(&bindaddr,0,sizeof(struct sockaddr_in));
bindaddr.sin_family = AF_INET; if (parse_addr_port(bindaddress, NULL, &(bindaddr.sin_addr.s_addr),
bindaddr.sin_port = htons((uint16_t) usePort); &usePort)<0) {
if(tor_lookup_hostname(hostname, &(bindaddr.sin_addr.s_addr)) != 0) { log_fn(LOG_WARN, "Error parsing/resolving BindAddress %s",bindaddress);
log_fn(LOG_WARN,"Can't resolve BindAddress %s",hostname);
tor_free(hostname);
return -1; return -1;
} }
tor_free(hostname); if (usePort==0)
usePort = bindport;
bindaddr.sin_family = AF_INET;
bindaddr.sin_port = htons((uint16_t) usePort);
s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if (s < 0) { if (s < 0) {

View File

@ -737,7 +737,8 @@ void connection_ap_handshake_socks_reply(connection_t *conn, char *reply,
int connection_exit_begin_conn(cell_t *cell, circuit_t *circ) { int connection_exit_begin_conn(cell_t *cell, circuit_t *circ) {
connection_t *n_stream; connection_t *n_stream;
relay_header_t rh; relay_header_t rh;
char *colon; char *address=NULL;
uint16_t port;
assert_circuit_ok(circ); assert_circuit_ok(circ);
relay_header_unpack(&rh, cell->payload); relay_header_unpack(&rh, cell->payload);
@ -750,15 +751,13 @@ int connection_exit_begin_conn(cell_t *cell, circuit_t *circ) {
log_fn(LOG_WARN,"relay begin cell has no \\0. Dropping."); log_fn(LOG_WARN,"relay begin cell has no \\0. Dropping.");
return 0; return 0;
} }
colon = strchr(cell->payload+RELAY_HEADER_SIZE, ':'); if (parse_addr_port(cell->payload+RELAY_HEADER_SIZE, &address, NULL,&port)<0){
if(!colon) { log_fn(LOG_WARN,"Unable to parse addr:port in relay begin cell. Dropping.");
log_fn(LOG_WARN,"relay begin cell has no colon. Dropping.");
return 0; return 0;
} }
*colon = 0; if (port==0) {
log_fn(LOG_WARN,"Missing port in relay begin cell. Dropping.");
if(!atoi(colon+1)) { /* bad port */ tor_free(address);
log_fn(LOG_WARN,"relay begin cell has invalid port '%s'. Dropping.", colon+1);
return 0; return 0;
} }
@ -767,7 +766,7 @@ int connection_exit_begin_conn(cell_t *cell, circuit_t *circ) {
n_stream->purpose = EXIT_PURPOSE_CONNECT; n_stream->purpose = EXIT_PURPOSE_CONNECT;
n_stream->stream_id = rh.stream_id; n_stream->stream_id = rh.stream_id;
n_stream->port = atoi(colon+1); n_stream->port = port;
/* leave n_stream->s at -1, because it's not yet valid */ /* leave n_stream->s at -1, because it's not yet valid */
n_stream->package_window = STREAMWINDOW_START; n_stream->package_window = STREAMWINDOW_START;
n_stream->deliver_window = STREAMWINDOW_START; n_stream->deliver_window = STREAMWINDOW_START;
@ -784,6 +783,7 @@ int connection_exit_begin_conn(cell_t *cell, circuit_t *circ) {
connection_edge_end(n_stream, END_STREAM_REASON_EXITPOLICY, n_stream->cpath_layer); connection_edge_end(n_stream, END_STREAM_REASON_EXITPOLICY, n_stream->cpath_layer);
connection_free(n_stream); connection_free(n_stream);
circuit_mark_for_close(circ); /* knock the whole thing down, somebody screwed up */ circuit_mark_for_close(circ); /* knock the whole thing down, somebody screwed up */
tor_free(address);
return 0; return 0;
} }
assert_circuit_ok(circ); assert_circuit_ok(circ);
@ -796,9 +796,10 @@ int connection_exit_begin_conn(cell_t *cell, circuit_t *circ) {
assert_circuit_ok(circ); assert_circuit_ok(circ);
connection_exit_connect(n_stream); connection_exit_connect(n_stream);
tor_free(address);
return 0; return 0;
} }
n_stream->address = tor_strdup(cell->payload + RELAY_HEADER_SIZE); n_stream->address = address;
n_stream->state = EXIT_CONN_STATE_RESOLVEFAILED; n_stream->state = EXIT_CONN_STATE_RESOLVEFAILED;
/* default to failed, change in dns_resolve if it turns out not to fail */ /* default to failed, change in dns_resolve if it turns out not to fail */

View File

@ -127,9 +127,10 @@ static void add_service(rend_service_t *service)
*/ */
static rend_service_port_config_t *parse_port_config(const char *string) static rend_service_port_config_t *parse_port_config(const char *string)
{ {
int virtport, realport, r; int virtport;
struct in_addr addr; uint16_t realport;
char *endptr, *colon, *addrstring; uint32_t addr;
char *endptr;
rend_service_port_config_t *result; rend_service_port_config_t *result;
virtport = (int) strtol(string, &endptr, 10); virtport = (int) strtol(string, &endptr, 10);
@ -145,45 +146,28 @@ static rend_service_port_config_t *parse_port_config(const char *string)
if (!*string) { if (!*string) {
/* No addr:port part; use default. */ /* No addr:port part; use default. */
realport = virtport; realport = virtport;
addr.s_addr = htonl(0x7F000001u); /* 127.0.0.1 */ addr = 0x7F000001u; /* 127.0.0.1 */
} else { } else if (strchr(string, ':') || strchr(string, '.')) {
colon = strchr(string, ':'); if (parse_addr_port(string, NULL, &addr, &realport)<0) {
if (colon) { log_fn(LOG_WARN,"Unparseable address in hidden service port configuration");
/* Try to parse addr:port. */ return NULL;
addrstring = tor_strndup(string, colon-string);
r = tor_inet_aton(addrstring, &addr);
tor_free(addrstring);
if (!r) {
log_fn(LOG_WARN,"Unparseable address in hidden service port configuration");
return NULL;
}
realport = strtol(colon+1, &endptr, 10);
if (*endptr) {
log_fn(LOG_WARN,"Unparseable or missing port in hidden service port configuration.");
return NULL;
}
} else if (strchr(string, '.') && tor_inet_aton(string, &addr)) {
/* We have addr; use deafult port. */
realport = virtport;
} else {
/* No addr:port, no addr -- must be port. */
realport = strtol(string, &endptr, 10);
if (*endptr) {
log_fn(LOG_WARN, "Unparseable of missing port in hidden service port configuration.");
return NULL;
}
addr.s_addr = htonl(0x7F000001u); /* Default to 127.0.0.1 */
} }
} if (!realport)
if (realport < 1 || realport > 65535) { realport = virtport;
log_fn(LOG_WARN, "Port out of range in hidden service port configuration."); } else {
return NULL; /* No addr:port, no addr -- must be port. */
realport = strtol(string, &endptr, 10);
if (*endptr) {
log_fn(LOG_WARN, "Unparseable of missing port in hidden service port configuration.");
return NULL;
}
addr = 0x7F000001u; /* Default to 127.0.0.1 */
} }
result = tor_malloc(sizeof(rend_service_port_config_t)); result = tor_malloc(sizeof(rend_service_port_config_t));
result->virtual_port = virtport; result->virtual_port = virtport;
result->real_port = realport; result->real_port = realport;
result->real_address = (uint32_t) ntohl(addr.s_addr); result->real_address = addr;
return result; return result;
} }