sockets - Injecting raw packet onto network in C (Linux) -


i trying write function takes stream of bytes (including ethernet header , perhaps upper layer protocols encapsulated within ethernet packet well) , send out on network on particular interface.

here summary of code:

// create socket int s = socket(pf_packet, sock_raw, htons(eth_p_all)); if (s < 0) {     // error handling }  // set receive/send packets on 1 interface (stored in variable iface_name e.g. eth0) struct ifreq ifr; bzero(&ifr, sizeof(struct ifreq)); strncpy(ifr.ifr_ifrn.ifrn_name, iface_name, ifnamsiz); if (ioctl(s, siocgifindex, &ifr) < 0) {     // error handling }  // set socaddr write struct sockaddr sa; sa.sa_family = pf_packet; sa.sa_data = htons(eth_p_all);  // write wire // buf has type char* , len has type int // buf contains ethernet header, followed payload (e.g. arp packet or ip packet) if ( sendto(s, buf, len, 0, &sa, sizeof(struct sockaddr) ) < 0 ) {     perror("sendto");     // more error handling } 

i error

sendto: invalid argument 

how can fix error?

my initial guess somehow caused sa argument, since others standard , there's not go wrong. replace argument of sockaddr_ll type in this example, mean extracting header information buf , seems little pointless since it's there, ready go. there must better way? encapsulate, unencapsulate, re-encapsulate, send seems has many unnecessary steps. writing underlying interface pre-existing code, there no way can adapt input not have data-link layer headers included.

its libraries:

http://linux.die.net/man/7/raw see header definition , @ how makes socket he's not using htons, rather: ipproto_raw located in

  #include <netinet/in.h> 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -