我想检测传入互联网数据包的ip选项。这是我的密码。
//#define IPPROTO_IP 0
//#define IP_OPTIONS 68
#define SENDER_PORT_NUM 53
#define SENDER_IP_ADDR "127.0.0.1"
#define true 1
static void bail(const char *error)
{
printf("%s: %sn", error, strerror(errno));
exit(1);
}
struct ip_datagram{
unsigned char ver_ihl;
unsigned char tos;
unsigned short totlen;
unsigned short id;
unsigned short flags_offs;
unsigned char ttl;
unsigned char proto;
unsigned short checksum;
unsigned long src;
unsigned long dst;
unsigned char payload[1500];
};
int main(){
int s,b,sso,gso, i;
int contatore = 5;
int enabled = 1;
char *data, *buffer;
int addrlen;
struct sockaddr_in addr;
struct ip_datagram *ip, *ip_reply;
struct icmphdr* icmp;
//char *src_addr="93.34.229.111";
char *dst_addr="127.0.0.1";
data = malloc(sizeof(struct ip_datagram)+ sizeof(struct icmphdr));
buffer = malloc(sizeof(struct ip_datagram)+ sizeof(struct icmphdr));
ip = (struct ip_datagram *) (data );
icmp = (struct icmphdr*) (data + sizeof(struct ip_datagram));
ip->ver_ihl = 45;
//ip->ihl = 15;
ip->totlen = sizeof(struct ip_datagram);
ip->proto = IPPROTO_ICMP;
//ip->src =(inet_addr(src_addr));
ip->dst = inet_addr(dst_addr);
ip->checksum = in_cksum((unsigned short *)ip, sizeof(struct ip_datagram));
icmp->type = ICMP_ECHO;
icmp->checksum = in_cksum((unsigned short *)icmp, sizeof(struct icmphdr));
s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if(s<0)
printf("errore socketn");
sso = setsockopt(s, IPPROTO_IP, IP_OPTIONS, &enabled, sizeof(enabled));
if(sso<0)
bail("errore setsockoptionsn");
gso = (s, IPPROTO_IP, IP_OPTIONS, &enabled, sizeof(enabled));
if(gso<0)
bail("errore getsockoptionsn");
else
printf("IP_OPTIONS VALUE :%dn", enabled);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = ip->dst;
while(contatore>0){
int sent_bytes = sendto(s, (const char *)data, ip->totlen, 0, (struct sockaddr*)&addr, sizeof(struct sockaddr) );
if(sent_bytes <0){
printf("failed to send packetsn");
return 3;
}
//printf("Sent %d byte packet from %s to %sn", ip->totlen, src_addr, dst_addr);
printf("Sent %d byte packet to %sn", ip->totlen, dst_addr);
addrlen = sizeof(addr);
int bytes = recvfrom(s, buffer, sizeof(struct ip_datagram) + sizeof(struct icmphdr), 0, (struct sockaddr*)&addr, &addrlen);
if(bytes<=0){
printf("errore recvfromn");
break;
}
else{
char *cp;
ip_reply = (struct ip_datagram*) buffer;
cp = (char *)&ip_reply->src;
printf("Received %d byte reply from %u.%u.%u.%u:n", ntohs(ip_reply->totlen), cp[0]&0xff,cp[1]&0xff,cp[2]&0xff,cp[3]&0xff);
printf("ID: %dn", ntohs(ip_reply->id));
printf("TTL: %dn", ip_reply->ttl);
printf("type of service: %.1Xn", ip_reply->tos);
printf("IP ADDRESS SOURCE: %.4Xn", htonl(ip_reply->src));
printf("IP ADDRESS DESTINATION: %.4Xn", htonl(ip_reply->dst));
printf("versione + header length: %.1Xn", ip_reply->ver_ihl);
contatore--;
printf("-----------------n");
printf("****************n");
}
}
}
问题是报头长度是6,但是对于检测所有ip选项,长度应该是20。在setsockopt中,启用变量的类型可能应该是char,但我在运行程序时遇到了一些问题:事实上,对于char类型,它会给我一个无效参数的错误。我还想知道如何使用getsockpoption打印这些选项。怎么了?我希望我的问题很清楚:)
您为IP标头定义的结构可能与IP数据包标头广泛兼容,但它还不够好,也不容易使用。
在我的Linux系统上,系统头在/usr/include/Linux/IP.h中定义IP头,如下所示:
struct iphdr {
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u8 ihl:4,
version:4;
#elif defined (__BIG_ENDIAN_BITFIELD)
__u8 version:4,
ihl:4;
#else
#error "Please fix <asm/byteorder.h>"
#endif
__u8 tos;
__be16 tot_len;
__be16 id;
__be16 frag_off;
__u8 ttl;
__u8 protocol;
__sum16 check;
__be32 saddr;
__be32 daddr;
/*The options start here. */
};
正如您所看到的,这会适当地中断IP版本和标头长度,并且为大或小的endian目标编译更安全。
使用这样的结构定义,您可以在sizeof(struct iphdr)
字节之后找到IP选项的开始,并且整个IP标头的大小将是ip.ihl * 4
,其中IP是struct iphdr
。
您需要乘以上面的4,因为ihl
是组成IP标头的32位字的数量,而不是标头本身的字节计数。
您可以查看IPv4(维基百科),其中包含有关IP标头选项及其格式的一些一般信息,以及IP选项的IANA列表,以获取完整列表,其中包含描述每个选项详细信息的各种RFC的链接。