views:

195

answers:

5

Hi,

How do I check with C if a port on my local machine (if required by passing an IP or interface, too), is in listen state? I don't want to connect to this port for checking because I don't want to irritate the service behind this port.

I want to use this to add the missing net.tcp.listen item to Zabbix.

EDIT - THIS IS THE REAL ANSWER:

The correct way is to read the socket tables:

/proc/net/tcp /proc/net/tcp6

They contain lines like:

sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
0: 00000000:1F40 00000000:0000 0A 00000000:00000000 00:00000000 00000000   101        0 4083927 1 f5d15240 750 0 0 2 -1
1: 00000000:2742 00000000:0000 0A 00000000:00000000 00:00000000 00000000  1002        0 6100 1 decd76c0 750 0 0 2 -1

and can easily parsed for listening sockets (dst:00000000:0000). An strace on netstat shows that netstat works the same way.

+2  A: 

Something like netstat ?

`netstat -anp`

Will list all the ports and should do the trick.

Since this is open-source software, you can probably take some inspiration in it. And i believe it is written in C.

ereOn
Good idea. Netstat has to be called with -l, thought.
Daniel
@Daniel: Thanks ;) I wasn't sure about the parameters and have no Linux at my disposal at the moment.
ereOn
Your idea however let me to do an "strace netstat -l", which showed me the real solution (see my answer to this question).
Daniel
+6  A: 

Just try to open the port for listening. You will get an error.

There is no way you can steal a port from another process, so this will be safe, and of course easy to implement as it requires no additional code other than proper error handling.

Peter Tillemans
probably the most portable way, too
matja
This won't indicate whether the port is in `listen` state or in a `connected` state. Just that it is used.
ereOn
Good idea. Sadly has the race condition that your service, if it tries to use its port exactly in the same moment, might raise an error then.
Daniel
A: 

I don't know if you just want to see if your ports are open properly or if you want to integrate this into an application somehow. If yours is the prior case

nmap localhost

should handle it for you

dagoof
very, very bad idea
unbeli
As I stated: I don't want to connect to the port, even if nmap has implemented stealth scans which won't reach the app.
Daniel
A: 

Hi,

As per your query it seems to be that you want to check open port on your server, but on sure which command need to be run. No need to worries for example we try to found out if port 80 open on your server.

First login into server as root user

root@[~]# whoami
root

Now run following command.

root@[~]# netstat -anp | grep :80

It will give following output if port 80 open.

tcp        0      0 78.129.128.40:80            207.46.12.20:62746          TIME_WAIT   -
tcp        0      0 78.129.128.40:80            74.53.3.132:47003           TIME_WAIT   -
tcp        0      0 78.129.128.40:80            85.9.80.110:63325           FIN_WAIT2   -

And if port 80 blocked in your server you will not receive any output.

You can also use following URL to check open ports on your server.

http://www.yougetsignal.com/tools/open-ports/

Regards, Gunjan

I need this for a monitoring tool, to invoking other websites is not feasable. In addition I wanted to implement this in C, not as shell commands.
Daniel
A: 

The correct way is to read the socket tables:

/proc/net/tcp /proc/net/tcp6

They contain lines like:

sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
0: 00000000:1F40 00000000:0000 0A 00000000:00000000 00:00000000 00000000   101        0 4083927 1 f5d15240 750 0 0 2 -1
1: 00000000:2742 00000000:0000 0A 00000000:00000000 00:00000000 00000000  1002        0 6100 1 decd76c0 750 0 0 2 -1

and can easily parsed for listening sockets (dst:00000000:0000). An strace on netstat shows that netstat works the same way.

Daniel