Re: how to enumerate TCP and UDP listeners
Re: how to enumerate TCP and UDP listeners
- Subject: Re: how to enumerate TCP and UDP listeners
- From: "Peter Sichel" <email@hidden>
- Date: Fri, 20 Oct 2006 16:29:43 -0400
On 10/20/06, email@hidden wrote:
>How can I enumerate open TCP and UDP ports on my computer
>programmatically in OS X? Also, is there a way to enumerate open TCP
>connections?
You can use sysctl to retrieve the PCB (Protocol Control Block) list
and then parse the list entries (one per connection) to examine the
connection state. "UNIX Network Programming" by Stevens describes
this in more detail.
The parameters of interest are:
CTL_NET.AF_INET.IPPROTO_TCP.TCPCTL_PCBLIST
CTL_NET.AF_INET.IPPROTO_UDP.UDPCTL_PCBLIST
I have found it difficult to associate individual connection entries
with the corresponding user level sockets and process IDs or names. The
more kernel data structures you examine, the greater your compatibility
risk since they were never intended to provide binary compatibility
(only source level). As Zack suggested, UNIX lsof will do this analysis
for you, but can be expensive. The advantage is that Apple updates lsof
as needed so you don't have to keep fixing your own code.
My "Connection List" tool (IPNetMonitorX) uses a mixed approach. It
uses sysctl to retrieve the connection parameters every second, and
calls on lsof to update other information less frequently.
I'd love to figure out a stable and efficient way to get all the
connection and process information programatically, but I am unsure
whether it exists at this time.
Kind Regards,
- Peter Sichel
Sustainable Softworks
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden