Forwarding UDP packets in C using the socket API - c

I am writing a content filter in C using the socket API that will intercept DNS requests, and only return the response if the domain is allowed. The pseudocode to describe this is:
Redirect all DNS queries to the content filter program which is listening on UDP port X.
Content filter program extracts domain being queried and determines if it is allowed or not.
If it is allowed, then the content filter program forwards the original DNS request packet to the original destination DNS server while maintaining the original source and IP+port so that the DNS server can send the reply directly back to the client.
If the domain is not allowed, then no reply is sent.
I currently have the program listening on UDP port X but the problem is that I can't access the IP headers, and therefore can't simply forward the DNS request to the original server while maintaining the original headers.
I have tried using socket(AF_INET, SOCK_RAW, IPPROTO_UDP) but that doesn't bind on port X (understandably), and doesn't receive any traffic.
What is the best way to go about listening on UDP port X, while still being able to access the IP headers of incoming packets?

I think recvfrom on an UDP socket should give you the correct source address. You still probably need a raw socket for forwarding the message.

The functionality for SOCK_RAW based sockets varies depending on the platform you are on. Generally, when you want to get access to the full IP datagram information, then I would recommend using the Berkeley Packet Filter to tap the data-link layer frames addressed to UDP port of interest.

Related

Socket, how to find local address when binded with INADDR_ANY

I'm recoding a simple ftp server and I'm stuck of the implemetation of the PASV command.
In fact, when the client send a PASV command, I have to create a new server socket and send back the infos (address and port) to the client so that he can connect to it to create the data connection.
What I do step by step is:
Create a server socket with INADDR_ANY and random port (port 0)
listen() with this socket
getsockname() this socket to get it's infos
send back the infos to the client on the command connection
the accept() is done later, when the user need to use the data con
(Is that the right way to do it ?)
The thing is, the server socket that I create on the server side is binded with INADDR_ANY so getsockname() on it always return 0.0.0.0 (cause it is binded to virtually all the ips off the system).
Is that case, what address should I send back to the client and how to find it ?
How real ftp servers handle this ?
Thanks :)
What I do step by step is:
Create a server socket with INADDR_ANY and random port (port 0)
listen() with this socket
getsockname() this socket to get it's infos
send back the infos to the client on the command connection
the accept() is done later, when the user need to use the data con
(Is that the right way to do it ?)
In general, yes, however steps #1 and #3 tend to be more complicated than that.
For one thing, getsockname() cannot get the true IP until the socket is actually connected to someone (which, in this case, means you would have to call getsockname() on the socket returned by accept(), not on the socket that you call accept() on). So, when binding to INADDR_ANY, you should just report the IP of the interface that the command socket is connected to. In which case, it is better to just bind the listening socket to only that interface directly and not to INAADDR_ANY at all.
For another thing, even if the server machine only has 1 interface installed, if the server is behind a NAT router, and the client is outside the NAT, then you would have to report the router's public IP instead of your server's listening IP. You would have to know the router's public IP ahead of time, either by storing it in your app's configuration, or by dynamically querying the router itself via uPNP, or by querying an outside service like http://iplookup.flashfxp.com/. Unless the router is FTP-aware (some are) and is smart enough to replace the reported IP for you when passing through the router, in which case you do have to report your listening IP instead.
You should call getsockname() on the command socket. That gives you the IP address the client used to connect to you. It can be different for each client on a multi-homed host. If you're behind a NAT device you should use its public IP address, which you will have to obtain via configuration.

Using Wifi module ESP8266 to send UDP broadcast message

I need to communicate Arduino module with my Phone through ESP8266 wifi module
They all link to the local LAN network and didn't know the IP address of each other, so I plan to send UDP broadcast message from my phone, but ESP8266 module seems doesn't receive the message
The ESP8266 library I use is from here:
https://github.com/itead/ITEADLIB_Arduino_WeeESP8266
and use the function registerUDP() and recv()
Can somebody help me?
I don't have enough reputation to leave a comment so I will make an answer.
I have a feeling that your registerUDP() need to specify an IP address and I guess you are supplying your DHCP assigned IP address. Where as the UDP broadcast is sent using a multicast or broadcast IP.
You can modify the initiator code to send UDP packet to all IP within the same subnet (e.g. 192.168.1.1 to 192.168.1.254) and see if it works.

Problems with receiving udp multicast packets on client side

I have strange problem with multicast server/client application. I wrote simple server and client udp multicast application in C and I tested it using two laptops and one wireless router.Server is connected with the ethernet cable to the wrt54gl linksys router with 192.168.1.101 IP address. Client with IP 192.168.1.105 address is connected to the router via wireless interface. They can ping each other. Server is sending UDP packets using sendto function and client is receiving them using recvfrom function. When I run my server I can see sent udp packet in wireshark destined for multicast group with IP address 226.1.1.1 but on client side I don't receive those packets and also I can't see them in wireshark. I'm pretty sure that code is properly written. Do you have experience with multicast to advice me?
Thank you

raw_socket communication using udp protocol

Am just beginner of this socket programming ...
i tried to make a UDP client_server program using SOCK_RAW .I can send message from the server part.But in client part am getting garbage values. And the receiving message packet size also different.Can U help me to do this client server program using SOCK_RAW.
I tried this client -server with eth0 & eht1,i tried to send 1 packet from eth0 to eth1 sending side is showing "OK" message.. but received data is garbage..
before i done normal client server program using UDP protocol.I got correct output from normal udp client server .When i tried with SOCK_ROW its showing error in sending part.
Make your life simpler and use SOCK_DGRAM - Kernel will take care of datagram headers, you only need to provide valid IP address and data. It is user's responsibility to append (before transmitting) and parse (upon reception) UDP headers using SOCK_RAW.
Most likely you are seeing IP and UDP headers and calling them "garbage". That is what SOCK_ROW does. You should parse and skip protocol headers if you want to get to your message content while using SOCK_ROW.

Communicate to public IP from a local network (WinSock, C)

I'm new to this forum (at least at asking questions), so please be patient. I'm trying to write simple client-server applications to communicate using datagram (UDP) sockets, with C and Windows.
The scenario is very simple, I want to be able to send/receive data from a machine in some local network (behind a GATEWAY/NAT) to some host having public IP. I can send the data, but it seems impossible to receive any data back, because the remote server can't see the client's local IP.
I don't want the user to perform manual port forwarding on the GATEWAY, and I also don't want to use UPnP because of security issues (though it also should be enabled by the user).
I think it is possible because applications like Skype or Browsers can do it, but how ?
Are there some automatically forwarded ports or things like that ? I'm exhausted of searching the web ... PLEASE HELP !
For UDP connections, most home NAT gateways will automatically create a temporary reverse mapping for each outbound packet.
Consider a scenario where your client is running on the internal machine with IP address 192.168.4.5, sending a UDP packet from port 65000 to the external address 64.34.119.12:6789, via a gateway with external IP address 192.0.43.10. When the gateway sees your internal client send a UDP packet from 192.168.4.5:65000 to the external address, it will NAT it to an external address and port, like 192.0.43.10:5500. Your server will see a packet with a source address of 192.0.43.10:5500 and destination address 64.34.119.12:6789. The router also sets up a reverse mapping, so that if it sees a packet arrive on the external interface with a source of 64.34.119.12:6789 and a destination of 192.0.43.10:5500, it will redirect it back to 192.168.4.5:65000. This mapping typically times out after a short while.
This means that in simple cases, all you need to do is:
On the client, use the same port to send to the server and listen for responses;
On the server, respond to the client at the address and port that the client's packet was receieved from, using the same server port that recieved the packet;
Have the client send the initial packet in the conversation;
Don't leave the "connection" idle for more than a few minutes at a time.

Resources