xref: /original-bsd/usr.bin/netstat/unix.c (revision 81a135f6)
1 #ifndef lint
2 static char sccsid[] = "@(#)unix.c	1.1 (Berkeley) 06/03/84";
3 #endif
4 
5 /*
6  * Display protocol blocks in the unix domain.
7  */
8 #include <sys/param.h>
9 #include <sys/protosw.h>
10 #include <sys/socket.h>
11 #include <sys/socketvar.h>
12 #include <sys/mbuf.h>
13 #include <sys/un.h>
14 #include <sys/unpcb.h>
15 #define	KERNEL
16 #include <sys/file.h>
17 
18 int	Aflag;
19 int	kmem;
20 
21 unixpr(nfileaddr, fileaddr, unixsw)
22 	off_t nfileaddr, fileaddr;
23 	struct protosw *unixsw;
24 {
25 	register struct file *fp;
26 	struct file *filep;
27 	struct socket sock, *so = &sock;
28 
29 	if (nfileaddr == 0 || fileaddr == 0) {
30 		printf("nfile or file not in namelist.\n");
31 		return;
32 	}
33 	klseek(kmem, nfileaddr, L_SET);
34 	if (read(kmem, &nfile, sizeof (nfile)) != sizeof (nfile)) {
35 		printf("nfile: bad read.\n");
36 		return;
37 	}
38 	klseek(kmem, fileaddr, L_SET);
39 	if (read(kmem, &filep, sizeof (filep)) != sizeof (filep)) {
40 		printf("File table address, bad read.\n");
41 		return;
42 	}
43 	file = (struct file *)calloc(nfile, sizeof (struct file));
44 	if (file == (struct file *)0) {
45 		printf("Out of memory (file table).\n");
46 		return;
47 	}
48 	klseek(kmem, (off_t)filep, L_SET);
49 	if (read(kmem, file, nfile * sizeof (struct file)) !=
50 	    nfile * sizeof (struct file)) {
51 		printf("File table read error.\n");
52 		return;
53 	}
54 	fileNFILE = file + nfile;
55 	for (fp = file; fp < fileNFILE; fp++) {
56 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
57 			continue;
58 		klseek(kmem, fp->f_data, L_SET);
59 		if (read(kmem, so, sizeof (*so)) != sizeof (*so))
60 			continue;
61 		/* kludge */
62 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
63 			if (so->so_pcb)
64 				unixdomainpr(so, fp->f_data);
65 	}
66 	free((char *)file);
67 }
68 
69 static	char *socktype[] =
70     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
71 
72 unixdomainpr(so, soaddr)
73 	register struct socket *so;
74 	caddr_t soaddr;
75 {
76 	struct unpcb unpcb, *unp = &unpcb;
77 	struct mbuf mbuf, *m;
78 	static int first = 1;
79 
80 	klseek(kmem, so->so_pcb, L_SET);
81 	if (read(kmem, unp, sizeof (*unp)) != sizeof (*unp))
82 		return;
83 	if (unp->unp_remaddr) {
84 		m = &mbuf;
85 		klseek(kmem, unp->unp_remaddr, L_SET);
86 		if (read(kmem, m, sizeof (*m)) != sizeof (*m))
87 			m = (struct mbuf *)0;
88 	} else
89 		m = (struct mbuf *)0;
90 	if (first) {
91 		printf(
92 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Remaddr\n",
93 		    "Address", "Type", "Recv-Q", "Send-Q",
94 		    "Inode", "Conn", "Refs", "Nextref");
95 		first = 0;
96 	}
97 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
98 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
99 	    unp->unp_inode, unp->unp_conn,
100 	    unp->unp_refs, unp->unp_nextref);
101 	if (m)
102 		printf(" %.*s", m->m_len, mtod(m, char *));
103 	putchar('\n');
104 }
105