summaryrefslogtreecommitdiff
path: root/src/print_ip_addr.c
diff options
context:
space:
mode:
authorEmeric Planet <emeric.planet@datadoghq.com>2017-12-11 11:38:31 +0100
committerMichael Stapelberg <stapelberg@users.noreply.github.com>2017-12-11 11:38:31 +0100
commit53fb9b4f18cd9ee3a64be5084ccc76aeea279515 (patch)
tree7e87969fb73094b851f037c8fae3547ec579df46 /src/print_ip_addr.c
parentc3424e10bee91a4e0f21e7f0b72975e938e3f501 (diff)
Add IPv6 address when IPv4 isn't available (#247)
Diffstat (limited to 'src/print_ip_addr.c')
-rw-r--r--src/print_ip_addr.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/print_ip_addr.c b/src/print_ip_addr.c
index 09b0992..f9cd1f4 100644
--- a/src/print_ip_addr.c
+++ b/src/print_ip_addr.c
@@ -17,9 +17,14 @@
* interface is up and running but hasn't got an IP address yet
*
*/
-const char *get_ip_addr(const char *interface) {
+const char *get_ip_addr(const char *interface, int family) {
static char part[512];
- socklen_t len = sizeof(struct sockaddr_in);
+ socklen_t len = 0;
+ if (family == AF_INET)
+ len = sizeof(struct sockaddr_in);
+ else if (family == AF_INET6)
+ len = sizeof(struct sockaddr_in6);
+
memset(part, 0, sizeof(part));
struct ifaddrs *ifaddr, *addrp;
@@ -30,13 +35,13 @@ const char *get_ip_addr(const char *interface) {
if (ifaddr == NULL)
return NULL;
- /* Skip until we are at the AF_INET address of interface */
+ /* Skip until we are at the input family address of interface */
for (addrp = ifaddr;
(addrp != NULL &&
(strcmp(addrp->ifa_name, interface) != 0 ||
addrp->ifa_addr == NULL ||
- addrp->ifa_addr->sa_family != AF_INET));
+ addrp->ifa_addr->sa_family != family));
addrp = addrp->ifa_next) {
/* Check if the interface is down */