aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2004-10-12 15:48:30 +0000
committerNick Mathewson <nickm@torproject.org>2004-10-12 15:48:30 +0000
commit8b037509f3b6f065a467900a5f4d01e21b98f152 (patch)
tree24d57f0050d07a280dc2700e76e9d42c6263ef70 /src/common
parent1efad74164fb02fbe493bd2c0366936940c879aa (diff)
downloadtor-8b037509f3b6f065a467900a5f4d01e21b98f152.tar
tor-8b037509f3b6f065a467900a5f4d01e21b98f152.tar.gz
Add functions to parse addr[:port] consistently
svn:r2440
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util.c56
-rw-r--r--src/common/util.h2
2 files changed, 58 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 5d5f3b554..4391ac241 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -2028,6 +2028,62 @@ int tor_lookup_hostname(const char *name, uint32_t *addr)
}
}
+/** Parse a string of the form "host[:port]" from <b>addrport</b>. If
+ * <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>. If <b>port</b> is provided, store the port number
+ * into *<b>port</b>, or 0 if no port is given. Return 0 on success,
+ * -1 on failure.
+ */
+int
+parse_addr_port(const char *addrport, char **address, uint32_t *addr,
+ uint16_t *port)
+{
+ const char *colon;
+ char *_address = NULL;
+ int _port;
+ int ok = 1;
+
+ tor_assert(addrport);
+ tor_assert(port);
+
+ colon = strchr(addrport, ':');
+ if (colon) {
+ _address = tor_strndup(addrport, colon-addrport);
+ _port = atoi(colon+1);
+ if (_port<1 || _port>65536) {
+ log_fn(LOG_WARN, "Port '%s' out of range", colon+1);
+ _port = 0;
+ ok = 0;
+ }
+ } else {
+ _address = tor_strdup(addrport);
+ _port = 0;
+ }
+
+ if (addr) {
+ /* There's an addr pointer, so we need to resolve the hostname. */
+ if (tor_lookup_hostname(_address,addr)) {
+ log_fn(LOG_WARN, "Couldn't look up '%s'", _address);
+ ok = 0;
+ *addr = 0;
+ }
+ }
+
+ if (address && ok) {
+ *address = _address;
+ } else {
+ if (address)
+ *address = NULL;
+ tor_free(_address);
+ }
+ if (port)
+ *port = ok ? ((uint16_t) _port) : 0;
+
+ return ok ? 0 : -1;
+}
+
#ifndef MS_WINDOWS
struct tor_mutex_t {
};
diff --git a/src/common/util.h b/src/common/util.h
index 2b13f92b4..d3a3d6241 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -260,6 +260,8 @@ int switch_id(char *user, char *group);
struct in_addr;
int tor_inet_aton(const char *cp, struct in_addr *addr);
int tor_lookup_hostname(const char *name, uint32_t *addr);
+int parse_addr_port(const char *addrport, char **address, uint32_t *addr,
+ uint16_t *port);
/* For stupid historical reasons, windows sockets have an independent
* set of errnos, and an independent way to get them. Also, you can't