netstat.c revision dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0
1/*
2 * Copyright (c) 2008, The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *  * Neither the name of Google, Inc. nor the names of its contributors
15 *    may be used to endorse or promote products derived from this
16 *    software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <stdio.h>
33#include <stdlib.h>
34
35typedef union iaddr iaddr;
36
37union iaddr {
38    unsigned u;
39    unsigned char b[4];
40};
41
42static const char *state2str(unsigned state)
43{
44    switch(state){
45    case 0x1: return "ESTABLISHED";
46    case 0x2: return "SYN_SENT";
47    case 0x3: return "SYN_RECV";
48    case 0x4: return "FIN_WAIT1";
49    case 0x5: return "FIN_WAIT2";
50    case 0x6: return "TIME_WAIT";
51    case 0x7: return "CLOSE";
52    case 0x8: return "CLOSE_WAIT";
53    case 0x9: return "LAST_ACK";
54    case 0xA: return "LISTEN";
55    case 0xB: return "CLOSING";
56    default: return "UNKNOWN";
57    }
58}
59
60void addr2str(iaddr addr, unsigned port, char *buf)
61{
62    if(port) {
63        snprintf(buf, 64, "%d.%d.%d.%d:%d",
64                 addr.b[0], addr.b[1], addr.b[2], addr.b[3], port);
65    } else {
66        snprintf(buf, 64, "%d.%d.%d.%d:*",
67                 addr.b[0], addr.b[1], addr.b[2], addr.b[3]);
68    }
69}
70
71int netstat_main(int argc, char *argv[])
72{
73    char buf[512];
74    char lip[64];
75    char rip[64];
76    iaddr laddr, raddr;
77    unsigned lport, rport, state, txq, rxq, num;
78    int n;
79    FILE *fp;
80
81    printf("Proto Recv-Q Send-Q Local Address          Foreign Address        State\n");
82
83    fp = fopen("/proc/net/tcp", "r");
84    if(fp != 0) {
85        fgets(buf, 512, fp);
86        while(fgets(buf, 512, fp)){
87            n = sscanf(buf, " %d: %x:%x %x:%x %x %x:%x",
88                       &num, &laddr.u, &lport, &raddr.u, &rport,
89                       &state, &txq, &rxq);
90            if(n == 8) {
91                addr2str(laddr, lport, lip);
92                addr2str(raddr, rport, rip);
93
94                printf("tcp   %6d %6d %-22s %-22s %s\n",
95                       txq, rxq, lip, rip,
96                       state2str(state));
97            }
98        }
99        fclose(fp);
100    }
101    fp = fopen("/proc/net/udp", "r");
102    if(fp != 0) {
103        fgets(buf, 512, fp);
104        while(fgets(buf, 512, fp)){
105            n = sscanf(buf, " %d: %x:%x %x:%x %x %x:%x",
106                       &num, &laddr.u, &lport, &raddr.u, &rport,
107                       &state, &txq, &rxq);
108            if(n == 8) {
109                addr2str(laddr, lport, lip);
110                addr2str(raddr, rport, rip);
111
112                printf("udp   %6d %6d %-22s %-22s\n",
113                       txq, rxq, lip, rip);
114            }
115        }
116        fclose(fp);
117    }
118
119    return 0;
120}
121