Skip to content

Commit

Permalink
utils: Add useful function for later use
Browse files Browse the repository at this point in the history
Signed-off-by: David Goulet <[email protected]>
  • Loading branch information
dgoulet committed Feb 24, 2017
1 parent 3891910 commit 98e8421
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,31 @@ int utils_get_port_from_addr(const struct sockaddr *sa)

return port;
}

/*
* For a given sockaddr, return a const pointer to the address data structure.
* Return NULL if family is not IPv4 or IPv6.
*/
ATTR_HIDDEN
const char *utils_get_addr_from_sockaddr(const struct sockaddr *sa)
{
static char buf[256];
const void *addrp;

assert(sa);

memset(buf, 0, sizeof(buf));

if (sa->sa_family == AF_INET) {
addrp = &((const struct sockaddr_in *) sa)->sin_addr;
} else if (sa->sa_family == AF_INET6) {
addrp = &((const struct sockaddr_in6 *) sa)->sin6_addr;
} else {
goto end;
}

inet_ntop(sa->sa_family, addrp, buf, sizeof(buf));

end:
return buf;
}
7 changes: 4 additions & 3 deletions src/common/utils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (C) 2000-2008 - Shaun Clowes <[email protected]>
* 2008-2011 - Robert Hogan <[email protected]>
* 2013 - David Goulet <[email protected]>
* Copyright (C) 2000-2008 - Shaun Clowes <[email protected]>
* 2008-2011 - Robert Hogan <[email protected]>
* 2013 - David Goulet <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 2 only, as
Expand Down Expand Up @@ -34,5 +34,6 @@ int utils_sockaddr_is_localhost(const struct sockaddr *sa);
int utils_localhost_resolve(const char *name, int af, void *buf, size_t len);
int utils_is_addr_any(const struct sockaddr *sa);
int utils_get_port_from_addr(const struct sockaddr *sa);
const char *utils_get_addr_from_sockaddr(const struct sockaddr *sa);

#endif /* TORSOCKS_UTILS_H */

0 comments on commit 98e8421

Please sign in to comment.