forked from ecki/net-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netstat.c
2491 lines (2229 loc) · 64 KB
/
netstat.c
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* netstat This file contains an implementation of the command
* that helps in debugging the networking modules.
*
* NET-TOOLS A collection of programs that form the base set of the
* NET-3 Networking Distribution for the LINUX operating
* system.
*
* Version: $Id: netstat.c,v 1.73 2011-04-20 01:35:22 ecki Exp $
*
* Authors: Fred Baumgarten, <[email protected]>
* Fred N. van Kempen, <[email protected]>
* Phil Packer, <[email protected]>
* Johannes Stille, <[email protected]>
* Bernd Eckenfels, <[email protected]>
* Phil Blundell <[email protected]>
* Tuan Hoang <[email protected]>
*
* Tuned for NET3 by:
* Alan Cox, <[email protected]>
* Copyright (c) 1993 Fred Baumgarten
*
* Modified:
*
*960116 {1.01} Bernd Eckenfels: verbose, cleanups
*960204 {1.10} Bernd Eckenfels: aftrans, usage, new route_info,
* DLFT_AF
*960204 {1.11} Bernd Eckenfels: netlink support
*960204 {1.12} Bernd Eckenfels: route_init()
*960215 {1.13} Bernd Eckenfels: netlink_print honors HAVE_
*960217 {1.14} Bernd Eckenfels: masq_info from Jos Vos and
* ax25_info from Jonathan Naylor.
*960218 {1.15} Bernd Eckenfels: ipx_info rewritten, -e for tcp/ipx
*960220 {1.16} Bernd Eckenfels: minor output reformats, -a for -x
*960221 {1.17} Bernd Eckenfels: route_init->getroute_init
*960426 {1.18} Bernd Eckenfels: new RTACTION, SYM/NUM, FIB/CACHE
*960517 {1.19} Bernd Eckenfels: usage() spelling fix and --unix inode,
* ':' is part of sock_addr for --inet
*960822 {x.xx} Frank Strauss: INET6 support
*
*970406 {1.33} Philip Copeland Added snmp reporting support module -s
* code provided by Andi Kleen
* (relly needs to be kernel hooked but
* this will do in the meantime)
* minor header file misplacement tidy up.
*980815 {1.xx} Stephane Fillod: X.25 support
*980411 {1.34} Arnaldo Carvalho i18n: catgets -> gnu gettext, substitution
* of sprintf for snprintf
*10/1998 Andi Kleen Use new interface primitives.
*990101 {1.36} Bernd Eckenfels usage updated to include -s and -C -F,
* fixed netstat -rC output (lib/inet_gr.c)
* removed broken NETLINK Support
* fixed format for /proc/net/udp|tcp|raw
* added -w,-t,-u TcpExt support to -s
*990131 {1.37} Jan Kratochvil added -p for prg_cache() & friends
* Flames to <[email protected]>.
* Tuan Hoang added IGMP support for IPv4 and IPv6
*
*990420 {1.38} Tuan Hoang removed a useless assignment from igmp_do_one()
*20010404 {1.39} Arnaldo Carvalho de Melo - use setlocale
*20081201 {1.42} Brian Micek added -L|--udplite options for RFC 3828
*20020722 {1.51} Thomas Preusser added SCTP over IPv4 support
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <netdb.h>
#include <paths.h>
#include <pwd.h>
#include <getopt.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <net/if.h>
#include <dirent.h>
#include "net-support.h"
#include "pathnames.h"
#include "version.h"
#include "config.h"
#include "intl.h"
#include "sockets.h"
#include "interface.h"
#include "util.h"
#include "proc.h"
#if HAVE_SELINUX
#include <selinux/selinux.h>
#endif
#if HAVE_AFBLUETOOTH
#include <bluetooth/bluetooth.h>
#endif
#define PROGNAME_WIDTH 20
#define SELINUX_WIDTH 50
#if !defined(s6_addr32) && defined(in6a_words)
#define s6_addr32 in6a_words /* libinet6 */
#endif
/* prototypes for statistics.c */
void parsesnmp(int, int, int, int);
void parsesnmp6(int, int, int);
typedef enum {
SS_FREE = 0, /* not allocated */
SS_UNCONNECTED, /* unconnected to any socket */
SS_CONNECTING, /* in process of connecting */
SS_CONNECTED, /* connected to socket */
SS_DISCONNECTING /* in process of disconnecting */
} socket_state;
#define SO_ACCEPTCON (1<<16) /* performed a listen */
#define SO_WAITDATA (1<<17) /* wait data to read */
#define SO_NOSPACE (1<<18) /* no space to write */
#define DFLT_AF "inet"
#define FEATURE_NETSTAT
#include "lib/net-features.h"
static char *Release = RELEASE, *Signature = "Fred Baumgarten, Alan Cox, Bernd Eckenfels, Phil Blundell, Tuan Hoang, Brian Micek and others";
#define E_READ -1
#define E_IOCTL -3
int flag_int = 0;
int flag_rou = 0;
int flag_mas = 0;
int flag_sta = 0;
int flag_all = 0;
int flag_lst = 0;
int flag_cnt = 0;
int flag_deb = 0;
int flag_not = 0;
int flag_cf = 0;
int flag_opt = 0;
int flag_raw = 0;
int flag_tcp = 0;
int flag_sctp= 0;
int flag_udp = 0;
int flag_udplite = 0;
int flag_igmp= 0;
int flag_rom = 0;
int flag_exp = 1;
int flag_wide= 0;
int flag_prg = 0;
int flag_arg = 0;
int flag_noprot = 0;
int flag_ver = 0;
int flag_l2cap = 0;
int flag_rfcomm = 0;
int flag_selinux = 0;
FILE *procinfo;
#define INFO_GUTS1(file,name,proc,prot) \
procinfo = proc_fopen((file)); \
if (procinfo == NULL) { \
if (errno != ENOENT && errno != EACCES) { \
perror((file)); \
return -1; \
} \
if (!flag_noprot && (flag_arg || flag_ver)) \
ESYSNOT("netstat", (name)); \
if (!flag_noprot && flag_arg) \
rc = 1; \
} else { \
do { \
if (fgets(buffer, sizeof(buffer), procinfo)) \
(proc)(lnr++, buffer,prot); \
} while (!feof(procinfo)); \
fclose(procinfo); \
}
#if HAVE_AFINET6
#define INFO_GUTS2(file,proc,prot) \
lnr = 0; \
procinfo = proc_fopen((file)); \
if (procinfo != NULL) { \
do { \
if (fgets(buffer, sizeof(buffer), procinfo)) \
(proc)(lnr++, buffer,prot); \
} while (!feof(procinfo)); \
fclose(procinfo); \
}
#else
#define INFO_GUTS2(file,proc,prot)
#endif
#define INFO_GUTS3 \
return rc;
#define INFO_GUTS6(file,file6,name,proc,prot4,prot6) \
char buffer[8192]; \
int rc = 0; \
int lnr = 0; \
if (!flag_arg || flag_inet) { \
INFO_GUTS1(file,name,proc,prot4) \
} \
if (!flag_arg || flag_inet6) { \
INFO_GUTS2(file6,proc,prot6) \
} \
INFO_GUTS3
#define INFO_GUTS(file,name,proc,prot) \
char buffer[8192]; \
int rc = 0; \
int lnr = 0; \
INFO_GUTS1(file,name,proc,prot) \
INFO_GUTS3
#define PROGNAME_WIDTHs PROGNAME_WIDTH1(PROGNAME_WIDTH)
#define PROGNAME_WIDTH1(s) PROGNAME_WIDTH2(s)
#define PROGNAME_WIDTH2(s) #s
#define SELINUX_WIDTHs SELINUX_WIDTH1(SELINUX_WIDTH)
#define SELINUX_WIDTH1(s) SELINUX_WIDTH2(s)
#define SELINUX_WIDTH2(s) #s
#define PRG_HASH_SIZE 211
static struct prg_node {
struct prg_node *next;
unsigned long inode;
char name[PROGNAME_WIDTH];
char scon[SELINUX_WIDTH];
} *prg_hash[PRG_HASH_SIZE];
static char prg_cache_loaded = 0;
#define PRG_HASHIT(x) ((x) % PRG_HASH_SIZE)
#define PROGNAME_BANNER "PID/Program name"
#define SELINUX_BANNER "Security Context"
#define print_progname_banner() do { if (flag_prg) printf(" %-" PROGNAME_WIDTHs "s",PROGNAME_BANNER); } while (0)
#define print_selinux_banner() do { if (flag_selinux) printf("%-" SELINUX_WIDTHs "s"," " SELINUX_BANNER); } while (0)
#define PRG_LOCAL_ADDRESS "local_address"
#define PRG_INODE "inode"
#define PRG_SOCKET_PFX "socket:["
#define PRG_SOCKET_PFXl (strlen(PRG_SOCKET_PFX))
#define PRG_SOCKET_PFX2 "[0000]:"
#define PRG_SOCKET_PFX2l (strlen(PRG_SOCKET_PFX2))
#ifndef LINE_MAX
#define LINE_MAX 4096
#endif
#define PATH_PROC "/proc"
#define PATH_FD_SUFF "fd"
#define PATH_FD_SUFFl strlen(PATH_FD_SUFF)
#define PATH_PROC_X_FD PATH_PROC "/%s/" PATH_FD_SUFF
#define PATH_CMDLINE "cmdline"
#define PATH_CMDLINEl strlen(PATH_CMDLINE)
static void prg_cache_add(unsigned long inode, char *name, const char *scon)
{
unsigned hi = PRG_HASHIT(inode);
struct prg_node **pnp,*pn;
prg_cache_loaded = 2;
for (pnp = prg_hash + hi; (pn = *pnp); pnp = &pn->next) {
if (pn->inode == inode) {
/* Some warning should be appropriate here
as we got multiple processes for one i-node */
return;
}
}
if (!(*pnp = malloc(sizeof(**pnp))))
return;
pn = *pnp;
pn->next = NULL;
pn->inode = inode;
safe_strncpy(pn->name, name, sizeof(pn->name));
{
int len = (strlen(scon) - sizeof(pn->scon)) + 1;
if (len > 0)
safe_strncpy(pn->scon, &scon[len + 1], sizeof(pn->scon));
else
safe_strncpy(pn->scon, scon, sizeof(pn->scon));
}
}
static const char *prg_cache_get(unsigned long inode)
{
unsigned hi = PRG_HASHIT(inode);
struct prg_node *pn;
for (pn = prg_hash[hi]; pn; pn = pn->next)
if (pn->inode == inode)
return (pn->name);
return ("-");
}
static const char *prg_cache_get_con(unsigned long inode)
{
unsigned hi = PRG_HASHIT(inode);
struct prg_node *pn;
for (pn = prg_hash[hi]; pn; pn = pn->next)
if (pn->inode == inode)
return (pn->scon);
return ("-");
}
static void prg_cache_clear(void)
{
struct prg_node **pnp,*pn;
if (prg_cache_loaded == 2)
for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++)
while ((pn = *pnp)) {
*pnp = pn->next;
free(pn);
}
prg_cache_loaded = 0;
}
static void wait_continous(void)
{
fflush(stdout);
sleep(1);
}
static int extract_type_1_socket_inode(const char lname[], unsigned long * inode_p) {
/* If lname is of the form "socket:[12345]", extract the "12345"
as *inode_p. Otherwise, return -1 as *inode_p.
*/
if (strlen(lname) < PRG_SOCKET_PFXl+3) return(-1);
if (memcmp(lname, PRG_SOCKET_PFX, PRG_SOCKET_PFXl)) return(-1);
if (lname[strlen(lname)-1] != ']') return(-1);
{
char inode_str[strlen(lname + 1)]; /* e.g. "12345" */
const int inode_str_len = strlen(lname) - PRG_SOCKET_PFXl - 1;
char *serr;
strncpy(inode_str, lname+PRG_SOCKET_PFXl, inode_str_len);
inode_str[inode_str_len] = '\0';
*inode_p = strtoul(inode_str, &serr, 0);
if (!serr || *serr || *inode_p == ~0)
return(-1);
}
return(0);
}
static int extract_type_2_socket_inode(const char lname[], unsigned long * inode_p) {
/* If lname is of the form "[0000]:12345", extract the "12345"
as *inode_p. Otherwise, return -1 as *inode_p.
*/
if (strlen(lname) < PRG_SOCKET_PFX2l+1) return(-1);
if (memcmp(lname, PRG_SOCKET_PFX2, PRG_SOCKET_PFX2l)) return(-1);
{
char *serr;
*inode_p = strtoul(lname + PRG_SOCKET_PFX2l, &serr, 0);
if (!serr || *serr || *inode_p == ~0)
return(-1);
}
return(0);
}
static void prg_cache_load(void)
{
char line[LINE_MAX], eacces=0;
int procfdlen, fd, cmdllen, lnamelen;
char lname[30], cmdlbuf[512], finbuf[PROGNAME_WIDTH];
unsigned long inode;
const char *cs, *cmdlp;
DIR *dirproc = NULL, *dirfd = NULL;
struct dirent *direproc, *direfd;
#if HAVE_SELINUX
security_context_t scon = NULL;
#endif
if (prg_cache_loaded || !flag_prg) return;
prg_cache_loaded = 1;
cmdlbuf[sizeof(cmdlbuf) - 1] = '\0';
if (!(dirproc=opendir(PATH_PROC))) goto fail;
while (errno = 0, direproc = readdir(dirproc)) {
for (cs = direproc->d_name; *cs; cs++)
if (!isdigit(*cs))
break;
if (*cs)
continue;
procfdlen = snprintf(line,sizeof(line),PATH_PROC_X_FD,direproc->d_name);
if (procfdlen <= 0 || procfdlen >= sizeof(line) - 5)
continue;
errno = 0;
dirfd = opendir(line);
if (! dirfd) {
if (errno == EACCES)
eacces = 1;
continue;
}
line[procfdlen] = '/';
cmdlp = NULL;
while ((direfd = readdir(dirfd))) {
/* Skip . and .. */
if (!isdigit(direfd->d_name[0]))
continue;
if (procfdlen + 1 + strlen(direfd->d_name) + 1 > sizeof(line))
continue;
memcpy(line + procfdlen - PATH_FD_SUFFl, PATH_FD_SUFF "/",
PATH_FD_SUFFl + 1);
safe_strncpy(line + procfdlen + 1, direfd->d_name,
sizeof(line) - procfdlen - 1);
lnamelen = readlink(line, lname, sizeof(lname) - 1);
if (lnamelen == -1)
continue;
lname[lnamelen] = '\0'; /*make it a null-terminated string*/
if (extract_type_1_socket_inode(lname, &inode) < 0)
if (extract_type_2_socket_inode(lname, &inode) < 0)
continue;
if (!cmdlp) {
if (procfdlen - PATH_FD_SUFFl + PATH_CMDLINEl >=
sizeof(line) - 5)
continue;
safe_strncpy(line + procfdlen - PATH_FD_SUFFl, PATH_CMDLINE,
sizeof(line) - procfdlen + PATH_FD_SUFFl);
fd = open(line, O_RDONLY);
if (fd < 0)
continue;
cmdllen = read(fd, cmdlbuf, sizeof(cmdlbuf) - 1);
if (close(fd))
continue;
if (cmdllen == -1)
continue;
if (cmdllen < sizeof(cmdlbuf) - 1)
cmdlbuf[cmdllen]='\0';
if (cmdlbuf[0] == '/' && (cmdlp = strrchr(cmdlbuf, '/')))
cmdlp++;
else
cmdlp = cmdlbuf;
}
snprintf(finbuf, sizeof(finbuf), "%s/%s", direproc->d_name, cmdlp);
#if HAVE_SELINUX
if (getpidcon(atoi(direproc->d_name), &scon) == -1) {
scon=xstrdup("-");
}
prg_cache_add(inode, finbuf, scon);
freecon(scon);
#else
prg_cache_add(inode, finbuf, "-");
#endif
}
closedir(dirfd);
dirfd = NULL;
}
if (dirproc)
closedir(dirproc);
if (dirfd)
closedir(dirfd);
if (!eacces)
return;
if (prg_cache_loaded == 1) {
fail:
fprintf(stderr,_("(No info could be read for \"-p\": geteuid()=%d but you should be root.)\n"),
geteuid());
}
else
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 HAVE_AFNETROM
static const char *netrom_state[] =
{
N_("LISTENING"),
N_("CONN SENT"),
N_("DISC SENT"),
N_("ESTABLISHED")
};
static int netrom_info(void)
{
FILE *f;
char buffer[256], dev[16];
int st, vs, vr, sendq, recvq, ret;
f = proc_fopen(_PATH_PROCNET_NR);
if (f == NULL) {
if (errno != ENOENT) {
perror(_PATH_PROCNET_NR);
return (-1);
}
if (flag_arg || flag_ver)
ESYSNOT("netstat", "AF NETROM");
if (flag_arg)
return (1);
else
return (0);
}
printf(_("Active NET/ROM sockets\n"));
printf(_("User Dest Source Device State Vr/Vs Send-Q Recv-Q\n"));
if (fgets(buffer, 256, f))
/* eat line */;
while (fgets(buffer, 256, f)) {
buffer[9] = 0;
buffer[19] = 0;
buffer[29] = 0;
ret = sscanf(buffer + 30, "%s %*x/%*x %*x/%*x %d %d %d %*d %*d/%*d %*d/%*d %*d/%*d %*d/%*d %*d/%*d %*d %d %d %*d",
dev, &st, &vs, &vr, &sendq, &recvq);
if (ret != 6) {
printf(_("Problem reading data from %s\n"), _PATH_PROCNET_NR);
continue;
}
printf("%-9s %-9s %-9s %-6s %-11s %03d/%03d %-6d %-6d\n",
buffer, buffer + 10, buffer + 20,
dev,
_(netrom_state[st]),
vr, vs, sendq, recvq);
}
fclose(f);
return 0;
}
#endif
#if HAVE_AFROSE
static const char * const rose_state[] =
{
N_("LISTENING"),
N_("CONN SENT"),
N_("DISC SENT"),
N_("ESTABLISHED"),
};
static int rose_info(void)
{
FILE *f;
char buffer[256], dev[6];
int ret, st, lci, neigh;
char src_addr[10], src_call[9], dest_addr[10], dest_call[9];
f = fopen(_PATH_PROCNET_ROSE, "r");
if (f == NULL) {
if (errno != ENOENT) {
perror(_PATH_PROCNET_ROSE);
return (-1);
}
if (flag_arg || flag_ver)
ESYSNOT("netstat", "AF ROSE");
if (flag_arg)
return (1);
else
return (0);
}
printf(_("Active ROSE sockets\n"));
printf(_("dest_addr dest_call src_addr src_call dev lci neigh state\n"));
if (fgets(buffer, 256, f))
/* eat line */;
while (fgets(buffer, 256, f)) {
ret = sscanf(buffer, "%s %s %s %s %s %d %d %d",
dest_addr, dest_call, src_addr, src_call, dev, &lci, &neigh, &st);
if (ret != 8) {
printf(_("Problem reading data from %s\n"), _PATH_PROCNET_ROSE);
continue;
}
printf("%-10s %-9s %-10s %-9s %-5s %3d %5d %s\n",
dest_addr, dest_call, src_addr, src_call, dev, lci, neigh, _(rose_state[st]));
}
fclose(f);
return 0;
}
#endif
/* These enums are used by IPX too. :-( */
enum {
TCP_ESTABLISHED = 1,
TCP_SYN_SENT,
TCP_SYN_RECV,
TCP_FIN_WAIT1,
TCP_FIN_WAIT2,
TCP_TIME_WAIT,
TCP_CLOSE,
TCP_CLOSE_WAIT,
TCP_LAST_ACK,
TCP_LISTEN,
TCP_CLOSING /* now a valid state */
};
#if HAVE_AFINET || HAVE_AFINET6
static const char *tcp_state[] =
{
"",
N_("ESTABLISHED"),
N_("SYN_SENT"),
N_("SYN_RECV"),
N_("FIN_WAIT1"),
N_("FIN_WAIT2"),
N_("TIME_WAIT"),
N_("CLOSE"),
N_("CLOSE_WAIT"),
N_("LAST_ACK"),
N_("LISTEN"),
N_("CLOSING")
};
static void finish_this_one(int uid, unsigned long inode, const char *timers)
{
struct passwd *pw;
if (flag_exp > 1) {
if (!(flag_not & FLAG_NUM_USER) && ((pw = getpwuid(uid)) != NULL))
printf(" %-10s ", pw->pw_name);
else
printf(" %-10d ", uid);
printf("%-10lu",inode);
}
if (flag_prg)
printf(" %-" PROGNAME_WIDTHs "s",prg_cache_get(inode));
if (flag_selinux)
printf(" %-" SELINUX_WIDTHs "s",prg_cache_get_con(inode));
if (flag_opt)
printf(" %s", timers);
putchar('\n');
}
static void igmp_do_one(int lnr, const char *line,const char *prot)
{
char mcast_addr[128];
struct sockaddr_storage sas;
struct sockaddr_in *sin = (struct sockaddr_in *)&sas;
#if HAVE_AFINET6
char addr6[INET6_ADDRSTRLEN];
struct in6_addr in6;
extern struct aftype inet6_aftype;
#endif
const struct aftype *ap;
static int idx_flag = 0;
static int igmp6_flag = 0;
static char device[16];
int num, idx, refcnt;
char* offset;
if (lnr == 0) {
/* IPV6 ONLY */
/* igmp6 file does not have any comments on first line */
if ( strstr( line, "Device" ) == NULL ) {
igmp6_flag = 1;
} else {
/* IPV4 ONLY */
/* 2.1.x kernels and up have Idx field */
/* 2.0.x and below do not have Idx field */
if ( strncmp( line, "Idx", strlen("Idx") ) == 0 )
idx_flag = 1;
else
idx_flag = 0;
return;
}
}
if (igmp6_flag) { /* IPV6 */
#if HAVE_AFINET6
num = sscanf( line, "%d %15s %64[0-9A-Fa-f] %d", &idx, device, mcast_addr, &refcnt );
if (num == 4) {
/* Demangle what the kernel gives us */
sscanf(mcast_addr, "%08X%08X%08X%08X",
&in6.s6_addr32[0], &in6.s6_addr32[1],
&in6.s6_addr32[2], &in6.s6_addr32[3]);
in6.s6_addr32[0] = htonl(in6.s6_addr32[0]);
in6.s6_addr32[1] = htonl(in6.s6_addr32[1]);
in6.s6_addr32[2] = htonl(in6.s6_addr32[2]);
in6.s6_addr32[3] = htonl(in6.s6_addr32[3]);
inet_ntop(AF_INET6, &in6, addr6, sizeof(addr6));
inet6_aftype.input(1, addr6, &sas);
sas.ss_family = AF_INET6;
} else {
fprintf(stderr, _("warning, got bogus igmp6 line %d.\n"), lnr);
return;
}
if ((ap = get_afntype(sas.ss_family)) == NULL) {
fprintf(stderr, _("netstat: unsupported address family %d !\n"),
sas.ss_family);
return;
}
safe_strncpy(mcast_addr, ap->sprint(&sas, flag_not & FLAG_NUM_HOST),
sizeof(mcast_addr));
printf("%-15s %-6d %s\n", device, refcnt, mcast_addr);
#endif
} else { /* IPV4 */
#if HAVE_AFINET
if (line[0] != '\t') {
if (idx_flag) {
if ((num = sscanf(line, "%d\t%15c", &idx, device)) < 2) {
fprintf(stderr, _("warning, got bogus igmp line %d.\n"), lnr);
return;
}
} else {
if ((num = sscanf(line, "%15c", device)) < 1 ) {
fprintf(stderr, _("warning, got bogus igmp line %d.\n"), lnr);
return;
}
}
offset = strrchr(device, ':');
if (offset)
*offset = 0;
return;
} else if ( line[0] == '\t' ) {
if ( (num = sscanf(line, "\t%8[0-9A-Fa-f] %d", mcast_addr, &refcnt)) < 2 ) {
fprintf(stderr, _("warning, got bogus igmp line %d.\n"), lnr);
return;
}
sscanf(mcast_addr, "%X", &sin->sin_addr.s_addr);
sas.ss_family = AF_INET;
} else {
fprintf(stderr, _("warning, got bogus igmp line %d.\n"), lnr);
return;
}
if ((ap = get_afntype(sas.ss_family)) == NULL) {
fprintf(stderr, _("netstat: unsupported address family %d !\n"),
sas.ss_family);
return;
}
safe_strncpy(mcast_addr, ap->sprint(&sas, flag_not & FLAG_NUM_HOST),
sizeof(mcast_addr));
printf("%-15s %-6d %s\n", device, refcnt, mcast_addr );
#endif
} /* IPV4 */
}
#if HAVE_AFX25
static int x25_info(void)
{
FILE *f=proc_fopen(_PATH_PROCNET_X25);
char buffer[256],dev[16];
int st,vs,vr,sendq,recvq,lci;
static char *x25_state[5]=
{
"LISTENING",
"SABM_SENT",
"DISC_SENT",
"ESTABLISHED",
"RECOVERY"
};
if(!f)
{
if (errno != ENOENT) {
perror(_PATH_PROCNET_X25);
return(-1);
}
if (flag_arg || flag_ver)
ESYSNOT("netstat","AF X25");
if (flag_arg)
return(1);
else
return(0);
}
printf( _("Active X.25 sockets\n"));
/* IMHO, Vr/Vs is not very usefull --SF */
printf( _("Dest Source Device LCI State Vr/Vs Send-Q Recv-Q\n"));
if (fgets(buffer,256,f))
/* eat line */;
while(fgets(buffer,256,f))
{
buffer[10]=0;
buffer[20]=0;
sscanf(buffer+22,"%s %d %d %d %d %*d %*d %*d %*d %*d %*d %d %d %*d",
dev,&lci,&st,&vs,&vr,&sendq,&recvq);
if (!(flag_all || lci))
continue;
printf("%-15s %-15s %-7s %-3d %-11s %02d/%02d %-6d %-6d\n",
buffer,buffer+11,
dev,
lci,
x25_state[st],
vr,vs,sendq,recvq);
}
fclose(f);
return 0;
}
#endif
static int igmp_info(void)
{
INFO_GUTS6(_PATH_PROCNET_IGMP, _PATH_PROCNET_IGMP6, "AF INET (igmp)",
igmp_do_one, "igmp", "igmp6");
}
static const char *sctp_socket_state_str(int state)
{
if (state >= 0 && state < ARRAY_SIZE(tcp_state))
return tcp_state[state];
else {
static char state_str_buf[64];
sprintf(state_str_buf, "UNKNOWN(%d)", state);
return state_str_buf;
}
}
static const struct aftype *process_sctp_addr_str(const char *addr_str, struct sockaddr_storage *sas)
{
if (strchr(addr_str,':')) {
#if HAVE_AFINET6
extern struct aftype inet6_aftype;
/* Demangle what the kernel gives us */
struct in6_addr in6;
char addr6_str[INET6_ADDRSTRLEN];
unsigned u0, u1, u2, u3, u4, u5, u6, u7;
sscanf(addr_str, "%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X",
&u0, &u1, &u2, &u3, &u4, &u5, &u6, &u7);
in6.s6_addr16[0] = htons(u0);
in6.s6_addr16[1] = htons(u1);
in6.s6_addr16[2] = htons(u2);
in6.s6_addr16[3] = htons(u3);
in6.s6_addr16[4] = htons(u4);
in6.s6_addr16[5] = htons(u5);
in6.s6_addr16[6] = htons(u6);
in6.s6_addr16[7] = htons(u7);
inet_ntop(AF_INET6, &in6, addr6_str, sizeof(addr6_str));
inet6_aftype.input(1, addr6_str, sas);
sas->ss_family = AF_INET6;
#endif
} else {
struct sockaddr_in *sin = (struct sockaddr_in *)sas;
sin->sin_addr.s_addr = inet_addr(addr_str);
sas->ss_family = AF_INET;
}
return get_afntype(sas->ss_family);
}
static void sctp_eps_do_one(int lnr, char *line, const char *proto)
{
char buffer[1024];
int state, port;
int uid;
unsigned long inode;
const struct aftype *ap;
struct sockaddr_storage localsas;
const char *sst_str;
const char *lport_str;
const char *uid_str;
const char *inode_str;
char *laddrs_str;
if (lnr == 0) {
/* ENDPT SOCK STY SST HBKT LPORT UID INODE LADDRS */
return;
}
strtok(line, " \t\n"); /* skip endpt */
strtok(0, " \t\n"); /* skip sock */
strtok(0, " \t\n"); /* skip sty */
sst_str = strtok(0, " \t\n");
strtok(0, " \t\n"); /* skip hash bucket */
lport_str = strtok(0, " \t\n");
uid_str = strtok(0, " \t\n");
inode_str = strtok(0, " \t\n");
laddrs_str = strtok(0, "\t\n");
if (!sst_str || !lport_str || !uid_str || !inode_str) {
fprintf(stderr, _("warning, got bogus sctp eps line.\n"));
return;
}
state = atoi(sst_str);
port = atoi(lport_str);
uid = atoi(uid_str);
inode = strtoul(inode_str,0,0);
const char *this_local_addr;
int first = 1;
char local_port[16];
snprintf(local_port, sizeof(local_port), "%s",
get_sname(htons(port), proto, flag_not & FLAG_NUM_PORT));
for (this_local_addr = strtok(laddrs_str, " \t\n");
this_local_addr;
this_local_addr = strtok(0, " \t\n")) {
char local_addr[64];
ap = process_sctp_addr_str(this_local_addr, &localsas);
if (ap)
safe_strncpy(local_addr, ap->sprint(&localsas, flag_not), sizeof(local_addr));
else
sprintf(local_addr, _("unsupported address family %d"), localsas.ss_family);
if (first)
printf("sctp ");
else
printf("\n ");
sprintf(buffer, "%s:%s", local_addr, local_port);
printf("%-47s", buffer);
printf(" %-11s", first ? sctp_socket_state_str(state) : "");
first = 0;
}
finish_this_one(uid, inode, "");
}
static void sctp_assoc_do_one(int lnr, char *line, const char *proto)
{
char buffer[1024];
int state, lport,rport;
int uid;
unsigned rxqueue,txqueue;
unsigned long inode;
const struct aftype *ap;
struct sockaddr_storage localsas, remotesas;
const char *sst_str;
const char *txqueue_str;
const char *rxqueue_str;
const char *lport_str, *rport_str;
const char *uid_str;
const char *inode_str;
char *laddrs_str;
char *raddrs_str;
if (lnr == 0) {
/* ASSOC SOCK STY SST ST HBKT ASSOC-ID TX_QUEUE RX_QUEUE UID INODE LPORT RPORT LADDRS <-> RADDRS */
return;
}
strtok(line, " \t\n"); /* skip assoc */
strtok(0, " \t\n"); /* skip sock */
strtok(0, " \t\n"); /* skip sty */
sst_str = strtok(0, " \t\n");
strtok(0, " \t\n");
strtok(0, " \t\n"); /* skip hash bucket */
strtok(0, " \t\n"); /* skip hash assoc-id */
txqueue_str = strtok(0, " \t\n");
rxqueue_str = strtok(0, " \t\n");
uid_str = strtok(0, " \t\n");
inode_str = strtok(0, " \t\n");
lport_str = strtok(0, " \t\n");
rport_str = strtok(0, " \t\n");
laddrs_str = strtok(0, "<->\t\n");
raddrs_str = strtok(0, "<->\t\n");
if (!sst_str || !txqueue_str || !rxqueue_str || !uid_str ||
!inode_str || !lport_str || !rport_str) {
fprintf(stderr, _("warning, got bogus sctp assoc line.\n"));
return;
}
state = atoi(sst_str);
txqueue = atoi(txqueue_str);
rxqueue = atoi(rxqueue_str);
uid = atoi(uid_str);
inode = strtoul(inode_str, 0, 0);
lport = atoi(lport_str);
rport = atoi(rport_str);
/*print all addresses*/
const char *this_local_addr;
const char *this_remote_addr;
char *ss1, *ss2;
int first = 1;
char local_port[16];
char remote_port[16];
snprintf(local_port, sizeof(local_port), "%s",
get_sname(htons(lport), proto,
flag_not & FLAG_NUM_PORT));
snprintf(remote_port, sizeof(remote_port), "%s",
get_sname(htons(rport), proto,
flag_not & FLAG_NUM_PORT));