LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2010, 08:11 AM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Getting a network port


I want to select a network port. The program I am writing is usually non-root, so ports 1024 and up are the usable space. This program will be running two other programs, telling one of them to listen to that port, and telling the other to connect to that port. Those programs have no means to just use a passed file descriptor or file name for this purpose. They can only do their function by TCP listen or connection to a port. The language this program will be written in is C.

Sorry that the description is vague. But that is intention to avoid the thread going off topic into other aspects of this program. I want to strictly focus on finding the proper means to choose a port number. This does include what range of ports might be preferred (for example, if the choice is made by random numbers).

I do know that one possibility is for this program to set up its own sockets for communication to each of the two programs, and shuttle traffic between them after the connections are made. That is a fallback plan. I am, right now, trying to find a way to make the two programs connect directly (one will listen, then the other will connect to it). I just know if I didn't say this, someone would suggest exactly this as a "solution". I want something better (e.g. direct).
 
Old 08-06-2010, 09:02 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
If your program tries to open a socket for listening on a particular port, the OS will either allow it, or not. If the port is already in use, it will disallow it. Choose a port number that does not appear in /etc/services.
Your fallback plan should be the primary plan. There really isn't another option. If there was, why would anyone use sockets?
--- rod.
 
Old 08-06-2010, 09:58 AM   #3
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by theNbomr View Post
There really isn't another option. If there was, why would anyone use sockets?
--- rod.
I don't understand the logic that gets to that last part. Potentially, there might be a way to reserve a port, disallowing others to use it (as already happens now), but have a means to pass it on to another process. One thing I'm trying to think through is to bind a couple sockets now, and see if I can get those programs to just close the sockets passed to them as soon as they run. There would just be a minor window where something else on the system could grab that port.
 
Old 08-06-2010, 10:50 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Are you trying to establish communication between processes that are always on a single host? If so, then you probably don't want to use sockets at all. Interprocess Communication (IPC) provides much more efficient means of doing this. If this is what you are after, then a good place to get started with it would be Beej's Guide to IPC.

Otherwise, I think you're overcomplicating the matter of allocating a port number. Just choose one; there are plenty available, and the well known ones are already documented (/etc/services). Make your application(s) understand a method of using a non-default port (commandline argument, config file setting, environment variable), in case they have to run where the default port is unavailable. There isn't any way of 'reserving' a port number. Especially, you can't bind to a port, and somehow prevent other processes from binding to it once the socket is closed.

--- rod.
 
Old 08-09-2010, 10:36 AM   #5
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
Quote:
Originally Posted by theNbomr View Post
Are you trying to establish communication between processes that are always on a single host? If so, then you probably don't want to use sockets at all. Interprocess Communication (IPC) provides much more efficient means of doing this ...
Quote:
Originally Posted by theNbomr View Post
Otherwise, I think you're overcomplicating the matter of allocating a port number. Just choose one; there are plenty available ...
Yes, it will be on a single host. Unfortunately, the applications that need to talk to each other are not mine. Although they are open source, I don't want to have to be in the loop to maintain patches to them to make them handle IPC beyond just port numbers. All I would be doing is invoking them in a way that ensures the two I do invoke (one to listen, other to connect) talk to each other. Ports will need to be random in some way because there can be many instances of my program running pairs of the other programs.
Quote:
Originally Posted by theNbomr View Post
There isn't any way of 'reserving' a port number. Especially, you can't bind to a port, and somehow prevent other processes from binding to it once the socket is closed.
Right ... or somehow allow select processes to bind to it when it is reserved, AFAIK. Unfortunately, the applications don't have a means to use an inherited socket descriptor or Unix namespace sockets (if I were to modify the applications, I'd most likely add Unix namespace sockets).

It looks like what I need to do is just generate attempts at random ports and try to see if the first program, to do a listen, can succeed. If it fails, generate a new random number and try again. Then once it works, start the 2nd program. But this gets really messy because more than half of these will be OpenSSH doing port forwarding (to carry out the same thing between machines). And OpenSSH goes ahead and logs in to the remote host even with ExitOnForwardFailure specified (it exits after logging in), which could trigger login rate checks.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to disable network port 53 Brickwall Fedora 6 01-29-2009 05:08 PM
USB network port, how to? skippy1729 Linux - Wireless Networking 23 11-01-2008 02:15 PM
best port scanner To scan open port in a network tanveer Linux - Security 8 01-21-2007 08:19 PM
network console port ah_kayyali Linux - Networking 3 03-19-2006 11:20 AM
network port to file antken Linux - General 3 11-01-2002 09:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration