recvfrom not receiving ethernet packets!
recvfrom not receiving ethernet packets!
- Subject: recvfrom not receiving ethernet packets!
- From: Daniel Zuniga <email@hidden>
- Date: Mon, 31 Jan 2011 20:33:49 +1100
Hi all,
I have been trying to build a simple program to write an Ethernet packet and other to read an Ethernet packet. The one that writes seems to be working fine (I can see the packet in Wireshark) but the reading does not work; it just keeps waiting. Can anyone help me to find out the problem? Here is the code.
Thanks,
Daniel
Writing program (works fine):
main()
{
int fd;
int result, write;
struct ndrv_protocol_desc prot_desc;
struct ndrv_demux_desc demux_desc[1];
struct sockaddr_ndrv ndrv;
u_int8_t packet[1000];
//Open socket
fd = socket(PF_NDRV, SOCK_RAW, 0);
//error handing
//Bind to interface en1
strlcpy((char*)ndrv.snd_name, "en1", sizeof(ndrv.snd_name));
ndrv.snd_len = sizeof(ndrv);
ndrv.snd_family = AF_NDRV;
bind(fd, (struct sockaddr*)&ndrv, sizeof(ndrv));
//error handing
//Build packet
memset(&packet, 0, sizeof(packet));
//Destination MAC address - here goes my en1 MAC address
packet[0] = 0x00;
packet[1] = 0x21;
packet[2] = 0x41;
packet[3] = 0x21;
packet[4] = 0x11;
packet[5] = 0x31;
//Source MAC address - fictitious MAC address
packet[6] = 0x12;
packet[7] = 0x23;
packet[8] = 0x34;
packet[9] = 0x45;
packet[10] = 0x56;
packet[11] = 0x67;
//Set packet type - this is the type field in the frame; no specific reason for this number but I think must match the demux descriptor in the reading program.
packet[12] = 0x0f;
packet[13] = 0x0f;
//Some Data
strcpy((char*)&packet[14], "ABCDEFGHIJKLMNOPQRSTUVWXZ1234567890 This is a test \0");
//Send packet
write = sendto(fd, &packet, 1014, 0, (struct sockaddr*)&ndrv, sizeof(ndrv));
//error handling
close(fd);
return;
}
Reading program (not working, it keeps waiting for packet):
main()
{
int fd;
int read;
struct ndrv_protocol_desc prot_desc;
struct ndrv_demux_desc demux_desc[1];
struct sockaddr_ndrv ndrv;
u_int8_t buffer[1000];
//Open socket
fd = socket(PF_NDRV, SOCK_RAW, 0);
//error handing
//Bind to interface en1
ndrv.snd_len = sizeof(ndrv);
ndrv.snd_family = AF_NDRV;
strlcpy((char*)ndrv.snd_name, "en1", sizeof(ndrv.snd_name));
bind(fd, (struct sockaddr*)&ndrv, sizeof(ndrv));
//error handing
//Define protocol
prot_desc.version = (u_int32_t)1;
prot_desc.protocol_family = (u_int32_t)13458; //Just a number that do not match any other protocol defined in the interface
prot_desc.demux_count = (u_int32_t)1;
prot_desc.demux_list = (struct ndrv_demux_desc*)&demux_desc;
//Define DEMUX
demux_desc[0].type = NDRV_DEMUXTYPE_ETHERTYPE;
demux_desc[0].length = 2;
demux_desc[0].data.ether_type = htons(0x0f0f); //same as ethernet type in the writing program
//Set protocol using socket options
setsockopt(fd, SOL_NDRVPROTO, NDRV_SETDMXSPEC, (caddr_t)&prot_desc, sizeof(prot_desc));
//error handing
//Receive packet
read = recvfrom(fd, &buffer, sizeof(buffer), 0, NULL, NULL);
//error handing
close(fd);
return;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden