16

I've been working for 5 days trying to learn and implement Network monitor on the iPhone. I looked into netstat code from apple, and i lost like 25% of my hair.

I found links for JB Devices but i need it to execute on a non JB device. (Irrespective of whether Apple accepts it on the App store or not).

I found some useful links :

how to get tcp udp opening port list on iPhone (I couldn't parse the data returned in this question :( )

Data Usage on iPhone

sysctlbyname buf return type (I'm not a networking guy..could not understand this one, may be you guys can help :) )

TCP/UPD port list

I can say i got some thing from the first link. Can you guys help me to Parse the data ? Is there any other method to achieve this??

Community
  • 1
  • 1
Shrey
  • 1,959
  • 2
  • 21
  • 44
  • so basically you want to see whats happening on your network through your iPhone. – Antp Nov 20 '13 at 20:58
  • Do you want parse the response of the post how to get tcp udp opening port list on iPhone ? http://stackoverflow.com/questions/8176839/how-to-get-tcp-udp-opening-port-list-on-iphone-by-objective-c?rq=1 And no matter if it does not accept by apple? – dcorbatta Nov 20 '13 at 21:37
  • @dcorbatta - Yes, I can live with that..coz i won't be uploading it on appstore. – Shrey Nov 21 '13 at 05:02
  • @Hisenberg, Did you see my example project? What more detail do you need? I'm giving you a dictionary with the conections. – dcorbatta Nov 21 '13 at 05:04
  • @dcorbatta - I'm going through it... :) – Shrey Nov 21 '13 at 05:12
  • Great, if you need more help let me know. – dcorbatta Nov 21 '13 at 05:14

1 Answers1

11

Ok, you have all that you need over the table.

You can check the question Is there any private api to monitor network traffic on iPhone? And here you can find the source code of inet. That code have all you need to parse the data returned of how to get tcp udp opening port list on iPhone

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

Ok, yes the source code of inet is a little complicate. But, you know that netstat print the net status. So, you only need to see when inet execute printf, at this point you will have the data parsed.

If you try to compile the source of inet for iOS, you will find a lot of problems: some header files are not present in ios sdk. Ok, no problem copy the headers.

For the simulator you only need copy the header of netstat.h. And add some struct declaration that are private:

struct  xtcpcb_n {
    u_int32_t                      xt_len;
    u_int32_t                        xt_kind;                /* XSO_TCPCB */

    u_int64_t t_segq;
    int     t_dupacks;              /* consecutive dup acks recd */

    int t_timer[TCPT_NTIMERS_EXT];  /* tcp timers */

    int     t_state;                /* state of this connection */
    u_int   t_flags;

    int     t_force;                /* 1 if forcing out a byte */

    tcp_seq snd_una;                /* send unacknowledged */
    tcp_seq snd_max;                /* highest sequence number sent;
                                     * used to recognize retransmits
                                     */
    tcp_seq snd_nxt;                /* send next */
    tcp_seq snd_up;                 /* send urgent pointer */

    tcp_seq snd_wl1;                /* window update seg seq number */
    tcp_seq snd_wl2;                /* window update seg ack number */
    tcp_seq iss;                    /* initial send sequence number */
    tcp_seq irs;                    /* initial receive sequence number */

    tcp_seq rcv_nxt;                /* receive next */
    tcp_seq rcv_adv;                /* advertised window */
    u_int32_t rcv_wnd;              /* receive window */
    tcp_seq rcv_up;                 /* receive urgent pointer */

    u_int32_t snd_wnd;              /* send window */
    u_int32_t snd_cwnd;             /* congestion-controlled window */
    u_int32_t snd_ssthresh;         /* snd_cwnd size threshold for
                                     * for slow start exponential to
                                     * linear switch
                                     */
    u_int   t_maxopd;               /* mss plus options */

    u_int32_t t_rcvtime;            /* time at which a packet was received */
    u_int32_t t_starttime;          /* time connection was established */
    int     t_rtttime;              /* round trip time */
    tcp_seq t_rtseq;                /* sequence number being timed */

    int     t_rxtcur;               /* current retransmit value (ticks) */
    u_int   t_maxseg;               /* maximum segment size */
    int     t_srtt;                 /* smoothed round-trip time */
    int     t_rttvar;               /* variance in round-trip time */

    int     t_rxtshift;             /* log(2) of rexmt exp. backoff */
    u_int   t_rttmin;               /* minimum rtt allowed */
    u_int32_t t_rttupdated;         /* number of times rtt sampled */
    u_int32_t max_sndwnd;           /* largest window peer has offered */

    int     t_softerror;            /* possible error not yet reported */
    /* out-of-band data */
    char    t_oobflags;             /* have some */
    char    t_iobc;                 /* input character */
    /* RFC 1323 variables */
    u_char  snd_scale;              /* window scaling for send window */
    u_char  rcv_scale;              /* window scaling for recv window */
    u_char  request_r_scale;        /* pending window scaling */
    u_char  requested_s_scale;
    u_int32_t ts_recent;            /* timestamp echo data */

    u_int32_t ts_recent_age;        /* when last updated */
    tcp_seq last_ack_sent;
    /* RFC 1644 variables */
    tcp_cc  cc_send;                /* send connection count */
    tcp_cc  cc_recv;                /* receive connection count */
    tcp_seq snd_recover;            /* for use in fast recovery */
    /* experimental */
    u_int32_t snd_cwnd_prev;        /* cwnd prior to retransmit */
    u_int32_t snd_ssthresh_prev;    /* ssthresh prior to retransmit */
    u_int32_t t_badrxtwin;          /* window for retransmit recovery */
};


struct  xinpcb_n {
    u_int32_t               xi_len;         /* length of this structure */
    u_int32_t               xi_kind;                /* XSO_INPCB */
    u_int64_t               xi_inpp;
    u_short                 inp_fport;      /* foreign port */
    u_short                 inp_lport;      /* local port */
    u_int64_t               inp_ppcb;       /* pointer to per-protocol pcb */
    inp_gen_t               inp_gencnt;     /* generation count of this instance */
    int                             inp_flags;      /* generic IP/datagram flags */
    u_int32_t               inp_flow;
    u_char                  inp_vflag;
    u_char                  inp_ip_ttl;     /* time to live */
    u_char                  inp_ip_p;       /* protocol */
    union {                                 /* foreign host table entry */
        struct  in_addr_4in6    inp46_foreign;
        struct  in6_addr        inp6_foreign;
    }                               inp_dependfaddr;
    union {                                 /* local host table entry */
        struct  in_addr_4in6    inp46_local;
        struct  in6_addr        inp6_local;
    }                               inp_dependladdr;
    struct {
        u_char          inp4_ip_tos;    /* type of service */
    }                               inp_depend4;
    struct {
        u_int8_t        inp6_hlim;
        int                     inp6_cksum;
        u_short         inp6_ifindex;
        short           inp6_hops;
    }                               inp_depend6;
    u_int32_t               inp_flowhash;
};


#define SO_TC_STATS_MAX 4

struct data_stats {
    u_int64_t       rxpackets;
    u_int64_t       rxbytes;
    u_int64_t       txpackets;
    u_int64_t       txbytes;
};

struct xgen_n {
    u_int32_t   xgn_len;            /* length of this structure */
    u_int32_t   xgn_kind;       /* number of PCBs at this time */
};

#define XSO_SOCKET  0x001
#define XSO_RCVBUF  0x002
#define XSO_SNDBUF  0x004
#define XSO_STATS   0x008
#define XSO_INPCB   0x010
#define XSO_TCPCB   0x020

struct  xsocket_n {
    u_int32_t       xso_len;        /* length of this structure */
    u_int32_t       xso_kind;       /* XSO_SOCKET */
    u_int64_t       xso_so; /* makes a convenient handle */
    short           so_type;
    u_int32_t       so_options;
    short           so_linger;
    short           so_state;
    u_int64_t       so_pcb;     /* another convenient handle */
    int             xso_protocol;
    int             xso_family;
    short           so_qlen;
    short           so_incqlen;
    short           so_qlimit;
    short           so_timeo;
    u_short         so_error;
    pid_t           so_pgid;
    u_int32_t       so_oobmark;
    uid_t           so_uid;     /* XXX */
};

struct xsockbuf_n {
    u_int32_t       xsb_len;        /* length of this structure */
    u_int32_t       xsb_kind;       /* XSO_RCVBUF or XSO_SNDBUF */
    u_int32_t       sb_cc;
    u_int32_t       sb_hiwat;
    u_int32_t       sb_mbcnt;
    u_int32_t       sb_mbmax;
    int32_t         sb_lowat;
    short           sb_flags;
    short           sb_timeo;
};

struct xsockstat_n {
    u_int32_t       xst_len;        /* length of this structure */
    u_int32_t       xst_kind;       /* XSO_STATS */
    struct data_stats   xst_tc_stats[SO_TC_STATS_MAX];
};

But for the Device you need to copy some other headers of the kernel, I don't know why the headers aren't in the device sdk (maybe if you use them Apple will not approve your app, I don't know, but that doesn't matter).

You can copy the missing headers from the simulator SDK to your project. Or you can change the header path to the simulator SDK. (I copied the headers).
If you copy the headers you will need change some include declaration.

You don't need all the inet.c code. You only need these functions:

  • protopr
  • inetprint
  • inetname

Then inside those functions you will see the printf with the data parsed, you can add that in a dictionary.

Here is my code. You can see a class named: DHInet with two method that return a NSArray with a list of NSDictionary with the information of the connections.

I hope you find it useful, and if anyone wants to give me a hand in improving the code, so be it, I need to clean the code, because it has a lot of ifdef that are not necessary.

Community
  • 1
  • 1
dcorbatta
  • 1,893
  • 19
  • 15
  • Buddy your sample looks amazing...The only thing i'm missing here is Foreign Address is not complete.. Can i get the complete URL ?? – Shrey Nov 21 '13 at 05:22
  • no...m geting something like.. 'key: Foreign Address, value: maa03s17-in-f31..https ' – Shrey Nov 21 '13 at 05:27
  • Try changing the Wflag's value by 1, it is in DHInet.m there some flags that you can change – dcorbatta Nov 21 '13 at 05:27
  • i changed it to '1'.. still the same result – Shrey Nov 21 '13 at 05:29
  • I changed nflag to 1..and now i can get the foreign IP... :) – Shrey Nov 21 '13 at 05:32
  • I only change Wflag, and I can see the whole URL. – dcorbatta Nov 21 '13 at 05:36
  • Nope just the nflag.. Can we get the process id also..I know its not the part of the question, bt still can you point me in the direction how to get it – Shrey Nov 21 '13 at 05:36
  • Mmm... I don't know. Netstat give that information??? Now I need to sleep. But tomorrow I can see a little more. – dcorbatta Nov 21 '13 at 05:41
  • @Hisenberg, I was looking in inet and netstat. But I didn't find any of PID. I know that in MacOS you can get this info with: lsof -i -P, maybe you need start researching how this command work and what system call it does. – dcorbatta Nov 22 '13 at 02:00
  • 1
    @dcorbatta Do you have any idea how to get interface name with netstat per connection? – Legoless Jan 30 '14 at 12:26
  • Really? I'm going to check. Thanks. – dcorbatta Jan 16 '15 at 20:14
  • I was checking, the problem isn't iOS8, the problem is the architecture (arm64 and i86_64). I think that the problem is with the size of the struct xinpgen. – dcorbatta Jan 19 '15 at 03:37
  • anyone have any luck getting this to work on iOS8 or 9 (or arm64)? – Locksleyu Feb 19 '16 at 15:34
  • @Locksleyu, Sorry for the delay. :P You need to take a look into this version of inet.c http://www.opensource.apple.com/source/network_cmds/network_cmds-328/netstat.tproj/inet.c I have to adapt my code to add this modification for 64bit. – dcorbatta Feb 20 '16 at 18:31
  • @dcorbatta first many thanks for your small app. it worked really well. The only issue is that I'm only seeing tcp connections inside the app. That was what I was fearing. I have a security app on android that I'd like to port but missing the system view is a pain. Is it the normal behavior? Is there any possibility to request the permission to have a device/system view? – narb Jan 16 '18 at 20:22