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*25587Sbloom static char sccsid[] = "@(#)netcmds.c 5.2 (Berkeley) 12/11/85"; 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> 2421459Smckusick #include <netinet/in_pcb.h> 2521459Smckusick 2621459Smckusick #define streq(a,b) (strcmp(a,b)==0) 2721459Smckusick 2821459Smckusick netcmd(cmd, args) 2921459Smckusick char *cmd, *args; 3021459Smckusick { 3121459Smckusick 3221459Smckusick if (prefix(cmd, "tcp") || prefix(cmd, "udp")) { 3321459Smckusick selectproto(cmd); 3421459Smckusick return (1); 3521459Smckusick } 3621459Smckusick if (prefix(cmd, "ignore") || prefix(cmd, "display")) { 3721459Smckusick changeitems(args, prefix(cmd, "display")); 3821459Smckusick return (1); 3921459Smckusick } 4021459Smckusick if (prefix(cmd, "reset")) { 4121459Smckusick selectproto(0); 4221459Smckusick selecthost(0); 4321459Smckusick selectport(-1); 4421459Smckusick return (1); 4521459Smckusick } 4621459Smckusick if (prefix(cmd, "show")) { 4721459Smckusick move(CMDLINE, 0); clrtoeol(); 4821459Smckusick if (*args == '\0') { 4921459Smckusick showprotos(); 5021459Smckusick showhosts(); 5121459Smckusick showports(); 5221459Smckusick return (1); 5321459Smckusick } 5421459Smckusick if (prefix(args, "protos")) 5521459Smckusick showprotos(); 5621459Smckusick else if (prefix(args, "hosts")) 5721459Smckusick showhosts(); 5821459Smckusick else if (prefix(args, "ports")) 5921459Smckusick showports(); 6021459Smckusick else 6121459Smckusick addstr("show what?"); 6221459Smckusick return (1); 6321459Smckusick } 6421459Smckusick return (0); 6521459Smckusick } 6621459Smckusick 6721459Smckusick static 6821459Smckusick changeitems(args, onoff) 6921459Smckusick char *args; 7021459Smckusick int onoff; 7121459Smckusick { 7221459Smckusick register char *cp; 7321459Smckusick struct servent *sp; 7421459Smckusick struct hostent *hp; 7521459Smckusick struct in_addr in; 7621459Smckusick char *index(); 7721459Smckusick 7821459Smckusick cp = index(args, '\n'); 7921459Smckusick if (cp) 8021459Smckusick *cp = '\0'; 8121459Smckusick for (;;args = cp) { 8221459Smckusick for (cp = args; *cp && isspace(*cp); cp++) 8321459Smckusick ; 8421459Smckusick args = cp; 8521459Smckusick for (; *cp && !isspace(*cp); cp++) 8621459Smckusick ; 8721459Smckusick if (*cp) 8821459Smckusick *cp++ = '\0'; 8921459Smckusick if (cp - args == 0) 9021459Smckusick break; 9121459Smckusick sp = getservbyname(args, 9221459Smckusick protos == TCP ? "tcp" : protos == UDP ? "udp" : 0); 9321459Smckusick if (sp) { 9421459Smckusick selectport(sp->s_name, onoff); 9521459Smckusick continue; 9621459Smckusick } 9721459Smckusick hp = gethostbyname(args); 9821459Smckusick if (hp == 0) { 9921459Smckusick in.s_addr = inet_addr(args); 10021459Smckusick if (in.s_addr == -1) { 10121459Smckusick error("%s: unknown host or port", args); 10221459Smckusick continue; 10321459Smckusick } 10421459Smckusick } else 10521459Smckusick in = *(struct in_addr *)hp->h_addr; 10621459Smckusick selecthost(&in, onoff); 10721459Smckusick } 10821459Smckusick } 10921459Smckusick 11021459Smckusick static 11121459Smckusick selectproto(proto) 11221459Smckusick char *proto; 11321459Smckusick { 11421459Smckusick int new = protos; 11521459Smckusick 11621459Smckusick if (proto == 0 || streq(proto, "all")) 11721459Smckusick new = TCP|UDP; 11821459Smckusick else if (streq(proto, "tcp")) 11921459Smckusick new = TCP; 12021459Smckusick else if (streq(proto, "udp")) 12121459Smckusick new = UDP; 12221459Smckusick return (new != protos, protos = new); 12321459Smckusick } 12421459Smckusick 12521459Smckusick static 12621459Smckusick showprotos() 12721459Smckusick { 12821459Smckusick 12921459Smckusick if ((protos&TCP) == 0) 13021459Smckusick addch('!'); 13121459Smckusick addstr("tcp "); 13221459Smckusick if ((protos&UDP) == 0) 13321459Smckusick addch('!'); 13421459Smckusick addstr("udp "); 13521459Smckusick } 13621459Smckusick 13721459Smckusick static struct pitem { 13821459Smckusick long port; 13921459Smckusick int onoff; 14021459Smckusick } *ports; 14121459Smckusick 14221459Smckusick static 14321459Smckusick selectport(port, onoff) 14421459Smckusick long port; 14521459Smckusick int onoff; 14621459Smckusick { 147*25587Sbloom register struct pitem *p; 14821459Smckusick 14921459Smckusick if (port == -1) { 15021459Smckusick if (ports == 0) 15121459Smckusick return (0); 15221459Smckusick free((char *)ports), ports = 0; 15321459Smckusick nports = 0; 15421459Smckusick return (1); 15521459Smckusick } 15621459Smckusick for (p = ports; p < ports+nports; p++) 15721459Smckusick if (p->port == port) { 15821459Smckusick p->onoff = onoff; 15921459Smckusick return (0); 16021459Smckusick } 16121459Smckusick if (nports == 0) 16221459Smckusick ports = (struct pitem *)malloc(sizeof (*p)); 16321459Smckusick else 16421459Smckusick ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p)); 16521459Smckusick p = &ports[nports++]; 16621459Smckusick p->port = port; 16721459Smckusick p->onoff = onoff; 16821459Smckusick return (1); 16921459Smckusick } 17021459Smckusick 17121459Smckusick checkport(inp) 17221459Smckusick register struct inpcb *inp; 17321459Smckusick { 17421459Smckusick register struct pitem *p; 17521459Smckusick 17621459Smckusick if (ports) 17721459Smckusick for (p = ports; p < ports+nports; p++) 17821459Smckusick if (p->port == inp->inp_lport || p->port == inp->inp_fport) 17921459Smckusick return (p->onoff); 18021459Smckusick return (1); 18121459Smckusick } 18221459Smckusick 18321459Smckusick static 18421459Smckusick showports() 18521459Smckusick { 18621459Smckusick register struct pitem *p; 18721459Smckusick struct servent *sp; 18821459Smckusick 18921459Smckusick for (p = ports; p < ports+nports; p++) { 19021459Smckusick sp = getservbyport(p->port, 19121459Smckusick protos == TCP|UDP ? 0 : protos == TCP ? "tcp" : "udp"); 19221459Smckusick if (!p->onoff) 19321459Smckusick addch('!'); 19421459Smckusick if (sp) 19521459Smckusick printw("%s ", sp->s_name); 19621459Smckusick else 19721459Smckusick printw("%d ", p->port); 19821459Smckusick } 19921459Smckusick } 20021459Smckusick 20121459Smckusick static struct hitem { 20221459Smckusick struct in_addr addr; 20321459Smckusick int onoff; 20421459Smckusick } *hosts; 20521459Smckusick 20621459Smckusick static 20721459Smckusick selecthost(in, onoff) 20821459Smckusick struct in_addr *in; 20921459Smckusick { 21021459Smckusick register struct hitem *p; 21121459Smckusick 21221459Smckusick if (in == 0) { 21321459Smckusick if (hosts == 0) 21421459Smckusick return (0); 21521459Smckusick free((char *)hosts), hosts = 0; 21621459Smckusick nhosts = 0; 21721459Smckusick return (1); 21821459Smckusick } 21921459Smckusick for (p = hosts; p < hosts+nhosts; p++) 22021459Smckusick if (p->addr.s_addr == in->s_addr) { 22121459Smckusick p->onoff = onoff; 22221459Smckusick return (0); 22321459Smckusick } 22421459Smckusick if (nhosts == 0) 22521459Smckusick hosts = (struct hitem *)malloc(sizeof (*p)); 22621459Smckusick else 22721459Smckusick hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p)); 22821459Smckusick p = &hosts[nhosts++]; 22921459Smckusick p->addr = *in; 23021459Smckusick p->onoff = onoff; 23121459Smckusick return (1); 23221459Smckusick } 23321459Smckusick 23421459Smckusick checkhost(inp) 23521459Smckusick register struct inpcb *inp; 23621459Smckusick { 23721459Smckusick register struct hitem *p; 23821459Smckusick 23921459Smckusick if (hosts) 24021459Smckusick for (p = hosts; p < hosts+nhosts; p++) 24121459Smckusick if (p->addr.s_addr == inp->inp_laddr.s_addr || 24221459Smckusick p->addr.s_addr == inp->inp_faddr.s_addr) 24321459Smckusick return (p->onoff); 24421459Smckusick return (1); 24521459Smckusick } 24621459Smckusick 24721459Smckusick static 24821459Smckusick showhosts() 24921459Smckusick { 25021459Smckusick register struct hitem *p; 25121459Smckusick struct hostent *hp; 25221459Smckusick 25321459Smckusick for (p = hosts; p < hosts+nhosts; p++) { 25421459Smckusick hp = gethostbyaddr(&p->addr, sizeof (p->addr), AF_INET); 25521459Smckusick if (!p->onoff) 25621459Smckusick addch('!'); 25721459Smckusick printw("%s ", hp ? hp->h_name : inet_ntoa(p->addr)); 25821459Smckusick } 25921459Smckusick } 260