| 1 |
/* |
| 2 |
* Copyright (c) 2017, Arnon Yaari |
| 3 |
* All rights reserved. |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
/*! Based on code from |
| 9 |
https://lists.samba.org/archive/samba-technical/2009-February/063079.html |
| 10 |
!*/ |
| 11 |
|
| 12 |
#include <string.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <unistd.h> |
| 15 |
#include <net/if.h> |
| 16 |
#include <netinet/in.h> |
| 17 |
#include <sys/ioctl.h> |
| 18 |
#include <sys/types.h> |
| 19 |
#include <sys/socket.h> |
| 20 |
|
| 21 |
#include "ifaddrs.h" |
| 22 |
|
| 23 |
#define MAX(x,y) ((x)>(y)?(x):(y)) |
| 24 |
#define SIZE(p) MAX((p).sa_len,sizeof(p)) |
| 25 |
|
| 26 |
|
| 27 |
static struct sockaddr * |
| 28 |
sa_dup(struct sockaddr *sa1) |
| 29 |
{ |
| 30 |
struct sockaddr *sa2; |
| 31 |
size_t sz = sa1->sa_len; |
| 32 |
sa2 = (struct sockaddr *) calloc(1, sz); |
| 33 |
if (sa2 == NULL) |
| 34 |
return NULL; |
| 35 |
memcpy(sa2, sa1, sz); |
| 36 |
return sa2; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
void freeifaddrs(struct ifaddrs *ifp) |
| 41 |
{ |
| 42 |
if (NULL == ifp) return; |
| 43 |
free(ifp->ifa_name); |
| 44 |
free(ifp->ifa_addr); |
| 45 |
free(ifp->ifa_netmask); |
| 46 |
free(ifp->ifa_dstaddr); |
| 47 |
freeifaddrs(ifp->ifa_next); |
| 48 |
free(ifp); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
int getifaddrs(struct ifaddrs **ifap) |
| 53 |
{ |
| 54 |
int sd, ifsize; |
| 55 |
char *ccp, *ecp; |
| 56 |
struct ifconf ifc; |
| 57 |
struct ifreq *ifr; |
| 58 |
struct ifaddrs *cifa = NULL; /* current */ |
| 59 |
struct ifaddrs *pifa = NULL; /* previous */ |
| 60 |
const size_t IFREQSZ = sizeof(struct ifreq); |
| 61 |
int fam; |
| 62 |
|
| 63 |
*ifap = NULL; |
| 64 |
|
| 65 |
sd = socket(AF_INET, SOCK_DGRAM, 0); |
| 66 |
if (sd == -1) |
| 67 |
goto error; |
| 68 |
|
| 69 |
/* find how much memory to allocate for the SIOCGIFCONF call */ |
| 70 |
if (ioctl(sd, SIOCGSIZIFCONF, (caddr_t)&ifsize) < 0) |
| 71 |
goto error; |
| 72 |
|
| 73 |
ifc.ifc_req = (struct ifreq *) calloc(1, ifsize); |
| 74 |
if (ifc.ifc_req == NULL) |
| 75 |
goto error; |
| 76 |
ifc.ifc_len = ifsize; |
| 77 |
|
| 78 |
if (ioctl(sd, SIOCGIFCONF, &ifc) < 0) |
| 79 |
goto error; |
| 80 |
|
| 81 |
ccp = (char *)ifc.ifc_req; |
| 82 |
ecp = ccp + ifsize; |
| 83 |
|
| 84 |
while (ccp < ecp) { |
| 85 |
|
| 86 |
ifr = (struct ifreq *) ccp; |
| 87 |
ifsize = sizeof(ifr->ifr_name) + SIZE(ifr->ifr_addr); |
| 88 |
fam = ifr->ifr_addr.sa_family; |
| 89 |
|
| 90 |
if (fam == AF_INET || fam == AF_INET6) { |
| 91 |
cifa = (struct ifaddrs *) calloc(1, sizeof(struct ifaddrs)); |
| 92 |
if (cifa == NULL) |
| 93 |
goto error; |
| 94 |
cifa->ifa_next = NULL; |
| 95 |
|
| 96 |
if (pifa == NULL) *ifap = cifa; /* first one */ |
| 97 |
else pifa->ifa_next = cifa; |
| 98 |
|
| 99 |
cifa->ifa_name = strdup(ifr->ifr_name); |
| 100 |
if (cifa->ifa_name == NULL) |
| 101 |
goto error; |
| 102 |
cifa->ifa_flags = 0; |
| 103 |
cifa->ifa_dstaddr = NULL; |
| 104 |
|
| 105 |
cifa->ifa_addr = sa_dup(&ifr->ifr_addr); |
| 106 |
if (cifa->ifa_addr == NULL) |
| 107 |
goto error; |
| 108 |
|
| 109 |
if (fam == AF_INET) { |
| 110 |
if (ioctl(sd, SIOCGIFNETMASK, ifr, IFREQSZ) < 0) |
| 111 |
goto error; |
| 112 |
cifa->ifa_netmask = sa_dup(&ifr->ifr_addr); |
| 113 |
if (cifa->ifa_netmask == NULL) |
| 114 |
goto error; |
| 115 |
} |
| 116 |
|
| 117 |
if (0 == ioctl(sd, SIOCGIFFLAGS, ifr)) /* optional */ |
| 118 |
cifa->ifa_flags = ifr->ifr_flags; |
| 119 |
|
| 120 |
if (fam == AF_INET) { |
| 121 |
if (ioctl(sd, SIOCGIFDSTADDR, ifr, IFREQSZ) < 0) { |
| 122 |
if (0 == ioctl(sd, SIOCGIFBRDADDR, ifr, IFREQSZ)) { |
| 123 |
cifa->ifa_dstaddr = sa_dup(&ifr->ifr_addr); |
| 124 |
if (cifa->ifa_dstaddr == NULL) |
| 125 |
goto error; |
| 126 |
} |
| 127 |
} |
| 128 |
else { |
| 129 |
cifa->ifa_dstaddr = sa_dup(&ifr->ifr_addr); |
| 130 |
if (cifa->ifa_dstaddr == NULL) |
| 131 |
goto error; |
| 132 |
} |
| 133 |
} |
| 134 |
pifa = cifa; |
| 135 |
} |
| 136 |
|
| 137 |
ccp += ifsize; |
| 138 |
} |
| 139 |
free(ifc.ifc_req); |
| 140 |
close(sd); |
| 141 |
return 0; |
| 142 |
error: |
| 143 |
if (ifc.ifc_req != NULL) |
| 144 |
free(ifc.ifc_req); |
| 145 |
if (sd != -1) |
| 146 |
close(sd); |
| 147 |
freeifaddrs(*ifap); |
| 148 |
return (-1); |
| 149 |
} |