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 pathutil.c
6313 lines (5133 loc) · 175 KB
/
util.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
/*
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
*
* http://www.ntop.org
*
* Copyright (C) 1998-2012 Luca Deri <[email protected]>
*
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
*
* 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"
#include <stdarg.h>
#ifndef WIN32
#include <syslog.h>
#endif
#ifndef HAVE_GETOPT_LONG
#include "./utils/getopt/getopt.c"
#include "./utils/getopt/getopt1.c"
#endif
/* #define ADDRESS_DEBUG */
/* #define FINGERPRINT_DEBUG */
/* FTP */
static SessionInfo *passiveSessions = NULL;
static u_short passiveSessionsLen;
/* VoIP */
static SessionInfo *voipSessions = NULL;
static u_short voipSessionsLen;
static char *versionSite[] = {
CONST_VERSIONCHECK_SITE,
NULL
};
/* ************************************ */
static HostTraffic* __getFirstHost(u_int actualDeviceId, u_int beginIdx, char *file, int line) {
u_int idx;
accessMutex(&myGlobals.hostsHashLockMutex, "__getFirstHost");
for(idx=beginIdx; idx<myGlobals.device[actualDeviceId].hosts.actualHashSize; idx++) {
HostTraffic *el = myGlobals.device[actualDeviceId].hosts.hash_hostTraffic[idx];
while(el != NULL) {
if(broadcastHost(el) || (el == myGlobals.otherHostEntry)) {
el = el->next;
} else {
if(el->magic != CONST_MAGIC_NUMBER) {
traceEvent(CONST_TRACE_ERROR,
"Bad magic number [expected=%d/real=%d][deviceId=%d] getFirstHost()[%s/%d]",
CONST_MAGIC_NUMBER, el->magic, actualDeviceId, file, line);
releaseMutex(&myGlobals.hostsHashLockMutex);
return(NULL);
}
if(!is_host_ready_to_purge(actualDeviceId, el, time(NULL))) {
/* Do not return hosts that will soon be purged off memory */
releaseMutex(&myGlobals.hostsHashLockMutex);
return(el);
} else {
el = el->next;
}
}
}
}
releaseMutex(&myGlobals.hostsHashLockMutex);
return(NULL);
}
/* ************************************ */
HostTraffic* _getFirstHost(u_int actualDeviceId, char *file, int line) {
return(__getFirstHost(actualDeviceId, 0 /* FIRST_HOSTS_ENTRY */, file, line));
}
/* ************************************ */
HostTraffic* _getNextHost(u_int actualDeviceId, HostTraffic *host, char *file, int line) {
u_int nextIdx;
time_t now = time(NULL);
accessMutex(&myGlobals.hostsHashLockMutex, "getNextHost");
if((host == NULL) || (host->magic != CONST_MAGIC_NUMBER)) {
releaseMutex(&myGlobals.hostsHashLockMutex);
return(NULL);
}
nextIdx = host->hostTrafficBucket+1;
while(host->next != NULL) {
if(host->next->magic != CONST_MAGIC_NUMBER) {
traceEvent(CONST_TRACE_ERROR, "Bad magic number (expected=%d/real=%d) getNextHost()[%s/%d]",
CONST_MAGIC_NUMBER, host->next->magic, file, line);
releaseMutex(&myGlobals.hostsHashLockMutex);
return(NULL);
}
if(!is_host_ready_to_purge(actualDeviceId, host->next, now)) {
releaseMutex(&myGlobals.hostsHashLockMutex);
return(host->next);
} else
host = host->next;
}
/* No host has been found: move to next bucket */
releaseMutex(&myGlobals.hostsHashLockMutex);
if(nextIdx < myGlobals.device[actualDeviceId].hosts.actualHashSize)
return(__getFirstHost(actualDeviceId, nextIdx, file, line));
else
return(NULL);
}
/* ************************************ */
HostTraffic* findHostByNumIP(HostAddr hostIpAddress, short vlanId, u_int actualDeviceId) {
HostTraffic *el;
u_int idx = hashHost(&hostIpAddress, NULL, &el, actualDeviceId);
if(el != NULL)
return(el); /* Found */
else if(idx == FLAG_NO_PEER)
return(NULL);
else
el = myGlobals.device[actualDeviceId].hosts.hash_hostTraffic[idx];
for(; el != NULL; el = el->next) {
if((el->hostNumIpAddress != NULL) && (addrcmp(&el->hostIpAddress,&hostIpAddress) == 0)) {
if((vlanId > 0) && (el->vlanId != vlanId)) continue;
return(el);
}
}
if(el == NULL) {
/*
Fallback:
probably a local host has been searched using an IP
address (we should have used a MAC)
*/
for(idx=0; idx<myGlobals.device[actualDeviceId].hosts.actualHashSize; idx++) {
el = myGlobals.device[actualDeviceId].hosts.hash_hostTraffic[idx];
for(; el != NULL; el = el->next) {
if((el->hostNumIpAddress != NULL) && (addrcmp(&el->hostIpAddress,&hostIpAddress) == 0)) {
if((vlanId > 0) && (el->vlanId != vlanId)) continue;
return(el);
}
}
}
}
return(NULL);
}
/* ************************************ */
char* serial2str(HostSerial theSerial, char *buf, int buf_len) {
buf[0] = '\0';
if(buf_len >= 2*sizeof(HostSerial)) {
int i;
char tmpStr[16];
char *ptr = (char*)&theSerial;
for(i=0; i<sizeof(HostSerial); i++) {
snprintf(tmpStr, sizeof(tmpStr), "%02X", ptr[i] & 0xFF);
strcat(buf, tmpStr);
}
}
return(buf);
}
/* ************************************ */
void str2serial(HostSerial *theSerial, char *buf, int buf_len) {
if(buf_len >= 2*sizeof(HostSerial)) {
int i, j;
char tmpStr[16];
u_char *ptr = (u_char*)theSerial;
for(i=0, j=0; j<sizeof(HostSerial); j++) {
u_int c;
tmpStr[0] = buf[i++];
tmpStr[1] = buf[i++];
tmpStr[2] = '\0';
sscanf(tmpStr, "%02X", &c);
ptr[j] = c & 0xFF;
/*
ptr[j] = ((u_int8_t)buf[i]) * 16 + ((u_int8_t)buf[i+1]);
i += 2;
*/
}
}
}
/* ************************************ */
HostTraffic* findHostBySerial(HostSerialIndex serialHostIndex, u_int actualDeviceId) {
HostSerial theSerial;
if(emptySerial(&serialHostIndex)) return(NULL);
getHostSerialFromId(serialHostIndex, &theSerial);
if(theSerial.serialType == SERIAL_IPV4 || theSerial.serialType == SERIAL_IPV6) {
return(findHostByNumIP(theSerial.value.ipSerial.ipAddress,
theSerial.value.ipSerial.vlanId,
actualDeviceId));
} else {
/* MAC */
return(findHostByMAC((char*)theSerial.value.ethSerial.ethAddress,
theSerial.value.ethSerial.vlanId,
actualDeviceId));
}
}
/* ************************************ */
HostTraffic* findHostByMAC(char* macAddr, short vlanId, u_int actualDeviceId) {
HostTraffic *el;
u_int idx = hashHost(NULL, (u_char*)macAddr, &el, actualDeviceId);
if(el != NULL)
return(el); /* Found */
else if(idx == FLAG_NO_PEER)
return(NULL);
else
el = myGlobals.device[actualDeviceId].hosts.hash_hostTraffic[idx];
for(; el != NULL; el = el->next) {
if(!memcmp((char*)el->ethAddress, macAddr, LEN_ETHERNET_ADDRESS)) {
if((vlanId > 0) && (el->vlanId != vlanId))
continue;
else
return(el);
}
}
return(NULL);
}
/* *****************************************************/
unsigned long in6_hash(struct in6_addr *addr) {
return
(addr->s6_addr[13] ) | (addr->s6_addr[15] << 8) |
(addr->s6_addr[14] << 16) | (addr->s6_addr[11] << 24);
}
/* *************************************** */
unsigned short computeIdx(HostAddr *srcAddr, HostAddr *dstAddr, int sport, int dport) {
unsigned short idx = 0;
if (srcAddr->hostFamily != dstAddr->hostFamily)
return -1;
switch (srcAddr->hostFamily) {
case AF_INET:
/*
* The hash key has to be calculated in a specular
* way: its value has to be the same regardless
* of the flow direction.
*
* Patch on the line below courtesy of
* Paul Chapman <[email protected]>
*/
idx = (u_int)(dstAddr->Ip4Address.s_addr+srcAddr->Ip4Address.s_addr+sport+dport);
break;
case AF_INET6:
idx = (u_int)(dstAddr->Ip6Address.s6_addr[0] +
dstAddr->Ip6Address.s6_addr[0] +
srcAddr->Ip6Address.s6_addr[0] +
srcAddr->Ip6Address.s6_addr[0] + sport +! dport);
break;
}
return idx;
}
/* ******************************************** */
u_int16_t computeTransId(HostAddr *srcAddr, HostAddr *dstAddr, int sport, int dport) {
u_int16_t transactionId = 0;
if (srcAddr->hostFamily != dstAddr->hostFamily)
return -1;
switch (srcAddr->hostFamily) {
case AF_INET:
transactionId = (u_int16_t)(3*srcAddr->Ip4Address.s_addr+
dstAddr->Ip4Address.s_addr+5*dport+7*sport);
break;
case AF_INET6:
transactionId = (u_int16_t)(3*srcAddr->Ip6Address.s6_addr[0]+
dstAddr->Ip6Address.s6_addr[0]+5*dport+7*sport);
break;
}
return transactionId;
}
/* ***************************** */
int in6_isglobal(struct in6_addr *addr) {
return (addr->s6_addr[0] & 0xe0) == 0x20;
}
/* ***************************** */
short addrcmp(HostAddr *addr1, HostAddr *addr2) {
if(addr1 == NULL)
if(addr2 == NULL)
return 0;
else
return 1;
else if(addr2 == NULL)
return -1;
if(addr1->hostFamily == 0)
if(addr2->hostFamily == 0)
return 0;
else
return 1;
else if(addr2->hostFamily == 0)
return -1;
if(addr1->hostFamily != addr2->hostFamily) {
if(addr1->hostFamily > addr2->hostFamily)
return 1;
else
return -1;
}
switch (addr1->hostFamily) {
case AF_INET:
if (addr1->Ip4Address.s_addr > addr2->Ip4Address.s_addr)
return (1);
else if (addr1->Ip4Address.s_addr < addr2->Ip4Address.s_addr)
return (-1);
else
return (0);
/*return (addr1->Ip4Address.s_addr != addr2->Ip4Address.s_addr);*/
case AF_INET6:
if(memcmp(&addr1->Ip6Address,&addr2->Ip6Address,sizeof(struct in6_addr)) > 0)
return (1);
else if (memcmp(&addr1->Ip6Address,&addr2->Ip6Address,sizeof(struct in6_addr)) <0)
return (-1);
else
return (0);
break;
default:
return 1;
}
}
/* ****************************************** */
HostAddr *addrcpy(HostAddr *dst, HostAddr *src) {
dst->hostFamily = src->hostFamily;
switch (src->hostFamily) {
case AF_INET:
memcpy(&dst->Ip4Address, &src->Ip4Address, sizeof(struct in_addr));
return(dst);
case AF_INET6:
memcpy(&dst->Ip6Address, &src->Ip6Address, sizeof(struct in6_addr));
return(dst);
default:
return NULL;
}
}
/* ****************************************** */
int addrinit(HostAddr *addr) {
addr->hostFamily = AF_INET;
addr->Ip4Address.s_addr = 0;
return(0);
}
/* ****************************************** */
unsigned short addrget(HostAddr *Haddr,void *addr, int *family , int *size) {
struct in_addr v4addr;
*family = Haddr->hostFamily;
switch(Haddr->hostFamily) {
case AF_INET:
v4addr.s_addr = ntohl(Haddr->Ip4Address.s_addr);
memcpy((struct in_addr *)addr,&v4addr,sizeof(struct in_addr));
*size = sizeof(struct in_addr);
break;
case AF_INET6:
memcpy((struct in6_addr *)addr,&Haddr->Ip6Address, sizeof(struct in6_addr));
*size = sizeof(struct in6_addr);
break;
}
return 1;
}
/* ****************************************** */
unsigned short addrput(int family, HostAddr *dst, void *src) {
if (dst == NULL)
return -1;
dst->hostFamily = family;
switch (family) {
case AF_INET:
memcpy(&dst->Ip4Address, (struct in_addr *)src,sizeof(struct in_addr));
break;
case AF_INET6:
memcpy(&dst->Ip6Address, (struct in6_addr *)src, sizeof(struct in6_addr));
break;
}
return(1);
}
/* ****************************************** */
unsigned short addrnull(HostAddr *addr) {
switch(addr->hostFamily) {
case AF_INET:
return (addr->Ip4Address.s_addr == 0x0);
case AF_INET6:
return (addr->Ip6Address.s6_addr[0] == 0x0);
default:
return(1);
}
}
/* ****************************************** */
unsigned short addrfull(HostAddr *addr) {
switch(addr->hostFamily) {
case AF_INET:
return (addr->Ip4Address.s_addr == 0xffffffff);
case AF_INET6:
return(0);
default:
return 0;
}
}
/* ****************************************** */
unsigned short prefixlookup(struct in6_addr *addr, NtopIfaceAddr *addrs, int size) {
int found = 0;
NtopIfaceAddr *it;
for (it = addrs ; it != NULL; it = it->next) {
if (size == 0)
size = it->af.inet6.prefixlen / 8;
#if DEBUG
{
char buf[47], buf1[47];
traceEvent(CONST_TRACE_INFO, "DEBUG: comparing [%s/%s]: %d",
_intop(addr, buf, INET6_ADDRSTRLEN),
_intop(&it->af.inet6.ifAddr, buf1, INET6_ADDRSTRLEN), found);
}
#endif
if (memcmp(&it->af.inet6.ifAddr,addr,size) == 0) {
found = 1;
break;
}
}
return found;
}
/* ****************************************** */
unsigned short addrlookup(struct in6_addr *addr, NtopIfaceAddr *addrs) {
return (prefixlookup(addr,addrs, sizeof(struct in6_addr)));
}
/* ****************************************** */
NtopIfaceAddr *getLocalHostAddressv6(NtopIfaceAddr *addrs, char* device) {
#if 0
struct iface_handler *ih;
struct iface_if *ii;
struct iface_addr *ia;
NtopIfaceAddr *tmp = NULL;
int count, addr_pos;
if(!(ih = iface_new()))
return NULL;
for(ii = iface_getif_first(ih) ; ii ; ii = iface_getif_next(ii))
if(!strcmp(ii->name,device))
if(iface_if_getinfo(ii) & IFACE_INFO_UP) {
/* Allocate memory for IPv6 addresses */
count = iface_if_addrcount(ii, AF_INET6);
if(count == 0) break;
addrs = (NtopIfaceAddr *)calloc(count, sizeof(NtopIfaceAddr));
addr_pos = 0;
for(ia = iface_getaddr_first(ii, AF_INET6) ; ia ;
ia = iface_getaddr_next(ia, AF_INET6)) {
struct iface_addr_inet6 i6;
iface_addr_getinfo(ia, &i6);
if(in6_isglobal(&i6.addr)&& (addr_pos < count)) {
tmp = &addrs[addr_pos];
tmp->family = AF_INET6;
memcpy(&tmp->af.inet6.ifAddr, &i6.addr,sizeof(struct in6_addr));
tmp->af.inet6.prefixlen = ia->af.inet6.prefixlen;
tmp->next = &addrs[addr_pos+1];
addr_pos++;
}
}
}
if(tmp != NULL) tmp->next = NULL;
iface_destroy(ih);
return(addrs);
#else
return(NULL);
#endif
}
/*******************************************/
/*
* Copy arg vector into a new buffer, concatenating arguments with spaces.
*/
char* copy_argv(register char **argv) {
register char **p;
register u_int len = 0;
char *buf;
char *src, *dst;
p = argv;
if(*p == 0)
return 0;
while (*p)
len += (int)(strlen(*p++) + 1);
buf = (char*)malloc(len);
if(buf == NULL) {
traceEvent(CONST_TRACE_FATALERROR, "Insufficient memory for copy_argv");
exit(20); /* Just in case */
}
p = argv;
dst = buf;
while ((src = *p++) != NULL) {
while ((*dst++ = *src++) != '\0')
;
dst[-1] = ' ';
}
dst[-1] = '\0';
return buf;
}
/**************************************/
unsigned short isLinkLocalAddress(struct in6_addr *addr,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
int i;
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
if(addr == NULL)
return 1;
else if(addr->s6_addr == 0x0)
return 0; /* IP-less myGlobals.device (is it trying to boot via DHCP/BOOTP ?) */
else {
for(i=0; i<myGlobals.numDevices; i++)
if(IN6_IS_ADDR_LINKLOCAL(addr)) {
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "DEBUG: %s is a linklocal address", intop(addr));
#endif
return 1;
}
return 0;
}
}
/*******************************************/
unsigned short in6_isMulticastAddress(struct in6_addr *addr,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
if(IN6_IS_ADDR_MULTICAST(addr)) {
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "DEBUG: %s is multicast [%X/%X]",
intop(addr));
#endif
return 1;
} else
return 0;
}
/*******************************************/
#ifdef INET6
unsigned short in6_isLocalAddress(struct in6_addr *addr, u_int deviceId,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
if(deviceId >= myGlobals.numDevices) {
traceEvent(CONST_TRACE_WARNING, "Index %u out of range [0..%u] - address treated as remote",
deviceId, myGlobals.numDevices);
return(0);
}
if(addrlookup(addr,myGlobals.device[deviceId].v6Addrs) == 1) {
#ifdef ADDRESS_DEBUG
traceEvent(CONST_TRACE_INFO, "ADDRESS_DEBUG: %s is local", intop(addr));
#endif
return 1;
}
if(myGlobals.runningPref.trackOnlyLocalHosts)
return(0);
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "DEBUG: %s is %s", intop(addr));
#endif
/* Link Local Addresses are local */
return(isLinkLocalAddress(addr, the_local_network, the_local_network_mask));
}
#endif
/* ******************************************* */
unsigned short in6_isPrivateAddress(struct in6_addr *addr,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
/* IPv6 have private addresses ?*/
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
return(0);
}
/* ********************************* */
unsigned short in_isBroadcastAddress(struct in_addr *addr,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
int i;
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
if(addr == NULL)
return 1;
else if(addr->s_addr == 0x0)
return 0; /* IP-less myGlobals.device (is it trying to boot via DHCP/BOOTP ?) */
else {
for(i=0; i<myGlobals.numDevices; i++) {
if(!myGlobals.device[i].virtualDevice) {
if(myGlobals.device[i].netmask.s_addr == 0xFFFFFFFF) /* PPP */
return 0;
if((addr->s_addr | myGlobals.device[i].netmask.s_addr) == addr->s_addr) {
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "DEBUG: %s is a broadcast address", intoa(*addr));
#endif
return 1;
}
if((addr->s_addr & ~myGlobals.device[i].netmask.s_addr) == ~myGlobals.device[i].netmask.s_addr) {
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "DEBUG: %s is a network address", intoa(*addr));
#endif
return 1;
}
}
}
return(in_isPseudoBroadcastAddress(addr, the_local_network, the_local_network_mask));
}
}
/* ********************************* */
unsigned short in_isMulticastAddress(struct in_addr *addr,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
if((addr->s_addr & CONST_MULTICAST_MASK) == CONST_MULTICAST_MASK) {
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "DEBUG: %s is multicast [%X/%X]",
intoa(*addr),
((unsigned long)(addr->s_addr) & CONST_MULTICAST_MASK),
CONST_MULTICAST_MASK
);
#endif
return 1;
} else
return 0;
}
/* ********************************* */
u_int8_t num_network_bits(u_int32_t addr) {
u_int8_t i, j, bits = 0, fields[4];
memcpy(fields, &addr, 4);
for(i = 8; i <= 8; i--)
for(j=0; j<4; j++)
if ((fields[j] & (1 << i)) != 0) bits++;
return(bits);
}
/* ********************************* */
unsigned short in_isLocalAddress(struct in_addr *addr, u_int deviceId,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
if(deviceId >= myGlobals.numDevices) {
traceEvent(CONST_TRACE_WARNING, "Index %u out of range [0..%u] - address treated as remote",
deviceId, myGlobals.numDevices);
return(0);
}
#ifdef ADDRESS_DEBUG
traceEvent(CONST_TRACE_INFO, "Address: %s", intoa(*addr));
traceEvent(CONST_TRACE_INFO, "Network: %s", intoa(myGlobals.device[deviceId].network));
traceEvent(CONST_TRACE_INFO, "NetMask: %s", intoa(myGlobals.device[deviceId].netmask));
#endif
if(addr == NULL) return(0);
if(!myGlobals.runningPref.mergeInterfaces) {
if((addr->s_addr & myGlobals.device[deviceId].netmask.s_addr) == myGlobals.device[deviceId].network.s_addr) {
#ifdef ADDRESS_DEBUG
traceEvent(CONST_TRACE_INFO, "ADDRESS_DEBUG: %s is local", intoa(*addr));
#endif
if(the_local_network && the_local_network_mask)
(*the_local_network) = myGlobals.device[deviceId].network.s_addr,
(*the_local_network_mask) = num_network_bits(myGlobals.device[deviceId].netmask.s_addr);
return 1;
}
} else {
int i;
for(i=0; i<myGlobals.numDevices; i++)
if((addr->s_addr & myGlobals.device[i].netmask.s_addr) == myGlobals.device[i].network.s_addr) {
#ifdef ADDRESS_DEBUG
traceEvent(CONST_TRACE_INFO, "ADDRESS_DEBUG: %s is local", intoa(*addr));
#endif
if(the_local_network && the_local_network_mask)
(*the_local_network) = myGlobals.device[i].network.s_addr,
(*the_local_network_mask) = num_network_bits(myGlobals.device[deviceId].netmask.s_addr);
return 1;
}
}
if(myGlobals.runningPref.trackOnlyLocalHosts)
return(0);
/* Broadcast is considered a local address */
return(in_isBroadcastAddress(addr, the_local_network, the_local_network_mask));
}
/* ********************************* */
unsigned short in_isPrivateAddress(struct in_addr *addr,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
/* See http://www.isi.edu/in-notes/rfc1918.txt */
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
/* Fixes below courtesy of Wies-Software <[email protected]> */
if( ((addr->s_addr & 0xFF000000) == 0x0A000000) /* 10.0.0.0/8 */
|| ((addr->s_addr & 0xFFF00000) == 0xAC100000) /* 172.16/12 */
|| ((addr->s_addr & 0xFFFF0000) == 0xC0A80000) /* 192.168/16 */
|| ((addr->s_addr & 0xFF000000) == 0x7F000000) /* 127.0.0.0/8 */
)
return(1);
else
return(0);
}
/***************************************/
unsigned short isBroadcastAddress(HostAddr *addr,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
switch(addr->hostFamily) {
case AF_INET:
return (in_isBroadcastAddress(&addr->Ip4Address, the_local_network, the_local_network_mask));
case AF_INET6:
return (isLinkLocalAddress(&addr->Ip6Address, NULL, NULL));
default: return(0);
}
}
/* ******************************************** */
unsigned short isMulticastAddress(HostAddr *addr,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
switch(addr->hostFamily) {
case AF_INET:
return (in_isMulticastAddress(&addr->Ip4Address, the_local_network, the_local_network_mask));
case AF_INET6:
return (in6_isMulticastAddress(&addr->Ip6Address, NULL, NULL));
default: return(0);
}
}
/* ************************************************* */
unsigned short isLocalAddress(HostAddr *addr, u_int deviceId,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
switch(addr->hostFamily) {
case AF_INET:
return (in_isLocalAddress(&addr->Ip4Address, deviceId, the_local_network, the_local_network_mask));
#ifdef INET6
case AF_INET6:
return (in6_isLocalAddress(&addr->Ip6Address, deviceId, NULL, NULL));
#endif
default: return(0);
}
}
/* ************************************************** */
unsigned short isPrivateAddress(HostAddr *addr,
u_int32_t *the_local_network,
u_int32_t *the_local_network_mask) {
if(the_local_network && the_local_network_mask)
(*the_local_network) = 0, (*the_local_network_mask) = 0;
switch(addr->hostFamily) {
case AF_INET:
return (in_isPrivateAddress(&addr->Ip4Address, the_local_network, the_local_network_mask));
case AF_INET6:
return (in6_isPrivateAddress(&addr->Ip6Address, NULL, NULL));
default: return(0);
}
}
/* ************************************************ */
int dotted2bits(char *mask) {
int fields[4], fields_num;
fields_num = sscanf(mask, "%d.%d.%d.%d",
&fields[0], &fields[1], &fields[2], &fields[3]);
if(fields_num != 4) {
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "DEBUG: dotted2bits (%s) = %d", mask, fields[0]);
#endif
return(atoi(mask));
} else
return(num_network_bits(((fields[0] & 0xff) << 24)
+ ((fields[1] & 0xff) << 16)
+ ((fields[2] & 0xff) << 8)
+ (fields[3] & 0xff)));
}
/* ********************************* */
/* Example: "131.114.0.0/16,193.43.104.0/255.255.255.0" */
void handleAddressLists(char* addresses, NetworkStats theNetworks[MAX_NUM_NETWORKS],
u_short *numNetworks, char *localAddresses,
int localAddressesLen, int flagWhat) {
char *strtokState, *address;
int laBufferPosition = 0, laBufferUsed = 0, i;
if((addresses == NULL) || (addresses[0] == '\0'))
return;
if(0) traceEvent(CONST_TRACE_NOISY,
"Processing %s parameter '%s'",
flagWhat == CONST_HANDLEADDRESSLISTS_MAIN ? "-m | --local-subnets" :
flagWhat == CONST_HANDLEADDRESSLISTS_RRD ? "RRD" :
flagWhat == CONST_HANDLEADDRESSLISTS_NETFLOW ? "Netflow white/black list" :
flagWhat == CONST_HANDLEADDRESSLISTS_COMMUNITIES ? "community" : "unknown",
addresses);
memset(localAddresses, 0, localAddressesLen);
address = strtok_r(addresses, ",", &strtokState);
while(address != NULL) {
u_int32_t network, networkMask, broadcast;
int bits, a, b, c, d;
char *mask = strchr(address, '/');
char *equal = strchr(address, '=');
if(equal != NULL) {
char key[64];
equal[0] = '\0';
safe_snprintf(__FILE__, __LINE__, key, sizeof(key), "subnet.name.%s", address);
storePrefsValue(key, &equal[1]);
}
if(mask == NULL) {
bits = 32;
} else {
mask[0] = '\0';
mask++;
bits = dotted2bits(mask);
}
if(sscanf(address, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) {
traceEvent(CONST_TRACE_WARNING, "Bad format '%s' - ignoring entry",
address);
address = strtok_r(NULL, ",", &strtokState);
continue;
}
if(bits == CONST_INVALIDNETMASK) {
/* malformed netmask specification */
traceEvent(CONST_TRACE_WARNING, "Net mask '%s' not valid - ignoring entry",
mask);
address = strtok_r(NULL, ",", &strtokState);
continue;
}
network = ((a & 0xff) << 24) + ((b & 0xff) << 16) + ((c & 0xff) << 8) + (d & 0xff);
/* Special case the /32 mask - yeah, we could probably do it with some fancy
u long long stuff, but this is simpler...
Burton Strauss <[email protected]> Jun2002
*/
if (bits == 32) {
networkMask = 0xffffffff;
} else {
networkMask = 0xffffffff >> bits;
networkMask = ~networkMask;
}
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "DEBUG: Nw=%08X - Mask: %08X [%08X]",
network, networkMask, (network & networkMask));
#endif
if((networkMask >= 0xFFFFFF00) /* Courtesy of Roy-Magne Mo <[email protected]> */
&& ((network & networkMask) != network)) {
/* malformed network specification */
traceEvent(CONST_TRACE_WARNING, "%d.%d.%d.%d/%d is not a valid network - correcting mask",