parse_addr_port was vague about what to do when port_out was NULL. Make it behave usefully.

svn:r4716
This commit is contained in:
Nick Mathewson 2005-08-05 01:51:19 +00:00
parent 35b04df4fd
commit 666ab41e2b

View File

@ -1135,13 +1135,15 @@ int is_local_IP(uint32_t ip) {
* <b>address</b> is provided, set *<b>address</b> to a copy of the
* host portion of the string. If <b>addr</b> is provided, try to
* resolve the host portion of the string and store it into
* *<b>addr</b> (in host byte order). If <b>port</b> is provided,
* store the port number into *<b>port</b>, or 0 if no port is given.
* *<b>addr</b> (in host byte order). If <b>port_out</b> is provided,
* store the port number into *<b>port_out</b>, or 0 if no port is given.
* If <b>port_out</b> is NULL, then there must be no port number in
* <b>addrport</b>.
* Return 0 on success, -1 on failure.
*/
int
parse_addr_port(const char *addrport, char **address, uint32_t *addr,
uint16_t *port)
uint16_t *port_out)
{
const char *colon;
char *_address = NULL;
@ -1149,7 +1151,6 @@ parse_addr_port(const char *addrport, char **address, uint32_t *addr,
int ok = 1;
tor_assert(addrport);
tor_assert(port);
colon = strchr(addrport, ':');
if (colon) {
@ -1159,6 +1160,11 @@ parse_addr_port(const char *addrport, char **address, uint32_t *addr,
log_fn(LOG_WARN, "Port '%s' out of range", colon+1);
ok = 0;
}
if (!port_out) {
log_fn(LOG_WARN, "Port '%s' given on '%s' when not required", colon+1,
addrport);
ok = 0;
}
} else {
_address = tor_strdup(addrport);
_port = 0;
@ -1181,8 +1187,8 @@ parse_addr_port(const char *addrport, char **address, uint32_t *addr,
*address = NULL;
tor_free(_address);
}
if (port)
*port = ok ? ((uint16_t) _port) : 0;
if (port_out)
*port_out = ok ? ((uint16_t) _port) : 0;
return ok ? 0 : -1;
}