Fix order of 2nd and 3rd arguments to fread so the count of the
[unix-history] / usr / src / usr.bin / netstat / inet.c
CommitLineData
c7fc5288 1#ifndef lint
444a708d 2static char sccsid[] = "@(#)inet.c 4.4 82/10/07";
c7fc5288
SL
3#endif
4
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <sys/socketvar.h>
8#include <sys/mbuf.h>
9#include <sys/protosw.h>
10#include <net/route.h>
11#define TCPSTATES
12#include <net/inet.h>
13#include <netdb.h>
14
15struct inpcb inpcb;
16struct tcpcb tcpcb;
17struct socket socket;
18struct protosw proto;
19extern int kmem;
20extern int Aflag;
21extern int aflag;
22extern int nflag;
23
24static int first = 1;
25char *inetname();
26
27/*
28 * Print a summary of connections related to an Internet
29 * protocol. For TCP, also give state of connection.
30 * Listening processes (aflag) are suppressed unless the
31 * -a (all) flag is specified.
32 */
33protopr(off, name)
34 off_t off;
35 char *name;
36{
37 struct inpcb cb;
38 register struct inpcb *prev, *next;
39 int istcp;
40
41 if (off == 0) {
42 printf("%s control block: symbol not in namelist\n", name);
43 return;
44 }
45 istcp = strcmp(name, "tcp") == 0;
46 klseek(kmem, off, 0);
47 read(kmem, &cb, sizeof (struct inpcb));
48 inpcb = cb;
49 prev = (struct inpcb *)off;
50 if (first) {
51 printf("Active connections");
52 if (aflag)
53 printf(" (including servers)");
54 putchar('\n');
55 if (Aflag)
56 printf("%-8.8s ", "PCB");
57 printf("%-5.5s %-6.6s %-6.6s %-18.18s %-18.18s %s\n",
58 "Proto", "Recv-Q", "Send-Q",
59 "Local Address", "Foreign Address", "(state)");
60 first = 0;
61 }
62 while (inpcb.inp_next != (struct inpcb *)off) {
63 char *cp;
64
65 next = inpcb.inp_next;
66 klseek(kmem, (off_t)next, 0);
67 read(kmem, &inpcb, sizeof (inpcb));
68 if (inpcb.inp_prev != prev) {
69 printf("???\n");
70 break;
71 }
72 if (!aflag &&
444a708d 73 inet_lnaof(inpcb.inp_laddr.s_addr) == INADDR_ANY) {
c7fc5288
SL
74 prev = next;
75 continue;
76 }
77 klseek(kmem, (off_t)inpcb.inp_socket, 0);
78 read(kmem, &socket, sizeof (socket));
79 if (istcp) {
80 klseek(kmem, (off_t)inpcb.inp_ppcb, 0);
81 read(kmem, &tcpcb, sizeof (tcpcb));
82 }
83 if (Aflag)
84 printf("%8x ", inpcb.inp_ppcb);
85 printf("%-5.5s %6d %6d ", name, socket.so_rcv.sb_cc,
86 socket.so_snd.sb_cc);
87 inetprint(&inpcb.inp_laddr, inpcb.inp_lport, name);
88 inetprint(&inpcb.inp_faddr, inpcb.inp_fport, name);
89 if (istcp) {
90 if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
91 printf(" %d", tcpcb.t_state);
92 else
93 printf(" %s", tcpstates[tcpcb.t_state]);
94 }
95 putchar('\n');
96 prev = next;
97 }
98}
99
100/*
101 * Pretty print an Internet address (net address + port).
102 * If the nflag was specified, use numbers instead of names.
103 */
104inetprint(in, port, proto)
105 register struct in_addr *in;
106 int port;
107 char *proto;
108{
109 struct servent *sp = 0;
110 char line[80], *cp, *index();
111
112 sprintf(line, "%.10s.", inetname(*in));
113 cp = index(line, '\0');
114#if vax || pdp11
115 port = ntohs((u_short)port);
116#endif
117 if (!nflag && port)
9bce7b73 118 sp = getservbyport(port, proto);
c7fc5288
SL
119 if (sp || port == 0)
120 sprintf(cp, "%.8s", sp ? sp->s_name : "*");
121 else
122 sprintf(cp, "%d", port);
123 printf(" %-18.18s", line);
124}
125
c7fc5288
SL
126/*
127 * Construct an Internet address representation.
128 * If the nflag has been supplied, give
129 * numeric value, otherwise try for symbolic name.
130 */
131char *
132inetname(in)
133 struct in_addr in;
134{
135 char *cp = 0;
136 static char line[50];
137
138 if (!nflag) {
444a708d
SL
139 if (inet_lnaof(in) == INADDR_ANY) {
140 struct netent *np;
c7fc5288 141
444a708d 142 np = getnetbyaddr(inet_netof(in), AF_INET);
c7fc5288
SL
143 if (np)
144 cp = np->n_name;
145 } else {
9bce7b73 146 struct hostent *hp;
c7fc5288 147
3394f0b5
SL
148 hp = gethostbyaddr(&in, sizeof (struct in_addr),
149 AF_INET);
c7fc5288
SL
150 if (hp)
151 cp = hp->h_name;
152 }
153 }
154 if (in.s_addr == INADDR_ANY)
155 strcpy(line, "*");
156 else if (cp)
157 strcpy(line, cp);
158 else {
159 u_char *ucp = (u_char *)&in;
160 sprintf(line, "%u.%u.%u.%u", ucp[0], ucp[1], ucp[2], ucp[3]);
161 }
162 return (line);
163}