1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 33 #ifdef lint 34 static const char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93"; 35 #endif 36 37 /* 38 * Common network command support routines. 39 */ 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 46 #include <net/route.h> 47 #include <netinet/in.h> 48 #include <netinet/in_systm.h> 49 #include <netinet/ip.h> 50 #include <netinet/in_pcb.h> 51 #include <arpa/inet.h> 52 53 #include <ctype.h> 54 #include <netdb.h> 55 #include <stdlib.h> 56 #include <string.h> 57 58 #include "systat.h" 59 #include "extern.h" 60 61 #define streq(a,b) (strcmp(a,b)==0) 62 63 static struct hitem { 64 struct in_addr addr; 65 int onoff; 66 } *hosts; 67 68 int nports, nhosts, protos; 69 70 static void changeitems(const char *, int); 71 static int selectproto(const char *); 72 static void showprotos(void); 73 static int selectport(long, int); 74 static void showports(void); 75 static int selecthost(struct in_addr *, int); 76 static void showhosts(void); 77 78 int 79 netcmd(const char *cmd, const char *args) 80 { 81 82 if (prefix(cmd, "proto")) { 83 if (*args == '\0') { 84 move(CMDLINE, 0); 85 clrtoeol(); 86 addstr("which proto?"); 87 } else if (!selectproto(args)) { 88 error("%s: Unknown protocol.", args); 89 } 90 return (1); 91 } 92 if (prefix(cmd, "ignore") || prefix(cmd, "display")) { 93 changeitems(args, prefix(cmd, "display")); 94 return (1); 95 } 96 if (prefix(cmd, "reset")) { 97 selectproto(0); 98 selecthost(0, 0); 99 selectport(-1, 0); 100 return (1); 101 } 102 if (prefix(cmd, "show")) { 103 move(CMDLINE, 0); clrtoeol(); 104 if (*args == '\0') { 105 showprotos(); 106 showhosts(); 107 showports(); 108 return (1); 109 } 110 if (prefix(args, "protos")) 111 showprotos(); 112 else if (prefix(args, "hosts")) 113 showhosts(); 114 else if (prefix(args, "ports")) 115 showports(); 116 else 117 addstr("show what?"); 118 return (1); 119 } 120 return (0); 121 } 122 123 static void 124 changeitems(const char *args, int onoff) 125 { 126 char *cp, *tmpstr, *tmpstr1; 127 struct servent *sp; 128 struct hostent *hp; 129 struct in_addr in; 130 131 tmpstr = tmpstr1 = strdup(args); 132 cp = strchr(tmpstr1, '\n'); 133 if (cp) 134 *cp = '\0'; 135 for (;;tmpstr1 = cp) { 136 for (cp = tmpstr1; *cp && isspace(*cp); cp++) 137 ; 138 tmpstr1 = cp; 139 for (; *cp && !isspace(*cp); cp++) 140 ; 141 if (*cp) 142 *cp++ = '\0'; 143 if (cp - tmpstr1 == 0) 144 break; 145 sp = getservbyname(tmpstr1, 146 protos == TCP ? "tcp" : protos == UDP ? "udp" : 0); 147 if (sp) { 148 selectport(sp->s_port, onoff); 149 continue; 150 } 151 hp = gethostbyname(tmpstr1); 152 if (hp == NULL) { 153 in.s_addr = inet_addr(tmpstr1); 154 if (in.s_addr == INADDR_NONE) { 155 error("%s: unknown host or port", tmpstr1); 156 continue; 157 } 158 } else 159 in = *(struct in_addr *)hp->h_addr; 160 selecthost(&in, onoff); 161 } 162 free(tmpstr); 163 } 164 165 static int 166 selectproto(const char *proto) 167 { 168 169 if (proto == NULL || streq(proto, "all")) 170 protos = TCP | UDP; 171 else if (streq(proto, "tcp")) 172 protos = TCP; 173 else if (streq(proto, "udp")) 174 protos = UDP; 175 else 176 return (0); 177 178 return (protos); 179 } 180 181 static void 182 showprotos(void) 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(long port, int onoff) 200 { 201 struct pitem *p; 202 203 if (port == -1) { 204 if (ports == NULL) 205 return (0); 206 free((char *)ports), ports = 0; 207 nports = 0; 208 return (1); 209 } 210 for (p = ports; p < ports + nports; p++) 211 if (p->port == port) { 212 p->onoff = onoff; 213 return (0); 214 } 215 if (nports == 0) 216 ports = (struct pitem *)malloc(sizeof (*p)); 217 else 218 ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p)); 219 p = &ports[nports++]; 220 p->port = port; 221 p->onoff = onoff; 222 return (1); 223 } 224 225 int 226 checkport(struct in_conninfo *inc) 227 { 228 struct pitem *p; 229 230 if (ports) 231 for (p = ports; p < ports+nports; p++) 232 if (p->port == inc->inc_lport || p->port == inc->inc_fport) 233 return (p->onoff); 234 return (1); 235 } 236 237 static void 238 showports(void) 239 { 240 struct pitem *p; 241 struct servent *sp; 242 243 for (p = ports; p < ports+nports; p++) { 244 sp = getservbyport(p->port, 245 protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp"); 246 if (!p->onoff) 247 addch('!'); 248 if (sp) 249 printw("%s ", sp->s_name); 250 else 251 printw("%d ", p->port); 252 } 253 } 254 255 static int 256 selecthost(struct in_addr *in, int onoff) 257 { 258 struct hitem *p; 259 260 if (in == NULL) { 261 if (hosts == NULL) 262 return (0); 263 free((char *)hosts), hosts = 0; 264 nhosts = 0; 265 return (1); 266 } 267 for (p = hosts; p < hosts+nhosts; p++) 268 if (p->addr.s_addr == in->s_addr) { 269 p->onoff = onoff; 270 return (0); 271 } 272 if (nhosts == 0) 273 hosts = (struct hitem *)malloc(sizeof (*p)); 274 else 275 hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p)); 276 p = &hosts[nhosts++]; 277 p->addr = *in; 278 p->onoff = onoff; 279 return (1); 280 } 281 282 int 283 checkhost(struct in_conninfo *inc) 284 { 285 struct hitem *p; 286 287 if (hosts) 288 for (p = hosts; p < hosts+nhosts; p++) 289 if (p->addr.s_addr == inc->inc_laddr.s_addr || 290 p->addr.s_addr == inc->inc_faddr.s_addr) 291 return (p->onoff); 292 return (1); 293 } 294 295 static void 296 showhosts(void) 297 { 298 struct hitem *p; 299 struct hostent *hp; 300 301 for (p = hosts; p < hosts+nhosts; p++) { 302 hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET); 303 if (!p->onoff) 304 addch('!'); 305 printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr)); 306 } 307 } 308