Networking Forums

Networking Forums > Computer Networking > Linux Networking > Problems with RAW Sockets

Reply
Thread Tools Display Modes

Problems with RAW Sockets

 
 
53.122.194.71 [Michael]
Guest
Posts: n/a

 
      07-24-2003, 10:32 PM

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, &ether_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
 
Reply With Quote
 
 
 
 
Atul Kumar
Guest
Posts: n/a

 
      07-25-2003, 01:55 PM
>
> //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


Dear Michael,
Nothing is wrong with your code except two missing
parantheses. Replace the line
if( written = write(sock_fd, (const void *)msg, msg_size) == -1)
by
if( ( written = write(sock_fd, (const void *)msg, msg_size) ) ==
-1)

Atul
 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Max TCP sockets CJS Windows Networking 1 02-28-2007 06:20 PM
sockets Sam Linux Networking 2 07-07-2004 08:54 AM
UDP sockets Sven Jacobs Linux Networking 0 05-10-2004 06:06 AM
Sockets Anthony T. Wilson Linux Networking 1 12-25-2003 07:45 PM
raw sockets Linux Networking 1 12-22-2003 02:28 PM



1 2 3 4 5 6 7 8 9 10 11