Doddy
Doddy

Reputation: 1259

Get used ports and states

How can I get used ports and their states on Linux? Basically, everything that netstat can do, but in C?

Upvotes: 3

Views: 1198

Answers (2)

Paul Rubel
Paul Rubel

Reputation: 27222

Running strace on a run of netstat will show you the system calls it makes and their arguments.

$ strace netstat
...
open("/proc/net/tcp6", O_RDONLY)        = 3
open("/proc/net/udp", O_RDONLY)         = 3
...

This is often a good way to find out what a program is doing or the calls it makes and can sometimes be easier than looking at the source if all you need is to find out which call to look up on a man page.

Upvotes: 3

BRPocock
BRPocock

Reputation: 13914

Well, for “everything that netstat can do,” you could start with netstat itself. The source code is here:

http://net-tools.git.sourceforge.net/git/gitweb.cgi?p=net-tools/net-tools;a=blob;f=netstat.c;h=f046f09162689f258f8920c1c2af27e01cdc77f2;hb=HEAD

It should be noted that most of what netstat does, it obtains from the /proc filesystem; it looks like the *_do_one routines hold most of the "interesting" guts.

Upvotes: 1

Related Questions