This is a low-level network debugging utility that utilizes raw packet i/o to construct and deconstruct tcp, udp, ipv4, arp, and icmp packets over ethernet.
Diff: sniffer.h
- Revision:
- 5:c56386b9fc33
- Parent:
- 4:88fc7fa58931
- Child:
- 6:66c4cd9073aa
--- a/sniffer.h Tue Oct 12 06:21:05 2010 +0000 +++ b/sniffer.h Tue Oct 12 06:37:21 2010 +0000 @@ -64,29 +64,29 @@ Ethernet_FrameHeader *outframe_header; public: - // Ethernet Frame Header + /// Ethernet Frame Header (incoming) Ethernet_FrameHeader *frame_header; - // IP Packet Header + /// IP Packet Header (incoming) IP_PacketHeader *ip_packet; - // ARP Packet + /// ARP Packet (incoming) ARP_Packet *arp_packet; - // TCP Packet + /// TCP Packet (incoming) TCP_SegmentHeader *tcp_packet; - // UDP Packet + /// UDP Packet (incoming) UDP_Packet *udp_packet; - // ICMP Packet + /// ICMP Packet (incoming) ICMP_Packet *icmp_packet; - // Generic + /// Generic - total data bytes unsigned int data_bytes; public: - // Constructor + /// Constructor inline Sniffer() : linked(LED1), received(LED2) { @@ -94,6 +94,7 @@ eth.address((char *)mac.octet); } + /// Inject the raw ethernet frame inline bool inject(void *data, unsigned int bytes) { // Send the packet @@ -105,6 +106,7 @@ return send_status; } + /// Inject the raw payload into an ethernet frame with the given destination and ethertype inline bool inject(Ethernet_MAC dest, u16 ethertype, void *packet, unsigned int bytes) { memset(outframe, 0x00, bytes); @@ -140,6 +142,7 @@ return send_status; } + /// Wait until there is more data to receive inline void wait_for_data() { while (true) @@ -158,7 +161,7 @@ } } - // Wait for an ethernet frame + /// Wait for an ethernet frame (will be stored in appropriate class member pointers) inline void next() { wait_for_data(); @@ -174,6 +177,7 @@ decode_ethernet(frame); } + /// Decode the given ethernet frame inline void decode_ethernet(void *frame) { Ethernet_FrameHeader *header = frame_header = (Ethernet_FrameHeader*)frame; @@ -193,6 +197,7 @@ } } + /// Decode the given ARP packet inline void decode_arp(ARP_Packet *packet) { fix_endian_arp(packet); @@ -200,6 +205,7 @@ arp_packet = packet; } + /// Decode the given IPv4 packet inline void decode_ip(IP_PacketHeader *packet) { u16 chk = checksum(packet, sizeof(IP_PacketHeader), &packet->header_checksum, 2); @@ -238,12 +244,14 @@ if (tcp_handler) (*tcp_handler)(tcp_packet,data_bytes); } + /// Attach a member function to be called on all TCP packets template <class T> inline void attach_tcp(T *inst, void (T::*func)(TCP_SegmentHeader *tcp_packet, u32 data_bytes)) { tcp_handler = new member_handler<T,TCP_SegmentHeader*,u32,void>(inst, func); } + /// Attach a non-member function to be called on all TCP packets inline void attach_tcp(void (*func)(TCP_SegmentHeader *tcp_packet, u32 data_bytes)) { tcp_handler = new function_handler<TCP_SegmentHeader*,u32,void>(func);