OneProbeOneReply
 All Classes Files Functions Variables Typedefs Macros
oneprobeonereply.cpp
Go to the documentation of this file.
1 #include "oneprobeonereply.h"
2 #include "util.h"
3 #include "checksum.h"
4 
5 #include <iostream>
6 
7 //#include <sstream>
8 //#include <string>
9 #include <string.h>
10 
11 #include <net/if.h>
12 #include <sys/ioctl.h>
13 //#include <stdlib.h>
14 //#include <stdio.h>
15 //#include <unistd.h>
16 //#include <pthread.h>
17 
23 {
24 
25 }
26 
28 {
29 }
30 
31 
32 /********************/
34 /********************/
35 
43 int OneProbeOneReply::_udp(GeneralOption* option, ProbeReply* reply, std::string dst_ip_addr)
44 {
45  if (option->verbose_mode())
46  {
47  std::cout << "UDP: send probe to destination IP " << dst_ip_addr << std::endl;
48  }
49  int socketfd[1];
50  char pkt_buf[MAX_PACKET];
51  char rcv_buf[MAX_PACKET];
52  struct sockaddr_in dst_addr;
53  struct sockaddr_in src_addr;
54  struct timeval tv;
55  u_int8_t retry = 0;
56 
57  bool done = false;
58  bool error = false; //if true exit
59 
60  memset(rcv_buf,0,sizeof(rcv_buf));
61 
62  uint32 dst_addr_raw= Util::string2number(dst_ip_addr);
63 
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());
67 
68  reply->reset();
69 
70  if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
71  {
72  perror("UDP: error in ICMP socket creation");
73 
74  reply->set_scan_result(ERROR);
75  return ERROR;
76  }
77 
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)
81  {
82  perror("UDP: setsockopt SO_RCVTIMEO - ");
83  reply->set_scan_result(ERROR);
84  return ERROR;
85  }
86 
87  while (retry <= option->retries() && !done && !error) {
88 
89  //send
90  memset(&pkt_buf, '0', UDP_SIZE);
91 
92  //critical zone
93  pthread_mutex_t lock = option->lock();
94  pthread_mutex_lock(&lock);
95 
96  int ttl = option->ttl();
97 
98  if (setsockopt(option->socketudp(), IPPROTO_IP, IP_TTL, &ttl, sizeof(option->ttl())) < 0)
99  {
100  if (option->verbose_mode())
101  std::cout << "UDP error while setting TTL option" << std::endl;
102  perror("General UDP socket - setsockopt");
103 
104  fflush(stdout);
105  close(socketfd[ICMP]);
106 
107  reply->set_scan_result(ERROR);
108  return ERROR;
109  }
110 
111  if (setsockopt(option->socketudp(), IPPROTO_IP, IP_OPTIONS, NULL, 0) < 0)
112  {
113  if (option->verbose_mode())
114  std::cout << "UDP error while UNsetting prespecified timestamp option" << std::endl;
115  perror("UDP error while UNsetting prespecified timestamp option\n");
116 
117  fflush(stdout);
118  close(socketfd[ICMP]);
119 
120  reply->set_scan_result(ERROR);
121  return ERROR;
122  }
123 
124  if (sendto(option->socketudp(), pkt_buf, UDP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr)) < 0)
125  {
126  perror("UDP-probe: error in sendto UDP");
127  close(socketfd[ICMP]);
128 
129  reply->set_scan_result(ERROR);
130  return ERROR;
131  }
132 
133  pthread_mutex_unlock(&lock);
134  //end critical zone
135 
136  // set the ending time
137  int seconds = (option->timeout()/1000);
138  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
139 
140  do
141  {
142  socklen_t lsock = sizeof(dst_addr);
143  int pkt_len;
144  memset(rcv_buf, 0, sizeof (rcv_buf));
145 
146  if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
147  {
148  //timeout expired
149  if (option->verbose_mode())
150  std::cout << "[" << dst_ip_addr << "] UDP_TIMEOUT - Try #" << retry+1 << std::endl;
151  retry ++;
152  break;
153  }
154 
155  // packet received
156  reply->set_probe(rcv_buf);
157 
158  // which type of ICMP packet is?
159  if (reply->src_addr_raw() == dst_addr_raw && (reply->code() == ICMP_DEST_UNREACH || reply->code() == ICMP_TIME_EXCEEDED))
160  {
161  if (reply->scan_result() != ICMP_PORT_UNREACH || reply->scan_result() != ICMP_TIME_EXCEEDED)
162  {
163  if (option->verbose_mode())
164  std::cout << "[" << dst_ip_addr << "] UDP " << reply->scan_result() <<" from " << reply->src_addr() << std::endl;
165  error = true;
166  }
167  else
168  {
169  if (option->verbose_mode())
170  std::cout << "[" << dst_ip_addr << "] UDP " << reply->scan_result() <<" from " << reply->src_addr() << std::endl;
171  done = true;
172  }
173  }
174  //check the timeout
175  else if (!done && (clock() > endwait))
176  {
177  if (option->verbose_mode())
178  std::cout << "[" << dst_ip_addr << "] UDP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
179  retry++;
180  break;
181  }
182  }
183  while(!done && !error);
184 
185  if (error)
186  break;
187 
188  }//while retry
189 
190  close(socketfd[ICMP]);
191 
192  return reply->scan_result();
193 }
194 
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)
207 {
208  if (option->verbose_mode())
209  {
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;
212  }
213 
214  int socketfd[1];
215  char pkt_buf[MAX_PACKET];
216  char rcv_buf[MAX_PACKET];
217  struct sockaddr_in dst_addr;
218  struct sockaddr_in src_addr;
219  struct timeval tv;
220 
221  uint8 retry = 0;
222 
223  bool done = false;
224  bool error = false;
225 
226  memset(rcv_buf, 0, sizeof (rcv_buf));
227 
228  uint32 dst_addr_raw= Util::string2number(dst_ip_addr);
229 
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());
233 
234  reply->reset();
235 
236  if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)
237  {
238  if (option->verbose_mode())
239  std::cout << "UDPTS error in ICMP socket creation" << std::endl;
240  perror("UDPTS error in ICMP socket creation");
241 
242  error = true;
243  reply->set_scan_result(ERROR);
244  return ERROR;
245  }
246 
247  tv.tv_sec = option->timeout() / 1000;
248  tv.tv_usec = (option->timeout() % 1000) * 1000;
249 
250  if (setsockopt(socketfd[ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv)) < 0)
251  {
252  if (option->verbose_mode())
253  std::cout << "Setsock PROB" << std::endl;
254  perror("UDPTS-PL setsockopt");
255 
256  reply->set_scan_result(ERROR);
257  return ERROR;
258  }
259 
260  pthread_mutex_t lock;
261  unsigned char tspace[4 + 8*NIPS];
262  TSOption* tsOpt;
263 
264  while (retry < option->retries() && !done && !error)
265  {
266  //send
267  memset(&pkt_buf, '0', UDP_SIZE);
268 
269  //critical zone
270  lock = option->lock();
271  pthread_mutex_lock(&lock);
272 
273 
274  memset(tspace, 0, sizeof(tspace));
275 
276  tsOpt = (TSOption*)tspace;
277 
278  tsOpt->type = 68;
279  tsOpt->length = 36;
280  tsOpt->pointer = 5;
281  tsOpt->overflag = 3;
282 
283  tsOpt->ip1 = Util::string2number(ts_addr_1);
284  tsOpt->ip2 = Util::string2number(ts_addr_2);
285  tsOpt->ip3 = Util::string2number(ts_addr_3);
286  tsOpt->ip4 = Util::string2number(ts_addr_4);
287 
288  if (setsockopt(option->socketudp(), IPPROTO_IP, IP_OPTIONS, tspace, sizeof (tspace)) < 0) {
289  if (option->verbose_mode())
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");
292 
293  fflush(stdout);
294  close(socketfd[ICMP]);
295 
296  reply->set_scan_result(ERROR);
297  return ERROR;
298  }
299 
300  int s_to = sendto(option->socketudp(), pkt_buf, UDP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr));
301 
302  if (s_to < 0) {
303  if (option->verbose_mode())
304  std::cout << "[" << dst_ip_addr << "] UDP_TS error in sendto" << std::endl;
305  perror("UDP_TS error in sendto");
306 
307  close(socketfd[ICMP]);
308  error = true;
309 
310  reply->set_scan_result(ERROR);
311  return ERROR;
312  }
313 
314  pthread_mutex_unlock(&lock);
315  // end critical zone
316 
317  // set the ending time
318  int seconds = (option->timeout()/1000);
319  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
320 
321  do
322  {
323  socklen_t lsock = sizeof(dst_addr);
324 
325  memset(rcv_buf, 0, sizeof (rcv_buf));
326 
327  int pkt_len;
328  pkt_len = recvfrom(socketfd[ICMP], &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock);
329 
330  if ( pkt_len < 0) {
331  if (option->verbose_mode())
332  std::cout << "[" << dst_ip_addr << "] UDP_TS TIMEOUT [1] - Try #" << retry+1 << std::endl;
333 
335 
336  retry++;
337  break;
338  }
339 
340  // packet received
341  reply->set_probe(rcv_buf);
342 
343  // which type of ICMP packet is?
344  if (reply->src_addr_raw() == dst_addr_raw && reply->code() == ICMP_DEST_UNREACH)
345  {
346  if (reply->scan_result() != ICMP_PORT_UNREACH)
347  {
348  if (option->verbose_mode())
349  std::cout << "[" << dst_ip_addr << "] UDP_TS " << reply->scan_result() << " (type/code = " << reply->code() << "/" << reply->subcode() << ") from " << reply->src_addr() << std::endl;
350  error = true;
351  }
352  else
353  {
354  if (option->verbose_mode())
355  std::cout << "[" << dst_ip_addr << "] UDP_TS " << reply->scan_result() <<" from " << reply->src_addr() << std::endl;
356  done = true;
357  }
358  }
359  else if (!done && (clock() > endwait))
360  {
361  if (option->verbose_mode())
362  std::cout << "[" << dst_ip_addr << "] UDP_TS TIMEOUT [2] - Try #" << retry+1 << std::endl;
363  retry++;
364  break;
365  }
366  }
367  while (!done && !error);
368 
369  if (error)
370  break;
371 
372  }//endwhile
373 
374  close(socketfd[ICMP]);
375  return reply->scan_result();
376 }
377 
385 int OneProbeOneReply::_icmp(GeneralOption *option, ProbeReply *reply, std::string dst_ip_addr)
386 {
387  if (option->verbose_mode())
388  std::cout << "ICMP: send probe to destination IP " << dst_ip_addr << std::endl;
389 
390  int socketfd;
391  char pkt_buf[MAX_PACKET];
392  struct sockaddr_in dst_addr;
393  struct sockaddr_in src_addr;
394  struct icmp *icmp_buf;
395  struct timeval tv;
396  u_int8_t retry = 0;
397 
398  bool done = false;
399 
400  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
401 
402  dst_addr.sin_family = AF_INET;
403  dst_addr.sin_addr.s_addr = dst_addr_raw;
404 
405  reply->reset();
406 
407  if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
408  {
409  perror("Ping: error in socket creation");
410 
411  reply->set_scan_result(ERROR);
412  return ERROR;
413  }
414 
415  tv.tv_sec = option->timeout() / 1000;
416  tv.tv_usec = (option->timeout() % 1000) * 1000;
417 
418  if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv)) < 0)
419  {
420  perror("Ping: setsockopt SO_RCVTIMEO:");
421  }
422 
423  while (retry <= option->retries() && !done)
424  {
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);
432 
433  if (sendto(socketfd, icmp_buf, ICMP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr)) < 0)
434  {
435  perror("ICMP error in sendto UDP");
436  close(socketfd);
437 
438  reply->set_scan_result(ERROR);
439  return ERROR;
440  }
441  // set the ending time
442 
443  int seconds = (option->timeout()/1000);
444  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
445 
446  do
447  {
448  socklen_t lsock = sizeof(dst_addr);
449  int pkt_len;
450 
451  if ((pkt_len = recvfrom(socketfd, &pkt_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
452  {
453  retry++;
454  break;
455  }
456 
457  reply->set_probe(pkt_buf);
458 
459  if (reply->code() == ICMP_ECHOREPLY && reply->src_addr_raw() == dst_addr_raw)
460  {
461  if (option->verbose_mode())
462  std::cout << "[" << dst_ip_addr << "] ICMP_ECHOREPLY (" << pkt_len << " bytes)" << std::endl;
463  done = true;
464  }
465  //check the timeout
466  else if (!done && (clock() > endwait))
467  {
468  if (option->verbose_mode())
469  std::cout << "[" << dst_ip_addr << "] ICMP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
470  retry++;
471  break;
472  }
473  }
474  while (!done);
475  }
476  close(socketfd);
477 
478  return reply->scan_result();
479 }
480 
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)
493 {
494  if (option->verbose_mode())
495  {
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;
498  }
499 
500  int socketfd = 0;
501  char pkt_buf[MAX_PACKET];
502  char rcv_buf[MAX_PACKET];
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;
508  struct timeval tv;
509  unsigned int retry = 0;
510 
511  bool done = false;
512  bool error = false;
513 
514  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
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());
518 
519  reply->reset();
520 
521  if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1) {
522  if (option->verbose_mode())
523  std::cout << "ICMP_TS: error in socket creation" << std::endl;
524  perror("ICMP_TS: error in socket creation");
525 
526  error = true;
527  reply->set_scan_result(ERROR);
528  return ERROR;
529  }
530 
531  tv.tv_sec = option->timeout() / 1000;
532  tv.tv_usec = (option->timeout() % 1000) * 1000;
533 
534  //setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv));
535  if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv)) < 0)
536  {
537  if (option->verbose_mode())
538  std::cout << "Setsock PROB" << std::endl;
539  perror("ICMP_TS setsockopt");
540 
541  reply->set_scan_result(ERROR);
542  return ERROR;
543  }
544 
545  pthread_mutex_t lock;
546  unsigned char tspace[4 + 8*NIPS];
547  memset(tspace, 0, sizeof(tspace));
548  TSOption* tsOpt;
549 
550  while (retry < option->verbose_mode() && !done && !error)
551  {
552  //critical zone
553  lock = option->lock();
554  pthread_mutex_lock(&lock);
555 
556  tsOpt = (TSOption*)tspace;
557 
558  tsOpt->type = 68;
559  tsOpt->length = 36;
560  tsOpt->pointer = 5;
561  tsOpt->overflag = 3;
562 
563  tsOpt->ip1 = Util::string2number(ts_addr_1);
564  tsOpt->ip2 = Util::string2number(ts_addr_2);
565  tsOpt->ip3 = Util::string2number(ts_addr_3);
566  tsOpt->ip4 = Util::string2number(ts_addr_4);
567 
568  if (setsockopt(socketfd, IPPROTO_IP, IP_OPTIONS, tspace, sizeof (tspace)) < 0)
569  {
570  if (option->verbose_mode())
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");
573 
574  fflush(stdout);
575  close(socketfd);
576 
577  error = true;
578  reply->set_scan_result(ERROR);
579  return ERROR;
580  }
581 
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);
589 
590  int s_to = sendto(socketfd,icmp_buf, ICMP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr));
591 
592  if (s_to < 0)
593  {
594  if (option->verbose_mode())
595  std::cout << "ICMP_TS: error in sendto";
596  perror("ICMP_TS: error in sendto");
597 
598  close(socketfd);
599  error = true;
600  reply->set_scan_result(ERROR);
601  }
602 
603  pthread_mutex_unlock(&lock);
604  // end critical zone
605 
606  // set the ending time
607  int seconds = (option->timeout()/1000);
608  clock_t endwait = clock() + seconds * CLOCKS_PER_SEC ;
609 
610  do
611  {
612  // receive
613 
614  socklen_t lsock = sizeof(dst_addr);
615  memset(rcv_buf, 0, sizeof (rcv_buf));
616 
617  int pkt_len;
618  pkt_len = recvfrom(socketfd, &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock);
619 
620  if (pkt_len < 0)
621  {
622  retry++;
624  break;
625  }
626 
627  reply->set_probe(rcv_buf);
628 
629  if (reply->code() == ICMP_ECHOREPLY && reply->src_addr_raw() == dst_addr_raw) {
630 
631  if (! reply->options())
632  {
633  if (option->verbose_mode())
634  std::cout << "[" << dst_ip_addr << "] ICMP_TS " << reply->scan_result() <<" from " << reply->src_addr() << std::endl;
635  error = true;
636  break;
637  }
638  else
639  {
640  if (option->verbose_mode())
641  std::cout << "[" << dst_ip_addr << "] ICMP_TS " << reply->scan_result() <<" from " << reply->src_addr() << std::endl;
642  done = true;
643  }
644  }
645  else
646  {
647  if (option->verbose_mode())
648  std::cout << "Original message with different option [" << dst_ip_addr << "]" << std::endl;
649  done = true;
650  }
651  if (!done && (clock() > endwait))
652  {
653  if (option->verbose_mode())
654  std::cout << "[" << dst_ip_addr << "] ICMPTS_TIMEOUT[2] - Try #" << retry+1 << std::endl;
655  retry++;
657  break;
658  }
659  }
660  while (!done);
661  } //endwhile
662 
663  close(socketfd);
664  return reply->scan_result();
665 }
666 
674 int OneProbeOneReply::_tcp(GeneralOption* option, ProbeReply* reply, std::string dst_ip_addr)
675 {
676  if (option->verbose_mode())
677  std::cout << "TCP: send probe to destination IP " << dst_ip_addr << std::endl;
678 
679  int socketfd;
680  char pkt_buf[MAX_PACKET];
681  char rcv_buf[MAX_PACKET];
682 
683  struct sockaddr_in dst_addr;
684  struct sockaddr_in src_addr;
685  struct tcphdr *tcp_buf;
686  struct timeval tv;
687  u_int8_t retry = 0;
688 
689  bool done = false;
690 
691  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
692  dst_addr.sin_family = AF_INET;
693  dst_addr.sin_addr.s_addr = dst_addr_raw;
694 
695  reply->reset();
696 
697  //socket TCP
698  if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0) {
699  perror("TCP: error in TCP socket creation");
700 
701  reply->set_scan_result(ERROR);
702  return ERROR;
703  }
704 
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)
708  {
709  perror("TCP: setsockopt SO_RCVTIMEO: ");
710  }
711  uint32 src_addr_raw = 0;
712  if (_retrieve_src_addr(src_addr_raw, option) < 0)
713  {
714  reply->set_scan_result(ERROR);
715  return ERROR;
716  }
717 
718  while (retry < option->retries() && !done)
719  {
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;
726  // specify only syn flag
727  tcp_buf->syn = 1;
728  tcp_buf->window=htons(option->ip_id());
729  tcp_buf->urg_ptr=0;
730  tcp_buf->doff = 5;
731 
732  //create the pseudo header
733  src_addr.sin_addr.s_addr = src_addr_raw;
734 
735  //struct pseudoheader ph;
736  struct pseudoheader ph;
737  ph.sip = (u_int32_t)(dst_addr.sin_addr.s_addr);
738  ph.dip = (u_int32_t)(src_addr.sin_addr.s_addr);
739  ph.zero = 0;
740  ph.protocol = 6;
741  ph.tcplen = htons(20);
742  tcp_buf->check = 0;
743  tcp_buf->check = in_chksum_tcp((unsigned short *)&ph, (unsigned short *)tcp_buf, 20);
744 
745  if (sendto(socketfd, tcp_buf, TCP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr)) < 0)
746  {
747  perror("TCP: error in sendto");
748  close(socketfd);
749 
750  reply->set_scan_result(ERROR);
751  return ERROR;
752  }
753 
754  // set the ending time
755  int seconds = (option->timeout()/1000);
756  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
757 
758  do
759  {
760  socklen_t lsock = sizeof(dst_addr);
761  int pkt_len;
762  memset(rcv_buf, 0, sizeof (rcv_buf));
763 
764  if ((pkt_len = recvfrom(socketfd, &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
765  {
766  if (option->verbose_mode())
767  std::cout << "[" << dst_ip_addr << "] TCP_TIMEOUT - Try #" << retry+1 << std::endl;
768  retry++;
770  break;
771  }
772 
773  reply->set_probe(rcv_buf);
774 
775  if (reply->dst_addr_raw() == src_addr_raw && reply->ip_protocol() == 6)
776  {
777  if ((htons(reply->dst_port()) != 22) || (reply->src_addr_raw() != dst_addr_raw))
778  {
779  if (clock() > endwait)
780  {
781  if (option->verbose_mode())
782  std::cout << "[" << dst_ip_addr << "] TCP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
783  fflush(stdout);
784  retry++;
786  break;
787  }
788  continue;
789  }
790  else
791  {
792  if (option->verbose_mode())
793  std::cout << "[" << dst_ip_addr << "] TCP " << reply->scan_result() << std::endl;
794  }
795  done = true;
796  }
797 
798  else if (clock() > endwait)
799  {
800  if (option->verbose_mode())
801  std::cout << "[" << dst_ip_addr << "] TCP_TIMEOUT[2] - Try #" << retry+1 << std::endl;
803  retry++;
804  break;
805  }
806  }
807  while(!done);
808  } //while retry
809 
810  close(socketfd);
811 
812  return reply->scan_result();
813 }
814 
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)
827 {
828  if (option->verbose_mode())
829  {
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;
832  }
833 
834  int socketfd;
835  char pkt_buf[MAX_PACKET];
836  char rcv_buf[MAX_PACKET];
837  struct sockaddr_in dst_addr;
838  struct sockaddr_in src_addr;
839  struct tcphdr *tcp_buf;
840  struct timeval tv;
841  u_int8_t retry = 0;
842 
843  bool done = false;
844  bool error = false;
845 
846  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
847  dst_addr.sin_family = AF_INET;
848  dst_addr.sin_addr.s_addr = dst_addr_raw;
849 
850  reply->reset();
851 
852  //socket TCP
853  if ((socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0)
854  {
855  if (option->timeout())
856  std::cout << "TCP_TS error in TCP socket creation" << std::endl;
857  perror("TCP_TS error in UDP socket creation");
858 
859  error = true;
860  reply->set_scan_result(ERROR);
861  return ERROR;
862  }
863 
864  tv.tv_sec = option->timeout() / 1000;
865  tv.tv_usec = (option->timeout() % 1000) * 1000;
866 
867  if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv)) < 0)
868  {
869  if (option->verbose_mode())
870  std::cout << "TCP_TS setsockopt SO_RCVTIMEO" << std::endl;
871  perror("TCP_TS setsockopt SO_RCVTIMEO");
872 
873  reply->set_scan_result(ERROR);
874  }
875 
876  pthread_mutex_t lock;
877  unsigned char tspace[4 + 8*NIPS];
878  TSOption* tsOpt;
879 
880  uint32 src_addr_raw = 0;
881  if (_retrieve_src_addr(src_addr_raw, option) < 0)
882  {
883  reply->set_scan_result(ERROR);
884  return ERROR;
885  }
886 
887  while (retry < option->retries() && !done)
888  {
889  //critical zone
890  lock = option->lock();
891  pthread_mutex_lock(&lock);
892 
893  tsOpt = (TSOption*)tspace;
894 
895  tsOpt->type = 68;
896  tsOpt->length = 36;
897  tsOpt->pointer = 5;
898  tsOpt->overflag = 3;
899 
900  tsOpt->ip1 = Util::string2number(ts_addr_1);
901  tsOpt->ip2 = Util::string2number(ts_addr_2);
902  tsOpt->ip3 = Util::string2number(ts_addr_3);
903  tsOpt->ip4 = Util::string2number(ts_addr_4);
904 
905  if (setsockopt(socketfd, IPPROTO_IP, IP_OPTIONS, tspace, sizeof (tspace)) < 0)
906  {
907  if (option->verbose_mode())
908  std::cout << "[" << dst_ip_addr << "] TCP_TS: error while setting prespecified timestamp option" << std::endl;
909 
910  perror("TCP_TS error while setting prespecified timestamp option");
911  close(socketfd);
912 
913  reply->set_scan_result(ERROR);
914  return ERROR;
915  }
916 
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;
923  //specify only syn flag
924  tcp_buf->syn = 1;
925  tcp_buf->window=htons(option->ip_id());
926  tcp_buf->urg_ptr=0;
927  tcp_buf->doff = 5;
928 
929  //create the pseudo header
930  src_addr.sin_addr.s_addr = src_addr_raw;
931 
932  struct pseudoheader ph;
933  ph.sip = (uint32)(dst_addr.sin_addr.s_addr);
934  ph.dip = (u_int32_t)(src_addr.sin_addr.s_addr);
935  ph.zero = 0;
936  ph.protocol = 6;
937  ph.tcplen = htons(20);
938  tcp_buf->check = 0;
939  tcp_buf->check = in_chksum_tcp((unsigned short *)&ph, (unsigned short *)tcp_buf, 20);
940 
941  if (sendto(socketfd, tcp_buf, TCP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr)) < 0)
942  {
943  if (option->verbose_mode())
944  std::cout << "[" << dst_ip_addr << "] TCP_TS error in sendto" << std::endl;
945 
946  perror("TCP_TS error in sendto");
947  close(socketfd);
948  reply->set_scan_result(ERROR);
949  return ERROR;
950  }
951 
952  pthread_mutex_unlock(&lock);
953  // end critical zone
954 
955  // set the ending time
956  int seconds = option->timeout()/1000;
957  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
958 
959  do
960  {
961  socklen_t lsock = sizeof(dst_addr);
962  int pkt_len;
963  memset(rcv_buf, 0, sizeof (rcv_buf));
964 
965  if ((pkt_len = recvfrom(socketfd, &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
966  {
967  if (option->verbose_mode())
968  std::cout << "[" << dst_ip_addr << "] TCP_TS TIMEOUT - Try #" << retry+1 << std::endl;
969  retry++;
971  break;
972  }
973 
974  reply->set_probe(rcv_buf);
975 
976  if (reply->src_addr_raw() == dst_addr_raw && reply->ip_protocol() == 6)
977  {
978  if (option->verbose_mode())
979  std::cout << "[" << dst_ip_addr << "] TCP_TS (" << pkt_len << " bytes)" << std::endl;
980 
981  done = true;
982  }
983  else if (!done && (clock() > endwait))
984  {
985  if (option->verbose_mode())
986  std::cout << "[" << dst_ip_addr << "] TCP_TS TIMEOUT[2] - Try #" << retry+1 << std::endl;
988  retry++;
989  break;
990  }
991  else
992  {
993  if (option->verbose_mode())
994  std::cout << "[" << dst_ip_addr << "] TCP_TS " << reply->scan_result() << std::endl;
996  retry++;
997  break;
998  }
999  }
1000  while(!done);
1001  }//while retry
1002 
1003  close(socketfd);
1004  return reply->scan_result();
1005 }
1006 
1007 int OneProbeOneReply::_protocol(GeneralOption* option, ProbeReply* reply, std::string dst_ip_addr)
1008 {
1009  if (option->verbose_mode())
1010  {
1011  std::cout << "PROTOCOL: send probe to destination IP " << dst_ip_addr << std::endl;
1012  }
1013 
1014  int socketfd[ICMP];
1015  char pkt_buf[MAX_PACKET];
1016  char rcv_buf[MAX_PACKET];
1017  struct sockaddr_in dst_addr;
1018  struct sockaddr_in src_addr;
1019  struct iphdr *ip_dummy;
1020  struct timeval tv;
1021 
1022  u_int8_t retry = 0;
1023 
1024  bool done = false;
1025  bool error = false;
1026 
1027  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
1028  dst_addr.sin_family = AF_INET;
1029  dst_addr.sin_addr.s_addr = dst_addr_raw;
1030 
1031  reply->reset();
1032 
1033  //socket icmp
1034  if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
1035  {
1036  if (option->verbose_mode())
1037  std::cout << "[" << dst_ip_addr << "] PROTOCOL error in raw socket creation" << std::endl;
1038  perror("PROTOCOL error in raw socket creation");
1039 
1040  reply->set_scan_result(ERROR);
1041  return ERROR;
1042  }
1043 
1044  //set timeout on icmp
1045  tv.tv_sec = option->timeout() / 1000;
1046  tv.tv_usec = (option->timeout() % 1000) * 1000;
1047 
1048  if ((setsockopt(socketfd[ICMP], SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv))) < 0)
1049  {
1050  if (option->verbose_mode())
1051  std::cout << "PROTOCOL setsockopt SO_RCVTIMEO" << std::endl;
1052  perror("PROTOCOL setsockopt ICMP RO_RCVTIMEO");
1053  }
1054 
1055  uint32 src_addr_raw = 0;
1056  if (_retrieve_src_addr(src_addr_raw, option) < 0)
1057  {
1058  reply->set_scan_result(ERROR);
1059  return ERROR;
1060  }
1061 
1062  int ttl = option->ttl();
1063  if (setsockopt(option->socketudp(), IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0)
1064  {
1065  if (option->verbose_mode())
1066  std::cout << "UDP error while setting TTL option" << std::endl;
1067  perror("General UDP socket - setsockopt");
1068 
1069  fflush(stdout);
1070  close(socketfd[ICMP]);
1071 
1072  reply->set_scan_result(ERROR);
1073  return ERROR;
1074  }
1075 
1076  if (setsockopt(option->socketudp(), IPPROTO_IP, IP_OPTIONS, NULL, 0) < 0)
1077  {
1078  if (option->verbose_mode())
1079  std::cout << "UDP error while UNsetting prespecified timestamp option" << std::endl;
1080  perror("UDP error while UNsetting prespecified timestamp option\n");
1081 
1082  fflush(stdout);
1083  close(socketfd[ICMP]);
1084 
1085  reply->set_scan_result(ERROR);
1086  return ERROR;
1087  }
1088 
1089  pthread_mutex_t lock;
1090 
1091  while (retry < option->retries() && !done && !error)
1092  {
1093  //critical zone
1094  lock = option->lock();
1095  pthread_mutex_lock(&lock);
1096 
1097  //craft ip dummy
1098  memset(pkt_buf, 0, sizeof (pkt_buf));
1099 
1100  ip_dummy = (struct iphdr *) pkt_buf;
1101  ip_dummy->ihl = 5;
1102  ip_dummy->version = 4;
1103  ip_dummy->tos = 0;
1104  ip_dummy->tot_len = IP_SIZE + UDP_SIZE;
1105  //ip_dummy->id = htons(1016);
1106  ip_dummy->id = htons(option->ip_id());
1107  ip_dummy->ttl = option->ttl();
1108  ip_dummy->protocol = option->protocol_number();
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); //considera 24 tanto sono 4 byte di zeri
1113  ip_dummy->check = newcksum;
1114 
1115  //send protocol probe
1116  int s_to = sendto(option->socketudp(), pkt_buf, IP_SIZE + UDP_SIZE, 0, (struct sockaddr*) &dst_addr, sizeof(dst_addr));
1117  if (s_to < 0)
1118  {
1119  if (option->verbose_mode())
1120  std::cout << "[" << dst_ip_addr << "] PROTOCOL error in sendto" << std::endl;
1121  perror("PROTOCOL error in sendto");
1122 
1123  close(socketfd[ICMP]);
1124  reply->set_scan_result(ERROR);
1125  return ERROR;
1126  }
1127 
1128  pthread_mutex_unlock(&lock);
1129  //end critical zone
1130 
1131  // set the ending time
1132  int seconds = option->timeout()/1000;
1133  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC;
1134 
1135  do
1136  {
1137  socklen_t lsock = sizeof(dst_addr);
1138  int pkt_len;
1139  memset(rcv_buf, 0, sizeof (rcv_buf));
1140 
1141  //receive
1142  if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
1143  {
1144  if (option->verbose_mode())
1145  std::cout << "[" << dst_ip_addr << "] PROTOCOL TIMEOUT - Try #" << retry+1 << std::endl;
1146 
1147  reply->set_scan_result(NO_RESPONSE);
1148  retry++;
1149  break;
1150  }
1151 
1152  reply->set_probe(rcv_buf);
1153 
1154 
1155  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())
1156  {
1157  if (reply->subcode() != ICMP_PROT_UNREACH )
1158  {
1159  if (option->verbose_mode())
1160  std::cout << "[" << dst_ip_addr << "] PROTOCOL " << reply->scan_result() << " from " << reply->dst_addr() << std::endl;
1161  error = true;
1162  }
1163  else
1164  {
1165  if (option->verbose_mode())
1166  std::cout << "[" << dst_ip_addr << "] PROTOCOL " << reply->scan_result() << " from " << reply->dst_addr() << std::endl;
1167  done = true;
1168  }
1169  }
1170  //check the timeout
1171  else if (clock() > endwait)
1172  {
1173  if (option->verbose_mode())
1174  std::cout << "[" << dst_ip_addr << "] PROTOCOL TIMEOUT[2] - Try #" << retry+1 << std::endl;
1175 
1176  retry++;
1177  break;
1178  }
1179  }
1180  while(!done && !error);
1181  if (error)
1182  break;
1183  }//while retry
1184 
1185  close(socketfd[ICMP]);
1186  return reply->scan_result();
1187 }
1188 
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)
1201 {
1202  if (option->verbose_mode())
1203  {
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;
1206  }
1207 
1208  struct sockaddr_in src_addr;
1209 
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());
1213 
1214  int socketfd[ICMP];
1215  char pkt_buf[MAX_PACKET];
1216  char rcv_buf[MAX_PACKET];
1217  struct sockaddr_in dst_addr;
1218  struct iphdr *ip_dummy;
1219  struct timeval tv;
1220 
1221  u_int8_t retry = 0;
1222 
1223  bool done = false;
1224  bool error = false;
1225 
1226  uint32 dst_addr_raw = Util::string2number(dst_ip_addr);
1227  dst_addr.sin_family = AF_INET;
1228  dst_addr.sin_addr.s_addr = dst_addr_raw;
1229 
1230  reply->reset();
1231 
1232  //socket icmp
1233  if ((socketfd[ICMP] = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) <0)
1234  {
1235  if (option->verbose_mode())
1236  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS error in raw socket creation" << std::endl;
1237  perror("PROTOCOL_TS error in raw socket creation");
1238 
1239  reply->set_scan_result(ERROR);
1240  return ERROR;
1241  }
1242 
1243  //set timeout on icmp
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)
1247  {
1248  if (option->verbose_mode())
1249  std::cout << "PROTOCOL_TS setsockopt SO_RCVTIMEO" << std::endl;
1250  perror("PROTOCOL_TS setsockopt ICMP SO_RVTIMEO: ");
1251  }
1252 
1253  uint32 src_addr_raw = 0;
1254  if (_retrieve_src_addr(src_addr_raw, option) < 0)
1255  {
1256  reply->set_scan_result(ERROR);
1257  return ERROR;
1258  }
1259 
1260  pthread_mutex_t lock;
1261  unsigned char tspace[4 + 8*NIPS];
1262  TSOption* tsOpt;
1263 
1264  while (retry < option->retries() && !done && !error)
1265  {
1266  //crafting ip dummy
1267  memset(pkt_buf, 0, sizeof (pkt_buf));
1268  ip_dummy = (struct iphdr *) pkt_buf;
1269  ip_dummy->ihl = 14;
1270  ip_dummy->version = 4;
1271  ip_dummy->tos = 0;
1272  ip_dummy->tot_len = IP_SIZE_TS + UDP_SIZE;
1273  ip_dummy->id = htons(1016);
1274  ip_dummy->ttl= option->ttl();
1275  ip_dummy->protocol= option->protocol_number();
1276  ip_dummy->saddr= src_addr_raw;
1277  ip_dummy->daddr= dst_addr_raw;
1278  ip_dummy->check=0;
1279 
1280  //critical zone
1281  lock = option->lock();
1282  pthread_mutex_lock(&lock);
1283 
1284  tsOpt = (TSOption*)tspace;
1285 
1286  tsOpt->type = 68;
1287  tsOpt->length = 36;
1288  tsOpt->pointer = 5;
1289  tsOpt->overflag = 3;
1290 
1291  tsOpt->ip1 = Util::string2number(ts_addr_1);
1292  tsOpt->ip2 = Util::string2number(ts_addr_2);
1293  tsOpt->ip3 = Util::string2number(ts_addr_3);
1294  tsOpt->ip4 = Util::string2number(ts_addr_4);
1295 
1296  memcpy(&(pkt_buf[20]), tspace, sizeof(tspace));
1297 
1298  unsigned short newcksum = cksum((unsigned short *)pkt_buf, IP_SIZE_TS);
1299  ip_dummy->check = newcksum;
1300 
1301  //send probe
1302  if (sendto(option->socketudp(), pkt_buf, IP_SIZE_TS + UDP_SIZE, 0, (struct sockaddr *) &dst_addr, sizeof(dst_addr)) < 0)
1303  {
1304  if (option->verbose_mode())
1305  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS error in sendto" << std::endl;
1306  perror("PROTOCOL_TS error in sendto");
1307  close(socketfd[ICMP]);
1308  reply->set_scan_result(ERROR);
1309  return ERROR;
1310  }
1311 
1312  pthread_mutex_unlock(&lock);
1313  // end critical zone
1314 
1315  // set the ending time
1316  int seconds = option->timeout()/1000;
1317  clock_t endwait = clock () + seconds * CLOCKS_PER_SEC ;
1318 
1319  do
1320  {
1321  socklen_t lsock = sizeof(dst_addr);
1322  int pkt_len;
1323  memset(rcv_buf, 0, sizeof (rcv_buf));
1324 
1325  //receive
1326  if ((pkt_len = recvfrom(socketfd[ICMP], &rcv_buf, MAX_PACKET, 0, (struct sockaddr *) &src_addr, &lsock)) < 0)
1327  {
1328  if (option->verbose_mode())
1329  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS TIMEOUT - Try #" << retry+1 << std::endl;
1330  reply->set_scan_result(NO_RESPONSE);
1331  retry++;
1332  break;
1333  }
1334 
1335  //packet received
1336  reply->set_probe(rcv_buf);
1337 
1338  //analyse result
1339  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())
1340  {
1341  if (reply->subcode() == ICMP_PROT_UNREACH)
1342  {
1343  if (!reply->options())
1344  {
1345  if (option->verbose_mode())
1346  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS " << reply->scan_result() << " from " << reply->dst_addr() << std::endl;
1347  error = true;
1348  break;
1349  }
1350  done = true;
1351  if (option->verbose_mode())
1352  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS " << reply->scan_result() << " from " << reply->dst_addr() << std::endl;
1353 
1354  }
1355  else
1356  {
1357  if (option->verbose_mode())
1358  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS " << reply->scan_result() << " from " << reply->src_addr() << std::endl;
1359  error = true;
1360  }
1361  }
1362  else if (!done && (clock() > endwait))
1363  {
1364  if (option->verbose_mode())
1365  std::cout << "[" << dst_ip_addr << "] PROTOCOL_TS TIMEOUT[2] - Try #" << retry+1 << std::endl;
1366  retry++;
1367  reply->set_scan_result(NO_RESPONSE);
1368  break;
1369  }
1370 
1371  }
1372  while (!done && !error);
1373 
1374  if (error)
1375  break;
1376 
1377  }
1378 
1379  close(socketfd[ICMP]);
1380  return reply->scan_result();
1381 }
1382 
1389 int OneProbeOneReply::_retrieve_src_addr(uint32& src_addr_raw, GeneralOption* option)
1390 {
1391  int fd;
1392  struct if_nameindex *curif, *ifs;
1393  struct ifreq req;
1394 
1395  if((fd = socket(PF_INET, SOCK_DGRAM, 0)) != -1)
1396  {
1397  ifs = if_nameindex();
1398  if(ifs)
1399  {
1400  for(curif = ifs; curif && curif->if_name; curif++)
1401  {
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");
1406  else
1407  if ((strcmp(curif->if_name, (option->eth()).c_str())) == 0)
1408  {
1409  src_addr_raw = ((struct sockaddr_in*) &req.ifr_addr)->sin_addr.s_addr;
1410  return 0;
1411  }
1412  }
1413  if_freenameindex(ifs);
1414  if(close(fd)!=0)
1415  perror("Problem arised while detecting eth IP address [closing file]");
1416  }
1417  else
1418  perror("if_nameindex");
1419  }
1420  else
1421  {
1422  perror("socket");
1423  return -1;
1424  }
1425 }
1426 
1427 
1428 /********************/
1430 /********************/
1431 
1440 int OneProbeOneReply::udp(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1441 {
1442  // UDP D
1443  return _udp(option, reply, dst_addr);
1444 }
1445 
1454 int OneProbeOneReply::udp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1455 {
1456  // UDP_TS D|DDDD
1457  return _udp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1458 }
1459 
1469 int OneProbeOneReply::udp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1)
1470 {
1471  // UDP_TS D|XXXX
1472  return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1473 }
1474 
1485 int OneProbeOneReply::udp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2)
1486 {
1487  // UDP_TS D|XYYY
1488  return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1489 }
1490 
1502 int OneProbeOneReply::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)
1503 {
1504  // UDP_TS D|XYZZ
1505  return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1506 }
1507 
1520 int OneProbeOneReply::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)
1521 {
1522  // UDP_TS D|XYZW
1523  return _udp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1524 }
1525 
1534 int OneProbeOneReply::icmp(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1535 {
1536  // ICMP D
1537  return _icmp(option, reply, dst_addr);
1538 }
1539 
1548 int OneProbeOneReply::icmp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1549 {
1550  // ICMP_TS D|DDDD
1551  return _icmp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1552 }
1553 
1563 int OneProbeOneReply::icmp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1)
1564 {
1565  // send ICMP_TS to D|XXXX
1566  return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1567 }
1568 
1579 int OneProbeOneReply::icmp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2)
1580 {
1581  // send ICMP_TS to D|XYYY
1582  return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1583 }
1584 
1596 int OneProbeOneReply::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)
1597 {
1598  // send ICMP_TS to D|XYZZ
1599  return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1600 }
1601 
1614 int OneProbeOneReply::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)
1615 {
1616  // send ICMP_TS to D|XYZW
1617  return _icmp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1618 }
1619 
1628 int OneProbeOneReply::tcp(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1629 {
1630  return _tcp(option, reply, dst_addr);
1631 
1632 }
1633 
1642 int OneProbeOneReply::tcp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1643 {
1644  // send TCP_TS to D|DDDD
1645  return _tcp_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1646 }
1647 
1657 int OneProbeOneReply::tcp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1)
1658 {
1659  // send TCP_TS to D|XXXX
1660  return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1661 }
1662 
1673 int OneProbeOneReply::tcp_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2)
1674 {
1675  // send TCP_TS to D|XYYY
1676  return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1677 }
1678 
1690 int OneProbeOneReply::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)
1691 {
1692  // send TCP_TS to D|XYZZ
1693  return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1694 }
1695 
1708 int OneProbeOneReply::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)
1709 {
1710  // send TCP_TS to D|XYZW
1711  return _tcp_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1712 }
1713 
1722 int OneProbeOneReply::protocol(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1723 {
1724  // send PROTOCOL to D
1725  return _protocol(option, reply, dst_addr);
1726 }
1727 
1736 int OneProbeOneReply::protocol_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr)
1737 {
1738  // send PROTOCOL_TS to D|DDDD
1739  return _protocol_ts(option, reply, dst_addr, dst_addr, dst_addr, dst_addr, dst_addr);
1740 }
1741 
1751 int OneProbeOneReply::protocol_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1)
1752 {
1753  // send PROTOCOL_TS to D|XXXX
1754  return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_1, ts_addr_1, ts_addr_1);
1755 }
1756 
1767 int OneProbeOneReply::protocol_ts(GeneralOption* option, ProbeReply* reply, std::string dst_addr, std::string ts_addr_1, std::string ts_addr_2)
1768 {
1769  // send PROTOCOL_TS to D|XYYY
1770  return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_2, ts_addr_2);
1771 }
1772 
1784 int OneProbeOneReply::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)
1785 {
1786  // send PROTOCOL_TS to D|XYZZ
1787  return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_3);
1788 }
1789 
1802 int OneProbeOneReply::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)
1803 {
1804  // send PROTOCOL_TS to D|XYZW
1805  return _protocol_ts(option, reply, dst_addr, ts_addr_1, ts_addr_2, ts_addr_3, ts_addr_4);
1806 }