summaryrefslogtreecommitdiff
path: root/src/common/address.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2011-05-13 10:43:41 -0400
committerNick Mathewson <nickm@torproject.org>2011-05-13 10:43:41 -0400
commit5f2a1a7b4f20be121cd30def95cf7789924ca70a (patch)
treec9545f5af482e1380261a2fe7ba2e73b175b3b66 /src/common/address.c
parente0d5a6e1849673589c5b7f04d89e25194167344d (diff)
parent7f654a6a6fb5e956b996eece36ff95e590a6ad63 (diff)
downloadtor-5f2a1a7b4f20be121cd30def95cf7789924ca70a.tar.gz
tor-5f2a1a7b4f20be121cd30def95cf7789924ca70a.zip
Merge branch 'feature3076_squashed' into maint-0.2.2
Diffstat (limited to 'src/common/address.c')
-rw-r--r--src/common/address.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/common/address.c b/src/common/address.c
index 34e109fcf4..1c725393d9 100644
--- a/src/common/address.c
+++ b/src/common/address.c
@@ -43,6 +43,9 @@
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h> /* FreeBSD needs this to know what version it is */
#endif
+#ifdef HAVE_SYS_UN_H
+#include <sys/un.h>
+#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -120,6 +123,33 @@ tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
return 0;
}
+/** Return a newly allocated string holding the address described in
+ * <b>sa</b>. AF_UNIX, AF_UNSPEC, AF_INET, and AF_INET6 are supported. */
+char *
+tor_sockaddr_to_str(const struct sockaddr *sa)
+{
+ char address[TOR_ADDR_BUF_LEN];
+ char *result;
+ tor_addr_t addr;
+ uint16_t port;
+#ifdef HAVE_SYS_UN_H
+ if (sa->sa_family == AF_UNIX) {
+ struct sockaddr_un *s_un = (struct sockaddr_un *)sa;
+ tor_asprintf(&result, "unix:%s", s_un->sun_path);
+ return result;
+ }
+#endif
+ if (sa->sa_family == AF_UNSPEC)
+ return tor_strdup("unspec");
+
+ if (tor_addr_from_sockaddr(&addr, sa, &port) < 0)
+ return NULL;
+ if (! tor_addr_to_str(address, &addr, sizeof(address), 1))
+ return NULL;
+ tor_asprintf(&result, "%s:%d", address, (int)port);
+ return result;
+}
+
/** Set address <b>a</b> to the unspecified address. This address belongs to
* no family. */
void