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