mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-14 07:03:44 +01:00
Add a new "tor_sockaddr_to_str()" function
It does what it says on the tin. It turns out I'll want this in a couple of places.
This commit is contained in:
parent
3da661b242
commit
28cc7b0180
@ -43,6 +43,9 @@
|
|||||||
#ifdef HAVE_SYS_PARAM_H
|
#ifdef HAVE_SYS_PARAM_H
|
||||||
#include <sys/param.h> /* FreeBSD needs this to know what version it is */
|
#include <sys/param.h> /* FreeBSD needs this to know what version it is */
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef HAVE_SYS_UN_H
|
||||||
|
#include <sys/un.h>
|
||||||
|
#endif
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -120,6 +123,33 @@ tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
|
|||||||
return 0;
|
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
|
/** Set address <b>a</b> to the unspecified address. This address belongs to
|
||||||
* no family. */
|
* no family. */
|
||||||
void
|
void
|
||||||
|
@ -44,6 +44,7 @@ socklen_t tor_addr_to_sockaddr(const tor_addr_t *a, uint16_t port,
|
|||||||
int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
|
int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa,
|
||||||
uint16_t *port_out);
|
uint16_t *port_out);
|
||||||
void tor_addr_make_unspec(tor_addr_t *a);
|
void tor_addr_make_unspec(tor_addr_t *a);
|
||||||
|
char *tor_sockaddr_to_str(const struct sockaddr *sa);
|
||||||
|
|
||||||
/** Return an in6_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
|
/** Return an in6_addr* equivalent to <b>a</b>, or NULL if <b>a</b> is not
|
||||||
* an IPv6 address. */
|
* an IPv6 address. */
|
||||||
|
Loading…
Reference in New Issue
Block a user