date and time created 85/05/30 16:59:16 by mckusick
[unix-history] / usr / src / usr.bin / systat / netcmds.c
CommitLineData
47d81412
KM
1#ifndef lint
2static char sccsid[] = "@(#)netcmds.c 1.1 (Berkeley) %G%";
3#endif
4
5/*
6 * Common network command support routines.
7 */
8#include "systat.h"
9#include <ctype.h>
10
11#include <sys/socket.h>
12#include <sys/socketvar.h>
13#include <sys/mbuf.h>
14#include <sys/protosw.h>
15
16#include <net/route.h>
17#include <netinet/in_systm.h>
18#include <netinet/in_pcb.h>
19
20#define streq(a,b) (strcmp(a,b)==0)
21
22netcmd(cmd, args)
23 char *cmd, *args;
24{
25
26 if (prefix(cmd, "tcp") || prefix(cmd, "udp")) {
27 selectproto(cmd);
28 return (1);
29 }
30 if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
31 changeitems(args, prefix(cmd, "display"));
32 return (1);
33 }
34 if (prefix(cmd, "reset")) {
35 selectproto(0);
36 selecthost(0);
37 selectport(-1);
38 return (1);
39 }
40 if (prefix(cmd, "show")) {
41 move(CMDLINE, 0); clrtoeol();
42 if (*args == '\0') {
43 showprotos();
44 showhosts();
45 showports();
46 return (1);
47 }
48 if (prefix(args, "protos"))
49 showprotos();
50 else if (prefix(args, "hosts"))
51 showhosts();
52 else if (prefix(args, "ports"))
53 showports();
54 else
55 addstr("show what?");
56 return (1);
57 }
58 return (0);
59}
60
61static
62changeitems(args, onoff)
63 char *args;
64 int onoff;
65{
66 register char *cp;
67 register int i;
68 struct servent *sp;
69 struct hostent *hp;
70 struct in_addr in;
71 char *index();
72
73 cp = index(args, '\n');
74 if (cp)
75 *cp = '\0';
76 for (;;args = cp) {
77 for (cp = args; *cp && isspace(*cp); cp++)
78 ;
79 args = cp;
80 for (; *cp && !isspace(*cp); cp++)
81 ;
82 if (*cp)
83 *cp++ = '\0';
84 if (cp - args == 0)
85 break;
86 sp = getservbyname(args,
87 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
88 if (sp) {
89 selectport(sp->s_name, onoff);
90 continue;
91 }
92 hp = gethostbyname(args);
93 if (hp == 0) {
94 in.s_addr = inet_addr(args);
95 if (in.s_addr == -1) {
96 error("%s: unknown host or port", args);
97 continue;
98 }
99 } else
100 in = *(struct in_addr *)hp->h_addr;
101 selecthost(&in, onoff);
102 }
103}
104
105static
106selectproto(proto)
107 char *proto;
108{
109 int new = protos;
110
111 if (proto == 0 || streq(proto, "all"))
112 new = TCP|UDP;
113 else if (streq(proto, "tcp"))
114 new = TCP;
115 else if (streq(proto, "udp"))
116 new = UDP;
117 return (new != protos, protos = new);
118}
119
120static
121showprotos()
122{
123
124 if ((protos&TCP) == 0)
125 addch('!');
126 addstr("tcp ");
127 if ((protos&UDP) == 0)
128 addch('!');
129 addstr("udp ");
130}
131
132static struct pitem {
133 long port;
134 int onoff;
135} *ports;
136
137static
138selectport(port, onoff)
139 long port;
140 int onoff;
141{
142 register struct pitem *p, *open;
143
144 if (port == -1) {
145 if (ports == 0)
146 return (0);
147 free((char *)ports), ports = 0;
148 nports = 0;
149 return (1);
150 }
151 open = 0;
152 for (p = ports; p < ports+nports; p++)
153 if (p->port == port) {
154 p->onoff = onoff;
155 return (0);
156 }
157 if (nports == 0)
158 ports = (struct pitem *)malloc(sizeof (*p));
159 else
160 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
161 p = &ports[nports++];
162 p->port = port;
163 p->onoff = onoff;
164 return (1);
165}
166
167checkport(inp)
168 register struct inpcb *inp;
169{
170 register struct pitem *p;
171
172 if (ports)
173 for (p = ports; p < ports+nports; p++)
174 if (p->port == inp->inp_lport || p->port == inp->inp_fport)
175 return (p->onoff);
176 return (1);
177}
178
179static
180showports()
181{
182 register struct pitem *p;
183 struct servent *sp;
184
185 for (p = ports; p < ports+nports; p++) {
186 sp = getservbyport(p->port,
187 protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp");
188 if (!p->onoff)
189 addch('!');
190 if (sp)
191 printw("%s ", sp->s_name);
192 else
193 printw("%d ", p->port);
194 }
195}
196
197static struct hitem {
198 struct in_addr addr;
199 int onoff;
200} *hosts;
201
202static
203selecthost(in, onoff)
204 struct in_addr *in;
205{
206 register struct hitem *p;
207
208 if (in == 0) {
209 if (hosts == 0)
210 return (0);
211 free((char *)hosts), hosts = 0;
212 nhosts = 0;
213 return (1);
214 }
215 for (p = hosts; p < hosts+nhosts; p++)
216 if (p->addr.s_addr == in->s_addr) {
217 p->onoff = onoff;
218 return (0);
219 }
220 if (nhosts == 0)
221 hosts = (struct hitem *)malloc(sizeof (*p));
222 else
223 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
224 p = &hosts[nhosts++];
225 p->addr = *in;
226 p->onoff = onoff;
227 return (1);
228}
229
230checkhost(inp)
231 register struct inpcb *inp;
232{
233 register struct hitem *p;
234
235 if (hosts)
236 for (p = hosts; p < hosts+nhosts; p++)
237 if (p->addr.s_addr == inp->inp_laddr.s_addr ||
238 p->addr.s_addr == inp->inp_faddr.s_addr)
239 return (p->onoff);
240 return (1);
241}
242
243static
244showhosts()
245{
246 register struct hitem *p;
247 struct hostent *hp;
248
249 for (p = hosts; p < hosts+nhosts; p++) {
250 hp = gethostbyaddr(&p->addr, sizeof (p->addr), AF_INET);
251 if (!p->onoff)
252 addch('!');
253 printw("%s ", hp ? hp->h_name : inet_ntoa(p->addr));
254 }
255}