• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Newbie question about receiving raw ethernet packet
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Newbie question about receiving raw ethernet packet


  • Subject: Newbie question about receiving raw ethernet packet
  • From: Jean Bovet <email@hidden>
  • Date: Wed, 13 Apr 2005 17:04:38 -0700

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

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Newbie question about receiving raw ethernet packet
      • From: Rich Kubota <email@hidden>
  • Prev by Date: Re: ioctl always returns 0
  • Next by Date: Re: ioctl always returns 0
  • Previous by thread: Re: Appending to files with CFFTP
  • Next by thread: Re: Newbie question about receiving raw ethernet packet
  • Index(es):
    • Date
    • Thread