12 #include <sys/ioctl.h>
47 std::cout <<
"UDP: send probe to destination IP " << dst_ip_addr << std::endl;
52 struct sockaddr_in dst_addr;
53 struct sockaddr_in src_addr;
60 memset(rcv_buf,0,
sizeof(rcv_buf));
64 dst_addr.sin_family = AF_INET;
65 dst_addr.sin_addr.s_addr = dst_addr_raw;
66 dst_addr.sin_port = htons(option->
dst_port());
70 if ((socketfd[
ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
72 perror(
"UDP: error in ICMP socket creation");
78 tv.tv_sec = option->
timeout() / 1000;
79 tv.tv_usec = (option->
timeout() % 1000) * 1000;
80 if (setsockopt(socketfd[
ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
82 perror(
"UDP: setsockopt SO_RCVTIMEO - ");
87 while (retry <= option->retries() && !done && !error) {
93 pthread_mutex_t
lock = option->
lock();
94 pthread_mutex_lock(&lock);
96 int ttl = option->
ttl();
98 if (setsockopt(option->
socketudp(), IPPROTO_IP, IP_TTL, &ttl,
sizeof(option->
ttl())) < 0)
101 std::cout <<
"UDP error while setting TTL option" << std::endl;
102 perror(
"General UDP socket - setsockopt");
105 close(socketfd[ICMP]);
111 if (setsockopt(option->
socketudp(), IPPROTO_IP, IP_OPTIONS, NULL, 0) < 0)
114 std::cout <<
"UDP error while UNsetting prespecified timestamp option" << std::endl;
115 perror(
"UDP error while UNsetting prespecified timestamp option\n");
118 close(socketfd[ICMP]);
124 if (sendto(option->
socketudp(), pkt_buf,
UDP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr)) < 0)
126 perror(
"UDP-probe: error in sendto UDP");
127 close(socketfd[ICMP]);
133 pthread_mutex_unlock(&lock);
137 int seconds = (option->
timeout()/1000);
138 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
142 socklen_t lsock =
sizeof(dst_addr);
144 memset(rcv_buf, 0,
sizeof (rcv_buf));
146 if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
150 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TIMEOUT - Try #" << retry+1 << std::endl;
159 if (reply->
src_addr_raw() == dst_addr_raw && (reply->
code() == ICMP_DEST_UNREACH || reply->
code() == ICMP_TIME_EXCEEDED))
164 std::cout <<
"[" << dst_ip_addr <<
"] UDP " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
170 std::cout <<
"[" << dst_ip_addr <<
"] UDP " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
175 else if (!done && (clock() > endwait))
178 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
183 while(!done && !error);
190 close(socketfd[ICMP]);
206 int OneProbeOneReply::_udp_ts(
GeneralOption* option,
ProbeReply* reply, std::string dst_ip_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3, std::string ts_addr_4)
210 std::cout <<
"UDP_TS: send probe to destination IP " << dst_ip_addr;
211 std::cout <<
" with prespecified timestamp IPs " << ts_addr_1 <<
", " << ts_addr_2 <<
", " << ts_addr_3 <<
", " << ts_addr_4 << std::endl;
217 struct sockaddr_in dst_addr;
218 struct sockaddr_in src_addr;
226 memset(rcv_buf, 0,
sizeof (rcv_buf));
230 dst_addr.sin_family = AF_INET;
231 dst_addr.sin_addr.s_addr = dst_addr_raw;
232 dst_addr.sin_port = htons(option->
dst_port());
236 if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)
239 std::cout <<
"UDPTS error in ICMP socket creation" << std::endl;
240 perror(
"UDPTS error in ICMP socket creation");
247 tv.tv_sec = option->
timeout() / 1000;
248 tv.tv_usec = (option->
timeout() % 1000) * 1000;
250 if (setsockopt(socketfd[ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
253 std::cout <<
"Setsock PROB" << std::endl;
254 perror(
"UDPTS-PL setsockopt");
260 pthread_mutex_t
lock;
261 unsigned char tspace[4 + 8*
NIPS];
264 while (retry < option->retries() && !done && !error)
267 memset(&pkt_buf,
'0', UDP_SIZE);
270 lock = option->
lock();
271 pthread_mutex_lock(&lock);
274 memset(tspace, 0,
sizeof(tspace));
288 if (setsockopt(option->
socketudp(), IPPROTO_IP, IP_OPTIONS, tspace,
sizeof (tspace)) < 0) {
290 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS error while setting prespecified timestamp option" << std::endl;
291 perror(
"UDP_TS error while setting prespecified timestamp option");
294 close(socketfd[ICMP]);
300 int s_to = sendto(option->
socketudp(), pkt_buf,
UDP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr));
304 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS error in sendto" << std::endl;
305 perror(
"UDP_TS error in sendto");
307 close(socketfd[ICMP]);
314 pthread_mutex_unlock(&lock);
318 int seconds = (option->
timeout()/1000);
319 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
323 socklen_t lsock =
sizeof(dst_addr);
325 memset(rcv_buf, 0,
sizeof (rcv_buf));
328 pkt_len = recvfrom(socketfd[ICMP], &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock);
332 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS TIMEOUT [1] - Try #" << retry+1 << std::endl;
344 if (reply->
src_addr_raw() == dst_addr_raw && reply->
code() == ICMP_DEST_UNREACH)
349 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS " << reply->
scan_result() <<
" (type/code = " << reply->
code() <<
"/" << reply->
subcode() <<
") from " << reply->
src_addr() << std::endl;
355 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
359 else if (!done && (clock() > endwait))
362 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS TIMEOUT [2] - Try #" << retry+1 << std::endl;
367 while (!done && !error);
374 close(socketfd[ICMP]);
388 std::cout <<
"ICMP: send probe to destination IP " << dst_ip_addr << std::endl;
392 struct sockaddr_in dst_addr;
393 struct sockaddr_in src_addr;
394 struct icmp *icmp_buf;
402 dst_addr.sin_family = AF_INET;
403 dst_addr.sin_addr.s_addr = dst_addr_raw;
407 if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
409 perror(
"Ping: error in socket creation");
415 tv.tv_sec = option->
timeout() / 1000;
416 tv.tv_usec = (option->
timeout() % 1000) * 1000;
418 if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
420 perror(
"Ping: setsockopt SO_RCVTIMEO:");
423 while (retry <= option->retries() && !done)
425 icmp_buf = (
struct icmp *) pkt_buf;
426 icmp_buf->icmp_type = ICMP_ECHO;
427 icmp_buf->icmp_code = 0;
428 icmp_buf->icmp_id = htons(option->
ip_id());
429 icmp_buf->icmp_seq = htons(retry);
430 icmp_buf->icmp_cksum = 0;
431 icmp_buf->icmp_cksum =
cksum((
unsigned short *) icmp_buf,
ICMP_SIZE);
433 if (sendto(socketfd, icmp_buf,
ICMP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr)) < 0)
435 perror(
"ICMP error in sendto UDP");
443 int seconds = (option->
timeout()/1000);
444 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
448 socklen_t lsock =
sizeof(dst_addr);
451 if ((pkt_len = recvfrom(socketfd, &pkt_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
459 if (reply->
code() == ICMP_ECHOREPLY && reply->
src_addr_raw() == dst_addr_raw)
462 std::cout <<
"[" << dst_ip_addr <<
"] ICMP_ECHOREPLY (" << pkt_len <<
" bytes)" << std::endl;
466 else if (!done && (clock() > endwait))
469 std::cout <<
"[" << dst_ip_addr <<
"] ICMP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
492 int OneProbeOneReply::_icmp_ts(
GeneralOption* option,
ProbeReply* reply, std::string dst_ip_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3, std::string ts_addr_4)
496 std::cout <<
"ICMP_TS: send probe to destination IP " << dst_ip_addr;
497 std::cout <<
" with prespecified timestamp IPs " << ts_addr_1 <<
", " << ts_addr_2 <<
", " << ts_addr_3 <<
", " << ts_addr_4 << std::endl;
503 struct sockaddr_in dst_addr;
504 struct sockaddr_in src_addr;
505 struct in_addr rcv_addr;
506 struct icmp *icmp_buf;
507 struct iphdr* ip_buf;
509 unsigned int retry = 0;
515 dst_addr.sin_family = AF_INET;
516 dst_addr.sin_addr.s_addr = dst_addr_raw;
517 dst_addr.sin_port = htons(option->
dst_port());
521 if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1) {
523 std::cout <<
"ICMP_TS: error in socket creation" << std::endl;
524 perror(
"ICMP_TS: error in socket creation");
531 tv.tv_sec = option->
timeout() / 1000;
532 tv.tv_usec = (option->
timeout() % 1000) * 1000;
535 if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
538 std::cout <<
"Setsock PROB" << std::endl;
539 perror(
"ICMP_TS setsockopt");
545 pthread_mutex_t
lock;
546 unsigned char tspace[4 + 8*
NIPS];
547 memset(tspace, 0,
sizeof(tspace));
550 while (retry < option->verbose_mode() && !done && !error)
553 lock = option->
lock();
554 pthread_mutex_lock(&lock);
568 if (setsockopt(socketfd, IPPROTO_IP, IP_OPTIONS, tspace,
sizeof (tspace)) < 0)
571 std::cout <<
"[" << dst_ip_addr <<
"] ICMP_TS error while setting prespecified timestamp option" << std::endl;
572 perror(
"ICMP_TS error while setting prespecified timestamp option");
582 icmp_buf = (
struct icmp *) pkt_buf;
583 icmp_buf->icmp_type = ICMP_ECHO;
584 icmp_buf->icmp_code = 0;
585 icmp_buf->icmp_id = htons(option->
ip_id());
586 icmp_buf->icmp_seq = 0;
587 icmp_buf->icmp_cksum = 0;
588 icmp_buf->icmp_cksum =
cksum((
unsigned short *) icmp_buf,
ICMP_SIZE);
590 int s_to = sendto(socketfd,icmp_buf,
ICMP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr));
595 std::cout <<
"ICMP_TS: error in sendto";
596 perror(
"ICMP_TS: error in sendto");
603 pthread_mutex_unlock(&lock);
607 int seconds = (option->
timeout()/1000);
608 clock_t endwait = clock() + seconds * CLOCKS_PER_SEC ;
614 socklen_t lsock =
sizeof(dst_addr);
615 memset(rcv_buf, 0,
sizeof (rcv_buf));
618 pkt_len = recvfrom(socketfd, &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock);
629 if (reply->
code() == ICMP_ECHOREPLY && reply->
src_addr_raw() == dst_addr_raw) {
634 std::cout <<
"[" << dst_ip_addr <<
"] ICMP_TS " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
641 std::cout <<
"[" << dst_ip_addr <<
"] ICMP_TS " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
648 std::cout <<
"Original message with different option [" << dst_ip_addr <<
"]" << std::endl;
651 if (!done && (clock() > endwait))
654 std::cout <<
"[" << dst_ip_addr <<
"] ICMPTS_TIMEOUT[2] - Try #" << retry+1 << std::endl;
677 std::cout <<
"TCP: send probe to destination IP " << dst_ip_addr << std::endl;
683 struct sockaddr_in dst_addr;
684 struct sockaddr_in src_addr;
685 struct tcphdr *tcp_buf;
692 dst_addr.sin_family = AF_INET;
693 dst_addr.sin_addr.s_addr = dst_addr_raw;
698 if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0) {
699 perror(
"TCP: error in TCP socket creation");
705 tv.tv_sec = option->
timeout() / 1000;
706 tv.tv_usec = (option->
timeout() % 1000) * 1000;
707 if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
709 perror(
"TCP: setsockopt SO_RCVTIMEO: ");
712 if (_retrieve_src_addr(src_addr_raw, option) < 0)
718 while (retry < option->retries() && !done)
720 memset(pkt_buf, 0,
sizeof (pkt_buf));
721 tcp_buf = (
struct tcphdr *) pkt_buf;
722 tcp_buf->source = htons(22);
723 tcp_buf->dest = htons(737);
724 tcp_buf->seq = htonl(option->
ip_id());
725 tcp_buf->ack_seq = 0;
728 tcp_buf->window=htons(option->
ip_id());
733 src_addr.sin_addr.s_addr = src_addr_raw;
737 ph.
sip = (u_int32_t)(dst_addr.sin_addr.s_addr);
738 ph.dip = (u_int32_t)(src_addr.sin_addr.s_addr);
741 ph.tcplen = htons(20);
743 tcp_buf->check =
in_chksum_tcp((
unsigned short *)&ph, (
unsigned short *)tcp_buf, 20);
745 if (sendto(socketfd, tcp_buf,
TCP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr)) < 0)
747 perror(
"TCP: error in sendto");
755 int seconds = (option->
timeout()/1000);
756 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
760 socklen_t lsock =
sizeof(dst_addr);
762 memset(rcv_buf, 0,
sizeof (rcv_buf));
764 if ((pkt_len = recvfrom(socketfd, &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
767 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TIMEOUT - Try #" << retry+1 << std::endl;
779 if (clock() > endwait)
782 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
793 std::cout <<
"[" << dst_ip_addr <<
"] TCP " << reply->
scan_result() << std::endl;
798 else if (clock() > endwait)
801 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
826 int OneProbeOneReply::_tcp_ts(
GeneralOption* option,
ProbeReply* reply, std::string dst_ip_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3, std::string ts_addr_4)
830 std::cout <<
"TCP_TS: send probe to destination IP " << dst_ip_addr;
831 std::cout <<
" with prespecified timestamp IPs " << ts_addr_1 <<
", " << ts_addr_2 <<
", " << ts_addr_3 <<
", " << ts_addr_4 << std::endl;
837 struct sockaddr_in dst_addr;
838 struct sockaddr_in src_addr;
839 struct tcphdr *tcp_buf;
847 dst_addr.sin_family = AF_INET;
848 dst_addr.sin_addr.s_addr = dst_addr_raw;
853 if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0)
856 std::cout <<
"TCP_TS error in TCP socket creation" << std::endl;
857 perror(
"TCP_TS error in UDP socket creation");
864 tv.tv_sec = option->
timeout() / 1000;
865 tv.tv_usec = (option->
timeout() % 1000) * 1000;
867 if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
870 std::cout <<
"TCP_TS setsockopt SO_RCVTIMEO" << std::endl;
871 perror(
"TCP_TS setsockopt SO_RCVTIMEO");
876 pthread_mutex_t
lock;
877 unsigned char tspace[4 + 8*
NIPS];
881 if (_retrieve_src_addr(src_addr_raw, option) < 0)
887 while (retry < option->retries() && !done)
890 lock = option->
lock();
891 pthread_mutex_lock(&lock);
905 if (setsockopt(socketfd, IPPROTO_IP, IP_OPTIONS, tspace,
sizeof (tspace)) < 0)
908 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS: error while setting prespecified timestamp option" << std::endl;
910 perror(
"TCP_TS error while setting prespecified timestamp option");
917 memset(pkt_buf, 0,
sizeof (pkt_buf));
918 tcp_buf = (
struct tcphdr *) pkt_buf;
919 tcp_buf->source = htons(22);
920 tcp_buf->dest = htons(737);
921 tcp_buf->seq = htonl(option->
ip_id());
922 tcp_buf->ack_seq = 0;
925 tcp_buf->window=htons(option->
ip_id());
930 src_addr.sin_addr.s_addr = src_addr_raw;
933 ph.
sip = (
uint32)(dst_addr.sin_addr.s_addr);
934 ph.dip = (u_int32_t)(src_addr.sin_addr.s_addr);
937 ph.tcplen = htons(20);
939 tcp_buf->check =
in_chksum_tcp((
unsigned short *)&ph, (
unsigned short *)tcp_buf, 20);
941 if (sendto(socketfd, tcp_buf,
TCP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr)) < 0)
944 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS error in sendto" << std::endl;
946 perror(
"TCP_TS error in sendto");
952 pthread_mutex_unlock(&lock);
956 int seconds = option->
timeout()/1000;
957 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
961 socklen_t lsock =
sizeof(dst_addr);
963 memset(rcv_buf, 0,
sizeof (rcv_buf));
965 if ((pkt_len = recvfrom(socketfd, &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
968 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS TIMEOUT - Try #" << retry+1 << std::endl;
979 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS (" << pkt_len <<
" bytes)" << std::endl;
983 else if (!done && (clock() > endwait))
986 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS TIMEOUT[2] - Try #" << retry+1 << std::endl;
994 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS " << reply->
scan_result() << std::endl;
1011 std::cout <<
"PROTOCOL: send probe to destination IP " << dst_ip_addr << std::endl;
1017 struct sockaddr_in dst_addr;
1018 struct sockaddr_in src_addr;
1019 struct iphdr *ip_dummy;
1028 dst_addr.sin_family = AF_INET;
1029 dst_addr.sin_addr.s_addr = dst_addr_raw;
1034 if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
1037 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL error in raw socket creation" << std::endl;
1038 perror(
"PROTOCOL error in raw socket creation");
1045 tv.tv_sec = option->
timeout() / 1000;
1046 tv.tv_usec = (option->
timeout() % 1000) * 1000;
1048 if ((setsockopt(socketfd[ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv))) < 0)
1051 std::cout <<
"PROTOCOL setsockopt SO_RCVTIMEO" << std::endl;
1052 perror(
"PROTOCOL setsockopt ICMP RO_RCVTIMEO");
1056 if (_retrieve_src_addr(src_addr_raw, option) < 0)
1062 int ttl = option->
ttl();
1063 if (setsockopt(option->
socketudp(), IPPROTO_IP, IP_TTL, &ttl,
sizeof(ttl)) < 0)
1066 std::cout <<
"UDP error while setting TTL option" << std::endl;
1067 perror(
"General UDP socket - setsockopt");
1070 close(socketfd[ICMP]);
1076 if (setsockopt(option->
socketudp(), IPPROTO_IP, IP_OPTIONS, NULL, 0) < 0)
1079 std::cout <<
"UDP error while UNsetting prespecified timestamp option" << std::endl;
1080 perror(
"UDP error while UNsetting prespecified timestamp option\n");
1083 close(socketfd[ICMP]);
1089 pthread_mutex_t
lock;
1091 while (retry < option->retries() && !done && !error)
1094 lock = option->
lock();
1095 pthread_mutex_lock(&lock);
1098 memset(pkt_buf, 0,
sizeof (pkt_buf));
1100 ip_dummy = (
struct iphdr *) pkt_buf;
1102 ip_dummy->version = 4;
1106 ip_dummy->id = htons(option->
ip_id());
1107 ip_dummy->ttl = option->
ttl();
1109 ip_dummy->saddr = src_addr_raw;
1110 ip_dummy->daddr = dst_addr_raw;
1111 ip_dummy->check = 0;
1112 unsigned short newcksum =
cksum((
unsigned short *)pkt_buf, 20);
1113 ip_dummy->check = newcksum;
1116 int s_to = sendto(option->
socketudp(), pkt_buf,
IP_SIZE +
UDP_SIZE, 0, (
struct sockaddr*) &dst_addr,
sizeof(dst_addr));
1120 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL error in sendto" << std::endl;
1121 perror(
"PROTOCOL error in sendto");
1123 close(socketfd[ICMP]);
1128 pthread_mutex_unlock(&lock);
1132 int seconds = option->
timeout()/1000;
1133 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC;
1137 socklen_t lsock =
sizeof(dst_addr);
1139 memset(rcv_buf, 0,
sizeof (rcv_buf));
1142 if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
1145 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL TIMEOUT - Try #" << retry+1 << std::endl;
1157 if (reply->
subcode() != ICMP_PROT_UNREACH )
1160 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL " << reply->
scan_result() <<
" from " << reply->
dst_addr() << std::endl;
1166 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL " << reply->
scan_result() <<
" from " << reply->
dst_addr() << std::endl;
1171 else if (clock() > endwait)
1174 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL TIMEOUT[2] - Try #" << retry+1 << std::endl;
1180 while(!done && !error);
1185 close(socketfd[ICMP]);
1200 int OneProbeOneReply::_protocol_ts(
GeneralOption* option,
ProbeReply* reply, std::string dst_ip_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3, std::string ts_addr_4)
1204 std::cout <<
"PROTOCOL_TS: send probe to destination IP " << dst_ip_addr;
1205 std::cout <<
" with prespecified timestamp IPs " << ts_addr_1 <<
", " << ts_addr_2 <<
", " << ts_addr_3 <<
", " << ts_addr_4 << std::endl;
1208 struct sockaddr_in src_addr;
1210 src_addr.sin_family = AF_INET;
1211 src_addr.sin_addr.s_addr = htonl(INADDR_ANY);
1212 src_addr.sin_port = htons(option->
src_port());
1217 struct sockaddr_in dst_addr;
1218 struct iphdr *ip_dummy;
1227 dst_addr.sin_family = AF_INET;
1228 dst_addr.sin_addr.s_addr = dst_addr_raw;
1233 if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) <0)
1236 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS error in raw socket creation" << std::endl;
1237 perror(
"PROTOCOL_TS error in raw socket creation");
1244 tv.tv_sec = option->
timeout() / 1000;
1245 tv.tv_usec = (option->
timeout() % 1000) * 1000;
1246 if (setsockopt(socketfd[ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
1249 std::cout <<
"PROTOCOL_TS setsockopt SO_RCVTIMEO" << std::endl;
1250 perror(
"PROTOCOL_TS setsockopt ICMP SO_RVTIMEO: ");
1254 if (_retrieve_src_addr(src_addr_raw, option) < 0)
1260 pthread_mutex_t
lock;
1261 unsigned char tspace[4 + 8*
NIPS];
1264 while (retry < option->retries() && !done && !error)
1267 memset(pkt_buf, 0,
sizeof (pkt_buf));
1268 ip_dummy = (
struct iphdr *) pkt_buf;
1270 ip_dummy->version = 4;
1273 ip_dummy->id = htons(1016);
1274 ip_dummy->ttl= option->
ttl();
1276 ip_dummy->saddr= src_addr_raw;
1277 ip_dummy->daddr= dst_addr_raw;
1281 lock = option->
lock();
1282 pthread_mutex_lock(&lock);
1296 memcpy(&(pkt_buf[20]), tspace,
sizeof(tspace));
1298 unsigned short newcksum =
cksum((
unsigned short *)pkt_buf,
IP_SIZE_TS);
1299 ip_dummy->check = newcksum;
1305 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS error in sendto" << std::endl;
1306 perror(
"PROTOCOL_TS error in sendto");
1307 close(socketfd[ICMP]);
1312 pthread_mutex_unlock(&lock);
1316 int seconds = option->
timeout()/1000;
1317 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
1321 socklen_t lsock =
sizeof(dst_addr);
1323 memset(rcv_buf, 0,
sizeof (rcv_buf));
1326 if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
1329 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS TIMEOUT - Try #" << retry+1 << std::endl;
1341 if (reply->
subcode() == ICMP_PROT_UNREACH)
1346 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS " << reply->
scan_result() <<
" from " << reply->
dst_addr() << std::endl;
1352 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS " << reply->
scan_result() <<
" from " << reply->
dst_addr() << std::endl;
1358 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
1362 else if (!done && (clock() > endwait))
1365 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS TIMEOUT[2] - Try #" << retry+1 << std::endl;
1372 while (!done && !error);
1379 close(socketfd[ICMP]);
1392 struct if_nameindex *curif, *ifs;
1395 if((fd = socket(PF_INET, SOCK_DGRAM, 0)) != -1)
1397 ifs = if_nameindex();
1400 for(curif = ifs; curif && curif->if_name; curif++)
1402 strncpy(req.ifr_name, curif->if_name, IFNAMSIZ);
1403 req.ifr_name[IFNAMSIZ] = 0;
1404 if (ioctl(fd, SIOCGIFADDR, &req) < 0)
1405 perror(
"Problem arised while detecting eth IP address");
1407 if ((strcmp(curif->if_name, (option->
eth()).c_str())) == 0)
1409 src_addr_raw = ((
struct sockaddr_in*) &req.ifr_addr)->sin_addr.s_addr;
1413 if_freenameindex(ifs);
1415 perror(
"Problem arised while detecting eth IP address [closing file]");
1418 perror(
"if_nameindex");
1443 return _udp(option, reply, dst_addr);
1457 return _udp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1472 return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1488 return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1505 return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1523 return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1537 return _icmp(option, reply, dst_addr);
1551 return _icmp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1566 return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1582 return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1599 return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1617 return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1630 return _tcp(option, reply, dst_addr);
1645 return _tcp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1660 return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1676 return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1693 return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1711 return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1725 return _protocol(option, reply, dst_addr);
1739 return _protocol_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1754 return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1770 return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1787 return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1805 return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);