/*
 * Copyright (c) 2001 Fenris, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * File:  headers.h
 *
 * Purpose:  We chose to define our types based on the TCP/IP layers.  By
 * adding more types later, we will be able to expand this tool to handle
 * additional protocols.  I had a very pretty text picture in the original
 * version of this file, but Zak trashed it.  If you need a visual for this
 * information (or just better understanding) I recommend Stevens' TCP/IP
 * Illustrated, Volume I.   --Hunter
 *
 */
#ifndef _HEADERS_H
#define _HEADERS_H

/*******************************  Ethernet  *******************************/
#define ETH_TYP_IP      0x0800
#define ETH_TYP_ARP     0x0806
#define ETH_TYP_RARP    0x8035
#define ETH_TYP_IPV6    0x86dd
#define ETH_TYP_LO      0x9000

typedef struct {
    unsigned char       eth_dst[6];             /* destination MAC address */
    unsigned char       eth_src[6];             /* source MAC address */
    unsigned short      eth_typ;                /* frame type */
} Eth_Header;


/*******************************  ARP/RARP  *******************************/
#define ARP_TYP_ETH     1
#define ARP_TYP_802     6
#define ARP_REQUEST     1
#define ARP_REPLY       2
#define RARP_REQUEST    3
#define RARP_REPLY      4

typedef struct {
    unsigned short      arp_hrd_typ;            /* hardware type */
    unsigned short      arp_prt_typ;            /* protocol type */
    unsigned char       arp_hrd_len;            /* hardware length */
    unsigned char       arp_prt_len;            /* protocol length */
    unsigned short      arp_typ;                /* ARP operation type */
    unsigned char       arp_sdr_mac[6];         /* sender MAC address */
    unsigned char       arp_sdr_ip[4];          /* sender IP address */
    unsigned char       arp_tgt_mac[6];         /* target MAC address */
    unsigned char       arp_tgt_ip[4];          /* target IP address */
} Arp_Header;


/*******************************  IP  *******************************/
#define IP_PRT_ICMP     1
#define IP_PRT_IGMP     2
#define IP_PRT_TCP      6
#define IP_PRT_UDP      17
#define IP_PRT_MUX      18
#define IP_PRT_LCL      63

typedef struct {
    unsigned int        ip_hdr_len : 4;         /* header length (in words) */
    unsigned int        ip_ver : 4;             /* IP version number */
    unsigned char       ip_tos;                 /* type of service */
    unsigned short      ip_pkt_len;             /* total length (in bytes */
    unsigned short      ip_id;                  /* identification */
    unsigned short      ip_frg_off;             /* fragment offset */
    unsigned char       ip_ttl;                 /* time to live */
    unsigned char       ip_protocol;            /* protocol type */
    unsigned short      ip_chksum;              /* header checksum */
    unsigned int        ip_src;                 /* source IP address */
    unsigned int        ip_dst;                 /* destination IP address */
} Ip_Header;


/*******************************  TCP  *******************************/
#define TCP_FIN         0x01
#define TCP_SYN         0x02
#define TCP_RST         0x04
#define TCP_PSH         0x08
#define TCP_ACK         0x10
#define TCP_URG         0x20

typedef struct {
    unsigned short      tcp_src_port;           /* source port number */
    unsigned short      tcp_dst_port;           /* destination port number */
    unsigned int        tcp_seq_num;            /* sequence number */
    unsigned int        tcp_ack_num;            /* acknowledgement number */
    unsigned int        tcp_reserved : 4;       /* unused */
    unsigned int        tcp_hdr_len : 4;        /* header length (in words */
    unsigned char       tcp_flags;              /* TCP flags */
    unsigned short      tcp_win_siz;            /* window size */
    unsigned short      tcp_chksum;             /* header checksum */
    unsigned short      tcp_urg_ptr;            /* urgent pointer */
} Tcp_Header;


/*******************************  UDP  *******************************/

typedef struct {
    unsigned short      udp_src_port;           /* source port number */
    unsigned short      udp_dst_port;           /* destination port number */
    unsigned short      udp_len;                /* length */
    unsigned short      udp_chksum;             /* checksum */
} Udp_Header;


/* ICMP */
#define ICMP_ECHOREPLY          0               /* echo reply */
#define ICMP_UNREACH            3               /* dest unreachable, codes: */
#define     ICMP_UNREACH_NET                    0   /* bad net */
#define     ICMP_UNREACH_HOST                   1   /* bad host */
#define     ICMP_UNREACH_PROTOCOL               2   /* bad protocol */
#define     ICMP_UNREACH_PORT                   3   /* bad port */
#define     ICMP_UNREACH_NEEDFRAG               4   /* IP_DF caused drop */
#define     ICMP_UNREACH_SRCFAIL                5   /* src route failed */
#define     ICMP_UNREACH_NET_UNKNOWN            6   /* unknown net */
#define     ICMP_UNREACH_HOST_UNKNOWN           7   /* unknown host */
#define     ICMP_UNREACH_ISOLATED               8   /* src host isolated */
#define     ICMP_UNREACH_NET_PROHIB             9   /* prohibited access */
#define     ICMP_UNREACH_HOST_PROHIB            10  /* ditto */
#define     ICMP_UNREACH_TOSNET                 11  /* bad tos for net */
#define     ICMP_UNREACH_TOSHOST                12  /* bad tos for host */
#define     ICMP_UNREACH_FILTER_PROHIB          13  /* admin prohib */
#define     ICMP_UNREACH_HOST_PRECEDENCE        14  /* host prec vio. */
#define     ICMP_UNREACH_PRECEDENCE_CUTOFF      15  /* prec cutoff */
#define ICMP_SOURCEQUENCH       4               /* packet lost, slow down */
#define ICMP_REDIRECT           5               /* shorter route, codes: */
#define     ICMP_REDIRECT_NET                   0   /* for network */
#define     ICMP_REDIRECT_HOST                  1   /* for host */
#define     ICMP_REDIRECT_TOSNET                2   /* for tos and net */
#define     ICMP_REDIRECT_TOSHOST               3   /* for tos and host */
#define ICMP_ECHO               8               /* echo service */
#define ICMP_ROUTERADVERT       9               /* router advertisement */
#define ICMP_ROUTERSOLICIT      10              /* router solicitation */
#define ICMP_TIMXCEED           11              /* time exceeded, code: */
#define     ICMP_TIMXCEED_INTRANS               0   /* ttl==0 in transit */
#define     ICMP_TIMXCEED_REASS                 1   /* ttl==0 in reass */
#define ICMP_PARAMPROB          12              /* ip header bad */
#define     ICMP_PARAMPROB_ERRATPTR             0   /* error at param ptr */
#define     ICMP_PARAMPROB_OPTABSENT            1   /* req. opt. absent */
#define     ICMP_PARAMPROB_LENGTH               2   /* bad length */
#define ICMP_TSTAMP             13              /* timestamp request */
#define ICMP_TSTAMPREPLY        14              /* timestamp reply */
#define ICMP_IREQ               15              /* information request */
#define ICMP_IREQREPLY          16              /* information reply */
#define ICMP_MASKREQ            17              /* address mask request */
#define ICMP_MASKREPLY          18              /* address mask reply */

typedef struct {
    unsigned char       icmp_typ;               /* type number (see above) */
    unsigned char       icmp_code;              /* code number (see above) */
    unsigned short      icmp_chksum;            /* checksum */
} Icmp_Header;


/*******************************  IGMP  *******************************/
#define IGMP_TYP_QRY    1
#define IGMP_TYP_RSP    2

typedef struct {
    unsigned int        igmp_typ : 4;           /* type number */
    unsigned int        igmp_ver : 4;           /* version number */
    unsigned short      igmp_chksum;            /* checksum */
    unsigned int        igmp_addr;              /* group address */
} Igmp_Header;


/**********************************************************************/

union LL {
    Eth_Header *    eth;
};

union NL {
    Arp_Header *    arp;
    Ip_Header *     ip;
};

union TL {
    Tcp_Header *    tcp;
    Udp_Header *    udp;
    Icmp_Header *   icmp;
    Igmp_Header *   igmp;
};

enum Type {unknown_t, arp_t, tcp_t, udp_t, icmp_t, igmp_t};

typedef struct {
    union LL ll;
    union NL nl;
    union TL tl;
    const unsigned char *data;
    enum Type type;
} Packet_Headers;

#endif /* _HEADERS_H */


syntax highlighted by Code2HTML, v. 0.9.1