我目前正在尝试用C创建原始ICMPv6数据包。我只发现IPv4示例运行良好,我看不出我对IPv6的错误。
到目前为止我所知道的事情:
我查看了一个旧mailing-list post,发现我需要在in6_addr中设置一些变量(→错误22),但除此之外他们正在使用:
sock = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
在BCP38 project上,他们显然使用了LIBNET。如果我可以使用套接字,我更喜欢避免使用librairies。在这种情况下,如果不通过socket.h,如何调用“网络API”。
我读过here IP_HDRINCL 在IPv6中没有等效内容。 (但为什么?)
以下代码,发送ICMPv6数据包可能是由于IPPROTO_ICMPV6,但内核添加了标头和非常糟糕的数据包内容......(目的地址错误,我还没有解决一些字节顺序问题)。它在IPv6中有效。 当我使用IPPROTO_RAW时,数据包根本就不会被发送......
有什么想法吗?提前致谢
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
struct ipv6header {
unsigned char priority:4, version:4;
unsigned char flow[3];
unsigned short int length;
unsigned char nexthdr;
unsigned char hoplimit;
unsigned int saddr[4];
unsigned int daddr[4];
};
struct icmpv6header {
unsigned char type;
unsigned char code;
unsigned short int chk_sum;
unsigned int body;
};
int main()
{
char* packet = (char*) malloc(sizeof(struct ipv6header)+sizeof(struct icmpv6header));
struct ipv6header* ip = (struct ipv6header*) packet;
struct icmpv6header* icmp = (struct icmpv6header*) (packet+sizeof(struct ipv6header));
icmp->type = 128;
icmp->code = 0;
icmp->chk_sum = (0x6a13);
icmp->body = htonl(1234);
ip->version = 6;
ip->priority = 0;
(ip->flow)[0] = 0;
(ip->flow)[1] = 0;
(ip->flow)[2] = 0;
ip->length = ((unsigned short int) sizeof(struct icmpv6header));
ip->nexthdr = 58;
ip->hoplimit = 255;
struct sockaddr_in6 remote;
remote.sin6_family = AF_INET6;
remote.sin6_port = 0;
remote.sin6_flowinfo = 0;
remote.sin6_scope_id = 0;
inet_pton(AF_INET6, "2001:470:x:x:y:y:y:dd7b", &(remote.sin6_addr));
inet_pton(AF_INET6, "2001:470:x:x:bee:bee:bee:bee", &(ip->saddr));
inet_pton(AF_INET6, "2001:470:x:x:y:y:y:dd7b", &(ip->daddr));
int sock, optval;
sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
if(sock == -1)
{
printf("Error setting socket\n");
return -1;
}
int ret = setsockopt(sock, IPPROTO_IPV6, IP_HDRINCL, &optval, sizeof(int));
if(ret != 0) {
printf("Error setting options %d\n", ret);
return -1;
}
printf("Socket options done\n");
int ret = sendto(sock, packet, ip->length, 0, (struct sockaddr *) &remote, sizeof(remote));
if(ret != ip->length) {
printf("Packet not sent : %d (%d)\n",ret,errno);
return -1;
}
printf("Packet sent\n");
return 0;
}
答案 0 :(得分:1)
我遇到了类似的问题,并使用IPV6_HDRINCL
代替IP_HDRINCL
解决了这个问题。设置套接字选项时。
答案 1 :(得分:0)
好的,几句代码重写后我成功了:
主要问题是,当我使用IPPROTO_RAW
时,由于长度不好导致数据包被截断。结果,系统在它到达网络之前丢弃了它。
我也改变了
uint16_t daddr[8];
和
uint16_t ipdst[8] = {htons(0x2001),htons(0x470),htons(x),htons(x),htons(y),htons(y),htons(y),htons(0xdd7b)};
uint16_t ipsrc[8] = {htons(0x2001),htons(0x470),htons(x),htons(x),htons(0xbee),htons(0xbee),htons(0xbee),htons(0xbee)};
int j = 0;
for(j=0;j<=7;j++) {
remote.sin6_addr.s6_addr16[j]=ipdst[j];
ip->saddr[j]=ipsrc[j];
ip->daddr[j]=ipdst[j];
}
谢谢!