如何在lo
接口上接收所有IPv6数据包(TCP,UDP,ICMP,...)I。我使用命令ping6 ::1
发送ICMP数据包,但没有收到它们。接收所有IPv6数据包
谢谢
#include <linux/if_ether.h>
#include <error.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main() {
int socket_fd;
char buffer[1480];
struct sockaddr_in6 sin6;
struct sockaddr sin;
socket_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
setsockopt(socket_fd , SOL_SOCKET , SO_BINDTODEVICE , "lo" , strlen("lo")+ 1);
if (socket_fd < 0) {
perror("Failed to create socket");
}
ssize_t data_size;
// Why am I unable to receve any data?
data_size = recvfrom(socket_fd, buffer, 1480, 0, &sin, (socklen_t *) &sin);
return 0;
}
它在界面'lo'上绑定; 'setsockopt(socket_fd,SOL_SOCKET,SO_BINDTODEVICE,“lo”,strlen(“lo”)+ 1);' – camabeh