home *** CD-ROM | disk | FTP | other *** search
/ Steganos Hacker Tools / SHT151.iso / programme / scanner / nmapNTsp1 / Win_2000.exe / nmapNT-src / tcpip.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-07  |  39.2 KB  |  1,324 lines

  1. #include "tcpip.h"
  2.  
  3. extern struct ops o;
  4.  
  5. #ifndef WIN32
  6. /* Globals */
  7. int jumpok = 0;
  8. static jmp_buf jmp_env;
  9. /* Sig_ALRM handler */
  10. void sig_alarm(int signo) {
  11. if (jumpok)
  12.   longjmp(jmp_env, 1);
  13. return;
  14. }
  15. #endif
  16.  
  17. inline void sethdrinclude(int sd) {
  18. #ifdef IP_HDRINCL
  19. int one = 1;
  20. setsockopt(sd, IPPROTO_IP, IP_HDRINCL, (void *) &one, sizeof(one));
  21. #endif
  22. }
  23.  
  24. /* Tests whether a packet sent to  IP is LIKELY to route 
  25.  through the kernel localhost interface */
  26. int islocalhost(struct in_addr *addr) {
  27. char dev[128];
  28.   /* If it is 0.0.0.0 or starts with 127.0.0.1 then it is 
  29.      probably localhost */
  30.   if ((addr->s_addr & htonl(0xFF000000)) == htonl(0x7F000000))
  31.     return 1;
  32.  
  33.   if (!addr->s_addr)
  34.     return 1;
  35.  
  36.   /* If it is the same addy as a local interface, then it is
  37.      probably localhost */
  38.  
  39.   if (ipaddr2devname(dev, addr) != -1)
  40.     return 1;
  41.  
  42.   /* OK, so to a first approximation, this addy is probably not
  43.      localhost */
  44.   return 0;
  45. }
  46.  
  47. /* Calls pcap_open_live and spits out an error (and quits) if the call faile.
  48.    So a valid pcap_t will always be returned. */
  49. pcap_t *my_pcap_open_live(char *device, int snaplen, int promisc, int to_ms) 
  50. {
  51.   char err0r[PCAP_ERRBUF_SIZE];
  52.   pcap_t *pt;
  53.   if (!((pt = pcap_open_live(device, snaplen, promisc, to_ms, err0r)))) {
  54.     fatal("pcap_open_live: %s\nThere are several possible reasons for this, depending on your operating system:\n"
  55.           "LINUX: If you are getting Socket type not supported, try modprobe af_packet or recompile your kernel with SOCK_PACKET enabled.\n"
  56.           "*BSD:  If you are getting device not configured, you need to recompile your kernel with Berkeley Packet Filter support.  If you are getting No such file or directory, try creating the device (eg cd /dev; MAKEDEV <device>; or use mknod).\n"
  57.           "SOLARIS:  If you are trying to scan localhost and getting '/dev/lo0: No such file or directory', complain to Sun.  I don't think Solaris can support advanced localhost scans.  You can probably use \"-P0 -sT localhost\" though.\n\n", err0r);
  58.   }
  59.   return pt;
  60. }
  61.  
  62. /* Standard swiped internet checksum routine */
  63. inline unsigned short in_cksum(unsigned short *ptr,int nbytes) {
  64.  
  65. register int            sum;            /* XXX assumes long == 32 bits */
  66. u_short                 oddbyte;
  67. register u_short        answer;         /* assumes u_short == 16 bits */
  68.  
  69. /*
  70.  * Our algorithm is simple, using a 32-bit accumulator (sum),
  71.  * we add sequential 16-bit words to it, and at the end, fold back
  72.  * all the carry bits from the top 16 bits into the lower 16 bits.
  73.  */
  74.  
  75. sum = 0;
  76. while (nbytes > 1)  {
  77. sum += *ptr++;
  78. nbytes -= 2;
  79. }
  80.  
  81. /* mop up an odd byte, if necessary */
  82. if (nbytes == 1) {
  83. oddbyte = 0;            /* make sure top half is zero */
  84. *((u_char *) &oddbyte) = *(u_char *)ptr;   /* one byte only */
  85. sum += oddbyte;
  86. }
  87.  
  88. /*
  89.  * Add back carry outs from top 16 bits to low 16 bits.
  90.  */
  91.  
  92. sum  = (sum >> 16) + (sum & 0xffff);    /* add high-16 to low-16 */
  93. sum += (sum >> 16);                     /* add carry */
  94. answer = ~sum;          /* ones-complement, then truncate to 16 bits */
  95. return(answer);
  96. }
  97.  
  98.  
  99.  
  100.  
  101. /* Tries to resolve given hostname and stores
  102.    result in ip .  returns 0 if hostname cannot
  103.    be resolved */
  104. int resolve(char *hostname, struct in_addr *ip) {
  105.   struct hostent *h;
  106.  
  107.   if (!hostname || !*hostname)
  108.     fatal("NULL or zero-length hostname passed to resolve()");
  109.  
  110.   if (inet_aton(hostname, ip))
  111.     return 1; /* damn, that was easy ;) */
  112.   if ((h = gethostbyname(hostname))) {
  113.     memcpy(ip, h->h_addr_list[0], sizeof(struct in_addr));
  114.     return 1;
  115.   }
  116.   return 0;
  117. }
  118.  
  119. int send_tcp_raw_decoys( int sd, struct in_addr *victim, unsigned short sport, 
  120.              unsigned short dport, unsigned int seq,
  121.              unsigned int ack, unsigned char flags,
  122.              unsigned short window, char *options, int optlen,
  123.              char *data, unsigned short datalen) 
  124. {
  125.   int decoy;
  126.  
  127.   for(decoy = 0; decoy < o.numdecoys; decoy++) 
  128.     if (send_tcp_raw(sd, &o.decoys[decoy], victim, sport, dport, seq, ack,
  129.              flags, window, options, optlen, data, datalen) == -1)
  130.       return -1;
  131.  
  132.   return 0;
  133. }
  134.  
  135.  
  136. int send_tcp_raw( int sd, struct in_addr *source, 
  137.           struct in_addr *victim, unsigned short sport, 
  138.           unsigned short dport, unsigned int seq,
  139.           unsigned int ack, unsigned char flags,
  140.           unsigned short window, char *options, int optlen,
  141.           char *data, unsigned short datalen) 
  142. {
  143.  
  144. struct pseudo_header { 
  145.   /*for computing TCP checksum, see TCP/IP Illustrated p. 145 */
  146.   unsigned int s_addy;
  147.   unsigned int d_addr;
  148.   char zer0;
  149.   unsigned char protocol;
  150.   unsigned short length;
  151. };
  152. char *packet = safe_malloc(sizeof(struct ip) + sizeof(struct tcphdr) + optlen + datalen);
  153. struct ip *ip = (struct ip *) packet;
  154. struct tcphdr *tcp = (struct tcphdr *) (packet + sizeof(struct ip));
  155. struct pseudo_header *pseudo =  (struct pseudo_header *) (packet + sizeof(struct ip) - sizeof(struct pseudo_header)); 
  156. static int myttl = 0;
  157.  
  158.  /*With these placement we get data and some field alignment so we aren't
  159.    wasting too much in computing the checksum */
  160. int res = -1;
  161. struct sockaddr_in sock;
  162. char myname[MAXHOSTNAMELEN + 1];
  163. struct hostent *myhostent = NULL;
  164. int source_malloced = 0;
  165.  
  166. /* check that required fields are there and not too silly */
  167. /* We used to check that sport and dport were nonzer0, but scr3w that! */
  168. if ( !victim || sd < 0) {
  169.   fprintf(stderr, "send_tcp_raw: One or more of your parameters suck!\n");
  170.   free(packet);
  171.   return -1;
  172. }
  173.  
  174. if (optlen % 4) {
  175.   fatal("send_tcp_raw called with an option length argument of %d which is illegal because it is not divisible by 4", optlen);
  176. }
  177.  
  178.  
  179. if (!myttl) myttl = (get_random_uint() % 23) + 37;
  180.  
  181. /* It was a tough decision whether to do this here for every packet
  182.    or let the calling function deal with it.  In the end I grudgingly decided
  183.    to do it here and potentially waste a couple microseconds... */
  184. sethdrinclude(sd); 
  185.  
  186. /* if they didn't give a source address, fill in our first address */
  187. if (!source) {
  188.   source_malloced = 1;
  189.   source = safe_malloc(sizeof(struct in_addr));
  190.   if (gethostname(myname, MAXHOSTNAMELEN) || 
  191.       !(myhostent = gethostbyname(myname)))
  192.        fatal("Cannot get hostname!  Try using -S <my_IP_address> or -e <interface to scan through>\n");
  193.   memcpy(source, myhostent->h_addr_list[0], sizeof(struct in_addr));
  194. #if ( TCPIP_DEBUGGING )
  195.     printf("We skillfully deduced that your address is %s\n", 
  196.        inet_ntoa(*source));
  197. #endif
  198. }
  199.  
  200.  
  201. /*do we even have to fill out this damn thing?  This is a raw packet, 
  202.   after all */
  203. sock.sin_family = AF_INET;
  204. sock.sin_port = htons(dport);
  205. sock.sin_addr.s_addr = victim->s_addr;
  206.  
  207.  
  208. bzero((char *) packet, sizeof(struct ip) + sizeof(struct tcphdr));
  209.  
  210. pseudo->s_addy = source->s_addr;
  211. pseudo->d_addr = victim->s_addr;
  212. pseudo->protocol = IPPROTO_TCP;
  213. pseudo->length = htons(sizeof(struct tcphdr) + optlen + datalen);
  214.  
  215. tcp->th_sport = htons(sport);
  216. tcp->th_dport = htons(dport);
  217. if (seq) {
  218.   tcp->th_seq = htonl(seq);
  219. }
  220. else if (flags & TH_SYN) {
  221.   get_random_bytes(&(tcp->th_seq), 4);
  222. }
  223.  
  224. if (ack)
  225.   tcp->th_ack = htonl(ack);
  226. /*else if (flags & TH_ACK)
  227.   tcp->th_ack = rand() + rand();*/
  228.  
  229. tcp->th_off = 5 + (optlen /4) /*words*/;
  230. tcp->th_flags = flags;
  231.  
  232. if (window)
  233.   tcp->th_win = htons(window);
  234. else tcp->th_win = htons(1024 * (myttl % 4 + 1)); /* Who cares */
  235.  
  236.  /* We should probably copy the data over too */
  237.  if (data && datalen)
  238.    memcpy(packet + sizeof(struct ip) + sizeof(struct tcphdr) + optlen, data, datalen);
  239.  /* And the options */
  240.  if (optlen) {
  241.    memcpy(packet + sizeof(struct ip) + sizeof(struct tcphdr), options, optlen);
  242.  }
  243.  
  244. #if STUPID_SOLARIS_CHECKSUM_BUG
  245.  tcp->th_sum = sizeof(struct tcphdr) + optlen + datalen; 
  246. #else
  247. tcp->th_sum = in_cksum((unsigned short *)pseudo, sizeof(struct tcphdr) + 
  248.                optlen + sizeof(struct pseudo_header) + datalen);
  249. #endif
  250. /* Now for the ip header */
  251.  
  252. bzero(packet, sizeof(struct ip)); 
  253. ip->ip_v = 4;
  254. ip->ip_hl = 5;
  255. ip->ip_len = BSDFIX(sizeof(struct ip) + sizeof(struct tcphdr) + optlen + datalen);
  256. get_random_bytes(&(ip->ip_id), 2);
  257. ip->ip_ttl = myttl;
  258. ip->ip_p = IPPROTO_TCP;
  259. ip->ip_src.s_addr = source->s_addr;
  260. ip->ip_dst.s_addr= victim->s_addr;
  261. #if HAVE_IP_IP_SUM
  262. ip->ip_sum = in_cksum((unsigned short *)ip, sizeof(struct ip));
  263. #endif
  264.  
  265. if (TCPIP_DEBUGGING > 1) {
  266.   log_write(LOG_STDOUT, "Raw TCP packet creation completed!  Here it is:\n");
  267.   readtcppacket(packet,BSDUFIX(ip->ip_len));
  268. }
  269.  
  270. res = Sendto("send_tcp_raw", sd, packet, BSDUFIX(ip->ip_len), 0,
  271.          (struct sockaddr *)&sock,  (int)sizeof(struct sockaddr_in));
  272.  
  273. if (source_malloced) free(source);
  274. free(packet);
  275. return res;
  276. }
  277.  
  278. inline int Sendto(char *functionname, int sd, char *packet, int len, 
  279.        unsigned int flags, struct sockaddr *to, int tolen) {
  280.  
  281. struct sockaddr_in *sin = (struct sockaddr_in *) to;
  282. int res;
  283. int retries = 0;
  284. int sleeptime = 0;
  285.  
  286. do {
  287.   if (TCPIP_DEBUGGING > 1) {  
  288.     log_write(LOG_STDOUT, "trying sendto(%d, packet, %d, 0, %s, %d)",
  289.        sd, len, inet_ntoa(sin->sin_addr), tolen);
  290.   }
  291.   if ((res = sendto(sd, packet, len, flags, to, tolen)) == -1) {
  292.     error("sendto in %s: sendto(%d, packet, %d, 0, %s, %d) => %s",
  293.       functionname, sd, len, inet_ntoa(sin->sin_addr), tolen,
  294.       strerror(errno));
  295.     if (retries > 2 || errno == EPERM) 
  296.       return -1;
  297.     sleeptime = 15 * (1 << (2 * retries));
  298.     error("Sleeping %d seconds then retrying", sleeptime);
  299.     fflush(stderr);
  300.     sleep(sleeptime);
  301.   }
  302.   retries++;
  303. } while( res == -1);
  304.  
  305. if (TCPIP_DEBUGGING > 1)
  306.   log_write(LOG_STDOUT, "successfully sent %d bytes of raw_tcp!\n", res);
  307.  
  308. return res;
  309. }
  310.  
  311. /* A simple function I wrote to help in debugging, shows the important fields
  312.    of a TCP packet*/
  313. int readtcppacket(char *packet, int readdata) {
  314.  
  315. struct ip *ip = (struct ip *) packet;
  316. struct tcphdr *tcp = (struct tcphdr *) (packet + sizeof(struct ip));
  317. char *data = packet +  sizeof(struct ip) + sizeof(struct tcphdr);
  318. int tot_len;
  319. struct in_addr bullshit, bullshit2;
  320. char sourcehost[16];
  321. int i;
  322. int realfrag = 0;
  323.  
  324. if (!packet) {
  325.   fprintf(stderr, "readtcppacket: packet is NULL!\n");
  326.   return -1;
  327.     }
  328.  
  329. bullshit.s_addr = ip->ip_src.s_addr; bullshit2.s_addr = ip->ip_dst.s_addr;
  330. /* this is gay */
  331. realfrag = BSDFIX(ntohs(ip->ip_off) & 8191 /* 2^13 - 1 */);
  332. tot_len = BSDFIX(ip->ip_len);
  333. strncpy(sourcehost, inet_ntoa(bullshit), 16);
  334. i =  4 * (ntohs(ip->ip_hl) + ntohs(tcp->th_off));
  335. if (ip->ip_p== IPPROTO_TCP) {
  336.   if (realfrag) 
  337.     printf("Packet is fragmented, offset field: %u\n", realfrag);
  338.   else {
  339.     printf("TCP packet: %s:%d -> %s:%d (total: %d bytes)\n", sourcehost, 
  340.        ntohs(tcp->th_sport), inet_ntoa(bullshit2), 
  341.        ntohs(tcp->th_dport), tot_len);
  342.     printf("Flags: ");
  343.     if (!tcp->th_flags) printf("(none)");
  344.     if (tcp->th_flags & TH_RST) printf("RST ");
  345.     if (tcp->th_flags & TH_SYN) printf("SYN ");
  346.     if (tcp->th_flags & TH_ACK) printf("ACK ");
  347.     if (tcp->th_flags & TH_PUSH) printf("PSH ");
  348.     if (tcp->th_flags & TH_FIN) printf("FIN ");
  349.     if (tcp->th_flags & TH_URG) printf("URG ");
  350.     printf("\n");
  351.  
  352.     printf("ttl: %hu ", ip->ip_ttl);
  353.  
  354.     if (tcp->th_flags & (TH_SYN | TH_ACK)) printf("Seq: %u\tAck: %u\n", 
  355.                           (unsigned int) ntohl(tcp->th_seq), (unsigned int) ntohl(tcp->th_ack));
  356.     else if (tcp->th_flags & TH_SYN) printf("Seq: %u\n", (unsigned int) ntohl(tcp->th_seq));
  357.     else if (tcp->th_flags & TH_ACK) printf("Ack: %u\n", (unsigned int) ntohl(tcp->th_ack));
  358.   }
  359. }
  360. if (readdata && i < tot_len) {
  361. printf("Data portion:\n");
  362. while(i < tot_len)  printf("%2X%c", data[i], (++i%16)? ' ' : '\n');
  363. printf("\n");
  364. }
  365. return 0;
  366. }
  367.  
  368. /* A simple function I wrote to help in debugging, shows the important fields
  369.    of a UDP packet*/
  370. int readudppacket(char *packet, int readdata) {
  371.  
  372. struct ip *ip = (struct ip *) packet;
  373. udphdr_bsd *udp = (udphdr_bsd *) (packet + sizeof(struct ip));
  374. char *data = packet +  sizeof(struct ip) + sizeof(udphdr_bsd);
  375. int tot_len;
  376. struct in_addr bullshit, bullshit2;
  377. char sourcehost[16];
  378. int i;
  379. int realfrag = 0;
  380.  
  381. if (!packet) {
  382.   fprintf(stderr, "readudppacket: packet is NULL!\n");
  383.   return -1;
  384.     }
  385.  
  386. bullshit.s_addr = ip->ip_src.s_addr; bullshit2.s_addr = ip->ip_dst.s_addr;
  387. /* this is gay */
  388. realfrag = BSDFIX(ntohs(ip->ip_off) & 8191 /* 2^13 - 1 */);
  389. tot_len = BSDFIX(ip->ip_len);
  390. strncpy(sourcehost, inet_ntoa(bullshit), 16);
  391. i =  4 * (ntohs(ip->ip_hl)) + 8;
  392. if (ip->ip_p== IPPROTO_UDP) {
  393.   if (realfrag) 
  394.     printf("Packet is fragmented, offset field: %u\n", realfrag);
  395.   else {
  396.     printf("UDP packet: %s:%d -> %s:%d (total: %d bytes)\n", sourcehost, 
  397.        ntohs(udp->uh_sport), inet_ntoa(bullshit2), 
  398.        ntohs(udp->uh_dport), tot_len);
  399.  
  400.     printf("ttl: %hu ", ip->ip_ttl);
  401.   }
  402. }
  403.  if (readdata && i < tot_len) {
  404.    printf("Data portion:\n");
  405.    while(i < tot_len)  printf("%2X%c", data[i], (++i%16)? ' ' : '\n');
  406.    printf("\n");
  407.  }
  408.  return 0;
  409. }
  410.  
  411. int send_udp_raw_decoys( int sd, struct in_addr *victim, unsigned short sport, 
  412.           unsigned short dport, char *data, unsigned short datalen) {
  413.   int decoy;
  414.   
  415.   for(decoy = 0; decoy < o.numdecoys; decoy++) 
  416.     if (send_udp_raw(sd, &o.decoys[decoy], victim, sport, dport, data, 
  417.              datalen) == -1)
  418.       return -1;
  419.  
  420.   return 0;
  421. }
  422.  
  423.  
  424.  
  425. int send_udp_raw( int sd, struct in_addr *source, 
  426.           struct in_addr *victim, unsigned short sport, 
  427.           unsigned short dport, char *data, unsigned short datalen) 
  428. {
  429.  
  430. char *packet = safe_malloc(sizeof(struct ip) + sizeof(udphdr_bsd) + datalen);
  431. struct ip *ip = (struct ip *) packet;
  432. udphdr_bsd *udp = (udphdr_bsd *) (packet + sizeof(struct ip));
  433. static int myttl = 0;
  434.  
  435. int res;
  436. struct sockaddr_in sock;
  437. char myname[MAXHOSTNAMELEN + 1];
  438. struct hostent *myhostent = NULL;
  439. int source_malloced = 0;
  440. struct pseudo_udp_hdr {
  441.   struct in_addr source;
  442.   struct in_addr dest;        
  443.   char zero;
  444.   char proto;        
  445.   unsigned short length;
  446. } *pseudo = (struct pseudo_udp_hdr *) ((char *)udp - 12) ;
  447.  
  448. /* check that required fields are there and not too silly */
  449. if ( !victim || !sport || !dport || sd < 0) {
  450.   fprintf(stderr, "send_udp_raw: One or more of your parameters suck!\n");
  451.   free(packet);
  452.   return -1;
  453. }
  454.  
  455.  
  456. if (!myttl) myttl = (get_random_uint() % 23) + 37;
  457.  
  458. /* It was a tough decision whether to do this here for every packet
  459.    or let the calling function deal with it.  In the end I grudgingly decided
  460.    to do it here and potentially waste a couple microseconds... */
  461. sethdrinclude(sd); 
  462.  
  463. /* if they didn't give a source address, fill in our first address */
  464. if (!source) {
  465.   source_malloced = 1;
  466.   source = safe_malloc(sizeof(struct in_addr));
  467.   if (gethostname(myname, MAXHOSTNAMELEN) || 
  468.       !(myhostent = gethostbyname(myname)))
  469.     fatal("Cannot get hostname!  Try using -S <my_IP_address> or -e <interface to scan through>\n");
  470.   memcpy(source, myhostent->h_addr_list[0], sizeof(struct in_addr));
  471. #if ( TCPIP_DEBUGGING )
  472.     printf("We skillfully deduced that your address is %s\n", 
  473.        inet_ntoa(*source));
  474. #endif
  475. }
  476.  
  477.  
  478. /*do we even have to fill out this damn thing?  This is a raw packet, 
  479.   after all */
  480. sock.sin_family = AF_INET;
  481. sock.sin_port = htons(dport);
  482. sock.sin_addr.s_addr = victim->s_addr;
  483.  
  484.  
  485. bzero((char *) packet, sizeof(struct ip) + sizeof(udphdr_bsd));
  486.  
  487. udp->uh_sport = htons(sport);
  488. udp->uh_dport = htons(dport);
  489. udp->uh_ulen = htons(8 + datalen);
  490.  
  491.  /* We should probably copy the data over too */
  492. if (data)
  493.   memcpy(packet + sizeof(struct ip) + sizeof(udphdr_bsd), data, datalen);
  494.  
  495. /* Now the psuedo header for checksuming */
  496. pseudo->source.s_addr = source->s_addr;
  497. pseudo->dest.s_addr = victim->s_addr;
  498. pseudo->proto = IPPROTO_UDP;
  499. pseudo->length = htons(sizeof(udphdr_bsd) + datalen);
  500.  
  501. /* OK, now we should be able to compute a valid checksum */
  502. #if STUPID_SOLARIS_CHECKSUM_BUG
  503.  udp->uh_sum = sizeof(struct udphdr) + datalen;
  504. #else
  505. udp->uh_sum = in_cksum((unsigned short *)pseudo, 20 /* pseudo + UDP headers */ + datalen);
  506. #endif
  507.  
  508. /* Goodbye, pseudo header! */
  509. bzero(pseudo, 12);
  510.  
  511. /* Now for the ip header */
  512. ip->ip_v = 4;
  513. ip->ip_hl = 5;
  514. ip->ip_len = BSDFIX(sizeof(struct ip) + sizeof(udphdr_bsd) + datalen);
  515. get_random_bytes(&(ip->ip_id), 2);
  516. ip->ip_ttl = myttl;
  517. ip->ip_p = IPPROTO_UDP;
  518. ip->ip_src.s_addr = source->s_addr;
  519. ip->ip_dst.s_addr= victim->s_addr;
  520. #if HAVE_IP_IP_SUM
  521. ip->ip_sum = in_cksum((unsigned short *)ip, sizeof(struct ip));
  522. #endif
  523.  
  524. if (TCPIP_DEBUGGING > 1) {
  525.   printf("Raw UDP packet creation completed!  Here it is:\n");
  526.   readudppacket(packet,1);
  527. }
  528.  
  529. res = Sendto("send_udp_raw", sd, packet, BSDUFIX(ip->ip_len), 0,
  530.          (struct sockaddr *)&sock,  (int)sizeof(struct sockaddr_in));
  531.  
  532. if (source_malloced) free(source);
  533. free(packet);
  534. return res;
  535. }
  536.  
  537. int send_small_fragz_decoys(int sd, struct in_addr *victim, unsigned long seq, 
  538.                 int sport, int dport, int flags) {
  539.   int decoy;
  540.  
  541.   for(decoy = 0; decoy < o.numdecoys; decoy++) 
  542.     if (send_small_fragz(sd, &o.decoys[decoy], victim, seq, sport, 
  543.                 dport, 
  544.                 flags) == -1)
  545.       return -1;
  546.  
  547.   return 0;
  548. }
  549.  
  550. /* Much of this is swiped from my send_tcp_raw function above, which 
  551.    doesn't support fragmentation */
  552. int send_small_fragz(int sd, struct in_addr *source, struct in_addr *victim,
  553.              unsigned long seq, int sport, int dport, int flags)
  554.  {
  555.  
  556. struct pseudo_header { 
  557. /*for computing TCP checksum, see TCP/IP Illustrated p. 145 */
  558.   unsigned long s_addy;
  559.   unsigned long d_addr;
  560.   char zer0;
  561.   unsigned char protocol;
  562.   unsigned short length;
  563. };
  564. /*In this placement we get data and some field alignment so we aren't wasting
  565.   too much to compute the TCP checksum.*/
  566.  
  567. char packet[sizeof(struct ip) + sizeof(struct tcphdr) + 100];
  568. struct ip *ip = (struct ip *) packet;
  569. struct tcphdr *tcp = (struct tcphdr *) (packet + sizeof(struct ip));
  570. struct pseudo_header *pseudo = (struct pseudo_header *) (packet + sizeof(struct ip) - sizeof(struct pseudo_header)); 
  571. char *frag2 = packet + sizeof(struct ip) + 16;
  572. struct ip *ip2 = (struct ip *) (frag2 - sizeof(struct ip));
  573. static int myttl = 0;
  574. int res;
  575. struct sockaddr_in sock;
  576. int id;
  577.  
  578. if (!myttl)  myttl = (time(NULL) % 14) + 51;
  579.  
  580. /* It was a tough decision whether to do this here for every packet
  581.    or let the calling function deal with it.  In the end I grudgingly decided
  582.    to do it here and potentially waste a couple microseconds... */
  583. sethdrinclude(sd);
  584.  
  585.  
  586. /*Why do we have to fill out this damn thing? This is a raw packet, after all */
  587. sock.sin_family = AF_INET;
  588. sock.sin_port = htons(dport);
  589.  
  590. sock.sin_addr.s_addr = victim->s_addr;
  591.  
  592. bzero((char *)packet, sizeof(struct ip) + sizeof(struct tcphdr));
  593.  
  594. pseudo->s_addy = source->s_addr;
  595. pseudo->d_addr = victim->s_addr;
  596. pseudo->protocol = IPPROTO_TCP;
  597. pseudo->length = htons(sizeof(struct tcphdr));
  598.  
  599. tcp->th_sport = htons(sport);
  600. tcp->th_dport = htons(dport);
  601. tcp->th_seq = (seq)? htonl(seq) : get_random_uint();
  602.  
  603. tcp->th_off = 5 /*words*/;
  604. tcp->th_flags = flags;
  605.  
  606. tcp->th_win = htons(2048); /* Who cares */
  607.  
  608. tcp->th_sum = in_cksum((unsigned short *)pseudo, 
  609.                sizeof(struct tcphdr) + sizeof(struct pseudo_header));
  610.  
  611. /* Now for the ip header of frag1 */
  612.  
  613. bzero((char *) packet, sizeof(struct ip)); 
  614. ip->ip_v = 4;
  615. ip->ip_hl = 5;
  616. /*RFC 791 allows 8 octet frags, but I get "operation not permitted" (EPERM)
  617.   when I try that.  */
  618. ip->ip_len = BSDFIX(sizeof(struct ip) + 16);
  619. id = ip->ip_id = get_random_uint();
  620. ip->ip_off = BSDFIX(MORE_FRAGMENTS);
  621. ip->ip_ttl = myttl;
  622. ip->ip_p = IPPROTO_TCP;
  623. ip->ip_src.s_addr = source->s_addr;
  624. ip->ip_dst.s_addr = victim->s_addr;
  625.  
  626. #if HAVE_IP_IP_SUM
  627. ip->ip_sum= in_cksum((unsigned short *)ip, sizeof(struct ip));
  628. #endif
  629. if (o.debugging > 1) {
  630.   log_write(LOG_STDOUT, "Raw TCP packet fragment #1 creation completed!  Here it is:\n");
  631.   hdump(packet,20);
  632. }
  633. if (o.debugging > 1) 
  634.   log_write(LOG_STDOUT, "\nTrying sendto(%d , packet, %d, 0 , %s , %d)\n",
  635.      sd, ntohs(ip->ip_len), inet_ntoa(*victim),
  636.      (int) sizeof(struct sockaddr_in));
  637. /* Lets save this and send it AFTER we send the second one, just to be
  638.    cute ;) */
  639.  
  640. if ((res = sendto(sd, packet,sizeof(struct ip) + 16 , 0, 
  641.           (struct sockaddr *)&sock, sizeof(struct sockaddr_in))) == -1)
  642.   {
  643.     perror("sendto in send_syn_fragz");
  644.     return -1;
  645.   }
  646. if (o.debugging > 1) log_write(LOG_STDOUT, "successfully sent %d bytes of raw_tcp!\n", res);
  647.  
  648. /* Create the second fragment */
  649.  
  650. bzero((char *) ip2, sizeof(struct ip));
  651. ip2->ip_v= 4;
  652. ip2->ip_hl = 5;
  653. ip2->ip_len = BSDFIX(sizeof(struct ip) + 4); /* the rest of our TCP packet */
  654. ip2->ip_id = id;
  655. ip2->ip_off = BSDFIX(2);
  656. ip2->ip_ttl = myttl;
  657. ip2->ip_p = IPPROTO_TCP;
  658. ip2->ip_src.s_addr = source->s_addr;
  659. ip2->ip_dst.s_addr = victim->s_addr;
  660.  
  661. #if HAVE_IP_IP_SUM
  662. ip2->ip_sum = in_cksum((unsigned short *)ip2, sizeof(struct ip));
  663. #endif
  664. if (o.debugging > 1) {
  665.   log_write(LOG_STDOUT, "Raw TCP packet fragment creation completed!  Here it is:\n");
  666.   hdump(packet,20);
  667. }
  668. if (o.debugging > 1) 
  669.  
  670.   log_write(LOG_STDOUT, "\nTrying sendto(%d , ip2, %d, 0 , %s , %d)\n", sd, 
  671.      ntohs(ip2->ip_len), inet_ntoa(*victim), (int) sizeof(struct sockaddr_in));
  672. if ((res = sendto(sd, (void *)ip2,sizeof(struct ip) + 4 , 0, 
  673.           (struct sockaddr *)&sock, (int) sizeof(struct sockaddr_in))) == -1)
  674.   {
  675.     perror("sendto in send_tcp_raw frag #2");
  676.     return -1;
  677.   }
  678.  
  679. return 1;
  680. }
  681.  
  682. int send_ip_raw_decoys( int sd, struct in_addr *victim, unsigned char proto,
  683.             char *data, unsigned short datalen) {
  684.  
  685.   int decoy;
  686.  
  687.   for(decoy = 0; decoy < o.numdecoys; decoy++) 
  688.     if (send_ip_raw(sd, &o.decoys[decoy], victim, proto, data, 
  689.                datalen) == -1)
  690.       return -1;
  691.  
  692.   return 0;
  693.  
  694.  
  695. }
  696.  
  697. int send_ip_raw( int sd, struct in_addr *source, 
  698.           struct in_addr *victim, unsigned char proto,
  699.           char *data, unsigned short datalen) 
  700. {
  701.  
  702. char *packet = safe_malloc(sizeof(struct ip) + datalen);
  703. struct ip *ip = (struct ip *) packet;
  704. static int myttl = 0;
  705.  
  706. int res = -1;
  707. struct sockaddr_in sock;
  708. char myname[MAXHOSTNAMELEN + 1];
  709. struct hostent *myhostent = NULL;
  710. int source_malloced = 0;
  711.  
  712. /* check that required fields are there and not too silly */
  713. if ( !victim || sd < 0) {
  714.   fprintf(stderr, "send_ip_raw: One or more of your parameters suck!\n");
  715.   free(packet);
  716.   return -1;
  717. }
  718.  
  719. if (!myttl) myttl = (get_random_uint() % 23) + 37;
  720.  
  721. /* It was a tough decision whether to do this here for every packet
  722.    or let the calling function deal with it.  In the end I grudgingly decided
  723.    to do it here and potentially waste a couple microseconds... */
  724. sethdrinclude(sd); 
  725.  
  726. /* if they didn't give a source address, fill in our first address */
  727. if (!source) {
  728.   source_malloced = 1;
  729.   source = safe_malloc(sizeof(struct in_addr));
  730.   if (gethostname(myname, MAXHOSTNAMELEN) || 
  731.       !(myhostent = gethostbyname(myname)))
  732.     fatal("Cannot get hostname!  Try using -S <my_IP_address> or -e <interface to scan through>\n");
  733.   memcpy(source, myhostent->h_addr_list[0], sizeof(struct in_addr));
  734. #if ( TCPIP_DEBUGGING )
  735.     printf("We skillfully deduced that your address is %s\n", 
  736.        inet_ntoa(*source));
  737. #endif
  738. }
  739.  
  740.  
  741. /*do we even have to fill out this damn thing?  This is a raw packet, 
  742.   after all */
  743. sock.sin_family = AF_INET;
  744. sock.sin_port = 0;
  745. sock.sin_addr.s_addr = victim->s_addr;
  746.  
  747.  
  748. bzero((char *) packet, sizeof(struct ip));
  749.  
  750. /* Now for the ip header */
  751.  
  752. ip->ip_v = 4;
  753. ip->ip_hl = 5;
  754. ip->ip_len = BSDFIX(sizeof(struct ip) + datalen);
  755. get_random_bytes(&(ip->ip_id), 2);
  756. ip->ip_ttl = myttl;
  757. ip->ip_p = proto;
  758. ip->ip_src.s_addr = source->s_addr;
  759. ip->ip_dst.s_addr= victim->s_addr;
  760. #if HAVE_IP_IP_SUM
  761. ip->ip_sum = in_cksum((unsigned short *)ip, sizeof(struct ip));
  762. #endif
  763.  
  764.  /* We should probably copy the data over too */
  765.  if (data)
  766.    memcpy(packet + sizeof(struct ip), data, datalen);
  767.  
  768. if (TCPIP_DEBUGGING > 1) {
  769.   printf("Raw IP packet creation completed!  Here it is:\n");
  770.   hdump(packet, BSDUFIX(ip->ip_len));
  771. }
  772.  
  773.  
  774. res = Sendto("send_ip_raw", sd, packet, BSDUFIX(ip->ip_len), 0,
  775.          (struct sockaddr *)&sock,  (int)sizeof(struct sockaddr_in));
  776.  
  777. if (source_malloced) free(source);
  778. free(packet); 
  779. return res;
  780. }
  781.  
  782. inline int unblock_socket(int sd) {
  783. int options;
  784. /*Unblock our socket to prevent recvfrom from blocking forever
  785.   on certain target ports. */
  786. #ifdef WIN32
  787. #else
  788. options = O_NONBLOCK | fcntl(sd, F_GETFL);
  789. fcntl(sd, F_SETFL, options);
  790. #endif
  791. return 1;
  792. }
  793.  
  794. /* Get the source address and interface name */
  795. #if 0
  796. char *getsourceif(struct in_addr *src, struct in_addr *dst) {
  797. int sd, sd2;
  798. unsigned short p1;
  799. struct sockaddr_in sock;
  800. int socklen = sizeof(struct sockaddr_in);
  801. struct sockaddr sa;
  802. int sasize = sizeof(struct sockaddr);
  803. int ports, res;
  804. char buf[65536];
  805. struct timeval tv;
  806. unsigned int start;
  807. int data_offset, ihl, *intptr;
  808. int done = 0;
  809.  
  810.   /* Get us some unreserved port numbers */
  811.   get_random_bytes(&p1, 2);
  812.   if (p1 < 5000) p1 += 5000;
  813.  
  814.   if (!getuid()) {
  815.     if ((sd2 = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL))) == -1)
  816.       {perror("Linux Packet Socket troubles"); return 0;}
  817.     unblock_socket(sd2);
  818.     if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  819.       {perror("Socket troubles"); return 0;}
  820.     sock.sin_family = AF_INET;
  821.     sock.sin_addr = *dst;
  822.     sock.sin_port = htons(p1);
  823.     if (connect(sd, (struct sockaddr *) &sock, sizeof(struct sockaddr_in)) == -1)
  824.       { perror("UDP connect()");
  825.       close(sd);
  826.       close(sd2);
  827.       return NULL;
  828.       }
  829.     if (getsockname(sd, (SA *)&sock, &socklen) == -1) {
  830.       perror("getsockname");
  831.       close(sd);
  832.       close(sd2);
  833.       return NULL;
  834.     }
  835.     ports = (ntohs(sock.sin_port) << 16) + p1;
  836. #if ( TCPIP_DEBUGGING )
  837.       printf("ports is %X\n", ports);
  838. #endif
  839.     if (send(sd, "", 0, 0) == -1)
  840.     fatal("Could not send UDP packet");
  841.     start = time(NULL);
  842.     do {
  843.       tv.tv_sec = 2;
  844.       tv.tv_usec = 0;
  845.       res = recvfrom(sd2, buf, 65535, 0, &sa, &sasize);
  846.       if (res < 0) {
  847.     if (errno != EWOULDBLOCK)
  848.       perror("recvfrom");
  849.       }
  850.       if (res > 0) {
  851. #if ( TCPIP_DEBUGGING )
  852.     printf("Got packet!\n");
  853.     printf("sa.sa_data: %s\n", sa.sa_data);
  854.     printf("Hex dump of packet (len %d):\n", res);
  855.     hdump(buf, res);
  856. #endif
  857.     data_offset = get_link_offset(sa.sa_data);
  858.     ihl = (*(buf + data_offset) & 0xf) * 4;
  859.     /* If it is big enough and it is IPv4 */
  860.     if (res >=  data_offset + ihl + 4 &&
  861.         (*(buf + data_offset) & 0x40)) {
  862.       intptr = (int *)  ((char *) buf + data_offset + ihl);
  863.       if (*intptr == ntohl(ports)) {
  864.         intptr = (int *) ((char *) buf + data_offset + 12);
  865. #if ( TCPIP_DEBUGGING )
  866.         printf("We've found our packet [krad]\n");
  867. #endif
  868.         memcpy(src, buf + data_offset + 12, 4);
  869.         close(sd);
  870.         close(sd2);
  871.         return strdup(sa.sa_data);
  872.       }
  873.     }
  874.       }        
  875.     } while(!done && time(NULL) - start < 2);
  876.     close(sd);
  877.     close(sd2);
  878.   }
  879.  
  880. return NULL;
  881. }
  882. #endif /* 0 */
  883.  
  884. int getsourceip(struct in_addr *src, struct in_addr *dst) {
  885.   int sd;
  886.   struct sockaddr_in sock;
  887.   int socklen = sizeof(struct sockaddr_in);
  888.   unsigned short p1;
  889.  
  890.   get_random_bytes(&p1, 2);
  891.   if (p1 < 5000) p1 += 5000;
  892.  
  893.   if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  894.     {perror("Socket troubles"); return 0;}
  895.   sock.sin_family = AF_INET;
  896.   sock.sin_addr = *dst;
  897.   sock.sin_port = htons(p1);
  898.   if (connect(sd, (struct sockaddr *) &sock, sizeof(struct sockaddr_in)) == -1)
  899.     { perror("UDP connect()");
  900.     close(sd);
  901.     return 0;
  902.     }
  903.   bzero(&sock, sizeof(struct sockaddr_in));
  904.   if (getsockname(sd, (SA *)&sock, &socklen) == -1) {
  905.     perror("getsockname");
  906.     close(sd);
  907.     return 0;
  908.   }
  909.  
  910.   src->s_addr = sock.sin_addr.s_addr;
  911.   close(sd);
  912.   return 1; /* Calling function responsible for checking validity */
  913. }
  914.  
  915. #if 0
  916. int get_link_offset(char *device) {
  917. int sd;
  918. struct ifreq ifr;
  919. sd = socket(AF_INET, SOCK_DGRAM, 0);
  920. memset(&ifr, 0, sizeof(ifr));
  921. strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  922. #if (defined(SIOCGIFHWADDR) && defined(ARPHRD_ETHER) && 
  923.      defined(ARPHRD_METRICOM) && defined(ARPHRD_SLIP) && defined(ARPHRD_CSLIP)
  924.      && defined(ARPHRD_SLIP6) && defined(ARPHRD_PPP) && 
  925.      defined(ARPHRD_LOOPBACK) )
  926. if (ioctlsocket(sd, SIOCGIFHWADDR, &ifr) < 0 ) {
  927.   fatal("Can't obtain link offset.  What kind of interface are you using?");
  928.   }
  929. close(sd);
  930. switch (ifr.ifr_hwaddr.sa_family) {
  931. case ARPHRD_ETHER:  /* These two are standard ethernet */
  932. case ARPHRD_METRICOM:
  933.   return 14;
  934.   break;
  935. case ARPHRD_SLIP:
  936. case ARPHRD_CSLIP:
  937. case ARPHRD_SLIP6:
  938. case ARPHRD_CSLIP6:
  939. case ARPHRD_PPP:
  940.   return 0;
  941.   break;
  942. case ARPHRD_LOOPBACK:  /* Loopback interface (obviously) */
  943.   return 14;
  944.   break;
  945. default:
  946.   fatal("Unknown link layer device: %d", ifr.ifr_hwaddr.sa_family);
  947. }
  948. #else
  949. printf("get_link_offset called even though your host doesn't support it.  Assuming Ethernet or Loopback connection (wild guess)\n");
  950. return 14;
  951. #endif
  952. /* Not reached */
  953. exit(1);
  954. }
  955. #endif
  956.  
  957. /* Read an IP packet using libpcap .  We return the packet and take
  958.    a pcap descripter and a pointer to the packet length (which we set
  959.    in the function. If you want a maximum length returned, you
  960.    should specify that in pcap_open_live() */
  961.  
  962. /* to_usec is the timeout period in microseconds -- use 0 to skip the
  963.    test and -1 to block forever.  Note that we don't interrupt pcap, so
  964.    low values (and 0) degenerate to the timeout specified 
  965.    in pcap_open_live()
  966.  */
  967.  
  968. char *readip_pcap(pcap_t *pd, unsigned int *len, long to_usec) {
  969. int offset = -1;
  970. struct pcap_pkthdr head;
  971. char *p;
  972. int datalink;
  973. int timedout = 0;
  974. struct timeval tv_start, tv_end;
  975.  
  976. if (!pd) fatal("NULL packet device passed to readip_pcap");
  977.  
  978. /* New packet capture device, need to recompute offset */
  979.  if ( (datalink = pcap_datalink(pd)) < 0)
  980.    fatal("Cannot obtain datalink information: %s", pcap_geterr(pd));
  981.  switch(datalink) {
  982.  case DLT_EN10MB: offset = 14; break;
  983.  case DLT_IEEE802: offset = 22; break;
  984. #ifdef DLT_LOOP
  985.  case DLT_LOOP:
  986. #endif
  987.  case DLT_NULL: offset = 4; break;
  988.  case DLT_SLIP:
  989. #ifdef DLT_SLIP_BSDOS
  990.  case DLT_SLIP_BSDOS:
  991. #endif
  992. #if (FREEBSD || OPENBSD || NETBSD || BSDI)
  993.    offset = 16;
  994. #else
  995.    offset = 24; /* Anyone use this??? */
  996. #endif
  997.    break;
  998.  case DLT_PPP: 
  999. #ifdef DLT_PPP_BSDOS
  1000.  case DLT_PPP_BSDOS:
  1001. #endif
  1002. #if (FREEBSD || OPENBSD || NETBSD || BSDI)
  1003.    offset = 4;
  1004. #else
  1005. #ifdef SOLARIS
  1006.    offset = 8;
  1007. #else
  1008.    offset = 24; /* Anyone use this? */
  1009. #endif /* ifdef solaris */
  1010. #endif /* if freebsd || openbsd || netbsd || bsdi */
  1011.    break;
  1012.  case DLT_RAW: offset = 0; break;
  1013.  case DLT_FDDI: offset = 21; break;
  1014. #ifdef DLT_ENC
  1015.  case DLT_ENC: offset = 12; break;
  1016. #endif
  1017.  default: fatal("Unknown datalink type (%d)", datalink);
  1018.  }
  1019.  
  1020.  if (to_usec > 0) {
  1021.    gettimeofday(&tv_start, NULL);
  1022.  }
  1023.  do {
  1024.    p = (char *) pcap_next(pd, &head);
  1025.    if (p)
  1026.      p += offset;
  1027.    if (!p || (*p & 0x40) != 0x40) {
  1028.      /* Should we timeout? */
  1029.      if (to_usec == 0) {
  1030.        timedout = 1;
  1031.      } else if (to_usec > 0) {
  1032.        gettimeofday(&tv_end, NULL);
  1033.        if (TIMEVAL_SUBTRACT(tv_end, tv_start) >= to_usec) {
  1034.      timedout = 1;     
  1035.        }
  1036.      }
  1037.    }
  1038.  } while(!timedout && (!p || (*p & 0x40) != 0x40)); /* Go until we get IPv4 packet */
  1039.  if (timedout) {
  1040.    *len = 0;
  1041.    return NULL;
  1042.  }
  1043.  *len = head.caplen - offset;
  1044.  return p;
  1045. }
  1046.  
  1047. int ipaddr2devname( char *dev, struct in_addr *addr ) {
  1048. struct interface_info *mydevs;
  1049. int numdevs;
  1050. int i;
  1051. mydevs = getinterfaces(&numdevs);
  1052.  
  1053. if (!mydevs) return -1;
  1054.  
  1055. for(i=0; i < numdevs; i++) {
  1056.   if (addr->s_addr == mydevs[i].addr.s_addr) {
  1057.     strcpy(dev, mydevs[i].name);
  1058.     return 0;
  1059.   }
  1060. }
  1061. return -1;
  1062. }
  1063.  
  1064. int devname2ipaddr(char *dev, struct in_addr *addr) {
  1065. struct interface_info *mydevs;
  1066. int numdevs;
  1067. int i;
  1068. mydevs = getinterfaces(&numdevs);
  1069.  
  1070. if (!mydevs) return -1;
  1071.  
  1072. for(i=0; i < numdevs; i++) {
  1073.   if (!strcmp(dev, mydevs[i].name)) {  
  1074.     memcpy(addr, (char *) &mydevs[i].addr, sizeof(struct in_addr));
  1075.     return 0;
  1076.   }
  1077. }
  1078. return -1;
  1079. }
  1080.  
  1081.  
  1082. struct interface_info *getinterfaces(int *howmany) {
  1083.   static int initialized = 0;
  1084.   static struct interface_info mydevs[48];
  1085.   static int numinterfaces = 0;
  1086.   int sd;
  1087.   int len;
  1088.   char *p;
  1089.   char buf[10240];
  1090.   struct ifconf ifc;
  1091.   struct ifreq *ifr;
  1092.   struct sockaddr_in *sin;
  1093.  
  1094.   if (!initialized) {
  1095.  
  1096.     initialized = 1;
  1097.     /* Dummy socket for ioctl */
  1098.     sd = socket(AF_INET, SOCK_DGRAM, 0);
  1099.     if (sd < 0) pfatal("socket in getinterfaces");
  1100.     ifc.ifc_len = sizeof(buf);
  1101.     ifc.ifc_buf = buf;
  1102.     if (ioctlsocket(sd, SIOCGIFCONF, &ifc) < 0) {
  1103.       fatal("Failed to determine your configured interfaces!\n");
  1104.     }
  1105.     close(sd);
  1106.     ifr = (struct ifreq *) buf;
  1107.     if (ifc.ifc_len == 0) 
  1108.       fatal("getinterfaces: SIOCGIFCONF claims you have no network interfaces!\n");
  1109. #if HAVE_SOCKADDR_SA_LEN
  1110.     /*    len = MAX(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);*/
  1111.     len = ifr->ifr_addr.sa_len + sizeof(ifr->ifr_name);
  1112. #else
  1113.     len = sizeof(struct ifreq);
  1114.     /* len = sizeof(SA); */
  1115. #endif
  1116.  
  1117. #if TCPIP_DEBUGGING
  1118.     printf("ifnet list length = %d\n",ifc.ifc_len);
  1119.     printf("sa_len = %d\n",len);
  1120.     hdump(buf, ifc.ifc_len);
  1121.     printf("ifr = %X\n",(unsigned int)(*(char **)&ifr));
  1122.     printf("Size of struct ifreq: %d\n", sizeof(struct ifreq));
  1123. #endif
  1124.  
  1125.     for(; ifr && *((char *)ifr) && ((char *)ifr) < buf + ifc.ifc_len; 
  1126.     ((*(char **)&ifr) += len )) {
  1127. #if TCPIP_DEBUGGING
  1128.       printf("ifr_name size = %d\n", sizeof(ifr->ifr_name));
  1129.       printf("ifr = %X\n",(unsigned int)(*(char **)&ifr));
  1130. #endif
  1131.  
  1132.       /* skip any device with no name */
  1133.       if (!*((char *)ifr))
  1134.         continue;
  1135.  
  1136.       sin = (struct sockaddr_in *) &ifr->ifr_addr;
  1137.       memcpy(&(mydevs[numinterfaces].addr), (char *) &(sin->sin_addr), sizeof(struct in_addr));
  1138.       /* In case it is a stinkin' alias */
  1139.       if ((p = strchr(ifr->ifr_name, ':')))
  1140.     *p = '\0';
  1141.       strncpy(mydevs[numinterfaces].name, ifr->ifr_name, 63);
  1142.       mydevs[numinterfaces].name[63] = '\0';
  1143.  
  1144.  
  1145. #if TCPIP_DEBUGGING
  1146.       printf("Interface %d is %s\n",numinterfaces,mydevs[numinterfaces].name);
  1147. #endif
  1148.  
  1149.       numinterfaces++;
  1150.       if (numinterfaces == 47)  {      
  1151.     error("My god!  You seem to have WAY too many interfaces!  Things may not work right\n");
  1152.     break;
  1153.       }
  1154. #if HAVE_SOCKADDR_SA_LEN
  1155.       /* len = MAX(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);*/
  1156.       len = ifr->ifr_addr.sa_len + sizeof(ifr->ifr_name);
  1157. #endif 
  1158.       mydevs[numinterfaces].name[0] = '\0';
  1159.     }
  1160.   }
  1161.   if (howmany) *howmany = numinterfaces;
  1162.   return mydevs;
  1163. }
  1164.  
  1165.  
  1166.  
  1167. /* An awesome function to determine what interface a packet to a given
  1168.    destination should be routed through.  It returns NULL if no appropriate
  1169.    interface is found, oterwise it returns the device name and fills in the
  1170.    source parameter.   Some of the stuff is
  1171.    from Stevens' Unix Network Programming V2.  He had an easier suggestion
  1172.    for doing this (in the book), but it isn't portable :( */
  1173. #define ROUTETHROUGH_MAXROUTES 512
  1174. char *routethrough(struct in_addr *dest, struct in_addr *source) {
  1175.   static int initialized = 0;
  1176.   int i;
  1177.   struct in_addr addy;
  1178.   static enum { procroutetechnique, connectsockettechnique, guesstechnique } technique = procroutetechnique;
  1179.   char buf[10240];
  1180.   struct interface_info *mydevs;
  1181.   static struct myroute {
  1182.     struct interface_info *dev;
  1183.     unsigned int mask;
  1184.     unsigned int dest;
  1185.   } myroutes[ROUTETHROUGH_MAXROUTES];
  1186.   int numinterfaces = 0;
  1187.   char *p, *endptr;
  1188.   char iface[64];
  1189.   static int numroutes = 0;
  1190.   FILE *routez;
  1191.  
  1192.   if (!dest) fatal("ipaddr2devname passed a NULL dest address");
  1193.  
  1194.   if (!initialized) {  
  1195.     /* Dummy socket for ioctl */
  1196.     initialized = 1;
  1197.     mydevs = getinterfaces(&numinterfaces);
  1198.  
  1199.     /* Now we must go through several techniques to determine info */
  1200.     routez = fopen("/proc/net/route", "r");
  1201.  
  1202.     if (routez) {
  1203.       /* OK, linux style /proc/net/route ... we can handle this ... */
  1204.       /* Now that we've got the interfaces, we g0 after the r0ut3Z */
  1205.       
  1206.       fgets(buf, sizeof(buf), routez); /* Kill the first line */
  1207.       while(fgets(buf,sizeof(buf), routez)) {
  1208.     p = strtok(buf, " \t\n");
  1209.     if (!p) {
  1210.       error("Could not find interface in /proc/net/route line");
  1211.       continue;
  1212.     }
  1213.     if (*p == '*')
  1214.       continue; /* Deleted route -- any other valid reason for
  1215.                a route to start with an asterict? */
  1216.     Strncpy(iface, p, sizeof(iface));
  1217.     if ((p = strchr(iface, ':'))) {
  1218.       *p = '\0'; /* To support IP aliasing */
  1219.     }
  1220.     p = strtok(NULL, " \t\n");
  1221.     endptr = NULL;
  1222.     myroutes[numroutes].dest = strtoul(p, &endptr, 16);
  1223.     if (!endptr || *endptr) {
  1224.       error("Failed to determine Destination from /proc/net/route");
  1225.       continue;
  1226.     }
  1227.     for(i=0; i < 6; i++) {
  1228.       p = strtok(NULL, " \t\n");
  1229.       if (!p) break;
  1230.     }
  1231.     if (!p) {
  1232.       error("Failed to find field %d in /proc/net/route", i + 2);
  1233.       continue;
  1234.     }
  1235.     endptr = NULL;
  1236.     myroutes[numroutes].mask = strtoul(p, &endptr, 16);
  1237.     if (!endptr || *endptr) {
  1238.       error("Failed to determine mask from /proc/net/route");
  1239.       continue;
  1240.     }
  1241.  
  1242.  
  1243. #if TCPIP_DEBUGGING
  1244.       printf("#%d: for dev %s, The dest is %X and the mask is %X\n", numroutes, iface, myroutes[numroutes].dest, myroutes[numroutes].mask);
  1245. #endif
  1246.       for(i=0; i < numinterfaces; i++)
  1247.         if (!strcmp(iface, mydevs[i].name)) {
  1248.           myroutes[numroutes].dev = &mydevs[i];
  1249.           break;
  1250.         }
  1251.       if (i == numinterfaces) 
  1252.         fatal("Failed to find interface %s mentioned in /proc/net/route\n", iface);
  1253.       numroutes++;
  1254.       if (numroutes == ROUTETHROUGH_MAXROUTES)
  1255.         fatal("My god!  You seem to have WAY to many routes!\n");
  1256.       }
  1257.       fclose(routez);
  1258.     } else {
  1259.       technique = connectsockettechnique;
  1260.     }
  1261.   } else {  
  1262.     mydevs = getinterfaces(&numinterfaces);
  1263.   }
  1264.   /* WHEW, that takes care of initializing, now we have the easy job of 
  1265.      finding which route matches */
  1266.   if (islocalhost(dest)) {
  1267.     if (source)
  1268.       source->s_addr = htonl(0x7F000001);
  1269.     /* Now we find the localhost interface name, assuming 127.0.0.1 is
  1270.        localhost (it damn well better be!)... */
  1271.     for(i=0; i < numinterfaces; i++) {    
  1272.       if (mydevs[i].addr.s_addr == htonl(0x7F000001)) {
  1273.     return mydevs[i].name;
  1274.       }
  1275.     }
  1276.     return NULL;
  1277.   }
  1278.  
  1279.   if (technique == procroutetechnique) {    
  1280.     for(i=0; i < numroutes; i++) {  
  1281.       if ((dest->s_addr & myroutes[i].mask) == myroutes[i].dest) {
  1282.     if (source) {
  1283.       source->s_addr = myroutes[i].dev->addr.s_addr;
  1284.     }
  1285.     return myroutes[i].dev->name;      
  1286.       }
  1287.     }
  1288.   } else if (technique == connectsockettechnique) {
  1289.       if (!getsourceip(&addy, dest))
  1290.     return NULL;
  1291.       if (!addy.s_addr)  {  /* Solaris 2.4 */
  1292.         struct hostent *myhostent = NULL;
  1293.         char myname[MAXHOSTNAMELEN + 1];
  1294.         if (gethostname(myname, MAXHOSTNAMELEN) || 
  1295.            !(myhostent = gethostbyname(myname)))
  1296.       fatal("Cannot get hostname!  Try using -S <my_IP_address> or -e <interface to scan through>\n");
  1297.         memcpy(&(addy.s_addr), myhostent->h_addr_list[0], sizeof(struct in_addr));
  1298. #if ( TCPIP_DEBUGGING )
  1299.       printf("We skillfully deduced that your address is %s\n", 
  1300.         inet_ntoa(*source));
  1301. #endif
  1302.       }
  1303.  
  1304.       /* Now we insure this claimed address is a real interface ... */
  1305.       for(i=0; i < numinterfaces; i++)
  1306.     if (mydevs[i].addr.s_addr == addy.s_addr) {
  1307.       if (source) {
  1308.         source->s_addr = addy.s_addr;      
  1309.       }
  1310.       return mydevs[i].name;
  1311.     }  
  1312.       return NULL;
  1313.     } else 
  1314.       fatal("I know sendmail technique ... I know rdist technique ... but I don't know what the hell kindof technique you are attempting!!!");
  1315.     return NULL;
  1316. }
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.