Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I receive raw, layer 2 packets in C/C++?
    text
    copied!<p>How do I receive layer 2 packets in POSIXy C++? The packets only have src and dst MAC address, type/length, and custom formatted data. They're not TCP or UDP or IP or IGMP or ARP or whatever - they're a home-brewed format given unto me by the Hardware guys.</p> <p>My <code>socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)</code> never returns from its <code>recvfrom()</code>.</p> <p>I can send fine, I just can't receive no matter what options I fling at the network stack.</p> <p>(Platform is VxWorks, but I can translate POSIX or Linux or whatever...)</p> <p>receive code (current incarnation):</p> <pre><code> int s; if ((s = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) &lt; 0) { printf("socket create error."); return -1; } struct ifreq _ifr; strncpy(_ifr.ifr_name, "lltemac0", strlen("lltemac0")); ioctl(s, IP_SIOCGIFINDEX, &amp;_ifr); struct sockaddr_ll _sockAttrib; memset(&amp;_sockAttrib, 0, 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, 0, sizeof(packet)); if (recvfrom(s, (char *)packet, sizeof(packet), 0, (struct sockaddr *)&amp;_sockAttrib, &amp;_sockAttribLen) &lt; 0) { printf("packet receive error."); } // code never reaches here </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload