在POSIXy C中如何接收第二层数据包?数据包只有src和dst MAC地址,类型/长度和自定义格式的数据.他们不是TCP或UDP或IP或IGMP或ARP或其他 – 他们是由五金家伙给我的家庭酿造的格式.
我的套接字(AF_PACKET,SOCK_RAW,IPPROTO_RAW)不会从其recvfrom()返回.
我可以发送正常,我无法接收,无论我在网络堆栈中的什么选项.
(平台是VxWorks,但我可以翻译POSIX或Linux或其他任何…)
接收代码(当前化身):
int s; if ((s = socket(AF_PACKET,IPPROTO_RAW)) < 0) { printf("socket create error."); return -1; } struct ifreq _ifr; strncpy(_ifr.ifr_name,"lltemac0",strlen("lltemac0")); ioctl(s,IP_SIOCGIFINDEX,&_ifr); struct sockaddr_ll _sockAttrib; memset(&_sockAttrib,sizeof(_sockAttrib)); _sockAttrib.sll_len = sizeof(_sockAttrib); _sockAttrib.sll_family = AF_PACKET; _sockAttrib.sll_protocol = IFT_ETHER; _sockAttrib.sll_ifindex = _ifr.ifr_ifindex; _sockAttrib.sll_hatype = 0xFFFF; _sockAttrib.sll_pkttype = PACKET_HOST; _sockAttrib.sll_halen = 6; _sockAttrib.sll_addr[0] = 0x00; _sockAttrib.sll_addr[1] = 0x02; _sockAttrib.sll_addr[2] = 0x03; _sockAttrib.sll_addr[3] = 0x12; _sockAttrib.sll_addr[4] = 0x34; _sockAttrib.sll_addr[5] = 0x56; int _sockAttribLen = sizeof(_sockAttrib); char packet[64]; memset(packet,sizeof(packet)); if (recvfrom(s,(char *)packet,sizeof(packet),(struct sockaddr *)&_sockAttrib,&_sockAttribLen) < 0) { printf("packet receive error."); } // code never reaches here