0

I'm trying to get TCP open port list in iphone by using sysctlbyname().

 sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp,
     size_t newlen);

.

 #include <sys/sysctl.h>

 size_t len = 0;
 if (sysctlbyname("net.inet.tcp.pcblist", 0, &len, 0, 0) < 0) {
      perror("sysctlbyname");
 } else {
      char *buf = malloc(len);
      //printf("%d",sizeof(buf));
      sysctlbyname("net.inet.tcp.pcblist", buf, &len, 0, 0);
      NSData *data = [NSData dataWithBytesNoCopy:buf length:len];
      NSLog(@"data = %@", data);

      //printf("%d",sizeof(buf));
      //printf("%s",buf);
 }

The information is copied into the buffer specified by oldp.

OUTPUT::

data = <18000000 34000000 d8160000 00000000 7d760000 00000000 0c020000 00000000 00000000 00000000 00000000 0050c598 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 c2160000 00000000 40008000 00000000 01400000

Buffer is filled with data here.but I'm unable to print the data in readable format.converting this data into NSString won't help as internally buffer has its own structure.

Anyone knows how to obtain TCP open port list as output from this data?

Thanks.

virata
  • 1,882
  • 15
  • 22

1 Answers1

2

copy netstat code from BSD source.

see the printproto() function in main.c

This will explore all related to this buffer and how to get TCP port list.

Thanks.

virata
  • 1,882
  • 15
  • 22