- #ifndef PACKET_H
- #define PACKET_H
- extern "C"
- {
- #include "ntddk.h"
- #ifndef dprintf
- #define dprintf KdPrint
- #endif
- }
- #include "ndis.h"
- //
- // Ethernet header
- //
- typedef struct _ETHERNET_FRAME
- {
- unsigned char DestinationAddress[6]; // 0xFFFFFF is Broadcast
- unsigned char SourceAddress[6];
- unsigned int FrameType; // in host-order
- } EHTERNET_FRAME, *PETHERNET_FRAME;
- #define ETHERNET_FRAME_LENGTH 14
- #define ETHERNET_FRAME_TYPE_INVALID 0xFFFF // Invalid Ethernet Frame
- #define ETHERNET_FRAME_TYPE_TCPIP 0x0800 // TCP/IP Protocol
- #define ETHERNET_FRAME_TYPE_PUP 0x0200 // PUP Protocol
- #define ETHERNET_FRAME_TYPE_ARP 0x0806 // ARP protocol
- #define ETHERNET_FRAME_TYPE_RARP 0x8035 // RAPR Protocol
- //
- // Ip header
- //
- typedef struct _IP_HEADER
- {
- union
- {
- unsigned char VersionAndHeaderLength; // Version 4 bit, Header Length 4 bit * 4
- struct
- {
- unsigned char HeaderLength : 4;
- unsigned char Version : 4;
- };
- };
- unsigned char TypeOfService;
- unsigned int DatagramLength;
- unsigned int Id;
- unsigned int FlagsAndFragmentOffset; // Flags 3 bit, Fragment Offset 13 bit
- unsigned char TimeToLive;
- unsigned char Protocol;
- unsigned int CheckSum;
- unsigned char SourceIp[4];
- unsigned char DestinationIp[4];
- } IP_HEADER, *PIP_HEADER;
- #define IP_HEADER_LENGTH 20
- #define PROTOCOL_INVALID_IP 0xFF
- #define PROTOCOL_INVALID_TCP 0xFE
- #define PROTOCOL_INVALID_UDP 0xFD
- #define PROTOCOL_INVALID_ICMP 0xFC
- #define PROTOCOL_TCP 0x06
- #define PROTOCOL_UDP 0x11
- #define PROTOCOL_ICMP 0x01
- #define PROTOCOL_IGMP 0x02
- #define HEADER_LENGTH_MULTIPLE 4
- #define GET_IP_VERSION(verlen) ((verlen & 0xF0) >> 4)
- #define GET_IP_HEADER_LENGTH(verlen) ((verlen & 0x0F) * HEADER_LENGTH_MULTIPLE)
- #define GET_IP_FLAGS(ffo) ((ffo & 0xE000) >> 13)
- #define GET_IP_FRAGMENT_OFFSET(ffo) (ffo & 0x1FFF)
- //
- // Tcp Header
- //
- typedef struct _TCP_HEADER
- {
- unsigned int SourcePort;
- unsigned int DestinationPort;
- unsigned long SeqNumber;
- unsigned long AckNumber;
- union
- {
- unsigned int LenAndCodeBits; // Header length 4 bit, Reserved 6 bit, Code Bits 6 bit
- struct
- {
- unsigned int Reserved1 : 8;
- unsigned int TcpCode : 6;
- unsigned int Reserved2 : 2;
- };
- struct
- {
- unsigned int Reserved3 : 4;
- unsigned int HeaderLength : 4;
- unsigned int TcpFin : 1;
- unsigned int TcpSyn : 1;
- unsigned int TcpRst : 1;
- unsigned int TcpPsh : 1;
- unsigned int TcpAck : 1;
- unsigned int TcpUrg : 1;
- unsigned int Reserved4 : 2;
- };
- };
- unsigned int Window;
- unsigned int CheckSum;
- unsigned int UrgentPointer;
- } TCP_HEADER, *PTCP_HEADER;
- #define TCP_HEADER_LENGTH 20
- #define GET_TCP_HEADER_LENGTH(lcb) (((lcb & 0xF000) >> 12) * HEADER_LENGTH_MULTIPLE)
- #define GET_TCP_CODE_BITS(lcb) (lcb & 0x003F)
- //
- // Udp Header
- //
- typedef struct _UDP_HEADER
- {
- unsigned int SourcePort;
- unsigned int DestinationPort;
- unsigned int Length; // including this header
- unsigned int CheckSum;
- } UDP_HEADER, *PUDP_HEADER;
- #define UDP_HEADER_LENGTH 8
- //
- // Icmp Header
- //
- typedef struct _ICMP_HEADER
- {
- unsigned char Type;
- unsigned char Code; // type sub code
- unsigned int CheckSum;
- unsigned int ID;
- unsigned int Seq;
- } ICMP_HEADER, *PICMP_HEADER;
- #define ICMP_HEADER_LENGTH 8
- #define ICMP_NORMAL 0
- #define ICMP_REQUEST 1
- #define ICMP_RESPONSE 2
- #define NETBIOS_MIN_PACKET_SIZE 48
- USHORT
- ntohs(
- IN USHORT netshort
- );
- ULONG
- ntohl(
- IN ULONG netlong
- );
- #endif