36 #include <sys/ioctl.h>
67 std::cout <<
"UDP: send probe to destination IP " << dst_ip_addr << std::endl;
72 struct sockaddr_in dst_addr;
73 struct sockaddr_in src_addr;
80 memset(rcv_buf,0,
sizeof(rcv_buf));
84 dst_addr.sin_family = AF_INET;
85 dst_addr.sin_addr.s_addr = dst_addr_raw;
86 dst_addr.sin_port = htons(option->
dst_port());
90 if ((socketfd[
ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
92 perror(
"UDP: error in ICMP socket creation");
98 tv.tv_sec = option->
timeout() / 1000;
99 tv.tv_usec = (option->
timeout() % 1000) * 1000;
100 if (setsockopt(socketfd[
ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
102 perror(
"UDP: setsockopt SO_RCVTIMEO - ");
107 while (retry <= option->retries() && !done && !error) {
113 pthread_mutex_t
lock = option->
lock();
114 pthread_mutex_lock(&lock);
116 int ttl = option->
ttl();
118 if (setsockopt(option->
socketudp(), IPPROTO_IP, IP_TTL, &ttl,
sizeof(option->
ttl())) < 0)
121 std::cout <<
"UDP error while setting TTL option" << std::endl;
122 perror(
"General UDP socket - setsockopt");
125 close(socketfd[ICMP]);
131 if (setsockopt(option->
socketudp(), IPPROTO_IP, IP_OPTIONS, NULL, 0) < 0)
134 std::cout <<
"UDP error while UNsetting prespecified timestamp option" << std::endl;
135 perror(
"UDP error while UNsetting prespecified timestamp option\n");
138 close(socketfd[ICMP]);
144 if (sendto(option->
socketudp(), pkt_buf,
UDP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr)) < 0)
146 perror(
"UDP-probe: error in sendto UDP");
147 close(socketfd[ICMP]);
153 pthread_mutex_unlock(&lock);
157 int seconds = (option->
timeout()/1000);
158 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
162 socklen_t lsock =
sizeof(dst_addr);
164 memset(rcv_buf, 0,
sizeof (rcv_buf));
166 if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
170 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TIMEOUT - Try #" << retry+1 << std::endl;
179 if (reply->
src_addr_raw() == dst_addr_raw && (reply->
code() == ICMP_DEST_UNREACH || reply->
code() == ICMP_TIME_EXCEEDED))
184 std::cout <<
"[" << dst_ip_addr <<
"] UDP " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
190 std::cout <<
"[" << dst_ip_addr <<
"] UDP " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
195 else if (!done && (clock() > endwait))
198 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
203 while(!done && !error);
210 close(socketfd[ICMP]);
226 int FastProbing::_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)
230 std::cout <<
"UDP_TS: send probe to destination IP " << dst_ip_addr;
231 std::cout <<
" with prespecified timestamp IPs " << ts_addr_1 <<
", " << ts_addr_2 <<
", " << ts_addr_3 <<
", " << ts_addr_4 << std::endl;
237 struct sockaddr_in dst_addr;
238 struct sockaddr_in src_addr;
246 memset(rcv_buf, 0,
sizeof (rcv_buf));
250 dst_addr.sin_family = AF_INET;
251 dst_addr.sin_addr.s_addr = dst_addr_raw;
252 dst_addr.sin_port = htons(option->
dst_port());
256 if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)
259 std::cout <<
"UDPTS error in ICMP socket creation" << std::endl;
260 perror(
"UDPTS error in ICMP socket creation");
267 tv.tv_sec = option->
timeout() / 1000;
268 tv.tv_usec = (option->
timeout() % 1000) * 1000;
270 if (setsockopt(socketfd[ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
273 std::cout <<
"Setsock PROB" << std::endl;
274 perror(
"UDPTS-PL setsockopt");
280 pthread_mutex_t
lock;
281 unsigned char tspace[4 + 8*
NIPS];
284 while (retry < option->retries() && !done && !error)
287 memset(&pkt_buf,
'0', UDP_SIZE);
290 lock = option->
lock();
291 pthread_mutex_lock(&lock);
294 memset(tspace, 0,
sizeof(tspace));
308 if (setsockopt(option->
socketudp(), IPPROTO_IP, IP_OPTIONS, tspace,
sizeof (tspace)) < 0) {
310 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS error while setting prespecified timestamp option" << std::endl;
311 perror(
"UDP_TS error while setting prespecified timestamp option");
314 close(socketfd[ICMP]);
320 int s_to = sendto(option->
socketudp(), pkt_buf,
UDP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr));
324 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS error in sendto" << std::endl;
325 perror(
"UDP_TS error in sendto");
327 close(socketfd[ICMP]);
334 pthread_mutex_unlock(&lock);
338 int seconds = (option->
timeout()/1000);
339 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
343 socklen_t lsock =
sizeof(dst_addr);
345 memset(rcv_buf, 0,
sizeof (rcv_buf));
348 pkt_len = recvfrom(socketfd[ICMP], &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock);
352 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS TIMEOUT [1] - Try #" << retry+1 << std::endl;
364 if (reply->
src_addr_raw() == dst_addr_raw && reply->
code() == ICMP_DEST_UNREACH)
369 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS " << reply->
scan_result() <<
" (type/code = " << reply->
code() <<
"/" << reply->
subcode() <<
") from " << reply->
src_addr() << std::endl;
375 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
379 else if (!done && (clock() > endwait))
382 std::cout <<
"[" << dst_ip_addr <<
"] UDP_TS TIMEOUT [2] - Try #" << retry+1 << std::endl;
387 while (!done && !error);
394 close(socketfd[ICMP]);
408 std::cout <<
"ICMP: send probe to destination IP " << dst_ip_addr << std::endl;
412 struct sockaddr_in dst_addr;
413 struct sockaddr_in src_addr;
414 struct icmp *icmp_buf;
422 dst_addr.sin_family = AF_INET;
423 dst_addr.sin_addr.s_addr = dst_addr_raw;
427 if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
429 perror(
"Ping: error in socket creation");
435 tv.tv_sec = option->
timeout() / 1000;
436 tv.tv_usec = (option->
timeout() % 1000) * 1000;
438 if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
440 perror(
"Ping: setsockopt SO_RCVTIMEO:");
443 while (retry <= option->retries() && !done)
445 icmp_buf = (
struct icmp *) pkt_buf;
446 icmp_buf->icmp_type = ICMP_ECHO;
447 icmp_buf->icmp_code = 0;
448 icmp_buf->icmp_id = htons(option->
ip_id());
449 icmp_buf->icmp_seq = htons(retry);
450 icmp_buf->icmp_cksum = 0;
451 icmp_buf->icmp_cksum =
cksum((
unsigned short *) icmp_buf,
ICMP_SIZE);
453 if (sendto(socketfd, icmp_buf,
ICMP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr)) < 0)
455 perror(
"ICMP error in sendto UDP");
463 int seconds = (option->
timeout()/1000);
464 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
468 socklen_t lsock =
sizeof(dst_addr);
471 if ((pkt_len = recvfrom(socketfd, &pkt_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
479 if (reply->
code() == ICMP_ECHOREPLY && reply->
src_addr_raw() == dst_addr_raw)
482 std::cout <<
"[" << dst_ip_addr <<
"] ICMP_ECHOREPLY (" << pkt_len <<
" bytes)" << std::endl;
486 else if (!done && (clock() > endwait))
489 std::cout <<
"[" << dst_ip_addr <<
"] ICMP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
512 int FastProbing::_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)
516 std::cout <<
"ICMP_TS: send probe to destination IP " << dst_ip_addr;
517 std::cout <<
" with prespecified timestamp IPs " << ts_addr_1 <<
", " << ts_addr_2 <<
", " << ts_addr_3 <<
", " << ts_addr_4 << std::endl;
523 struct sockaddr_in dst_addr;
524 struct sockaddr_in src_addr;
525 struct in_addr rcv_addr;
526 struct icmp *icmp_buf;
527 struct iphdr* ip_buf;
529 unsigned int retry = 0;
535 dst_addr.sin_family = AF_INET;
536 dst_addr.sin_addr.s_addr = dst_addr_raw;
537 dst_addr.sin_port = htons(option->
dst_port());
541 if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1) {
543 std::cout <<
"ICMP_TS: error in socket creation" << std::endl;
544 perror(
"ICMP_TS: error in socket creation");
551 tv.tv_sec = option->
timeout() / 1000;
552 tv.tv_usec = (option->
timeout() % 1000) * 1000;
555 if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
558 std::cout <<
"Setsock PROB" << std::endl;
559 perror(
"ICMP_TS setsockopt");
565 pthread_mutex_t
lock;
566 unsigned char tspace[4 + 8*
NIPS];
567 memset(tspace, 0,
sizeof(tspace));
570 while (retry < option->verbose_mode() && !done && !error)
573 lock = option->
lock();
574 pthread_mutex_lock(&lock);
588 if (setsockopt(socketfd, IPPROTO_IP, IP_OPTIONS, tspace,
sizeof (tspace)) < 0)
591 std::cout <<
"[" << dst_ip_addr <<
"] ICMP_TS error while setting prespecified timestamp option" << std::endl;
592 perror(
"ICMP_TS error while setting prespecified timestamp option");
602 icmp_buf = (
struct icmp *) pkt_buf;
603 icmp_buf->icmp_type = ICMP_ECHO;
604 icmp_buf->icmp_code = 0;
605 icmp_buf->icmp_id = htons(option->
ip_id());
606 icmp_buf->icmp_seq = 0;
607 icmp_buf->icmp_cksum = 0;
608 icmp_buf->icmp_cksum =
cksum((
unsigned short *) icmp_buf,
ICMP_SIZE);
610 int s_to = sendto(socketfd,icmp_buf,
ICMP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr));
615 std::cout <<
"ICMP_TS: error in sendto";
616 perror(
"ICMP_TS: error in sendto");
623 pthread_mutex_unlock(&lock);
627 int seconds = (option->
timeout()/1000);
628 clock_t endwait = clock() + seconds * CLOCKS_PER_SEC ;
634 socklen_t lsock =
sizeof(dst_addr);
635 memset(rcv_buf, 0,
sizeof (rcv_buf));
638 pkt_len = recvfrom(socketfd, &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock);
649 if (reply->
code() == ICMP_ECHOREPLY && reply->
src_addr_raw() == dst_addr_raw) {
654 std::cout <<
"[" << dst_ip_addr <<
"] ICMP_TS " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
661 std::cout <<
"[" << dst_ip_addr <<
"] ICMP_TS " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
668 std::cout <<
"Original message with different option [" << dst_ip_addr <<
"]" << std::endl;
671 if (!done && (clock() > endwait))
674 std::cout <<
"[" << dst_ip_addr <<
"] ICMPTS_TIMEOUT[2] - Try #" << retry+1 << std::endl;
697 std::cout <<
"TCP: send probe to destination IP " << dst_ip_addr << std::endl;
703 struct sockaddr_in dst_addr;
704 struct sockaddr_in src_addr;
705 struct tcphdr *tcp_buf;
712 dst_addr.sin_family = AF_INET;
713 dst_addr.sin_addr.s_addr = dst_addr_raw;
718 if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0) {
719 perror(
"TCP: error in TCP socket creation");
725 tv.tv_sec = option->
timeout() / 1000;
726 tv.tv_usec = (option->
timeout() % 1000) * 1000;
727 if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
729 perror(
"TCP: setsockopt SO_RCVTIMEO: ");
732 if (_retrieve_src_addr(src_addr_raw, option) < 0)
738 while (retry < option->retries() && !done)
740 memset(pkt_buf, 0,
sizeof (pkt_buf));
741 tcp_buf = (
struct tcphdr *) pkt_buf;
742 tcp_buf->source = htons(22);
743 tcp_buf->dest = htons(737);
744 tcp_buf->seq = htonl(option->
ip_id());
745 tcp_buf->ack_seq = 0;
748 tcp_buf->window=htons(option->
ip_id());
753 src_addr.sin_addr.s_addr = src_addr_raw;
757 ph.
sip = (u_int32_t)(dst_addr.sin_addr.s_addr);
758 ph.dip = (u_int32_t)(src_addr.sin_addr.s_addr);
761 ph.tcplen = htons(20);
763 tcp_buf->check =
in_chksum_tcp((
unsigned short *)&ph, (
unsigned short *)tcp_buf, 20);
765 if (sendto(socketfd, tcp_buf,
TCP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr)) < 0)
767 perror(
"TCP: error in sendto");
775 int seconds = (option->
timeout()/1000);
776 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
780 socklen_t lsock =
sizeof(dst_addr);
782 memset(rcv_buf, 0,
sizeof (rcv_buf));
784 if ((pkt_len = recvfrom(socketfd, &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
787 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TIMEOUT - Try #" << retry+1 << std::endl;
799 if (clock() > endwait)
802 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
813 std::cout <<
"[" << dst_ip_addr <<
"] TCP " << reply->
scan_result() << std::endl;
818 else if (clock() > endwait)
821 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
846 int FastProbing::_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)
850 std::cout <<
"TCP_TS: send probe to destination IP " << dst_ip_addr;
851 std::cout <<
" with prespecified timestamp IPs " << ts_addr_1 <<
", " << ts_addr_2 <<
", " << ts_addr_3 <<
", " << ts_addr_4 << std::endl;
857 struct sockaddr_in dst_addr;
858 struct sockaddr_in src_addr;
859 struct tcphdr *tcp_buf;
867 dst_addr.sin_family = AF_INET;
868 dst_addr.sin_addr.s_addr = dst_addr_raw;
873 if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0)
876 std::cout <<
"TCP_TS error in TCP socket creation" << std::endl;
877 perror(
"TCP_TS error in UDP socket creation");
884 tv.tv_sec = option->
timeout() / 1000;
885 tv.tv_usec = (option->
timeout() % 1000) * 1000;
887 if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
890 std::cout <<
"TCP_TS setsockopt SO_RCVTIMEO" << std::endl;
891 perror(
"TCP_TS setsockopt SO_RCVTIMEO");
896 pthread_mutex_t
lock;
897 unsigned char tspace[4 + 8*
NIPS];
901 if (_retrieve_src_addr(src_addr_raw, option) < 0)
907 while (retry < option->retries() && !done)
910 lock = option->
lock();
911 pthread_mutex_lock(&lock);
925 if (setsockopt(socketfd, IPPROTO_IP, IP_OPTIONS, tspace,
sizeof (tspace)) < 0)
928 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS: error while setting prespecified timestamp option" << std::endl;
930 perror(
"TCP_TS error while setting prespecified timestamp option");
937 memset(pkt_buf, 0,
sizeof (pkt_buf));
938 tcp_buf = (
struct tcphdr *) pkt_buf;
939 tcp_buf->source = htons(22);
940 tcp_buf->dest = htons(737);
941 tcp_buf->seq = htonl(option->
ip_id());
942 tcp_buf->ack_seq = 0;
945 tcp_buf->window=htons(option->
ip_id());
950 src_addr.sin_addr.s_addr = src_addr_raw;
953 ph.
sip = (
uint32)(dst_addr.sin_addr.s_addr);
954 ph.dip = (u_int32_t)(src_addr.sin_addr.s_addr);
957 ph.tcplen = htons(20);
959 tcp_buf->check =
in_chksum_tcp((
unsigned short *)&ph, (
unsigned short *)tcp_buf, 20);
961 if (sendto(socketfd, tcp_buf,
TCP_SIZE, 0, (
struct sockaddr *) &dst_addr,
sizeof(dst_addr)) < 0)
964 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS error in sendto" << std::endl;
966 perror(
"TCP_TS error in sendto");
972 pthread_mutex_unlock(&lock);
976 int seconds = option->
timeout()/1000;
977 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
981 socklen_t lsock =
sizeof(dst_addr);
983 memset(rcv_buf, 0,
sizeof (rcv_buf));
985 if ((pkt_len = recvfrom(socketfd, &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
988 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS TIMEOUT - Try #" << retry+1 << std::endl;
999 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS (" << pkt_len <<
" bytes)" << std::endl;
1003 else if (!done && (clock() > endwait))
1006 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS TIMEOUT[2] - Try #" << retry+1 << std::endl;
1014 std::cout <<
"[" << dst_ip_addr <<
"] TCP_TS " << reply->
scan_result() << std::endl;
1031 std::cout <<
"PROTOCOL: send probe to destination IP " << dst_ip_addr << std::endl;
1037 struct sockaddr_in dst_addr;
1038 struct sockaddr_in src_addr;
1039 struct iphdr *ip_dummy;
1048 dst_addr.sin_family = AF_INET;
1049 dst_addr.sin_addr.s_addr = dst_addr_raw;
1054 if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
1057 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL error in raw socket creation" << std::endl;
1058 perror(
"PROTOCOL error in raw socket creation");
1065 tv.tv_sec = option->
timeout() / 1000;
1066 tv.tv_usec = (option->
timeout() % 1000) * 1000;
1068 if ((setsockopt(socketfd[ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv))) < 0)
1071 std::cout <<
"PROTOCOL setsockopt SO_RCVTIMEO" << std::endl;
1072 perror(
"PROTOCOL setsockopt ICMP RO_RCVTIMEO");
1076 if (_retrieve_src_addr(src_addr_raw, option) < 0)
1082 int ttl = option->
ttl();
1083 if (setsockopt(option->
socketudp(), IPPROTO_IP, IP_TTL, &ttl,
sizeof(ttl)) < 0)
1086 std::cout <<
"UDP error while setting TTL option" << std::endl;
1087 perror(
"General UDP socket - setsockopt");
1090 close(socketfd[ICMP]);
1096 if (setsockopt(option->
socketudp(), IPPROTO_IP, IP_OPTIONS, NULL, 0) < 0)
1099 std::cout <<
"UDP error while UNsetting prespecified timestamp option" << std::endl;
1100 perror(
"UDP error while UNsetting prespecified timestamp option\n");
1103 close(socketfd[ICMP]);
1109 pthread_mutex_t
lock;
1111 while (retry < option->retries() && !done && !error)
1114 lock = option->
lock();
1115 pthread_mutex_lock(&lock);
1118 memset(pkt_buf, 0,
sizeof (pkt_buf));
1120 ip_dummy = (
struct iphdr *) pkt_buf;
1122 ip_dummy->version = 4;
1126 ip_dummy->id = htons(option->
ip_id());
1127 ip_dummy->ttl = option->
ttl();
1129 ip_dummy->saddr = src_addr_raw;
1130 ip_dummy->daddr = dst_addr_raw;
1131 ip_dummy->check = 0;
1132 unsigned short newcksum =
cksum((
unsigned short *)pkt_buf, 20);
1133 ip_dummy->check = newcksum;
1136 int s_to = sendto(option->
socketudp(), pkt_buf,
IP_SIZE +
UDP_SIZE, 0, (
struct sockaddr*) &dst_addr,
sizeof(dst_addr));
1140 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL error in sendto" << std::endl;
1141 perror(
"PROTOCOL error in sendto");
1143 close(socketfd[ICMP]);
1148 pthread_mutex_unlock(&lock);
1152 int seconds = option->
timeout()/1000;
1153 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC;
1157 socklen_t lsock =
sizeof(dst_addr);
1159 memset(rcv_buf, 0,
sizeof (rcv_buf));
1162 if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
1165 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL TIMEOUT - Try #" << retry+1 << std::endl;
1177 if (reply->
subcode() != ICMP_PROT_UNREACH )
1180 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL " << reply->
scan_result() <<
" from " << reply->
dst_addr() << std::endl;
1186 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL " << reply->
scan_result() <<
" from " << reply->
dst_addr() << std::endl;
1191 else if (clock() > endwait)
1194 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL TIMEOUT[2] - Try #" << retry+1 << std::endl;
1200 while(!done && !error);
1205 close(socketfd[ICMP]);
1220 int FastProbing::_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)
1224 std::cout <<
"PROTOCOL_TS: send probe to destination IP " << dst_ip_addr;
1225 std::cout <<
" with prespecified timestamp IPs " << ts_addr_1 <<
", " << ts_addr_2 <<
", " << ts_addr_3 <<
", " << ts_addr_4 << std::endl;
1228 struct sockaddr_in src_addr;
1230 src_addr.sin_family = AF_INET;
1231 src_addr.sin_addr.s_addr = htonl(INADDR_ANY);
1232 src_addr.sin_port = htons(option->
src_port());
1237 struct sockaddr_in dst_addr;
1238 struct iphdr *ip_dummy;
1247 dst_addr.sin_family = AF_INET;
1248 dst_addr.sin_addr.s_addr = dst_addr_raw;
1253 if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) <0)
1256 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS error in raw socket creation" << std::endl;
1257 perror(
"PROTOCOL_TS error in raw socket creation");
1264 tv.tv_sec = option->
timeout() / 1000;
1265 tv.tv_usec = (option->
timeout() % 1000) * 1000;
1266 if (setsockopt(socketfd[ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof (tv)) < 0)
1269 std::cout <<
"PROTOCOL_TS setsockopt SO_RCVTIMEO" << std::endl;
1270 perror(
"PROTOCOL_TS setsockopt ICMP SO_RVTIMEO: ");
1274 if (_retrieve_src_addr(src_addr_raw, option) < 0)
1280 pthread_mutex_t
lock;
1281 unsigned char tspace[4 + 8*
NIPS];
1284 while (retry < option->retries() && !done && !error)
1287 memset(pkt_buf, 0,
sizeof (pkt_buf));
1288 ip_dummy = (
struct iphdr *) pkt_buf;
1290 ip_dummy->version = 4;
1293 ip_dummy->id = htons(1016);
1294 ip_dummy->ttl= option->
ttl();
1296 ip_dummy->saddr= src_addr_raw;
1297 ip_dummy->daddr= dst_addr_raw;
1301 lock = option->
lock();
1302 pthread_mutex_lock(&lock);
1316 memcpy(&(pkt_buf[20]), tspace,
sizeof(tspace));
1318 unsigned short newcksum =
cksum((
unsigned short *)pkt_buf,
IP_SIZE_TS);
1319 ip_dummy->check = newcksum;
1325 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS error in sendto" << std::endl;
1326 perror(
"PROTOCOL_TS error in sendto");
1327 close(socketfd[ICMP]);
1332 pthread_mutex_unlock(&lock);
1336 int seconds = option->
timeout()/1000;
1337 clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
1341 socklen_t lsock =
sizeof(dst_addr);
1343 memset(rcv_buf, 0,
sizeof (rcv_buf));
1346 if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf,
MAX_PACKET, 0, (
struct sockaddr *) &src_addr, &lsock)) < 0)
1349 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS TIMEOUT - Try #" << retry+1 << std::endl;
1361 if (reply->
subcode() == ICMP_PROT_UNREACH)
1366 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS " << reply->
scan_result() <<
" from " << reply->
dst_addr() << std::endl;
1372 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS " << reply->
scan_result() <<
" from " << reply->
dst_addr() << std::endl;
1378 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS " << reply->
scan_result() <<
" from " << reply->
src_addr() << std::endl;
1382 else if (!done && (clock() > endwait))
1385 std::cout <<
"[" << dst_ip_addr <<
"] PROTOCOL_TS TIMEOUT[2] - Try #" << retry+1 << std::endl;
1392 while (!done && !error);
1399 close(socketfd[ICMP]);
1412 struct if_nameindex *curif, *ifs;
1415 if((fd = socket(PF_INET, SOCK_DGRAM, 0)) != -1)
1417 ifs = if_nameindex();
1420 for(curif = ifs; curif && curif->if_name; curif++)
1422 strncpy(req.ifr_name, curif->if_name, IFNAMSIZ);
1423 req.ifr_name[IFNAMSIZ] = 0;
1424 if (ioctl(fd, SIOCGIFADDR, &req) < 0)
1425 perror(
"Problem arised while detecting eth IP address");
1427 if ((strcmp(curif->if_name, (option->
eth()).c_str())) == 0)
1429 src_addr_raw = ((
struct sockaddr_in*) &req.ifr_addr)->sin_addr.s_addr;
1433 if_freenameindex(ifs);
1435 perror(
"Problem arised while detecting eth IP address [closing file]");
1438 perror(
"if_nameindex");
1463 return _udp(option, reply, dst_addr);
1477 return _udp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1492 return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1508 return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1525 return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1543 return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1557 return _icmp(option, reply, dst_addr);
1571 return _icmp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1586 return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1602 return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1619 return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1637 return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1650 return _tcp(option, reply, dst_addr);
1665 return _tcp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1680 return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1696 return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1713 return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1731 return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1745 return _protocol(option, reply, dst_addr);
1759 return _protocol_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1774 return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1790 return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1807 return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1825 return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);