On Sat, Sep 13, 2008 at 9:46 PM, Tom=E1s =D3 h=C9ilidhe wrote: > > Anyone know what's the most portable C networking library to use for > sending custom frames? By "custom frames", I mean having the ability to > write the entire frame all by yourself (I think some people use the term > "raw sockets" to refer to this). For instance, here's a DNS request: > > unsigned char frame[] =3D > { > /* Offset 0 : Beginning of Ethernet frame */ > > 0x00,0x03,0x6f,0x23,0x25,0x4a, /* Destination MAC */ > 0x00,0x40,0x12,0x1b,0x77,0x36, /* Source MAC */ > 0x08,0x00, /* Protocol =3D=3D IP */ > > /* Offset 14 : Beginning of IP packet */ > > 0x45, /* IP Version and header length */ > 0x00, /* TOS */ > 0x00,0x3c, /* Total Length */ > 0x00,0x00, /* Identification */ > 0x40,0x00, /* Flags and Fragment Offset */ > 0x40, /* Time To Live */ > 0x11, /* Transport Layer Protocol =3D=3D UDP */ > > 0,0, /* IP Header Checksum */ > > 192,168,0,55, /* Source IP */ > 208,67,222,222, /* Destination IP */ > > /* Offset 34 : Beginning of UDP segment */ > > 0x80,0x01, /* Source port */ > 0x00,0x35, /* Destination port */ > 0x00,0x28, /* Length of UDP segment (header + data) */ > 0x00,0x00, /* Checksum */ > > /* Offset 42 : Beginning of DNS datagram */ > > 0x22,0xf4, /* Transaction ID */ > 0x01,0x00, /* Flags */ > 0x00,0x01, /* Questions */ > 0x00,0x00, /* Answer RR's */ > 0x00,0x00, /* Athority RR's */ > 0x00,0x00, /* Additional RR's */ > > 3,'w','w','w',6,'g','o','o','g','l','e',3,'c','o','m',0, > > 0x00,0x01, /* Type A (Host Address) */ > 0x00,0x01 /* Class IN */ > }; > > At the moment I'm using Berkeley Sockets for sending custom frames, but > I'm not sure if it's the best library to be using. I'd like if my > program worked on stuff like Windows, Linux, Mac, Solaris, but it'd be a > bonus if it worked on stuff like XBox and Playstation 3 aswell! Here's > how I use Berkeley Sockets to send custom frames: > > First of all, I open a "raw socket": > > int s =3D socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); > > Next, I have to get a handle to the network card. Let's say that the > network card is called "wlan0": > > struct sockaddr_ll nic_dev =3D {0}; > struct ifreq card =3D {0}; > strcpy(card.ifr_name, "wlan0"); > ioctl(s, SIOCGIFINDEX, &card); > nic_dev.sll_ifindex =3D card.ifr_ifindex; > > Next when I want to send a frame, all I have to do is: > > sendto(s, &frame, sizeof frame, 0, (struct sockaddr > const*)&nic_dev,sizeof nic_dev); > > Is there anything better than Berkeley Socket for sending custom frames? > Anything more portable? Or is there a better way of using Berkeley > sockets than the way I've shown above? > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > I ran into this a little while back and the code at this link was very help= ful http://www.codeproject.com/KB/IP/sendrawpacket.aspx BTW: All the folks who had comments about comments on the code you guys really need to write some more code :-), this is prototype code that tamas was after and I could read it just fine :-) -- = http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist