1 /* $NetBSD: netcmds.c,v 1.2 1995/01/20 08:52:03 jtc Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93"; 39 #endif 40 static char rcsid[] = "$NetBSD: netcmds.c,v 1.2 1995/01/20 08:52:03 jtc Exp $"; 41 #endif /* not lint */ 42 43 /* 44 * Common network command support routines. 45 */ 46 #include <sys/param.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/mbuf.h> 50 #include <sys/protosw.h> 51 52 #include <net/route.h> 53 #include <netinet/in.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/ip.h> 56 #include <netinet/in_pcb.h> 57 58 #include <netdb.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <ctype.h> 62 #include "systat.h" 63 #include "extern.h" 64 65 #define streq(a,b) (strcmp(a,b)==0) 66 67 static struct hitem { 68 struct in_addr addr; 69 int onoff; 70 } *hosts; 71 72 int nports, nhosts, protos; 73 74 static void changeitems __P((char *, int)); 75 static int selectproto __P((char *)); 76 static void showprotos __P((void)); 77 static int selectport __P((long, int)); 78 static void showports __P((void)); 79 static int selecthost __P((struct in_addr *, int)); 80 static void showhosts __P((void)); 81 82 int 83 netcmd(cmd, args) 84 char *cmd, *args; 85 { 86 87 if (prefix(cmd, "tcp") || prefix(cmd, "udp")) { 88 selectproto(cmd); 89 return (1); 90 } 91 if (prefix(cmd, "ignore") || prefix(cmd, "display")) { 92 changeitems(args, prefix(cmd, "display")); 93 return (1); 94 } 95 if (prefix(cmd, "reset")) { 96 selectproto(0); 97 selecthost(0, 0); 98 selectport(-1, 0); 99 return (1); 100 } 101 if (prefix(cmd, "show")) { 102 move(CMDLINE, 0); clrtoeol(); 103 if (*args == '\0') { 104 showprotos(); 105 showhosts(); 106 showports(); 107 return (1); 108 } 109 if (prefix(args, "protos")) 110 showprotos(); 111 else if (prefix(args, "hosts")) 112 showhosts(); 113 else if (prefix(args, "ports")) 114 showports(); 115 else 116 addstr("show what?"); 117 return (1); 118 } 119 return (0); 120 } 121 122 123 static void 124 changeitems(args, onoff) 125 char *args; 126 int onoff; 127 { 128 register char *cp; 129 struct servent *sp; 130 struct hostent *hp; 131 struct in_addr in; 132 char *index(); 133 134 cp = index(args, '\n'); 135 if (cp) 136 *cp = '\0'; 137 for (;;args = cp) { 138 for (cp = args; *cp && isspace(*cp); cp++) 139 ; 140 args = cp; 141 for (; *cp && !isspace(*cp); cp++) 142 ; 143 if (*cp) 144 *cp++ = '\0'; 145 if (cp - args == 0) 146 break; 147 sp = getservbyname(args, 148 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0); 149 if (sp) { 150 selectport(sp->s_port, onoff); 151 continue; 152 } 153 hp = gethostbyname(args); 154 if (hp == 0) { 155 in.s_addr = inet_addr(args); 156 if (in.s_addr == -1) { 157 error("%s: unknown host or port", args); 158 continue; 159 } 160 } else 161 in = *(struct in_addr *)hp->h_addr; 162 selecthost(&in, onoff); 163 } 164 } 165 166 static int 167 selectproto(proto) 168 char *proto; 169 { 170 int new = protos; 171 172 if (proto == 0 || streq(proto, "all")) 173 new = TCP|UDP; 174 else if (streq(proto, "tcp")) 175 new = TCP; 176 else if (streq(proto, "udp")) 177 new = UDP; 178 return (new != protos, protos = new); 179 } 180 181 static void 182 showprotos() 183 { 184 185 if ((protos&TCP) == 0) 186 addch('!'); 187 addstr("tcp "); 188 if ((protos&UDP) == 0) 189 addch('!'); 190 addstr("udp "); 191 } 192 193 static struct pitem { 194 long port; 195 int onoff; 196 } *ports; 197 198 static int 199 selectport(port, onoff) 200 long port; 201 int onoff; 202 { 203 register struct pitem *p; 204 205 if (port == -1) { 206 if (ports == 0) 207 return (0); 208 free((char *)ports), ports = 0; 209 nports = 0; 210 return (1); 211 } 212 for (p = ports; p < ports+nports; p++) 213 if (p->port == port) { 214 p->onoff = onoff; 215 return (0); 216 } 217 if (nports == 0) 218 ports = (struct pitem *)malloc(sizeof (*p)); 219 else 220 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p)); 221 p = &ports[nports++]; 222 p->port = port; 223 p->onoff = onoff; 224 return (1); 225 } 226 227 int 228 checkport(inp) 229 register struct inpcb *inp; 230 { 231 register struct pitem *p; 232 233 if (ports) 234 for (p = ports; p < ports+nports; p++) 235 if (p->port == inp->inp_lport || p->port == inp->inp_fport) 236 return (p->onoff); 237 return (1); 238 } 239 240 static void 241 showports() 242 { 243 register struct pitem *p; 244 struct servent *sp; 245 246 for (p = ports; p < ports+nports; p++) { 247 sp = getservbyport(p->port, 248 protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp"); 249 if (!p->onoff) 250 addch('!'); 251 if (sp) 252 printw("%s ", sp->s_name); 253 else 254 printw("%d ", p->port); 255 } 256 } 257 258 static int 259 selecthost(in, onoff) 260 struct in_addr *in; 261 int onoff; 262 { 263 register struct hitem *p; 264 265 if (in == 0) { 266 if (hosts == 0) 267 return (0); 268 free((char *)hosts), hosts = 0; 269 nhosts = 0; 270 return (1); 271 } 272 for (p = hosts; p < hosts+nhosts; p++) 273 if (p->addr.s_addr == in->s_addr) { 274 p->onoff = onoff; 275 return (0); 276 } 277 if (nhosts == 0) 278 hosts = (struct hitem *)malloc(sizeof (*p)); 279 else 280 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p)); 281 p = &hosts[nhosts++]; 282 p->addr = *in; 283 p->onoff = onoff; 284 return (1); 285 } 286 287 int 288 checkhost(inp) 289 register struct inpcb *inp; 290 { 291 register struct hitem *p; 292 293 if (hosts) 294 for (p = hosts; p < hosts+nhosts; p++) 295 if (p->addr.s_addr == inp->inp_laddr.s_addr || 296 p->addr.s_addr == inp->inp_faddr.s_addr) 297 return (p->onoff); 298 return (1); 299 } 300 301 static void 302 showhosts() 303 { 304 register struct hitem *p; 305 struct hostent *hp; 306 307 for (p = hosts; p < hosts+nhosts; p++) { 308 hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET); 309 if (!p->onoff) 310 addch('!'); 311 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr)); 312 } 313 } 314