Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Ethernet code



On 3/13/02 6:01 PM, "Justin C. Walker" <email@hidden> wrote:

> On Wednesday, March 13, 2002, at 05:47 PM, Josh Graessley wrote:
>
>> If you need some sample code, let me know, I think I can dig some up
>> somewhere.
>
> I'd like some, please...

Sample code for setting up a PF_NDRV socket:
void ndrv_sample(void)
{
struct sockaddr_ndrv ndrv;
struct ndrv_protocol_desc proto;
struct ndrv_demux_desc demux;
int s;
int status;

// Create the PF_NDRV socket
s = socket(PF_NDRV, SOCK_RAW, 0);
if (s < 0)
{
// Handle failure
}


// Bind the socket to an ethernet interface, "en0" in this
// sample code.
strncpy(ndrv.snd_name, "en0", sizeof(ndrv.snd_name));
ndrv.snd_len = sizeof(ndrv);
ndrv.snd_family = AF_NDRV;

if (bind(s, (struct sockaddr*)&ndrv, sizeof(ndrv)) < 0)
{
// Handle failure
}

// Register the protocol
bzero(&proto, sizeof(proto));
bzero(&demux, sizeof(demux));
proto.version = NDRV_PROTOCOL_DESC_VERS;
proto.protocol_family = 0xBE; // Not sure what should go here
proto.demux_count = 1;
proto.demux_list = &demux;
demux.type = NDRV_DEMUXTYPE_ETHERTYPE;
demux.length = sizeof(demux.data.ether_type);
demux.data.ether_type = htons(0x1234); // Use the ethertype you're
// interested in here, see
// ndrv.h for more info
status = setsockopt(s, SOL_NDRVPROTO, NDRV_SETDMXSPEC,
(caddr_t)&proto, sizeof(proto));
if (status < 0)
{
// Handle error here
// You should get an error, probably ENOTSUPPORTED
// on systems prior to 10.1.2
}
}

You may need to get a newer version of ndrv.h from darwin. The headers that
were changed in 10.1.2 are not shipped with the software updates but are
required to build this sample code. You may need to replace ndrv.h in:

/usr/include/net/
/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/net/
/System/Library/Frameworks/System.framework/Versions/B/PrivateHeaders/net/

I pulled together the sample code above from another project, I think it
should work. I don't have the code for reading packets and I can't recall if
you call recvfrom and sendto or what to send and receive packets. I worked
with someone else in Apple to get the in kernel implementation working, but
never wrote the user land code.

When you register your protocol, you can use multiple demux descriptors. For
example, with IP, you'd want to register a demux descriptor for IP and
another one for ARP. Registering a protocol sends the demux descriptors to
the ethernet family, as packets are received they are demuxed and should end
up in your socket. There are some limitations, if you register a demux
descriptor that matches something someone else has already registered, the
results are "undefined". The protocol_family is used to try and detect this.
The protocol family should be something similar to PF_NDRV but specific to
your protocol and should not overlap with any other protocols.
Unfortunately, there is no registry of protocols, so I'm not sure what
you're supposed to use there.

Enjoy,

-josh
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Ethernet code (From: "Justin C. Walker" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.