The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sendto failing "resource temporarily unavailable" Naanu SUN Solaris 6 03-16-2007 05:41 PM
end-end packet delay? yogesh_powar IP Networking 4 12-13-2005 01:21 PM
Socket Programming socket soshell High Level Programming 2 06-29-2004 08:49 AM
Getting an ACK for RAW SYN packet zampya High Level Programming 17 05-24-2004 10:15 PM
Seeing IP packet manjunath IP Networking 4 09-16-2002 12:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-31-2005
Rakesh Ranjan's Avatar
Rakesh Ranjan Rakesh Ranjan is offline
Registered User
  
 

Join Date: Aug 2005
Location: India
Posts: 42
sendto in packet socket

Hi,
I have created a packet socket (PF_PACKET, SOCK_DGRAM, htons(ETH_P_ARP)) to catch the ARP packets coming to my machine and send appropriate reply.
Now I'm able to recieve the ARP requests using recvfrom but don't know how to send the reply. I looked into man page but I'm not able to comprehend it esp. do I need to give the reciever machine's harware address in the sll_addr field of sockaddr_ll structure (its 8 byte field but my reciever's ethernet address is just 6 byte). Then do I need to bind it to the socket. And lastly in the sendto what to do with the argument specifing reciever's address (do i need to pass same sockaddr_ll structure there and any type casting requd. esp because sockaddr_ll field is 20 byte as against 14 byte sockaddr struct.)
Also specify apart from setting ethernet card to promiscous mode do i need anymore configuration changes.

If possible send me a code fragment showing the use of sendto with packet socket with SOCK_DGRAM.

Please help if u know its very urgent. ur timely reply will b appreciated.
  #2 (permalink)  
Old 08-31-2005
andryk's Avatar
andryk andryk is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2003
Posts: 448
I've never been involved in such project but i guess you need to build the entire packet starting from link level header (eth) to arp and can use write() to send out packet, mac is used to route, no need of ip address required by sendto()
... and of course if u have multiple IF, you might want to bind to the correct IF owning the src mac of the pkt u are sending
Do let me know if my answer hinted u good
  #3 (permalink)  
Old 08-31-2005
Rakesh Ranjan's Avatar
Rakesh Ranjan Rakesh Ranjan is offline
Registered User
  
 

Join Date: Aug 2005
Location: India
Posts: 42
Quote:
Originally Posted by andryk
I've never been involved in such project but i guess you need to build the entire packet starting from link level header (eth) to arp and can use write() to send out packet, mac is used to route, no need of ip address required by sendto()
... and of course if u have multiple IF, you might want to bind to the correct IF owning the src mac of the pkt u are sending
Do let me know if my answer hinted u good

thanks andryk
Since I'm using SOCK_DGRAM I think I need not create any ethernet header the kernel functions parse the packet to remove the header and store it in the sockaddr_ll struct as I could figure out from the man page I need to create an ARP header for reply.
Now the problem comes to the location of address. With some more googling I'm getting the addresss of the client as sockaddr_ll structure ( as against normal sockaddr_in structure we declare the address used in recvfrom as sockaddr_ll and type cast it to sockaddr when passing to recvfrom) as shown below

struct sockaddr_ll client; //declaration for storing address of client sending ARP request

recvfrom(sock,buff,sizeof(buff),0,(struct sockaddr*)&client,&len);

And now I'm getting the address to be used in sendto function's secondlast argument (I guess that this argument is used to construct the ethernet header for reply)

int n = sendto(sock,buff,sizeof(buff),0,(struct sockaddr*)&client,len);

But still I'm not out of trouble as now the sendto doesn't give any error but if I check the characters transferred by sendto (in n above) it is 0. And also the other side doesn't show signs of recieving any packet as seen by monitors (same code as it is recieving the packet well).

So what can be the problem ..... can not binding create problems (I thought no binding will b requd for it makes the app to monitor just one interface and besides does it have to do anything with sending....I guess not) .
Also for that type conversion (from socaddr_ll to sockaddr I'm in doubt for wont it make us to lose some fields as the source is larger than destination).

If u get an answer instantly post it for I can't proceed without that.
And thanks again for showing interest.
  #4 (permalink)  
Old 08-31-2005
andryk's Avatar
andryk andryk is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2003
Posts: 448
A really tough one!

What if u try to use write()...

Building ethernet header is fairly simple just look at /usr/include/linux/if_ether.h just 3 data fields which can be filled by data u have read from recvfrom(), You already know how to build an arp reply header ...
... all you have left to do is then write() down the entire buffer eth+arp, check write() ret-code and sniff/monitor the pkt

For the sockaddr cast problem, i really have no clue
  #5 (permalink)  
Old 09-01-2005
Rakesh Ranjan's Avatar
Rakesh Ranjan Rakesh Ranjan is offline
Registered User
  
 

Join Date: Aug 2005
Location: India
Posts: 42
Thanks andryk,
I'll take up ur suggestion and I don't have a choice too. I was just avoiding the SOCK_RAW for I think I need to delve into details of the device driver (may be netdevice) , I suppose I'll have to write on the device directly so I was using a function like sendto to avoid that as I need to finish up this work in short time.
I won't like to pester u more but for the last time I'm sending u the code ( in attachment ) I wrote if u (or anyone) happen to get the fault in it please tell me so that I don't repeat it (plz. forgive me for poor formatting as I coded it in vi but formatting seems to have changed during uploading...the code is error free but as said doesn't send anything to client (0 bytes sent) I have mentioned the problem in the comments.
Attached Files
File Type: txt Cfile_arp_rcvsnd.txt (5.6 KB, 152 views)
File Type: txt arpheader.h.txt (579 Bytes, 94 views)

Last edited by Rakesh Ranjan; 09-02-2005 at 08:08 AM..
  #6 (permalink)  
Old 09-02-2005
Rakesh Ranjan's Avatar
Rakesh Ranjan Rakesh Ranjan is offline
Registered User
  
 

Join Date: Aug 2005
Location: India
Posts: 42
Eureka!! Eureka!! I found the fault (can't really say that what short of fault was it).
I just type casted the reply buffer to char* in the sendto.
I really can't understand why that worked , I know that the buffer field (2nd field) in sendto is void* so probably some type casting was requd. but in the previous code (in attachment of previous message) too I had type casted it to 'struct arpheader*' but that didn't work now I have declared a buffer of size equal to struct arpheader and transfered the contents of reply to that buffer and sent it, and wow! it worked.

So plz. if anyone like me stuck up with that try this recipe too (don't guarantee any success). And those who can reason why this works send it to me too.
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On




All times are GMT -4. The time now is 06:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0