2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)netstat.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/systat/netstat.c,v 1.13 1999/08/30 08:18:08 peter Exp $
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/protosw.h>
45 #include <sys/sysctl.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <net/route.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
53 #include <netinet/ip6.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/ip_icmp.h>
57 #include <netinet/icmp_var.h>
58 #include <netinet/ip_var.h>
59 #include <netinet/tcp.h>
60 #include <netinet/tcpip.h>
61 #include <netinet/tcp_seq.h>
63 #include <netinet/tcp_fsm.h>
64 #include <netinet/tcp_timer.h>
65 #include <netinet/tcp_var.h>
66 #include <netinet/tcp_debug.h>
67 #include <netinet/udp.h>
68 #include <netinet/udp_var.h>
80 static void enter(struct inpcb *, struct xsocket *, int, const char *);
81 static char *inetname(struct in_addr);
82 static void inetprint(struct in_addr *, int, const char *);
84 #define streq(a,b) (strcmp(a,b)==0)
85 #define YMAX(w) ((w)->_maxy-1)
92 return (subwin(stdscr, LINES-5-1, 0, 5, 0));
96 struct netinfo *ni_forw, *ni_prev;
97 short ni_line; /* line on screen */
98 short ni_seen; /* 0 when not present in list */
100 #define NIF_LACHG 0x1 /* local address changed */
101 #define NIF_FACHG 0x2 /* foreign address changed */
102 short ni_state; /* tcp state */
103 const char *ni_proto; /* protocol */
104 struct in_addr ni_laddr; /* local address */
105 long ni_lport; /* local port */
106 struct in_addr ni_faddr; /* foreign address */
107 long ni_fport; /* foreign port */
108 long ni_rcvcc; /* rcv buffer character count */
109 long ni_sndcc; /* snd buffer character count */
113 struct netinfo *ni_forw, *ni_prev;
116 static int aflag = 0;
117 static int nflag = 0;
118 static int lastrow = 1;
121 closenetstat(WINDOW *w)
127 p = (struct netinfo *)netcb.ni_forw;
128 while (p != (struct netinfo *)&netcb) {
129 if (p->ni_line != -1)
144 netcb.ni_forw = netcb.ni_prev = (struct netinfo *)&netcb;
152 struct xtcpcb *xtcp = (struct xtcpcb *)xig;
156 if (xtcp->xt_len < sizeof(*xtcp))
158 xso = &xtcp->xt_socket;
159 state = xtcp->xt_tp.t_state;
160 enter(&xtcp->xt_inp, xso, state, "tcp");
166 struct xinpcb *xinp = (struct xinpcb *)xig;
169 if (xinp->xi_len < sizeof(*xinp))
171 xso = &xinp->xi_socket;
172 enter(&xinp->xi_inp, xso, 0, "udp");
176 fetchnetstat_proto(void (*enter_proto)(void *),
180 size_t i, len, elem_len;
182 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
184 warn("sysctl: %s", mibvar);
187 if ((buf = malloc(len)) == NULL) {
188 warn("malloc %lu bytes", (u_long)len);
191 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
192 warn("sysctl: %s", mibvar);
198 * XXX this is better with a single PCB type
204 if (len < sizeof(size_t)) {
205 warnx("sysctl: short read");
209 elem_len = *(size_t *)buf;
212 for (i = 0; i < len; i++, buf2 += elem_len) {
213 if (*(size_t *)(buf2) != elem_len) {
214 warn("sysctl: inconsistent PCB len");
228 for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw)
231 fetchnetstat_proto(enter_tcp, "net.inet.tcp.pcblist");
233 fetchnetstat_proto(enter_udp, "net.inet.udp.pcblist");
237 enter(struct inpcb *inp, struct xsocket *so, int state, const char *proto)
241 if (!aflag && inet_lnaof(inp->inp_laddr) == INADDR_ANY)
243 if (nhosts && !checkhost(inp))
245 if (nports && !checkport(inp))
248 * pcblist may return non-ipv4 sockets, but at the moment
249 * -netstat code doesn't support other than ipv4.
251 if ((inp->inp_vflag & INP_IPV4) == 0)
254 * Only take exact matches, any sockets with
255 * previously unbound addresses will be deleted
256 * below in the display routine because they
257 * will appear as ``not seen'' in the kernel
260 for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
261 if (!streq(proto, p->ni_proto))
263 if (p->ni_lport != inp->inp_lport ||
264 p->ni_laddr.s_addr != inp->inp_laddr.s_addr)
266 if (p->ni_faddr.s_addr == inp->inp_faddr.s_addr &&
267 p->ni_fport == inp->inp_fport)
270 if (p == (struct netinfo *)&netcb) {
271 if ((p = malloc(sizeof(*p))) == NULL) {
272 error("Out of memory");
275 p->ni_prev = (struct netinfo *)&netcb;
276 p->ni_forw = netcb.ni_forw;
277 netcb.ni_forw->ni_prev = p;
280 p->ni_laddr = inp->inp_laddr;
281 p->ni_lport = inp->inp_lport;
282 p->ni_faddr = inp->inp_faddr;
283 p->ni_fport = inp->inp_fport;
285 p->ni_flags = NIF_LACHG|NIF_FACHG;
287 p->ni_rcvcc = so->so_rcv.sb_cc;
288 p->ni_sndcc = so->so_snd.sb_cc;
293 /* column locations */
295 #define FADDR LADDR+22
296 #define CPUID FADDR+22
297 #define PROTO CPUID+4
298 #define RCVCC PROTO+6
299 #define SNDCC RCVCC+7
300 #define STATE SNDCC+7
306 wmove(wnd, 0, 0); wclrtobot(wnd);
307 mvwaddstr(wnd, 0, LADDR, "Local Address");
308 mvwaddstr(wnd, 0, FADDR, "Foreign Address");
309 mvwaddstr(wnd, 0, PROTO, "Proto");
310 mvwaddstr(wnd, 0, RCVCC, "Recv-Q");
311 mvwaddstr(wnd, 0, SNDCC, "Send-Q");
312 mvwaddstr(wnd, 0, STATE, "(state)");
318 struct netinfo *p, *q;
321 * First, delete any connections that have gone
322 * away and adjust the position of connections
323 * below to reflect the deleted line.
326 while (p != (struct netinfo *)&netcb) {
327 if (p->ni_line == -1 || p->ni_seen) {
331 wmove(wnd, p->ni_line, 0); wdeleteln(wnd);
333 for (; q != (struct netinfo *)&netcb; q = q->ni_forw)
334 if (q != p && q->ni_line > p->ni_line) {
336 /* this shouldn't be necessary */
337 q->ni_flags |= NIF_LACHG|NIF_FACHG;
341 p->ni_prev->ni_forw = p->ni_forw;
342 p->ni_forw->ni_prev = p->ni_prev;
347 * Update existing connections and add new ones.
349 for (p = netcb.ni_forw; p != (struct netinfo *)&netcb; p = p->ni_forw) {
350 if (p->ni_line == -1) {
352 * Add a new entry if possible.
354 if (lastrow > YMAX(wnd))
356 p->ni_line = lastrow++;
357 p->ni_flags |= NIF_LACHG|NIF_FACHG;
359 if (p->ni_flags & NIF_LACHG) {
360 wmove(wnd, p->ni_line, LADDR);
361 inetprint(&p->ni_laddr, p->ni_lport, p->ni_proto);
362 p->ni_flags &= ~NIF_LACHG;
364 if (p->ni_flags & NIF_FACHG) {
365 wmove(wnd, p->ni_line, FADDR);
366 inetprint(&p->ni_faddr, p->ni_fport, p->ni_proto);
367 p->ni_flags &= ~NIF_FACHG;
369 mvwaddstr(wnd, p->ni_line, PROTO, p->ni_proto);
370 mvwprintw(wnd, p->ni_line, RCVCC, "%6d", p->ni_rcvcc);
371 mvwprintw(wnd, p->ni_line, SNDCC, "%6d", p->ni_sndcc);
372 if (streq(p->ni_proto, "tcp")) {
373 if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES)
374 mvwprintw(wnd, p->ni_line, STATE, "%d",
377 mvwaddstr(wnd, p->ni_line, STATE,
378 tcpstates[p->ni_state]);
382 if (lastrow < YMAX(wnd)) {
383 wmove(wnd, lastrow, 0); wclrtobot(wnd);
384 wmove(wnd, YMAX(wnd), 0); wdeleteln(wnd); /* XXX */
389 * Pretty print an Internet address (net address + port).
390 * If the nflag was specified, use numbers instead of names.
393 inetprint(struct in_addr *in, int port, const char *proto)
395 struct servent *sp = NULL;
398 snprintf(line, sizeof(line), "%.*s.", 16, inetname(*in));
399 cp = strchr(line, '\0');
401 sp = getservbyport(port, proto);
403 snprintf(cp, sizeof(line) - (cp - line), "%.8s",
404 sp ? sp->s_name : "*");
406 snprintf(cp, sizeof(line) - (cp - line), "%d",
407 ntohs((u_short)port));
408 /* pad to full column to clear any garbage */
409 cp = strchr(line, '\0');
410 while (cp - line < 22)
417 * Construct an Internet address representation.
418 * If the nflag has been supplied, give
419 * numeric value, otherwise try for symbolic name.
422 inetname(struct in_addr in)
425 static char line[50];
429 if (!nflag && in.s_addr != INADDR_ANY) {
430 int net = inet_netof(in);
431 int lna = inet_lnaof(in);
433 if (lna == INADDR_ANY) {
434 np = getnetbyaddr(net, AF_INET);
439 hp = gethostbyaddr(&in, sizeof (in), AF_INET);
444 if (in.s_addr == INADDR_ANY)
447 snprintf(line, sizeof(line), "%s", cp);
449 in.s_addr = ntohl(in.s_addr);
450 #define C(x) ((x) & 0xff)
451 snprintf(line, sizeof(line), "%u.%u.%u.%u", C(in.s_addr >> 24),
452 C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
458 cmdnetstat(const char *cmd, char *args)
462 if (prefix(cmd, "all")) {
466 if (prefix(cmd, "numbers") || prefix(cmd, "names")) {
469 new = prefix(cmd, "numbers");
473 for (; p != (struct netinfo *)&netcb; p = p->ni_forw) {
474 if (p->ni_line == -1)
476 p->ni_flags |= NIF_LACHG|NIF_FACHG;
481 if (!netcmd(cmd, args))