6

i have never written anything like it, how do i check things like if a port is empty using c program in Linux environment thanks a lot.

ps looking for a way, by not using bind or connect and checking if it failed.

edit i cant use bind or connect, looking for faster way to find 3k ports that are free in a row

JohnnyF
  • 1,053
  • 4
  • 16
  • 31
  • The link here http://stackoverflow.com/a/10294941/2959769 explains how this can lead to race condition – Vinay Shukla Jul 28 '14 at 06:43
  • 1
    Have in mind ports are not simply "open" and "closed". They are bound by interface and protocol. You can have port 80 TCP open by an application in interface `lo`, another application opening the same port in the same interface but UDP, and other two applications doing the same but in interface `eth0` etc. Same port open by numerous different servers and they will all be happy with that. – Havenard Jul 28 '14 at 07:28
  • Related: https://unix.stackexchange.com/a/15513/31228 suggests a strategy for reserving a contiguous range of ports. – Jonathan Ben-Avraham May 22 '20 at 13:43
  • @VinayShukla For many applications, for example, on servers that have only services running and no users initiating connections, the chances of this race condition ever occurring are so low as to be worth the risk of whatever strategy is used to check for free ports, with or without actually binding to them. – Jonathan Ben-Avraham May 22 '20 at 13:51

4 Answers4

2

Better way is to use next free port,You can also use 0 port bind will use the next available port.

You can get port selected by bind() by following code

struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(sock, (struct sockaddr *)&sin, &len) != -1)
  printf("port number %d\n", ntohs(sin.sin_port)); 

Also refer How to bind to any available port? for more inforamtion

Community
  • 1
  • 1
Rahul R Dhobi
  • 5,668
  • 1
  • 29
  • 38
1

Run following command using system() or popen()

netstat -antu

It will give list of all used port of your machine. You need to parse output of that command and then you will have list of all busy port.

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • 2
    I think that is not the apt way of doing this while it is being parsed maybe the connection to the port is closed and it becomes free. – Vinay Shukla Jul 28 '14 at 06:42
  • Yea in that case it is not much Optimized way of doing that. Usualy i do check ports using checking bind or connect but OP does not wants that way. – Jeegar Patel Jul 28 '14 at 06:47
  • 1
    Great, i hope it will work, i went and made this program in matlab because i didnt know i have the system command in c – JohnnyF Jul 28 '14 at 07:06
  • Happy to help. In C on Linux machine you need to include stdlib.h to use system(). – Jeegar Patel Jul 28 '14 at 07:10
  • It is bad practice to use `system()` for something you can do programatically. It is handy, but it is hacky and ugly. – Havenard Jul 28 '14 at 07:10
  • System command would do the same. Common man. One will not implement whole source code of netstat command to not use system(). – Jeegar Patel Jul 28 '14 at 07:12
  • No need to implement it whole, only the part that matters. – Havenard Jul 28 '14 at 07:12
  • Because it is relying on external commands to do something you could fix yourself. Those commands may not even be installed or may have different syntax or output depending on the system version. It is understandable if you would use it to run `ffmpeg` for instance, but `netstat` is pushing. – Havenard Jul 28 '14 at 07:20
  • If command is not installed or something else it will through error. And Before running your application your config file or anything else should make sure about its dependencies required command and all. I do not have deep knowledge of networking and Os then why would i write same logic agin instead of using system command? No benefits in writing same logic/code again. That command would be tested ok but if you write new code who will test that? – Jeegar Patel Jul 28 '14 at 07:24
  • 3
    @Mr.32 Also ``system`` is easy to use incorrectly, leading to vulnerabilities. For example, if the process is running with privilegues and omits the absolute path to ``netstat`` (like in this answer), it may be possible for users to place a malicious executable called ``netstat`` in the ``PATH`` of the process, making it run with privileuges. Other issues include platform dependent output of netstat-called executables (I imagine BSD netstat to have a different output, if it exists there at all) – Jonas Schäfer Jul 28 '14 at 07:58
0

How about you using bind() directly, and if it doesn't succeed you can try another port.

You just checked, that a port was free, but someone already used it would be a race condition,so checking if a port is free and then binding it is not possible

You can also read /proc/net/tcp for help but the race condition can still occur.

Vinay Shukla
  • 1,818
  • 13
  • 41
-1

i had same problem the question is do you need to check just one port or many ports

If you need to check just one or few use bind, if it works then its free (and dont forget to free the socket)

if like me you need to check many ports, then what worked for me is run system('netstat -tulpn') and redirect output to a file/variable and then on this info search for ":{yourport}"

worked for me

ps if like me you need to keep them free, tell your computer not to randomlly allocate ports in that area

JohnnyF
  • 1,053
  • 4
  • 16
  • 31