1 /* $NetBSD: netcmds.c,v 1.4 1995/05/21 17:14:38 mycroft 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.4 1995/05/21 17:14:38 mycroft 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 <arpa/inet.h> 59 60 #include <netdb.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <ctype.h> 64 #include "systat.h" 65 #include "extern.h" 66 67 #define streq(a,b) (strcmp(a,b)==0) 68 69 static struct hitem { 70 struct in_addr addr; 71 int onoff; 72 } *hosts; 73 74 int nports, nhosts, protos; 75 76 static void changeitems __P((char *, int)); 77 static int selectproto __P((char *)); 78 static void showprotos __P((void)); 79 static int selectport __P((long, int)); 80 static void showports __P((void)); 81 static int selecthost __P((struct in_addr *, int)); 82 static void showhosts __P((void)); 83 84 int 85 netcmd(cmd, args) 86 char *cmd, *args; 87 { 88 89 if (prefix(cmd, "tcp") || prefix(cmd, "udp")) { 90 selectproto(cmd); 91 return (1); 92 } 93 if (prefix(cmd, "ignore") || prefix(cmd, "display")) { 94 changeitems(args, prefix(cmd, "display")); 95 return (1); 96 } 97 if (prefix(cmd, "reset")) { 98 selectproto(0); 99 selecthost(0, 0); 100 selectport(-1, 0); 101 return (1); 102 } 103 if (prefix(cmd, "show")) { 104 move(CMDLINE, 0); clrtoeol(); 105 if (*args == '\0') { 106 showprotos(); 107 showhosts(); 108 showports(); 109 return (1); 110 } 111 if (prefix(args, "protos")) 112 showprotos(); 113 else if (prefix(args, "hosts")) 114 showhosts(); 115 else if (prefix(args, "ports")) 116 showports(); 117 else 118 addstr("show what?"); 119 return (1); 120 } 121 return (0); 122 } 123 124 125 static void 126 changeitems(args, onoff) 127 char *args; 128 int onoff; 129 { 130 register char *cp; 131 struct servent *sp; 132 struct hostent *hp; 133 struct in_addr in; 134 char *index(); 135 136 cp = index(args, '\n'); 137 if (cp) 138 *cp = '\0'; 139 for (;;args = cp) { 140 for (cp = args; *cp && isspace(*cp); cp++) 141 ; 142 args = cp; 143 for (; *cp && !isspace(*cp); cp++) 144 ; 145 if (*cp) 146 *cp++ = '\0'; 147 if (cp - args == 0) 148 break; 149 sp = getservbyname(args, 150 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0); 151 if (sp) { 152 selectport(sp->s_port, onoff); 153 continue; 154 } 155 if (inet_aton(args, &in) == 0) { 156 hp = gethostbyname(args); 157 if (hp == 0) { 158 error("%s: unknown host or port", args); 159 continue; 160 } 161 memcpy(&in, hp->h_addr, hp->h_length); 162 } 163 selecthost(&in, onoff); 164 } 165 } 166 167 static int 168 selectproto(proto) 169 char *proto; 170 { 171 int new = protos; 172 173 if (proto == 0 || streq(proto, "all")) 174 new = TCP|UDP; 175 else if (streq(proto, "tcp")) 176 new = TCP; 177 else if (streq(proto, "udp")) 178 new = UDP; 179 return (new != protos, protos = new); 180 } 181 182 static void 183 showprotos() 184 { 185 186 if ((protos&TCP) == 0) 187 addch('!'); 188 addstr("tcp "); 189 if ((protos&UDP) == 0) 190 addch('!'); 191 addstr("udp "); 192 } 193 194 static struct pitem { 195 long port; 196 int onoff; 197 } *ports; 198 199 static int 200 selectport(port, onoff) 201 long port; 202 int onoff; 203 { 204 register struct pitem *p; 205 206 if (port == -1) { 207 if (ports == 0) 208 return (0); 209 free((char *)ports), ports = 0; 210 nports = 0; 211 return (1); 212 } 213 for (p = ports; p < ports+nports; p++) 214 if (p->port == port) { 215 p->onoff = onoff; 216 return (0); 217 } 218 if (nports == 0) 219 ports = (struct pitem *)malloc(sizeof (*p)); 220 else 221 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p)); 222 p = &ports[nports++]; 223 p->port = port; 224 p->onoff = onoff; 225 return (1); 226 } 227 228 int 229 checkport(inp) 230 register struct inpcb *inp; 231 { 232 register struct pitem *p; 233 234 if (ports) 235 for (p = ports; p < ports+nports; p++) 236 if (p->port == inp->inp_lport || p->port == inp->inp_fport) 237 return (p->onoff); 238 return (1); 239 } 240 241 static void 242 showports() 243 { 244 register struct pitem *p; 245 struct servent *sp; 246 247 for (p = ports; p < ports+nports; p++) { 248 sp = getservbyport(p->port, 249 protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp"); 250 if (!p->onoff) 251 addch('!'); 252 if (sp) 253 printw("%s ", sp->s_name); 254 else 255 printw("%d ", p->port); 256 } 257 } 258 259 static int 260 selecthost(in, onoff) 261 struct in_addr *in; 262 int onoff; 263 { 264 register struct hitem *p; 265 266 if (in == 0) { 267 if (hosts == 0) 268 return (0); 269 free((char *)hosts), hosts = 0; 270 nhosts = 0; 271 return (1); 272 } 273 for (p = hosts; p < hosts+nhosts; p++) 274 if (p->addr.s_addr == in->s_addr) { 275 p->onoff = onoff; 276 return (0); 277 } 278 if (nhosts == 0) 279 hosts = (struct hitem *)malloc(sizeof (*p)); 280 else 281 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p)); 282 p = &hosts[nhosts++]; 283 p->addr = *in; 284 p->onoff = onoff; 285 return (1); 286 } 287 288 int 289 checkhost(inp) 290 register struct inpcb *inp; 291 { 292 register struct hitem *p; 293 294 if (hosts) 295 for (p = hosts; p < hosts+nhosts; p++) 296 if (p->addr.s_addr == inp->inp_laddr.s_addr || 297 p->addr.s_addr == inp->inp_faddr.s_addr) 298 return (p->onoff); 299 return (1); 300 } 301 302 static void 303 showhosts() 304 { 305 register struct hitem *p; 306 struct hostent *hp; 307 308 for (p = hosts; p < hosts+nhosts; p++) { 309 hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET); 310 if (!p->onoff) 311 addch('!'); 312 printw("%s ", hp ? hp->h_name : inet_ntoa(p->addr)); 313 } 314 } 315