sendto in packet socket


 
Thread Tools Search this Thread
Top Forums Programming sendto in packet socket
# 1  
Old 08-31-2005
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  
Old 08-31-2005
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 Smilie
Do let me know if my answer hinted u good Smilie
# 3  
Old 08-31-2005
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 Smilie
Do let me know if my answer hinted u good Smilie

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  
Old 08-31-2005
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 Smilie
# 5  
Old 09-01-2005
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.

Last edited by Rakesh Ranjan; 09-02-2005 at 08:08 AM..
# 6  
Old 09-02-2005
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Packet loss coming with big packet size ping

(5 Replies)
Discussion started by: Vishal_dba
5 Replies

2. Programming

Raw Socket Programming - Efficient Packet Sniffer

Hi, I have the requirement to sniff packets from the Ethernet card on my Linux machine and process it and feed it to a RANAP protocol stack. So far I have written the raw packet sniffer and successfully sniffing packets and do little processing. However, for huge number of packets ... (9 Replies)
Discussion started by: rstnsrr
9 Replies

3. IP Networking

NS3 SendTo

Hi, I am working to implement a basic WiFi infrastructure in NS3. I have placed 16 APs in grid and one client is moving using random walk. Client and AP can communicate through message exchange. However, when I try a message of size more than 30 bytes, it is crashed. Please help me if anyone have... (0 Replies)
Discussion started by: dandapat
0 Replies

4. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

5. Programming

Receiving broadcast packets using packet socket

Hello I try to send DHCP RENEW packets to the network and receive the responses. I broadcast the packet and I can see that it's successfully sent using Wireshark. But I have difficulties receiving the responses.I use packet sockets to catch the packets. I can see that there are responses to my... (0 Replies)
Discussion started by: xyzt
0 Replies

6. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

7. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

8. IP Networking

Error in sendto and recvfrom

Hi guys below is the code which I wrote to send some message to the server and the receive a a reply message from the server. Initially, I wrote one program to sendto and one program to receive from, it is working fine seperately. When I tried coding sendto and recvfrom in a sinlge program then... (0 Replies)
Discussion started by: arunc.kris
0 Replies

9. IP Networking

sendto invalid argument

Hi I lost a lot of time in understanding the message "sendto Invalid argument" when I execute the following code. This code is a simple UDP sender improved with some reliability feature. My goal is to send a file. I've reported only the code which may be useful. Can anyone help me? Thank you... (0 Replies)
Discussion started by: Puntino
0 Replies

10. Ubuntu

Permission denied with sendto in Ubuntu

I hope to post in the right place, otherwise I apologize for this I wrote a UDP sender and a UDP receiver, my goal is to transmit a file from th sender to the receiver. I'm using Ubuntu 8.04 Compiling goes well but when I execute the sender, its "sendto" gives me the error "Permission denied" .... (0 Replies)
Discussion started by: Puntino
0 Replies
Login or Register to Ask a Question