我正在上一堂关于计算机和网络安全的课程。我们正在编写一个数据包欺骗器。我可以从互联网上下载并使用它,但我更喜欢自己写这些东西。下面是我用来表示ip标头的结构,我是basing off of the wikipedia article。我正在尝试发送icmp ping数据包。我已经成功完成了,但只是在将ip标头长度的值分配给版本字段后,反之亦然。不知何故,我设置我的结构错误,或者我分配错误的值,我不知道我做错了什么。
struct ip_header
{
uint8_t version : 4 // version
, ihl : 4; // ip header length
uint8_t dscp : 6 // differentiated services code point
, ecn : 2; // explicit congestion notification
uint16_t total_length; // entire packet size in bytes
uint16_t identification; // a unique identifier
uint16_t flags : 3 // control and identify fragments
, frag_offset : 13; // offset of fragment relative to the original
uint8_t ttl; // how many hops the packet is allowd to travel
uint8_t protocol; // what protocol is in use
uint16_t checksum; // value used to determine bad packets
uint32_t src_ip; // where the packet is form
uint32_t dest_ip; // where the packet is going
};
如果我分配version
和ihl
,如下所示,wireshark报告标题错误,“Bogus IPV4版本(0,必须为4)”。
char buffer[1024];
struct ip_header* ip = (struct ip_header*) buffer;
ip->version = 4;
ip->ihl = 5;
但是,在更改到以下列表后,ICMP请求通过就好了。
char buffer[1024];
struct ip_header* ip = (struct ip_header*) buffer;
ip->version = 5;
ip->ihl = 4;
我试过在数字周围放置htons
,但这似乎没有做任何有用的事情。我在这里错过了什么?
您只需要更正结构的字节序。查看<netinet/ip.h>
文件中定义的IP头结构:
struct iphdr
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int ihl:4;
unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int version:4;
unsigned int ihl:4;
#else
# error "Please fix <bits/endian.h>"
#endif
uint8_t tos;
uint16_t tot_len;
uint16_t id;
uint16_t frag_off;
uint8_t ttl;
uint8_t protocol;
uint16_t check;
uint32_t saddr;
uint32_t daddr;
/*The options start here. */
};