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

packetHeader.h File Reference

#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/if_ether.h>
#include <net/if_arp.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>

Go to the source code of this file.

Classes

struct  rst_arphdr
 ARP header specialized for Ethernet at layer 2 and IP at layer 3. More...

struct  rst_buf

Defines

#define RST_ETH_HDR_LEN   14
#define RST_ARP_HDR_LEN   28
#define RST_IP_HDR_LEN   20
#define RST_TCP_HDR_LEN   20
#define RST_UDP_HDR_LEN   8
#define RST_ICMP_HDR_LEN   8
#define RST_ETH_HDR   0x01
#define RST_ARP_HDR   0x02
#define RST_IP_HDR   0x04
#define RST_ICMP_HDR   0x20
#define RST_TCP_HDR   0x08
#define RST_UDP_HDR   0x10

Functions

void rst_initEth (struct ethhdr *, unsigned char *, unsigned char *, unsigned short)
 Initialize an Ethernet header.

void rst_initARPrequest (struct rst_arphdr *, unsigned char *, unsigned int, unsigned int)
 Initialize an ARP request header.

void rst_initARPreply (struct rst_arphdr *, unsigned char *, unsigned int, unsigned char *, unsigned int)
 Initialize an ARP reply header.

void rst_initIP (struct iphdr *, unsigned short, unsigned char, unsigned int, unsigned int)
 Initialize an IP header.

void rst_initICMP (struct icmphdr *, unsigned char)
 Initialize an ICMP header.

void rst_initTCP (struct tcphdr *, unsigned short, unsigned short)
 Initialize a TCP header.

void rst_initUDP (struct udphdr *, unsigned short, unsigned short, unsigned short)
 Initialize a UDP header.

void rst_initRbuf (struct rst_buf *, unsigned char *, int, unsigned int)
 Initialize an rst_buf.

int rst_verifyRbuf (struct rst_buf *)
 Check that an rst_buf is safe, in the sense that a call to rst_htonp will not abort.

void rst_htonp (struct rst_buf *)
 Do host-to-network byte swapping and checksum calculation.

void rst_ntohp (struct rst_buf *)
 Parse an incoming packet and do network-to-host byte swapping.

unsigned char * rst_ethData (struct rst_buf *)
 Get the address of the Ethernet header in rBuf.

unsigned char * rst_ipData (struct rst_buf *)
 Get the address of the IP header in rBuf.

unsigned char * rst_icmpData (struct rst_buf *)
 Get the address of the ICMP header in rBuf.

unsigned char * rst_tcpData (struct rst_buf *)
 Get the address of the TCP header in rBuf.

unsigned char * rst_udpData (struct rst_buf *)
 Get the address of the UDP header in rBuf.

unsigned short rst_getIPchecksum (struct iphdr *)
 Compute an IP checksum.

unsigned short rst_getICMPchecksum (struct icmphdr *, unsigned short)
 Compute an ICMP checksum.

unsigned short rst_getTCPchecksum (struct iphdr *, struct tcphdr *, unsigned short)
 Compute an TCP checksum.

unsigned short rst_getUDPchecksum (struct iphdr *, struct udphdr *, unsigned short)
 Compute an UDP checksum.


Define Documentation

#define RST_ARP_HDR   0x02
 

#define RST_ARP_HDR_LEN   28
 

#define RST_ETH_HDR   0x01
 

#define RST_ETH_HDR_LEN   14
 

#define RST_ICMP_HDR   0x20
 

#define RST_ICMP_HDR_LEN   8
 

#define RST_IP_HDR   0x04
 

#define RST_IP_HDR_LEN   20
 

#define RST_TCP_HDR   0x08
 

#define RST_TCP_HDR_LEN   20
 

#define RST_UDP_HDR   0x10
 

#define RST_UDP_HDR_LEN   8
 


Function Documentation

unsigned char* rst_ethData struct rst_buf  ) 
 

Get the address of the Ethernet header in rBuf.

unsigned short rst_getICMPchecksum struct icmphdr *  ,
unsigned  short
 

Compute an ICMP checksum.

unsigned short rst_getIPchecksum struct iphdr *   ) 
 

Compute an IP checksum.

unsigned short rst_getTCPchecksum struct iphdr *  ,
struct tcphdr *  ,
unsigned  short
 

Compute an TCP checksum.

unsigned short rst_getUDPchecksum struct iphdr *  ,
struct udphdr *  ,
unsigned  short
 

Compute an UDP checksum.

void rst_htonp struct rst_buf rBuf  ) 
 

Do host-to-network byte swapping and checksum calculation.

Parameters:
rBuf an initialized rst_buf
Precondition:
rBuf.buf[0..rBuf.bufLen-1] is allocated

for each non-null pointer p in rBuf, *p is in rBuf.buf[0..rBuf.bufLen-1]

for each non-null pointer p in rBuf, any length fields in *p specify a header that lies in rBuf.buf[0..rBuf.bufLen-1]

Postcondition:
for each non-null pointer p in rBuf, appropriate byte swapping and checksum calculation has been done.

unsigned char* rst_icmpData struct rst_buf  ) 
 

Get the address of the ICMP header in rBuf.

void rst_initARPreply struct rst_arphdr arphdr,
unsigned char *  senderMAC,
unsigned int  senderIP,
unsigned char *  targetMAC,
unsigned int  targetIP
 

Initialize an ARP reply header.

Parameters:
arphdr address of header to be initialized
senderMAC sender's MAC address
senderIP sender's IP address
targetMAC target's MAC address
targetIP target's IP address
Precondition:
arphdr points to at least RST_ARP_HDR_LEN bytes

senderMAC points to at least 6 bytes

targetMAC points to at least 6 bytes

Postcondition:
arphdr initialized, in host byte order

void rst_initARPrequest struct rst_arphdr arphdr,
unsigned char *  senderMAC,
unsigned int  senderIP,
unsigned int  targetIP
 

Initialize an ARP request header.

Parameters:
arphdr address of header to be initialized
senderMAC sender's MAC address
senderIP sender's IP address
targetIP target's IP address
Precondition:
arphdr points to at least RST_ARP_HDR_LEN bytes

senderMAC points to at least 6 bytes

Postcondition:
arphdr initialized, in host byte order

void rst_initEth struct ethhdr *  ethhdr,
unsigned char *  dst,
unsigned char *  src,
unsigned short  proto
 

Initialize an Ethernet header.

Parameters:
ethhdr address of header to be initialized
dst destination MAC address
src source MAC address
proto next layer protocol
Precondition:
ethhdr points to at least RST_ETH_HDR_LEN bytes

dst points to at least 6 bytes

src points to at least 6 bytes

Postcondition:
ethhdr initialized, in host byte order

void rst_initICMP struct icmphdr *  icmphdr,
unsigned char  type
 

Initialize an ICMP header.

Parameters:
icmphdr address of header to be initialized
type ICMP type
Precondition:
icmphdr points to at least RST_ICMP_HDR_LEN bytes
Postcondition:
icmphdr initialized, in host byte order

void rst_initIP struct iphdr *  iphdr,
unsigned short  totalLen,
unsigned char  protocol,
unsigned int  srcAddr,
unsigned int  dstAddr
 

Initialize an IP header.

Parameters:
iphdr address of header to be initialized
totalLen total IP packet length in bytes
protocol next layer protocol
srcAddr IP source address
dstAddr IP destination address
Precondition:
iphdr points to at least RST_IP_HDR_LEN bytes
Postcondition:
iphdr initialized, in host byte order

void rst_initRbuf struct rst_buf rBuf,
unsigned char *  buf,
int  bufLen,
unsigned int  mask
 

Initialize an rst_buf.

Parameters:
rBuf rst_buf to be initialized
buf frame byte array
bufLen length of buf
mask specification of active header pointers
Postcondition:
rBuf.buf == buf

rBuf.bufLen == bufLen

the masked rBuf pointers point to the appropriate locations in buf; the unmasked pointers are null

void rst_initTCP struct tcphdr *  tcphdr,
unsigned short  srcPort,
unsigned short  dstPort
 

Initialize a TCP header.

Parameters:
tcphdr address of header to be initialized
srcPort source port
dstPort destination port
Precondition:
tcphdr points to at least RST_TCP_HDR_LEN bytes
Postcondition:
tcphdr initialized, in host byte order

void rst_initUDP struct udphdr *  udphdr,
unsigned short  srcPort,
unsigned short  dstPort,
unsigned short  len
 

Initialize a UDP header.

Parameters:
udphdr address of header to be initialized
srcPort source port
dstPort destination port
len total UDP length in bytes
Precondition:
udphdr points to at least RST_UDP_HDR_LEN bytes
Postcondition:
udphdr initialized, in host byte order

unsigned char* rst_ipData struct rst_buf  ) 
 

Get the address of the IP header in rBuf.

void rst_ntohp struct rst_buf rBuf  ) 
 

Parse an incoming packet and do network-to-host byte swapping.

Parameters:
rBuf an rst_buf containing the packet to be parsed in rBuf.buf[0..rBuf.bufLen-1]
Precondition:
rBuf.buf[0..rBuf.bufLen-1] is allocated and "legal"
Postcondition:
for each non-null pointer p in rBuf, appropriate byte swapping and checksum calculation has been done.

unsigned char* rst_tcpData struct rst_buf  ) 
 

Get the address of the TCP header in rBuf.

unsigned char* rst_udpData struct rst_buf  ) 
 

Get the address of the UDP header in rBuf.

int rst_verifyRbuf struct rst_buf rBuf  ) 
 

Check that an rst_buf is safe, in the sense that a call to rst_htonp will not abort.

Three kinds of checks are made:

  1. The non-null header pointers are one of:
    • Ethernet
    • Ethernet and ARP
    • Ethernet and IP
    • Ethernet, IP and ICMP
    • Ethernet, IP and TCP
    • Ethernet, IP and UDP
  2. For the non-null header pointers, the headers start at buf[0], are contiguous and non-overlapping, and are completely contained in buf.
  3. The data indicated by the dynamic length fields, where present, are completely contained in buf.
Parameters:
rBuf an rst_buf
Precondition:
rBuf.buf[0..rBuf.bufLen-1] is allocated

rBuf.bufLen >= 0

no IP or TCP options are present

Returns:
0 if rBuf is safe; non-zero otherwise


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