I'm a newbie to OS X, i have search my problem in google, i find someone has the same problem with me, he has posted a mail to this maillist,i just forward his/her mail,
Hi,
I'm really new to network programming and I've been reading e-mails
from this list as well as the BSDLLCTest example provided by Apple.
I'm trying to extract from this example the lines needed to capture
raw ethernet packet (I have written a C++ tool that I launch using
sudo). Everything works well - no error reported - but I don't
receive anything (I'm using another computer to send raw packet to
my main machine).
Here is my code:
******
int create_socket(void)
{
fd = socket(AF_NDRV, SOCK_RAW, 0);
if(fd < 0) {
printf("socket() error = %d (%s)\n", errno, get_error(errno));
return errno;
}
return NO_ERR;
}
void create_socket_address(char *name)
{
saddr.sa_len = sizeof(struct sockaddr);
saddr.sa_family = AF_NDRV;
strcpy(saddr.sa_data, name);
}
int bind_socket(void)
{
int err = bind(fd, &saddr, sizeof(saddr));
if(err != 0) {
printf("bind error = %d (%s)\n", err, get_error(err));
return err;
}
return NO_ERR;
}
void set_listening_protocol(void)
{
struct ndrv_protocol_desc desc;
struct ndrv_demux_desc demux_desc[1];
bzero(&desc, sizeof(desc)); // zero out
the strucuture
bzero(&demux_desc, sizeof(demux_desc)); // zero out
the strucuture
desc.version = NDRV_PROTOCOL_DESC_VERS;
desc.protocol_family = NDRV_DEMUXTYPE_ETHERTYPE;
desc.demux_count = (u_int32_t)1;
desc.demux_list = (struct ndrv_demux_desc*)&demux_desc;
demux_desc[0].type = NDRV_DEMUXTYPE_ETHERTYPE;
demux_desc[0].length = sizeof(unsigned short);
demux_desc[0].data.ether_type = htons(0x888e);
int result = setsockopt(fd, SOL_NDRVPROTO, NDRV_SETDMXSPEC,
(caddr_t)&desc, sizeof(desc));
if (result != 0)
printf("setsockopt error = %d\n", result);
}
int main (int argc, const char * argv[])
{
// Create the socket (needs super-user privileges - use 'sudo')
int err = create_socket();
if(err != 0)
return err;
// Ethernet 0 (built-in)
create_socket_address("en1");
// Bind socket
err = bind_socket();
if(err != 0)
return err;
set_listening_protocol();
char buffer[2048];
int addr_len = sizeof(saddr);
int n = recvfrom(fd, buffer, 2048, 0, &saddr, &addr_len);
// int n = recvfrom(fd, buffer, 2048, 0, NULL, NULL);
printf("%d bytes read\n", n);
return 0;
}
****
I probably put incorrect values in my set_listening_protocol()...
any tips ? Also, what happens if I set the two last parameters from
the recvfrom() to NULL ?
Thanks for any help!
Jean Bovet
This email sent to email@hidden