-
Notifications
You must be signed in to change notification settings - Fork 33
/
wsdd2.c
1023 lines (908 loc) · 26.6 KB
/
wsdd2.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
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
WSDD - Web Service Dynamic Discovery protocol server
Main file for general network handling.
Copyright (c) 2016 NETGEAR
Copyright (c) 2016 Hiro Sugawara
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE // asprintf()
#include "wsdd.h"
#include <stddef.h> // NULL
#include <limits.h> // HOST_NAME_MAX
#include <stdbool.h> // bool
#include <stdio.h> // snprintf(), asprintf()
#include <stdlib.h> // calloc(), free(), EXIT_FAILURE
#include <stdarg.h> // va_list, va_start()
#include <setjmp.h> // jmp_buf, setjmp(), longjmp()
#include <signal.h> // sig_atomic_t, SIGHUP, SIGINT, SIGTERM
#include <unistd.h> // gethostname(), getpid(), setsid(), close(), dup2()
#include <syslog.h> // openlog()
#include <string.h> // strncpy(), strchr(), strsignal()
#include <fcntl.h> // open()
#include <ctype.h> // isdigit(), isspace()
#include <errno.h> // errno, ENOMEM
#include <err.h> // err()
#include <libgen.h> // basename()
#include <sys/select.h> // FD_SET()
#include <sys/socket.h> // SOCK_DGRAM
#include <sys/stat.h> // stat()
#include <netdb.h> // struct servent, getservbyname()
#include <arpa/inet.h> // inet_ntop()
#include <net/if.h> // if_indextoname()
#include <netinet/in.h> // IPPROTO_IP
#include <ifaddrs.h> // struct ifaddrs, getifaddrs()
#include <linux/netlink.h> // NETLINK_ROUTE
#include <linux/rtnetlink.h> // RTMGRP_LINK
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096 // PAGE_SIZE
#endif
bool is_daemon = false;
int debug_L, debug_W, debug_N;
const char *hostname = NULL, *hostaliases = NULL, *netbiosname = NULL, *netbiosaliases = NULL, *workgroup = NULL;
static char *ifname = NULL;
static unsigned ifindex = 0;
static struct ifaddrs *ifaddrs_list = NULL;
static int netlink_recv(struct endpoint *ep);
static struct service services[] = {
{
.name = "wsdd-mcast-v4",
.family = AF_INET,
.type = SOCK_DGRAM,
.port_name = "wsdd",
.port_num = 3702,
.mcast_addr = "239.255.255.250",
.init = wsd_init,
.recv = wsd_recv,
.exit = wsd_exit,
},
{
.name = "wsdd-mcast-v6",
.family = AF_INET6,
.type = SOCK_DGRAM,
.port_name = "wsdd",
.port_num = 3702,
.mcast_addr = "ff02::c",
.init = wsd_init,
.recv = wsd_recv,
.exit = wsd_exit,
},
{
.name = "wsdd-http-v4",
.family = AF_INET,
.type = SOCK_STREAM,
.port_name = "wsdd",
.port_num = 3702,
.recv = wsd_recv,
},
{
.name = "wsdd-http-v6",
.family = AF_INET6,
.type = SOCK_STREAM,
.port_name = "wsdd",
.port_num = 3702,
.recv = wsd_recv,
},
{
.name = "llmnr-mcast-v4",
.family = AF_INET,
.type = SOCK_DGRAM,
.port_name = "llmnr",
.port_num = 5355,
.mcast_addr = "224.0.0.252",
.init = llmnr_init,
.recv = llmnr_recv,
.exit = llmnr_exit,
},
{
.name = "llmnr-mcast-v6",
.family = AF_INET6,
.type = SOCK_DGRAM,
.port_name = "llmnr",
.port_num = 5355,
.mcast_addr = "ff02::1:3",
.init = llmnr_init,
.recv = llmnr_recv,
.exit = llmnr_exit,
},
{
.name = "llmnr-tcp-v4",
.family = AF_INET,
.type = SOCK_STREAM,
.port_name = "llmnr",
.port_num = 5355,
.init = llmnr_init,
.recv = llmnr_recv,
.exit = llmnr_exit,
},
{
.name = "llmnr-tcp-v6",
.family = AF_INET6,
.type = SOCK_STREAM,
.port_name = "llmnr",
.port_num = 5355,
.init = llmnr_init,
.recv = llmnr_recv,
.exit = llmnr_exit,
},
{
.name = "netlink-v4v6",
.family = AF_NETLINK,
.type = SOCK_RAW,
.protocol = NETLINK_ROUTE,
.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR,
.recv = netlink_recv,
},
};
/*
* Find the interface address *ci which corresponds to received message sender address *sa
* in order to reply with the "right" IP address.
*/
int connected_if(const _saddr_t *sa, _saddr_t *ci)
{
int rv = -1;
for (struct ifaddrs *ifa = ifaddrs_list; ifa; ifa = ifa->ifa_next) {
const uint8_t *_if, *_nm, *_sa;
uint8_t *_ca;
size_t alen;
if (ifa->ifa_flags & IFF_SLAVE || !ifa->ifa_netmask ||
!ifa->ifa_addr || ifa->ifa_addr->sa_family != sa->sa.sa_family)
continue;
if (ifindex && if_nametoindex(ifa->ifa_name) != ifindex)
continue;
if (debug_W >= 4) {
char ifa_addr[_ADDRSTRLEN], ifa_netmask[_ADDRSTRLEN], sa_addr[_ADDRSTRLEN];
if (!inet_ntop(ifa->ifa_addr->sa_family, _SIN_ADDR((_saddr_t *)ifa->ifa_addr), ifa_addr, sizeof(ifa_addr)))
ifa_addr[0] = '\0';
if (!inet_ntop(sa->sa.sa_family, _SIN_ADDR(sa), ifa_netmask, sizeof(ifa_netmask)))
ifa_netmask[0] = '\0';
if (!inet_ntop(ifa->ifa_netmask->sa_family, _SIN_ADDR((_saddr_t *)ifa->ifa_netmask), sa_addr, sizeof(sa_addr)))
sa_addr[0] = '\0';
DEBUG(4, W, "%s: %s: if=%s nm=%s sc=%s", __func__, ifa->ifa_name, ifa_addr, ifa_netmask, sa_addr);
}
switch (sa->ss.ss_family) {
case AF_INET:
_if = (uint8_t *) &((_saddr_t *)ifa->ifa_addr)->in.sin_addr;
_nm = (uint8_t *) &((_saddr_t *)ifa->ifa_netmask)->in.sin_addr;
_sa = (uint8_t *) &sa->in.sin_addr;
_ca = (uint8_t *) &ci->in.sin_addr;
alen = sizeof(sa->in.sin_addr);
break;
case AF_INET6:
_if = (uint8_t *) &((_saddr_t *)ifa->ifa_addr)->in6.sin6_addr;
_nm = (uint8_t *) &((_saddr_t *)ifa->ifa_netmask)->in6.sin6_addr;
_sa = (uint8_t *) &sa->in6.sin6_addr;
_ca = (uint8_t *) &ci->in6.sin6_addr;
alen = sizeof(sa->in6.sin6_addr);
break;
default:
continue;
}
rv = 0;
for (size_t i = 0; i < alen; i++) {
if ((_if[i] & _nm[i]) != (_sa[i] & _nm[i])) {
rv = -1;
break;
}
}
if (!rv) {
ci->ss.ss_family = sa->ss.ss_family;
memcpy(_ca, _if, alen);
break;
}
}
if (debug_W >= 4) {
char name[_ADDRSTRLEN];
if (inet_ntop(ci->ss.ss_family, _SIN_ADDR(ci), name, sizeof(name)))
DEBUG(4, W, "%s: ci=%s rv=%d", __func__, name, rv);
}
if (rv) errno = ENONET;
return rv;
}
char *ip2uri(const char *ip)
{
if (*ip == '[' || !strchr(ip, ':'))
return strdup(ip);
char *uri = NULL;
#if 0 /* WINDOWS 7 does not honor [xx::xx] notation. */
asprintf(&uri, "[%s]", ip);
#else
char name[HOST_NAME_MAX + 1];
if (!gethostname(name, sizeof(name) - 1))
uri = strdup(name);
#endif
return uri;
}
static struct endpoint *endpoints;
static const struct sock_params {
int family;
const char *name;
int ipproto_ip;
int ip_multicast_loop;
int ip_add_membership;
int ip_drop_membership;
int ip_pktinfo;
size_t llen, mlen, mreqlen;
} sock_params[] = {
[AF_INET] = {
.family = AF_INET,
.name = "IPv4",
.ipproto_ip = IPPROTO_IP,
.ip_multicast_loop = IP_MULTICAST_LOOP,
.ip_add_membership = IP_ADD_MEMBERSHIP,
.ip_drop_membership = IP_DROP_MEMBERSHIP,
.ip_pktinfo = IP_PKTINFO,
.llen = sizeof(struct sockaddr_in),
.mlen = sizeof(struct sockaddr_in),
.mreqlen = sizeof(endpoints[0].mreq.ip_mreq),
},
[AF_INET6] = {
.family = AF_INET6,
.name = "IPv6",
.ipproto_ip = IPPROTO_IPV6,
.ip_multicast_loop = IPV6_MULTICAST_LOOP,
.ip_add_membership = IPV6_ADD_MEMBERSHIP,
.ip_drop_membership = IPV6_DROP_MEMBERSHIP,
.ip_pktinfo = IPV6_RECVPKTINFO,
.llen = sizeof(struct sockaddr_in6),
.mlen = sizeof(struct sockaddr_in6),
.mreqlen = sizeof(endpoints[0].mreq.ipv6_mreq),
},
[AF_NETLINK] = {
.family = AF_NETLINK,
.name = "NETLINK",
.llen = sizeof(struct sockaddr_nl),
},
};
static const char *const socktype_str[] = {
[SOCK_STREAM] = "tcp",
[SOCK_DGRAM] = "udp",
[SOCK_SEQPACKET] = "seq",
};
static int open_ep(struct endpoint **epp, struct service *sv, const struct ifaddrs *ifa)
{
#define __FUNCTION__ "open_ep"
const unsigned int disable = 0, enable = 1;
struct endpoint *ep = (struct endpoint *) calloc(sizeof(*ep), 1);
if ((*epp = ep) == NULL) {
errno = ENOMEM;
err(EXIT_FAILURE, __FUNCTION__ ": calloc");
}
strncpy(ep->ifname, ifa->ifa_name, sizeof(ep->ifname));
ep->service = sv;
ep->family = sv->family;
ep->type = sv->type;
ep->protocol = sv->protocol;
if (sv->family >= (int) ARRAY_SIZE(sock_params) || !sock_params[ep->family].name) {
ep->errstr = __FUNCTION__ ": Unsupported address family";
ep->_errno = EINVAL;
return -1;
}
if (sv->family == AF_INET || sv->family == AF_INET6) {
struct servent *se = getservbyname(sv->port_name, socktype_str[sv->type]);
ep->port = se ? ntohs(se->s_port) : 0;
if (!ep->port)
ep->port = sv->port_num;
if (!ep->port) {
ep->errstr = __FUNCTION__ ": No port number";
ep->_errno = EADDRNOTAVAIL;
return -1;
}
}
const struct sock_params *sp = &sock_params[ep->family];
ep->mcast.ss.ss_family = ep->family;
ep->mlen = sp->mlen;
ep->local.ss.ss_family = ep->family;
ep->llen = sp->llen;
ep->mreqlen = sp->mreqlen;
switch (ep->family) {
case AF_INET:
if (sv->mcast_addr) {
ep->mcast.in.sin_port = htons(ep->port);
if (inet_pton(ep->family, sv->mcast_addr, &ep->mcast.in.sin_addr.s_addr) != 1) {
ep->errstr = __FUNCTION__ ": Bad mcast IP addr";
ep->_errno = errno;
return -1;
}
ep->mreq.ip_mreq.imr_multiaddr = ep->mcast.in.sin_addr;
#ifdef USE_ip_mreq
ep->mreq.ip_mreq.imr_interface = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
#else
ep->mreq.ip_mreq.imr_address = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
ep->mreq.ip_mreq.imr_ifindex = if_nametoindex(ep->ifname);
#endif
}
//ep->local.saddr_in = *(struct sockaddr_in *)ifa->ifa_addr;
ep->local.in.sin_addr.s_addr = htonl(INADDR_ANY);
ep->local.in.sin_port = htons(ep->port);
break;
case AF_INET6:
if (sv->mcast_addr) {
ep->mcast.in6.sin6_port = htons(ep->port);
if (inet_pton(ep->family, sv->mcast_addr, ep->mcast.in6.sin6_addr.s6_addr) != 1) {
ep->errstr = __FUNCTION__ ": Bad mcast IPv6 addr";
ep->_errno = errno;
return -1;
}
ep->mreq.ipv6_mreq.ipv6mr_multiaddr = ep->mcast.in6.sin6_addr;
ep->mreq.ipv6_mreq.ipv6mr_interface = if_nametoindex(ep->ifname);
}
//ep->local.in6 = *(struct sockaddr_in6 *)ifa->ifa_addr;
ep->local.in6.sin6_addr = in6addr_any;
ep->local.in6.sin6_port = htons(ep->port);
break;
case AF_NETLINK:
ep->local.nl.nl_pid = getpid();
ep->local.nl.nl_groups = ep->service->nl_groups;
break;
}
ep->sock = socket(ep->family, ep->type | SOCK_CLOEXEC, ep->protocol);
if (ep->sock < 0) {
ep->errstr = __FUNCTION__ ": Can't open socket";
ep->_errno = errno;
return -1;
}
setsockopt(ep->sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof enable);
#ifdef SO_REUSEPORT
setsockopt(ep->sock, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof enable);
#endif
#ifdef SO_RCVBUFFORCE
int rcvbuf = 128 * 1024;
if ((ep->family == AF_NETLINK) &&
setsockopt(ep->sock, SOL_SOCKET, SO_RCVBUFFORCE, &rcvbuf, sizeof rcvbuf)) {
LOG(LOG_WARNING, "%s: SO_RCVBUFFORCE: %s", __FUNCTION__, strerror(errno));
}
#endif
#ifdef IPV6_V6ONLY
if ((ep->family == AF_INET6) &&
setsockopt(ep->sock, sp->ipproto_ip, IPV6_V6ONLY, &enable, sizeof enable)) {
ep->errstr = __FUNCTION__ ": IPV6_V6ONLY";
ep->_errno = errno;
close(ep->sock);
return -1;
}
#endif
#ifdef SO_BINDTODEVICE
if (!sv->mcast_addr && (ep->family == AF_INET || ep->family == AF_INET6)) {
if (setsockopt(ep->sock, SOL_SOCKET, SO_BINDTODEVICE, ep->ifname, strlen(ep->ifname))) {
ep->errstr = __FUNCTION__ ": SO_BINDTODEVICE";
ep->_errno = errno;
close(ep->sock);
return -1;
}
}
#endif
if (bind(ep->sock, (struct sockaddr *)&ep->local, ep->llen)) {
ep->errstr = __FUNCTION__ ": bind";
ep->_errno = errno;
close(ep->sock);
ep->sock = -1;
DEBUG(0, W, "%s: %s: %s", ep->service->name, ep->errstr, strerror(ep->_errno));
return (ep->_errno == EADDRINUSE) ? 0 : -1;
}
if (ep->type == SOCK_DGRAM &&
setsockopt(ep->sock, sp->ipproto_ip, sp->ip_pktinfo, &enable, sizeof(enable))) {
ep->errstr = __FUNCTION__ ": PKTINFO";
ep->_errno = errno;
close(ep->sock);
return -1;
}
if (sv->mcast_addr) {
#ifdef IP_MULTICAST_IF
/* Set multicast sending interface to avoid error: wsdd-mcast-v4: wsd_send_soap_msg: send: No route to host */
if (ep->family == AF_INET && ep->type == SOCK_DGRAM &&
setsockopt(ep->sock, sp->ipproto_ip, IP_MULTICAST_IF, &ep->mreq, ep->mreqlen)) {
ep->errstr = __FUNCTION__ ": IP_MULTICAST_IF";
ep->_errno = errno;
close(ep->sock);
return -1;
}
#endif
#ifdef IPV6_MULTICAST_IF
/* Set multicast sending interface for IPv6 */
if (ep->family == AF_INET6 && ep->type == SOCK_DGRAM &&
setsockopt(ep->sock, sp->ipproto_ip, IPV6_MULTICAST_IF, &ep->mreq.ipv6_mreq.ipv6mr_interface, sizeof(ep->mreq.ipv6_mreq.ipv6mr_interface))) {
ep->errstr = __FUNCTION__ ": IPV6_MULTICAST_IF";
ep->_errno = errno;
close(ep->sock);
return -1;
}
#endif
/* Disable loopback. */
if (setsockopt(ep->sock, sp->ipproto_ip, sp->ip_multicast_loop, &disable, sizeof(disable))) {
ep->errstr = __FUNCTION__ ": IP_MULTICAST_LOOP";
ep->_errno = errno;
close(ep->sock);
return -1;
}
/* Set inbound multicast. */
if (setsockopt(ep->sock, sp->ipproto_ip, sp->ip_add_membership, &ep->mreq, ep->mreqlen)) {
ep->errstr = __FUNCTION__ ": IP_ADD_MEMBERSHIP";
ep->_errno = errno;
close(ep->sock);
return -1;
}
}
if (ep->type == SOCK_STREAM && listen(ep->sock, 5)) {
ep->errstr = __FUNCTION__ ": listen";
ep->_errno = errno;
close(ep->sock);
return -1;
}
return 0;
#undef __FUNCTION__
}
static void close_ep(struct endpoint *ep)
{
if (ep->service->exit)
ep->service->exit(ep);
if (ep->service->mcast_addr)
setsockopt(ep->sock, sock_params[ep->family].ipproto_ip,
sock_params[ep->family].ip_drop_membership, &ep->mreq, ep->mreqlen);
close(ep->sock);
}
static jmp_buf sigenv;
volatile sig_atomic_t restart;
void restart_service(void)
{
DEBUG(1, W, "restarting service.");
restart = 1;
longjmp(sigenv, 1);
}
static bool is_new_addr(struct nlmsghdr *nh)
{
struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA(nh);
struct rtattr *rta = IFA_RTA(ifam);
size_t rtasize = IFA_PAYLOAD(nh);
if (nh->nlmsg_type != RTM_NEWADDR)
return false;
if (ifindex && ifam->ifa_index != ifindex) {
char buf[IFNAMSIZ];
if (!if_indextoname(ifam->ifa_index, buf) || strcmp(buf, ifname) != 0)
return false;
ifindex = ifam->ifa_index;
}
while (RTA_OK(rta, rtasize)) {
if (rta->rta_type == IFA_CACHEINFO) {
struct ifa_cacheinfo *cache_info = (struct ifa_cacheinfo *) RTA_DATA(rta);
if (cache_info->cstamp != cache_info->tstamp)
return false;
}
rta = RTA_NEXT(rta, rtasize);
}
return true;
}
static int netlink_recv(struct endpoint *ep)
{
#define __FUNCTION__ "netlink_recv"
char buf[PAGE_SIZE];
struct sockaddr_nl sa;
struct iovec iov = { buf, sizeof buf };
struct msghdr msg = { &sa, sizeof sa, &iov, 1, NULL, 0, 0 };
ssize_t msglen = recvmsg(ep->sock, &msg, 0);
DEBUG(2, W, "%s: %zd bytes", __func__, msglen);
if (msglen <= 0) {
ep->_errno = errno;
ep->errstr = __FUNCTION__ ": netlink_recv: recv";
return -1;
}
#ifdef NL_DEBUG
nl_debug(buf, msglen);
#endif
for (struct nlmsghdr *nh = (struct nlmsghdr *) buf;
NLMSG_OK(nh, msglen) && nh->nlmsg_type != NLMSG_DONE;
nh = NLMSG_NEXT(nh, msglen)) {
if (is_new_addr(nh) || nh->nlmsg_type == RTM_DELADDR) {
DEBUG(1, W, __FUNCTION__ ": address addition/change/deletion detected.");
restart_service();
break;
}
}
return 0;
#undef __FUNCTION__
}
static void sighandler(int sig)
{
DEBUG(0, W, "'%s' signal received.", strsignal(sig));
switch (sig) {
case SIGHUP:
restart = 1;
break;
default:
restart = 2;
}
}
static char *get_smbparm(const char *name, const char *_default)
{
#define __FUNCTION__ "get_smbparm"
char *cmd = NULL, *result = NULL;
if (asprintf(&cmd, "testparm -s --parameter-name=\"%s\" 2>/dev/null", name) <= 0) {
DEBUG(0, W, __FUNCTION__ ": can't allocate cmd string");
return NULL;
}
FILE *pp = popen(cmd, "r");
free(cmd);
if (!pp) {
DEBUG(0, W, __FUNCTION__ ": can't run testparam");
return strdup(_default);
}
char buf[PAGE_SIZE];
if (!fgets(buf, sizeof(buf), pp) || !buf[0] || buf[0] == '\n') {
DEBUG(0, W, "cannot read %s from testparm", name);
result = strdup(_default);
} else { // trim whitespace
char *p;
for (p = buf + strlen(buf) - 1; buf < p && isspace(*p); p--)
*p = '\0';
for (p = buf; *p && isspace(*p); p++)
;
result = strdup(p);
}
pclose(pp);
return result;
#undef __FUNCTION__
}
static void help(const char *prog, int ec, const char *fmt, ...)
{
if (fmt) {
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
puts("\n");
}
printf( "WSDD and LLMNR daemon\n"
"Usage: %s [options]\n"
" -h this message\n"
" -d become daemon\n"
" -4 IPv4 only\n"
" -6 IPv6 only\n"
" -u UDP only\n"
" -t TCP only\n"
" -l LLMNR only\n"
" -w WSDD only\n"
" -L increment LLMNR debug level (%d)\n"
" -W increment WSDD debug level (%d)\n"
" -i <interface> reply only on this interface (%s)\n"
" -H <name> set host name (%s)\n"
" -A \"name list\" set host aliases (%s)\n"
" -N <name> set netbios name (%s)\n"
" -B \"name list\" set netbios aliases (%s)\n"
" -G <name> set workgroup (%s)\n"
" -b \"key1:val1,key2:val2,...\" boot parameters:\n",
prog, debug_L, debug_W, ifname ? ifname : "any",
hostname, hostaliases, netbiosname, netbiosaliases, workgroup
);
printBootInfoKeys(stdout, 11);
exit(ec);
}
static void init_sysinfo()
{
char hostn[HOST_NAME_MAX + 1];
if (!hostname && gethostname(hostn, sizeof(hostn) - 1) != 0)
err(EXIT_FAILURE, "gethostname");
char *p = strchr(hostn, '.');
if (p) *p = '\0';
hostname = strdup(hostn);
if (!hostaliases && !(hostaliases = get_smbparm("additional dns hostnames", "")))
err(EXIT_FAILURE, "get_smbparm");
if (!netbiosname && !(netbiosname = get_smbparm("netbios name", hostname)))
err(EXIT_FAILURE, "get_smbparm");
if (!netbiosaliases && !(netbiosaliases = get_smbparm("netbios aliases", "")))
err(EXIT_FAILURE, "get_smbparm");
if (!workgroup && !(workgroup = get_smbparm("workgroup", "WORKGROUP")))
err(EXIT_FAILURE, "get_smbparm");
init_getresp();
}
#define _4 1
#define _6 2
#define _TCP 1
#define _UDP 2
#define _LLMNR 1
#define _WSDD 2
int main(int argc, char **argv)
{
int opt;
const char *prog = basename(argv[0]);
unsigned int ipv46 = 0, tcpudp = 0, llmnrwsdd = 0;
init_sysinfo();
while ((opt = getopt(argc, argv, "hd46utlwLWi:H:N:G:b:")) != -1) {
switch (opt) {
case 'h':
help(prog, EXIT_SUCCESS, NULL);
break;
case 'd':
is_daemon = true;
break;
case '4':
ipv46 |= _4;
break;
case '6':
ipv46 |= _6;
break;
case 'u':
tcpudp |= _UDP;
break;
case 't':
tcpudp |= _TCP;
break;
case 'l':
llmnrwsdd |= _LLMNR;
break;
case 'w':
llmnrwsdd |= _WSDD;
break;
case 'L':
debug_L++;
break;
case 'W':
debug_W++;
break;
case 'i':
if (optarg && strlen(optarg) && strcmp(optarg, "any") != 0) {
ifname = strdup(optarg);
ifindex = ifname ? if_nametoindex(ifname) : 0;
if (!ifindex) help(prog, EXIT_FAILURE, "Bad interface '%s'", ifname);
} else {
free(ifname);
ifname = NULL;
ifindex = 0;
}
break;
case 'H':
if (optarg != NULL && strlen(optarg) > 0)
hostname = strdup(optarg);
break;
case 'N':
if (optarg != NULL && strlen(optarg) > 0)
netbiosname = strdup(optarg);
break;
case 'G':
if (optarg != NULL && strlen(optarg) > 0)
workgroup = strdup(optarg);
break;
case 'b':
while (optarg)
if (set_getresp(optarg, (const char **)&optarg) != 0)
help(prog, EXIT_FAILURE, "Bad key:val '%s'", optarg);
break;
case '?':
if (strchr("iHNGb", optopt))
printf("Option -%c requires an argument.\n", optopt);
/* ... fall through ... */
default:
help(prog, EXIT_FAILURE, "Bad option '%c'", opt);
}
}
if (argc > optind)
help(prog, EXIT_FAILURE, "Unknown argument '%s'", argv[optind]);
if (!ipv46)
ipv46 = _4 | _6;
if (!llmnrwsdd)
llmnrwsdd = _LLMNR | _WSDD;
if (!tcpudp)
tcpudp = _TCP | _UDP;
if (is_daemon) {
pid_t pid = fork();
if (pid < 0)
err(EXIT_FAILURE, "fork");
if (pid)
exit(EXIT_SUCCESS);
chdir("/");
int fd;
if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO) close(fd);
}
if (setsid() < 0)
exit(EXIT_FAILURE);
}
openlog(prog, LOG_PID, LOG_USER);
LOG(LOG_INFO, "starting.");
again:
{} /* Necessary to satisfy C syntax for statement labeling. */
struct sigaction sigact, oldact;
DEBUG(1, W, "ifname %s, ifindex %d", ifname, ifindex);
DEBUG(1, W, "hostname %s, netbios name %s, workgroup %s", hostname, netbiosname, workgroup);
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigact.sa_handler = sighandler;
if (sigaction(SIGINT, &sigact, &oldact) ||
sigaction(SIGHUP, &sigact, &oldact) ||
sigaction(SIGTERM, &sigact, &oldact)) {
err(EXIT_FAILURE, "cannot install signal handler.");
}
// Refresh ifaddrs list
if (ifaddrs_list != NULL)
freeifaddrs(ifaddrs_list);
if (getifaddrs(&ifaddrs_list) != 0)
err(EXIT_FAILURE, "getifaddrs()");
fd_set fds;
int rv = 0, nfds = -1;
struct endpoint *ep, *badep = NULL;
FD_ZERO(&fds);
for (size_t svn = 0; svn < ARRAY_SIZE(services); svn++) {
struct service *sv = &services[svn];
if (!(ipv46 & _4) && sv->family == AF_INET)
continue;
if (!(ipv46 & _6) && sv->family == AF_INET6)
continue;
if (!(tcpudp & _TCP) && sv->type == SOCK_STREAM)
continue;
if (!(tcpudp & _UDP) && sv->type == SOCK_DGRAM)
continue;
if (!(llmnrwsdd & _LLMNR) && strstr(sv->name, "llmnr"))
continue;
if (!(llmnrwsdd & _WSDD) && strstr(sv->name, "wsdd"))
continue;
if (sv->family == AF_INET || sv->family == AF_INET6) {
for (struct ifaddrs *ifa = ifaddrs_list; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_flags & IFF_SLAVE || !ifa->ifa_netmask ||
!ifa->ifa_addr || ifa->ifa_addr->sa_family != sv->family)
continue;
char ifaddr[_ADDRSTRLEN];
void *addr = _SIN_ADDR((_saddr_t *)ifa->ifa_addr);
inet_ntop(ifa->ifa_addr->sa_family, addr, ifaddr, sizeof(ifaddr));
if (ifname && strcmp(ifa->ifa_name, ifname) != 0) {
//DEBUG(2, W, "skipped %s: not selected", ifa->ifa_name);
ifa->ifa_flags |= IFF_SLAVE; // mark as not used
continue;
}
// skip if already bound to this interface
ep = NULL;
for (struct endpoint *e = endpoints; e; e = e->next)
if (e->service == sv && strcmp(e->ifname, ifa->ifa_name) == 0)
ep = e;
// show interface
DEBUG(1, W, "%s %s port %d %s %s @ %s%s", sv->name,
socktype_str[sv->type], sv->port_num,
sv->mcast_addr ? sv->mcast_addr : "-",
ifaddr, ifa->ifa_name, ep ? ": already bound" : "");
if (ep)
continue;
if (!ifname && sv->mcast_addr && !(ifa->ifa_flags & IFF_MULTICAST)) {
DEBUG(2, W, "skipped %s: not multicast", ifa->ifa_name);
ifa->ifa_flags |= IFF_SLAVE; // mark as not used
continue;
}
if (!ifname && (ifa->ifa_flags & IFF_LOOPBACK)) {
DEBUG(2, W, "skipped %s: loopback", ifa->ifa_name);
ifa->ifa_flags |= IFF_SLAVE; // mark as not used
continue;
}
if (!ifname && ((!strcmp(ifa->ifa_name, "LeafNets")) ||
(!strncmp(ifa->ifa_name, "docker", 6)) ||
(!strncmp(ifa->ifa_name, "veth", 4)) ||
(!strncmp(ifa->ifa_name, "tun", 3)) ||
(!strncmp(ifa->ifa_name, "ppp", 3)) ||
(!strncmp(ifa->ifa_name, "zt", 2)))) {
DEBUG(2, W, "skipped %s: excluded by name", ifa->ifa_name);
ifa->ifa_flags |= IFF_SLAVE; // mark as not used
continue;
}
// skip bridge ports unless it is specified on the command line
if (!ifname) {
struct stat st;
char path[sizeof("/sys/class/net//brport") + IFNAMSIZ];
snprintf(path, sizeof(path), "/sys/class/net/%s/brport", ifa->ifa_name);
if (stat(path, &st) == 0) {
DEBUG(2, W, "skipped %s: bridge port", ifa->ifa_name);
ifa->ifa_flags |= IFF_SLAVE; // mark as not used
continue;
}
}
// open socket for this interface/family
if (open_ep(&ep, sv, ifa) != 0) {
LOG(LOG_ERR, "error: %s: %s: %s", ep->service->name,
ep->errstr, strerror(ep->_errno));
ifa->ifa_flags |= IFF_SLAVE; // mark as not used
free(ep);
continue;
} else if (ep->sock < 0) {
free(ep);
} else {
ep->next = endpoints;
endpoints = ep;
FD_SET(ep->sock, &fds);
if (nfds < ep->sock)
nfds = ep->sock;
}
}
} else if (sv->family == AF_NETLINK) {
struct ifaddrs ifa = {};
ifa.ifa_name = "netlink";
DEBUG(2, W, "%s 0x%x @ %s", sv->name, sv->nl_groups, ifa.ifa_name);
if (open_ep(&ep, sv, &ifa) != 0) {
badep = ep;
break;
} else if (ep->sock < 0) {
free(ep);
} else {
ep->next = endpoints;
endpoints = ep;
FD_SET(ep->sock, &fds);
if (nfds < ep->sock)
nfds = ep->sock;
}
}
}
if (!badep) {
for (struct endpoint *ep = endpoints; ep; ep = ep->next) {
if (ep->service->init && ep->service->init(ep)) {
DEBUG(1, W, "%s init failed: %s: %s", ep->service->name,
ep->errstr, strerror(ep->_errno));
//badep = ep;
}
}
}
if (!badep) {
int n = 0;
if (setjmp(sigenv))
goto end;
do {
fd_set rfds = fds;
n = select(nfds + 1, &rfds, NULL, NULL, NULL);
DEBUG(4, W, "select: n=%d", n);
for (ep = endpoints; n > 0 && ep; ep = ep->next) {
if (!FD_ISSET(ep->sock, &rfds))
continue;
DEBUG(3, W, "dispatch %s recv", ep->service->name);
n--;
if (ep->service->recv) {
int ret = ep->service->recv(ep);
if (ret < 0) {
DEBUG(1, W, "Detected %s socket error, restarting",
ep->service->name);
restart_service();
}
}
}
} while (n >= 0 && !restart);
if (n < 0 && errno != EINTR) {
LOG(LOG_WARNING, "%s: select: %s", __func__, strerror(errno));
rv = EXIT_FAILURE;
}
}
end:
{} /* Necessary to satisfy C syntax for statement labeling. */
const char *badservice = NULL, *badbad = NULL;
int baderrno = 0;
if (badep) {
badservice = badep->service->name ? badep->service->name : "";
badbad = badep->errstr ? badep->errstr : "";
baderrno = badep->_errno;
}
while (endpoints) {
struct endpoint *tempep = endpoints->next;
close_ep(endpoints);
free(endpoints);
endpoints = tempep;