121460Smckusick /* 221460Smckusick * Copyright (c) 1980 Regents of the University of California. 321460Smckusick * All rights reserved. The Berkeley software License Agreement 421460Smckusick * specifies the terms and conditions for redistribution. 521460Smckusick */ 621460Smckusick 721459Smckusick #ifndef lint 8*46265Storek static char sccsid[] = "@(#)netcmds.c 5.5 (Berkeley) 02/04/91"; 921460Smckusick #endif not lint 1021459Smckusick 1121459Smckusick /* 1221459Smckusick * Common network command support routines. 1321459Smckusick */ 1421459Smckusick #include "systat.h" 1521459Smckusick #include <ctype.h> 1621459Smckusick 1721459Smckusick #include <sys/socket.h> 1821459Smckusick #include <sys/socketvar.h> 1921459Smckusick #include <sys/mbuf.h> 2021459Smckusick #include <sys/protosw.h> 2121459Smckusick 2221459Smckusick #include <net/route.h> 2321459Smckusick #include <netinet/in_systm.h> 2440891Ssklower #include <netinet/ip.h> 2521459Smckusick #include <netinet/in_pcb.h> 2621459Smckusick 2721459Smckusick #define streq(a,b) (strcmp(a,b)==0) 2821459Smckusick 29*46265Storek static void changeitems(), showprotos(), showports(), showhosts(); 30*46265Storek static int selectproto(), selectport(), selecthost(); 31*46265Storek 3221459Smckusick netcmd(cmd, args) 3321459Smckusick char *cmd, *args; 3421459Smckusick { 3521459Smckusick 3621459Smckusick if (prefix(cmd, "tcp") || prefix(cmd, "udp")) { 3721459Smckusick selectproto(cmd); 3821459Smckusick return (1); 3921459Smckusick } 4021459Smckusick if (prefix(cmd, "ignore") || prefix(cmd, "display")) { 4121459Smckusick changeitems(args, prefix(cmd, "display")); 4221459Smckusick return (1); 4321459Smckusick } 4421459Smckusick if (prefix(cmd, "reset")) { 4521459Smckusick selectproto(0); 4621459Smckusick selecthost(0); 4721459Smckusick selectport(-1); 4821459Smckusick return (1); 4921459Smckusick } 5021459Smckusick if (prefix(cmd, "show")) { 5121459Smckusick move(CMDLINE, 0); clrtoeol(); 5221459Smckusick if (*args == '\0') { 5321459Smckusick showprotos(); 5421459Smckusick showhosts(); 5521459Smckusick showports(); 5621459Smckusick return (1); 5721459Smckusick } 5821459Smckusick if (prefix(args, "protos")) 5921459Smckusick showprotos(); 6021459Smckusick else if (prefix(args, "hosts")) 6121459Smckusick showhosts(); 6221459Smckusick else if (prefix(args, "ports")) 6321459Smckusick showports(); 6421459Smckusick else 6521459Smckusick addstr("show what?"); 6621459Smckusick return (1); 6721459Smckusick } 6821459Smckusick return (0); 6921459Smckusick } 7021459Smckusick 71*46265Storek static void 7221459Smckusick changeitems(args, onoff) 7321459Smckusick char *args; 7421459Smckusick int onoff; 7521459Smckusick { 7621459Smckusick register char *cp; 7721459Smckusick struct servent *sp; 7821459Smckusick struct hostent *hp; 7921459Smckusick struct in_addr in; 8021459Smckusick char *index(); 8121459Smckusick 8221459Smckusick cp = index(args, '\n'); 8321459Smckusick if (cp) 8421459Smckusick *cp = '\0'; 8521459Smckusick for (;;args = cp) { 8621459Smckusick for (cp = args; *cp && isspace(*cp); cp++) 8721459Smckusick ; 8821459Smckusick args = cp; 8921459Smckusick for (; *cp && !isspace(*cp); cp++) 9021459Smckusick ; 9121459Smckusick if (*cp) 9221459Smckusick *cp++ = '\0'; 9321459Smckusick if (cp - args == 0) 9421459Smckusick break; 9521459Smckusick sp = getservbyname(args, 9621459Smckusick protos == TCP ? "tcp" : protos == UDP ? "udp" : 0); 9721459Smckusick if (sp) { 9832188Sbostic selectport(sp->s_port, onoff); 9921459Smckusick continue; 10021459Smckusick } 10121459Smckusick hp = gethostbyname(args); 10221459Smckusick if (hp == 0) { 10321459Smckusick in.s_addr = inet_addr(args); 10421459Smckusick if (in.s_addr == -1) { 10521459Smckusick error("%s: unknown host or port", args); 10621459Smckusick continue; 10721459Smckusick } 10821459Smckusick } else 10921459Smckusick in = *(struct in_addr *)hp->h_addr; 11021459Smckusick selecthost(&in, onoff); 11121459Smckusick } 11221459Smckusick } 11321459Smckusick 114*46265Storek static int 11521459Smckusick selectproto(proto) 11621459Smckusick char *proto; 11721459Smckusick { 11821459Smckusick int new = protos; 11921459Smckusick 12021459Smckusick if (proto == 0 || streq(proto, "all")) 12121459Smckusick new = TCP|UDP; 12221459Smckusick else if (streq(proto, "tcp")) 12321459Smckusick new = TCP; 12421459Smckusick else if (streq(proto, "udp")) 12521459Smckusick new = UDP; 12621459Smckusick return (new != protos, protos = new); 12721459Smckusick } 12821459Smckusick 129*46265Storek static void 13021459Smckusick showprotos() 13121459Smckusick { 13221459Smckusick 13321459Smckusick if ((protos&TCP) == 0) 13421459Smckusick addch('!'); 13521459Smckusick addstr("tcp "); 13621459Smckusick if ((protos&UDP) == 0) 13721459Smckusick addch('!'); 13821459Smckusick addstr("udp "); 13921459Smckusick } 14021459Smckusick 14121459Smckusick static struct pitem { 14221459Smckusick long port; 14321459Smckusick int onoff; 14421459Smckusick } *ports; 14521459Smckusick 146*46265Storek static int 14721459Smckusick selectport(port, onoff) 14821459Smckusick long port; 14921459Smckusick int onoff; 15021459Smckusick { 15125587Sbloom register struct pitem *p; 15221459Smckusick 15321459Smckusick if (port == -1) { 15421459Smckusick if (ports == 0) 15521459Smckusick return (0); 15621459Smckusick free((char *)ports), ports = 0; 15721459Smckusick nports = 0; 15821459Smckusick return (1); 15921459Smckusick } 16021459Smckusick for (p = ports; p < ports+nports; p++) 16121459Smckusick if (p->port == port) { 16221459Smckusick p->onoff = onoff; 16321459Smckusick return (0); 16421459Smckusick } 16521459Smckusick if (nports == 0) 16621459Smckusick ports = (struct pitem *)malloc(sizeof (*p)); 16721459Smckusick else 16821459Smckusick ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p)); 16921459Smckusick p = &ports[nports++]; 17021459Smckusick p->port = port; 17121459Smckusick p->onoff = onoff; 17221459Smckusick return (1); 17321459Smckusick } 17421459Smckusick 17521459Smckusick checkport(inp) 17621459Smckusick register struct inpcb *inp; 17721459Smckusick { 17821459Smckusick register struct pitem *p; 17921459Smckusick 18021459Smckusick if (ports) 18121459Smckusick for (p = ports; p < ports+nports; p++) 18221459Smckusick if (p->port == inp->inp_lport || p->port == inp->inp_fport) 18321459Smckusick return (p->onoff); 18421459Smckusick return (1); 18521459Smckusick } 18621459Smckusick 187*46265Storek static void 18821459Smckusick showports() 18921459Smckusick { 19021459Smckusick register struct pitem *p; 19121459Smckusick struct servent *sp; 19221459Smckusick 19321459Smckusick for (p = ports; p < ports+nports; p++) { 19421459Smckusick sp = getservbyport(p->port, 19521459Smckusick protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp"); 19621459Smckusick if (!p->onoff) 19721459Smckusick addch('!'); 19821459Smckusick if (sp) 19921459Smckusick printw("%s ", sp->s_name); 20021459Smckusick else 20121459Smckusick printw("%d ", p->port); 20221459Smckusick } 20321459Smckusick } 20421459Smckusick 20521459Smckusick static struct hitem { 20621459Smckusick struct in_addr addr; 20721459Smckusick int onoff; 20821459Smckusick } *hosts; 20921459Smckusick 210*46265Storek static int 21121459Smckusick selecthost(in, onoff) 21221459Smckusick struct in_addr *in; 213*46265Storek int onoff; 21421459Smckusick { 21521459Smckusick register struct hitem *p; 21621459Smckusick 21721459Smckusick if (in == 0) { 21821459Smckusick if (hosts == 0) 21921459Smckusick return (0); 22021459Smckusick free((char *)hosts), hosts = 0; 22121459Smckusick nhosts = 0; 22221459Smckusick return (1); 22321459Smckusick } 22421459Smckusick for (p = hosts; p < hosts+nhosts; p++) 22521459Smckusick if (p->addr.s_addr == in->s_addr) { 22621459Smckusick p->onoff = onoff; 22721459Smckusick return (0); 22821459Smckusick } 22921459Smckusick if (nhosts == 0) 23021459Smckusick hosts = (struct hitem *)malloc(sizeof (*p)); 23121459Smckusick else 23221459Smckusick hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p)); 23321459Smckusick p = &hosts[nhosts++]; 23421459Smckusick p->addr = *in; 23521459Smckusick p->onoff = onoff; 23621459Smckusick return (1); 23721459Smckusick } 23821459Smckusick 23921459Smckusick checkhost(inp) 24021459Smckusick register struct inpcb *inp; 24121459Smckusick { 24221459Smckusick register struct hitem *p; 24321459Smckusick 24421459Smckusick if (hosts) 24521459Smckusick for (p = hosts; p < hosts+nhosts; p++) 24621459Smckusick if (p->addr.s_addr == inp->inp_laddr.s_addr || 24721459Smckusick p->addr.s_addr == inp->inp_faddr.s_addr) 24821459Smckusick return (p->onoff); 24921459Smckusick return (1); 25021459Smckusick } 25121459Smckusick 252*46265Storek static void 25321459Smckusick showhosts() 25421459Smckusick { 25521459Smckusick register struct hitem *p; 25621459Smckusick struct hostent *hp; 25721459Smckusick 25821459Smckusick for (p = hosts; p < hosts+nhosts; p++) { 25921459Smckusick hp = gethostbyaddr(&p->addr, sizeof (p->addr), AF_INET); 26021459Smckusick if (!p->onoff) 26121459Smckusick addch('!'); 26221459Smckusick printw("%s ", hp ? hp->h_name : inet_ntoa(p->addr)); 26321459Smckusick } 26421459Smckusick } 265