This repository has been archived by the owner on Jan 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathaddress.c
1477 lines (1227 loc) · 37.6 KB
/
address.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
/*
* Copyright (C) 1998-2012 Luca Deri <[email protected]>
*
* http://www.ntop.org/
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "ntop.h"
/* Global */
static char hex[] = "0123456789ABCDEF";
/* Forward */
static u_int _ns_get16(const u_char *src);
static int _ns_name_ntop(const u_char *src,
char *dst, size_t dstsiz);
static int _dn_skipname(const u_char *ptr, const u_char *eom); /* forward */
static int _ns_name_uncompress(const u_char *msg,
const u_char *eom, const u_char *src,
char *dst, size_t dstsiz);
static int _ns_name_unpack(const u_char *msg,
const u_char *eom, const u_char *src,
u_char *dst, size_t dstsiz);
static void updateDeviceHostNameInfo(HostAddr addr, char* symbolic, int actualDeviceId, int type);
static void updateHostNameInfo(HostAddr addr, char* symbolic, int type);
/* #define DNS_DEBUG */
/* #define MDNS_DEBUG */
typedef struct hostAddrList {
HostAddr addr;
struct hostAddrList *next, *prev;
} HostAddrList;
static HostAddrList *hostAddrList_head = NULL, *hostAddrList_tail = NULL;
typedef struct {
time_t dump_date;
char hostname[MAX_LEN_SYM_HOST_NAME];
} HostNameCache;
/* **************************************************** */
void cacheHostName(HostAddr *addr, char* symbolic) {
HostNameCache name;
datum data_data, key_data;
accessMutex(&myGlobals.serialLockMutex, "cacheHostName");
name.dump_date = myGlobals.actTime;
safe_snprintf(__FILE__, __LINE__, name.hostname, sizeof(name.hostname), "%s", symbolic);
key_data.dptr = (char*)addr, key_data.dsize = sizeof(HostAddr);
data_data.dptr = (char*)&name, data_data.dsize = (int)(strlen(name.hostname)+sizeof(name.dump_date)+1);
if(gdbm_store(myGlobals.resolverCacheFile, key_data, data_data, GDBM_REPLACE) != 0)
traceEvent(CONST_TRACE_ERROR, "While adding host name %s", symbolic);
// traceEvent(CONST_TRACE_INFO, "Cached host name %s", symbolic);
releaseMutex(&myGlobals.serialLockMutex);
}
/* **************************************************** */
char* getHostNameFromCache(HostAddr *addr, char *buf, u_int buf_len) {
char *ret;
datum return_data, key_data;
accessMutex(&myGlobals.serialLockMutex, "getHostNameFromCache");
key_data.dptr = (char*)addr, key_data.dsize = sizeof(HostAddr);
return_data = gdbm_fetch(myGlobals.resolverCacheFile, key_data);
if(return_data.dptr != NULL) {
HostNameCache *dump = (HostNameCache*)return_data.dptr;
safe_snprintf(__FILE__, __LINE__, buf, buf_len, "%s", dump->hostname);
free(return_data.dptr);
ret = buf;
} else {
/* Not found */
ret = NULL;
}
releaseMutex(&myGlobals.serialLockMutex);
// traceEvent(CONST_TRACE_INFO, "Resolved %s", ret ? ret : "<none>");
return(ret);
}
/* **************************************** */
void initAddressResolution(void) {
createCondvar(&myGlobals.queueAddressCondvar);
}
/* **************************************** */
static void updateDeviceHostNameInfo(HostAddr addr, char* symbolic, int actualDeviceId, int type) {
HostTraffic *el;
if(myGlobals.ntopRunState > FLAG_NTOPSTATE_RUN) return;
/* Search the instance and update its name */
for(el=getFirstHost(actualDeviceId); el != NULL; el = getNextHost(actualDeviceId, el)) {
if(addrcmp(&el->hostIpAddress, &addr) == 0) {
accessAddrResMutex("updateHostNameInfo");
if(el != NULL) {
unsigned short i;
if(strlen(symbolic) >= (MAX_LEN_SYM_HOST_NAME-1))
symbolic[MAX_LEN_SYM_HOST_NAME-2] = '\0';
/* Really needed ? */
for(i=0; i<strlen(symbolic); i++) symbolic[i] = tolower(symbolic[i]);
/* traceEvent(CONST_TRACE_INFO, "[%s] --> %s", el->hostResolvedName, symbolic); */
setResolvedName(el, symbolic, type);
}
releaseAddrResMutex();
}
}
}
/* **************************************** */
static void updateHostNameInfo(HostAddr addr, char* symbolic, int type) {
int i;
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "updateDeviceHostNameInfo(%s <--> %s)", symbolic, addrtostr(&addr));
#endif
cacheHostName(&addr, symbolic);
for(i=0; i<myGlobals.numDevices; i++) {
if(!myGlobals.device[i].virtualDevice)
updateDeviceHostNameInfo(addr, symbolic, i, type);
}
}
/* ************************************ */
static void queueAddress(HostAddr elem) {
HostAddrList *cloned = NULL;
if(myGlobals.runningPref.numericFlag == noDnsResolution) return;
if(_pseudoLocalAddress(&elem, NULL, NULL)) {
/* Local Host */
if(myGlobals.runningPref.trackOnlyLocalHosts) return;
else if(myGlobals.runningPref.numericFlag == dnsResolutionForLocalRemoteOnly) return;
} else {
/* Remote Host */
if(myGlobals.runningPref.numericFlag == dnsResolutionForLocalHostsOnly) return;
}
accessAddrResMutex("queueAddress");
if(myGlobals.addressQueuedCurrent > 16384) {
myGlobals.addressUnresolvedDrops++;
} else {
/* First check if the address we want to resolve is already in queue
waiting to be resolved */
HostAddrList *head = hostAddrList_head;
while(head != NULL) {
if(memcmp(&head->addr, &elem, sizeof(elem)) == 0) {
releaseAddrResMutex();
return;
}
head = head->next;
}
#ifdef DEBUG
traceEvent(CONST_TRACE_ERROR, "queueAddress(%s)", addrtostr(&elem));
#endif
if((cloned = (HostAddrList*)calloc(1, sizeof(HostAddrList))) != NULL) {
memcpy(&cloned->addr, &elem, sizeof(HostAddr));
if(hostAddrList_head) hostAddrList_head->prev = cloned;
cloned->next = hostAddrList_head, cloned->prev = NULL;
hostAddrList_head = cloned;
if(hostAddrList_tail == NULL)
hostAddrList_tail = cloned; /* First element of the list */
signalCondvar(&myGlobals.queueAddressCondvar, 0);
myGlobals.addressQueuedCurrent++;
if(myGlobals.addressQueuedCurrent > myGlobals.addressQueuedMax)
myGlobals.addressQueuedMax = myGlobals.addressQueuedCurrent;
}
}
releaseAddrResMutex();
}
/* ************************************ */
static void processAddressResRequest(HostAddrList *elem) {
if(elem) {
struct hostent *he = NULL;
int family, size;
char theAddr[64];
#if defined(HAVE_GETHOSTBYADDR_R)
struct hostent _hp, *__hp;
char buffer[4096]; /* It MUST be 4096 as otherwise on Linux will fail */
#endif
memset(theAddr, 0, sizeof(theAddr));
addrget(&elem->addr, theAddr, &family, &size);
#if defined(HAVE_GETHOSTBYADDR_R)
#ifdef SOLARIS
he = gethostbyaddr_r((const char*)theAddr, size,
family, &_hp,
buffer, sizeof(buffer),
&h_errno);
#else
#if 0
traceEvent(CONST_TRACE_INFO, "About to resolve %s [family=%d][size=%d]", addrtostr(&elem->addr), family, size);
#endif
if(gethostbyaddr_r((const char*)theAddr, size,
family, &_hp,
buffer, sizeof(buffer),
&__hp, &h_errno) == 0) {
if(h_errno == 0)
he = &_hp;
else
he = NULL;
} else
he = NULL;
#endif
#else
he = gethostbyaddr(theAddr, size, family);
#endif
if((he != NULL) && (he->h_name != NULL)) {
updateHostNameInfo(elem->addr, he->h_name, FLAG_HOST_SYM_ADDR_TYPE_NAME);
accessAddrResMutex("dequeueAddress"); myGlobals.resolvedAddresses++; releaseAddrResMutex();
} else {
#if 0
traceEvent(CONST_TRACE_ERROR, "Address resolution failure [%d][%s]", h_errno, hstrerror(h_errno));
#endif
accessAddrResMutex("dequeueAddress"); myGlobals.failedResolvedAddresses++; releaseAddrResMutex();
}
memset(elem, 0, sizeof(HostAddr));
free(elem);
}
}
/* ************************************ */
static void* dequeueNextAddress(void) {
HostAddrList *elem;
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "DEBUG: Waiting for address to resolve...");
#endif
while(hostAddrList_tail == NULL) {
if(myGlobals.ntopRunState > FLAG_NTOPSTATE_RUN) break;
waitCondvar(&myGlobals.queueAddressCondvar);
}
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "DEBUG: Address resolution started...");
#endif
accessAddrResMutex("dequeueAddress");
if(hostAddrList_tail != NULL) {
elem = hostAddrList_tail;
hostAddrList_tail = hostAddrList_tail->prev;
if(hostAddrList_head == elem)
hostAddrList_head = NULL;
if(elem->prev != NULL)
elem->prev->next = NULL;
if(myGlobals.addressQueuedCurrent > 0) myGlobals.addressQueuedCurrent--;
} else
elem = NULL;
releaseAddrResMutex();
return(elem);
}
/* ************************************ */
void* dequeueAddress(void *_i) {
int dqaIndex = (int)((long)_i);
HostAddrList *elem;
traceEvent(CONST_TRACE_INFO,
"THREADMGMT[t%lu]: DNSAR(%d): Address resolution thread running",
(long unsigned int)pthread_self(), dqaIndex+1);
while(myGlobals.ntopRunState <= FLAG_NTOPSTATE_RUN) {
elem = dequeueNextAddress();
processAddressResRequest(elem);
} /* endless loop */
myGlobals.dequeueAddressThreadId[dqaIndex] = 0;
/* We're shutting down so let'e empty the queue */
do {
elem = dequeueNextAddress();
if(elem != NULL)
free(elem);
} while(elem != NULL);
traceEvent(CONST_TRACE_INFO, "THREADMGMT[t%lu]: DNSAR(%d): Address resolution thread terminated [p%d]",
(long unsigned int)pthread_self(), dqaIndex+1,
#ifndef WIN32
getpid()
#else
0
#endif
);
return(NULL);
}
/* ************************************ */
void cleanupAddressQueue(void) {
/* Nothing to do */
}
/* ************************************ */
char* _intop(struct in6_addr *addr, char *buf, u_short buflen) {
char *ret = (char *)inet_ntop(AF_INET6, addr, buf, buflen);
return(ret);
}
/* ************************************ */
char* intop(struct in6_addr *addr) {
static char ntop_buf[INET6_ADDRSTRLEN+1];
memset(ntop_buf, 0, INET6_ADDRSTRLEN);
return (char *)_intop(addr, ntop_buf,sizeof(ntop_buf));
}
/* ************************************ */
/*
* A faster replacement for inet_ntoa().
*/
char* _intoa(struct in_addr addr, char* buf, u_short bufLen) {
char *cp, *retStr;
u_int byte;
int n;
cp = &buf[bufLen];
*--cp = '\0';
n = 4;
do {
byte = addr.s_addr & 0xff;
*--cp = byte % 10 + '0';
byte /= 10;
if (byte > 0) {
*--cp = byte % 10 + '0';
byte /= 10;
if (byte > 0)
*--cp = byte + '0';
}
*--cp = '.';
addr.s_addr >>= 8;
} while (--n > 0);
/* Convert the string to lowercase */
retStr = (char*)(cp+1);
return(retStr);
}
/* ************************************ */
char* intoa(struct in_addr addr) {
static char buf[sizeof "ff:ff:ff:ff:ff:ff:255.255.255.255"];
return(_intoa(addr, buf, sizeof(buf)));
}
/* ************************************ */
char* addrtostr(HostAddr *addr) {
if (addr == NULL)
return NULL;
switch(addr->hostFamily) {
case AF_INET:
return(char *)(intoa(addr->Ip4Address));
case AF_INET6:
return(char *)(intop(&addr->Ip6Address));
default: return("???");
}
}
/* ************************************ */
char * _addrtostr(HostAddr *addr, char* buf, u_short bufLen) {
if (addr == NULL)
return NULL;
switch(addr->hostFamily) {
case AF_INET:
return (_intoa(addr->Ip4Address,buf,bufLen));
case AF_INET6:
return (_intop(&addr->Ip6Address,buf,bufLen));
default: return("???");
}
}
/* ************************************ */
char * _addrtonum(HostAddr *addr, char* buf, u_short bufLen) {
if((addr == NULL) || (buf == NULL))
return NULL;
switch(addr->hostFamily) {
case AF_INET:
safe_snprintf(__FILE__, __LINE__, buf, bufLen, "%u", addr->Ip4Address.s_addr);
break;
case AF_INET6:
if(_intop(&addr->Ip6Address, buf, bufLen) == NULL)
BufferTooSmall(buf, bufLen);
break;
default:
return("???");
}
return(buf);
}
/* ******************************* */
/* This function automatically updates the instance name */
void ipaddr2str(HostTraffic *el, HostAddr hostIpAddress,
short vlanId, u_int actualDeviceId) {
HostTraffic *h;
if(((hostIpAddress.hostFamily == AF_INET) && (hostIpAddress.addr._hostIp4Address.s_addr == 0))
|| (el->hostResolvedNameType == FLAG_HOST_SYM_ADDR_TYPE_NAME))
return;
h = findHostByNumIP(hostIpAddress, vlanId, actualDeviceId);
if((el != NULL) && (h != NULL)
/* && (el != h) */
&& (h->hostResolvedNameType == FLAG_HOST_SYM_ADDR_TYPE_NAME)
&& (h->hostNumIpAddress[0] != '\0')
&& strcmp(h->hostNumIpAddress, h->hostResolvedName)
&& strcmp(h->hostResolvedName, "0.0.0.0")) {
#ifdef DEBUG
traceEvent(CONST_TRACE_ERROR, "Recycling %s = %s ", addrtostr(&hostIpAddress), el->hostResolvedName);
#endif
strcpy(el->hostResolvedName, h->hostResolvedName), el->hostResolvedNameType = h->hostResolvedNameType;
} else if(getHostNameFromCache(&el->hostIpAddress, el->hostResolvedName, sizeof(el->hostResolvedName)) != NULL) {
el->hostResolvedNameType = FLAG_HOST_SYM_ADDR_TYPE_NAME;
} else
queueAddress(hostIpAddress);
}
/* ************************************ */
char* etheraddr_string(const u_char *ep, char *buf) {
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
ep[0] & 0xFF, ep[1] & 0xFF,
ep[2] & 0xFF, ep[3] & 0xFF,
ep[4] & 0xFF, ep[5] & 0xFF);
return (buf);
}
/* ************************************ */
char* llcsap_string(u_char sap) {
char *cp;
static char buf[sizeof("sap 00")];
cp = buf;
strncpy(cp, "sap ", sizeof(buf));
cp += strlen(cp);
*cp++ = hex[sap >> 4 & 0xf];
*cp++ = hex[sap & 0xf];
*cp++ = '\0';
/* traceEvent(CONST_TRACE_INFO, "%s", buf); */
return(buf);
}
/* ************************************ */
/*
The FDDI code below has been grabbed from
tcpdump
*/
static u_char fddi_bit_swap[] = {
0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
};
void extract_fddi_addrs(struct fddi_header *fddip, char *fsrc, char *fdst)
{
int i;
for (i = 0; i < 6; ++i)
fdst[i] = fddi_bit_swap[fddip->dhost[i]];
for (i = 0; i < 6; ++i)
fsrc[i] = fddi_bit_swap[fddip->shost[i]];
}
/* ************************************ */
static u_int _ns_get16(const u_char *src) {
u_int dst;
NS_GET16(dst, src);
return (dst);
}
/* ************************************ */
int printable(int ch) {
return (ch > 0x20 && ch < 0x7f);
}
/* ************************************ */
static int special(int ch) {
switch (ch) {
case 0x22: /* '"' */
case 0x2E: /* '.' */
case 0x3B: /* ';' */
case 0x5C: /* '\\' */
/* Special modifiers in zone files. */
case 0x40: /* '@' */
case 0x24: /* '$' */
return (1);
default:
return (0);
}
}
/* ************************************ */
static int _ns_name_ntop(const u_char *src,
char *dst, size_t dstsiz) {
const u_char *cp;
char *dn, *eom;
u_char c;
u_int n;
static char digits[] = "0123456789";
cp = src;
dn = dst;
eom = dst + dstsiz;
while ((n = *cp++) != 0) {
if ((n & NS_CMPRSFLGS) != 0) {
/* Some kind of compression pointer. */
errno = EMSGSIZE;
return (-1);
}
if (dn != dst) {
if (dn >= eom) {
errno = EMSGSIZE;
return (-1);
}
*dn++ = '.';
}
if (dn + n >= eom) {
errno = EMSGSIZE;
return (-1);
}
for (; n > 0; n--) {
c = *cp++;
if (special(c)) {
if (dn + 1 >= eom) {
errno = EMSGSIZE;
return (-1);
}
*dn++ = '\\';
*dn++ = (char)c;
} else if (!printable(c)) {
if (dn + 3 >= eom) {
errno = EMSGSIZE;
return (-1);
}
*dn++ = '\\';
*dn++ = digits[c / 100];
*dn++ = digits[(c % 100) / 10];
*dn++ = digits[c % 10];
} else {
if (dn >= eom) {
errno = EMSGSIZE;
return (-1);
}
*dn++ = (char)c;
}
}
}
if (dn == dst) {
if (dn >= eom) {
errno = EMSGSIZE;
return (-1);
}
*dn++ = '.';
}
if (dn >= eom) {
errno = EMSGSIZE;
return (-1);
}
*dn++ = '\0';
return((int)(dn - dst));
}
/* ************************************ */
static char* _res_skip_rr(char *cp, char *eom) {
int tmp;
int dlen;
if ((tmp = _dn_skipname((u_char *)cp, (u_char *)eom)) == -1)
return (NULL); /* compression error */
cp += tmp;
if ((cp + RRFIXEDSZ) > eom)
return (NULL);
cp += INT16SZ; /* type */
cp += INT16SZ; /* class */
cp += INT32SZ; /* ttl */
dlen = _ns_get16((u_char*)cp);
cp += INT16SZ; /* dlen */
cp += dlen;
if (cp > eom)
return (NULL);
return (cp);
}
/* ************************************ */
static int dn_expand_(const u_char *msg, const u_char *eom, const u_char *src,
char *dst, int dstsiz) {
int n = _ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
if (n > 0 && dst[0] == '.')
dst[0] = '\0';
return (n);
}
/* ************************************ */
static int _ns_name_unpack(const u_char *msg,
const u_char *eom, const u_char *src,
u_char *dst, size_t dstsiz) {
const u_char *srcp, *dstlim;
u_char *dstp;
int n, len, checked;
len = -1;
checked = 0;
dstp = dst;
srcp = src;
dstlim = dst + dstsiz;
if (srcp < msg || srcp >= eom) {
errno = EMSGSIZE;
return (-1);
}
/* Fetch next label in domain name. */
while ((n = *srcp++) != 0) {
/* Check for indirection. */
switch (n & NS_CMPRSFLGS) {
case 0:
/* Limit checks. */
if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
errno = EMSGSIZE;
return (-1);
}
checked += n + 1;
*dstp++ = n;
memcpy(dstp, srcp, n);
dstp += n;
srcp += n;
break;
case NS_CMPRSFLGS:
if (srcp >= eom) {
errno = EMSGSIZE;
return (-1);
}
if (len < 0)
len = (int)(srcp - src + 1);
srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
if (srcp < msg || srcp >= eom) { /* Out of range. */
errno = EMSGSIZE;
return (-1);
}
checked += 2;
/*
* Check for loops in the compressed name;
* if we've looked at the whole message,
* there must be a loop.
*/
if (checked >= eom - msg) {
errno = EMSGSIZE;
return (-1);
}
break;
default:
errno = EMSGSIZE;
return (-1); /* flag error */
}
}
*dstp = '\0';
if (len < 0)
len = (int)(srcp - src);
return (len);
}
/* ************************************ */
static int _ns_name_uncompress(const u_char *msg,
const u_char *eom, const u_char *src,
char *dst, size_t dstsiz) {
u_char tmp[NS_MAXCDNAME];
int n;
if ((n = _ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
return (-1);
if (_ns_name_ntop(tmp, dst, dstsiz) == -1)
return (-1);
return (n);
}
/* ************************************ */
static int _ns_name_skip(const u_char **ptrptr, const u_char *eom) {
const u_char *cp;
u_int n;
cp = *ptrptr;
while (cp < eom && (n = *cp++) != 0) {
/* Check for indirection. */
switch (n & NS_CMPRSFLGS) {
case 0: /* normal case, n == len */
cp += n;
continue;
case NS_CMPRSFLGS: /* indirection */
cp++;
break;
default: /* illegal type */
errno = EMSGSIZE;
return (-1);
}
break;
}
if (cp > eom) {
errno = EMSGSIZE;
return (-1);
}
*ptrptr = cp;
return (0);
}
/* ************************************ */
static int _dn_skipname(const u_char *ptr, const u_char *eom) {
const u_char *saveptr = ptr;
if (_ns_name_skip(&ptr, eom) == -1)
return (-1);
return((int)(ptr - saveptr));
}
/* ************************************ */
static void msdns_filter_name(char *msg) {
int i, j, max = (int)strlen(msg);
for(i=0, j=0; i<max; i++) {
#ifdef MDNS_DEBUG
if(0) traceEvent(CONST_TRACE_INFO, "DNS_DEBUG: [i=%d][%c][%d]", i, msg[i], msg[i]);
#endif
if(msg[i] != '\\') {
if(msg[i] > 0) /* FIX: can we do better? */
msg[j++] = msg[i];
} else {
char tmpStr[8], tmpStr2[8];
int id;
if((msg[i+1] >= '0') && (msg[i+1] <= '9')) {
#ifdef MDNS_DEBUG
if(0){
traceEvent(CONST_TRACE_INFO, "DNS_DEBUG: [i=%d][%c][%d]", i+1, msg[i+1], msg[i+1]);
traceEvent(CONST_TRACE_INFO, "DNS_DEBUG: [i=%d][%c][%d]", i+2, msg[i+2], msg[i+2]);
traceEvent(CONST_TRACE_INFO, "DNS_DEBUG: [i=%d][%c][%d]", i+3, msg[i+3], msg[i+3]);
}
#endif
tmpStr[0] = msg[i+1];
tmpStr[1] = msg[i+2];
tmpStr[2] = msg[i+3];
tmpStr[3] = '\0';
id = atoi(tmpStr);
if(id == 128)
msg[j++] = '\'';
else if(id < 128) {
safe_snprintf(__FILE__, __LINE__, tmpStr2, sizeof(tmpStr2), "%c", id);
msg[j++] = tmpStr2[0];
}
i += 3;
} else {
i++;
msg[j++] = msg[i];
}
}
}
msg[j] = '\0';
}
/* ************************************ */
static char* _res_skip(char *msg,
int numFieldsToSkip,
char *eom) {
char *cp;
HEADER *hp;
int tmp;
int n;
/*
* Skip the header fields.
*/
hp = (HEADER *)msg;
cp = msg + HFIXEDSZ;
/*
* skip question records.
*/
n = (int)ntohs((unsigned short int)hp->qdcount);
if (n > 0) {
while (--n >= 0 && cp < eom) {
tmp = _dn_skipname((u_char *)cp, (u_char *)eom);
if (tmp == -1) return(NULL);
cp += tmp;
cp += INT16SZ; /* type */
cp += INT16SZ; /* class */
}
}
if (--numFieldsToSkip <= 0) return(cp);
/*
* skip myGlobals.authoritative answer records
*/
n = (int)ntohs((unsigned short int)hp->ancount);
if (n > 0) {
while (--n >= 0 && cp < eom) {
cp = _res_skip_rr(cp, eom);
if (cp == NULL) return(NULL);
}
}
if (--numFieldsToSkip == 0) return(cp);
/*
* skip name server records
*/
n = (int)ntohs((unsigned short int)hp->nscount);
if (n > 0) {
while (--n >= 0 && cp < eom) {
cp = _res_skip_rr(cp, eom);
if (cp == NULL) return(NULL);
}
}
if (--numFieldsToSkip == 0) return(cp);
/*
* skip additional records
*/
n = (int)ntohs((unsigned short int)hp->arcount);
if (n > 0) {
while (--n >= 0 && cp < eom) {
cp = _res_skip_rr(cp, eom);
if (cp == NULL) return(NULL);
}
}
return(cp);
}
/* ************************************ */
void setHostName(HostTraffic *srcHost, char *name) {
u_short tmpStrLen = min(strlen(name), MAX_LEN_SYM_HOST_NAME);
strncpy(srcHost->hostResolvedName, name, tmpStrLen);
srcHost->hostResolvedName[tmpStrLen] = '\0';
}
/* ************************************ */
//#define MDNS_DEBUG
static void handleMdnsName(HostTraffic *srcHost, u_short sport, u_char *mdns_name) {
char *mdnsStrtokState, *name = NULL, *appl = NULL, *proto = NULL, *domain = NULL;
char *tmpStr = strdup((char*)mdns_name);
if(tmpStr != NULL) {
msdns_filter_name(tmpStr);
/* S's Music._daap._tcp.localcal */
#ifdef MDNS_DEBUG
traceEvent(CONST_TRACE_INFO, "DNS_DEBUG: (1) [%s]", tmpStr);
#endif
name = strtok_r(tmpStr, "._", &mdnsStrtokState);
if(name) {
appl = strtok_r(NULL, "._", &mdnsStrtokState);
if(appl) {
proto = strtok_r(NULL, "._", &mdnsStrtokState);
if(proto) {
domain = strtok_r(NULL, "._", &mdnsStrtokState);
}
}
}
if((domain != NULL)
&& ((!strcmp(domain, "local"))
|| (!strcmp(domain, "localafpovertcp"))
)) {
#ifdef MDNS_DEBUG
traceEvent(CONST_TRACE_INFO, "DNS_DEBUG: (2) [%s] [%s][%s][%s][%s]",
srcHost->hostNumIpAddress,
name, appl, proto, domain);
#endif
if((!strcmp(appl, "ipp")) || (!strcmp(appl, "printer"))) {