Main Page | Class List | File List | Class Members | File Members

packetHeader.h

Go to the documentation of this file.
00001 #ifndef PACKET_HDR_H
00002 #define PACKET_HDR_H
00003 
00004 // standard C headers
00005 #include <string.h>
00006 #include <stdio.h>
00007 #include <unistd.h>
00008 #include <stdlib.h>
00009 
00010 // protocol headers
00011 #include <linux/if_ether.h>
00012 #include <net/if_arp.h>
00013 #include <netinet/ip.h>
00014 #include <netinet/ip_icmp.h>
00015 #include <netinet/tcp.h>
00016 #include <netinet/udp.h>
00017 #include <netinet/udp.h>
00018 
00019 #include <string.h> // for memset, memcpy
00020 
00021 #define RST_ETH_HDR_LEN 14
00022 #define RST_ARP_HDR_LEN 28
00023 #define RST_IP_HDR_LEN 20       // without options
00024 #define RST_TCP_HDR_LEN 20      // without options
00025 #define RST_UDP_HDR_LEN 8
00026 #define RST_ICMP_HDR_LEN 8
00027 
00031 struct rst_arphdr {
00032         // fixed portion
00033         unsigned short int format2;     // layer 2 address format
00034         unsigned short int format3;     // layer 3 address format
00035         unsigned char length2;          // layer 2 address length
00036         unsigned char length3;          // layer3 address length
00037         unsigned short int opCode;      // ARP opcode
00038 
00039         // variable portion, specialized here for Ethernet/IP
00040         unsigned char senderMAC[6]; // sender hardware address
00041         unsigned char senderIP[4];  // sender IP address
00042         unsigned char targetMAC[6]; // target hardware address
00043         unsigned char targetIP[4];  // target IP address
00044 };
00045 
00046 // ***** header initialization
00047 
00048 void rst_initEth(struct ethhdr*,unsigned char*,unsigned char*,unsigned short);
00049 
00050 void rst_initARPrequest(struct rst_arphdr*,
00051  unsigned char*,unsigned int,unsigned int);
00052 
00053 void rst_initARPreply(struct rst_arphdr*,
00054  unsigned char*,unsigned int,unsigned char*,unsigned int);
00055 
00056 void rst_initIP(struct iphdr*,
00057  unsigned short,unsigned char,unsigned int,unsigned int);
00058 
00059 void rst_initICMP(struct icmphdr *,unsigned char);
00060 
00061 void rst_initTCP(struct tcphdr*,unsigned short,unsigned short);
00062 
00063 void rst_initUDP(struct udphdr *,unsigned short,unsigned short,unsigned short);
00064 
00065 // ***** htonp and ntohp: byte swap and checksum calculations
00066 
00067 #define RST_ETH_HDR     0x01
00068 #define RST_ARP_HDR     0x02
00069 #define RST_IP_HDR      0x04
00070 #define RST_ICMP_HDR    0x20
00071 #define RST_TCP_HDR     0x08
00072 #define RST_UDP_HDR     0x10
00073 
00074 struct rst_buf {
00075         unsigned char* buf;
00076         int bufLen;
00077 
00078         struct ethhdr* ethhdr;
00079         struct rst_arphdr* arphdr;
00080         struct iphdr* iphdr;
00081         struct icmphdr* icmphdr;
00082         struct tcphdr* tcphdr;
00083         struct udphdr* udphdr;
00084 };
00085 
00086 void rst_initRbuf(struct rst_buf*,unsigned char*,int,unsigned int);
00087 
00088 int rst_verifyRbuf(struct rst_buf*);
00089 
00090 void rst_htonp(struct rst_buf*);
00091 
00092 void rst_ntohp(struct rst_buf*);
00093 
00094 unsigned char* rst_ethData(struct rst_buf*);
00095 
00096 unsigned char* rst_ipData(struct rst_buf*);
00097 
00098 unsigned char* rst_icmpData(struct rst_buf*);
00099 
00100 unsigned char* rst_tcpData(struct rst_buf*);
00101 
00102 unsigned char* rst_udpData(struct rst_buf*);
00103 
00104 // ***** checksum calculation
00105 
00106 unsigned short rst_getIPchecksum(struct iphdr*);
00107 
00108 unsigned short rst_getICMPchecksum(struct icmphdr*,unsigned short);
00109 
00110 unsigned short rst_getTCPchecksum(struct iphdr*,struct tcphdr*,unsigned short);
00111 
00112 unsigned short rst_getUDPchecksum(struct iphdr*,struct udphdr*,unsigned short);
00113 
00114 #endif

Generated on Sun Jul 18 17:30:56 2004 by doxygen 1.3.6