• 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
Re: Newbie question about receiving raw ethernet packet
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie question about receiving raw ethernet packet


  • Subject: Re: Newbie question about receiving raw ethernet packet
  • From: Rich Kubota <email@hidden>
  • Date: Thu, 14 Apr 2005 14:21:10 -0700

Hello Jean,

the BSD sample doesn't really show how to capture Phase 1 Ethernet packets. I tried a quick modification and found the same problem which you describe. I recall that at one times, things worked. Maybe an alternative would be to implement a BPF socket. I'll send a sample to you directly.

rich kubota

At 5:04 PM -0700 4/13/05, Jean Bovet wrote:
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

-- Sincerely, Rich Kubota email@hidden (408) 974-6212 _______________________________________________ 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
References: 
 >Newbie question about receiving raw ethernet packet (From: Jean Bovet <email@hidden>)

  • Prev by Date: RE: m_dup
  • Next by Date: Re: m_dup
  • Previous by thread: Newbie question about receiving raw ethernet packet
  • Next by thread: m_dup
  • Index(es):
    • Date
    • Thread