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
|
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* -*- Mode: C; tab-width: 4 -*-
*
* Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mDNSUNP.h"
#include "mDNSDebug.h"
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
/* Some weird platforms derived from 4.4BSD Lite (e.g. EFI) need the ALIGN(P)
macro, usually defined in <sys/param.h> or someplace like that, to make sure the
CMSG_NXTHDR macro is well-formed. On such platforms, the symbol NEED_ALIGN_MACRO
should be set to the name of the header to include to get the ALIGN(P) macro.
*/
#ifdef NEED_ALIGN_MACRO
#include NEED_ALIGN_MACRO
#endif
/* Solaris defined SIOCGIFCONF etc in <sys/sockio.h> but
other platforms don't even have that include file. So,
if we haven't yet got a definition, let's try to find
<sys/sockio.h>.
*/
#ifndef SIOCGIFCONF
#include <sys/sockio.h>
#endif
/* sockaddr_dl is only referenced if we're using IP_RECVIF,
so only include the header in that case.
*/
#ifdef IP_RECVIF
#include <net/if_dl.h>
#endif
#if defined(AF_INET6) && HAVE_IPV6 && !HAVE_LINUX
#if !HAVE_SOLARIS
#include <net/if_var.h>
#else
#include <alloca.h>
#endif /* !HAVE_SOLARIS */
#include <netinet/in_var.h>
// NOTE: netinet/in_var.h implicitly includes netinet6/in6_var.h for us
#endif
#if defined(AF_INET6) && HAVE_IPV6
#if HAVE_LINUX
#include <netdb.h>
#include <arpa/inet.h>
/* Converts a prefix length to IPv6 network mask */
void plen_to_mask(int plen, char *addr) {
int i;
int colons=7; /* Number of colons in IPv6 address */
int bits_in_block=16; /* Bits per IPv6 block */
for(i=0;i<=colons;i++) {
int block, ones=0xffff, ones_in_block;
if(plen>bits_in_block) ones_in_block=bits_in_block;
else ones_in_block=plen;
block = ones & (ones << (bits_in_block-ones_in_block));
i==0 ? sprintf(addr, "%x", block) : sprintf(addr, "%s:%x", addr, block);
plen -= ones_in_block;
}
}
/* Gets IPv6 interface information from the /proc filesystem in linux*/
struct ifi_info *get_ifi_info_linuxv6(int family, int doaliases)
{
struct ifi_info *ifi, *ifihead, **ifipnext;
FILE *fp;
char addr[8][5];
int flags, myflags, index, plen, scope;
char ifname[8], lastname[IFNAMSIZ];
char addr6[32+7+1]; /* don't forget the seven ':' */
struct addrinfo hints, *res0;
struct sockaddr_in6 *sin6;
struct in6_addr *addrptr;
int err;
res0=NULL;
ifihead = NULL;
ifipnext = &ifihead;
lastname[0] = 0;
if ((fp = fopen(PROC_IFINET6_PATH, "r")) != NULL) {
while (fscanf(fp,
"%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %8s\n",
addr[0],addr[1],addr[2],addr[3],
addr[4],addr[5],addr[6],addr[7],
&index, &plen, &scope, &flags, ifname) != EOF) {
myflags = 0;
if (strncmp(lastname, ifname, IFNAMSIZ) == 0) {
if (doaliases == 0)
continue; /* already processed this interface */
myflags = IFI_ALIAS;
}
memcpy(lastname, ifname, IFNAMSIZ);
ifi = (struct ifi_info*)calloc(1, sizeof(struct ifi_info));
if (ifi == NULL) {
goto gotError;
}
*ifipnext = ifi; /* prev points to this new one */
ifipnext = &ifi->ifi_next; /* pointer to next one goes here */
sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
addr[0],addr[1],addr[2],addr[3],
addr[4],addr[5],addr[6],addr[7]);
/* Add address of the interface */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_flags = AI_NUMERICHOST;
err = getaddrinfo(addr6, NULL, &hints, &res0);
if (err) {
goto gotError;
}
ifi->ifi_addr = calloc(1, sizeof(struct sockaddr_in6));
if (ifi->ifi_addr == NULL) {
goto gotError;
}
memcpy(ifi->ifi_addr, res0->ai_addr, sizeof(struct sockaddr_in6));
/* Add netmask of the interface */
char ipv6addr[INET6_ADDRSTRLEN];
plen_to_mask(plen, ipv6addr);
ifi->ifi_netmask = calloc(1, sizeof(struct sockaddr_in6));
if (ifi->ifi_addr == NULL) {
goto gotError;
}
sin6=calloc(1, sizeof(struct sockaddr_in6));
addrptr=calloc(1, sizeof(struct in6_addr));
inet_pton(family, ipv6addr, addrptr);
sin6->sin6_family=family;
sin6->sin6_addr=*addrptr;
sin6->sin6_scope_id=scope;
memcpy(ifi->ifi_netmask, sin6, sizeof(struct sockaddr_in6));
free(sin6);
/* Add interface name */
memcpy(ifi->ifi_name, ifname, IFI_NAME);
/* Add interface index */
ifi->ifi_index = index;
/* If interface is in /proc then it is up*/
ifi->ifi_flags = IFF_UP;
freeaddrinfo(res0);
res0=NULL;
}
}
goto done;
gotError:
if (ifihead != NULL) {
free_ifi_info(ifihead);
ifihead = NULL;
}
if (res0 != NULL) {
freeaddrinfo(res0);
res0=NULL;
}
done:
return(ifihead); /* pointer to first structure in linked list */
}
#endif /* LINUX */
#endif /* defined(AF_INET6) && HAVE_IPV6 */
#if HAVE_SOLARIS
/*
* Converts prefix length to network mask. Assumes
* addr points to a zeroed out buffer and prefix <= sizeof(addr)
* Unlike plen_to_mask returns netmask in binary form and not
* in text form.
*/
static void plen_to_netmask(int prefix, unsigned char *addr) {
for (; prefix > 8; prefix -= 8)
*addr++ = 0xff;
for (; prefix > 0; prefix--)
*addr = (*addr >> 1) | 0x80;
}
/*
* This function goes through all the IP interfaces associated with a
* physical interface and finds the best matched one for use by mDNS.
* Returns NULL when none of the IP interfaces associated with a physical
* interface are usable. Otherwise returns the best matched interface
* information and a pointer to the best matched lifreq.
*/
struct ifi_info *
select_src_ifi_info_solaris(int sockfd, int numifs,
struct lifreq *lifrlist, const char *curifname,
struct lifreq **best_lifr)
{
struct lifreq *lifr;
struct lifreq lifrcopy;
struct ifi_info *ifi;
char *chptr;
char cmpifname[LIFNAMSIZ];
int i;
uint64_t best_lifrflags;
uint64_t ifflags;
*best_lifr = NULL;
/*
* Check all logical interfaces associated with the physical
* interface and figure out which one works best for us.
*/
for (i = numifs, lifr = lifrlist; i > 0; --i, ++lifr) {
if (strlcpy(cmpifname, lifr->lifr_name, sizeof(cmpifname)) >= sizeof(cmpifname))
continue; /* skip interface */
/* Strip logical interface number before checking ifname */
if ((chptr = strchr(cmpifname, ':')) != NULL)
*chptr = '\0';
/*
* Check ifname to see if the logical interface is associated
* with the physical interface we are interested in.
*/
if (strcmp(cmpifname, curifname) != 0)
continue;
lifrcopy = *lifr;
if (ioctl(sockfd, SIOCGLIFFLAGS, &lifrcopy) < 0) {
/* interface removed */
if (errno == ENXIO)
continue;
return(NULL);
}
ifflags = lifrcopy.lifr_flags;
/* ignore address if not up */
if ((ifflags & IFF_UP) == 0)
continue;
/*
* Avoid address if any of the following flags are set:
* IFF_NOXMIT: no packets transmitted over interface
* IFF_NOLOCAL: no address
* IFF_PRIVATE: is not advertised
*/
if (ifflags & (IFF_NOXMIT | IFF_NOLOCAL | IFF_PRIVATE))
continue;
/* A DHCP client will have IFF_UP set yet the address is zero. Ignore */
if (lifr->lifr_addr.ss_family == AF_INET) {
struct sockaddr_in *sinptr;
sinptr = (struct sockaddr_in *) &lifr->lifr_addr;
if (sinptr->sin_addr.s_addr == INADDR_ANY)
continue;
}
if (*best_lifr != NULL) {
/*
* Check if we found a better interface by checking
* the flags. If flags are identical we prefer
* the new found interface.
*/
uint64_t diff_flags = best_lifrflags ^ ifflags;
/* If interface has a different set of flags */
if (diff_flags != 0) {
/* Check flags in increasing order of ones we prefer */
/* Address temporary? */
if ((diff_flags & IFF_TEMPORARY) &&
(ifflags & IFF_TEMPORARY))
continue;
/* Deprecated address? */
if ((diff_flags & IFF_DEPRECATED) &&
(ifflags & IFF_DEPRECATED))
continue;
/* Last best-matched interface address has preferred? */
if ((diff_flags & IFF_PREFERRED) &&
((ifflags & IFF_PREFERRED) == 0))
continue;
}
}
/* Set best match interface & flags */
*best_lifr = lifr;
best_lifrflags = ifflags;
}
if (*best_lifr == NULL)
return(NULL);
/* Found a match: return the interface information */
ifi = calloc(1, sizeof(struct ifi_info));
if (ifi == NULL)
return(NULL);
ifi->ifi_flags = best_lifrflags;
ifi->ifi_index = if_nametoindex((*best_lifr)->lifr_name);
if (strlcpy(ifi->ifi_name, (*best_lifr)->lifr_name, sizeof(ifi->ifi_name)) >= sizeof(ifi->ifi_name)) {
free(ifi);
return(NULL);
}
return(ifi);
}
/*
* Returns a list of IP interface information on Solaris. The function
* returns all IP interfaces on the system with IPv4 address assigned
* when passed AF_INET and returns IP interfaces with IPv6 address assigned
* when AF_INET6 is passed.
*/
struct ifi_info *get_ifi_info_solaris(int family)
{
struct ifi_info *ifi, *ifihead, **ifipnext;
int sockfd;
int len;
char *buf;
char *cptr;
char ifname[LIFNAMSIZ], cmpifname[LIFNAMSIZ];
struct sockaddr_in *sinptr;
struct lifnum lifn;
struct lifconf lifc;
struct lifreq *lifrp, *best_lifr;
struct lifreq lifrcopy;
int numifs, nlifr, n;
#if defined(AF_INET6) && HAVE_IPV6
struct sockaddr_in6 *sinptr6;
#endif
ifihead = NULL;
sockfd = socket(family, SOCK_DGRAM, 0);
if (sockfd < 0)
goto gotError;
again:
lifn.lifn_family = family;
lifn.lifn_flags = 0;
if (ioctl(sockfd, SIOCGLIFNUM, &lifn) < 0)
goto gotError;
/*
* Pad interface count to detect & retrieve any
* additional interfaces between IFNUM & IFCONF calls.
*/
lifn.lifn_count += 4;
numifs = lifn.lifn_count;
len = numifs * sizeof (struct lifreq);
buf = alloca(len);
lifc.lifc_family = family;
lifc.lifc_len = len;
lifc.lifc_buf = buf;
lifc.lifc_flags = 0;
if (ioctl(sockfd, SIOCGLIFCONF, &lifc) < 0)
goto gotError;
nlifr = lifc.lifc_len / sizeof(struct lifreq);
if (nlifr >= numifs)
goto again;
lifrp = lifc.lifc_req;
ifipnext = &ifihead;
for (n = nlifr; n > 0; n--, lifrp++) {
if (lifrp->lifr_addr.ss_family != family)
continue;
/*
* See if we have already processed the interface
* by checking the interface names.
*/
if (strlcpy(ifname, lifrp->lifr_name, sizeof(ifname)) >= sizeof(ifname))
goto gotError;
if ((cptr = strchr(ifname, ':')) != NULL)
*cptr = '\0';
/*
* If any of the interfaces found so far share the physical
* interface name then we have already processed the interface.
*/
for (ifi = ifihead; ifi != NULL; ifi = ifi->ifi_next) {
/* Retrieve physical interface name */
(void) strlcpy(cmpifname, ifi->ifi_name, sizeof(cmpifname));
/* Strip logical interface number before checking ifname */
if ((cptr = strchr(cmpifname, ':')) != NULL)
*cptr = '\0';
if (strcmp(cmpifname, ifname) == 0)
break;
}
if (ifi != NULL)
continue; /* already processed */
/*
* New interface, find the one with the preferred source
* address for our use in Multicast DNS.
*/
if ((ifi = select_src_ifi_info_solaris(sockfd, nlifr,
lifc.lifc_req, ifname, &best_lifr)) == NULL)
continue;
assert(best_lifr != NULL);
assert((best_lifr->lifr_addr.ss_family == AF_INET6) ||
(best_lifr->lifr_addr.ss_family == AF_INET));
switch (best_lifr->lifr_addr.ss_family) {
#if defined(AF_INET6) && HAVE_IPV6
case AF_INET6:
sinptr6 = (struct sockaddr_in6 *) &best_lifr->lifr_addr;
ifi->ifi_addr = malloc(sizeof(struct sockaddr_in6));
if (ifi->ifi_addr == NULL)
goto gotError;
memcpy(ifi->ifi_addr, sinptr6, sizeof(struct sockaddr_in6));
ifi->ifi_netmask = calloc(1, sizeof(struct sockaddr_in6));
if (ifi->ifi_netmask == NULL)
goto gotError;
sinptr6 = (struct sockaddr_in6 *)(ifi->ifi_netmask);
sinptr6->sin6_family = AF_INET6;
plen_to_netmask(best_lifr->lifr_addrlen,
(unsigned char *) &(sinptr6->sin6_addr));
break;
#endif
case AF_INET:
sinptr = (struct sockaddr_in *) &best_lifr->lifr_addr;
ifi->ifi_addr = malloc(sizeof(struct sockaddr_in));
if (ifi->ifi_addr == NULL)
goto gotError;
memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in));
lifrcopy = *best_lifr;
if (ioctl(sockfd, SIOCGLIFNETMASK, &lifrcopy) < 0) {
/* interface removed */
if (errno == ENXIO) {
free(ifi->ifi_addr);
free(ifi);
continue;
}
goto gotError;
}
ifi->ifi_netmask = malloc(sizeof(struct sockaddr_in));
if (ifi->ifi_netmask == NULL)
goto gotError;
sinptr = (struct sockaddr_in *) &lifrcopy.lifr_addr;
sinptr->sin_family = AF_INET;
memcpy(ifi->ifi_netmask, sinptr, sizeof(struct sockaddr_in));
break;
default:
/* never reached */
break;
}
*ifipnext = ifi; /* prev points to this new one */
ifipnext = &ifi->ifi_next; /* pointer to next one goes here */
}
(void) close(sockfd);
return(ifihead); /* pointer to first structure in linked list */
gotError:
if (sockfd != -1)
(void) close(sockfd);
if (ifihead != NULL)
free_ifi_info(ifihead);
return(NULL);
}
#endif /* HAVE_SOLARIS */
struct ifi_info *get_ifi_info(int family, int doaliases)
{
int junk;
struct ifi_info *ifi, *ifihead, **ifipnext;
int sockfd, sockf6, len, lastlen, flags, myflags;
#ifdef NOT_HAVE_IF_NAMETOINDEX
int index = 200;
#endif
char *ptr, *buf, lastname[IFNAMSIZ], *cptr;
struct ifconf ifc;
struct ifreq *ifr, ifrcopy;
struct sockaddr_in *sinptr;
#if defined(AF_INET6) && HAVE_IPV6
struct sockaddr_in6 *sinptr6;
#endif
#if defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
if(family == AF_INET6) return get_ifi_info_linuxv6(family, doaliases);
#elif HAVE_SOLARIS
return get_ifi_info_solaris(family);
#endif
sockfd = -1;
sockf6 = -1;
buf = NULL;
ifihead = NULL;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
goto gotError;
}
lastlen = 0;
len = 100 * sizeof(struct ifreq); /* initial buffer size guess */
for ( ; ; ) {
buf = (char*)malloc(len);
if (buf == NULL) {
goto gotError;
}
ifc.ifc_len = len;
ifc.ifc_buf = buf;
if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
if (errno != EINVAL || lastlen != 0) {
goto gotError;
}
} else {
if (ifc.ifc_len == lastlen)
break; /* success, len has not changed */
lastlen = ifc.ifc_len;
}
len += 10 * sizeof(struct ifreq); /* increment */
free(buf);
}
ifihead = NULL;
ifipnext = &ifihead;
lastname[0] = 0;
/* end get_ifi_info1 */
/* include get_ifi_info2 */
for (ptr = buf; ptr < buf + ifc.ifc_len; ) {
ifr = (struct ifreq *) ptr;
/* Advance to next one in buffer */
if (sizeof(struct ifreq) > sizeof(ifr->ifr_name) + GET_SA_LEN(ifr->ifr_addr))
ptr += sizeof(struct ifreq);
else
ptr += sizeof(ifr->ifr_name) + GET_SA_LEN(ifr->ifr_addr);
// fprintf(stderr, "intf %p name=%s AF=%d\n", index, ifr->ifr_name, ifr->ifr_addr.sa_family);
if (ifr->ifr_addr.sa_family != family)
continue; /* ignore if not desired address family */
myflags = 0;
if ( (cptr = strchr(ifr->ifr_name, ':')) != NULL)
*cptr = 0; /* replace colon will null */
if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) {
if (doaliases == 0)
continue; /* already processed this interface */
myflags = IFI_ALIAS;
}
memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
ifrcopy = *ifr;
if (ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy) < 0) {
goto gotError;
}
flags = ifrcopy.ifr_flags;
if ((flags & IFF_UP) == 0)
continue; /* ignore if interface not up */
ifi = (struct ifi_info*)calloc(1, sizeof(struct ifi_info));
if (ifi == NULL) {
goto gotError;
}
*ifipnext = ifi; /* prev points to this new one */
ifipnext = &ifi->ifi_next; /* pointer to next one goes here */
ifi->ifi_flags = flags; /* IFF_xxx values */
ifi->ifi_myflags = myflags; /* IFI_xxx values */
#ifndef NOT_HAVE_IF_NAMETOINDEX
ifi->ifi_index = if_nametoindex(ifr->ifr_name);
#else
ifrcopy = *ifr;
#ifdef SIOCGIFINDEX
if ( 0 >= ioctl(sockfd, SIOCGIFINDEX, &ifrcopy))
ifi->ifi_index = ifrcopy.ifr_index;
else
#endif
ifi->ifi_index = index++; /* SIOCGIFINDEX is broken on Solaris 2.5ish, so fake it */
#endif
memcpy(ifi->ifi_name, ifr->ifr_name, IFI_NAME);
ifi->ifi_name[IFI_NAME-1] = '\0';
/* end get_ifi_info2 */
/* include get_ifi_info3 */
switch (ifr->ifr_addr.sa_family) {
case AF_INET:
sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
if (ifi->ifi_addr == NULL) {
ifi->ifi_addr = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in));
if (ifi->ifi_addr == NULL) {
goto gotError;
}
memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in));
#ifdef SIOCGIFNETMASK
if (ioctl(sockfd, SIOCGIFNETMASK, &ifrcopy) < 0) goto gotError;
ifi->ifi_netmask = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in));
if (ifi->ifi_netmask == NULL) goto gotError;
sinptr = (struct sockaddr_in *) &ifrcopy.ifr_addr;
/* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
#ifndef NOT_HAVE_SA_LEN
sinptr->sin_len = sizeof(struct sockaddr_in);
#endif
sinptr->sin_family = AF_INET;
memcpy(ifi->ifi_netmask, sinptr, sizeof(struct sockaddr_in));
#endif
#ifdef SIOCGIFBRDADDR
if (flags & IFF_BROADCAST) {
if (ioctl(sockfd, SIOCGIFBRDADDR, &ifrcopy) < 0) {
goto gotError;
}
sinptr = (struct sockaddr_in *) &ifrcopy.ifr_broadaddr;
/* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
#ifndef NOT_HAVE_SA_LEN
sinptr->sin_len = sizeof( struct sockaddr_in );
#endif
sinptr->sin_family = AF_INET;
ifi->ifi_brdaddr = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in));
if (ifi->ifi_brdaddr == NULL) {
goto gotError;
}
memcpy(ifi->ifi_brdaddr, sinptr, sizeof(struct sockaddr_in));
}
#endif
#ifdef SIOCGIFDSTADDR
if (flags & IFF_POINTOPOINT) {
if (ioctl(sockfd, SIOCGIFDSTADDR, &ifrcopy) < 0) {
goto gotError;
}
sinptr = (struct sockaddr_in *) &ifrcopy.ifr_dstaddr;
/* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
#ifndef NOT_HAVE_SA_LEN
sinptr->sin_len = sizeof( struct sockaddr_in );
#endif
sinptr->sin_family = AF_INET;
ifi->ifi_dstaddr = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in));
if (ifi->ifi_dstaddr == NULL) {
goto gotError;
}
memcpy(ifi->ifi_dstaddr, sinptr, sizeof(struct sockaddr_in));
}
#endif
}
break;
#if defined(AF_INET6) && HAVE_IPV6
case AF_INET6:
sinptr6 = (struct sockaddr_in6 *) &ifr->ifr_addr;
if (ifi->ifi_addr == NULL) {
ifi->ifi_addr = calloc(1, sizeof(struct sockaddr_in6));
if (ifi->ifi_addr == NULL) {
goto gotError;
}
/* Some platforms (*BSD) inject the prefix in IPv6LL addresses */
/* We need to strip that out */
if (IN6_IS_ADDR_LINKLOCAL(&sinptr6->sin6_addr))
sinptr6->sin6_addr.s6_addr[2] = sinptr6->sin6_addr.s6_addr[3] = 0;
memcpy(ifi->ifi_addr, sinptr6, sizeof(struct sockaddr_in6));
#ifdef SIOCGIFNETMASK_IN6
{
struct in6_ifreq ifr6;
if (sockf6 == -1)
sockf6 = socket(AF_INET6, SOCK_DGRAM, 0);
bzero(&ifr6, sizeof(ifr6));
memcpy(&ifr6.ifr_name, &ifr->ifr_name, sizeof(ifr6.ifr_name ));
memcpy(&ifr6.ifr_ifru.ifru_addr, &ifr->ifr_addr, sizeof(ifr6.ifr_ifru.ifru_addr));
if (ioctl(sockf6, SIOCGIFNETMASK_IN6, &ifr6) < 0) goto gotError;
ifi->ifi_netmask = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in6));
if (ifi->ifi_netmask == NULL) goto gotError;
sinptr6 = (struct sockaddr_in6 *) &ifr6.ifr_ifru.ifru_addr;
memcpy(ifi->ifi_netmask, sinptr6, sizeof(struct sockaddr_in6));
}
#endif
}
break;
#endif
default:
break;
}
}
goto done;
gotError:
if (ifihead != NULL) {
free_ifi_info(ifihead);
ifihead = NULL;
}
done:
if (buf != NULL) {
free(buf);
}
if (sockfd != -1) {
junk = close(sockfd);
assert(junk == 0);
}
if (sockf6 != -1) {
junk = close(sockf6);
assert(junk == 0);
}
return(ifihead); /* pointer to first structure in linked list */
}
/* end get_ifi_info3 */
/* include free_ifi_info */
void
free_ifi_info(struct ifi_info *ifihead)
{
struct ifi_info *ifi, *ifinext;
for (ifi = ifihead; ifi != NULL; ifi = ifinext) {
if (ifi->ifi_addr != NULL)
free(ifi->ifi_addr);
if (ifi->ifi_brdaddr != NULL)
free(ifi->ifi_brdaddr);
if (ifi->ifi_dstaddr != NULL)
free(ifi->ifi_dstaddr);
if (ifi->ifi_netmask != NULL)
free(ifi->ifi_netmask);
ifinext = ifi->ifi_next; /* can't fetch ifi_next after free() */
free(ifi); /* the ifi_info{} itself */
}
}
/* end free_ifi_info */
ssize_t
recvfrom_flags(int fd, void *ptr, size_t nbytes, int *flagsp,
struct sockaddr *sa, socklen_t *salenptr, struct my_in_pktinfo *pktp, u_char *ttl)
{
struct msghdr msg;
struct iovec iov[1];
ssize_t n;
#ifdef CMSG_FIRSTHDR
struct cmsghdr *cmptr;
union {
struct cmsghdr cm;
char control[1024];
pad64_t align8; /* ensure structure is 8-byte aligned on sparc */
} control_un;
*ttl = 255; // If kernel fails to provide TTL data then assume the TTL was 255 as it should be
msg.msg_control = (void *) control_un.control;
msg.msg_controllen = sizeof(control_un.control);
msg.msg_flags = 0;
#else
memset(&msg, 0, sizeof(msg)); /* make certain msg_accrightslen = 0 */
#endif /* CMSG_FIRSTHDR */
msg.msg_name = (char *) sa;
msg.msg_namelen = *salenptr;
iov[0].iov_base = (char *)ptr;
iov[0].iov_len = nbytes;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
if ( (n = recvmsg(fd, &msg, *flagsp)) < 0)
return(n);
*salenptr = msg.msg_namelen; /* pass back results */
if (pktp) {
/* 0.0.0.0, i/f = -1 */
/* We set the interface to -1 so that the caller can
tell whether we returned a meaningful value or
just some default. Previously this code just
set the value to 0, but I'm concerned that 0
might be a valid interface value.
*/
memset(pktp, 0, sizeof(struct my_in_pktinfo));
pktp->ipi_ifindex = -1;
}
/* end recvfrom_flags1 */
/* include recvfrom_flags2 */
#ifndef CMSG_FIRSTHDR
#warning CMSG_FIRSTHDR not defined. Will not be able to determine destination address, received interface, etc.
*flagsp = 0; /* pass back results */
return(n);
#else
*flagsp = msg.msg_flags; /* pass back results */
if (msg.msg_controllen < (socklen_t)sizeof(struct cmsghdr) ||
(msg.msg_flags & MSG_CTRUNC) || pktp == NULL)
return(n);
for (cmptr = CMSG_FIRSTHDR(&msg); cmptr != NULL;
cmptr = CMSG_NXTHDR(&msg, cmptr)) {
#ifdef IP_PKTINFO
#if in_pktinfo_definition_is_missing
struct in_pktinfo
{
int ipi_ifindex;
struct in_addr ipi_spec_dst;
struct in_addr ipi_addr;
};
#endif
if (cmptr->cmsg_level == IPPROTO_IP &&
cmptr->cmsg_type == IP_PKTINFO) {
struct in_pktinfo *tmp;
struct sockaddr_in *sin = (struct sockaddr_in*)&pktp->ipi_addr;
tmp = (struct in_pktinfo *) CMSG_DATA(cmptr);
sin->sin_family = AF_INET;
sin->sin_addr = tmp->ipi_addr;
sin->sin_port = 0;
pktp->ipi_ifindex = tmp->ipi_ifindex;
continue;
}
#endif
#ifdef IP_RECVDSTADDR
if (cmptr->cmsg_level == IPPROTO_IP &&
cmptr->cmsg_type == IP_RECVDSTADDR) {
struct sockaddr_in *sin = (struct sockaddr_in*)&pktp->ipi_addr;
sin->sin_family = AF_INET;
sin->sin_addr = *(struct in_addr*)CMSG_DATA(cmptr);
sin->sin_port = 0;
continue;
}
#endif
#ifdef IP_RECVIF
if (cmptr->cmsg_level == IPPROTO_IP &&
cmptr->cmsg_type == IP_RECVIF) {
struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA(cmptr);
#ifndef HAVE_BROKEN_RECVIF_NAME
int nameLen = (sdl->sdl_nlen < IFI_NAME - 1) ? sdl->sdl_nlen : (IFI_NAME - 1);
strncpy(pktp->ipi_ifname, sdl->sdl_data, nameLen);
#endif
(void) memcpy(&pktp->ipi_ifindex, CMSG_DATA(cmptr), sizeof(uint_t));
assert(pktp->ipi_ifname[IFI_NAME - 1] == 0);
// null terminated because of memset above
continue;
}
#endif
#ifdef IP_RECVTTL
if (cmptr->cmsg_level == IPPROTO_IP &&
cmptr->cmsg_type == IP_RECVTTL) {
*ttl = *(u_char*)CMSG_DATA(cmptr);
continue;
}
else if (cmptr->cmsg_level == IPPROTO_IP &&
cmptr->cmsg_type == IP_TTL) { // some implementations seem to send IP_TTL instead of IP_RECVTTL
*ttl = *(int*)CMSG_DATA(cmptr);
continue;
}
#endif
#if defined(IPV6_PKTINFO) && HAVE_IPV6
if (cmptr->cmsg_level == IPPROTO_IPV6 &&
cmptr->cmsg_type == IPV6_PKTINFO) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&pktp->ipi_addr;
struct in6_pktinfo *ip6_info = (struct in6_pktinfo*)CMSG_DATA(cmptr);
sin6->sin6_family = AF_INET6;
#ifndef NOT_HAVE_SA_LEN
sin6->sin6_len = sizeof(*sin6);
#endif
sin6->sin6_addr = ip6_info->ipi6_addr;
sin6->sin6_flowinfo = 0;
sin6->sin6_scope_id = 0;
sin6->sin6_port = 0;
pktp->ipi_ifindex = ip6_info->ipi6_ifindex;
continue;
}
#endif
#if defined(IPV6_HOPLIMIT) && HAVE_IPV6
if (cmptr->cmsg_level == IPPROTO_IPV6 &&
cmptr->cmsg_type == IPV6_HOPLIMIT) {
*ttl = *(int*)CMSG_DATA(cmptr);
continue;
}
#endif
assert(0); // unknown ancillary data
}
return(n);
#endif /* CMSG_FIRSTHDR */
}
// **********************************************************************************************
// daemonize the process. Adapted from "Unix Network Programming" vol 1 by Stevens, section 12.4.
// Returns 0 on success, -1 on failure.
#ifdef NOT_HAVE_DAEMON
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
int daemon(int nochdir, int noclose)
{
switch (fork())
{
case -1: return (-1); // Fork failed
case 0: break; // Child -- continue
default: _exit(0); // Parent -- exit
}
if (setsid() == -1) return(-1);
signal(SIGHUP, SIG_IGN);
switch (fork()) // Fork again, primarily for reasons of Unix trivia
{
case -1: return (-1); // Fork failed
case 0: break; // Child -- continue
default: _exit(0); // Parent -- exit
}
if (!nochdir) (void)chdir("/");
umask(0);
if (!noclose)
{
int fd = open("/dev/null", O_RDWR, 0);
if (fd != -1)
{
// Avoid unnecessarily duplicating a file descriptor to itself
if (fd != STDIN_FILENO) (void)dup2(fd, STDIN_FILENO);
if (fd != STDOUT_FILENO) (void)dup2(fd, STDOUT_FILENO);
if (fd != STDERR_FILENO) (void)dup2(fd, STDERR_FILENO);
if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO)
(void)close (fd);
}
}
return (0);
}
#endif /* NOT_HAVE_DAEMON */
|