inject raw IP packet using NDRV socket
inject raw IP packet using NDRV socket
- Subject: inject raw IP packet using NDRV socket
- From: Andy Huang <email@hidden>
- Date: Tue, 27 Apr 2010 16:31:35 -0500
Hi,
I am trying to inject raw IP packet using NDRV socket, the IP packet has multicast/UDP data. Here is my code:
- (int)initNdrvSocket:(char *)ifName
{
sock_raw = socket(AF_NDRV, SOCK_RAW, 0);
if (sock_raw < 0)
{
NSLog(@"ndrv_socket: socket() failed: %s\n", strerror(errno));
goto failed;
}
strlcpy((char *)ndrv.snd_name, ifName, sizeof(ndrv.snd_name));
ndrv.snd_len = sizeof(ndrv);
ndrv.snd_family = AF_NDRV;
if (bind(sock_raw, (struct sockaddr *)&ndrv, sizeof(ndrv)) < 0)
{
NSLog(@"ndrv_socket: bind() failed: %s\n", strerror(errno));
goto failed;
}
return (sock_raw);
failed:
if (sock_raw >= 0)
{
close(sock_raw);
sock_raw = -1;
}
return (-1);
}
- (int)bindNdrvSocket:(u_int32_t)family
EtherTypes:(const u_int16_t *)ether_types
EtherTypesCount:(int)ether_types_count
{
int i;
struct ndrv_protocol_desc proto;
struct ndrv_demux_desc * demux;
int status;
demux = (struct ndrv_demux_desc *)
malloc(sizeof(*demux) * ether_types_count);
proto.version = NDRV_PROTOCOL_DESC_VERS;
proto.protocol_family = family;
proto.demux_count = ether_types_count;
proto.demux_list = demux;
for (i = 0; i < ether_types_count; i++) {
demux[i].type = NDRV_DEMUXTYPE_ETHERTYPE;
demux[i].length = sizeof(demux[i].data.ether_type);
demux[i].data.ether_type = htons(ether_types[i]);
}
status = setsockopt(sock_raw, SOL_NDRVPROTO, NDRV_SETDMXSPEC,
(caddr_t)&proto, sizeof(proto));
free(demux);
if (status < 0) {
fprintf(stderr, "setsockopt(NDRV_SETDMXSPEC) failed: %s\n",
strerror(errno));
return (status);
}
return (0);
}
-(int)injectPacket:(u_char *)pkt Len:(int)len
{
int sock = [self initNdrvSocket:"en0"];
u_short ether_types[3] = {0x86a0, 0x86a1, 0x86a2};
int rc = [self bindNdrvSocket:NDRV_DEMUXTYPE_ETHERTYPE EtherTypes:ether_types EtherTypesCount:3];
if (rc < 0)
return -1;
// the buf already has ethernet header, so just copy the raw IP packet
bcopy(pkt, &(buf[ETHER_HDR_LEN]), ip_len);
int rc = sendto(sock_raw, buf, ETHER_HDR_LEN + ip_len, 0, (struct sockaddr *)&ndrv, sizeof(struct sockaddr));
.....
}
The injection was successful and I can verify it by using wireshark. see following:
0000 01 00 5e 1b 6a 01 48 55 41 4e 47 41 08 00 45 00 ..^.j.HU ANGA..E.
0010 00 4a 05 5a 00 00 1e 11 b0 56 20 61 74 75 e8 1b .J.Z.... .V atu..
0020 6a 01 04 24 30 39 00 36 f0 66 41 54 26 54 20 4d j..$09.6 .fAT&T M
0030 75 6c 74 69 63 61 73 74 0d 0a 54 75 65 20 41 70 ulticast ..Tue Ap
0040 72 20 32 37 20 31 36 3a 31 35 3a 32 35 20 43 44 r 27 16: 15:25 CD
0050 54 20 32 30 31 30 0d 0a T 2010..
But for some reason the multcast receiver just can't receive the packet. Any help will be greatly appreciated.
_______________________________________________
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