[BACK]Return to netcmds.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / systat

Annotation of src/usr.bin/systat/netcmds.c, Revision 1.4

1.4     ! millert     1: /*     $OpenBSD: netcmds.c,v 1.3 1997/01/17 07:13:25 millert Exp $     */
1.1       deraadt     2: /*     $NetBSD: netcmds.c,v 1.4 1995/05/21 17:14:38 mycroft Exp $      */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1980, 1992, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: #if 0
                     39: static char sccsid[] = "@(#)netcmds.c  8.1 (Berkeley) 6/6/93";
                     40: #endif
1.4     ! millert    41: static char rcsid[] = "$OpenBSD: netcmds.c,v 1.3 1997/01/17 07:13:25 millert Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
                     44: /*
                     45:  * Common network command support routines.
                     46:  */
                     47: #include <sys/param.h>
                     48: #include <sys/socket.h>
                     49: #include <sys/socketvar.h>
                     50: #include <sys/mbuf.h>
                     51: #include <sys/protosw.h>
                     52:
                     53: #include <net/route.h>
                     54: #include <netinet/in.h>
                     55: #include <netinet/in_systm.h>
                     56: #include <netinet/ip.h>
                     57: #include <netinet/in_pcb.h>
                     58:
                     59: #include <arpa/inet.h>
                     60:
                     61: #include <netdb.h>
                     62: #include <stdlib.h>
                     63: #include <string.h>
                     64: #include <ctype.h>
                     65: #include "systat.h"
                     66: #include "extern.h"
                     67:
                     68: #define        streq(a,b)      (strcmp(a,b)==0)
                     69:
                     70: static struct hitem {
                     71:        struct  in_addr addr;
                     72:        int     onoff;
                     73: } *hosts;
                     74:
                     75: int nports, nhosts, protos;
                     76:
                     77: static void changeitems __P((char *, int));
                     78: static int selectproto __P((char *));
                     79: static void showprotos __P((void));
                     80: static int selectport __P((long, int));
                     81: static void showports __P((void));
                     82: static int selecthost __P((struct in_addr *, int));
                     83: static void showhosts __P((void));
                     84:
                     85: int
                     86: netcmd(cmd, args)
                     87:        char *cmd, *args;
                     88: {
                     89:
                     90:        if (prefix(cmd, "tcp") || prefix(cmd, "udp")) {
                     91:                selectproto(cmd);
                     92:                return (1);
                     93:        }
                     94:        if (prefix(cmd, "ignore") || prefix(cmd, "display")) {
                     95:                changeitems(args, prefix(cmd, "display"));
                     96:                return (1);
                     97:        }
                     98:        if (prefix(cmd, "reset")) {
                     99:                selectproto(0);
                    100:                selecthost(0, 0);
                    101:                selectport(-1, 0);
                    102:                return (1);
                    103:        }
                    104:        if (prefix(cmd, "show")) {
                    105:                move(CMDLINE, 0); clrtoeol();
                    106:                if (*args == '\0') {
                    107:                        showprotos();
                    108:                        showhosts();
                    109:                        showports();
                    110:                        return (1);
                    111:                }
                    112:                if (prefix(args, "protos"))
                    113:                        showprotos();
                    114:                else if (prefix(args, "hosts"))
                    115:                        showhosts();
                    116:                else if (prefix(args, "ports"))
                    117:                        showports();
                    118:                else
                    119:                        addstr("show what?");
                    120:                return (1);
                    121:        }
                    122:        return (0);
                    123: }
                    124:
                    125:
                    126: static void
                    127: changeitems(args, onoff)
                    128:        char *args;
                    129:        int onoff;
                    130: {
                    131:        register char *cp;
                    132:        struct servent *sp;
                    133:        struct hostent *hp;
                    134:        struct in_addr in;
                    135:
1.3       millert   136:        cp = strchr(args, '\n');
1.1       deraadt   137:        if (cp)
                    138:                *cp = '\0';
                    139:        for (;;args = cp) {
                    140:                for (cp = args; *cp && isspace(*cp); cp++)
                    141:                        ;
                    142:                args = cp;
                    143:                for (; *cp && !isspace(*cp); cp++)
                    144:                        ;
                    145:                if (*cp)
                    146:                        *cp++ = '\0';
                    147:                if (cp - args == 0)
                    148:                        break;
                    149:                sp = getservbyname(args,
                    150:                    protos == TCP ? "tcp" : protos == UDP ? "udp" : 0);
                    151:                if (sp) {
                    152:                        selectport(sp->s_port, onoff);
                    153:                        continue;
                    154:                }
                    155:                if (inet_aton(args, &in) == 0) {
                    156:                        hp = gethostbyname(args);
                    157:                        if (hp == 0) {
                    158:                                error("%s: unknown host or port", args);
                    159:                                continue;
                    160:                        }
                    161:                        memcpy(&in, hp->h_addr, hp->h_length);
                    162:                }
                    163:                selecthost(&in, onoff);
                    164:        }
                    165: }
                    166:
                    167: static int
                    168: selectproto(proto)
                    169:        char *proto;
                    170: {
                    171:        int new = protos;
                    172:
                    173:        if (proto == 0 || streq(proto, "all"))
                    174:                new = TCP|UDP;
                    175:        else if (streq(proto, "tcp"))
                    176:                new = TCP;
                    177:        else if (streq(proto, "udp"))
                    178:                new = UDP;
1.4     ! millert   179:        return (protos = new);
1.1       deraadt   180: }
                    181:
                    182: static void
                    183: showprotos()
                    184: {
                    185:
                    186:        if ((protos&TCP) == 0)
                    187:                addch('!');
                    188:        addstr("tcp ");
                    189:        if ((protos&UDP) == 0)
                    190:                addch('!');
                    191:        addstr("udp ");
                    192: }
                    193:
                    194: static struct pitem {
                    195:        long    port;
                    196:        int     onoff;
                    197: } *ports;
                    198:
                    199: static int
                    200: selectport(port, onoff)
                    201:        long port;
                    202:        int onoff;
                    203: {
                    204:        register struct pitem *p;
                    205:
                    206:        if (port == -1) {
                    207:                if (ports == 0)
                    208:                        return (0);
                    209:                free((char *)ports), ports = 0;
                    210:                nports = 0;
                    211:                return (1);
                    212:        }
                    213:        for (p = ports; p < ports+nports; p++)
                    214:                if (p->port == port) {
                    215:                        p->onoff = onoff;
                    216:                        return (0);
                    217:                }
                    218:        if (nports == 0)
                    219:                ports = (struct pitem *)malloc(sizeof (*p));
                    220:        else
                    221:                ports = (struct pitem *)realloc(ports, (nports+1)*sizeof (*p));
                    222:        p = &ports[nports++];
                    223:        p->port = port;
                    224:        p->onoff = onoff;
                    225:        return (1);
                    226: }
                    227:
                    228: int
                    229: checkport(inp)
                    230:        register struct inpcb *inp;
                    231: {
                    232:        register struct pitem *p;
                    233:
                    234:        if (ports)
                    235:        for (p = ports; p < ports+nports; p++)
                    236:                if (p->port == inp->inp_lport || p->port == inp->inp_fport)
                    237:                        return (p->onoff);
                    238:        return (1);
                    239: }
                    240:
                    241: static void
                    242: showports()
                    243: {
                    244:        register struct pitem *p;
                    245:        struct servent *sp;
                    246:
                    247:        for (p = ports; p < ports+nports; p++) {
                    248:                sp = getservbyport(p->port,
1.4     ! millert   249:                    protos == (TCP|UDP) ? 0 : protos == TCP ? "tcp" : "udp");
1.1       deraadt   250:                if (!p->onoff)
                    251:                        addch('!');
                    252:                if (sp)
                    253:                        printw("%s ", sp->s_name);
                    254:                else
                    255:                        printw("%d ", p->port);
                    256:        }
                    257: }
                    258:
                    259: static int
                    260: selecthost(in, onoff)
                    261:        struct in_addr *in;
                    262:        int onoff;
                    263: {
                    264:        register struct hitem *p;
                    265:
                    266:        if (in == 0) {
                    267:                if (hosts == 0)
                    268:                        return (0);
                    269:                free((char *)hosts), hosts = 0;
                    270:                nhosts = 0;
                    271:                return (1);
                    272:        }
                    273:        for (p = hosts; p < hosts+nhosts; p++)
                    274:                if (p->addr.s_addr == in->s_addr) {
                    275:                        p->onoff = onoff;
                    276:                        return (0);
                    277:                }
                    278:        if (nhosts == 0)
                    279:                hosts = (struct hitem *)malloc(sizeof (*p));
                    280:        else
                    281:                hosts = (struct hitem *)realloc(hosts, (nhosts+1)*sizeof (*p));
                    282:        p = &hosts[nhosts++];
                    283:        p->addr = *in;
                    284:        p->onoff = onoff;
                    285:        return (1);
                    286: }
                    287:
                    288: int
                    289: checkhost(inp)
                    290:        register struct inpcb *inp;
                    291: {
                    292:        register struct hitem *p;
                    293:
                    294:        if (hosts)
                    295:        for (p = hosts; p < hosts+nhosts; p++)
                    296:                if (p->addr.s_addr == inp->inp_laddr.s_addr ||
                    297:                    p->addr.s_addr == inp->inp_faddr.s_addr)
                    298:                        return (p->onoff);
                    299:        return (1);
                    300: }
                    301:
                    302: static void
                    303: showhosts()
                    304: {
                    305:        register struct hitem *p;
                    306:        struct hostent *hp;
                    307:
                    308:        for (p = hosts; p < hosts+nhosts; p++) {
                    309:                hp = gethostbyaddr((char *)&p->addr, sizeof (p->addr), AF_INET);
                    310:                if (!p->onoff)
                    311:                        addch('!');
                    312:                printw("%s ", hp ? hp->h_name : inet_ntoa(p->addr));
                    313:        }
                    314: }