fd = socket(AF_NDRV, SOCK_RAW, 0); // Create RAW socket
if (fd == -1)
return errh->error("%s: socket: %s", ifname.c_str(), strerror(errno));
sockaddr sa;
memset(&sa, 0, sizeof(sa));
sa.sa_family=AF_NDRV;
strcpy(sa.sa_data,ifname.c_str());
int res = bind(fd, (struct sockaddr *)&sa, sizeof(sa)); // Bind socket to interface
if (res != 0) {
close(fd);
return errh->error("%s: bind: %s", ifname.c_str(), strerror(errno));
}
struct ndrv_protocol_desc SelectedPacketList; // Prepare list of Ethertypes we want to receive
struct ndrv_demux_desc *pDesc;
SelectedPacketList.version=NDRV_PROTOCOL_DESC_VERS;
SelectedPacketList.protocol_family=NDRV_DEMUXTYPE_ETHERTYPE;
SelectedPacketList.demux_count=3;
pDesc=(struct ndrv_demux_desc *)malloc(3*sizeof(struct ndrv_demux_desc));
if(pDesc == NULL)
return errh->error("%s: Could not allocate memory for protocol: %s", ifname.c_str(), strerror(errno));
SelectedPacketList.demux_list=pDesc;
memset(pDesc, 0, 3*sizeof(struct ndrv_demux_desc));
pDesc[0].type=NDRV_DEMUXTYPE_ETHERTYPE;
pDesc[0].length=sizeof(pDesc[0].data.ether_type);
pDesc[0].data.ether_type=htons(0x86a0);
pDesc[1].type=NDRV_DEMUXTYPE_ETHERTYPE;
pDesc[1].length=sizeof(pDesc[1].data.ether_type);
pDesc[1].data.ether_type=htons(0x86a1);
pDesc[2].type=NDRV_DEMUXTYPE_ETHERTYPE;
pDesc[2].length=sizeof(pDesc[2].data.ether_type);
pDesc[2].data.ether_type=htons(0x86a2);
// Register wanted Ethertypes on the socket
if(setsockopt(fd,SOL_NDRVPROTO,NDRV_SETDMXSPEC,&SelectedPacketList, sizeof(struct ndrv_protocol_desc)) < 0)
return errh->error("%s: cannot specify protocol: %s",ifname.c_str(), strerror(errno));
...