-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpds-scanner.cpp
904 lines (716 loc) · 26.9 KB
/
pds-scanner.cpp
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
#include "pds-scanner.h"
/**
* Application entry point
*/
int main(int argc, char **argv)
{
// Process parameters
processArguments(argc, argv);
// Print something nice
std::cout << std::endl << "========= ========= ========= SCAN STARTED ========= ========= =========" << std::endl;
// Extract required information from provided interface and store it to global variable/proper structure
extractAddressesForInterface();
// Prevent violent termination
signal(SIGINT, &preventViolentTermination);
// Initiate ARP scanning (if applicable) - IPv4
discoverDevicesARP();
// Initiate NDP scanning (if applicable) - IPv6
discoverDevicesNDP();
// Show discovered devices to user
debug_showDiscoveredDevices();
generateOutputFile();
return EXIT_SUCCESS;
}
/**
* Processes parameters supplied from commandline (using getopts() library)
*/
void processArguments(int argc, char** argv)
{
if (argc != ARGUMENT_NUMBER)
{
std::cerr << "Incorrect arguments supplied. Try ./pds-scanner -i interfaceName -f outputFileName.xml" << std::endl;
exit(2);
}
int ch;
while ((ch = getopt(argc, argv, "i:f:")) != -1)
{
switch (ch)
{
case 'i':
P.interfaceName = optarg;
break;
case 'f':
P.outputFileName = optarg;
break;
}
}
}
/**
* This coding masterpiece very clumsily computes neighbor solicited-node multicast address
* Even reading the previous line hurts me, let alone reading the code again. It pretty much
* does everything that could be done via some basic bitwise shift operations, but lets face
* it, I was unable to make it work the right way. Apologies on my side.
*
* Computation algorithm ripped off: https://en.wikipedia.org/wiki/Solicited-node_multicast_address
* Input: in6_addr of target node (rest from address structure)
* Ouput: in6_addr of neighbor solicited-node multicast address
*/
in6_addr computeNSMCNodeAddress(in6_addr targetAddr)
{
if (DEBUG)
{
std::cout << "======IP=====" << convertIPv6ToString(targetAddr) << std::endl;
}
// Prepare last 3 bytes of target address
uint8_t targetLast24b[3];
memcpy(targetLast24b, &targetAddr.s6_addr[13], 3);
// Prepare first 24 bytes of prefix address
in6_addr prefixSNMAddress;
convertStringToIPv6(IPV6_NS_PREFIX_OR_SOMETHING, &prefixSNMAddress);
uint8_t prefixFirst104b[13];
memcpy(prefixFirst104b, &prefixSNMAddress, 13);
if (DEBUG)
{
std::cout << std::endl << "===DEBUG===" << std::endl;
std::cout << std::hex << (unsigned short) targetLast24b[0] << "." << std::hex << (unsigned short) targetLast24b[1] << "." << std::hex << (unsigned short) targetLast24b[2] << std::endl;
std::cout << (unsigned short) prefixFirst104b[0] << "." << (unsigned short) prefixFirst104b[1] << "." << (unsigned short) prefixFirst104b[2] << "." << (unsigned short) prefixFirst104b[3] << std::endl;
std::cout << "===DEBUG===" << std::endl;
}
// Merge prepared values together
// BEHOLD MY MASTERPIECE!
unsigned char tmp_s6_addr[16];
tmp_s6_addr[0] = prefixFirst104b[0];
tmp_s6_addr[1] = prefixFirst104b[1];
tmp_s6_addr[2] = prefixFirst104b[2];
tmp_s6_addr[3] = prefixFirst104b[3];
tmp_s6_addr[4] = prefixFirst104b[4];
tmp_s6_addr[5] = prefixFirst104b[5];
tmp_s6_addr[6] = prefixFirst104b[6];
tmp_s6_addr[7] = prefixFirst104b[7];
tmp_s6_addr[8] = prefixFirst104b[8];
tmp_s6_addr[9] = prefixFirst104b[9];
tmp_s6_addr[10] = prefixFirst104b[10];
tmp_s6_addr[11] = prefixFirst104b[11];
tmp_s6_addr[12] = prefixFirst104b[12];
tmp_s6_addr[13] = targetLast24b[0];
tmp_s6_addr[14] = targetLast24b[1];
tmp_s6_addr[15] = targetLast24b[2];
in6_addr nodeAddress;
memcpy(&nodeAddress, &tmp_s6_addr, IPV6_LEN);
return nodeAddress;
}
/**
* Scans for devices connected (via NDP)
*/
void discoverDevicesNDP()
{
pid_t workerProc = fork();
if (workerProc != CHILD_PROCESS)
{
// Now, parent is receiving packets
ICMPv6Echo ping6PacketReply;
int ndpResponseSocket = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
if(ndpResponseSocket < 0)
{
std::cerr << "Unable to create NDP response socket. Better luck next time!" << std::endl;
exit(1);
}
// IPv6 address of all nodes (the one we are going to ping)
in6_addr myAddress;
convertStringToIPv6(addresses.ipv6AddressLocal, &myAddress);
// Prepare socket addr
sockaddr_in6 socketAddress = prepareNDPSocketAddress(myAddress, P.interfaceName);
int bindResult = bind(ndpResponseSocket, (struct sockaddr*) &socketAddress, sizeof(socketAddress));
int errb = errno;
if (bindResult < 0)
{
std::cerr << "Unable to bind socket for ping6 response! : " << errb << strerror(errb) << std::endl;
exit(1);
}
// Receive responses
while (true)
{
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
if (setsockopt(ndpResponseSocket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
{
std::cerr << "Unable to set timeout for ndpResponseSocket." << std::endl;
// But I guess that it is not a big deal
}
socklen_t forcedPointerLen = sizeof(socketAddress);
if (recvfrom(ndpResponseSocket, &ping6PacketReply, sizeof(ping6PacketReply), 0, (struct sockaddr*)&socketAddress, &forcedPointerLen) <= 0)
{
//std::cerr << "0 bytes response, problems when receiving packet, or timeouted." << std::endl;
break;
}
// Again focus only on PING replies
if (ping6PacketReply.type != 129)
{
if (DEBUG)
std::cout << "Received ICMPv6 packet, but was not of type Echo-Reply." << std::endl;
continue;
}
// Don't forget that it also pings back with your IP
if (addresses.ipv6AddressLocal != convertIPv6ToString(socketAddress.sin6_addr))
{
discoveredPingIPv6.push_back(socketAddress.sin6_addr);
std::cout << "IPv6 Device found: " << convertIPv6ToString(socketAddress.sin6_addr) << std::endl;
}
}
waitpid(workerProc, NULL, 0); // Wait for child that was sending ping requests
close(ndpResponseSocket);
// On discovered IPv6 send NS and read NA - parent should probably read (again), child should be sending - so we are not dealing with sharedmemory issues again
pid_t solicitationSender = fork();
int retry = 5;
if (solicitationSender != CHILD_PROCESS)
{
// Socket for receiving adverts
int ndpAdvertisementSocket = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
if (ndpAdvertisementSocket < 0)
{
std::cerr << "Unable to create advertisement receiver socket." << std::endl;
}
in6_addr myIPv6Address;
convertStringToIPv6(addresses.ipv6AddressLocal, &myIPv6Address);
while(true)
{
// ADVERTISEMENT RECEIVAL
NeighborAdvertisementPacket nap;
// Prepare receiving socket address
sockaddr_in6 socketAddressRecv = prepareNDPSocketAddress(myIPv6Address, P.interfaceName);
// And right away, receive something
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
setsockopt(ndpAdvertisementSocket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
socklen_t forcedPointerLen = sizeof(socketAddressRecv);
if (recvfrom(ndpAdvertisementSocket, &nap, sizeof(nap), 0, (struct sockaddr*)&socketAddressRecv, &forcedPointerLen) <= 0)
{
std::cerr << "\tTimeout expired... retrying" << std::endl;
if(!retry)
break;
retry--;
continue;
}
if (nap.head.nd_na_hdr.icmp6_type == 136)
{
std::cout << "Received NA reply from: " << convertIPv6ToString(nap.head.nd_na_target) << " with MAC ";
printReadableMACAddress(nap.MAC);
std::cout << std::endl;
// Store discovered device
Devices discovered;
memcpy(&discovered.macAddress, nap.MAC, MAC_ADDR_LEN);
discovered.ipv6AddressLL = convertIPv6ToString(nap.head.nd_na_target);
addDeviceRecord(discovered);
// Reset retry counter and inform user
std::cout << "Resetting retry counter, 5 retries remaining..." << std::endl;
retry = 5;
}
}
waitpid(solicitationSender, NULL, 0); // Wait for kid to do the sending
}
else
{
signal(SIGINT, &preventViolentTerminationChildProcess);
// For every discovered IPv6 send neighbor solicitation packet
for (std::vector<in6_addr>::iterator it = discoveredPingIPv6.begin(); it != discoveredPingIPv6.end(); it++)
{
// Socket for sending solicitation requests
int ndpSolicitationSocket = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
if(ndpSolicitationSocket < 0)
{
std::cerr << "Unable to create solicitation socket." << std::endl;
}
int sockOptMaxHosts = 255;
setsockopt(ndpSolicitationSocket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &sockOptMaxHosts, sizeof(sockOptMaxHosts));
// SOLICIT SEND-OUTS
NeighborSolicitationPacket nsp;
in6_addr targetAddr = *it;
// For each addres compute its multicast solicit node address
in6_addr NSMCNodeAddr = computeNSMCNodeAddress(*it);
// Socket address changes with every NS message to be send, so we have to change socket address appropriately in every iteration
// Prepare socket address
sockaddr_in6 socketAddress;
socketAddress.sin6_family = AF_INET6;
memcpy(&socketAddress.sin6_addr, &NSMCNodeAddr, IPV6_LEN);
socketAddress.sin6_flowinfo = 0;
socketAddress.sin6_port = 0;
socketAddress.sin6_scope_id = 0;
// Prepare packet
nsp.head.nd_ns_hdr.icmp6_type = 135; //ND_NEIGHBOR_SOLICIT
nsp.head.nd_ns_hdr.icmp6_code = 0;
nsp.head.nd_ns_hdr.icmp6_cksum = htons ( 0 );
nsp.head.nd_ns_reserved = htonl ( 0 );
memcpy(&nsp.head.nd_ns_target, &targetAddr, IPV6_LEN);
nsp.type = NDP_ETHERNET_HWTYPE ; // Not sure why, but with htons() it did not work
nsp.length = 1; // Not sure why, but with htons() it did not work
memcpy(&nsp.MAC, &addresses.macAddressLocal, MAC_ADDR_LEN);
// Send
sendto(ndpSolicitationSocket, &nsp, sizeof (nsp), 0, (sockaddr*)&socketAddress, sizeof(socketAddress));
int errbno = errno;
if (!errbno)
{
std::cerr << "Failed to send neighbor solicitation" << errbno << std::endl;
}
if (DEBUG)
std::cout << "=== Right about now, solicitation packet for " << convertIPv6ToString(*it) << std::endl;
close(ndpSolicitationSocket);
}
exit(0); // Kill the kid
}
}
else
{
// Child proces will now send discovery packets (pings)
// Prepare IPv6 socket
int ndpDiscoverySocket = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); // Whole ping6 packet construction is heavily inspired by original ping6 implementation
if (ndpDiscoverySocket < 0)
{
std::cerr << "Unable to open socket for NDP station discovery!" << std::endl;
exit(1);
}
// IPv6 address of all nodes (the one we are going to ping)
in6_addr allNodes;
convertStringToIPv6(IPV6_ALLNODES, &allNodes);
// Prepare IPv6 socket address
sockaddr_in6 socketAddress = prepareNDPSocketAddress(allNodes, P.interfaceName);
if (bind(ndpDiscoverySocket, (struct sockaddr*) &socketAddress, sizeof(socketAddress)) < 0)
{
std::cerr << "Unable to bind socket for NDP station discovery!" << std::endl;
exit(1);
}
// Create ICMPv6 ping packet
ICMPv6Echo ping6Packet;
ping6Packet.type = 128; // TODO: Introduce proper constant for this
ping6Packet.code = 0;
ping6Packet.identifier = htons ( 0 ); // As indirectly recommended by RFC ;)
ping6Packet.sequence_number = htons ( 0 ); // As indirectly recommended by RFC ;)
std::cout << "Discovering connected IPv6 devices..." << std::endl;
sendto(ndpDiscoverySocket, &ping6Packet, sizeof(ping6Packet), 0, (struct sockaddr*) &socketAddress, sizeof(socketAddress));
close(ndpDiscoverySocket);
if (DEBUG)
{
char str[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &allNodes, str, INET6_ADDRSTRLEN);
}
exit(EXIT_SUCCESS);
}
}
/**
* Extracts MAC and IPv4||IPv6 (if applicable) from specified interface
* This function works with Parameters structure
*/
void extractAddressesForInterface()
{
struct ifaddrs* addressesStruct = NULL;
struct ifaddrs* address = NULL;
uint8_t localMAC[MAC_ADDR_LEN];
void* tmpAddrPtr = NULL;
if (getifaddrs(&addressesStruct) != 0)
{
// Error
}
for (address = addressesStruct; address != NULL; address = address->ifa_next)
{
if (!address->ifa_addr)
continue;
// We are not interested in addresses belonging to other interfaces
if (P.interfaceName.compare(address->ifa_name))
continue;
// TODO: Consider the possibility of interface having multiple IP addresses
// IPv4 interface address
if (address->ifa_addr->sa_family == AF_INET)
{
char strBuffer[INET_ADDRSTRLEN];
// Only address & netmask
addresses.netmask = (struct sockaddr_in *) address->ifa_netmask;
addresses.ipv4 = (struct sockaddr_in *) address->ifa_addr;
tmpAddrPtr = &((struct sockaddr_in *) address->ifa_addr)->sin_addr;
inet_ntop(AF_INET, tmpAddrPtr, strBuffer, INET_ADDRSTRLEN);
addresses.ipv4AddressLocal = strBuffer;
if (DEBUG)
printf("Saved interface IPv4 address %s\n", addresses.ipv4AddressLocal.c_str());
}
else if (address->ifa_addr->sa_family == AF_INET6)
{
char strBuffer[INET6_ADDRSTRLEN];
tmpAddrPtr = &((struct sockaddr_in6 *) address->ifa_addr)->sin6_addr;
inet_ntop(AF_INET6, tmpAddrPtr, strBuffer, INET6_ADDRSTRLEN);
addresses.ipv6AddressLocal = strBuffer;
memcpy(&addresses.ipv6AddressLocalRaw, (struct in6_addr*) address->ifa_addr, IPV6_LEN);
if (DEBUG)
printf("Saved interface IPv6 address %s\n", addresses.ipv6AddressLocal.c_str());
}
// Also, retrieve MAC address for given interface
else if (address->ifa_data != 0)
{
// Prepare socket for mac-address retrieval
int32_t sd = socket(PF_INET, SOCK_DGRAM, 0);
if (sd < 0)
{
freeifaddrs(addressesStruct);
return;
}
struct ifreq req;
strcpy(req.ifr_name, address->ifa_name);
if (ioctl(sd, 0x8927, &req) != 1)
{
uint8_t* mac = (uint8_t*) req.ifr_ifru.ifru_hwaddr.sa_data;
memcpy(&addresses.macAddressLocal, mac, MAC_ADDR_LEN);
}
}
}
freeifaddrs(addressesStruct);
}
/**
* Executes scanning of network segment and looks for active stations (via ARP)
*/
void discoverDevicesARP()
{
std::cout << "Discovering connected IPv4 devices." << std::endl;
// Calculate important network addresses
uint32_t subnet = ntohl(inet_addr(inet_ntoa(addresses.netmask->sin_addr)));
uint32_t ip = ntohl(inet_addr(inet_ntoa(addresses.ipv4->sin_addr)));
uint32_t networkAddress = (subnet & ip);
uint32_t broadCastAddress = (networkAddress | (~subnet));
ARPPacket arpDiscoveryPacket;
pid_t workerProc = fork();
if (workerProc != CHILD_PROCESS)
{
// Parent will receive packets that child process requested
int arpResponseSocket = socket (AF_PACKET, SOCK_DGRAM, htons(ETH_P_ARP));
if (arpResponseSocket < 0) { std::cerr << "Unable to open socket for ARP response receiving!" << std::endl; exit(1); }
// Prepare local sockaddr_ll address
struct sockaddr_ll socketAddress;
prepareARPSocketAddress(&socketAddress, addresses.macAddressLocal);
if (bind(arpResponseSocket, (sockaddr*) &socketAddress, sizeof(socketAddress)) < 0)
{
std::cerr << "Unable to bind socket for receiving ARP packets." << std::endl;
exit(1);
}
// Take as long as it comes
while(true)
{
// Prepare 2s timeout for recvfrom()
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
if (setsockopt(arpResponseSocket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
{
std::cerr << "Unable to set timout for arpResponseSocket." << std::endl;
// But I guess that it is not a big deal
}
if (recvfrom(arpResponseSocket, &arpDiscoveryPacket, sizeof(arpDiscoveryPacket), 0, NULL, NULL) <= 0)
{
std::cerr << "ARP scan finished." << std::endl;
break; // On exceeded timeout break the scan
}
if (arpDiscoveryPacket.operation != htons(ARP_OPERATION_REPLY))
{
std::cerr << "Packet that arrived was not ARP reply from requested station." << std::endl;
continue;
}
// Inform user about discovered device and store it
std::cout << "Discovered: ";
printReadableMACAddress(arpDiscoveryPacket.sender_hw_addr);
std::cout << " on IPv4 address: " << getReadableIPv4Address(arpDiscoveryPacket.sender_proto_addr) << std::endl;
// Store discovered devices
Devices discovered;
memcpy(&discovered.macAddress, arpDiscoveryPacket.sender_hw_addr, MAC_ADDR_LEN);
memcpy(&discovered.ipv4Address, arpDiscoveryPacket.sender_proto_addr, IPV4_LEN);
addDeviceRecord(discovered);
}
waitpid(workerProc, NULL, 0); // Wait for child process to kill itself
}
else
{
signal(SIGINT, &preventViolentTerminationChildProcess);
// Create a socket and use it to send ARP request
int arpDiscoverySocket = socket (AF_PACKET, SOCK_DGRAM, htons(ETH_P_ARP));
if (arpDiscoverySocket < 0)
{
std::cerr << "Unable to open socket for ARP discovery!" << std::endl;
exit(1);
}
// For every IPv4 address within network segment, generate ARP request
for (uint32_t i = (networkAddress+1); i < broadCastAddress; i++)
{
// Send ARP request to the address & accept reply (in other process|thread?)
arpDiscoveryPacket.hw_type = htons ( ARP_ETHERNET_HWTYPE ) ;
arpDiscoveryPacket.proto_type = htons ( ARP_IPV4_PROTOTYPE ) ;
arpDiscoveryPacket.hw_addr_len = MAC_ADDR_LEN;
arpDiscoveryPacket.proto_addr_len = IPV4_LEN;
arpDiscoveryPacket.operation = htons ( ARP_OPERATION_REQUEST );
memcpy(&arpDiscoveryPacket.sender_hw_addr, &addresses.macAddressLocal, MAC_ADDR_LEN);
memcpy(&arpDiscoveryPacket.sender_proto_addr, &addresses.ipv4->sin_addr, IPV4_LEN);
// Set proper byteorder for address
uint32_t tmpBOAddress = htonl ( i );
// Set all target MAC address bytes to 0x00 (broadcast address)
memset(&arpDiscoveryPacket.target_hw_addr, 0x00, MAC_ADDR_LEN);
memcpy(&arpDiscoveryPacket.target_proto_addr, &tmpBOAddress, IPV4_LEN);
struct sockaddr_ll socketAddress;
socketAddress.sll_family = AF_PACKET; // Always AF_PACKET (manual)
socketAddress.sll_protocol = htons ( ETH_P_ARP );
socketAddress.sll_ifindex = if_nametoindex(P.interfaceName.c_str());
socketAddress.sll_hatype = 1; // It's ethennet hw type
socketAddress.sll_pkttype = PACKET_OTHERHOST;
socketAddress.sll_halen = MAC_ADDR_LEN;
memset(&socketAddress.sll_addr, 0xff, MAC_ADDR_LEN);
if (bind(arpDiscoverySocket, (sockaddr*)&socketAddress, sizeof(socketAddress)) < 0)
{
std::cerr << "Unable to bind socket for ARP discovery." << std::endl;
}
if (sendto(arpDiscoverySocket, &arpDiscoveryPacket, sizeof(arpDiscoveryPacket), 0, (sockaddr*)&socketAddress, sizeof(socketAddress)) < 0)
{
std::cerr << "Error when sending out ARP packet." << std::endl;
}
}
exit(EXIT_SUCCESS); // Commit suicide successfully.
}
}
/**
* Helper function that prepares sockaddr_ll basic values for socket
*/
void prepareARPSocketAddress(struct sockaddr_ll* socketAddress, uint8_t* address)
{
socketAddress->sll_family = AF_PACKET;
socketAddress->sll_protocol = htons (ETH_P_ARP);
socketAddress->sll_ifindex = if_nametoindex(P.interfaceName.c_str());
socketAddress->sll_hatype = 1;
socketAddress->sll_pkttype = PACKET_OTHERHOST;
socketAddress->sll_halen = MAC_ADDR_LEN;
memcpy(&socketAddress->sll_addr, &address, MAC_ADDR_LEN);
}
/**
* Prepares socket IPV6 address for NDP socket (returns the structure)
*/
sockaddr_in6 prepareNDPSocketAddress(in6_addr address, std::string interfaceName)
{
sockaddr_in6 socket;
socket.sin6_family = AF_INET6;
socket.sin6_port = 0; // ?
socket.sin6_flowinfo = 0; // ?
socket.sin6_addr = address;
socket.sin6_scope_id = if_nametoindex(interfaceName.c_str());
return socket;
}
/**
* Helper function transforming common IPv4 representation to dotted notation
*/
std::string getReadableIPv4Address(uint8_t* address)
{
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, (in_addr*) address, str, INET_ADDRSTRLEN);
return std::string(str);
}
/**
* Helper function transforming common IPv4 representation to dotted notation
* (different signature)
*/
std::string getReadableIPv4Address(uint32_t address)
{
char str[INET_ADDRSTRLEN];
uint32_t tmp = htonl(address);
inet_ntop(AF_INET, &tmp, str, INET_ADDRSTRLEN);
return std::string(str);
}
/**
* Helper fuinction converting common MAC address representation to something readable
*/
void printReadableMACAddress(uint8_t* MAC)
{
printf("%02X:%02X:%02X:%02X:%02X:%02X", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
}
/**
* Takes string representation of IPv6 address and stores its in6_addr
* representation to allNodes parameter.
*/
void convertStringToIPv6(std::string ipv6, in6_addr* allNodes)
{
inet_pton(AF_INET6, ipv6.c_str(), allNodes);
}
/**
* Helper function for converting IPv6 to something readable
*/
std::string convertIPv6ToString(in6_addr ipv6)
{
char str[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &ipv6, str, INET6_ADDRSTRLEN);
return std::string(str);
}
/**
* Smartly adds device record
* Considers possibility of already located IPv4 device with same MAC address
*/
void addDeviceRecord(Devices discovered)
{
// We have to consider discovered.macAddress to be the key and check whether corresponding record exists
bool recordPresent = false;
int elementIndex = 0;
for(std::vector<Devices>::iterator it = discoveredDevices.begin(); it != discoveredDevices.end(); it++)
{
// Record we are trying to store is already present (merge should be done)
if (it->macAddress[0] == discovered.macAddress[0] &&
it->macAddress[1] == discovered.macAddress[1] &&
it->macAddress[2] == discovered.macAddress[2] &&
it->macAddress[3] == discovered.macAddress[3] &&
it->macAddress[4] == discovered.macAddress[4] &&
it->macAddress[5] == discovered.macAddress[5])
{
recordPresent = true;
break;
}
elementIndex++;
}
if (recordPresent)
{
if (DEBUG)
std::cout << "Record is present and we will try to merge." << std::endl;
// We are only interested in IPv6 records for mergin
if (!discovered.ipv6AddressLL.empty())
{
// We should merge discovered device to existing record
if (discoveredDevices.at(elementIndex).ipv6AddressLL.empty())
{
discoveredDevices.at(elementIndex).ipv6AddressLL = discovered.ipv6AddressLL;
if (DEBUG)
std::cout << "Record was inserted to main slot at position " << elementIndex << std::endl;
}
else
{
// Device record already has IPv6 - use reserve slot
discoveredDevices.at(elementIndex).ipv6AddressSTD = discovered.ipv6AddressLL;
if (DEBUG)
std::cout << "Record was inserted to resever slot at position " << elementIndex << std::endl;
}
}
}
else
{
if (DEBUG)
std::cout << "Record was not present, but was made present." << std::endl;
discoveredDevices.push_back(discovered);
}
}
/**
* Generate output file
* Outputfile name is loaded from processed argument for -f parameter.
*/
bool generateOutputFile()
{
std::string ofHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
std::string ofRootElement = "<devices>\n";
std::string ofRootElementClose = "</devices>\n";
std::string ofContent = ofHeader;
ofContent.append(ofRootElement);
for (std::vector<Devices>::iterator it = discoveredDevices.begin(); it != discoveredDevices.end(); it++)
{
std::string ofHostNode = "\t<host mac=\"";
std::string ofHostNodeClose = "\t</host>\n";
// Prepare HOST & MAC values
std::stringstream ofFormattedMAC;
ofFormattedMAC <<
std::setfill('0') << std::setw(2) << std::hex << (unsigned short) it->macAddress[0]
<< std::setfill('0') << std::setw(2) << std::hex << (unsigned short) it->macAddress[1] << "."
<< std::setfill('0') << std::setw(2) << std::hex << (unsigned short) it->macAddress[2]
<< std::setfill('0') << std::setw(2) << std::hex << (unsigned short) it->macAddress[3] << "."
<< std::setfill('0') << std::setw(2) << std::hex << (unsigned short) it->macAddress[4]
<< std::setfill('0') << std::setw(2) << std::hex << (unsigned short) it->macAddress[5];
// Prepare IPv4, IPv6 values
// if (it->ipv4Address ) // TODO: Check for value not zero
std::string ofIPv4Node = "\t\t<ipv4>";
std::string ofFormattedIPV4 = getReadableIPv4Address(it->ipv4Address);
std::string ofIPv4NodeClose = "</ipv4>\n";
std::string ofIPv6Node1 = "";
std::string ofIPv6Node1Close = "";
if (!it->ipv6AddressLL.empty())
{
ofIPv6Node1.append("\t\t<ipv6>");
ofIPv6Node1.append(it->ipv6AddressLL);
ofIPv6Node1Close.append("</ipv6>\n");
}
std::string ofIPv6Node2 = "";
std::string ofIPv6Node2Close = "";
if (!it->ipv6AddressSTD.empty())
{
ofIPv6Node2.append("\t\t<ipv6>");
ofIPv6Node2.append(it->ipv6AddressSTD);
ofIPv6Node2Close.append("</ipv6>\n");
}
ofContent.append(ofHostNode);
ofContent.append(ofFormattedMAC.str());
ofContent.append("\">\n");
ofContent.append(ofIPv4Node);
ofContent.append(ofFormattedIPV4);
ofContent.append(ofIPv4NodeClose);
ofContent.append(ofIPv6Node1);
ofContent.append(ofIPv6Node1Close);
ofContent.append(ofIPv6Node2);
ofContent.append(ofIPv6Node2Close);
ofContent.append(ofHostNodeClose);
}
ofContent.append(ofRootElementClose);
// Open file and write it in it
std::ofstream outputFile;
outputFile.open(P.outputFileName.c_str(), std::ios::out | std::ios::trunc);
outputFile << ofContent;
outputFile.close();
// No space for error
return true;
}
/**
* Handles violent script termination (for now only CTRL+C type of signal)
* On SIGINT end restores cache to its former state and then exits (successfuly)
*/
void preventViolentTermination(int source)
{
std::cerr << std::endl << "Termination request recorded. Output file is being generated..." << std::endl;
std::string generationSuccess;
if (generateOutputFile())
{
generationSuccess = " [OK]";
}
else
{
generationSuccess = " [KO] - Sorry, there was an error during file generation. Permissions?";
}
std::cerr << "Output file generated:\t" << P.outputFileName << generationSuccess << std::endl;
exit(EXIT_SUCCESS);
}
/**
* Handles violent termination of the child processes
* Child processes are all using non-blocking calls and their
* termination does not require special handling, hence the
* function is empty
*/
void preventViolentTerminationChildProcess(int source)
{
std::cout << "\tAlso:" << std::endl;
std::cout << "\t\t...Child process terminated." << std::endl;
}
/**
* DEBUG FUNCTIONS
*/
void debug_showDiscoveredIPv6()
{
for (std::vector<in6_addr>::iterator it = discoveredPingIPv6.begin(); it != discoveredPingIPv6.end(); it++)
{
std::cout << "Discovered IPv6: " << convertIPv6ToString(*it) << std::endl;
}
}
void debug_showDiscoveredDevices()
{
std::cout << std::endl << "========= ========= ========= SCAN RESULTS ========= ========= =========" << std::endl << std::endl;
for (std::vector<Devices>::iterator it = discoveredDevices.begin(); it != discoveredDevices.end(); it++)
{
std::cout << "Discovered device: ";
printReadableMACAddress(it->macAddress);
std::cout << std::endl;
std::cout << "\tIPv4(" << getReadableIPv4Address(it->ipv4Address) <<")" << std::endl;
std::cout << "\tIPv6(" << it->ipv6AddressLL << ")" << std::endl;
std::cout << "\tIPv6-r(" << it->ipv6AddressSTD << ")" << std::endl << std::endl;
}
std::cout << "========= ========= ========= SCAN COMPLETED ========= ========= =========" << std::endl;
std::cout << "Scan output was written in " << P.outputFileName << std::endl;
}