我正在尝试使用c ++从Linux中的IP数据包中检索IP Options的数据。此代码找出一个具有IP选项的数据包,但问题是我无法获取IP选项值。有没有办法获取IP Options的数据?
#include <linux/ip.h>
if(key->eth.type == htons(ETH_P_IP) && key->ip.tos != 0)
{
struct iphdr *nh = (struct iphdr *)skb_network_header(skb);
if(nh != NULL && nh->ihl * 4 > sizeof(struct iphdr))
{
// get IP options
}
}
答案 0 :(得分:0)
我找到了一种访问网络头的每个字节的方法。因此,在典型的标头之后有IP选项字节。如果使用时间戳记IP选项,则第一个字节为type
,第二个字节为length
,第三个字节为overflow
和flags
。因此,第四个字节是实际IP选项数据的开始。在我的解决方案中,我尝试将数据转换为unsigned long int
。
struct iphdr *nh = (struct iphdr *)skb_network_header(skb); // skb is socket buffer
unsigned int iphdr_size = sizeof(struct iphdr);
if(nh != NULL && nh->ihl * 4 > iphdr_size)
{
u8 *opt;
opt = (u_int8_t *)nh;
unsigned long int optdata = 0;
unsigned int i;
for(i = iphdr_size + 4; i < nh->ihl * 4; i++)
{
optdata = (optdata << 8) | opt[i];
}
// Now we have data in optdata, do what you want to do
}
有一些有用的链接,例如:
IP & TCP Option Functions
Converting 8 byte char array into long