tfixes - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit a38a183626848f897085b4d2e54dad6d39146948
 (DIR) parent 2eef1fa31676e58b0038f1c02dcc8df5e23149e6
 (HTM) Author: rsc <devnull@localhost>
       Date:   Tue, 14 Feb 2006 19:42:28 +0000
       
       fixes
       
       Diffstat:
         M src/cmd/ip/snoopy/arp.c             |       2 ++
         M src/cmd/ip/snoopy/bootp.c           |       1 +
         M src/cmd/ip/snoopy/dat.h             |       1 +
         M src/cmd/ip/snoopy/dhcp.c            |      13 +++----------
         M src/cmd/ip/snoopy/dump.c            |      19 +++----------------
         M src/cmd/ip/snoopy/ether.c           |       2 ++
         M src/cmd/ip/snoopy/filter.y          |      33 +++++++++++++++++++++-----------
         M src/cmd/ip/snoopy/hdlc.c            |       1 +
         M src/cmd/ip/snoopy/icmp.c            |       1 +
         M src/cmd/ip/snoopy/icmp6.c           |       1 +
         M src/cmd/ip/snoopy/il.c              |       1 +
         M src/cmd/ip/snoopy/ip.c              |       1 +
         M src/cmd/ip/snoopy/ip6.c             |       1 +
         M src/cmd/ip/snoopy/main.c            |     161 +++++++++++++++++++++++--------
         M src/cmd/ip/snoopy/ninep.c           |      19 +++----------------
         M src/cmd/ip/snoopy/ospf.c            |      24 +++++-------------------
         M src/cmd/ip/snoopy/ppp.c             |       6 ++++++
         M src/cmd/ip/snoopy/pppoe_disc.c      |       2 ++
         M src/cmd/ip/snoopy/rtcp.c            |      20 +++-----------------
         M src/cmd/ip/snoopy/rtp.c             |      20 +++-----------------
         M src/cmd/ip/snoopy/tcp.c             |       1 +
         M src/cmd/ip/snoopy/udp.c             |       1 +
       
       22 files changed, 187 insertions(+), 144 deletions(-)
       ---
 (DIR) diff --git a/src/cmd/ip/snoopy/arp.c b/src/cmd/ip/snoopy/arp.c
       t@@ -112,6 +112,7 @@ Proto arp =
                p_filter,
                p_seprint,
                nil,
       +        nil,
                p_fields,
                defaultframer,
        };
       t@@ -123,6 +124,7 @@ Proto rarp =
                p_filter,
                p_seprint,
                nil,
       +        nil,
                p_fields,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/bootp.c b/src/cmd/ip/snoopy/bootp.c
       t@@ -171,6 +171,7 @@ Proto bootp =
                p_filter,
                p_seprint,
                p_mux,
       +        "%#.8lux",
                p_fields,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/dat.h b/src/cmd/ip/snoopy/dat.h
       t@@ -18,6 +18,7 @@ struct Proto
                int        (*filter)(Filter*, Msg*);
                int        (*seprint)(Msg*);
                Mux*        mux;
       +        char*        valfmt;
                Field*        field;
                int        (*framer)(int, uchar*, int);
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/dhcp.c b/src/cmd/ip/snoopy/dhcp.c
       t@@ -106,15 +106,7 @@ enum
        static void
        p_compile(Filter *f)
        {
       -        sysfatal("unknown bootp field: %s", f->s);
       -}
       -
       -static int
       -p_filter(Filter *f, Msg *m)
       -{
       -        USED(f);
       -        USED(m);
       -        return 0;
       +        sysfatal("unknown dhcp field: %s", f->s);
        }
        
        /*
       t@@ -474,10 +466,11 @@ Proto dhcp =
        {
                "dhcp",
                p_compile,
       -        p_filter,
       +        nil,
                p_seprint,
                nil,
                nil,
       +        nil,
                defaultframer,
        };
        
 (DIR) diff --git a/src/cmd/ip/snoopy/dump.c b/src/cmd/ip/snoopy/dump.c
       t@@ -5,20 +5,6 @@
        #include "dat.h"
        #include "protos.h"
        
       -static void
       -p_compile(Filter *f)
       -{
       -        USED(f);
       -}
       -
       -static int
       -p_filter(Filter *f, Msg *m)
       -{
       -        USED(f);
       -        USED(m);
       -        return 0;
       -}
       -
        static char tohex[16] = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'
       t@@ -83,10 +69,11 @@ p_seprint(Msg *m)
        Proto dump =
        {
                "dump",
       -        p_compile,
       -        p_filter,
       +        nil,
       +        nil,
                p_seprint,
                nil,
                nil,
       +        nil,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/ether.c b/src/cmd/ip/snoopy/ether.c
       t@@ -23,6 +23,7 @@ static Mux p_mux[] =
                {"ip6",         0x86dd, } ,
                {"pppoe_disc",        0x8863, },
                {"pppoe_sess",        0x8864, },
       +        {"eapol",        0x888e, },
                {0}
        };
        
       t@@ -116,6 +117,7 @@ Proto ether =
                p_filter,
                p_seprint,
                p_mux,
       +        "%#.4lux",
                p_fields,
                defaultframer
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/filter.y b/src/cmd/ip/snoopy/filter.y
       t@@ -12,6 +12,7 @@ char        *yyend;                /* end of buffer to be parsed */
        %term LOR
        %term LAND
        %term WORD
       +%term NE
        %right '!'
        %left '|'
        %left '&'
       t@@ -27,6 +28,14 @@ expr                : WORD
                                { $$ = $1; }
                        | WORD '=' WORD
                                { $2->l = $1; $2->r = $3; $$ = $2; }
       +                | WORD NE WORD
       +                        { $2->l = newfilter();
       +                          $2->l->op = '=';
       +                          $2->l->l = $1;
       +                          $2->l->r = $3;
       +                          $2->op = '!';
       +                          $$ = $2;
       +                        }
                        | WORD '(' expr ')'
                                { $1->l = $3; free($2); free($4); $$ = $1; }
                        | '(' expr ')'
       t@@ -84,17 +93,18 @@ yylex(void)
                }
        
                yylp++;
       -        if(*yylp == c)
       -                switch(c){
       -                case '&':
       -                        c = LAND;
       -                        yylp++;
       -                        break;
       -                case '|':
       -                        c = LOR;
       -                        yylp++;
       -                        break;
       -                }
       +        if(c == '!' && *yylp == '='){
       +                c = NE;
       +                yylp++;
       +        }
       +        else if(c == '&' && *yylp == '&'){
       +                c = LAND;
       +                yylp++;
       +        }
       +        else if(c == '|' && *yylp == '|'){
       +                c = LOR;
       +                yylp++;
       +        }
                yylval->op = c;
                return c;
        }
       t@@ -103,5 +113,6 @@ void
        yyerror(char *e)
        {
                USED(e);
       +//        longjmp(errjmp, 1);
                sysfatal("error parsing filter");
        }
 (DIR) diff --git a/src/cmd/ip/snoopy/hdlc.c b/src/cmd/ip/snoopy/hdlc.c
       t@@ -169,6 +169,7 @@ Proto hdlc =
                p_filter,
                p_seprint,
                p_mux,
       +        "%#.4lux",
                nil,
                p_framer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/icmp.c b/src/cmd/ip/snoopy/icmp.c
       t@@ -191,6 +191,7 @@ Proto icmp =
                p_filter,
                p_seprint,
                p_mux,
       +        "%lud",
                p_fields,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/icmp6.c b/src/cmd/ip/snoopy/icmp6.c
       t@@ -423,6 +423,7 @@ Proto icmp6 =
                p_filter,
                p_seprint,
                p_mux,
       +        "%lud",
                p_fields,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/il.c b/src/cmd/ip/snoopy/il.c
       t@@ -141,6 +141,7 @@ Proto il =
                p_filter,
                p_seprint,
                p_mux,
       +        "%lud",
                p_fields,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/ip.c b/src/cmd/ip/snoopy/ip.c
       t@@ -231,6 +231,7 @@ Proto ip =
                p_filter,
                p_seprint,
                p_mux,
       +        "%lud",
                p_fields,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/ip6.c b/src/cmd/ip/snoopy/ip6.c
       t@@ -304,6 +304,7 @@ Proto ip6 =
                p_filter,
                p_seprint,
                p_mux,
       +        "%lud",
                p_fields,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/main.c b/src/cmd/ip/snoopy/main.c
       t@@ -35,14 +35,21 @@ void        mkprotograph(void);
        Proto*        findproto(char *name);
        Filter*        compile(Filter *f);
        void        printfilter(Filter *f, char *tag);
       -void        printhelp(void);
       +void        printhelp(char*);
        void        tracepkt(uchar*, int);
        void        pcaphdr(void);
        
        void
       +printusage(void)
       +{
       +        fprint(2, "usage: %s [-CDdpst] [-N n] [-f filter] [-h first-header] path\n", argv0);
       +        fprint(2, "  for protocol help: %s -? [proto]\n", argv0);
       +}
       +
       +void
        usage(void)
        {
       -        fprint(2, "usage: %s [-std?] [-c] [-N n] [-f filter] [-h first-header] path", argv0);
       +        printusage();
                exits("usage");
        }
        
       t@@ -74,20 +81,19 @@ main(int argc, char **argv)
                mkprotograph();
        
                ARGBEGIN{
       +        default:
       +                usage();
                case '?':
       -                printhelp();
       +                printusage();
       +                printhelp(ARGF());
                        exits(0);
                        break;
                case 'N':
       -                p = ARGF();
       -                if(p == nil)
       -                        usage();
       +                p = EARGF(usage());
                        Nflag = atoi(p);
                        break;
                case 'f':
       -                p = ARGF();
       -                if(p == nil)
       -                        usage();
       +                p = EARGF(usage());
                        yyinit(p);
                        yyparse();
                        break;
       t@@ -95,9 +101,7 @@ main(int argc, char **argv)
                        sflag = 1;
                        break;
                case 'h':
       -                p = ARGF();
       -                if(p == nil)
       -                        usage();
       +                p = EARGF(usage());
                        root = findproto(p);
                        if(root == nil)
                                sysfatal("unknown protocol: %s", p);
       t@@ -123,22 +127,28 @@ main(int argc, char **argv)
                if(pcap)
                        pcaphdr();
        
       +        if(argc > 1)
       +                usage();
       +
                if(argc == 0)
                        file = nil;
                else
                        file = argv[0];
        
                if(tiflag){
       +                if(file == nil)
       +                        sysfatal("must specify file with -t");
                        fd = open(file, OREAD);
                        if(fd < 0)
                                sysfatal("opening %s: %r", file);
                }else{
                        fd = opendevice(file, pflag);
                        if(fd < 0)
       -                        sysfatal("opening device %s: %r", file ? file : "(all)");
       +                        sysfatal("opening device %s: %r", file);
                }
                if(root == nil)
                        root = &ether;
       +
                filter = compile(filter);
        
                if(tiflag){
       t@@ -215,7 +225,7 @@ _filterpkt(Filter *f, Msg *m)
                                        return 0;
                                m->needroot = 0;
                        }else{
       -                        if(m->pr != nil && !(m->pr->filter)(f, m))
       +                        if(m->pr && (m->pr->filter==nil || !(m->pr->filter)(f, m)))
                                        return 0;
                        }
                        if(f->l == nil)
       t@@ -313,13 +323,10 @@ void
        printpkt(char *p, char *e, uchar *ps, uchar *pe)
        {
                Msg m;
       -        uvlong dt;
       -        Tm tm;
       -        
       -        tm = *localtime(pkttime/1000000000LL);
       -        m.p = seprint(p, e, "%02d/%02d/%04d %02d:%02d:%02d.%09lld",
       -                tm.mon+1, tm.mday, tm.year+1900, tm.hour, tm.min, tm.sec,
       -                pkttime%1000000000LL);
       +        ulong dt;
       +
       +        dt = (pkttime-starttime)/1000000LL;
       +        m.p = seprint(p, e, "%6.6uld ms ", dt);
                m.ps = ps;
                m.pe = pe;
                m.e = e;
       t@@ -635,14 +642,20 @@ _compile(Filter *f, Proto *last)
                        _compile(f->r, last);
                        break;
                case WORD:
       -                if(last != nil)
       +                if(last != nil){
       +                        if(last->compile == nil)
       +                                sysfatal("unknown %s subprotocol: %s", f->pr->name, f->s);
                                (*last->compile)(f);
       +                }
                        if(f->l)
                                _compile(f->l, f->pr);
                        break;
                case '=':
                        if(last == nil)
                                sysfatal("internal error: compilewalk: badly formed tree");
       +                
       +                if(last->compile == nil)
       +                        sysfatal("unknown %s field: %s", f->pr->name, f->s);
                        (*last->compile)(f);
                        break;
                default:
       t@@ -759,9 +772,9 @@ _pf(Filter *f)
                case WORD:
                        fprint(2, "%s", f->s);
                        if(f->l != nil){
       -                        fprint(2, "( ");
       +                        fprint(2, "(");
                                _pf(f->l);
       -                        fprint(2, " )");
       +                        fprint(2, ")");
                        }
                        break;
                case LAND:
       t@@ -794,24 +807,96 @@ printfilter(Filter *f, char *tag)
        }
        
        void
       -printhelp(void)
       +cat(void)
       +{
       +        char buf[1024];
       +        int n;
       +        
       +        while((n = read(0, buf, sizeof buf)) > 0)
       +                write(1, buf, n);
       +}
       +
       +static int fd1 = -1;
       +void
       +startmc(void)
       +{
       +        int p[2];
       +        
       +        if(fd1 == -1)
       +                fd1 = dup(1, -1);
       +        
       +        if(pipe(p) < 0)
       +                return;
       +        switch(fork()){
       +        case -1:
       +                return;
       +        default:
       +                close(p[0]);
       +                dup(p[1], 1);
       +                if(p[1] != 1)
       +                        close(p[1]);
       +                return;
       +        case 0:
       +                close(p[1]);
       +                dup(p[0], 0);
       +                if(p[0] != 0)
       +                        close(p[0]);
       +                execl("/bin/mc", "mc", nil);
       +                cat();
       +                _exits(0);
       +        }
       +}
       +
       +void
       +stopmc(void)
        {
       +        close(1);
       +        dup(fd1, 1);
       +        waitpid();
       +}
       +
       +void
       +printhelp(char *name)
       +{
       +        int len;
                Proto *pr, **l;
                Mux *m;
                Field *f;
       -
       -        for(l = protos; *l != nil; l++){
       -                pr = *l;
       -                if(pr->field != nil){
       -                        print("%s's filter attr:\n", pr->name);
       -                        for(f = pr->field; f->name != nil; f++)
       -                                print("\t%s\t- %s\n", f->name, f->help);
       -                }
       -                if(pr->mux != nil){
       -                        print("%s's subprotos:\n", pr->name);
       -                        for(m = pr->mux; m->name != nil; m++)
       -                                print("\t%s\n", m->name);
       -                }
       +        char fmt[40];
       +        
       +        if(name == nil){
       +                print("protocols:\n");
       +                startmc();
       +                for(l=protos; (pr=*l) != nil; l++)
       +                        print("  %s\n", pr->name);
       +                stopmc();
       +                return;
       +        }
       +        
       +        pr = findproto(name);
       +        if(pr == nil){
       +                print("unknown protocol %s\n", name);
       +                return;
       +        }
       +        
       +        if(pr->field){
       +                print("%s's filter attributes:\n", pr->name);
       +                len = 0;
       +                for(f=pr->field; f->name; f++)
       +                        if(len < strlen(f->name))
       +                                len = strlen(f->name);
       +                startmc();
       +                for(f=pr->field; f->name; f++)
       +                        print("  %-*s - %s\n", len, f->name, f->help);
       +                stopmc();
       +        }
       +        if(pr->mux){
       +                print("%s's subprotos:\n", pr->name);
       +                startmc();
       +                snprint(fmt, sizeof fmt, "  %s %%s\n", pr->valfmt);
       +                for(m=pr->mux; m->name != nil; m++)
       +                        print(fmt, m->val, m->name);
       +                stopmc();
                }
        }
        
 (DIR) diff --git a/src/cmd/ip/snoopy/ninep.c b/src/cmd/ip/snoopy/ninep.c
       t@@ -5,20 +5,6 @@
        #include "dat.h"
        #include "protos.h"
        
       -static void
       -p_compile(Filter *f)
       -{
       -        sysfatal("unknown ninep field: %s", f->s);
       -}
       -
       -static int
       -p_filter(Filter *f, Msg *m)
       -{
       -        USED(f);
       -        USED(m);
       -        return 0;
       -}
       -
        static int
        p_seprint(Msg *m)
        {
       t@@ -46,10 +32,11 @@ p_seprint(Msg *m)
        Proto ninep =
        {
                "ninep",
       -        p_compile,
       -        p_filter,
       +        nil,
       +        nil,
                p_seprint,
                nil,
                nil,
       +        nil,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/ospf.c b/src/cmd/ip/snoopy/ospf.c
       t@@ -90,11 +90,10 @@ struct Ospfhello
        };
        
        char*
       -seprintospfhello(char *p, char *e, void *a, int x)
       +seprintospfhello(char *p, char *e, void *a)
        {
                Ospfhello *h = a;
        
       -        USED(x);
                return seprint(p, e, "%s(mask %V interval %d opt %ux pri %ux deadt %d designated %V bdesignated %V)",
                        ospftype[OSPFhello],
                        h->mask, NetS(h->interval), h->options, h->pri,
       t@@ -328,20 +327,6 @@ seprintospflsack(char *p, char *e, void *a, int len)
                return seprint(p, e, ")");
        }
        
       -static void
       -p_compile(Filter *f)
       -{
       -        sysfatal("unknown ospf field: %s", f->s);
       -}
       -
       -static int
       -p_filter(Filter *f, Msg *m)
       -{
       -        USED(f);
       -        USED(m);
       -        return 0;
       -}
       -
        int
        p_seprint(Msg *m)
        {
       t@@ -369,7 +354,7 @@ p_seprint(Msg *m)
        
                switch (ospf->type) {
                case OSPFhello:
       -                p = seprintospfhello(p, e, ospf->data, x);
       +                p = seprintospfhello(p, e, ospf->data);
                        break;
                case OSPFdd:
                        p = seprintospfdatadesc(p, e, ospf->data, x);
       t@@ -395,10 +380,11 @@ Default:
        Proto ospf =
        {
                "ospf",
       -        p_compile,
       -        p_filter,
       +        nil,
       +        nil,
                p_seprint,
                nil,
                nil,
       +        nil,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/ppp.c b/src/cmd/ip/snoopy/ppp.c
       t@@ -569,6 +569,7 @@ Proto ppp =
                p_filter,
                p_seprint,
                p_mux,
       +        "%#.4lux",
                nil,
                defaultframer,
        };
       t@@ -581,6 +582,7 @@ Proto ppp_ipcp =
                p_seprintipcp,
                nil,
                nil,
       +        nil,
                defaultframer,
        };
        
       t@@ -592,6 +594,7 @@ Proto ppp_lcp =
                p_seprintlcp,
                nil,
                nil,
       +        nil,
                defaultframer,
        };
        
       t@@ -603,6 +606,7 @@ Proto ppp_ccp =
                p_seprintccp,
                nil,
                nil,
       +        nil,
                defaultframer,
        };
        
       t@@ -614,6 +618,7 @@ Proto ppp_chap =
                p_seprintchap,
                nil,
                nil,
       +        nil,
                defaultframer,
        };
        
       t@@ -625,5 +630,6 @@ Proto ppp_comp =
                p_seprintcomp,
                nil,
                nil,
       +        nil,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/pppoe_disc.c b/src/cmd/ip/snoopy/pppoe_disc.c
       t@@ -155,6 +155,7 @@ Proto pppoe_disc =
                p_filter,
                p_seprintdisc,
                p_mux,
       +        "%lud",
                p_fields,
                defaultframer
        };
       t@@ -166,6 +167,7 @@ Proto pppoe_sess =
                p_filter,
                p_seprintsess,
                p_mux,
       +        "%lud",
                p_fields,
                defaultframer
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/rtcp.c b/src/cmd/ip/snoopy/rtcp.c
       t@@ -31,21 +31,6 @@ enum{
                REPORTLEN = 24,
        };
        
       -
       -static void
       -p_compile(Filter *f)
       -{
       -        sysfatal("unknown rtcp field: %s", f->s);
       -}
       -
       -static int
       -p_filter(Filter *f, Msg *m)
       -{
       -        USED(f);
       -        USED(m);
       -        return 0;
       -}
       -
        static int
        p_seprint(Msg *m)
        {
       t@@ -88,10 +73,11 @@ p_seprint(Msg *m)
        
        Proto rtcp = {
                "rtcp",
       -        p_compile,
       -        p_filter,
       +        nil,
       +        nil,
                p_seprint,
                nil,
                nil,
       +        nil,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/rtp.c b/src/cmd/ip/snoopy/rtp.c
       t@@ -17,21 +17,6 @@ enum{
                RTPLEN = 12,                // Minimum size of an RTP header
        };
        
       -
       -static void
       -p_compile(Filter *f)
       -{
       -        sysfatal("unknown rtp field: %s", f->s);
       -}
       -
       -static int
       -p_filter(Filter *f, Msg *m)
       -{
       -        USED(f);
       -        USED(m);
       -        return 0;
       -}
       -
        static int
        p_seprint(Msg *m)
        {
       t@@ -67,10 +52,11 @@ p_seprint(Msg *m)
        
        Proto rtp = {
                "rtp",
       -        p_compile,
       -        p_filter,
       +        nil,
       +        nil,
                p_seprint,
                nil,
                nil,
       +        nil,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/tcp.c b/src/cmd/ip/snoopy/tcp.c
       t@@ -216,6 +216,7 @@ Proto tcp =
                p_filter,
                p_seprint,
                p_mux,
       +        "%lud",
                p_fields,
                defaultframer,
        };
 (DIR) diff --git a/src/cmd/ip/snoopy/udp.c b/src/cmd/ip/snoopy/udp.c
       t@@ -126,6 +126,7 @@ Proto udp =
                p_filter,
                p_seprint,
                p_mux,
       +        "%lud",
                p_fields,
                defaultframer,
        };