gpt4 book ai didi

c - RAW Socket 发送不起作用

转载 作者:行者123 更新时间:2023-11-30 15:56:33 25 4
gpt4 key购买 nike

您好,我正在尝试借助简单的 RAW 套接字功能向开发板发送一些命令。这是我正在使用的界面设置。在命令行中,我将接口(interface)作为 eth0 传递。我的代码如下:

int CreateRawSocket(int proto)
{
int i;
struct timeval timeout;
printf("\n creating a raw socket for protocol %d",proto);
if((rawsock=socket(PF_PACKET,SOCK_RAW,htons(proto)))==-1)
{
perror("\n cannot create socket");
return -1;
}
printf("\n setting the timeout for the raw socket equal to one microsecond");
timeout.tv_sec = 0;
timeout.tv_usec =1;
setsockopt(rawsock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
setsockopt(rawsock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
i = 1;
setsockopt(rawsock, SOL_SOCKET, SO_DONTROUTE, &i, sizeof(i));
return rawsock;
}

int BindRawSocketToInterface(char *device,int proto)
{
struct sockaddr_ll sll;
struct ifreq ifr;
printf("\n initiating socket address and interface address to null");
bzero(&sll,sizeof(sll));
bzero(&ifr,sizeof(ifr));
printf("\n setting up the interface to bind");
strcpy((char *)ifr.ifr_name, device);
if((ioctl(rawsock,SIOGIFINDEX, &ifr))==-1)
{
printf("\n something went wrong while resetting the interface with name");
return -1;
}
//setting up the flags and index in one step-ifr.ifr_flags = 0;
strcpy((char *)ifr.ifr_name, device);
ifr.ifr_flags = ifr.ifr_flags || IFF_PROMISC || IFF_BROADCAST;
printf("\n setting up the socket for this interface and protocol");
sll.sll_family = AF_PACKET;
sll.sll_ifindex =ifr.ifr_ifindex;
sll.sll_protocol=htons(proto);
printf("running ioctl");
if((ioctl(rawsock,SIOGIFINDEX, &ifr))==-1)
{
printf("\n something went wrong while setting flags");
return -1;
}
printf("running bind");
int r;
if((r = bind(rawsock, (struct sockaddr *)&sll, sizeof(sll)))==-1)
{
printf("\n socket is not binding properly");
return -1;
}
return r;

/*you can use this command to make it non blocking
r = fcntl(rawsock, F_SETFL, fl | O_NDELAY);*/
}

一切正常,发送返回 30,这是正确的,但我在wireshark中没有看到数据包。我确信wireshark设置没问题。有什么建议吗?

最佳答案

我看到另一个也有类似的问题: Raw socket NOT sending

“数据包确实正在发送,但 Wireshark 只能在 6 分钟左右的间隙后显示它们”。

您可以尝试简单地通过抛出 HTTP 请求来检查 Wireshark 的延迟。

关于c - RAW Socket 发送不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11186035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com