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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
|
/* MIT License
*
* Copyright (c) 2024 Brad House
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* SPDX-License-Identifier: MIT
*/
#include "ares_private.h"
#include "ares_event.h"
#ifdef __ANDROID__
ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
ares_event_thread_t *e)
{
(void)configchg;
(void)e;
/* No ability */
return ARES_ENOTIMP;
}
void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
{
/* No-op */
(void)configchg;
}
#elif defined(__linux__)
# include <sys/inotify.h>
struct ares_event_configchg {
int inotify_fd;
ares_event_thread_t *e;
};
void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
{
if (configchg == NULL) {
return; /* LCOV_EXCL_LINE: DefensiveCoding */
}
/* Tell event system to stop monitoring for changes. This will cause the
* cleanup to be called */
ares_event_update(NULL, configchg->e, ARES_EVENT_FLAG_NONE, NULL,
configchg->inotify_fd, NULL, NULL, NULL);
}
static void ares_event_configchg_free(void *data)
{
ares_event_configchg_t *configchg = data;
if (configchg == NULL) {
return; /* LCOV_EXCL_LINE: DefensiveCoding */
}
if (configchg->inotify_fd >= 0) {
close(configchg->inotify_fd);
configchg->inotify_fd = -1;
}
ares_free(configchg);
}
static void ares_event_configchg_cb(ares_event_thread_t *e, ares_socket_t fd,
void *data, ares_event_flags_t flags)
{
const ares_event_configchg_t *configchg = data;
/* Some systems cannot read integer variables if they are not
* properly aligned. On other systems, incorrect alignment may
* decrease performance. Hence, the buffer used for reading from
* the inotify file descriptor should have the same alignment as
* struct inotify_event. */
unsigned char buf[4096]
__attribute__((aligned(__alignof__(struct inotify_event))));
const struct inotify_event *event;
ssize_t len;
ares_bool_t triggered = ARES_FALSE;
(void)fd;
(void)flags;
while (1) {
const unsigned char *ptr;
len = read(configchg->inotify_fd, buf, sizeof(buf));
if (len <= 0) {
break;
}
/* Loop over all events in the buffer. Says kernel will check the buffer
* size provided, so I assume it won't ever return partial events. */
for (ptr = buf; ptr < buf + len;
ptr += sizeof(struct inotify_event) + event->len) {
event = (const struct inotify_event *)((const void *)ptr);
if (event->len == 0 || ares_strlen(event->name) == 0) {
continue;
}
if (strcasecmp(event->name, "resolv.conf") == 0 ||
strcasecmp(event->name, "nsswitch.conf") == 0) {
triggered = ARES_TRUE;
}
}
}
/* Only process after all events are read. No need to process more often as
* we don't want to reload the config back to back */
if (triggered) {
ares_reinit(e->channel);
}
}
ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
ares_event_thread_t *e)
{
ares_status_t status = ARES_SUCCESS;
ares_event_configchg_t *c;
(void)e;
/* Not used by this implementation */
*configchg = NULL;
c = ares_malloc_zero(sizeof(*c));
if (c == NULL) {
return ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
}
c->e = e;
c->inotify_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (c->inotify_fd == -1) {
status = ARES_ESERVFAIL; /* LCOV_EXCL_LINE: UntestablePath */
goto done; /* LCOV_EXCL_LINE: UntestablePath */
}
/* We need to monitor /etc/resolv.conf, /etc/nsswitch.conf */
if (inotify_add_watch(c->inotify_fd, "/etc",
IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_ONLYDIR) ==
-1) {
status = ARES_ESERVFAIL; /* LCOV_EXCL_LINE: UntestablePath */
goto done; /* LCOV_EXCL_LINE: UntestablePath */
}
status =
ares_event_update(NULL, e, ARES_EVENT_FLAG_READ, ares_event_configchg_cb,
c->inotify_fd, c, ares_event_configchg_free, NULL);
done:
if (status != ARES_SUCCESS) {
ares_event_configchg_free(c);
} else {
*configchg = c;
}
return status;
}
#elif defined(USE_WINSOCK)
# include <winsock2.h>
# include <iphlpapi.h>
# include <stdio.h>
# include <windows.h>
struct ares_event_configchg {
HANDLE ifchg_hnd;
HKEY regip4;
HANDLE regip4_event;
HANDLE regip4_wait;
HKEY regip6;
HANDLE regip6_event;
HANDLE regip6_wait;
ares_event_thread_t *e;
};
void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
{
if (configchg == NULL) {
return;
}
# ifdef HAVE_NOTIFYIPINTERFACECHANGE
if (configchg->ifchg_hnd != NULL) {
CancelMibChangeNotify2(configchg->ifchg_hnd);
configchg->ifchg_hnd = NULL;
}
# endif
# ifdef HAVE_REGISTERWAITFORSINGLEOBJECT
if (configchg->regip4_wait != NULL) {
UnregisterWait(configchg->regip4_wait);
configchg->regip4_wait = NULL;
}
if (configchg->regip6_wait != NULL) {
UnregisterWait(configchg->regip6_wait);
configchg->regip6_wait = NULL;
}
if (configchg->regip4 != NULL) {
RegCloseKey(configchg->regip4);
configchg->regip4 = NULL;
}
if (configchg->regip6 != NULL) {
RegCloseKey(configchg->regip6);
configchg->regip6 = NULL;
}
if (configchg->regip4_event != NULL) {
CloseHandle(configchg->regip4_event);
configchg->regip4_event = NULL;
}
if (configchg->regip6_event != NULL) {
CloseHandle(configchg->regip6_event);
configchg->regip6_event = NULL;
}
# endif
ares_free(configchg);
}
# ifdef HAVE_NOTIFYIPINTERFACECHANGE
static void NETIOAPI_API_
ares_event_configchg_ip_cb(PVOID CallerContext, PMIB_IPINTERFACE_ROW Row,
MIB_NOTIFICATION_TYPE NotificationType)
{
ares_event_configchg_t *configchg = CallerContext;
(void)Row;
(void)NotificationType;
ares_reinit(configchg->e->channel);
}
# endif
static ares_bool_t
ares_event_configchg_regnotify(ares_event_configchg_t *configchg)
{
# ifdef HAVE_REGISTERWAITFORSINGLEOBJECT
# if defined(__WATCOMC__) && !defined(REG_NOTIFY_THREAD_AGNOSTIC)
# define REG_NOTIFY_THREAD_AGNOSTIC 0x10000000L
# endif
DWORD flags = REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET |
REG_NOTIFY_THREAD_AGNOSTIC;
if (RegNotifyChangeKeyValue(configchg->regip4, TRUE, flags,
configchg->regip4_event, TRUE) != ERROR_SUCCESS) {
return ARES_FALSE;
}
if (RegNotifyChangeKeyValue(configchg->regip6, TRUE, flags,
configchg->regip6_event, TRUE) != ERROR_SUCCESS) {
return ARES_FALSE;
}
# else
(void)configchg;
# endif
return ARES_TRUE;
}
static VOID CALLBACK ares_event_configchg_reg_cb(PVOID lpParameter,
BOOLEAN TimerOrWaitFired)
{
ares_event_configchg_t *configchg = lpParameter;
(void)TimerOrWaitFired;
ares_reinit(configchg->e->channel);
/* Re-arm, as its single-shot. However, we don't know which one needs to
* be re-armed, so we just do both */
ares_event_configchg_regnotify(configchg);
}
ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
ares_event_thread_t *e)
{
ares_status_t status = ARES_SUCCESS;
ares_event_configchg_t *c = NULL;
c = ares_malloc_zero(sizeof(**configchg));
if (c == NULL) {
return ARES_ENOMEM;
}
c->e = e;
# ifdef HAVE_NOTIFYIPINTERFACECHANGE
/* NOTE: If a user goes into the control panel and changes the network
* adapter DNS addresses manually, this will NOT trigger a notification.
* We've also tried listening on NotifyUnicastIpAddressChange(), but
* that didn't get triggered either.
*/
if (NotifyIpInterfaceChange(AF_UNSPEC, ares_event_configchg_ip_cb, c, FALSE,
&c->ifchg_hnd) != NO_ERROR) {
status = ARES_ESERVFAIL;
goto done;
}
# endif
# ifdef HAVE_REGISTERWAITFORSINGLEOBJECT
/* Monitor HKLM\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters\Interfaces
* and HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
* for changes via RegNotifyChangeKeyValue() */
if (RegOpenKeyExW(
HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces",
0, KEY_NOTIFY, &c->regip4) != ERROR_SUCCESS) {
status = ARES_ESERVFAIL;
goto done;
}
if (RegOpenKeyExW(
HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces",
0, KEY_NOTIFY, &c->regip6) != ERROR_SUCCESS) {
status = ARES_ESERVFAIL;
goto done;
}
c->regip4_event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (c->regip4_event == NULL) {
status = ARES_ESERVFAIL;
goto done;
}
c->regip6_event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (c->regip6_event == NULL) {
status = ARES_ESERVFAIL;
goto done;
}
if (!RegisterWaitForSingleObject(&c->regip4_wait, c->regip4_event,
ares_event_configchg_reg_cb, c, INFINITE,
WT_EXECUTEDEFAULT)) {
status = ARES_ESERVFAIL;
goto done;
}
if (!RegisterWaitForSingleObject(&c->regip6_wait, c->regip6_event,
ares_event_configchg_reg_cb, c, INFINITE,
WT_EXECUTEDEFAULT)) {
status = ARES_ESERVFAIL;
goto done;
}
# endif
if (!ares_event_configchg_regnotify(c)) {
status = ARES_ESERVFAIL;
goto done;
}
done:
if (status != ARES_SUCCESS) {
ares_event_configchg_destroy(c);
} else {
*configchg = c;
}
return status;
}
#elif defined(__APPLE__)
# include <sys/types.h>
# include <unistd.h>
# include <notify.h>
# include <dlfcn.h>
# include <fcntl.h>
struct ares_event_configchg {
int fd;
int token;
};
void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
{
(void)configchg;
/* Cleanup happens automatically */
}
static void ares_event_configchg_free(void *data)
{
ares_event_configchg_t *configchg = data;
if (configchg == NULL) {
return;
}
if (configchg->fd >= 0) {
notify_cancel(configchg->token);
/* automatically closes fd */
configchg->fd = -1;
}
ares_free(configchg);
}
static void ares_event_configchg_cb(ares_event_thread_t *e, ares_socket_t fd,
void *data, ares_event_flags_t flags)
{
ares_event_configchg_t *configchg = data;
ares_bool_t triggered = ARES_FALSE;
(void)fd;
(void)flags;
while (1) {
int t = 0;
ssize_t len;
len = read(configchg->fd, &t, sizeof(t));
if (len < (ssize_t)sizeof(t)) {
break;
}
/* Token is read in network byte order (yeah, docs don't mention this) */
t = (int)ntohl(t);
if (t != configchg->token) {
continue;
}
triggered = ARES_TRUE;
}
/* Only process after all events are read. No need to process more often as
* we don't want to reload the config back to back */
if (triggered) {
ares_reinit(e->channel);
}
}
ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
ares_event_thread_t *e)
{
ares_status_t status = ARES_SUCCESS;
void *handle = NULL;
const char *(*pdns_configuration_notify_key)(void) = NULL;
const char *notify_key = NULL;
int flags;
size_t i;
const char *searchlibs[] = {
"/usr/lib/libSystem.dylib",
"/System/Library/Frameworks/SystemConfiguration.framework/"
"SystemConfiguration",
NULL
};
*configchg = ares_malloc_zero(sizeof(**configchg));
if (*configchg == NULL) {
return ARES_ENOMEM;
}
/* Load symbol as it isn't normally public */
for (i = 0; searchlibs[i] != NULL; i++) {
handle = dlopen(searchlibs[i], RTLD_LAZY);
if (handle == NULL) {
/* Fail, loop! */
continue;
}
pdns_configuration_notify_key =
(const char *(*)(void))dlsym(handle, "dns_configuration_notify_key");
if (pdns_configuration_notify_key != NULL) {
break;
}
/* Fail, loop! */
dlclose(handle);
handle = NULL;
}
if (pdns_configuration_notify_key == NULL) {
status = ARES_ESERVFAIL;
goto done;
}
notify_key = pdns_configuration_notify_key();
if (notify_key == NULL) {
status = ARES_ESERVFAIL;
goto done;
}
if (notify_register_file_descriptor(notify_key, &(*configchg)->fd, 0,
&(*configchg)->token) !=
NOTIFY_STATUS_OK) {
status = ARES_ESERVFAIL;
goto done;
}
/* Set file descriptor to non-blocking */
flags = fcntl((*configchg)->fd, F_GETFL, 0);
fcntl((*configchg)->fd, F_SETFL, flags | O_NONBLOCK);
/* Register file descriptor with event subsystem */
status = ares_event_update(NULL, e, ARES_EVENT_FLAG_READ,
ares_event_configchg_cb, (*configchg)->fd,
*configchg, ares_event_configchg_free, NULL);
done:
if (status != ARES_SUCCESS) {
ares_event_configchg_free(*configchg);
*configchg = NULL;
}
if (handle) {
dlclose(handle);
}
return status;
}
#elif defined(HAVE_STAT) && !defined(_WIN32)
# ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
# endif
# ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
# endif
typedef struct {
size_t size;
time_t mtime;
} fileinfo_t;
struct ares_event_configchg {
ares_bool_t isup;
ares__thread_t *thread;
ares__htable_strvp_t *filestat;
ares__thread_mutex_t *lock;
ares__thread_cond_t *wake;
const char *resolvconf_path;
ares_event_thread_t *e;
};
static ares_status_t config_change_check(ares__htable_strvp_t *filestat,
const char *resolvconf_path)
{
size_t i;
const char *configfiles[5];
ares_bool_t changed = ARES_FALSE;
configfiles[0] = resolvconf_path;
configfiles[1] = "/etc/nsswitch.conf";
configfiles[2] = "/etc/netsvc.conf";
configfiles[3] = "/etc/svc.conf";
configfiles[4] = NULL;
for (i = 0; configfiles[i] != NULL; i++) {
fileinfo_t *fi = ares__htable_strvp_get_direct(filestat, configfiles[i]);
struct stat st;
if (stat(configfiles[i], &st) == 0) {
if (fi == NULL) {
fi = ares_malloc_zero(sizeof(*fi));
if (fi == NULL) {
return ARES_ENOMEM;
}
if (!ares__htable_strvp_insert(filestat, configfiles[i], fi)) {
ares_free(fi);
return ARES_ENOMEM;
}
}
if (fi->size != (size_t)st.st_size || fi->mtime != (time_t)st.st_mtime) {
changed = ARES_TRUE;
}
fi->size = (size_t)st.st_size;
fi->mtime = (time_t)st.st_mtime;
} else if (fi != NULL) {
/* File no longer exists, remove */
ares__htable_strvp_remove(filestat, configfiles[i]);
changed = ARES_TRUE;
}
}
if (changed) {
return ARES_SUCCESS;
}
return ARES_ENOTFOUND;
}
static void *ares_event_configchg_thread(void *arg)
{
ares_event_configchg_t *c = arg;
ares__thread_mutex_lock(c->lock);
while (c->isup) {
ares_status_t status;
if (ares__thread_cond_timedwait(c->wake, c->lock, 30000) != ARES_ETIMEOUT) {
continue;
}
/* make sure status didn't change even though we got a timeout */
if (!c->isup) {
break;
}
status = config_change_check(c->filestat, c->resolvconf_path);
if (status == ARES_SUCCESS) {
ares_reinit(c->e->channel);
}
}
ares__thread_mutex_unlock(c->lock);
return NULL;
}
ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
ares_event_thread_t *e)
{
ares_status_t status = ARES_SUCCESS;
ares_event_configchg_t *c = NULL;
*configchg = NULL;
c = ares_malloc_zero(sizeof(*c));
if (c == NULL) {
status = ARES_ENOMEM;
goto done;
}
c->e = e;
c->filestat = ares__htable_strvp_create(ares_free);
if (c->filestat == NULL) {
status = ARES_ENOMEM;
goto done;
}
c->wake = ares__thread_cond_create();
if (c->wake == NULL) {
status = ARES_ENOMEM;
goto done;
}
c->resolvconf_path = c->e->channel->resolvconf_path;
if (c->resolvconf_path == NULL) {
c->resolvconf_path = PATH_RESOLV_CONF;
}
status = config_change_check(c->filestat, c->resolvconf_path);
if (status == ARES_ENOMEM) {
goto done;
}
c->isup = ARES_TRUE;
status = ares__thread_create(&c->thread, ares_event_configchg_thread, c);
done:
if (status != ARES_SUCCESS) {
ares_event_configchg_destroy(c);
} else {
*configchg = c;
}
return status;
}
void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
{
if (configchg == NULL) {
return;
}
if (configchg->lock) {
ares__thread_mutex_lock(configchg->lock);
}
configchg->isup = ARES_FALSE;
if (configchg->wake) {
ares__thread_cond_signal(configchg->wake);
}
if (configchg->lock) {
ares__thread_mutex_unlock(configchg->lock);
}
if (configchg->thread) {
void *rv = NULL;
ares__thread_join(configchg->thread, &rv);
}
ares__thread_mutex_destroy(configchg->lock);
ares__thread_cond_destroy(configchg->wake);
ares__htable_strvp_destroy(configchg->filestat);
ares_free(configchg);
}
#else
ares_status_t ares_event_configchg_init(ares_event_configchg_t **configchg,
ares_event_thread_t *e)
{
/* No ability */
return ARES_ENOTIMP;
}
void ares_event_configchg_destroy(ares_event_configchg_t *configchg)
{
/* No-op */
}
#endif
|