19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1980, 1992, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 353e1c3ca6SGarrett Wollman /* 369b50d902SRodney W. Grimes static char sccsid[] = "@(#)netcmds.c 8.1 (Berkeley) 6/6/93"; 373e1c3ca6SGarrett Wollman */ 383e1c3ca6SGarrett Wollman static const char rcsid[] = 399ec49abdSPeter Wemm "$Id: netcmds.c,v 1.7 1997/12/04 03:44:44 steve Exp $"; 409b50d902SRodney W. Grimes #endif /* not lint */ 419b50d902SRodney W. Grimes 429b50d902SRodney W. Grimes /* 439b50d902SRodney W. Grimes * Common network command support routines. 449b50d902SRodney W. Grimes */ 459b50d902SRodney W. Grimes #include <sys/param.h> 46d8d89152SDavid Greenman #include <sys/queue.h> 479b50d902SRodney W. Grimes #include <sys/socket.h> 489b50d902SRodney W. Grimes #include <sys/socketvar.h> 499b50d902SRodney W. Grimes #include <sys/protosw.h> 509b50d902SRodney W. Grimes 519b50d902SRodney W. Grimes #include <net/route.h> 529b50d902SRodney W. Grimes #include <netinet/in.h> 539b50d902SRodney W. Grimes #include <netinet/in_systm.h> 549b50d902SRodney W. Grimes #include <netinet/ip.h> 559b50d902SRodney W. Grimes #include <netinet/in_pcb.h> 569ec49abdSPeter Wemm #include <arpa/inet.h> 579b50d902SRodney W. Grimes 589b50d902SRodney W. Grimes #include <netdb.h> 599b50d902SRodney W. Grimes #include <stdlib.h> 609b50d902SRodney W. Grimes #include <string.h> 619b50d902SRodney W. Grimes #include <ctype.h> 629b50d902SRodney W. Grimes #include "systat.h" 639b50d902SRodney W. Grimes #include "extern.h" 649b50d902SRodney W. Grimes 659b50d902SRodney W. Grimes #define streq(a,b) (strcmp(a,b)==0) 669b50d902SRodney W. Grimes 679b50d902SRodney W. Grimes static struct hitem { 689b50d902SRodney W. Grimes struct in_addr addr; 699b50d902SRodney W. Grimes int onoff; 709b50d902SRodney W. Grimes } *hosts; 719b50d902SRodney W. Grimes 729b50d902SRodney W. Grimes int nports, nhosts, protos; 739b50d902SRodney W. Grimes 749b50d902SRodney W. Grimes static void changeitems __P((char *, int)); 759b50d902SRodney W. Grimes static int selectproto __P((char *)); 769b50d902SRodney W. Grimes static void showprotos __P((void)); 779b50d902SRodney W. Grimes static int selectport __P((long, int)); 789b50d902SRodney W. Grimes static void showports __P((void)); 799b50d902SRodney W. Grimes static int selecthost __P((struct in_addr *, int)); 809b50d902SRodney W. Grimes static void showhosts __P((void)); 819b50d902SRodney W. Grimes 829b50d902SRodney W. Grimes int 839b50d902SRodney W. Grimes netcmd(cmd, args) 849b50d902SRodney W. Grimes char *cmd, *args; 859b50d902SRodney W. Grimes { 869b50d902SRodney W. Grimes 8779431394SSteve Price if (prefix(cmd, "proto")) { 8879431394SSteve Price if (*args == '\0') { 8979431394SSteve Price move(CMDLINE, 0); 9079431394SSteve Price clrtoeol(); 9179431394SSteve Price addstr("which proto?"); 9279431394SSteve Price } else if (!selectproto(args)) { 9379431394SSteve Price error("%s: Unknown protocol.", args); 9479431394SSteve Price } 959b50d902SRodney W. Grimes return (1); 969b50d902SRodney W. Grimes } 979b50d902SRodney W. Grimes if (prefix(cmd, "ignore") || prefix(cmd, "display")) { 989b50d902SRodney W. Grimes changeitems(args, prefix(cmd, "display")); 999b50d902SRodney W. Grimes return (1); 1009b50d902SRodney W. Grimes } 1019b50d902SRodney W. Grimes if (prefix(cmd, "reset")) { 1029b50d902SRodney W. Grimes selectproto(0); 1039b50d902SRodney W. Grimes selecthost(0, 0); 1049b50d902SRodney W. Grimes selectport(-1, 0); 1059b50d902SRodney W. Grimes return (1); 1069b50d902SRodney W. Grimes } 1079b50d902SRodney W. Grimes if (prefix(cmd, "show")) { 1089b50d902SRodney W. Grimes move(CMDLINE, 0); clrtoeol(); 1099b50d902SRodney W. Grimes if (*args == '\0') { 1109b50d902SRodney W. Grimes showprotos(); 1119b50d902SRodney W. Grimes showhosts(); 1129b50d902SRodney W. Grimes showports(); 1139b50d902SRodney W. Grimes return (1); 1149b50d902SRodney W. Grimes } 1159b50d902SRodney W. Grimes if (prefix(args, "protos")) 1169b50d902SRodney W. Grimes showprotos(); 1179b50d902SRodney W. Grimes else if (prefix(args, "hosts")) 1189b50d902SRodney W. Grimes showhosts(); 1199b50d902SRodney W. Grimes else if (prefix(args, "ports")) 1209b50d902SRodney W. Grimes showports(); 1219b50d902SRodney W. Grimes else 1229b50d902SRodney W. Grimes addstr("show what?"); 1239b50d902SRodney W. Grimes return (1); 1249b50d902SRodney W. Grimes } 1259b50d902SRodney W. Grimes return (0); 1269b50d902SRodney W. Grimes } 1279b50d902SRodney W. Grimes 1289b50d902SRodney W. Grimes 1299b50d902SRodney W. Grimes static void 1309b50d902SRodney W. Grimes changeitems(args, onoff) 1319b50d902SRodney W. Grimes char *args; 1329b50d902SRodney W. Grimes int onoff; 1339b50d902SRodney W. Grimes { 1349b50d902SRodney W. Grimes register char *cp; 1359b50d902SRodney W. Grimes struct servent *sp; 1369b50d902SRodney W. Grimes struct hostent *hp; 1379b50d902SRodney W. Grimes struct in_addr in; 1389b50d902SRodney W. Grimes char *index(); 1399b50d902SRodney W. Grimes 1409b50d902SRodney W. Grimes cp = index(args, '\n'); 1419b50d902SRodney W. Grimes if (cp) 1429b50d902SRodney W. Grimes *cp = '\0'; 1439b50d902SRodney W. Grimes for (;;args = cp) { 1449b50d902SRodney W. Grimes for (cp = args; *cp && isspace(*cp); cp++) 1459b50d902SRodney W. Grimes ; 1469b50d902SRodney W. Grimes args = cp; 1479b50d902SRodney W. Grimes for (; *cp && !isspace(*cp); cp++) 1489b50d902SRodney W. Grimes ; 1499b50d902SRodney W. Grimes if (*cp) 1509b50d902SRodney W. Grimes *cp++ = '\0'; 1519b50d902SRodney W. Grimes if (cp - args == 0) 1529b50d902SRodney W. Grimes break; 1539b50d902SRodney W. Grimes sp = getservbyname(args, 1549b50d902SRodney W. Grimes protos == TCP ? "tcp" : protos == UDP ? "udp" : 0); 1559b50d902SRodney W. Grimes if (sp) { 1569b50d902SRodney W. Grimes selectport(sp->s_port, onoff); 1579b50d902SRodney W. Grimes continue; 1589b50d902SRodney W. Grimes } 1599b50d902SRodney W. Grimes hp = gethostbyname(args); 1609b50d902SRodney W. Grimes if (hp == 0) { 1619b50d902SRodney W. Grimes in.s_addr = inet_addr(args); 1629b50d902SRodney W. Grimes if (in.s_addr == -1) { 1639b50d902SRodney W. Grimes error("%s: unknown host or port", args); 1649b50d902SRodney W. Grimes continue; 1659b50d902SRodney W. Grimes } 1669b50d902SRodney W. Grimes } else 1679b50d902SRodney W. Grimes in = *(struct in_addr *)hp->h_addr; 1689b50d902SRodney W. Grimes selecthost(&in, onoff); 1699b50d902SRodney W. Grimes } 1709b50d902SRodney W. Grimes } 1719b50d902SRodney W. Grimes 1729b50d902SRodney W. Grimes static int 1739b50d902SRodney W. Grimes selectproto(proto) 1749b50d902SRodney W. Grimes char *proto; 1759b50d902SRodney W. Grimes { 1769b50d902SRodney W. Grimes 1779b50d902SRodney W. Grimes if (proto == 0 || streq(proto, "all")) 17879431394SSteve Price protos = TCP | UDP; 1799b50d902SRodney W. Grimes else if (streq(proto, "tcp")) 18079431394SSteve Price protos = TCP; 1819b50d902SRodney W. Grimes else if (streq(proto, "udp")) 18279431394SSteve Price protos = UDP; 18379431394SSteve Price else 18479431394SSteve Price return (0); 18579431394SSteve Price 18679431394SSteve Price return (protos); 1879b50d902SRodney W. Grimes } 1889b50d902SRodney W. Grimes 1899b50d902SRodney W. Grimes static void 1909b50d902SRodney W. Grimes showprotos() 1919b50d902SRodney W. Grimes { 1929b50d902SRodney W. Grimes 1939b50d902SRodney W. Grimes if ((protos&TCP) == 0) 1949b50d902SRodney W. Grimes addch('!'); 1959b50d902SRodney W. Grimes addstr("tcp "); 1969b50d902SRodney W. Grimes if ((protos&UDP) == 0) 1979b50d902SRodney W. Grimes addch('!'); 1989b50d902SRodney W. Grimes addstr("udp "); 1999b50d902SRodney W. Grimes } 2009b50d902SRodney W. Grimes 2019b50d902SRodney W. Grimes static struct pitem { 2029b50d902SRodney W. Grimes long port; 2039b50d902SRodney W. Grimes int onoff; 2049b50d902SRodney W. Grimes } *ports; 2059b50d902SRodney W. Grimes 2069b50d902SRodney W. Grimes static int 2079b50d902SRodney W. Grimes selectport(port, onoff) 2089b50d902SRodney W. Grimes long port; 2099b50d902SRodney W. Grimes int onoff; 2109b50d902SRodney W. Grimes { 2119b50d902SRodney W. Grimes register struct pitem *p; 2129b50d902SRodney W. Grimes 2139b50d902SRodney W. Grimes if (port == -1) { 2149b50d902SRodney W. Grimes if (ports == 0) 2159b50d902SRodney W. Grimes return (0); 2169b50d902SRodney W. Grimes free((char *)ports), ports = 0; 2179b50d902SRodney W. Grimes nports = 0; 2189b50d902SRodney W. Grimes return (1); 2199b50d902SRodney W. Grimes } 2209b50d902SRodney W. Grimes for (p = ports; p < ports+nports; p++) 2219b50d902SRodney W. Grimes if (p->port == port) { 2229b50d902SRodney W. Grimes p->onoff = onoff; 2239b50d902SRodney W. Grimes return (0); 2249b50d902SRodney W. Grimes } 2259b50d902SRodney W. Grimes if (nports == 0) 2269b50d902SRodney W. Grimes ports = (struct pitem *)malloc(sizeof (*p)); 2279b50d902SRodney W. Grimes else 2289b50d902SRodney W. Grimes ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p)); 2299b50d902SRodney W. Grimes p = &ports[nports++]; 2309b50d902SRodney W. Grimes p->port = port; 2319b50d902SRodney W. Grimes p->onoff = onoff; 2329b50d902SRodney W. Grimes return (1); 2339b50d902SRodney W. Grimes } 2349b50d902SRodney W. Grimes 2359b50d902SRodney W. Grimes int 2369b50d902SRodney W. Grimes checkport(inp) 2379b50d902SRodney W. Grimes register struct inpcb *inp; 2389b50d902SRodney W. Grimes { 2399b50d902SRodney W. Grimes register struct pitem *p; 2409b50d902SRodney W. Grimes 2419b50d902SRodney W. Grimes if (ports) 2429b50d902SRodney W. Grimes for (p = ports; p < ports+nports; p++) 2439b50d902SRodney W. Grimes if (p->port == inp->inp_lport || p->port == inp->inp_fport) 2449b50d902SRodney W. Grimes return (p->onoff); 2459b50d902SRodney W. Grimes return (1); 2469b50d902SRodney W. Grimes } 2479b50d902SRodney W. Grimes 2489b50d902SRodney W. Grimes static void 2499b50d902SRodney W. Grimes showports() 2509b50d902SRodney W. Grimes { 2519b50d902SRodney W. Grimes register struct pitem *p; 2529b50d902SRodney W. Grimes struct servent *sp; 2539b50d902SRodney W. Grimes 2549b50d902SRodney W. Grimes for (p = ports; p < ports+nports; p++) { 2559b50d902SRodney W. Grimes sp = getservbyport(p->port, 2569b50d902SRodney W. Grimes protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp"); 2579b50d902SRodney W. Grimes if (!p->onoff) 2589b50d902SRodney W. Grimes addch('!'); 2599b50d902SRodney W. Grimes if (sp) 2609b50d902SRodney W. Grimes printw("%s ", sp->s_name); 2619b50d902SRodney W. Grimes else 2629b50d902SRodney W. Grimes printw("%d ", p->port); 2639b50d902SRodney W. Grimes } 2649b50d902SRodney W. Grimes } 2659b50d902SRodney W. Grimes 2669b50d902SRodney W. Grimes static int 2679b50d902SRodney W. Grimes selecthost(in, onoff) 2689b50d902SRodney W. Grimes struct in_addr *in; 2699b50d902SRodney W. Grimes int onoff; 2709b50d902SRodney W. Grimes { 2719b50d902SRodney W. Grimes register struct hitem *p; 2729b50d902SRodney W. Grimes 2739b50d902SRodney W. Grimes if (in == 0) { 2749b50d902SRodney W. Grimes if (hosts == 0) 2759b50d902SRodney W. Grimes return (0); 2769b50d902SRodney W. Grimes free((char *)hosts), hosts = 0; 2779b50d902SRodney W. Grimes nhosts = 0; 2789b50d902SRodney W. Grimes return (1); 2799b50d902SRodney W. Grimes } 2809b50d902SRodney W. Grimes for (p = hosts; p < hosts+nhosts; p++) 2819b50d902SRodney W. Grimes if (p->addr.s_addr == in->s_addr) { 2829b50d902SRodney W. Grimes p->onoff = onoff; 2839b50d902SRodney W. Grimes return (0); 2849b50d902SRodney W. Grimes } 2859b50d902SRodney W. Grimes if (nhosts == 0) 2869b50d902SRodney W. Grimes hosts = (struct hitem *)malloc(sizeof (*p)); 2879b50d902SRodney W. Grimes else 2889b50d902SRodney W. Grimes hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p)); 2899b50d902SRodney W. Grimes p = &hosts[nhosts++]; 2909b50d902SRodney W. Grimes p->addr = *in; 2919b50d902SRodney W. Grimes p->onoff = onoff; 2929b50d902SRodney W. Grimes return (1); 2939b50d902SRodney W. Grimes } 2949b50d902SRodney W. Grimes 2959b50d902SRodney W. Grimes int 2969b50d902SRodney W. Grimes checkhost(inp) 2979b50d902SRodney W. Grimes register struct inpcb *inp; 2989b50d902SRodney W. Grimes { 2999b50d902SRodney W. Grimes register struct hitem *p; 3009b50d902SRodney W. Grimes 3019b50d902SRodney W. Grimes if (hosts) 3029b50d902SRodney W. Grimes for (p = hosts; p < hosts+nhosts; p++) 3039b50d902SRodney W. Grimes if (p->addr.s_addr == inp->inp_laddr.s_addr || 3049b50d902SRodney W. Grimes p->addr.s_addr == inp->inp_faddr.s_addr) 3059b50d902SRodney W. Grimes return (p->onoff); 3069b50d902SRodney W. Grimes return (1); 3079b50d902SRodney W. Grimes } 3089b50d902SRodney W. Grimes 3099b50d902SRodney W. Grimes static void 3109b50d902SRodney W. Grimes showhosts() 3119b50d902SRodney W. Grimes { 3129b50d902SRodney W. Grimes register struct hitem *p; 3139b50d902SRodney W. Grimes struct hostent *hp; 3149b50d902SRodney W. Grimes 3159b50d902SRodney W. Grimes for (p = hosts; p < hosts+nhosts; p++) { 3169b50d902SRodney W. Grimes hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET); 3179b50d902SRodney W. Grimes if (!p->onoff) 3189b50d902SRodney W. Grimes addch('!'); 3199b50d902SRodney W. Grimes printw("%s ", hp ? hp->h_name : (char *)inet_ntoa(p->addr)); 3209b50d902SRodney W. Grimes } 3219b50d902SRodney W. Grimes } 322