我正在IPv6中使用RSVP协议,为了使其正常工作,我必须在发送的消息中设置IPv6路由器警报。我在互联网上做了很多研究,但运气不好,也许这里的人以前已经做过了。你可以在下面找到我用于测试的代码。
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define DATA_LENGTH 124
int main() {
int data_length = DATA_LENGTH;
unsigned char data[DATA_LENGTH] = {0x10, 0x01, 0xb0, 0x16, 0xff, 0x00, 0x00, 0x7c, 0x00, 0x18,
0x01, 0x02, 0x20, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x10, 0x32, 0x00, 0x20, 0xc2, 0x00, 0x18, 0x03, 0x02, 0x20, 0x02, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18,
0x0b, 0x02, 0x20, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x20, 0x00, 0x00, 0x5d, 0x27, 0x00, 0x08, 0x05, 0x01, 0x00, 0x00, 0x75, 0x30, 0x00, 0x24,
0x0c, 0x02, 0x00, 0x00, 0x00, 0x07, 0x01, 0x00, 0x00, 0x06, 0x7f, 0x00, 0x00, 0x05, 0x49, 0x55,
0x9f, 0x80, 0x4a, 0xba, 0xeb, 0x90, 0x4c, 0xee, 0xa8, 0x31, 0x00, 0x00, 0x00, 0x4b, 0x00, 0x00,
0x05, 0xdc};
unsigned char dest_address[16] = {0x20, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x05};
int socket_descriptor;
struct sockaddr_in6 dest;
const int yes = 1;
const int router_alert_value = 0;
socket_descriptor = socket(AF_INET6, SOCK_RAW, IPPROTO_RSVP);
if (socket_descriptor < 0) {
perror("Can't open socket to send path messagen");
}
memset(&dest, 0, sizeof(dest));
dest.sin6_family = AF_INET6;
memcpy(dest.sin6_addr.s6_addr, dest_address, sizeof(struct in6_addr));
dest.sin6_port = 0;
dest.sin6_flowinfo = 0;
if (setsockopt(socket_descriptor, SOL_SOCKET,
SO_REUSEADDR,(const void *)&yes, sizeof(yes)) < 0) {
printf("setsockopt(%d): %sn", SO_REUSEADDR, strerror(errno));
}
if(setsockopt(socket_descriptor, IPPROTO_IPV6,
IPV6_HOPOPTS, &yes, sizeof(yes)) < 0) {
printf("Can't set Hop-by-hop Option: %sn", strerror(errno));
}
if(setsockopt(socket_descriptor, IPPROTO_IPV6, IPV6_ROUTER_ALERT,
(const void *) &router_alert_value, sizeof(router_alert_value)) < 0) {
printf("Can't set IP Options: %sn", strerror(errno));
}
if(sendto(socket_descriptor, data, data_length, 0,
(struct sockaddr*) &dest, sizeof(dest)) < 0) {
printf("Error in sending message : %s (%d)n", strerror(errno), errno);
}
close(socket_descriptor);
return(0);
}
前面的代码编译并发送RSVP路径消息,但没有IPv6路由器警报选项。由于setsockopt方法出错时没有返回,我可以为您提供控制台输出,即:
Can't set Hop-by-hop Option: Invalid argument
Can't set IP Options: Protocol not available
我已经尝试过使用协议为0的套接字(以便使用逐跳的下一个标头),并将路由器警报选项直接添加到数据中,但它仍然不起作用。
任何想法或线索都将不胜感激。
在带有WinSock的Windows上,您可以使用IPV6_HDRINCL
构建IP标头并设置路由器警报标志。在Linux和类似的平台上,这根本不可用,您只需使用类似于Wireshark和tcpdump
的AF_PACKET
来处理原始数据包,并根据您的需求手工构建手工数据包。
当然,另一种选择是在内核中提出并实现一个新的接口来设置IPv6路由器警报。