netcmds.c revision 14543
1301169Slidl/*- 2301169Slidl * Copyright (c) 1980, 1992, 1993 3301169Slidl * The Regents of the University of California. All rights reserved. 4301169Slidl * 5301169Slidl * Redistribution and use in source and binary forms, with or without 6301169Slidl * modification, are permitted provided that the following conditions 7301169Slidl * are met: 8301169Slidl * 1. Redistributions of source code must retain the above copyright 9301169Slidl * notice, this list of conditions and the following disclaimer. 10301169Slidl * 2. Redistributions in binary form must reproduce the above copyright 11301169Slidl * notice, this list of conditions and the following disclaimer in the 12301169Slidl * documentation and/or other materials provided with the distribution. 13301169Slidl * 3. All advertising materials mentioning features or use of this software 14301169Slidl * must display the following acknowledgement: 15301169Slidl * This product includes software developed by the University of 16301169Slidl * California, Berkeley and its contributors. 17301169Slidl * 4. Neither the name of the University nor the names of its contributors 18301169Slidl * may be used to endorse or promote products derived from this software 19301169Slidl * without specific prior written permission. 20301169Slidl * 21301169Slidl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22301169Slidl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23301169Slidl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24301169Slidl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25301169Slidl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26301169Slidl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27301169Slidl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28301169Slidl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29301169Slidl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30301169Slidl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31301169Slidl * SUCH DAMAGE. 32301169Slidl */ 33301169Slidl 34301169Slidl#ifndef lint 35301169Slidlstatic char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93"; 36301169Slidl#endif /* not lint */ 37301169Slidl 38301169Slidl/* 39301169Slidl * Common network command support routines. 40301169Slidl */ 41301169Slidl#include <sys/param.h> 42301169Slidl#include <sys/queue.h> 43301169Slidl#include <sys/socket.h> 44301169Slidl#include <sys/socketvar.h> 45301169Slidl#include <sys/mbuf.h> 46301169Slidl#include <sys/protosw.h> 47301169Slidl 48301169Slidl#include <net/route.h> 49301169Slidl#include <netinet/in.h> 50301169Slidl#include <netinet/in_systm.h> 51301169Slidl#include <netinet/ip.h> 52301169Slidl#include <netinet/in_pcb.h> 53301169Slidl 54301169Slidl#include <netdb.h> 55301169Slidl#include <stdlib.h> 56301169Slidl#include <string.h> 57301169Slidl#include <ctype.h> 58301169Slidl#include "systat.h" 59301169Slidl#include "extern.h" 60301169Slidl 61301169Slidl#define streq(a,b) (strcmp(a,b)==0) 62301169Slidl 63301169Slidlstatic struct hitem { 64301169Slidl struct in_addr addr; 65301169Slidl int onoff; 66301169Slidl} *hosts; 67301169Slidl 68301169Slidlint nports, nhosts, protos; 69301169Slidl 70301169Slidlstatic void changeitems __P((char *, int)); 71301169Slidlstatic int selectproto __P((char *)); 72301169Slidlstatic void showprotos __P((void)); 73301169Slidlstatic int selectport __P((long, int)); 74301169Slidlstatic void showports __P((void)); 75301169Slidlstatic int selecthost __P((struct in_addr *, int)); 76301169Slidlstatic void showhosts __P((void)); 77301169Slidl 78301169Slidlint 79301169Slidlnetcmd(cmd, args) 80301169Slidl char *cmd, *args; 81301169Slidl{ 82301169Slidl 83301169Slidl if (prefix(cmd, "tcp") || prefix(cmd, "udp")) { 84301169Slidl selectproto(cmd); 85301169Slidl return (1); 86301169Slidl } 87301169Slidl if (prefix(cmd, "ignore") || prefix(cmd, "display")) { 88301169Slidl changeitems(args, prefix(cmd, "display")); 89301169Slidl return (1); 90301169Slidl } 91301169Slidl if (prefix(cmd, "reset")) { 92 selectproto(0); 93 selecthost(0, 0); 94 selectport(-1, 0); 95 return (1); 96 } 97 if (prefix(cmd, "show")) { 98 move(CMDLINE, 0); clrtoeol(); 99 if (*args == '\0') { 100 showprotos(); 101 showhosts(); 102 showports(); 103 return (1); 104 } 105 if (prefix(args, "protos")) 106 showprotos(); 107 else if (prefix(args, "hosts")) 108 showhosts(); 109 else if (prefix(args, "ports")) 110 showports(); 111 else 112 addstr("show what?"); 113 return (1); 114 } 115 return (0); 116} 117 118 119static void 120changeitems(args, onoff) 121 char *args; 122 int onoff; 123{ 124 register char *cp; 125 struct servent *sp; 126 struct hostent *hp; 127 struct in_addr in; 128 char *index(); 129 130 cp = index(args, '\n'); 131 if (cp) 132 *cp = '\0'; 133 for (;;args = cp) { 134 for (cp = args; *cp && isspace(*cp); cp++) 135 ; 136 args = cp; 137 for (; *cp && !isspace(*cp); cp++) 138 ; 139 if (*cp) 140 *cp++ = '\0'; 141 if (cp - args == 0) 142 break; 143 sp = getservbyname(args, 144 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0); 145 if (sp) { 146 selectport(sp->s_port, onoff); 147 continue; 148 } 149 hp = gethostbyname(args); 150 if (hp == 0) { 151 in.s_addr = inet_addr(args); 152 if (in.s_addr == -1) { 153 error("%s: unknown host or port", args); 154 continue; 155 } 156 } else 157 in = *(struct in_addr *)hp->h_addr; 158 selecthost(&in, onoff); 159 } 160} 161 162static int 163selectproto(proto) 164 char *proto; 165{ 166 int new = protos; 167 168 if (proto == 0 || streq(proto, "all")) 169 new = TCP|UDP; 170 else if (streq(proto, "tcp")) 171 new = TCP; 172 else if (streq(proto, "udp")) 173 new = UDP; 174 return (new != protos, protos = new); 175} 176 177static void 178showprotos() 179{ 180 181 if ((protos&TCP) == 0) 182 addch('!'); 183 addstr("tcp "); 184 if ((protos&UDP) == 0) 185 addch('!'); 186 addstr("udp "); 187} 188 189static struct pitem { 190 long port; 191 int onoff; 192} *ports; 193 194static int 195selectport(port, onoff) 196 long port; 197 int onoff; 198{ 199 register struct pitem *p; 200 201 if (port == -1) { 202 if (ports == 0) 203 return (0); 204 free((char *)ports), ports = 0; 205 nports = 0; 206 return (1); 207 } 208 for (p = ports; p < ports+nports; p++) 209 if (p->port == port) { 210 p->onoff = onoff; 211 return (0); 212 } 213 if (nports == 0) 214 ports = (struct pitem *)malloc(sizeof (*p)); 215 else 216 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p)); 217 p = &ports[nports++]; 218 p->port = port; 219 p->onoff = onoff; 220 return (1); 221} 222 223int 224checkport(inp) 225 register struct inpcb *inp; 226{ 227 register struct pitem *p; 228 229 if (ports) 230 for (p = ports; p < ports+nports; p++) 231 if (p->port == inp->inp_lport || p->port == inp->inp_fport) 232 return (p->onoff); 233 return (1); 234} 235 236static void 237showports() 238{ 239 register struct pitem *p; 240 struct servent *sp; 241 242 for (p = ports; p < ports+nports; p++) { 243 sp = getservbyport(p->port, 244 protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp"); 245 if (!p->onoff) 246 addch('!'); 247 if (sp) 248 printw("%s ", sp->s_name); 249 else 250 printw("%d ", p->port); 251 } 252} 253 254static int 255selecthost(in, onoff) 256 struct in_addr *in; 257 int onoff; 258{ 259 register struct hitem *p; 260 261 if (in == 0) { 262 if (hosts == 0) 263 return (0); 264 free((char *)hosts), hosts = 0; 265 nhosts = 0; 266 return (1); 267 } 268 for (p = hosts; p < hosts+nhosts; p++) 269 if (p->addr.s_addr == in->s_addr) { 270 p->onoff = onoff; 271 return (0); 272 } 273 if (nhosts == 0) 274 hosts = (struct hitem *)malloc(sizeof (*p)); 275 else 276 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p)); 277 p = &hosts[nhosts++]; 278 p->addr = *in; 279 p->onoff = onoff; 280 return (1); 281} 282 283int 284checkhost(inp) 285 register struct inpcb *inp; 286{ 287 register struct hitem *p; 288 289 if (hosts) 290 for (p = hosts; p < hosts+nhosts; p++) 291 if (p->addr.s_addr == inp->inp_laddr.s_addr || 292 p->addr.s_addr == inp->inp_faddr.s_addr) 293 return (p->onoff); 294 return (1); 295} 296 297static void 298showhosts() 299{ 300 register struct hitem *p; 301 struct hostent *hp; 302 303 for (p = hosts; p < hosts+nhosts; p++) { 304 hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET); 305 if (!p->onoff) 306 addch('!'); 307 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr)); 308 } 309} 310