2013-11-14 2 views
16

Ich habe 5 Tage lang versucht zu lernen und Netzwerkmonitor auf dem iPhone zu implementieren. Ich schaute in netstat Code von Apple, und ich verlor wie 25% meiner Haare.Network Activity Monitoring auf dem iPhone

Ich habe Links für JB-Geräte gefunden, aber ich brauche es auf einem Nicht-JB-Gerät auszuführen. (Unabhängig davon, ob Apple es im App Store akzeptiert oder nicht).

fand ich einige nützliche Links:

how to get tcp udp opening port list on iPhone (ich die Daten in dieser Frage zurück nicht analysieren konnte :()

Data Usage on iPhone

sysctlbyname buf return type (Ich bin kein Netzwerk-Typ .. kann dies nicht verstehen, können euch sein können helfen :))

TCP/UPD port list

Ich kann sagen, ich habe etwas vom ersten Link. Können Sie mir helfen, die Daten zu analysieren? Gibt es eine andere Methode, um dies zu erreichen?

+0

so im Grunde möchten Sie sehen, was passiert in Ihrem Netzwerk über Ihr iPhone. – Antp

+0

Willst du die Antwort des Beitrags analysieren, wie tcp udp öffnende Portliste auf iPhone erhalten? http://stackoverflow.com/questions/8176839/how-to-get-tcp-udp-opening-port-list-on-iphone-by-objective-c?rq=1 Und egal, ob es nicht akzeptiert durch Apfel? – dcorbatta

+0

@dcorbatta - Ja, ich kann damit leben ... ich werde es nicht auf appstore hochladen. – Hisenberg

Antwort

10

Ok, Sie haben alles, was Sie brauchen, über den Tisch.

Sie können die Frage Is there any private api to monitor network traffic on iPhone? Und hier finden Sie die Quelle code of inet. Dieser Code haben alle die Daten zurück von 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 analysieren müssen, ja der Quellcode von inet ist ein wenig kompliziert. Aber Sie wissen, dass Netstat den Netto-Status ausgibt. Sie müssen also nur sehen, wenn inet printf ausgeführt wird. An diesem Punkt werden die Daten analysiert.

Wenn Sie versuchen, die Quelle von inet für iOS zu kompilieren, werden Sie eine Menge Probleme finden: einige Header-Dateien sind nicht in ios sdk vorhanden. Ok, kein Problem Kopiere die Header.

Für den Simulator müssen Sie nur die Kopfzeile von netstat.h kopieren. Und einige Strukturdeklaration hinzufügen, die privat sind:

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]; 
}; 

Aber für das Gerät müssen Sie einige andere Header des Kernels kopieren, ich weiß nicht, warum die Header nicht in dem Gerät sdk sind (vielleicht, wenn Sie benutze sie Apple wird deine app nicht genehmigen, ich weiß es nicht, aber das ist egal).

Sie können die fehlenden Header aus dem Simulator-SDK in Ihr Projekt kopieren. Oder Sie können den Header-Pfad zum Simulator-SDK ändern. (Ich kopierte die Header).
Wenn Sie die Header kopieren, müssen Sie einige Include-Deklarationen ändern.

Sie benötigen nicht den gesamten inet.c-Code. Sie müssen nur diese Funktionen:

  • protopr
  • inetprint
  • inetname

Dann innerhalb dieser Funktionen finden Sie die printf mit den Daten analysiert sehen Sie, dass in einem Wörterbuch hinzufügen.

Here ist mein Code. Sie können eine Klasse namens: DHInet mit zwei Methoden sehen, die ein NSArray mit einer Liste von NSDictionary mit den Informationen der Verbindungen zurückgeben.

Ich hoffe, Sie finden es nützlich, und wenn jemand mich bei der Verbesserung des Codes helfen möchte, so sei es, ich muss den Code reinigen, weil es eine Menge von Ifdef hat, die nicht notwendig sind.

+0

Buddy Ihre Probe sieht toll aus ... Das einzige, was ich hier vermisse, ist Foreign Address ist nicht vollständig .. Kann ich die vollständige URL bekommen? – Hisenberg

+0

Mmm, zeigt der Printf die vollständige URL an? – dcorbatta

+0

no ... m geting so etwas wie .. 'Schlüssel: Foreign Address, Wert: maa03s17-in-f31..https' – Hisenberg