cleanpcap
/
protocol_header.h
/
1 contributor
#ifndef _PROTOCOL_HEADER_H
#define _PROTOCOL_HEADER_H
#include <netinet/in.h>
#include <arpa/inet.h>
#ifndef ETHER_ADDR_LEN
#define ETHER_ADDR_LEN 6
#endif //ETHER_ADDR_LEN
#ifndef IP4_ADDR_LEN
#define IP4_ADDR_LEN 4
#endif //IP4_ADDR_LEN
#ifndef ETHER_HEADER_SIZE
#define ETHER_HEADER_SIZE 14
#endif //ETHER_HEADER_SIZE
#ifndef IP4_HEADER_SIZE
#define IP4_HEADER_SIZE 20
#endif //IP4_HEADER_SIZE
#ifndef UDP_HEADER_SIZE
#define UDP_HEADER_SIZE 8
#endif //UDP_HEADER_SIZE
#ifndef STR_IP4_SIZE
#define STR_IP4_SIZE 15
#endif //STR_IP4_SIZE
/* Ethernet header */
struct sniff_ethernet {
uint8_t ether_dhost[ETHER_ADDR_LEN]; /* Destination host address */
uint8_t ether_shost[ETHER_ADDR_LEN]; /* Source host address */
uint16_t ether_type; /* IP? ARP? RARP? etc */
};
/* IP header */
typedef struct _sniff_ip_t {
uint8_t ip_vhl; /* version << 4 | header length >> 2 */
uint8_t ip_tos; /* type of service */
uint16_t ip_len; /* total length */
uint16_t ip_id; /* identification */
uint16_t ip_off; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
uint8_t ip_ttl; /* time to live */
uint8_t ip_p; /* protocol */
uint16_t ip_sum; /* checksum */
struct in_addr ip_src; /* source and dest address */
struct in_addr ip_dst; /* source and dest address */
} sniff_ip_t;
#define IP_HL(ip) (((ip)->ip_vhl) & 0x0f)
#define IP_V(ip) (((ip)->ip_vhl) >> 4)
/* IPv6 header */
typedef struct _sniff_ip6_t {
uint8_t ip_vtc; /* version << 4 | traffic_class msb >> 4*/
uint8_t ip_tcfl; /* traffic_class lsb << 4 | flow_label msb >> 4*/
uint16_t ip_flow_label; /* traffic_class lsb << 4 | flow_label msb >> 4*/
uint16_t payload_len;
uint8_t next_header;
uint8_t hop_limit;
uint32_t src_addr[4];
uint32_t dst_addr[4];
} sniff_ip6_t;
/* TCP header */
typedef struct _sniff_tcp_t {
uint16_t th_sport; /* source port */
uint16_t th_dport; /* destination port */
uint32_t th_seq; /* sequence number */
uint32_t th_ack; /* acknowledgement number */
uint8_t th_offx2; /* data offset, rsvd */
#define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
uint8_t th_flags;
#define TH_FIN 0x01
#define TH_SYN 0x02
#define TH_RST 0x04
#define TH_PUSH 0x08
#define TH_ACK 0x10
#define TH_URG 0x20
#define TH_ECE 0x40
#define TH_CWR 0x80
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
uint16_t th_win; /* window */
uint16_t th_sum; /* checksum */
uint16_t th_urp; /* urgent pointer */
} sniff_tcp_t;
/* UDP header */
typedef struct _sniff_udp_t {
uint16_t uh_sport; /* source port */
uint16_t uh_dport; /* destination port */
uint16_t uh_len; /* length */
uint16_t uh_sum; /* checksum */
} sniff_udp_t;
void printmac(const uint8_t *eth)
{
uint8_t i = 0;
for( i = 0; i < ETHER_ADDR_LEN; i++ )
{
printf( "%02x", eth[i] );
if( i != (ETHER_ADDR_LEN-1) )
{
printf(":");
}
}
}
#endif //_PROTOCOL_HEADER_H