3

I am developing a program in which i need to know the values of IP header fields inside my program.Since the IP header is of 20 bytes:

struct ipheader {

     unsigned char ip_hl:4, ip_v:4; /* this means that each member is 4 bits */
     unsigned char ip_tos;
     unsigned short int ip_len;
     unsigned short int ip_id;
     unsigned short int ip_off;
     unsigned char ip_ttl;
     unsigned char ip_p;
     unsigned short int ip_sum;
     unsigned int ip_src;
     unsigned int ip_dst;
    }; 

Is there any way that i can know the values of these fields inside my C program?

3
  • Are you wondering how to parse the fields from an already available packet, or how to capture the packet including headers in the first place? Jul 14, 2011 at 6:02
  • 1
    I suggest to have a look at libpcap - it should make your life a lot easier. Jul 14, 2011 at 6:20
  • the query is that how can i get these system values,since while creating a simple datagram packet (UDP) i fill the SrcIP and port ,DEst IP adress and port and send the packet ,rest of the value are picked by default form the system stack.so i want to know the value of the rest fields eg: ttl,tos etc
    – sumant
    Jul 14, 2011 at 8:45

3 Answers 3

1

Provided your compiler doesn't introduce any alignment blocks within that structure (make sure that CHAR_BIT is 8 and sizeof(struct ipheader) is 20), you should just be able to include it in your code as-is, and then add something like:

struct ipheader *iph = (struct ipheader *)blk;
printf ("TTL = %d\n", iph->ip_ttl);

In that code, you will have an IP header pointed to by blk, which is probably a char*. Casting it to the correct pointer type will allow you to easily access the fields.

The following complete program shows this in action:

#include <stdio.h>
#include <limits.h>

struct ipheader {
    /* 0 */ unsigned char ip_hl:4, ip_v:4;
    /* 1 */ unsigned char ip_tos;
    /* 2 */ unsigned short int ip_len;
    /* 3 */ unsigned short int ip_id;
    /* 4 */ unsigned short int ip_off;
    /* 5 */ unsigned char ip_ttl;
    /* 6 */ unsigned char ip_p;
    /* 7 */ unsigned short int ip_sum;
    /* 8 */ unsigned int ip_src;
    /* 9 */ unsigned int ip_dst;
};

int main (void) {
    char blk[] = {
        '\x00','\x11','\x22','\x22','\x33','\x33','\x44','\x44',
        '\x55','\x66','\x77','\x77','\x88','\x88','\x88','\x88',
        '\x99','\x99','\x99','\x99'
    };
    struct ipheader *iph = (struct ipheader *)(&blk);

    printf ("TTL = %x\n", iph->ip_ttl);
    printf ("sum = %x\n", iph->ip_sum);
    printf ("dst = %x\n", iph->ip_dst);

    return 0;
}

Output is, as expected:

TTL = 55
sum = 7777
dst = 99999999
1
  • basically the structure i provided was for reference, the real query is that how can i get these system values,since while creating a simple datagram packet (UDP) i fill the SrcIP and port ,DEst IP adress and port and send the packet ,rest of the value are picked by default form the system stack.the rest values include ip_ttl,ip_tos etc
    – sumant
    Jul 14, 2011 at 6:25
1

Some of those values can be set/retrieved via setsockopt()/getsockopt() calls at the SOL_IP/IPPROTO/IP level. Consult your OS' documentation (e.g: on Linux man 7 ip).

1

You should use setsockopt/getsockopt routines to interface with socket mechanism. These functions are operate on different levels (IPPROTO_IP, IPPROTO_TCP, etc) and some options are available only for particular socket types (e.g. option IP_TTL is available only for AF_INET sockets).

get TTL value:

int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
unsigned int opt_val;                                                         
unsigned int opt_len;                                                       
getsockopt(sock, IPPROTO_IP, IP_TTL, &opt_val, &opt_len);
printf("ttl %d\n", opt_val);

set TTL value:

int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
unsigned char ttl_val = 32;                                                                                                                                        
setsockopt(sock, IPPROTO_IP, IP_TTL, &ttl_val, sizeof(ttl_val));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.