Fast Probing
 All Classes Files Functions Variables Typedefs Macros
fastprobing.cpp
Go to the documentation of this file.
1 /*
2  * Fast Probing - An Active Probing Library for IP Options Equipped probes (http://traffic.comics.unina.it/)
3  *
4  * Copyright : (C) 2012 by Pietro Marchetta, Walter de Donato, Francesco Cesareo,
5  * Antonio Pescape' (PI)
6  * of the COMICS (COMputer for Interaction and
7  * CommunicationS) Group, Dipartimento di Informatica
8  * e Sistemistica of the University of Napoli "Federico II".
9  *
10  * email : pietro.marchetta@unina.it , walter.dedonato@unina.it , cesareo.francesco@gmail.com
11  * pescape@unina.it
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27 
28 #include "fastprobing.h"
29 #include "util.h"
30 #include "checksum.h"
31 
32 #include <iostream>
33 #include <string.h>
34 
35 #include <net/if.h>
36 #include <sys/ioctl.h>
37 
43 {
44 
45 }
46 
48 {
49 }
50 
51 
52 /********************/
54 /********************/
55 
63 int FastProbing::_udp(GeneralOption* option, ProbeReply* reply, std::string dst_ip_addr)
64 {
65  if (option->verbose_mode())
66  {
67  std::cout << "UDP: send probe to destination IP " << dst_ip_addr << std::endl;
68  }
69  int socketfd[1];
70  char pkt_buf[MAX_PACKET];
71  char rcv_buf[MAX_PACKET];
72  struct sockaddr_in dst_addr;
73  struct sockaddr_in src_addr;
74  struct timeval tv;
75  u_int8_t retry = 0;
76 
77  bool done = false;
78  bool error = false; //if true exit
79 
80  memset(rcv_buf,0,sizeof(rcv_buf));
81 
82  uint32 dst_addr_raw= Util::string2number(dst_ip_addr);
83 
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());
87 
88  reply->reset();
89 
90  if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
91  {
92  perror("UDP: error in ICMP socket creation");
93 
94  reply->set_scan_result(ERROR);
95  return ERROR;
96  }
97 
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)
101  {
102  perror("UDP: setsockopt SO_RCVTIMEO - ");
103  reply->set_scan_result(ERROR);
104  return ERROR;
105  }
106 
107  while (retry <= option->retries() && !done && !error) {
108 
109  //send
110  memset(&pkt_buf, '0', UDP_SIZE);
111 
112  //critical zone
113  pthread_mutex_t lock = option->lock();
114  pthread_mutex_lock(&lock);
115 
116  int ttl = option->ttl();
117 
118  if (setsockopt(option->socketudp(), IPPROTO_IP, IP_TTL, &ttl, sizeof(option->ttl())) < 0)
119  {
120  if (option->verbose_mode())
121  std::cout << "UDP error while setting TTL option" << std::endl;
122  perror("General UDP socket - setsockopt");
123 
124  fflush(stdout);
125  close(socketfd[ICMP]);
126 
127  reply->set_scan_result(ERROR);
128  return ERROR;
129  }
130 
131  if (setsockopt(option->socketudp(), IPPROTO_IP, IP_OPTIONS, NULL, 0) < 0)
132  {
133  if (option->verbose_mode())
134  std::cout << "UDP error while UNsetting prespecified timestamp option" << std::endl;
135  perror("UDP error while UNsetting prespecified timestamp option\n");
136 
137  fflush(stdout);
138  close(socketfd[ICMP]);
139 
140  reply->set_scan_result(ERROR);
141  return ERROR;
142  }
143 
144  if (sendto(option->socketudp(), pkt_buf, UDP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr)) < 0)
145  {
146  perror("UDP-probe: error in sendto UDP");
147  close(socketfd[ICMP]);
148 
149  reply->set_scan_result(ERROR);
150  return ERROR;
151  }
152 
153  pthread_mutex_unlock(&lock);
154  //end critical zone
155 
156  // set the ending time
157  int seconds = (option->timeout()/1000);
158  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
159 
160  do
161  {
162  socklen_t lsock = sizeof(dst_addr);
163  int pkt_len;
164  memset(rcv_buf, 0, sizeof (rcv_buf));
165 
166  if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
167  {
168  //timeout expired
169  if (option->verbose_mode())
170  std::cout << "[" << dst_ip_addr << "] UDP_TIMEOUT - Try #" << retry+1 << std::endl;
171  retry ++;
172  break;
173  }
174 
175  // packet received
176  reply->set_probe(rcv_buf);
177 
178  // which type of ICMP packet is?
179  if (reply->src_addr_raw() == dst_addr_raw && (reply->code() == ICMP_DEST_UNREACH || reply->code() == ICMP_TIME_EXCEEDED))
180  {
181  if (reply->scan_result() != ICMP_PORT_UNREACH || reply->scan_result() != ICMP_TIME_EXCEEDED)
182  {
183  if (option->verbose_mode())
184  std::cout << "[" << dst_ip_addr << "] UDP " << reply->scan_result() <<" from " << reply->src_addr() << std::endl;
185  error = true;
186  }
187  else
188  {
189  if (option->verbose_mode())
190  std::cout << "[" << dst_ip_addr << "] UDP " << reply->scan_result() <<" from " << reply->src_addr() << std::endl;
191  done = true;
192  }
193  }
194  //check the timeout
195  else if (!done && (clock() > endwait))
196  {
197  if (option->verbose_mode())
198  std::cout << "[" << dst_ip_addr << "] UDP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
199  retry++;
200  break;
201  }
202  }
203  while(!done && !error);
204 
205  if (error)
206  break;
207 
208  }//while retry
209 
210  close(socketfd[ICMP]);
211 
212  return reply->scan_result();
213 }
214 
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)
227 {
228  if (option->verbose_mode())
229  {
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;
232  }
233 
234  int socketfd[1];
235  char pkt_buf[MAX_PACKET];
236  char rcv_buf[MAX_PACKET];
237  struct sockaddr_in dst_addr;
238  struct sockaddr_in src_addr;
239  struct timeval tv;
240 
241  uint8 retry = 0;
242 
243  bool done = false;
244  bool error = false;
245 
246  memset(rcv_buf, 0, sizeof (rcv_buf));
247 
248  uint32 dst_addr_raw= Util::string2number(dst_ip_addr);
249 
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());
253 
254  reply->reset();
255 
256  if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)
257  {
258  if (option->verbose_mode())
259  std::cout << "UDPTS error in ICMP socket creation" << std::endl;
260  perror("UDPTS error in ICMP socket creation");
261 
262  error = true;
263  reply->set_scan_result(ERROR);
264  return ERROR;
265  }
266 
267  tv.tv_sec = option->timeout() / 1000;
268  tv.tv_usec = (option->timeout() % 1000) * 1000;
269 
270  if (setsockopt(socketfd[ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv)) < 0)
271  {
272  if (option->verbose_mode())
273  std::cout << "Setsock PROB" << std::endl;
274  perror("UDPTS-PL setsockopt");
275 
276  reply->set_scan_result(ERROR);
277  return ERROR;
278  }
279 
280  pthread_mutex_t lock;
281  unsigned char tspace[4 + 8*NIPS];
282  TSOption* tsOpt;
283 
284  while (retry < option->retries() && !done && !error)
285  {
286  //send
287  memset(&pkt_buf, '0', UDP_SIZE);
288 
289  //critical zone
290  lock = option->lock();
291  pthread_mutex_lock(&lock);
292 
293 
294  memset(tspace, 0, sizeof(tspace));
295 
296  tsOpt = (TSOption*)tspace;
297 
298  tsOpt->type = 68;
299  tsOpt->length = 36;
300  tsOpt->pointer = 5;
301  tsOpt->overflag = 3;
302 
303  tsOpt->ip1 = Util::string2number(ts_addr_1);
304  tsOpt->ip2 = Util::string2number(ts_addr_2);
305  tsOpt->ip3 = Util::string2number(ts_addr_3);
306  tsOpt->ip4 = Util::string2number(ts_addr_4);
307 
308  if (setsockopt(option->socketudp(), IPPROTO_IP, IP_OPTIONS, tspace, sizeof (tspace)) < 0) {
309  if (option->verbose_mode())
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");
312 
313  fflush(stdout);
314  close(socketfd[ICMP]);
315 
316  reply->set_scan_result(ERROR);
317  return ERROR;
318  }
319 
320  int s_to = sendto(option->socketudp(), pkt_buf, UDP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr));
321 
322  if (s_to < 0) {
323  if (option->verbose_mode())
324  std::cout << "[" << dst_ip_addr << "] UDP_TS error in sendto" << std::endl;
325  perror("UDP_TS error in sendto");
326 
327  close(socketfd[ICMP]);
328  error = true;
329 
330  reply->set_scan_result(ERROR);
331  return ERROR;
332  }
333 
334  pthread_mutex_unlock(&lock);
335  // end critical zone
336 
337  // set the ending time
338  int seconds = (option->timeout()/1000);
339  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
340 
341  do
342  {
343  socklen_t lsock = sizeof(dst_addr);
344 
345  memset(rcv_buf, 0, sizeof (rcv_buf));
346 
347  int pkt_len;
348  pkt_len = recvfrom(socketfd[ICMP], &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock);
349 
350  if ( pkt_len < 0) {
351  if (option->verbose_mode())
352  std::cout << "[" << dst_ip_addr << "] UDP_TS TIMEOUT [1] - Try #" << retry+1 << std::endl;
353 
355 
356  retry++;
357  break;
358  }
359 
360  // packet received
361  reply->set_probe(rcv_buf);
362 
363  // which type of ICMP packet is?
364  if (reply->src_addr_raw() == dst_addr_raw && reply->code() == ICMP_DEST_UNREACH)
365  {
366  if (reply->scan_result() != ICMP_PORT_UNREACH)
367  {
368  if (option->verbose_mode())
369  std::cout << "[" << dst_ip_addr << "] UDP_TS " << reply->scan_result() << " (type/code = " << reply->code() << "/" << reply->subcode() << ") from " << reply->src_addr() << std::endl;
370  error = true;
371  }
372  else
373  {
374  if (option->verbose_mode())
375  std::cout << "[" << dst_ip_addr << "] UDP_TS " << reply->scan_result() <<" from " << reply->src_addr() << std::endl;
376  done = true;
377  }
378  }
379  else if (!done && (clock() > endwait))
380  {
381  if (option->verbose_mode())
382  std::cout << "[" << dst_ip_addr << "] UDP_TS TIMEOUT [2] - Try #" << retry+1 << std::endl;
383  retry++;
384  break;
385  }
386  }
387  while (!done && !error);
388 
389  if (error)
390  break;
391 
392  }//endwhile
393 
394  close(socketfd[ICMP]);
395  return reply->scan_result();
396 }
397 
405 int FastProbing::_icmp(GeneralOption *option, ProbeReply *reply, std::string dst_ip_addr)
406 {
407  if (option->verbose_mode())
408  std::cout << "ICMP: send probe to destination IP " << dst_ip_addr << std::endl;
409 
410  int socketfd;
411  char pkt_buf[MAX_PACKET];
412  struct sockaddr_in dst_addr;
413  struct sockaddr_in src_addr;
414  struct icmp *icmp_buf;
415  struct timeval tv;
416  u_int8_t retry = 0;
417 
418  bool done = false;
419 
420  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
421 
422  dst_addr.sin_family = AF_INET;
423  dst_addr.sin_addr.s_addr = dst_addr_raw;
424 
425  reply->reset();
426 
427  if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
428  {
429  perror("Ping: error in socket creation");
430 
431  reply->set_scan_result(ERROR);
432  return ERROR;
433  }
434 
435  tv.tv_sec = option->timeout() / 1000;
436  tv.tv_usec = (option->timeout() % 1000) * 1000;
437 
438  if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv)) < 0)
439  {
440  perror("Ping: setsockopt SO_RCVTIMEO:");
441  }
442 
443  while (retry <= option->retries() && !done)
444  {
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);
452 
453  if (sendto(socketfd, icmp_buf, ICMP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr)) < 0)
454  {
455  perror("ICMP error in sendto UDP");
456  close(socketfd);
457 
458  reply->set_scan_result(ERROR);
459  return ERROR;
460  }
461  // set the ending time
462 
463  int seconds = (option->timeout()/1000);
464  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
465 
466  do
467  {
468  socklen_t lsock = sizeof(dst_addr);
469  int pkt_len;
470 
471  if ((pkt_len = recvfrom(socketfd, &pkt_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
472  {
473  retry++;
474  break;
475  }
476 
477  reply->set_probe(pkt_buf);
478 
479  if (reply->code() == ICMP_ECHOREPLY && reply->src_addr_raw() == dst_addr_raw)
480  {
481  if (option->verbose_mode())
482  std::cout << "[" << dst_ip_addr << "] ICMP_ECHOREPLY (" << pkt_len << " bytes)" << std::endl;
483  done = true;
484  }
485  //check the timeout
486  else if (!done && (clock() > endwait))
487  {
488  if (option->verbose_mode())
489  std::cout << "[" << dst_ip_addr << "] ICMP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
490  retry++;
491  break;
492  }
493  }
494  while (!done);
495  }
496  close(socketfd);
497 
498  return reply->scan_result();
499 }
500 
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)
513 {
514  if (option->verbose_mode())
515  {
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;
518  }
519 
520  int socketfd = 0;
521  char pkt_buf[MAX_PACKET];
522  char rcv_buf[MAX_PACKET];
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;
528  struct timeval tv;
529  unsigned int retry = 0;
530 
531  bool done = false;
532  bool error = false;
533 
534  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
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());
538 
539  reply->reset();
540 
541  if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1) {
542  if (option->verbose_mode())
543  std::cout << "ICMP_TS: error in socket creation" << std::endl;
544  perror("ICMP_TS: error in socket creation");
545 
546  error = true;
547  reply->set_scan_result(ERROR);
548  return ERROR;
549  }
550 
551  tv.tv_sec = option->timeout() / 1000;
552  tv.tv_usec = (option->timeout() % 1000) * 1000;
553 
554  //setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv));
555  if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv)) < 0)
556  {
557  if (option->verbose_mode())
558  std::cout << "Setsock PROB" << std::endl;
559  perror("ICMP_TS setsockopt");
560 
561  reply->set_scan_result(ERROR);
562  return ERROR;
563  }
564 
565  pthread_mutex_t lock;
566  unsigned char tspace[4 + 8*NIPS];
567  memset(tspace, 0, sizeof(tspace));
568  TSOption* tsOpt;
569 
570  while (retry < option->verbose_mode() && !done && !error)
571  {
572  //critical zone
573  lock = option->lock();
574  pthread_mutex_lock(&lock);
575 
576  tsOpt = (TSOption*)tspace;
577 
578  tsOpt->type = 68;
579  tsOpt->length = 36;
580  tsOpt->pointer = 5;
581  tsOpt->overflag = 3;
582 
583  tsOpt->ip1 = Util::string2number(ts_addr_1);
584  tsOpt->ip2 = Util::string2number(ts_addr_2);
585  tsOpt->ip3 = Util::string2number(ts_addr_3);
586  tsOpt->ip4 = Util::string2number(ts_addr_4);
587 
588  if (setsockopt(socketfd, IPPROTO_IP, IP_OPTIONS, tspace, sizeof (tspace)) < 0)
589  {
590  if (option->verbose_mode())
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");
593 
594  fflush(stdout);
595  close(socketfd);
596 
597  error = true;
598  reply->set_scan_result(ERROR);
599  return ERROR;
600  }
601 
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);
609 
610  int s_to = sendto(socketfd,icmp_buf, ICMP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr));
611 
612  if (s_to < 0)
613  {
614  if (option->verbose_mode())
615  std::cout << "ICMP_TS: error in sendto";
616  perror("ICMP_TS: error in sendto");
617 
618  close(socketfd);
619  error = true;
620  reply->set_scan_result(ERROR);
621  }
622 
623  pthread_mutex_unlock(&lock);
624  // end critical zone
625 
626  // set the ending time
627  int seconds = (option->timeout()/1000);
628  clock_t endwait = clock() + seconds * CLOCKS_PER_SEC ;
629 
630  do
631  {
632  // receive
633 
634  socklen_t lsock = sizeof(dst_addr);
635  memset(rcv_buf, 0, sizeof (rcv_buf));
636 
637  int pkt_len;
638  pkt_len = recvfrom(socketfd, &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock);
639 
640  if (pkt_len < 0)
641  {
642  retry++;
644  break;
645  }
646 
647  reply->set_probe(rcv_buf);
648 
649  if (reply->code() == ICMP_ECHOREPLY && reply->src_addr_raw() == dst_addr_raw) {
650 
651  if (! reply->options())
652  {
653  if (option->verbose_mode())
654  std::cout << "[" << dst_ip_addr << "] ICMP_TS " << reply->scan_result() <<" from " << reply->src_addr() << std::endl;
655  error = true;
656  break;
657  }
658  else
659  {
660  if (option->verbose_mode())
661  std::cout << "[" << dst_ip_addr << "] ICMP_TS " << reply->scan_result() <<" from " << reply->src_addr() << std::endl;
662  done = true;
663  }
664  }
665  else
666  {
667  if (option->verbose_mode())
668  std::cout << "Original message with different option [" << dst_ip_addr << "]" << std::endl;
669  done = true;
670  }
671  if (!done && (clock() > endwait))
672  {
673  if (option->verbose_mode())
674  std::cout << "[" << dst_ip_addr << "] ICMPTS_TIMEOUT[2] - Try #" << retry+1 << std::endl;
675  retry++;
677  break;
678  }
679  }
680  while (!done);
681  } //endwhile
682 
683  close(socketfd);
684  return reply->scan_result();
685 }
686 
694 int FastProbing::_tcp(GeneralOption* option, ProbeReply* reply, std::string dst_ip_addr)
695 {
696  if (option->verbose_mode())
697  std::cout << "TCP: send probe to destination IP " << dst_ip_addr << std::endl;
698 
699  int socketfd;
700  char pkt_buf[MAX_PACKET];
701  char rcv_buf[MAX_PACKET];
702 
703  struct sockaddr_in dst_addr;
704  struct sockaddr_in src_addr;
705  struct tcphdr *tcp_buf;
706  struct timeval tv;
707  u_int8_t retry = 0;
708 
709  bool done = false;
710 
711  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
712  dst_addr.sin_family = AF_INET;
713  dst_addr.sin_addr.s_addr = dst_addr_raw;
714 
715  reply->reset();
716 
717  //socket TCP
718  if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0) {
719  perror("TCP: error in TCP socket creation");
720 
721  reply->set_scan_result(ERROR);
722  return ERROR;
723  }
724 
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)
728  {
729  perror("TCP: setsockopt SO_RCVTIMEO: ");
730  }
731  uint32 src_addr_raw = 0;
732  if (_retrieve_src_addr(src_addr_raw, option) < 0)
733  {
734  reply->set_scan_result(ERROR);
735  return ERROR;
736  }
737 
738  while (retry < option->retries() && !done)
739  {
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;
746  // specify only syn flag
747  tcp_buf->syn = 1;
748  tcp_buf->window=htons(option->ip_id());
749  tcp_buf->urg_ptr=0;
750  tcp_buf->doff = 5;
751 
752  //create the pseudo header
753  src_addr.sin_addr.s_addr = src_addr_raw;
754 
755  //struct pseudoheader ph;
756  struct pseudoheader ph;
757  ph.sip = (u_int32_t)(dst_addr.sin_addr.s_addr);
758  ph.dip = (u_int32_t)(src_addr.sin_addr.s_addr);
759  ph.zero = 0;
760  ph.protocol = 6;
761  ph.tcplen = htons(20);
762  tcp_buf->check = 0;
763  tcp_buf->check = in_chksum_tcp((unsigned short *)&ph, (unsigned short *)tcp_buf, 20);
764 
765  if (sendto(socketfd, tcp_buf, TCP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr)) < 0)
766  {
767  perror("TCP: error in sendto");
768  close(socketfd);
769 
770  reply->set_scan_result(ERROR);
771  return ERROR;
772  }
773 
774  // set the ending time
775  int seconds = (option->timeout()/1000);
776  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
777 
778  do
779  {
780  socklen_t lsock = sizeof(dst_addr);
781  int pkt_len;
782  memset(rcv_buf, 0, sizeof (rcv_buf));
783 
784  if ((pkt_len = recvfrom(socketfd, &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
785  {
786  if (option->verbose_mode())
787  std::cout << "[" << dst_ip_addr << "] TCP_TIMEOUT - Try #" << retry+1 << std::endl;
788  retry++;
790  break;
791  }
792 
793  reply->set_probe(rcv_buf);
794 
795  if (reply->dst_addr_raw() == src_addr_raw && reply->ip_protocol() == 6)
796  {
797  if ((htons(reply->dst_port()) != 22) || (reply->src_addr_raw() != dst_addr_raw))
798  {
799  if (clock() > endwait)
800  {
801  if (option->verbose_mode())
802  std::cout << "[" << dst_ip_addr << "] TCP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
803  fflush(stdout);
804  retry++;
806  break;
807  }
808  continue;
809  }
810  else
811  {
812  if (option->verbose_mode())
813  std::cout << "[" << dst_ip_addr << "] TCP " << reply->scan_result() << std::endl;
814  }
815  done = true;
816  }
817 
818  else if (clock() > endwait)
819  {
820  if (option->verbose_mode())
821  std::cout << "[" << dst_ip_addr << "] TCP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
823  retry++;
824  break;
825  }
826  }
827  while(!done);
828  } //while retry
829 
830  close(socketfd);
831 
832  return reply->scan_result();
833 }
834 
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)
847 {
848  if (option->verbose_mode())
849  {
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;
852  }
853 
854  int socketfd;
855  char pkt_buf[MAX_PACKET];
856  char rcv_buf[MAX_PACKET];
857  struct sockaddr_in dst_addr;
858  struct sockaddr_in src_addr;
859  struct tcphdr *tcp_buf;
860  struct timeval tv;
861  u_int8_t retry = 0;
862 
863  bool done = false;
864  bool error = false;
865 
866  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
867  dst_addr.sin_family = AF_INET;
868  dst_addr.sin_addr.s_addr = dst_addr_raw;
869 
870  reply->reset();
871 
872  //socket TCP
873  if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0)
874  {
875  if (option->timeout())
876  std::cout << "TCP_TS error in TCP socket creation" << std::endl;
877  perror("TCP_TS error in UDP socket creation");
878 
879  error = true;
880  reply->set_scan_result(ERROR);
881  return ERROR;
882  }
883 
884  tv.tv_sec = option->timeout() / 1000;
885  tv.tv_usec = (option->timeout() % 1000) * 1000;
886 
887  if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv)) < 0)
888  {
889  if (option->verbose_mode())
890  std::cout << "TCP_TS setsockopt SO_RCVTIMEO" << std::endl;
891  perror("TCP_TS setsockopt SO_RCVTIMEO");
892 
893  reply->set_scan_result(ERROR);
894  }
895 
896  pthread_mutex_t lock;
897  unsigned char tspace[4 + 8*NIPS];
898  TSOption* tsOpt;
899 
900  uint32 src_addr_raw = 0;
901  if (_retrieve_src_addr(src_addr_raw, option) < 0)
902  {
903  reply->set_scan_result(ERROR);
904  return ERROR;
905  }
906 
907  while (retry < option->retries() && !done)
908  {
909  //critical zone
910  lock = option->lock();
911  pthread_mutex_lock(&lock);
912 
913  tsOpt = (TSOption*)tspace;
914 
915  tsOpt->type = 68;
916  tsOpt->length = 36;
917  tsOpt->pointer = 5;
918  tsOpt->overflag = 3;
919 
920  tsOpt->ip1 = Util::string2number(ts_addr_1);
921  tsOpt->ip2 = Util::string2number(ts_addr_2);
922  tsOpt->ip3 = Util::string2number(ts_addr_3);
923  tsOpt->ip4 = Util::string2number(ts_addr_4);
924 
925  if (setsockopt(socketfd, IPPROTO_IP, IP_OPTIONS, tspace, sizeof (tspace)) < 0)
926  {
927  if (option->verbose_mode())
928  std::cout << "[" << dst_ip_addr << "] TCP_TS: error while setting prespecified timestamp option" << std::endl;
929 
930  perror("TCP_TS error while setting prespecified timestamp option");
931  close(socketfd);
932 
933  reply->set_scan_result(ERROR);
934  return ERROR;
935  }
936 
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;
943  //specify only syn flag
944  tcp_buf->syn = 1;
945  tcp_buf->window=htons(option->ip_id());
946  tcp_buf->urg_ptr=0;
947  tcp_buf->doff = 5;
948 
949  //create the pseudo header
950  src_addr.sin_addr.s_addr = src_addr_raw;
951 
952  struct pseudoheader ph;
953  ph.sip = (uint32)(dst_addr.sin_addr.s_addr);
954  ph.dip = (u_int32_t)(src_addr.sin_addr.s_addr);
955  ph.zero = 0;
956  ph.protocol = 6;
957  ph.tcplen = htons(20);
958  tcp_buf->check = 0;
959  tcp_buf->check = in_chksum_tcp((unsigned short *)&ph, (unsigned short *)tcp_buf, 20);
960 
961  if (sendto(socketfd, tcp_buf, TCP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr)) < 0)
962  {
963  if (option->verbose_mode())
964  std::cout << "[" << dst_ip_addr << "] TCP_TS error in sendto" << std::endl;
965 
966  perror("TCP_TS error in sendto");
967  close(socketfd);
968  reply->set_scan_result(ERROR);
969  return ERROR;
970  }
971 
972  pthread_mutex_unlock(&lock);
973  // end critical zone
974 
975  // set the ending time
976  int seconds = option->timeout()/1000;
977  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
978 
979  do
980  {
981  socklen_t lsock = sizeof(dst_addr);
982  int pkt_len;
983  memset(rcv_buf, 0, sizeof (rcv_buf));
984 
985  if ((pkt_len = recvfrom(socketfd, &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
986  {
987  if (option->verbose_mode())
988  std::cout << "[" << dst_ip_addr << "] TCP_TS TIMEOUT - Try #" << retry+1 << std::endl;
989  retry++;
991  break;
992  }
993 
994  reply->set_probe(rcv_buf);
995 
996  if (reply->src_addr_raw() == dst_addr_raw && reply->ip_protocol() == 6)
997  {
998  if (option->verbose_mode())
999  std::cout << "[" << dst_ip_addr << "] TCP_TS (" << pkt_len << " bytes)" << std::endl;
1000 
1001  done = true;
1002  }
1003  else if (!done && (clock() > endwait))
1004  {
1005  if (option->verbose_mode())
1006  std::cout << "[" << dst_ip_addr << "] TCP_TS TIMEOUT[2] - Try #" << retry+1 << std::endl;
1007  reply->set_scan_result(NO_RESPONSE);
1008  retry++;
1009  break;
1010  }
1011  else
1012  {
1013  if (option->verbose_mode())
1014  std::cout << "[" << dst_ip_addr << "] TCP_TS " << reply->scan_result() << std::endl;
1015  reply->set_scan_result(NO_RESPONSE);
1016  retry++;
1017  break;
1018  }
1019  }
1020  while(!done);
1021  }//while retry
1022 
1023  close(socketfd);
1024  return reply->scan_result();
1025 }
1026 
1027 int FastProbing::_protocol(GeneralOption* option, ProbeReply* reply, std::string dst_ip_addr)
1028 {
1029  if (option->verbose_mode())
1030  {
1031  std::cout << "PROTOCOL: send probe to destination IP " << dst_ip_addr << std::endl;
1032  }
1033 
1034  int socketfd[ICMP];
1035  char pkt_buf[MAX_PACKET];
1036  char rcv_buf[MAX_PACKET];
1037  struct sockaddr_in dst_addr;
1038  struct sockaddr_in src_addr;
1039  struct iphdr *ip_dummy;
1040  struct timeval tv;
1041 
1042  u_int8_t retry = 0;
1043 
1044  bool done = false;
1045  bool error = false;
1046 
1047  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
1048  dst_addr.sin_family = AF_INET;
1049  dst_addr.sin_addr.s_addr = dst_addr_raw;
1050 
1051  reply->reset();
1052 
1053  //socket icmp
1054  if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
1055  {
1056  if (option->verbose_mode())
1057  std::cout << "[" << dst_ip_addr << "] PROTOCOL error in raw socket creation" << std::endl;
1058  perror("PROTOCOL error in raw socket creation");
1059 
1060  reply->set_scan_result(ERROR);
1061  return ERROR;
1062  }
1063 
1064  //set timeout on icmp
1065  tv.tv_sec = option->timeout() / 1000;
1066  tv.tv_usec = (option->timeout() % 1000) * 1000;
1067 
1068  if ((setsockopt(socketfd[ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv))) < 0)
1069  {
1070  if (option->verbose_mode())
1071  std::cout << "PROTOCOL setsockopt SO_RCVTIMEO" << std::endl;
1072  perror("PROTOCOL setsockopt ICMP RO_RCVTIMEO");
1073  }
1074 
1075  uint32 src_addr_raw = 0;
1076  if (_retrieve_src_addr(src_addr_raw, option) < 0)
1077  {
1078  reply->set_scan_result(ERROR);
1079  return ERROR;
1080  }
1081 
1082  int ttl = option->ttl();
1083  if (setsockopt(option->socketudp(), IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0)
1084  {
1085  if (option->verbose_mode())
1086  std::cout << "UDP error while setting TTL option" << std::endl;
1087  perror("General UDP socket - setsockopt");
1088 
1089  fflush(stdout);
1090  close(socketfd[ICMP]);
1091 
1092  reply->set_scan_result(ERROR);
1093  return ERROR;
1094  }
1095 
1096  if (setsockopt(option->socketudp(), IPPROTO_IP, IP_OPTIONS, NULL, 0) < 0)
1097  {
1098  if (option->verbose_mode())
1099  std::cout << "UDP error while UNsetting prespecified timestamp option" << std::endl;
1100  perror("UDP error while UNsetting prespecified timestamp option\n");
1101 
1102  fflush(stdout);
1103  close(socketfd[ICMP]);
1104 
1105  reply->set_scan_result(ERROR);
1106  return ERROR;
1107  }
1108 
1109  pthread_mutex_t lock;
1110 
1111  while (retry < option->retries() && !done && !error)
1112  {
1113  //critical zone
1114  lock = option->lock();
1115  pthread_mutex_lock(&lock);
1116 
1117  //craft ip dummy
1118  memset(pkt_buf, 0, sizeof (pkt_buf));
1119 
1120  ip_dummy = (struct iphdr *) pkt_buf;
1121  ip_dummy->ihl = 5;
1122  ip_dummy->version = 4;
1123  ip_dummy->tos = 0;
1124  ip_dummy->tot_len = IP_SIZE + UDP_SIZE;
1125  //ip_dummy->id = htons(1016);
1126  ip_dummy->id = htons(option->ip_id());
1127  ip_dummy->ttl = option->ttl();
1128  ip_dummy->protocol = option->protocol_number();
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); //considera 24 tanto sono 4 byte di zeri
1133  ip_dummy->check = newcksum;
1134 
1135  //send protocol probe
1136  int s_to = sendto(option->socketudp(), pkt_buf, IP_SIZE + UDP_SIZE, 0, (struct sockaddr*) &dst_addr, sizeof(dst_addr));
1137  if (s_to < 0)
1138  {
1139  if (option->verbose_mode())
1140  std::cout << "[" << dst_ip_addr << "] PROTOCOL error in sendto" << std::endl;
1141  perror("PROTOCOL error in sendto");
1142 
1143  close(socketfd[ICMP]);
1144  reply->set_scan_result(ERROR);
1145  return ERROR;
1146  }
1147 
1148  pthread_mutex_unlock(&lock);
1149  //end critical zone
1150 
1151  // set the ending time
1152  int seconds = option->timeout()/1000;
1153  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC;
1154 
1155  do
1156  {
1157  socklen_t lsock = sizeof(dst_addr);
1158  int pkt_len;
1159  memset(rcv_buf, 0, sizeof (rcv_buf));
1160 
1161  //receive
1162  if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
1163  {
1164  if (option->verbose_mode())
1165  std::cout << "[" << dst_ip_addr << "] PROTOCOL TIMEOUT - Try #" << retry+1 << std::endl;
1166 
1167  reply->set_scan_result(NO_RESPONSE);
1168  retry++;
1169  break;
1170  }
1171 
1172  reply->set_probe(rcv_buf);
1173 
1174 
1175  if (reply->ip_protocol() == 1 && reply->code() == ICMP_DEST_UNREACH && reply->iperror_dst_addr_raw() == dst_addr_raw && reply->iperror_ip_protocol() == option->protocol_number())
1176  {
1177  if (reply->subcode() != ICMP_PROT_UNREACH )
1178  {
1179  if (option->verbose_mode())
1180  std::cout << "[" << dst_ip_addr << "] PROTOCOL " << reply->scan_result() << " from " << reply->dst_addr() << std::endl;
1181  error = true;
1182  }
1183  else
1184  {
1185  if (option->verbose_mode())
1186  std::cout << "[" << dst_ip_addr << "] PROTOCOL " << reply->scan_result() << " from " << reply->dst_addr() << std::endl;
1187  done = true;
1188  }
1189  }
1190  //check the timeout
1191  else if (clock() > endwait)
1192  {
1193  if (option->verbose_mode())
1194  std::cout << "[" << dst_ip_addr << "] PROTOCOL TIMEOUT[2] - Try #" << retry+1 << std::endl;
1195 
1196  retry++;
1197  break;
1198  }
1199  }
1200  while(!done && !error);
1201  if (error)
1202  break;
1203  }//while retry
1204 
1205  close(socketfd[ICMP]);
1206  return reply->scan_result();
1207 }
1208 
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)
1221 {
1222  if (option->verbose_mode())
1223  {
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;
1226  }
1227 
1228  struct sockaddr_in src_addr;
1229 
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());
1233 
1234  int socketfd[ICMP];
1235  char pkt_buf[MAX_PACKET];
1236  char rcv_buf[MAX_PACKET];
1237  struct sockaddr_in dst_addr;
1238  struct iphdr *ip_dummy;
1239  struct timeval tv;
1240 
1241  u_int8_t retry = 0;
1242 
1243  bool done = false;
1244  bool error = false;
1245 
1246  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
1247  dst_addr.sin_family = AF_INET;
1248  dst_addr.sin_addr.s_addr = dst_addr_raw;
1249 
1250  reply->reset();
1251 
1252  //socket icmp
1253  if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) <0)
1254  {
1255  if (option->verbose_mode())
1256  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS error in raw socket creation" << std::endl;
1257  perror("PROTOCOL_TS error in raw socket creation");
1258 
1259  reply->set_scan_result(ERROR);
1260  return ERROR;
1261  }
1262 
1263  //set timeout on icmp
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)
1267  {
1268  if (option->verbose_mode())
1269  std::cout << "PROTOCOL_TS setsockopt SO_RCVTIMEO" << std::endl;
1270  perror("PROTOCOL_TS setsockopt ICMP SO_RVTIMEO: ");
1271  }
1272 
1273  uint32 src_addr_raw = 0;
1274  if (_retrieve_src_addr(src_addr_raw, option) < 0)
1275  {
1276  reply->set_scan_result(ERROR);
1277  return ERROR;
1278  }
1279 
1280  pthread_mutex_t lock;
1281  unsigned char tspace[4 + 8*NIPS];
1282  TSOption* tsOpt;
1283 
1284  while (retry < option->retries() && !done && !error)
1285  {
1286  //crafting ip dummy
1287  memset(pkt_buf, 0, sizeof (pkt_buf));
1288  ip_dummy = (struct iphdr *) pkt_buf;
1289  ip_dummy->ihl = 14;
1290  ip_dummy->version = 4;
1291  ip_dummy->tos = 0;
1292  ip_dummy->tot_len = IP_SIZE_TS + UDP_SIZE;
1293  ip_dummy->id = htons(1016);
1294  ip_dummy->ttl= option->ttl();
1295  ip_dummy->protocol= option->protocol_number();
1296  ip_dummy->saddr= src_addr_raw;
1297  ip_dummy->daddr= dst_addr_raw;
1298  ip_dummy->check=0;
1299 
1300  //critical zone
1301  lock = option->lock();
1302  pthread_mutex_lock(&lock);
1303 
1304  tsOpt = (TSOption*)tspace;
1305 
1306  tsOpt->type = 68;
1307  tsOpt->length = 36;
1308  tsOpt->pointer = 5;
1309  tsOpt->overflag = 3;
1310 
1311  tsOpt->ip1 = Util::string2number(ts_addr_1);
1312  tsOpt->ip2 = Util::string2number(ts_addr_2);
1313  tsOpt->ip3 = Util::string2number(ts_addr_3);
1314  tsOpt->ip4 = Util::string2number(ts_addr_4);
1315 
1316  memcpy(&(pkt_buf[20]), tspace, sizeof(tspace));
1317 
1318  unsigned short newcksum = cksum((unsigned short *)pkt_buf, IP_SIZE_TS);
1319  ip_dummy->check = newcksum;
1320 
1321  //send probe
1322  if (sendto(option->socketudp(), pkt_buf, IP_SIZE_TS + UDP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr)) < 0)
1323  {
1324  if (option->verbose_mode())
1325  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS error in sendto" << std::endl;
1326  perror("PROTOCOL_TS error in sendto");
1327  close(socketfd[ICMP]);
1328  reply->set_scan_result(ERROR);
1329  return ERROR;
1330  }
1331 
1332  pthread_mutex_unlock(&lock);
1333  // end critical zone
1334 
1335  // set the ending time
1336  int seconds = option->timeout()/1000;
1337  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
1338 
1339  do
1340  {
1341  socklen_t lsock = sizeof(dst_addr);
1342  int pkt_len;
1343  memset(rcv_buf, 0, sizeof (rcv_buf));
1344 
1345  //receive
1346  if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
1347  {
1348  if (option->verbose_mode())
1349  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS TIMEOUT - Try #" << retry+1 << std::endl;
1350  reply->set_scan_result(NO_RESPONSE);
1351  retry++;
1352  break;
1353  }
1354 
1355  //packet received
1356  reply->set_probe(rcv_buf);
1357 
1358  //analyse result
1359  if(reply->ip_protocol() == 1 && reply->code() == ICMP_DEST_UNREACH && reply->iperror_dst_addr_raw() == dst_addr_raw && reply->iperror_ip_protocol() == option->protocol_number())
1360  {
1361  if (reply->subcode() == ICMP_PROT_UNREACH)
1362  {
1363  if (!reply->options())
1364  {
1365  if (option->verbose_mode())
1366  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS " << reply->scan_result() << " from " << reply->dst_addr() << std::endl;
1367  error = true;
1368  break;
1369  }
1370  done = true;
1371  if (option->verbose_mode())
1372  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS " << reply->scan_result() << " from " << reply->dst_addr() << std::endl;
1373 
1374  }
1375  else
1376  {
1377  if (option->verbose_mode())
1378  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS " << reply->scan_result() << " from " << reply->src_addr() << std::endl;
1379  error = true;
1380  }
1381  }
1382  else if (!done && (clock() > endwait))
1383  {
1384  if (option->verbose_mode())
1385  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS TIMEOUT[2] - Try #" << retry+1 << std::endl;
1386  retry++;
1387  reply->set_scan_result(NO_RESPONSE);
1388  break;
1389  }
1390 
1391  }
1392  while (!done && !error);
1393 
1394  if (error)
1395  break;
1396 
1397  }
1398 
1399  close(socketfd[ICMP]);
1400  return reply->scan_result();
1401 }
1402 
1409 int FastProbing::_retrieve_src_addr(uint32& src_addr_raw, GeneralOption* option)
1410 {
1411  int fd;
1412  struct if_nameindex *curif, *ifs;
1413  struct ifreq req;
1414 
1415  if((fd = socket(PF_INET, SOCK_DGRAM, 0)) != -1)
1416  {
1417  ifs = if_nameindex();
1418  if(ifs)
1419  {
1420  for(curif = ifs; curif && curif->if_name; curif++)
1421  {
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");
1426  else
1427  if ((strcmp(curif->if_name, (option->eth()).c_str())) == 0)
1428  {
1429  src_addr_raw = ((struct sockaddr_in*) &req.ifr_addr)->sin_addr.s_addr;
1430  return 0;
1431  }
1432  }
1433  if_freenameindex(ifs);
1434  if(close(fd)!=0)
1435  perror("Problem arised while detecting eth IP address [closing file]");
1436  }
1437  else
1438  perror("if_nameindex");
1439  }
1440  else
1441  {
1442  perror("socket");
1443  return -1;
1444  }
1445 }
1446 
1447 
1448 /********************/
1450 /********************/
1451 
1460 int FastProbing::udp(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1461 {
1462  // UDP D
1463  return _udp(option, reply, dst_addr);
1464 }
1465 
1474 int FastProbing::udp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1475 {
1476  // UDP_TS D|DDDD
1477  return _udp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1478 }
1479 
1489 int FastProbing::udp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1)
1490 {
1491  // UDP_TS D|XXXX
1492  return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1493 }
1494 
1505 int FastProbing::udp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2)
1506 {
1507  // UDP_TS D|XYYY
1508  return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1509 }
1510 
1522 int FastProbing::udp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3)
1523 {
1524  // UDP_TS D|XYZZ
1525  return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1526 }
1527 
1540 int FastProbing::udp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3, std::string ts_addr_4)
1541 {
1542  // UDP_TS D|XYZW
1543  return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1544 }
1545 
1554 int FastProbing::icmp(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1555 {
1556  // ICMP D
1557  return _icmp(option, reply, dst_addr);
1558 }
1559 
1568 int FastProbing::icmp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1569 {
1570  // ICMP_TS D|DDDD
1571  return _icmp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1572 }
1573 
1583 int FastProbing::icmp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1)
1584 {
1585  // send ICMP_TS to D|XXXX
1586  return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1587 }
1588 
1599 int FastProbing::icmp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2)
1600 {
1601  // send ICMP_TS to D|XYYY
1602  return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1603 }
1604 
1616 int FastProbing::icmp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3)
1617 {
1618  // send ICMP_TS to D|XYZZ
1619  return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1620 }
1621 
1634 int FastProbing::icmp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3, std::string ts_addr_4)
1635 {
1636  // send ICMP_TS to D|XYZW
1637  return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1638 }
1639 
1648 int FastProbing::tcp(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1649 {
1650  return _tcp(option, reply, dst_addr);
1651 
1652 }
1653 
1662 int FastProbing::tcp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1663 {
1664  // send TCP_TS to D|DDDD
1665  return _tcp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1666 }
1667 
1677 int FastProbing::tcp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1)
1678 {
1679  // send TCP_TS to D|XXXX
1680  return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1681 }
1682 
1693 int FastProbing::tcp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2)
1694 {
1695  // send TCP_TS to D|XYYY
1696  return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1697 }
1698 
1710 int FastProbing::tcp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3)
1711 {
1712  // send TCP_TS to D|XYZZ
1713  return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1714 }
1715 
1728 int FastProbing::tcp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3, std::string ts_addr_4)
1729 {
1730  // send TCP_TS to D|XYZW
1731  return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1732 }
1733 
1742 int FastProbing::protocol(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1743 {
1744  // send PROTOCOL to D
1745  return _protocol(option, reply, dst_addr);
1746 }
1747 
1756 int FastProbing::protocol_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1757 {
1758  // send PROTOCOL_TS to D|DDDD
1759  return _protocol_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1760 }
1761 
1771 int FastProbing::protocol_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1)
1772 {
1773  // send PROTOCOL_TS to D|XXXX
1774  return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1775 }
1776 
1787 int FastProbing::protocol_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2)
1788 {
1789  // send PROTOCOL_TS to D|XYYY
1790  return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1791 }
1792 
1804 int FastProbing::protocol_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3)
1805 {
1806  // send PROTOCOL_TS to D|XYZZ
1807  return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1808 }
1809 
1822 int FastProbing::protocol_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2, std::string ts_addr_3, std::string ts_addr_4)
1823 {
1824  // send PROTOCOL_TS to D|XYZW
1825  return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1826 }