Operating System - HP-UX
1830930 Members
2429 Online
110017 Solutions
New Discussion

Emulate parts of netstat functionality through socket hack

 
Ralph Grothe
Honored Contributor

Emulate parts of netstat functionality through socket hack

Hi system hackers / networkers,

I couldn't quite make up my mind which would be the appropriate section for this posting (either Languages or Networking).
Indeed, this isn't anything especially HP-UX specific, but rather common Unix BSD Socket fair.
But I'm sure there are many experienced system hackers among you sysadmins.
I wish to write a wee prog / script which stats sockets of AF_INET *without* falling back to a system call to netstat.
I would want to do this in Perl with the Socket module, or the more comfortable OO module IO::Socket (which inherits from IO::Handle).
I know that the system calls of the socket library are 1:1 adaptations to Perl so any suggestions in C would also be very welcome.
Having read quite a bit about socket programming in Perl I now would know how to open a socket, bind it to a port, put it into listening mode, and make it accept connections.
Even how to trap dreaded PIPE signals, use syslog, change euid, chroot, disociate from the terminal, auto-background and serve clients, preforking, multiplexing, or simple forking.
I also read how to write clients that connect to a server.
What I didn't find though, was how to simply stat already established (or listening) sockets, say to simply count the number of connectios to a certain port.
As a last resort I think I could get the sources of a GNU netstat, and try to understand how they did it.
But I thought that maybe some of you also know, and could give me a little hint.

Many thanks
Ralph
Madness, thy name is system administration
2 REPLIES 2
Vincent Fleming
Honored Contributor

Re: Emulate parts of netstat functionality through socket hack

There's about 7000 lines of C code in netstat on my FreeBSD system. You can get the code at www.freebsd.org - I'm sure it's in there somewhere.

I think this is what you're looking for:
struct ngpcb *this, *next;
struct ngpcb ngpcb;
struct ngsock info;
struct socket sockb;
/* Read in socket structure */
kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
No matter where you go, there you are.
Ralph Grothe
Honored Contributor

Re: Emulate parts of netstat functionality through socket hack

Hi Vincent,

thanks for looking at netstat's sources.

I would have done this with my Linux distro's netstat as well.

Blimy, 7000 lines of code?

That's what I sort of feared, and why I posted here.

Unfortunately it looks as there aren't many HP-UX admin folks interested in this stuff, gathering from the overwhelming response to this post :-(
Madness, thy name is system administration