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

  1. #ifndef TCPIP_H
  2. #define TCPIP_H
  3.  
  4. #ifdef HAVE_CONFIG_H
  5. #include "config.h"
  6. #endif
  7.  
  8. #ifdef WIN32 
  9. #include <winclude.h>
  10. #else
  11. #ifdef STDC_HEADERS
  12. #include <stdlib.h>
  13. #else
  14. void *malloc();
  15. void *realloc();
  16. #endif
  17.  
  18. #if STDC_HEADERS || HAVE_STRING_H
  19. #include <string.h>
  20. #if !STDC_HEADERS && HAVE_MEMORY_H
  21. #include <memory.h>
  22. #endif
  23. #endif
  24. #if HAVE_STRINGS_H
  25. #include <strings.h>
  26. #endif
  27.  
  28. #ifdef HAVE_BSTRING_H
  29. #include <bstring.h>
  30. #endif
  31.  
  32. #ifndef HAVE_BZERO
  33. #define bzero(s, n) memset((s), 0, (n))
  34. #endif
  35.  
  36. #ifndef WIN32
  37. #ifndef HAVE_MEMCPY
  38. #define memcpy(d, s, n) bcopy((s), (d), (n))
  39. #endif
  40. #endif
  41.  
  42. #include <ctype.h>
  43. #include <sys/types.h>
  44.  
  45. #ifdef HAVE_SYS_PARAM_H
  46. #include <sys/param.h> /* Defines MAXHOSTNAMELEN on BSD*/
  47. #endif
  48.  
  49. /* Linux uses these defines in netinet/ip.h and netinet/tcp.h to
  50.    use the correct struct ip and struct tcphdr */
  51. #ifndef __FAVOR_BSD
  52. #define __FAVOR_BSD 1
  53. #endif
  54. #ifndef __BSD_SOURCE
  55. #define __BSD_SOURCE 1
  56. #endif
  57. #ifndef __USE_BSD
  58. #define __USE_BSD 1
  59. #endif
  60. /* BSDI needs this to insure the correct struct ip */
  61. #undef _IP_VHL
  62. #include <stdio.h>
  63. #include <netinet/in.h>
  64. #include <rpc/types.h>
  65. #include <netdb.h>
  66. #include <sys/socket.h>
  67. #include <arpa/inet.h>
  68. #ifndef NETINET_IN_SYSTEM_H  /* why the HELL does OpenBSD not do this? */
  69. #include <netinet/in_systm.h> /* defines n_long needed for netinet/ip.h */
  70. #define NETINET_IN_SYSTEM_H
  71. #endif
  72. #ifndef NETINET_IP_H  /* why the HELL does OpenBSD not do this? */
  73. #include <netinet/ip.h>
  74. #define NETINET_IP_H
  75. #endif
  76. #ifndef __FAVOR_BSD
  77. #define __FAVOR_BSD
  78. #endif
  79. #ifndef NETINET_TCP_H  /* why the HELL does OpenBSD not do this? */
  80. #include <netinet/tcp.h>          /*#include <netinet/ip_tcp.h>*/
  81. #define NETINET_TCP_H
  82. #endif
  83. #ifndef NETINET_UDP_H
  84. #include <netinet/udp.h>
  85. #define NETINET_UDP_H
  86. #endif
  87. #include <unistd.h>
  88. #include <fcntl.h>
  89. #ifndef NET_IF_H  /* why the HELL does OpenBSD not do this? */
  90. #include <net/if.h>
  91. #define NET_IF_H
  92. #endif
  93. #if HAVE_NETINET_IF_ETHER_H
  94. #ifndef NETINET_IF_ETHER_H
  95. #include <netinet/if_ether.h>
  96. #define NETINET_IF_ETHER_H
  97. #endif /* NETINET_IF_ETHER_H */
  98. #endif /* HAVE_NETINET_IF_ETHER_H */
  99.  
  100. #if TIME_WITH_SYS_TIME
  101. # include <sys/time.h>
  102. # include <time.h>
  103. #else
  104. # if HAVE_SYS_TIME_H
  105. #  include <sys/time.h>
  106. # else
  107. #  include <time.h>
  108. # endif
  109. #endif
  110.  
  111. #include <sys/ioctl.h>
  112. #include <pcap.h>
  113. #include <setjmp.h>
  114. #include <errno.h>
  115. #include <signal.h>
  116. #include <pcap.h>
  117. #if HAVE_SYS_SOCKIO_H
  118. #include <sys/sockio.h>  /* SIOCGIFCONF for Solaris */
  119. #endif
  120. #endif
  121. #include "error.h"
  122. #include "utils.h"
  123. #include "nmap.h"
  124. #include "global_structures.h"
  125.  
  126. #ifndef DEBUGGING
  127. #define DEBUGGING 1
  128. #endif
  129.  
  130. #ifndef TCPIP_DEBUGGING
  131. #define TCPIP_DEBUGGING 1
  132. #endif
  133.  
  134. #ifndef BSDFIX
  135. #if FREEBSD || BSDI || NETBSD 
  136. #define BSDFIX(x) x
  137. #define BSDUFIX(x) x
  138. #else
  139. #define BSDFIX(x) htons(x)
  140. #define BSDUFIX(x) ntohs(x)
  141. #endif
  142. #endif
  143.  
  144. /* Bogus TCP flag!!!  Krad! */
  145. #define TH_BOGUS 64
  146. #define TH_BOG 64
  147.  
  148. #define MORE_FRAGMENTS 8192 /*NOT a user serviceable parameter*/
  149.  
  150. /*
  151. #ifndef INTERFACEDEF
  152. #define INTERFACEDEF 1
  153.  
  154. struct interface_info {
  155.     struct in_addr addr;
  156. #ifdef WIN32
  157.     char name[512];
  158. //    char Wname[1024];
  159.     LPADAPTER adapter;
  160.     BYTE MAC[6];
  161. //    char chopname[512];
  162.     long ifid;
  163.     struct in_addr Gateway;
  164. #else
  165.     char name[64];
  166. #endif
  167.  
  168. };
  169.  
  170. #endif
  171. */
  172.  
  173. #ifndef HAVE_STRUCT_IP
  174. #define HAVE_STRUCT_IP
  175.  
  176. /* From Linux glibc, which apparently borrowed it from
  177.    BSD code.  Slightly modified for portability --fyodor@dhp.com */
  178. /*
  179.  * Structure of an internet header, naked of options.
  180.  */
  181.  
  182.  
  183. struct ip
  184.   {
  185. //#if WORDS_BIGENDIAN
  186. //    u_int8_t ip_v:4;                    /* version */
  187. //    u_int8_t ip_hl:4;                   /* header length */
  188. //#else
  189.     u_char ip_hl:4,                   /* header length */
  190.               ip_v:4;                    /* version */ 
  191. //#endif
  192.     //u_int8_t ip_tos;                    /* type of service */
  193.     u_char ip_tos;                    /* type of service */
  194.     short ip_len;                     /* total length */
  195.     short ip_id;                      /* identification */
  196.     short ip_off;                     /* fragment offset field */
  197. #define IP_RF 0x8000                    /* reserved fragment flag */
  198. #define IP_DF 0x4000                    /* dont fragment flag */
  199. #define IP_MF 0x2000                    /* more fragments flag */
  200. #define IP_OFFMASK 0x1fff               /* mask for fragmenting bits */
  201.     u_char ip_ttl;                    /* time to live */
  202.     u_char ip_p;                      /* protocol */
  203.     short ip_sum;                     /* checksum */
  204.     struct in_addr ip_src, ip_dst;      /* source and dest address */
  205.   };
  206.  
  207. #endif /* HAVE_STRUCT_IP */
  208.  
  209. #ifdef LINUX 
  210. typedef struct udphdr_bsd {
  211.          unsigned short uh_sport;           /* source port */
  212.          unsigned short uh_dport;           /* destination port */
  213.          unsigned short uh_ulen;            /* udp length */
  214.          unsigned short uh_sum;             /* udp checksum */
  215. } udphdr_bsd;
  216. #else
  217.  typedef struct udphdr udphdr_bsd;
  218. #endif 
  219.  
  220.  
  221. #ifndef HAVE_STRUCT_ICMP
  222. #define HAVE_STRUCT_ICMP
  223. /* From Linux /usr/include/netinet/ip_icmp.h GLIBC */
  224.  
  225. /*
  226.  * Internal of an ICMP Router Advertisement
  227.  */
  228. struct icmp_ra_addr
  229. {
  230.   u_int32_t ira_addr;
  231.   u_int32_t ira_preference;
  232. };
  233.  
  234. struct icmp
  235. {
  236.   u_int8_t  icmp_type;  /* type of message, see below */
  237.   u_int8_t  icmp_code;  /* type sub code */
  238.   u_int16_t icmp_cksum; /* ones complement checksum of struct */
  239.   union
  240.   {
  241.     u_char ih_pptr;             /* ICMP_PARAMPROB */
  242.     struct in_addr ih_gwaddr;   /* gateway address */
  243.     struct ih_idseq             /* echo datagram */
  244.     {
  245.       u_int16_t icd_id;
  246.       u_int16_t icd_seq;
  247.     } ih_idseq;
  248.     u_int32_t ih_void;
  249.  
  250.     /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
  251.     struct ih_pmtu
  252.     {
  253.       u_int16_t ipm_void;
  254.       u_int16_t ipm_nextmtu;
  255.     } ih_pmtu;
  256.  
  257.     struct ih_rtradv
  258.     {
  259.       u_int8_t irt_num_addrs;
  260.       u_int8_t irt_wpa;
  261.       u_int16_t irt_lifetime;
  262.     } ih_rtradv;
  263.   } icmp_hun;
  264. #define icmp_pptr       icmp_hun.ih_pptr
  265. #define icmp_gwaddr     icmp_hun.ih_gwaddr
  266. #define icmp_id         icmp_hun.ih_idseq.icd_id
  267. #define icmp_seq        icmp_hun.ih_idseq.icd_seq
  268. #define icmp_void       icmp_hun.ih_void
  269. #define icmp_pmvoid     icmp_hun.ih_pmtu.ipm_void
  270. #define icmp_nextmtu    icmp_hun.ih_pmtu.ipm_nextmtu
  271. #define icmp_num_addrs  icmp_hun.ih_rtradv.irt_num_addrs
  272. #define icmp_wpa        icmp_hun.ih_rtradv.irt_wpa
  273. #define icmp_lifetime   icmp_hun.ih_rtradv.irt_lifetime
  274.   union
  275.   {
  276.     struct
  277.     {
  278.       u_int32_t its_otime;
  279.       u_int32_t its_rtime;
  280.       u_int32_t its_ttime;
  281.     } id_ts;
  282.     struct
  283.     {
  284.       struct ip idi_ip;
  285.       /* options and then 64 bits of data */
  286.     } id_ip;
  287.     struct icmp_ra_addr id_radv;
  288.     u_int32_t   id_mask;
  289.     u_int8_t    id_data[1];
  290.   } icmp_dun;
  291. #define icmp_otime      icmp_dun.id_ts.its_otime
  292. #define icmp_rtime      icmp_dun.id_ts.its_rtime
  293. #define icmp_ttime      icmp_dun.id_ts.its_ttime
  294. #define icmp_ip         icmp_dun.id_ip.idi_ip
  295. #define icmp_radv       icmp_dun.id_radv
  296. #define icmp_mask       icmp_dun.id_mask
  297. #define icmp_data       icmp_dun.id_data
  298. };
  299. #endif /* HAVE_STRUCT_ICMP */
  300.  
  301.  /* This ideally should be a port that isn't in use for any protocol on our machine or on the target */
  302. #define MAGIC_PORT 49724
  303. #define TVAL2LONG(X)  X.tv_sec * 1e6 + X.tv_usec
  304. #define SA struct sockaddr
  305.  
  306. /* Prototypes */
  307.  
  308. /* Tries to resolve given hostname and stores
  309.    result in ip .  returns 0 if hostname cannot
  310.    be resolved */
  311. #ifdef WIN32
  312. inline int Sendto(char *functionname, int sd, char *packet, int len, 
  313.        unsigned int flags, struct sockaddr *to, int tolen,struct interface_info *ainfo);
  314. int send_tcp_raw( int sd, struct in_addr *source, 
  315.           struct in_addr *victim, unsigned short sport, 
  316.           unsigned short dport, unsigned int seq,
  317.           unsigned int ack, unsigned char flags,
  318.           unsigned short window, char *options, int optlen,
  319.           char *data, unsigned short datalen,struct interface_info *ainfo);
  320. int send_udp_raw( int sd, struct in_addr *source, 
  321.           struct in_addr *victim, unsigned short sport, 
  322.           unsigned short dport, char *data, unsigned short datalen,struct interface_info *ainfo);
  323. int send_small_fragz(int sd, struct in_addr *source, struct in_addr *victim,
  324.                      unsigned long seq, int sport, int dport, int flags,struct interface_info *ainfo);
  325. int send_ip_raw( int sd, struct in_addr *source, 
  326.           struct in_addr *victim, unsigned char proto,
  327.           char *data, unsigned short datalen,struct interface_info *ainfo);
  328. int send_tcp_raw_decoys( int sd, struct in_addr *victim, unsigned short sport, 
  329.                          unsigned short dport, unsigned int seq,
  330.                          unsigned int ack, unsigned char flags,
  331.                          unsigned short window, char *options, int optlen,
  332.                          char *data, unsigned short datalen,struct interface_info *ainfo);
  333.  
  334. int send_udp_raw_decoys( int sd, struct in_addr *victim, unsigned short sport, 
  335.              unsigned short dport, char *data, 
  336.              unsigned short datalen,struct interface_info *ainfo);
  337.  
  338. int send_small_fragz_decoys(int sd, struct in_addr *victim, unsigned long seq, 
  339.                             int sport, int dport, int flags,struct interface_info *ainfo);
  340.  
  341.  
  342. int send_ip_raw_decoys( int sd, struct in_addr *victim, unsigned char proto,
  343.                         char *data, unsigned short datalen,struct interface_info *ainfo);
  344. /* Convert an IP address to the device (IE ppp0 eth0) using that address */
  345. int ipaddr2dev( struct interface_info *device, struct in_addr *addr );
  346. struct interface_info routethrough(struct in_addr *dest, struct in_addr *source);
  347. #else
  348. inline int Sendto(char *functionname, int sd, char *packet, int len, 
  349.        unsigned int flags, struct sockaddr *to, int tolen);
  350. int send_tcp_raw( int sd, struct in_addr *source, 
  351.           struct in_addr *victim, unsigned short sport, 
  352.           unsigned short dport, unsigned int seq,
  353.           unsigned int ack, unsigned char flags,
  354.           unsigned short window, char *options, int optlen,
  355.           char *data, unsigned short datalen);
  356. int send_udp_raw( int sd, struct in_addr *source, 
  357.           struct in_addr *victim, unsigned short sport, 
  358.           unsigned short dport, char *data, unsigned short datalen);
  359. int send_small_fragz(int sd, struct in_addr *source, struct in_addr *victim,
  360.                      unsigned long seq, int sport, int dport, int flags);
  361. int send_ip_raw( int sd, struct in_addr *source, 
  362.           struct in_addr *victim, unsigned char proto,
  363.           char *data, unsigned short datalen);
  364. int send_tcp_raw_decoys( int sd, struct in_addr *victim, unsigned short sport, 
  365.                          unsigned short dport, unsigned int seq,
  366.                          unsigned int ack, unsigned char flags,
  367.                          unsigned short window, char *options, int optlen,
  368.                          char *data, unsigned short datalen);
  369.  
  370. int send_udp_raw_decoys( int sd, struct in_addr *victim, unsigned short sport, 
  371.              unsigned short dport, char *data, 
  372.              unsigned short datalen);
  373.  
  374. int send_small_fragz_decoys(int sd, struct in_addr *victim, unsigned long seq, 
  375.                             int sport, int dport, int flags);
  376.  
  377.  
  378. int send_ip_raw_decoys( int sd, struct in_addr *victim, unsigned char proto,
  379.                         char *data, unsigned short datalen);
  380. /* Convert an IP address to the device (IE ppp0 eth0) using that address */
  381. int ipaddr2devname( char *dev, struct in_addr *addr );
  382. char *routethrough(struct in_addr *dest, struct in_addr *source);
  383. #endif
  384. int resolve(char *hostname, struct in_addr *ip);
  385. /* An awesome function to determine what interface a packet to a given
  386.    destination should be routed through.  It returns NULL if no appropriate
  387.    interface is found, oterwise it returns the device name and fills in the
  388.    source parameter */
  389.  
  390. unsigned short in_cksum(unsigned short *ptr,int nbytes);
  391.  
  392. /* Decoy versions of the raw packet sending functions ... */
  393.  
  394. /* Calls pcap_open_live and spits out an error (and quits) if the call fails.
  395.    So a valid pcap_t will always be returned. */
  396. pcap_t *my_pcap_open_live(char *device, int snaplen, int promisc, int to_ms);
  397.  
  398. /* A simple function I wrote to help in debugging, shows the important fields
  399.    of a TCP packet*/
  400. int readtcppacket(char *packet, int readdata);
  401. int readudppacket(char *packet, int readdata);
  402. /* Convert an IP address to the device (IE ppp0 eth0) using that address */
  403. /*int ipaddr2devname( char *dev, struct in_addr *addr );*/
  404. /* And vice versa */
  405. int devname2ipaddr(char *dev, struct in_addr *addr);
  406. /* Where the above 2 functions get their info */
  407. struct interface_info *getinterfaces(int *howmany);
  408. inline void sethdrinclude(int sd);
  409. int getsourceip(struct in_addr *src, struct in_addr *dst);
  410. /* Get the source IP and interface name that a packet
  411.    to dst should be sent to.  Interface name is dynamically
  412.    assigned and thus should be freed */
  413. char *getsourceif(struct in_addr *src, struct in_addr *dst);
  414. int islocalhost(struct in_addr *addr);
  415. int unblock_socket(int sd);
  416. /* Standard swiped internet checksum routine */
  417. unsigned short in_cksum(unsigned short *ptr,int nbytes);
  418. /* Hex dump */
  419. int get_link_offset(char *device);
  420. char *readip_pcap(pcap_t *pd, unsigned int *len, long to_usec);
  421. #ifndef HAVE_INET_ATON
  422. int inet_aton(register const char *, struct in_addr *);
  423. #endif
  424.  
  425.  
  426. #endif /*TCPIP_H*/
  427.  
  428.  
  429.  
  430.  
  431.  
  432.