Hi all!
I want to send Ethernet Frames over a RAW sockets, but it doesn't work. That's what i've done so far...
unsigned char hw_source_addr[ETH_ALEN] = {0x00, 0x04, 0x76, 0x45, 0x9D, 0x73};
unsigned char hw_dest_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned char *msg;
int sock_fd;
int msg_size;
int written;
struct sockaddr_ll ll_source_addr;
struct ethhdr ether_header;
char test[4] = {'T', 'e', 's', 't'};
//create socket
if((sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)
{
perror("rp_send(): main(): socket()");
exit(1);
}
//fill Source Address struct
ll_source_addr.sll_family = AF_PACKET;
ll_source_addr.sll_protocol = htons(ETH_P_ALL);
ll_source_addr.sll_ifindex = 1;
ll_source_addr.sll_halen = ETH_ALEN;
memcpy(ll_source_addr.sll_addr, hw_source_addr, ETH_ALEN);
//bind the socket
if( bind(sock_fd, (struct sockaddr*)&ll_source_addr, sizeof(ll_source_addr)) == -1)
{
perror("rp_send(): main(): bind()");
exit(1);
}
//Assigning information to the header
memcpy(ether_header.h_dest, hw_dest_addr, ETH_ALEN);
memcpy(ether_header.h_source, hw_source_addr, ETH_ALEN);
ether_header.h_proto = htons(0x0800);
//Creating the packet
msg_size = sizeof(ether_header) + sizeof(test);
if((msg = (unsigned char *)malloc(msg_size*sizeof(unsigned char))) == NULL)
{
perror("rp_send: main(): malloc(): No memory available");
exit(1);
}
memcpy(msg, ðer_header, sizeof(ether_header));
memcpy(msg + sizeof(ether_header), test, sizeof(test));
//Send the packet
if( written = write(sock_fd, (const void *)msg, msg_size) == -1)
{
perror("rp_send: main(): sendto()");
exit(1);
}
printf("%i bytes printed on the socket.\n", written);
The last line's output is 0, so nothing is written at all. If I receive packets from this socket (with read()) all works fine. So please help me......
Greetings
Michael
==================================
Poster's IP address: 53.122.194.71
Posted via
http://nodevice.com
Linux Programmer's Site