Linux : IP address to service? - linux

I have an app running as a daemon on a linux box (Red Hat). The purpose of the app is to act as a registrar of sorts -- another device can connect to this host machine, and register (this is me, I am here -- this kind of thing). There is more than one IP address for this machine.
How do I connect/send/forward incoming connections (i.e., the registration data) to my daemon? That is, when the device wants to register and connects to my IP address, how do I direct the traffic to my process/daemon?
Thank you very much for all your help!
:bp:

Hi you may need to give a little more info.
What it sounds like you want to do is listen on a tcp port with your daemon. There are different ways to do this in different languages.
Another thing to watch out for are the reserved port ranges. Some ports can only be used by users with elevated privileges. See here http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers TCP and UDP port ranges, They dynamic range is free to use as long as there is nothing else on that port.
What language are you using? In Perl This, is how you listen. And I could list off other tutorials for other languages, but there is not much point.
In reponse to the comment, I think this stackoverflow question answers the question.

Related

What port number shall I use?

I am writing an app that is to be running on a Windows PC. I need to create a server socket listening on 127.0.0.1, and another client socket which is to connect with this server socket.
Since the data exchange between the two sockets are within the same machine and there is no client connecting from outside of the machine, what port to use is insignificant, as long as the two sockets use the same port number.
So, how do I decide which port number to use? Shall it be a hard-coded port number such as 49500? What if another unrelated app on this machine happen to use this port number? Or shall I get the list of all used ports and programmatically pick an unused port?
Just want to know what is the best approach. Thanks.
ports within 0 to 1023 are generally controlled and you should assign your socket with higher port numbers, although in that range ports within 1024 and 49151 can be registered for others to be informed about that and not use them.
if you want to avoid conflicts you can see registered ports on your machine and assign a port number to your socket which is empty but ports higher than that (49152 to 65535) are completely free and are not even registered.
generally, it is not common to worry about that. for example, two major applications like VMware and apache web server operate on the same port number (443), and if you want to use VMware workstation and Xampp (which works with apache) you have to simply make one of them listen on another port and its not a big deal. so in my opinion the best practice is to let your users change this via a config file or something similar.
for further information, you can search google. for instance this link might be useful:
https://www.sciencedirect.com/topics/computer-science/registered-port#:~:text=Well%2Dknown%20ports%E2%80%94Ports%20in,1023%20are%20assigned%20and%20controlled.&text=Registered%20ports%E2%80%94Ports%20in%20the,be%20registered%20to%20prevent%20duplication.&text=Dynamic%20ports%E2%80%94Ports%20in%20the,assigned%2C%20controlled%2C%20or%20registered.

Fundamental Networking Understanding

treading some unfamiliar waters. I'm currently writing a game client in Unity/C# and a game server in NodeJS. I've been doing a lot of Googling to try and get some answers, but there are some fundamentals I'm definitely not understanding. From what I understand, there are three relevant IP addresses here: my external IP address, 127.0.0.1, and 0.0.0.0. I've tried every permutation of these for client and server (with or without common sense), and the only combination that works is 127.0.0.1 x 127.0.0.1. I assume ultimately that the server will need to be on 0.0.0.0, but currently Unity errors out because the socket is already in use by the server- I assume that this would not be an issue with remote hosting.
My questions are:
Is it possible to host this on my home computer without socket overlap? Virtual machine?
Why does 127.0.0.1 x 127.0.0.1 work? Does the client convert it to another local address under the hood? I assume that they would both be UDP/127.0.0.1/port sockets.
I'd like to put the multiplayer into this game as soon as possible. Thank you!
Yes, it is possible, with virtual hosts, virtual machines. If you are working in windows, take a look at XAMPP or WAMPP if you are in a Mac.
This is a little big larger:
127.0.0.1 is the localhost (basically, yourself) also known as loopback adress.
And 0.0.0.0 is a non-routable meta-adress. Basically it serves to designate invalid targets, it's like a placeholder for "unknown" adresses. Serves to designate invalid targets doesn't means that is an invalid adress!
In your context, if you're using the 0.0.0.0 as a server it means "all IPv4 adresses on the local machine". If a host has two IP addresses, 192.168.1.1 and 10.1.2.1, and a server running on the host listens on 0.0.0.0, it will be reachable at both of those IPs.
This tutorial from Tom Weiland really helps me to learn about on-line programming in Unity, check it out: https://www.youtube.com/watch?v=uh8XaC0Y5MA

Python – Exchange Data Between Different Networks

I investigated a lot about this topic but most of the guides just teach how to exchange data between devices on the same network and, regarding exchanging data between devices on different networks, no source was totally clear to me. I hope with this question somebody can give me (and other users) a good overview. If you have any guide or book about it I’d be super interested (for Java would also be fine).
First of all I’m interested in the difference between programs that
need to exchange data quickly (it may be an online videogame) versus
programs that need to exchange data accurately (it may be a message
app). My understanding is that the difference between the two is the
protocol used: in the first case is UDP (where no checks are done to
ensure there is no packets loss), in the second case is TCP (where
checks are done and data is exchanged more slowly). Is this correct?
So in an hypothetical Python script in the first case the socket
created would look like this:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
While in the second case would look like this:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
My understanding is that to exchange data between different networks
you have to use port forwarding (very good explanation here),
concept that is clear to me. However, do you have any source that
suggests how to do it in Python? Also, is port forwarding
everything you need to do in order to exchange data between
different networks? Finally, I’m not sure I understand the role UPnP
plays in port forwarding. Based on this question it seems UPnP
is a way to automatically port forwarding. Is it correct? Can I use
miniupnpc library to do it automatically?
Finally, if I switch off and on my router, the private IP addresses
assigned to the devices connected to the network change (so the
private IP of my phone connected to my home WiFi could change, for
example, from 192.168.1.2 to 192.168.1.11). Does this represent a
problem in networking programming? If I set on the router a certain
port and the traffic that comes to that port is directed to a
certain private IP address and then this IP changes I suppose there is a
problem. Is this correct? If it is what is the solution?
Your understanding of use cases for UDP and TCP seem roughly
accurate. UDP ensure lower latency (not always) so for apps that
require lowest latency possible while also not caring about missed
packets, UDP is used. So if you think about video streaming, once a
packet is missed, it makes no sense to hold up every future packet
for that one old packet. This is because a small amount of data that
is missed doesn't really affect a user's watching experience. For
gaming, we want the newest data as soon as possible, so waiting for
old data also doesn't matter. But if you're implementing a protocol
or something that requires all data to be transmitted, TCP makes
sense since its absolutely vital that all information gets to the
receiver and in order.
There are a few methods to exchange data between two private networks. Port forwarding is certainly one method, and both machines on either network would have to have port forwarding. I don't know anything about automated port forwarding like you mention, but you can go into your router settings and set it up pretty easily. Another method of talking across networks is something like webRTC. Its a protocol that uses the STUN TURN and ICE protocols to perform something called NAT traversal. Short story shorter, it tricks your routers into letting your machines talk to each other(analogous to temporary port forwarding).
You're right that this could be an issue. However you should be able to setup static IP addresses in your router. So you can assign one machine to have a static IP address, setup port forwarding, and bam you have a permanent(hopefully) open connection.

How to reserve a tcp local port?

In my application, I bind a socket to the port 38614.
While in test, I find sometime the port is used by another application.
So I failed to bind on it and get the error, "Address already in use".
I did some research, and find I could set the parameter /proc/sys/net/ipv4/ip_loca_port_range to reserve the port for my application.
So I add a new line "net.ipv4.ip_local_port_range = 50000 60000" to the file /etc/sysctl.conf.
Then I reboot the system.
But I still find a port 34660 out of the range which I set is used by some application.
[root#xxxx ~]# netstat -apn | grep fe80::cef1:3
tcp6 0 0 fe80::cef1:12345 fe80::cef1:34660 ESTABLISHED 2401/xxxx
So what should I do to reserve some ports for my application?
Could anyone give me some advice?
Another method is using a small port, such as 1001 which is not used by any other application, but I do not think it is a good idea for the ports smaller than 1024 are reserved for well-known ports.
===============================================================
I have found the answer of the question "how do I reserve ports for my application?"
While I don't think it is a good idea, for it is difficult to make sure that our application runs earlier than any other application. Actually my application has to start after another application which does some prepare for my application.
The linux system may assign any port in the port pool to any application when it need a port.
===============================================================
The parameter "ip_local_reserved_ports" is similar to "ip_local_port_range", they are in the same folder, and it is much more difficult to be tested. In my test the parameter "ip_local_port_range" doesn't work as our expect, so I don't believe the "ip_local_reserved_ports" is ok.
B.R.
Forward
The idea to reserve the port is to block the usage of this port for an outgoing connection. I mean if you have a potential service that will open for LISTEN the port 8080, if you don't reserve it, and the range of ports include this port, could conflict with a outgoing connection that randomly uses 8080 as "source port".
I'm quoting answers of how do I reserve ports for my application?
You could try with:
sysctl -w net.ipv4.ip_local_reserved_ports = 49000, 49001
drop it in /etc/sysctl.conf, and then run sysctl -p.
To ensure the kernel won't give out 49000 and 49001 to clients as you wish to use them for your servers on linux.
Note that this is untested.
However, this is the accepted answer to the question:
Technically, there's no such thing as a "reserved port".
In TCP/UDP, the only way to "reserve" a port is to actually bind() a socket to it. A bound port will not be used by other applications; an unused port is, well, unused so other applications are free to use it.
If you are writing server software, then you can bind your sockets to specific ports as early as you want in the application code. Make the port numbers configurable, or at least clearly state them in the documentation, so that a systems administrator can quickly identify clashes and move conflicting applications to separate servers.

What is the theory behind port forwarding when using P2P programs

Every time I use a different router and different P2P program, I get the same problem - port forwarding. I then usually read random values of ports(TCP, UDP, whatever) and paste it into random places in my router setttings page and repeat this process until the damn thing starts working. As I am a bit tired of doing that i would like to understand the theory behind it a little bit, so that I can put the right things in right places immediately. Could anybody just explain it briefly to me in a few words? Apologies for lengthy description of the problem, but I didn't know how to describe the level of understanding that I am talking about in a more concise way.
Thanks.
Well, the router hides you from the outer world, so you can only make outgoing connections, for which router takes care of sending your requests to the outer world, receiving responses, and sending those back to you. No one can send a packet to you unless you have specifically asked for it—i.e. you can only receive responses.
In case on p2p, the ability to send packets to your machine is important if not vital. So what you do is ask router to forward (here! that's where the word comes from) all incoming packets to port X to your machine, port X.
Originally IP addresses were provided per device, now-a-days we tend to have 1 IP address per household (unless your doing something crazy), also called your external IP. Your external IP is your connection to the world via your router, but each computer within your network has it's own IP (called internal IP). Port forwarding allows the external world to establish communications with a specific computer.
A web server is a simple example, web services typically rely on port 80, what-if in your network you had 4 computers, 1 of which was your web server. How would the outside world know which PC to contact? Port Forwarding allows you to tell your router to direct internet traffic to that server.

Resources