Socket, how to find local address when binded with INADDR_ANY - c

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.

Related

How to bind the socket for the specific Ip address in the network?

When we are binding a socket, in the address field we can specify like this,
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
Here it will accept the request from any machine, Here I want accept the request from the specific Ip address.
For that I used like this
inet_addr(ipaddress);
But it doesn't work, How to get the connection form the single Ip address?
You don't need the networking stack to do that, you can do it at the application level.
After you've accepted an incoming connection, check the peer's address and if you don't like it just close() the connection.

connect function block when a router cannot connect to internet

I'm using the basic socket functions in C language, the line below:
int res = connect(sock_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
1. when my laptop is can access the internet, connect will return immediately,connection is built as expected.
2. when my laptop is offline, connect will return immediately, the errno is "network is unreachable.
3. when my laptop is connected to a wifi network that cannot access internet directly(need login opration or sth else), connect will block about 20 seconds before return, the errno is "connection refused"
It can be solved by set the sock_fd nonblocking, I want to know why it takes such a long time before connect return a connection mistake in the third situation?
In the 2nd case (network offline), the operating system knows it is offline and so the network libraries can immediately signal a failure.
In the 3rd case (network online, but unable to reach internet) the operating system thinks it is online, but has no way to tell that in advance that the connection won't succeed. The actual failure mode depends on the nature of the network you are connected to:
In the case you are experiencing, the network simply isn't responding at all to to your connection attempt. Your packets are simply being ignored. Eventually you experience a connection timeout, when the connect function gives up waiting for a response. It is normal for it to wait some period of time before giving up, since not all connections on the internet happen instantly (network is slow or congested, or the server on the other end is slow to respond)
In another case, a firewall might be configured to actively reject connections that are not allowed. This means that it will send a packet back in response that says the connection was rejected. If this happens you would quickly get an error back from connect().
There is a third possibility... this is a situation where you are connected to a network that requires you to enter a password on a web page, or click to acknowledge that you acknowledge the policies for using the internet at that business. No matter what internet site you are trying to access with your browser, you first see this page. This is called a captive portal and is implemented by responding to port 80 traffic at any IP address and sending the login page instead. If you were trying to connect a socket to port 80 (the http port) at any IP address, the connection would succeed (even though you are not actually connected to the remote server you expect). If you are using a port other than port 80, it would instead ignore or block the connection. I mention this in case you are trying to write a program that determines if you are connected to the internet or not. Using port 80 wouldn't work well in this case.

Forwarding UDP packets in C using the socket API

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.

How to connect socket via external IP (Mac )

My question is, how to connect to socket on romote mechine?
I can only connect sockets on same network..
I wrote a simple code (in c), that simulate a server (open socket and listen for client). in Mac.
I'm trying to connect this socket as a client from iPhone (with simple objectiv-c code).
If my internet on both, server and client, is on the same network (WiFi) and in client I trying to connect to 192.168.1.x, it's working.
But when, in client, I'm trying to connect via external IP (with the same port) connection is failed.
I never did this bofore. Maybe I miss somthing.. I've tried to turn my FireWall off. It did not help.
Thanks.
Edit: If it's not clear.. my Mac is connected by router.
In a setting like this, the "external IP" would typically be the IP of the router. In all likelihood you'll need to configure the router to forward the relevant port to the internal IP address.
It could also be the case that for the port forwarding to work, the request has to come in on the external (WAN) interface. This depends on how the router is configured. If that's the case, you'll need to make sure that you're accessing the external IP via the cellular network and not the Wi-Fi connection on your iPhone.
If you're connecting to your server via a local IP address (i.e., you're connecting to another machine on the same local router via a delegated DHCP address), then your issue is on the network, not the IP-stack of the local machine.
You'll need to look into your router settings ... many routers will block a number of services, especially those on custom ports, in order to prevent malicious attacks from sources external to the local network.

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