/
ddnsclient.c
296 lines (248 loc) · 6.61 KB
/
ddnsclient.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <signal.h>
#include <errno.h>
#include "ddnsclient.h"
#include "config.h" //Contains username, hostname and password for now
//Store most recent address to avoid duplicate update requests (Some servers block after too many nochg-Requests)
static uint16_t last_address[8] = { 0 };
//Print ipv6 in human-readable format into dst
static void
sprintf_ipv6(char* dst, const void *ipv6addr)
{
const uint16_t *data = ipv6addr;
const char *p = dst;
int i = 0;
for (i = 0; i < 7; i++) dst += snprintf(dst, 40 - (p - dst), "%x:", ntohs(data[i]));
snprintf(dst, 40 - (p - dst), "%x", ntohs(data[7]));
}
//Send request to server to store address
static void
activate_ip(const void *address)
{
//Do not update again if address has been sent before.
if (memcmp(address, last_address, 16) == 0)
{
if(verbose_mode)
{
log_ipv6(address);
log(" <<< already active\n");
}
return;
}
memcpy(last_address, address, 16);
//Log update
if(!verbose_mode) log_time();
log_ipv6(address);
log(" <<< activating\n");
char ipstring[40];
sprintf_ipv6(ipstring, address);
send_update_request(config_hostname, ipstring, config_username, config_password);
}
#define STRBOOL(f) ((f) ? #f" " : "")
static void
filter_ip(void *address, int ifa_flags, int ifa_scope, int preferred_time)
{
bool deprecated = ifa_flags & IFA_F_DEPRECATED;
bool temporary = ifa_flags & IFA_F_TEMPORARY;
bool global = ifa_scope == RT_SCOPE_UNIVERSE;
if (deprecated || temporary || !global) //filter old and temporary IPs
{
if(verbose_mode)
{
bool local = !global;
log_ipv6(address);
log(" rejected for being %s%s%s\n", STRBOOL(deprecated), STRBOOL(temporary), STRBOOL(local));
}
return;
}
else if (preferred_time <= 0)
{
if(verbose_mode)
{
log_ipv6(address);
log(" rejected for not preferred (time expired: %d)\n", preferred_time);
}
return;
}
else //accept and process further
{
log_ipv6(address);
if(verbose_mode) log("\n\tflags: dep: %d temp: %d global: %d pref: %d\n", deprecated, temporary, global, preferred_time);
activate_ip(address);
}
}
static struct
{
struct nlmsghdr nlmsg_info;
struct ifaddrmsg ifaddrmsg_info;
// char buffer[2048];
} netlink_req;
//Send request for current IP addresses
static void
send_ip_list_request(int sock)
{
int rtn;
memset(&netlink_req, 0, sizeof(netlink_req));
netlink_req.nlmsg_info.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
netlink_req.nlmsg_info.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
netlink_req.nlmsg_info.nlmsg_type = RTM_GETADDR;
netlink_req.nlmsg_info.nlmsg_pid = getpid();
netlink_req.ifaddrmsg_info.ifa_family = AF_INET6;
rtn = send(sock, &netlink_req, netlink_req.nlmsg_info.nlmsg_len, 0);
assert(rtn >= 0);
}
#define STRFLAGIFSET(v,f) ((v&f) != 0 ? #f" " : "")
static void
fprint_flags(FILE *o, int flags)
{
fprintf(o, "%s%s", STRFLAGIFSET(flags, IFA_F_TEMPORARY), STRFLAGIFSET(flags, IFA_F_DEPRECATED));
}
static int
listen_for_IP()
{
struct sockaddr_nl addr;
int sock, len;
const int RECV_SIZE = 512;
char recv_buffer[RECV_SIZE];
if ((sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
{
_perror("couldn't open NETLINK_ROUTE socket");
_assert(false);
return 1;
}
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_groups = RTMGRP_IPV6_IFADDR;
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
_perror("couldn't bind");
assert(false);
return 1;
}
send_ip_list_request(sock);
loop:
while ((len = recv(sock, recv_buffer, RECV_SIZE, 0)) > 0)
{
struct nlmsghdr* nlh = (struct nlmsghdr*) recv_buffer;
while (NLMSG_OK(nlh, len) && (nlh->nlmsg_type != NLMSG_DONE))
{
if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_GETADDR || nlh->nlmsg_type == RTM_DELADDR)
{
struct ifaddrmsg *ifa = (struct ifaddrmsg *) NLMSG_DATA(nlh);
if(verbose_mode)
{
log_time();
log("Message: [%d] flags: ", nlh->nlmsg_type);
fprint_flags(global_output, ifa->ifa_flags);
log(" scope: %d\n", ifa->ifa_scope);
}
if (ifa->ifa_family == AF_INET6)
{
void *address = NULL; //The IP Address:
struct ifa_cacheinfo *ci = NULL; //Cache info for address:
struct rtattr *rth = IFA_RTA(ifa);
int rtl = IFA_PAYLOAD(nlh);
while (rtl && RTA_OK(rth, rtl))
{
if (rth->rta_type == IFA_ADDRESS)
{
address = RTA_DATA(rth);
if(verbose_mode)
{
log("[IFA_ADDRESS]");
log_ipv6(address);
log("\n");
}
}
else if (rth->rta_type == IFA_CACHEINFO)
{
ci = (struct ifa_cacheinfo *) RTA_DATA(rth);
if(verbose_mode)
{
log("[IFA_CACHEINFO] valid: %d prefered: %d\n", ci->ifa_valid, ci->ifa_prefered);
}
}
else
{
verbose_log("[?%d?]\n", rth->rta_type);
}
rth = RTA_NEXT(rth, rtl);
}
assert(address != NULL);
assert(ci != NULL);
filter_ip(address, ifa->ifa_flags, ifa->ifa_scope, ci->ifa_prefered);
}
}
else if (nlh->nlmsg_type == NLMSG_ERROR)
{
struct nlmsgerr *err = (struct nlmsgerr*) NLMSG_DATA(nlh);
if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
{
log("ERROR truncated\n");
}
else
{
errno = -err->error;
_perror("RTNETLINK answers");
}
return -1;
}
nlh = NLMSG_NEXT(nlh, len);
}
fflush(global_output);
}
if (errno == EINTR || errno == EAGAIN) goto loop; //Would a do..while be cleaner?
assert(len != 0);
assert(len > 0);
return 0;
}
static void
on_sigterm(int signo)
{
log("Terminating: SIGTERM\n");
close_log();
exit(0);
}
int
main(int argc, char **argv)
{
global_output = stdout;
bool daemon_mode = false;
int c;
while ((c = getopt (argc, argv, "dv")) != -1)
switch (c)
{
case 'v':
verbose_mode = true;
break;
case 'd':
daemon_mode = true;
break;
//case '?':
default:
abort();
}
//Activate daemon-mode
if(daemon_mode)
{
signal(SIGTERM, on_sigterm);
skeleton_daemon();
const char* LOGPATH = "/var/log/ddnsclient";
global_output = fopen(LOGPATH, "a");
if(!global_output)
{
fprintf(stderr, "Unable to open log file %s\n", LOGPATH);
exit(1);
}
log_time();
log("Started as daemon\n");
}
assert(listen_for_IP());
assert(0);
return 1;
}