C program to get the values of IP header fields fr

2020-06-24 01:25发布

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?

标签: c
3条回答
We Are One
2楼-- · 2020-06-24 01:42

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));
查看更多
Animai°情兽
3楼-- · 2020-06-24 01:49

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).

查看更多
萌系小妹纸
4楼-- · 2020-06-24 01:52

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
查看更多
登录 后发表回答