Home > LIBIPQ, Linux, Netfiliter > Determining the IP protocol of a packet from LIBIPQ

Determining the IP protocol of a packet from LIBIPQ

February 3rd, 2005 SuperHac

When you recieved the ipq_packet_msg structure from LIBIPQ you need to determine what IP protocol (TCP, UDP, etc) is contained after the IP header. Below is a simple fuction to determine what IP protocol is contained within the packet.

int identify_ip_protocol (ipq_packet_msg_t *msg)
{
int protocol=0; /* 6 = TCP, 16 = UDP */

/* Cast the IP Header from the raw packet */
struct iphdr *iph = ((struct iphdr *) msg->payload);

/* get the protocol identifier from the ip header */
protocol = iph->protocol;

return(protocol);

}

Once you determine the protocol you can then cast out the next header. Like this for TCP:

/* Cast the TCP Header from the raw packet */
struct tcphdr *tcp = (struct tcphdr *)(m->payload + (iph->ihl << 2));

or like this for UDP:

/* Cast the UDP Header from the raw packet */
struct udphdr *udp = (struct udphdr *) (msg->payload + (iph->ihl << 2));

Categories: LIBIPQ, Linux, Netfiliter Tags:
Comments are closed.