10

I'm trying to monitor network activity on my iPhone from within an app I'm developing. Does iOS support a netstat-like command or something similar that can tell me what what inbound and outbound connections are active ?

Yahia
  • 69,653
  • 9
  • 115
  • 144
VinnyD
  • 3,500
  • 9
  • 34
  • 48
  • no, with Deepak on that -> only thing you can do is monitor the celluar data connectivity state. Thats Apple having its huge hold over what you can't do. – theiOSDude May 26 '11 at 08:30
  • then how does this app work? http://itunes.apple.com/us/app/netstat/id400071873?mt=8 – VinnyD May 26 '11 at 16:57
  • Very interesting indeed. How does http://itunes.apple.com/us/app/netstat/id400071873?mt=8 work? – Maciej Swic Jul 18 '11 at 18:20
  • There are more and more apps on the store that do this, I believe the answer is at a very low level after reading these questions: [Link1](http://stackoverflow.com/questions/1126790/how-to-get-network-adapter-stats-in-linux-mac-osx) [Link2](http://stackoverflow.com/questions/4281531/monitoring-network-usage-excluding-local-traffic) – Zebs Mar 06 '12 at 01:30
  • By using BSD's socket functions, pretty much like netstat is doing... – JustSid Mar 06 '12 at 01:30
  • This question gets pretty close: http://stackoverflow.com/questions/7946699/iphone-data-usage-tracking-monitoring – Zebs Mar 06 '12 at 02:12
  • Check my question..it has sample code with answer :http://stackoverflow.com/questions/19977759/network-activity-monitoring-on-iphone – Shrey Nov 22 '13 at 05:17

3 Answers3

11

After some searching I found Apple's code used for netstat.

Everything you need in the void protopr(uint32_t proto, char *name, int af) function.

I tested on the device and sysctlbyname("net.inet.tcp.pcblist_n",...) works.

That should be all you need.

fbernardo
  • 10,016
  • 3
  • 33
  • 46
  • I found the code on the documentation and it looks very promising, I believe they calculate and the print using `printf(" %7.7s[%1d] %7.7s[%1d]", "rxbytes", prioflag, "txbytes", prioflag);` But I am not that fluent on C, do you know how to interpret the data returned by `sysctlbyname`? – Zebs Mar 06 '12 at 20:26
  • Yes, I do know, but it's all in the Apple code I posted. If you want I can chat tomorrow and help you out with that. – fbernardo Mar 06 '12 at 20:35
  • I would very much appreciate chatting with you tomorrow, meanwhile I will do my best to work out all I can from the inet code, its really cool to find people willing to help online! – Zebs Mar 06 '12 at 20:46
  • @Zebs Sure, I'll just invite you to a chat in these comments when I'm ready. – fbernardo Mar 06 '12 at 21:08
  • definitively! I can send you my gmail using twitter if you want to use google talk. – Zebs Mar 07 '12 at 15:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8642/discussion-between-fbernardo-and-zebs) – fbernardo Mar 07 '12 at 15:27
  • Can I get in on this convo? I'm not that familiar with C either and have no clue how to implement this. – VinnyD Mar 08 '12 at 19:43
2

I can't test this but from what I gather you will have to use sysctl in combination with sysctlnametomib or alternatively sysctlbyname for this:

sysctlbyname("net.inet.tcp.pcblist", ...)

and/or

sysctlbyname("net.inet.udp.pcblist", ...)
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • I have tried and I can call the function but I do not know how to interpret the data it brings back. The question is looking for kbs in and kbs out, do you know how the data should be interpreted? – Zebs Mar 06 '12 at 20:21
  • @Zebs the question did not mention any kbs, it mentions active connections... I can't test this myself but I recommend that you take a look at the [source code of netstat](http://www.opensource.apple.com/source/network_cmds/network_cmds-356.8/netstat.tproj/inet.c) which should give you a good idea on how to interpret the results... – Yahia Mar 06 '12 at 20:24
  • You are absolutely right, that was my question and I assumed it was included in "Monitor Network activity" but you are absolutely right. I will deep dive into netstat to figure out how that data is interpreted. – Zebs Mar 06 '12 at 20:48
0

You can use burp suite for this purpose , By this you can inspect all the http data going through your phone. You have to install this in your mac/ubuntu/windows machine, and then create a proxy server with help of this tool and then modify your wifi setting in iphone/android phone to use your pc as proxy , and then it will capture all the input/output traffic.

Read full instruction here - http://www.engadget.com/2011/02/21/how-to-inspect-ioss-http-traffic-without-spending-a-dime/

vivex
  • 2,496
  • 1
  • 25
  • 30