1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
--- ez-ipupdate.c.orig
+++ ez-ipupdate.c
@@ -172,6 +172,17 @@
# ifdef HAVE_SYS_SOCKIO_H
# include <sys/sockio.h>
# endif
+# ifdef __linux__
+/*# include <linux/if.h> */
+# include <linux/netlink.h>
+# include <linux/rtnetlink.h>
+/* Under Linux, we reopen socket in get_if_addr() every time */
+# define socketopen(sock)
+# define socketclose(sock)
+# else
+# define socketopen(sock) sock = socket(AF_INET, SOCK_STREAM, 0)
+# define socketclose(sock) close(sock)
+# endif
#endif
#include <dprintf.h>
@@ -1605,6 +1616,114 @@
int get_if_addr(int sock, char *name, struct sockaddr_in *sin)
{
#ifdef IF_LOOKUP
+#ifdef __linux__
+ struct {
+ struct nlmsghdr nlmsg_info;
+ struct ifaddrmsg ifaddrmsg_info;
+ char buffer[2048];
+ } req;
+ struct nlmsghdr *curr;
+ int len;
+ char buf[8192];
+
+ /* open a socket and bind it.
+ Under non-linux, the socket can be kept open, but it seems under
+ linux we cannot use the same socket for several requests reliable
+ [although sometimes it works...] */
+ static struct sockaddr_nl local;
+ sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+ if(sock < 0) {
+ perror("socket");
+ return -1;
+ }
+ local.nl_family = AF_NETLINK;
+ local.nl_pad = 0;
+ local.nl_pid = getpid();
+ local.nl_groups = 0;
+ if(bind(sock, (struct sockaddr*) &local, sizeof(local)) < 0) {
+ perror("bind");
+ close(sock);
+ return -1;
+ }
+
+ memset(&req, 0, sizeof(req));
+ req.nlmsg_info.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
+ req.nlmsg_info.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
+ req.nlmsg_info.nlmsg_type = RTM_GETADDR;
+ req.nlmsg_info.nlmsg_pid = getpid();
+ req.ifaddrmsg_info.ifa_family = AF_INET;
+ if(send(sock, &req, req.nlmsg_info.nlmsg_len, 0) < 0) {
+ perror("sendmsg(sock)");
+ close(sock);
+ return -1;
+ }
+
+ len = recv(sock, buf, sizeof(buf), 0);
+ close(sock);
+ if(len < 0) {
+ perror("recv");
+ return -1;
+ } else if(len == 0) {
+ dprintf((stderr, "No interfaces found"));
+ return -1;
+ }
+
+ /* Initialize sin except for address */
+ bzero(sin, sizeof(struct sockaddr_in));
+ sin->sin_family = AF_INET;
+
+ /* We take the last non-private IP with matching name */
+ int found = 0;
+ curr = (struct nlmsghdr *) buf;
+ for(; NLMSG_OK(curr, len); curr = NLMSG_NEXT(curr, len)) {
+ struct ifaddrmsg *curraddr = (struct ifaddrmsg *) NLMSG_DATA(curr);
+ struct rtattr *datalist = (struct rtattr *) IFA_RTA(curraddr);
+ int datalen = IFA_PAYLOAD(curr);
+ int mystat = 0;
+ struct in_addr sin_addr;
+ in_addr_t addr;
+ for(; RTA_OK(datalist, datalen); datalist = RTA_NEXT(datalist, datalen)) {
+ switch(datalist->rta_type) {
+ case IFA_LABEL:
+ if(strcmp((char *)RTA_DATA(datalist), name) != 0)
+ mystat = -1;
+ break;
+ case IFA_LOCAL:
+ addr = ((struct in_addr *)RTA_DATA(datalist))->s_addr;
+ /* addr: 192.168.0.0/16 || 172.16.0.0/12 || 10.0.0.0/8 */
+ if(((addr & 0xFFFF) == 0xA8C0)
+ || ((addr & 0xF0FF) == 0x10AC)
+ || ((addr & 0xFF) == 0x0A)) {
+ mystat = -1;
+ }
+ else {
+ /* We must not store yet sin->sin_addr, since name might not match */
+ sin_addr = *((struct in_addr *)RTA_DATA(datalist));
+ mystat = 1;
+ }
+ break;
+ default:
+ break;
+ }
+ if(mystat < 0)
+ break;
+ }
+ if(mystat > 0) {
+ sin->sin_addr = sin_addr;
+ found = 1;
+ /* If you want to take the first non-private IP with matching name
+ uncomment the next break command:
+ break; */
+ }
+ }
+ if(found) {
+ dprintf((stderr, "%s: %s\n", name, inet_ntoa(sin->sin_addr)));
+ return 0;
+ }
+ dprintf((stderr, "%s: %s\n", name, "has no non-private address"));
+ return -1;
+#else
+/* ifndef __linux__ */
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
@@ -1638,7 +1757,10 @@
return -1;
}
return -1;
+#endif
+/* endif __linux__ */
#else
+/* ifndef IF_LOOKUP */
return -1;
#endif
}
@@ -4490,7 +4612,7 @@
#ifdef IF_LOOKUP
if(options & OPT_DAEMON)
{
- sock = socket(AF_INET, SOCK_STREAM, 0);
+ socketopen(sock);
}
#endif
@@ -4745,12 +4867,12 @@
struct sockaddr_in sin;
int sock;
- sock = socket(AF_INET, SOCK_STREAM, 0);
+ socketopen(sock);
if(get_if_addr(sock, interface, &sin) != 0)
{
exit(1);
}
- close(sock);
+ socketclose(sock);
snprintf(ipbuf, sizeof(ipbuf), "%s", inet_ntoa(sin.sin_addr));
#else
fprintf(stderr, "interface lookup not enabled at compile time\n");
@@ -4791,7 +4913,7 @@
struct sockaddr_in sin;
int sock;
- sock = socket(AF_INET, SOCK_STREAM, 0);
+ socketopen(sock);
if(get_if_addr(sock, interface, &sin) == 0)
{
if(address) { free(address); }
@@ -4802,7 +4924,7 @@
show_message("could not resolve ip address for %s.\n", interface);
exit(1);
}
- close(sock);
+ socketclose(sock);
}
for(i=0; i<ntrys; i++)
@@ -4846,12 +4968,12 @@
struct sockaddr_in sin;
int sock;
- sock = socket(AF_INET, SOCK_STREAM, 0);
+ socketopen(sock);
if(get_if_addr(sock, interface, &sin) != 0)
{
exit(1);
}
- close(sock);
+ socketclose(sock);
snprintf(ipbuf, sizeof(ipbuf), "%s", inet_ntoa(sin.sin_addr));
#else
fprintf(stderr, "interface lookup not enabled at compile time\n");
@@ -4878,7 +5000,7 @@
}
#ifdef IF_LOOKUP
- if(sock > 0) { close(sock); }
+ if(sock > 0) { socketclose(sock); }
#endif
if(address) { free(address); }
|