follow address type specificationbs
[unix-history] / usr / src / usr.bin / netstat / inet.c
CommitLineData
c7fc5288 1#ifndef lint
9bce7b73 2static char sccsid[] = "@(#)inet.c 4.2 82/10/05";
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 &&
73 in_lnaof(inpcb.inp_laddr.s_addr) == INADDR_ANY) {
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
126/*
127 * Return the network number from an internet
128 * address; handles class a/b/c network #'s.
129 */
130in_netof(in)
131 struct in_addr in;
132{
133#if vax || pdp11
134 register u_long net;
135
136 if ((in.s_addr&IN_CLASSA) == 0)
137 return (in.s_addr & IN_CLASSA_NET);
138 if ((in.s_addr&IN_CLASSB) == 0)
139 return ((int)htons((u_short)(in.s_addr & IN_CLASSB_NET)));
140 net = htonl((u_long)(in.s_addr & IN_CLASSC_NET));
141 net >>= 8;
142 return ((int)net);
143#else
144 return (IN_NETOF(in));
145#endif
146}
147
148/*
149 * Return the local network address portion of an
150 * internet address; handles class a/b/c network
151 * number formats.
152 */
153in_lnaof(in)
154 struct in_addr in;
155{
156#if vax || pdp11
157#define IN_LNAOF(in) \
158 (((in).s_addr&IN_CLASSA) == 0 ? (in).s_addr&IN_CLASSA_LNA : \
159 ((in).s_addr&IN_CLASSB) == 0 ? (in).s_addr&IN_CLASSB_LNA : \
160 (in).s_addr&IN_CLASSC_LNA)
161 return ((int)htonl((u_long)IN_LNAOF(in)));
162#else
163 return (IN_LNAOF(in));
164#endif
165}
166
167/*
168 * Construct an Internet address representation.
169 * If the nflag has been supplied, give
170 * numeric value, otherwise try for symbolic name.
171 */
172char *
173inetname(in)
174 struct in_addr in;
175{
176 char *cp = 0;
177 static char line[50];
178
179 if (!nflag) {
180 if (in_lnaof(in) == INADDR_ANY) {
9bce7b73 181 struct netent *np = getnetbyaddr(in_netof(in));
c7fc5288
SL
182
183 if (np)
184 cp = np->n_name;
185 } else {
9bce7b73 186 struct hostent *hp;
c7fc5288 187
9bce7b73 188 hp = gethostbyaddr(&in, sizeof (struct in_addr));
c7fc5288
SL
189 if (hp)
190 cp = hp->h_name;
191 }
192 }
193 if (in.s_addr == INADDR_ANY)
194 strcpy(line, "*");
195 else if (cp)
196 strcpy(line, cp);
197 else {
198 u_char *ucp = (u_char *)&in;
199 sprintf(line, "%u.%u.%u.%u", ucp[0], ucp[1], ucp[2], ucp[3]);
200 }
201 return (line);
202}