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