IPHeader


C Specification

#include <dolphin/ip.h>

#define IP_ALEN             4       // IP address length

#define IP_VER              0x4     // IP version 4
#define IP_VERHLEN          0x45    // Default 20 bytes header

// Fragment information
#define IP_DF               0x4000  // Don't fragment
#define IP_MF               0x2000  // More fragments
#define IP_FO               0x1fff  // Fragment offset

#define IP_FRAG(ip)         (((ip)->frag & IP_FO) << 3)

#define IP_HLEN(ip)         (((ip)->verlen & 0x0f) << 2)
#define IP_MIN_HLEN         20
#define IP_MAX_HLEN         60

// Protocol numbers
#define IP_PROTO_ICMP       1
#define IP_PROTO_TCP        6
#define IP_PROTO_UDP        17

typedef struct IPHeader
{
    u8   verlen;            // Version and header length
    u8   tos;               // Type of service
    u16  len;               // packet length
    u16  id;                // Identification
    u16  frag;              // Fragment information
    u8   ttl;               // Time to live
    u8   proto;             // Protocol
    u16  sum;               // Header checksum
    u8   src[IP_ALEN];      // Ip source
    u8   dst[IP_ALEN];      // Ip destination
} IPHeader;

Description

An IP datagram begins IP header followed by IP options (if any) and data:

IP header

IP data

See Also

UDPHeader, TCPHeader

Postel, J. B., ed. 1981 "Internet Protocol", RFC 791 (Sept.).