Ethereal-dev: [ethereal-dev] Question on sendto with PF_PACKET and SOCK_RAW.
I can't seem to find any documentation anywhere on this....
I want to use the new PF_PACKET and SOCK_RAW options to create
a socket that I can send raw ethernet frames on. SOCK_PACKET
was working ok for me, but it's deprecated, evidently, and
it wasn't working on eth8, so I figured it was time to upgrade...
Also, using SIOCGIFINDEX, looking for eth1, gives me '3', when the
only interfaces shown by ifconfig -a are eth0, eth1, and lo (in that order).
Is that correct?
Here is the code that tries to create the socket:
This method seems to work...
int createPacketSocket(const char* dev_name, int ether_type, int dev_idx) {
int s = socket(PF_PACKET, SOCK_RAW, htons(ether_type));
int r; //retval
if (s < 0) {
cerr << "ERROR: socket: " << strerror(errno) << endl;
return s;
}
struct sockaddr_ll myaddr;
// From the man 7 packet man page.
memset(&myaddr, '\0', sizeof(myaddr));
myaddr.sll_family = AF_PACKET;
myaddr.sll_protocol = htons(ether_type);
myaddr.sll_ifindex = dev_idx;
//strcpy(myaddr.sa_data, dev_name);
r = bind(s, (struct sockaddr*)(&myaddr), sizeof(myaddr));
if (r < 0) {
cerr << "ERROR: bind: " << strerror(errno) << endl;
return r;
}
nonblock(s);
return s;
}
sendto gives this error every time: sendto: Invalid argument
To send I use this:
int foo::sendPktTo(int dev_socket, const char* dev, const char* msg,
int msg_len) {
int r = 0;
struct sockaddr from;
memset(&from, 0, sizeof(from));
from.sa_family = AF_INET;
strcpy(from.sa_data, dev);
r = sendto(dev_socket, msg, msg_len, 0, &from, fromlen);
if (r < 0) {
VLOG << "ERROR: foo::sendto: " << strerror(errno) << endl;
}
return r;
}//sendTo
Any help will be greatly appreciated...
Thanks,
Ben
--
Ben Greear (greearb@xxxxxxxxxxxxxxx) http://www.candelatech.com
Author of ScryMUD: scry.wanfear.com 4444 (Released under GPL)
http://scry.wanfear.com http://scry.wanfear.com/~greear