我正在尝试发送数据包(不是 ip 数据包,而是 EAP 请求标识),但是 send() 函数返回 -1。
int sockfd = socket(AF_INET, SOCK_RAW, 0);
printf("socket: %d\n", sockfd);
struct sockaddr_in sock;
printf("creating packet\n");
char packet[27];
memcpy(packet + 0, (u_char* ) ethernet->ether_dhost, 6);
memcpy(packet + 6, (u_char* ) ethernet->ether_shost, 6);
memcpy(packet + 12, (u_char* ) ethernet + 12, 2);
memcpy(packet + 14, (u_char *)reqid, 1);
memcpy(packet + 15, (u_char *)reqid+1, 1);
memcpy(packet + 16, (u_char *)reqid+2, 4);
memcpy(packet + 20, (u_char *)reqid+6, 1);
memcpy(packet + 21, (u_char *)reqid+7, 1);
memcpy(packet + 22, (u_char *)reqid+8, 4);
memcpy(packet + 26, (u_char *)reqid+12, 1);
printf("sending packet\n");
if(send(sockfd, packet, sizeof(packet), 0) == -1)
{
printf("packet not sent\n");
//return;
}
数据包由以太网数据包和请求标识组成。
我知道数据包很好,每个值都在正确的位置。但是 send() 函数失败了。
errno show "Destination address required"并且 sockfd 值为 4。
顺便说一句,这是在 FreeBSD 上。
谢谢
编辑: 我没有目的地的 IP 地址。我只有它的MAC地址。
最佳答案
来自手册页:
The send() call may be used only when the socket is in a connected state (so that the intended recipient is known).
我相信你想要sendto
,而不是send
。
关于c++ - "Destination address required"尝试通过 SOCK_RAW 套接字发送数据时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36570953/