aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/netstat.c
blob: a4096650ce490aea0e391eb52dc6283f1cc34204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/* netstat.c - Display Linux networking subsystem.
 *
 * Copyright 2012 Ranjan Kumar <ranjankumar.bth@gmail.com>
 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
 *
 * Not in SUSv4.
 *
USE_NETSTAT(NEWTOY(netstat, "pWrxwutneal[-al]", TOYFLAG_BIN))
config NETSTAT
  bool "netstat"
  default n
  help
    usage: netstat [-pWrxwutneal]

    Display networking information.

    -r  Display routing table.
    -a  Display all sockets (Default: Connected).
    -l  Display listening server sockets.
    -t  Display TCP sockets.
    -u  Display UDP sockets.
    -w  Display Raw sockets.
    -x  Display Unix sockets.
    -e  Display other/more information.
    -n  Don't resolve names.
    -W  Wide Display.
    -p  Display PID/Program name for sockets.
*/

#define FOR_netstat
#include "toys.h"

#include <net/route.h>

GLOBALS(
  char current_name[21];
  int some_process_unidentified;
);

typedef union _iaddr {
  unsigned u;
  unsigned char b[4];
} iaddr;

typedef union _iaddr6 {
  struct {
    unsigned a;
    unsigned b;
    unsigned c;
    unsigned d;
  } u;
  unsigned char b[16];
} iaddr6;

#define ADDR_LEN (INET6_ADDRSTRLEN + 1 + 5 + 1)//IPv6 addr len + : + port + '\0'

#define SOCK_NOT_CONNECTED 1

typedef struct _pidlist {
  struct _pidlist *next;
  long inode;
  char name[21];
} PID_LIST;

PID_LIST *pid_list = NULL;

/*
 * used to convert string into int and
 * validate the input str for invalid int value or out-of-range.
 */
static unsigned long get_strtou(char *str, char **endp, int base)
{
  unsigned long uli;
  char *endptr;

  if (!isalnum(str[0])) {
    errno = ERANGE;
    return UINT_MAX;
  }
  errno = 0;
  uli = strtoul(str, &endptr, base);
  if (uli > UINT_MAX) {
    errno = ERANGE;
    return UINT_MAX;
  }

  if (endp) *endp = endptr;
  if (endptr[0]) {
    if (isalnum(endptr[0]) || errno) { //"123abc" or out-of-range
      errno = ERANGE;
      return UINT_MAX;
    }
    errno = EINVAL;
  }
  return uli;
}

/*
 * used to retrive pid name from pid list.
 */
static const char *get_pid_name(unsigned long inode)
{
  PID_LIST *tmp;

  for (tmp = pid_list; tmp; tmp = tmp->next)
    if (tmp->inode == inode) return tmp->name;

  return "-";
}

/*
 * For TCP/UDP/RAW display data.
 */
static void display_data(unsigned rport, char *label,
                         unsigned rxq, unsigned txq, char *lip, char *rip,
                         unsigned state, unsigned uid, unsigned long inode)
{
  char *ss_state = "UNKNOWN", buf[12];
  char *state_label[] = {"", "ESTABLISHED", "SYN_SENT", "SYN_RECV", "FIN_WAIT1",
                         "FIN_WAIT2", "TIME_WAIT", "CLOSE", "CLOSE_WAIT",
                         "LAST_ACK", "LISTEN", "CLOSING", "UNKNOWN"};
  char user[11];
  struct passwd *pw;

  if (!strcmp(label, "tcp")) {
    int sz = ARRAY_LEN(state_label);
    if (!state || state >= sz) state = sz-1;
    ss_state = state_label[state];
  }
  else if (!strcmp(label, "udp")) {
    if (state == 1) ss_state = state_label[state];
    else if (state == 7) ss_state = "";
  }
  else if (!strcmp(label, "raw")) sprintf(ss_state = buf, "%u", state);

  if (!(toys.optflags & FLAG_n) && (pw = getpwuid(uid))) {
    snprintf(user, sizeof(user), "%s", pw->pw_name);
  } else snprintf(user, sizeof(user), "%d", uid);

  xprintf("%3s   %6d %6d ", label, rxq, txq);
  xprintf((toys.optflags & FLAG_W) ? "%-51.51s %-51.51s " : "%-23.23s %-23.23s "
           , lip, rip);
  xprintf("%-11s", ss_state);
  if ((toys.optflags & FLAG_e)) xprintf(" %-10s %-11ld", user, inode);
  if ((toys.optflags & FLAG_p)) xprintf(" %s", get_pid_name(inode));
  xputc('\n');
}

/*
 * For TCP/UDP/RAW show data.
 */
static void show_data(unsigned rport, char *label, unsigned rxq, unsigned txq,
                      char *lip, char *rip, unsigned state, unsigned uid,
                      unsigned long inode)
{
  if (toys.optflags & FLAG_l) {
    if (!rport && (state & 0xA))
      display_data(rport, label, rxq, txq, lip, rip, state, uid, inode);
  } else if (toys.optflags & FLAG_a)
    display_data(rport, label, rxq, txq, lip, rip, state, uid, inode);
  //rport && (TCP | UDP | RAW)
  else if (rport & (0x10 | 0x20 | 0x40))
    display_data(rport, label, rxq, txq, lip, rip, state, uid, inode);
}

/*
 * used to get service name.
 */
static char *get_servname(int port, char *label)
{
  int lport = htons(port);
  if (!lport) return xmprintf("%s", "*");
  struct servent *ser = getservbyport(lport, label);
  if (ser) return xmprintf("%s", ser->s_name);
  return xmprintf("%u", (unsigned)ntohs(lport));
}

/*
 * used to convert address into text format.
 */
static void addr2str(int af, void *addr, unsigned port, char *buf, char *label)
{
  char ip[ADDR_LEN] = {0,};
  if (!inet_ntop(af, addr, ip, ADDR_LEN)) {
    *buf = '\0';
    return;
  }
  size_t iplen = strlen(ip);
  if (!port) {
    strncat(ip+iplen, ":*", ADDR_LEN-iplen-1);
    memcpy(buf, ip, ADDR_LEN);
    return;
  }

  if (!(toys.optflags & FLAG_n)) {
    struct addrinfo hints, *result, *rp;

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = af;

    if (!getaddrinfo(ip, NULL, &hints, &result)) {
      char hbuf[NI_MAXHOST] = {0,}, sbuf[NI_MAXSERV] = {0,};
      socklen_t sock_len;
      char *sname = NULL;
      int plen = 0;

      if (af == AF_INET) sock_len = sizeof(struct sockaddr_in);
      else sock_len = sizeof(struct sockaddr_in6);

      for (rp = result; rp; rp = rp->ai_next)
        if (!getnameinfo(rp->ai_addr, sock_len, hbuf, sizeof(hbuf), sbuf,
            sizeof(sbuf), NI_NUMERICSERV))
          break;

      freeaddrinfo(result);
      sname = get_servname(port, label);
      plen = strlen(sname);
      if (*hbuf) {
        memset(ip, 0, ADDR_LEN);
        memcpy(ip, hbuf, (ADDR_LEN - plen - 2));
        iplen = strlen(ip);
      }
      snprintf(ip + iplen, ADDR_LEN-iplen, ":%s", sname);
      free(sname);
    }
  }
  else snprintf(ip+iplen, ADDR_LEN-iplen, ":%d", port);
  memcpy(buf, ip, ADDR_LEN);
}

/*
 * display ipv4 info for TCP/UDP/RAW.
 */
static void show_ipv4(char *fname, char *label)
{
  FILE *fp = fopen(fname, "r");
  if (!fp) {
     perror_msg("'%s'", fname);
     return;
  }

  if(!fgets(toybuf, sizeof(toybuf), fp)) return; //skip header.

  while (fgets(toybuf, sizeof(toybuf), fp)) {
    char lip[ADDR_LEN] = {0,}, rip[ADDR_LEN] = {0,};
    iaddr laddr, raddr;
    unsigned lport, rport, state, txq, rxq, num, uid;
    unsigned long inode;

    int nitems = sscanf(toybuf, " %d: %x:%x %x:%x %x %x:%x %*X:%*X %*X %d %*d %ld",
                        &num, &laddr.u, &lport, &raddr.u, &rport, &state, &txq,
                        &rxq, &uid, &inode);
    if (nitems == 10) {
      addr2str(AF_INET, &laddr, lport, lip, label);
      addr2str(AF_INET, &raddr, rport, rip, label);
      show_data(rport, label, rxq, txq, lip, rip, state, uid, inode);
    }
  }//End of While
  fclose(fp);
}

/*
 * display ipv6 info for TCP/UDP/RAW.
 */
static void show_ipv6(char *fname, char *label)
{
  FILE *fp = fopen(fname, "r");
  if (!fp) {
     perror_msg("'%s'", fname);
     return;
  }

  if(!fgets(toybuf, sizeof(toybuf), fp)) return; //skip header.

  while (fgets(toybuf, sizeof(toybuf), fp)) {
    char lip[ADDR_LEN] = {0,}, rip[ADDR_LEN] = {0,};
    iaddr6 laddr6, raddr6;
    unsigned lport, rport, state, txq, rxq, num, uid;
    unsigned long inode;

    int nitems = sscanf(toybuf, " %d: %8x%8x%8x%8x:%x %8x%8x%8x%8x:%x %x %x:%x "
                                "%*X:%*X %*X %d %*d %ld",
                        &num, &laddr6.u.a, &laddr6.u.b, &laddr6.u.c,
                        &laddr6.u.d, &lport, &raddr6.u.a, &raddr6.u.b,
                        &raddr6.u.c, &raddr6.u.d, &rport, &state, &txq, &rxq,
                        &uid, &inode);
    if (nitems == 16) {
      addr2str(AF_INET6, &laddr6, lport, lip, label);
      addr2str(AF_INET6, &raddr6, rport, rip, label);
      show_data(rport, label, rxq, txq, lip, rip, state, uid, inode);
    }
  }//End of While
  fclose(fp);
}

static void show_unix_sockets(void)
{
  char *types[] = {"","STREAM","DGRAM","RAW","RDM","SEQPACKET","DCCP","PACKET"},
       *states[] = {"","LISTENING","CONNECTING","CONNECTED","DISCONNECTING"};
  unsigned long refcount, flags, type, state, inode;
  FILE *fp = xfopen("/proc/net/unix", "r");

  if(!fgets(toybuf, sizeof(toybuf), fp)) return; //skip header.

  while (fgets(toybuf, sizeof(toybuf), fp)) {
    unsigned offset = 0;

    // count = 6 or 7 (first field ignored, sockets don't always have filenames)
    if (6<sscanf(toybuf, "%*p: %lX %*X %lX %lX %lX %lu %n",
      &refcount, &flags, &type, &state, &inode, &offset))
        continue;

    // Linux exports only SO_ACCEPTCON since 2.3.15pre3 in 1999, but let's
    // filter in case they add more someday.
    flags &= 1<<16;

    // Only show unconnected listening sockets with -a
    if (state==1 && flags && !(toys.optflags&FLAG_a)) continue;

    if (type==10) type = 7; // move SOCK_PACKET into line
    if (type>ARRAY_LEN(types)) type = 0;
    if (state>ARRAY_LEN(states)) state = 0;
    sprintf(toybuf, "[ %s]", flags ? "ACC " : "");

    printf("unix  %-6ld %-11s %-10s %-13s %8lu ",
      refcount, toybuf, types[type], states[state], inode);
    if (toys.optflags & FLAG_p) printf("%-20s", get_pid_name(inode));

    if (offset) {
      char *s = toybuf+offset, *ss;

      if ((ss = strrchr(s, '\n'))) *ss = 0;
      xprintf("%s", s);
    }
    xputc('\n');
  }

  fclose(fp);
}

/*
 * extract inode value from the link.
 */
static long ss_inode(char *link)
{
  long inode = -1;
  //"link = socket:[12345]", get "12345" as inode.
  if (!strncmp(link, "socket:[", sizeof("socket:[")-1)) {
    inode = get_strtou(link + sizeof("socket:[")-1, (char**)&link, 0);
    if (*link != ']') inode = -1;
  }
  //"link = [0000]:12345", get "12345" as inode.
  else if (!strncmp(link, "[0000]:", sizeof("[0000]:")-1)) {
    inode = get_strtou(link + sizeof("[0000]:")-1, NULL, 0);
    //if not NULL terminated.
    if (errno) inode = -1;
  }
  return inode;
}

/*
 * add inode and progname in the pid list.
 */
static void add2list(long inode)
{
  PID_LIST *node = pid_list;

  for(; node; node = node->next) {
    if(node->inode == inode)
      return;
  }

  PID_LIST *new = (PID_LIST *)xzalloc(sizeof(PID_LIST));
  new->inode = inode;
  xstrncpy(new->name, TT.current_name, sizeof(new->name));
  new->next = pid_list;
  pid_list = new;
}

static void scan_pid_inodes(char *path)
{
  DIR *dp;
  struct dirent *entry;

  if (!(dp = opendir(path))) {
    if (errno == EACCES) {
      TT.some_process_unidentified = 1;
      return;
    } else perror_exit("%s", path);
  }
  while ((entry = readdir(dp))) {
    char link_name[64], *link;
    long inode;

    if (!isdigit(entry->d_name[0])) continue;
    snprintf(link_name, sizeof(link_name), "%s/%s", path, entry->d_name);
    if ((link = xreadlink(link_name)) && (inode = ss_inode(link))!=-1)
      add2list(inode);
    free(link);
  }
  closedir(dp);
}

static void scan_pid(int pid)
{
  char *line, *p, *fd_dir;

  snprintf(toybuf, sizeof(toybuf), "/proc/%d/cmdline", pid);
  line = xreadfile(toybuf, 0, 0);

  if ((p = strchr(line, ' '))) *p = 0; // "/bin/netstat -ntp" -> "/bin/netstat"
  snprintf(TT.current_name, sizeof(TT.current_name), "%d/%s",
           pid, getbasename(line)); // "584/netstat"
  free(line);

  fd_dir = xmprintf("/proc/%d/fd", pid);
  scan_pid_inodes(fd_dir);
  free(fd_dir);
}

static int scan_pids(struct dirtree *node)
{
  int pid;

  if (!node->parent) return DIRTREE_RECURSE;
  if ((pid = atol(node->name))) scan_pid(pid);

  return 0;
}

/*
 * Dealloc pid list.
 */
static void clean_pid_list(void)
{
  PID_LIST *tmp;
  while (pid_list) {
    tmp = pid_list->next;
    free(pid_list);
    pid_list = tmp;
  }
}

/*
 * extract inet4 route info from /proc/net/route file and display it.
 */
static void display_routes(void)
{
  static const char flagchars[] = "GHRDMDAC";
  static const unsigned flagarray[] = {
    RTF_GATEWAY, RTF_HOST, RTF_REINSTATE, RTF_DYNAMIC, RTF_MODIFIED
  };
  unsigned long dest, gate, mask;
  int flags, ref, use, metric, mss, win, irtt;
  char *out = toybuf, *flag_val;
  char iface[64]={0};
  FILE *fp = xfopen("/proc/net/route", "r");

  if(!fgets(toybuf, sizeof(toybuf), fp)) return; //skip header.

  xprintf("Kernel IP routing table\n"
          "Destination     Gateway         Genmask         Flags %s Iface\n",
          !(toys.optflags&FLAG_e) ? "  MSS Window  irtt" : "Metric Ref    Use");

  while (fgets(toybuf, sizeof(toybuf), fp)) {
     char *destip = 0, *gateip = 0, *maskip = 0;

     if (11 != sscanf(toybuf, "%63s%lx%lx%X%d%d%d%lx%d%d%d", iface, &dest,
       &gate, &flags, &ref, &use, &metric, &mask, &mss, &win, &irtt))
         break;

    // skip down interfaces.
    if (!(flags & RTF_UP)) continue;

// TODO /proc/net/ipv6_route

    if (dest) {
      if (inet_ntop(AF_INET, &dest, out, 16)) destip = out;
    } else destip = (toys.optflags&FLAG_n) ? "0.0.0.0" : "default";
    out += 16;

    if (gate) {
      if (inet_ntop(AF_INET, &gate, out, 16)) gateip = out;
    } else gateip = (toys.optflags&FLAG_n) ? "0.0.0.0" : "*";
    out += 16;

// TODO /24
    //For Mask
    if (inet_ntop(AF_INET, &mask, out, 16)) maskip = out;
    else maskip = "?";
    out += 16;

    //Get flag Values
    flag_val = out;
    *out++ = 'U';
    for (dest = 0; dest < ARRAY_LEN(flagarray); dest++)
      if (flags&flagarray[dest]) *out++ = flagchars[dest];
    *out = 0;
    if (flags & RTF_REJECT) *flag_val = '!';

    xprintf("%-15.15s %-15.15s %-16s%-6s", destip, gateip, maskip, flag_val);
    if (!(toys.optflags & FLAG_e))
      xprintf("%5d %-5d %6d %s\n", mss, win, irtt, iface);
    else xprintf("%-6d %-2d %7d %s\n", metric, ref, use, iface);
  }

  fclose(fp);
}

/*
 * netstat utility main function.
 */
void netstat_main(void)
{
  char *type = "w/o";

  if (toys.optflags & FLAG_r) {
    display_routes();

    return;
  }

  if (!(toys.optflags&(FLAG_t|FLAG_u|FLAG_w|FLAG_x)))
    toys.optflags |= FLAG_t|FLAG_u|FLAG_w|FLAG_x;

  if (toys.optflags & FLAG_a) type = "established and";
  else if (toys.optflags & FLAG_l) type = "only";

  if (toys.optflags & FLAG_p) {
    dirtree_read("/proc", scan_pids);
    // TODO: we probably shouldn't warn if all the processes we're going to
    // list were identified.
    if (TT.some_process_unidentified)
      fprintf(stderr,
        "(Not all processes could be identified, non-owned process info\n"
        " will not be shown, you would have to be root to see it all.)\n");
  }

  if (toys.optflags&(FLAG_t|FLAG_u|FLAG_w)) {
    int pad = (toys.optflags&FLAG_W) ? -51 : -23;

    printf("Active %s (%s servers)\n", "Internet connections", type);

    printf("Proto Recv-Q Send-Q %*s %*s State      ", pad, "Local Addres",
      pad, "Foreign Address");
    if (toys.optflags & FLAG_e) printf(" User       Inode      ");
    if (toys.optflags & FLAG_p) printf(" PID/Program Name");
    xputc('\n');

    if (toys.optflags & FLAG_t) {//For TCP
      show_ipv4("/proc/net/tcp",  "tcp");
      show_ipv6("/proc/net/tcp6", "tcp");
    }
    if (toys.optflags & FLAG_u) {//For UDP
      show_ipv4("/proc/net/udp",  "udp");
      show_ipv6("/proc/net/udp6", "udp");
    }
    if (toys.optflags & FLAG_w) {//For raw
      show_ipv4("/proc/net/raw",  "raw");
      show_ipv6("/proc/net/raw6", "raw");
    }
  }

  if (toys.optflags & FLAG_x) {
    xprintf("Active %s (%s servers)\n", "UNIX domain sockets", type);

    xprintf("Proto RefCnt Flags       Type       State           I-Node %s Path\n",
      (toys.optflags&FLAG_p) ? "PID/Program Name" : "I-Node");
    show_unix_sockets();
  }

  if (toys.optflags & FLAG_p) clean_pid_list();
  toys.exitval = 0;
}