ifa_addr can be NULL.
[darkstat] / localip.c
1 /* darkstat 3
2 * copyright (c) 2001-2011 Emil Mikulic.
3 *
4 * localip.c: determine local IP of our capture interface
5 *
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
8 */
9
10 #include "addr.h"
11 #include "config.h" /* for HAVE_IFADDRS_H */
12 #include "err.h"
13 #include "localip.h"
14 #include "bsd.h" /* for strlcpy */
15
16 #include <sys/socket.h>
17 #include <net/if.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #ifdef HAVE_IFADDRS_H
23 # include <ifaddrs.h>
24 #else
25 # ifdef HAVE_SYS_SOCKIO_H
26 # include <sys/sockio.h> /* for SIOCGIFADDR, especially on Solaris */
27 # endif
28 # include <sys/ioctl.h>
29 #endif
30
31 static const char *iface = NULL;
32 struct addr localip4, localip6;
33 static struct addr last_localip4, last_localip6;
34
35 void
36 localip_init(const char *interface)
37 {
38 iface = interface;
39 localip_update();
40 }
41
42 void
43 localip_update(void)
44 {
45 /* defaults */
46 localip4.family = IPv4;
47 localip4.ip.v4 = 0;
48
49 localip6.family = IPv6;
50 memset(&(localip6.ip.v6), 0, sizeof(localip6.ip.v6));
51
52 if (iface != NULL) {
53 /* not reading from capfile */
54 #ifdef HAVE_IFADDRS_H
55 int got_v4 = 0, got_v6 = 0;
56 struct ifaddrs *ifas, *ifa;
57
58 if (getifaddrs(&ifas) < 0)
59 err(1, "can't get own IP address on interface \"%s\"", iface);
60
61 for (ifa = ifas; ifa; ifa = ifa->ifa_next) {
62 if (got_v4 && got_v6)
63 break; /* Task is already complete. */
64
65 if (strncmp(ifa->ifa_name, iface, IFNAMSIZ))
66 continue; /* Wrong interface. */
67
68 if (!ifa->ifa_addr)
69 continue; /* This can be NULL, e.g. for ppp0. */
70
71 /* The first IPv4 name is always functional. */
72 if ((ifa->ifa_addr->sa_family == AF_INET) && !got_v4)
73 {
74 /* Good IPv4 address. */
75 localip4.ip.v4 =
76 ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
77 got_v4 = 1;
78 continue;
79 }
80
81 /* IPv6 needs some obvious exceptions. */
82 if ( ifa->ifa_addr->sa_family == AF_INET6 ) {
83 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
84
85 if ( IN6_IS_ADDR_LINKLOCAL(&(sa6->sin6_addr))
86 || IN6_IS_ADDR_SITELOCAL(&(sa6->sin6_addr)) )
87 continue;
88
89 /* Only standard IPv6 can reach this point. */
90 memcpy(&(localip6.ip.v6), &sa6->sin6_addr, sizeof(localip6.ip.v6));
91 got_v6 = 1;
92 }
93 }
94
95 freeifaddrs(ifas);
96
97 /* Report an error if IPv4 address could not be found. */
98 if (!got_v4)
99 err(1, "can't get own IPv4 address on interface \"%s\"", iface);
100
101 #else /* don't HAVE_IFADDRS_H */
102
103 int tmp = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
104 struct ifreq ifr;
105 struct sockaddr sa;
106
107 strlcpy(ifr.ifr_name, iface, IFNAMSIZ);
108 ifr.ifr_addr.sa_family = AF_INET;
109 if (ioctl(tmp, SIOCGIFADDR, &ifr) == -1) {
110 if (errno == EADDRNOTAVAIL) {
111 verbosef("lost local IP");
112 } else
113 err(1, "can't get own IP address on interface \"%s\"", iface);
114 } else {
115 /* success! */
116 sa = ifr.ifr_addr;
117 localip4.ip.v4 = ((struct sockaddr_in*)&sa)->sin_addr.s_addr;
118 }
119 close(tmp);
120
121 #endif
122 }
123
124 if (!addr_equal(&last_localip4, &localip4)) {
125 verbosef("localip4 update(%s) = %s", iface, addr_to_str(&localip4));
126 last_localip4 = localip4;
127 }
128 if (!addr_equal(&last_localip6, &localip6)) {
129 verbosef("localip6 update(%s) = %s", iface, addr_to_str(&localip6));
130 last_localip6 = localip6;
131 }
132 }
133
134 /* vim:set ts=3 sw=3 tw=78 expandtab: */