File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / hping2 / ars.h
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 22:11:37 2012 UTC (12 years, 3 months ago) by misho
Branches: hping2, MAIN
CVS tags: v2_0_0rc3p7, v2_0_0rc3p5, v2_0_0rc3p4, v2_0_0rc3p0, v2_0_0rc3, HEAD
hping2

    1: /* Copyright (C) 2000,2001 Salvatore Sanfilippo <antirez@invece.org>
    2:  * See the LICENSE file for more information. */
    3: 
    4: /* $Id: ars.h,v 1.1.1.1 2012/02/21 22:11:37 misho Exp $ */
    5: 
    6: #ifndef _ARS_H
    7: #define _ARS_H
    8: 
    9: /* define before including sys/socket.h */
   10: #if defined(__APPLE__)
   11: #define _BSD_SOCKLEN_T_ int
   12: #endif
   13: 
   14: #include <sys/types.h>
   15: #include <sys/socket.h>
   16: #include "systype.h"
   17: #include "in.h"
   18: #include "bytesex.h"
   19: 
   20: #ifndef TRUE
   21: #define TRUE	1
   22: #define FALSE	0
   23: #endif
   24: 
   25: 
   26: #ifndef MIN
   27: #define MIN(x,y)	((x)<(y)?(x):(y))
   28: #endif
   29: 
   30: #ifndef MAX
   31: #define MAX(x,y)	((x)>(y)?(x):(y))
   32: #endif
   33: 
   34: #ifdef DEBUG
   35: #define __D(x) x
   36: #else
   37: #define __D(x) do { } while (0);
   38: #endif
   39: 
   40: #ifndef __u8
   41: #define __u8	u_int8_t
   42: #define __u16	u_int16_t
   43: #define __u32	u_int32_t
   44: #endif
   45: 
   46: /* error codes */
   47: #define ARS_OK          0
   48: #define ARS_ERROR	1
   49: #define ARS_NOSPACE     2
   50: #define ARS_NOMEM       3
   51: #define ARS_INVALID	4
   52: 
   53: /* Headers size */
   54: #define ARS_ICMPHDR_SIZE	sizeof(struct ars_icmphdr)
   55: #define ARS_UDPHDR_SIZE		sizeof(struct ars_udphdr)
   56: #define ARS_TCPHDR_SIZE		sizeof(struct ars_tcphdr)
   57: #define ARS_IPHDR_SIZE		sizeof(struct ars_iphdr)
   58: #define ARS_PSEUDOHDR_SIZE	sizeof(struct pseudohdr)
   59: 
   60: /* IP defines */
   61: #define ARS_MAX_IP_SIZE		65535
   62: 
   63: #define ARS_IP_MF ((unsigned short)0x2000)	/* more fragments */
   64: #define ARS_IP_DF ((unsigned short)0x4000)	/* dont fragment */
   65: #define ARS_IP_RF ((unsigned short)0x8000)	/* reserved fragment flag */
   66: 
   67: #define ARS_IPOPT_COPY		0x80
   68: #define ARS_IPOPT_CLASS_MASK	0x60
   69: #define ARS_IPOPT_NUMBER_MASK	0x1f
   70: 
   71: #define	ARS_IPOPT_COPIED(o)		((o)&ARS_IPOPT_COPY)
   72: #define	ARS_IPOPT_CLASS(o)		((o)&ARS_IPOPT_CLASS_MASK)
   73: #define	ARS_IPOPT_NUMBER(o)		((o)&ARS_IPOPT_NUMBER_MASK)
   74: 
   75: #define	ARS_IPOPT_CONTROL		0x00
   76: #define	ARS_IPOPT_RESERVED1		0x20
   77: #define	ARS_IPOPT_MEASUREMENT		0x40
   78: #define	ARS_IPOPT_RESERVED2		0x60
   79: 
   80: #define ARS_IPOPT_END		(0 |ARS_IPOPT_CONTROL)
   81: #define ARS_IPOPT_NOOP		(1 |ARS_IPOPT_CONTROL)
   82: #define ARS_IPOPT_SEC		(2 |ARS_IPOPT_CONTROL|ARS_IPOPT_COPY)
   83: #define ARS_IPOPT_LSRR		(3 |ARS_IPOPT_CONTROL|ARS_IPOPT_COPY)
   84: #define ARS_IPOPT_TIMESTAMP	(4 |ARS_IPOPT_MEASUREMENT)
   85: #define ARS_IPOPT_RR		(7 |ARS_IPOPT_CONTROL)
   86: #define ARS_IPOPT_SID		(8 |ARS_IPOPT_CONTROL|ARS_IPOPT_COPY)
   87: #define ARS_IPOPT_SSRR		(9 |ARS_IPOPT_CONTROL|ARS_IPOPT_COPY)
   88: #define ARS_IPOPT_RA		(20|ARS_IPOPT_CONTROL|ARS_IPOPT_COPY)
   89: 
   90: #define ARS_IPOPT_OPTVAL 0
   91: #define ARS_IPOPT_OLEN   1
   92: #define ARS_IPOPT_OFFSET 2
   93: #define ARS_IPOPT_MINOFF 4
   94: #define ARS_MAX_IPOPTLEN 40
   95: #define ARS_IPOPT_NOP ARS_IPOPT_NOOP
   96: #define ARS_IPOPT_EOL ARS_IPOPT_END
   97: #define ARS_IPOPT_TS  ARS_IPOPT_TIMESTAMP
   98: 
   99: #define	ARS_IPOPT_TS_TSONLY	0		/* timestamps only */
  100: #define	ARS_IPOPT_TS_TSANDADDR	1		/* timestamps and addresses */
  101: #define	ARS_IPOPT_TS_PRESPEC	3		/* specified modules only */
  102: 
  103: /* IPV4 and IPV6 string rappresentation len */
  104: #define ARS_INET_ADDRSTRLEN	16
  105: #define ARS_INET6_ADDRSTRLEN	46
  106: 
  107: /* TCP */
  108: #define ARS_TCPOPT_EOL		0
  109: #define ARS_TCPOPT_NOP		1
  110: #define ARS_TCPOPT_MAXSEG	2
  111: #define ARS_TCPOPT_WINDOW	3
  112: #define ARS_TCPOPT_SACK_PERM	4
  113: #define ARS_TCPOPT_SACK		5
  114: #define ARS_TCPOPT_ECHOREQUEST	6
  115: #define ARS_TCPOPT_ECHOREPLY	7
  116: #define ARS_TCPOPT_TIMESTAMP	8
  117: 
  118: #define ARS_TCP_TH_FIN	0x01
  119: #define ARS_TCP_TH_SYN	0x02
  120: #define ARS_TCP_TH_RST	0x04
  121: #define ARS_TCP_TH_PUSH	0x08
  122: #define ARS_TCP_TH_ACK	0x10
  123: #define ARS_TCP_TH_URG	0x20
  124: #define	ARS_TCP_TH_X 	0x40	/* X tcp flag */
  125: #define ARS_TCP_TH_Y 	0x80	/* Y tcp flag */
  126: 
  127: /* ICMP TYPE */
  128: #define ARS_ICMP_ECHOREPLY          0       /* Echo Reply                   */
  129: #define ARS_ICMP_DEST_UNREACH       3       /* Destination Unreachable      */
  130: #define ARS_ICMP_SOURCE_QUENCH      4       /* Source Quench                */
  131: #define ARS_ICMP_REDIRECT           5       /* Redirect (change route)      */
  132: #define ARS_ICMP_ECHO               8       /* Echo Request                 */
  133: #define ARS_ICMP_TIME_EXCEEDED      11      /* Time Exceeded                */
  134: #define ARS_ICMP_PARAMETERPROB      12      /* Parameter Problem            */
  135: #define ARS_ICMP_TIMESTAMP          13      /* Timestamp Request            */
  136: #define ARS_ICMP_TIMESTAMPREPLY     14      /* Timestamp Reply              */
  137: #define ARS_ICMP_INFO_REQUEST       15      /* Information Request          */
  138: #define ARS_ICMP_INFO_REPLY         16      /* Information Reply            */
  139: #define ARS_ICMP_ADDRESS            17      /* Address Mask Request         */
  140: #define ARS_ICMP_ADDRESSREPLY       18      /* Address Mask Reply           */
  141: 
  142: /* Codes for UNREACHABLE */
  143: #define ARS_ICMP_UNR_NET		0       /* Network Unreachable */
  144: #define ARS_ICMP_UNR_HOST		1       /* Host Unreachable */
  145: #define ARS_ICMP_UNR_PROT		2       /* Protocol Unreachable */
  146: #define ARS_ICMP_UNR_PORT		3       /* Port Unreachable */
  147: #define ARS_ICMP_UNR_FRAG_NEEDED	4       /* Fragmentation Needed,DF set*/
  148: #define ARS_ICMP_UNR_SR_FAILED          5       /* Source Route failed */
  149: #define ARS_ICMP_UNR_UNK_NET		6
  150: #define ARS_ICMP_UNR_UNK_HOST		7
  151: #define ARS_ICMP_UNR_ISOLATED_HOST	8
  152: #define ARS_ICMP_UNR_NET_ANO		9
  153: #define ARS_ICMP_UNR_HOST_ANO		10
  154: #define ARS_ICMP_UNR_NET_UNR_TOS	11
  155: #define ARS_ICMP_UNR_HOST_UNR_TOS	12
  156: #define ARS_ICMP_UNR_PKT_FILTERED	13      /* Packet filtered */
  157: #define ARS_ICMP_UNR_PREC_VIOLATION	14      /* Precedence violation */
  158: #define ARS_ICMP_UNR_PREC_CUTOFF	15      /* Precedence cut off */
  159: #define ARS_NR_ICMP_UNREACH 15	/* Instead of hardcoded immediate value */
  160: 
  161: /* Codes for REDIRECT */
  162: #define ARS_ICMP_REDIR_NET		0       /* Redirect Net */
  163: #define ARS_ICMP_REDIR_HOST		1       /* Redirect Host */
  164: #define ARS_ICMP_REDIR_NETTOS		2       /* Redirect Net for TOS */
  165: #define ARS_ICMP_REDIR_HOSTTOS		3       /* Redirect Host for TOS */
  166: 
  167: /* Codes for TIME_EXCEEDED */
  168: #define ARS_ICMP_EXC_TTL		0       /* TTL count exceeded */
  169: #define ARS_ICMP_EXC_FRAGTIME		1       /* TTL exceeded reassembling */
  170: 
  171: /* The IP header structure */
  172: struct ars_iphdr {
  173: #if defined(BYTE_ORDER_LITTLE_ENDIAN)
  174:         __u8    ihl:4,
  175:                 version:4;
  176: #elif defined (BYTE_ORDER_BIG_ENDIAN)
  177:         __u8    version:4,
  178:                 ihl:4;
  179: #else
  180: #error  "Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN"
  181: #endif
  182:         __u8    tos;
  183:         __u16   tot_len;
  184:         __u16   id;
  185:         __u16   frag_off;
  186:         __u8    ttl;
  187:         __u8    protocol;
  188:         __u16   check;
  189:         __u32   saddr;
  190:         __u32   daddr;
  191: };
  192: 
  193: /* The IP options structure */
  194: struct ars_ipopt {
  195: 	u_int8_t kind;
  196: 	u_int8_t len;
  197: 	union {
  198: 		struct {
  199: 			u_int16_t s;
  200: 			u_int16_t c;
  201: 			u_int16_t h;
  202: 			u_int8_t tcc[3];
  203: 		} sec;		/* security */
  204: 		struct {
  205: 			u_int8_t ptr;
  206: 			u_int8_t data[37];
  207: 		} src;		/* loose and strinct source routing */
  208: 		struct {
  209: 			u_int8_t ptr;
  210: 			u_int8_t data[37];
  211: 		} rr;		/* record route */
  212: 		struct {
  213: 			u_int16_t id;
  214: 		} sid;		/* stream id */
  215: 		struct {
  216: 			u_int8_t ptr;
  217: 			u_int8_t flags;
  218: 			u_int8_t data[36];
  219: 		} tstamp;	/* timestamp */
  220: 	} un;
  221: };
  222: 
  223: /* The UDP header structure */
  224: struct ars_udphdr { 
  225: 	__u16 uh_sport;     /* source port */
  226: 	__u16 uh_dport;     /* destination port */
  227: 	__u16 uh_ulen;      /* udp length */
  228: 	__u16 uh_sum;       /* udp checksum */
  229: };
  230: 
  231: /* The TCP header structure */
  232: struct ars_tcphdr {
  233: 	__u16	th_sport;               /* source port */
  234: 	__u16	th_dport;               /* destination port */
  235: 	__u32	th_seq;                 /* sequence number */
  236: 	__u32	th_ack;                 /* acknowledgement number */
  237: #if defined (BYTE_ORDER_LITTLE_ENDIAN)
  238: 	__u8    th_x2:4,                /* (unused) */
  239: 		th_off:4;               /* data offset */
  240: #elif defined (BYTE_ORDER_BIG_ENDIAN)
  241: 	__u8    th_off:4,               /* data offset */
  242: 		th_x2:4;                /* (unused) */
  243: #else
  244: #error  "Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN"
  245: #endif
  246: 	__u8    th_flags;
  247: 	__u16   th_win;                 /* window */
  248: 	__u16   th_sum;                 /* checksum */
  249: 	__u16   th_urp;                 /* urgent pointer */
  250: };
  251: 
  252: /* The TCP options structure */
  253: struct ars_tcpopt {
  254: 	u_int8_t kind;
  255: 	u_int8_t len;
  256: 	union {
  257: 		struct {
  258: 			u_int16_t size;
  259: 		} mss;
  260: 		struct {
  261: 			u_int8_t shift;
  262: 		} win;
  263: 		struct {
  264: 			u_int16_t origin;
  265: 			u_int16_t size;
  266: 		} sack[10]; /* 10 SACK blocks in 44 bytes of space */
  267: 		struct {
  268: 			u_int8_t info[4];
  269: 		} echo;
  270: 		struct {
  271: 			u_int8_t tsval[4];
  272: 			u_int8_t tsecr[4];
  273: 		} timestamp;
  274: 	} un;
  275: };
  276: 
  277: /* The ICMP header structure */
  278: struct ars_icmphdr
  279: {
  280: 	__u8          type;
  281: 	__u8          code;
  282: 	__u16         checksum;
  283: 	union
  284: 	{
  285: 		struct
  286: 		{
  287: 			__u16   id;
  288: 			__u16   sequence;
  289: 		} echo; /* called echo since it's the most used */
  290: 		__u32   gateway;
  291: 	} un;
  292: };
  293: 
  294: /* TCP/UDP pseudo header used to compute the checksum */
  295: struct ars_pseudohdr
  296: {
  297: 	__u32 saddr;
  298: 	__u32 daddr;
  299: 	__u8  zero;
  300: 	__u8  protocol;
  301: 	__u16 lenght;
  302: };
  303: 
  304: struct ars_packet; /* forward declaration */
  305: 
  306: /* ARS layer */
  307: struct ars_layer {
  308: 	int l_type;
  309: 	int l_size;
  310: 	int l_flags;
  311: 	void *l_data;
  312: 	struct ars_packet *l_packet;
  313: };
  314: 
  315: #define ARS_MAX_LAYER 16
  316: 
  317: /* Types */
  318: #define ARS_TYPE_SIZE		32
  319: #define ARS_TYPE_NULL		0
  320: #define ARS_TYPE_IP		1
  321: #define ARS_TYPE_IPOPT		2
  322: #define ARS_TYPE_ICMP		3
  323: #define ARS_TYPE_UDP		4
  324: #define ARS_TYPE_TCP		5
  325: #define ARS_TYPE_TCPOPT		6
  326: #define ARS_TYPE_DATA		31
  327: 
  328: /* ARS packet context */
  329: struct ars_packet {
  330: 	char *p_error;
  331: 	int p_layer_nr;
  332: 	struct ars_layer p_layer[ARS_MAX_LAYER];
  333: 	void *p_default[ARS_TYPE_SIZE];
  334: 	int aux; /* Auxiliar variable for data exchange between functions */
  335: };
  336: 
  337: /* Facility to check for flags */
  338: #define ARS_TAKE(f,x)		(f & x)
  339: #define ARS_DONTTAKE(f, x)	(!(f & x))
  340: #define ARS_TAKE_NONE		0
  341: 
  342: /* IP layer flags */
  343: #define ARS_TAKE_IP_VERSION	(1 << 0)
  344: #define ARS_TAKE_IP_HDRLEN	(1 << 1)
  345: #define ARS_TAKE_IP_TOTLEN	(1 << 2)
  346: #define ARS_TAKE_IP_PROTOCOL	(1 << 3)
  347: #define ARS_TAKE_IP_CKSUM	(1 << 4)
  348: 
  349: /* ICMP layer flags */
  350: #define ARS_TAKE_ICMP_CKSUM	(1 << 0)
  351: 
  352: /* UDP layer flags */
  353: #define ARS_TAKE_UDP_CKSUM	(1 << 0)
  354: #define ARS_TAKE_UDP_LEN	(1 << 1)
  355: 
  356: /* TCP layer flags */
  357: #define ARS_TAKE_TCP_HDRLEN	(1 << 0)
  358: #define ARS_TAKE_TCP_CKSUM	(1 << 1)
  359: 
  360: /* Some function that acts on layer switch to the last layer with this */
  361: #define ARS_LAST_LAYER		-1
  362: 
  363: /* Structure and defines needed to calculate the internet-like checksum
  364:  * when the data is splitted in more not adjacent buffers */
  365: #define ARS_MC_INIT     0
  366: #define ARS_MC_UPDATE   1
  367: #define ARS_MC_FINAL    2
  368: 
  369: struct mc_context {
  370: 	u_int32_t oddbyte_flag;
  371: 	u_int32_t old;
  372: 	u_int8_t oddbyte;
  373: 	u_int8_t pad;
  374: };
  375: 
  376: /* ARS layer info structure */
  377: struct ars_layer_info {
  378: 	char *li_name; /* NULL = unused slot */
  379: 	int (*li_compiler) (struct ars_packet *pkt, int layer); /* NULL = NOP */
  380: 	int layer_id;
  381: };
  382: 
  383: /* ARS layer info table */
  384: struct ars_layer_info ars_linfo[ARS_TYPE_SIZE];
  385: 
  386: /* ARS interface managment structure and defines */
  387: #define ARS_IF_UP	(1 << 0)
  388: #define ARS_IF_LOOP	(1 << 1)
  389: #define ARS_IF_IPV4	(1 << 2)
  390: #define ARS_IF_IPV6 	(1 << 3)
  391: #define ARS_IF_MISCONF	(1 << 4)
  392: 
  393: #define ARS_IF_MAX_IFACE	16
  394: #define ARS_IF_NAME_SIZE	32
  395: 
  396: /* iface type are obtained using libpcap to avoid efforts duplication */
  397: struct ars_iface {
  398: 	char if_name[ARS_IF_NAME_SIZE];
  399: 	int if_mtu;
  400: 	int if_flags;
  401: 	char if_ipv4addr[ARS_INET_ADDRSTRLEN];
  402: 	char if_ipv6addr[ARS_INET6_ADDRSTRLEN];
  403: };
  404: 
  405: /* Flags for packet splitting */
  406: #define ARS_SPLIT_FTRUNC        (1 << 0)
  407: #define ARS_SPLIT_FBADCKSUM     (1 << 1)
  408: 
  409: /* More macros */
  410: #define ars_atou(x) strtoul(x, (char **) NULL, 0)
  411: 
  412: /* Prototypes */
  413: int ars_init(struct ars_packet *pkt);
  414: int ars_destroy(struct ars_packet *pkt);
  415: int ars_nospace(struct ars_packet *pkt);
  416: int ars_add_generic(struct ars_packet *pkt, size_t size, int type);
  417: void *ars_add_iphdr(struct ars_packet *pkt, int unused);
  418: void *ars_add_ipopt(struct ars_packet *pkt, int option);
  419: void *ars_add_udphdr(struct ars_packet *pkt, int unused);
  420: void *ars_add_tcphdr(struct ars_packet *pkt, int unused);
  421: void *ars_add_tcpopt(struct ars_packet *pkt, int option);
  422: void *ars_add_icmphdr(struct ars_packet *pkt, int unused);
  423: void *ars_add_data(struct ars_packet *pkt, int size);
  424: size_t ars_relative_size(struct ars_packet *pkt, int layer_nr);
  425: size_t ars_packet_size(struct ars_packet *pkt);
  426: u_int16_t ars_cksum(void *vbuf, size_t nbytes);
  427: u_int16_t ars_multi_cksum(struct mc_context *c, int op, void *vbuf, size_t nbytes);
  428: int ars_compile(struct ars_packet *pkt);
  429: int ars_udptcp_cksum(struct ars_packet *pkt, int layer, u_int16_t *sum);
  430: int ars_open_rawsocket(struct ars_packet *pkt);
  431: int ars_build_packet(struct ars_packet *pkt, unsigned char **packet, size_t *size);
  432: int ars_bsd_fix(struct ars_packet *pkt, unsigned char *packet, size_t size);
  433: int ars_set_flags(struct ars_packet *pkt, int layer, int flags);
  434: int ars_send(int s, struct ars_packet *pkt, struct sockaddr *sa, socklen_t slen);
  435: int ars_resolve(struct ars_packet *pkt, u_int32_t *dest, char *hostname);
  436: int ars_set_error(struct ars_packet *pkt, char *error);
  437: int ars_d_build(struct ars_packet *pkt, char *t);
  438: int ars_valid_layer(int layer);
  439: int ars_get_iface_list(struct ars_iface *iface, size_t *isize);
  440: int ars_get_iface(char *name, struct ars_iface *i);
  441: int ars_valid_layer(int layer);
  442: int ars_remove_layer(struct ars_packet *pkt, int layer);
  443: 
  444: /* split.c prototypes */
  445: int ars_seems_ip(struct ars_iphdr *ip, size_t size);
  446: int ars_guess_ipoff(void *packet, size_t size, int *lhs);
  447: int ars_check_ip_cksum(struct ars_iphdr *ip);
  448: int ars_check_icmp_cksum(struct ars_icmphdr *icmp, size_t size);
  449: int ars_split_packet(void *packet, size_t size, int ipoff, struct ars_packet *pkt);
  450: #endif /* _ARS_H */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>