add xns protocols Reference: /n/atom/patch/applied2013/snoopyxns Date: Wed Jun 19 02:28:12 CES 2013 Signed-off-by: quanstro@quanstro.net --- /sys/src/cmd/ip/snoopy/dat.h Wed Jun 19 02:27:59 2013 +++ /sys/src/cmd/ip/snoopy/dat.h Wed Jun 19 02:27:59 2013 @@ -66,6 +66,7 @@ Fether, /* ethernet address */ Fv4ip, /* v4 ip address */ Fv6ip, /* v6 ip address */ + Fxns, /* xns address */ Fba, /* byte array */ }; --- /sys/src/cmd/ip/snoopy/xns.c Thu Jan 1 00:00:00 1970 +++ /sys/src/cmd/ip/snoopy/xns.c Wed Jun 19 02:27:59 2013 @@ -0,0 +1,131 @@ +#include +#include +#include +#include +#include "dat.h" +#include "protos.h" + +static Mux p_mux[] = +{ + { "xnsecho", X2echo, }, + { 0 } +}; + +enum{ + Os, + Od, + Osd, + Ot, +}; + +static Field p_fields[] = +{ + {"s", Fxns, Os, "source address", } , + {"d", Fxns, Od, "destination address", } , + {"a", Fxns, Osd, "source|destination address",} , + {"sd", Fxns, Osd, "source|destination address",} , + {"t", Fnum, Ot, "sub protocol number", } , + {0} +}; + +static void +p_compile(Filter *f) +{ + Mux *m; + + if(f->op == '='){ + compile_cmp(xns.name, f, p_fields); + return; + } + for(m = p_mux; m->name != nil; m++) + if(strcmp(f->s, m->name) == 0){ + f->pr = m->pr; + f->ulv = m->val; + f->subop = Ot; + return; + } + sysfatal("unknown xns field or protocol: %s", f->s); +} + +int +xnscmp(uchar *u, uchar *v) +{ + ulong a, b; + + a = nhgetl(u); + b = nhgetl(v); + if(a && b) + if(a != b) + return 0; + u += 2; + v += 2; + + if(memcmp(u, v, 6)) + return 0; + u += 2; + v += 2; + + a = nhgets(u); + b = nhgets(v); + if(a && b) + if(a != b) + return 0; + return 1; +} + +static int +p_filter(Filter *f, Msg *m) +{ + XNpkt *h; + + if(m->pe-m->ps < XNpktsz-XNhdrsz) + return 0; + + h = (XNpkt*)m->ps; + m->ps += XNpktsz-XNhdrsz; + + switch(f->subop){ + case Os: + return xnscmp(h->sn, f->a); + case Od: + return xnscmp(h->dn, f->a); + case Osd: + return xnscmp(h->sn, f->a) || xnscmp(h->dn, f->a); + case Ot: + return h->xtype == f->ulv; + } + return 0; +} + + +static int +p_seprint(Msg *m) +{ + XNpkt *h; + int len; + + if(m->pe-m->ps < XNpktsz-XNhdrsz) + return -1; + h = (XNpkt*)m->ps; + demux(p_mux, h->xtype, h->xtype, m, &dump); + len = NetS(h->len); + if(len < m->pe - m->ps) + m->pe = m->ps + len; + m->ps = h->data; + + m->p = seprint(m->p, m->e, "len=%d hop=%.2x xtype=%.2x d=%J s=%J", + len, h->hop, h->xtype, h->dn, h->sn); + return 0; +} + +Proto xns = +{ + "xns", + p_compile, + p_filter, + p_seprint, + p_mux, + "%J", + p_fields, + defaultframer, +}; --- /sys/src/cmd/ip/snoopy/xnsecho.c Thu Jan 1 00:00:00 1970 +++ /sys/src/cmd/ip/snoopy/xnsecho.c Wed Jun 19 02:27:59 2013 @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include "dat.h" +#include "protos.h" + +enum{ + Oop, +}; + +static Field p_fields[] = +{ + {"op", Fnum, Oop, "operation", }, + {0} +}; + +static void +p_compile(Filter *f) +{ + if(f->op == '='){ + compile_cmp(xnsecho.name, f, p_fields); + return; + } + sysfatal("unknown xnsecho field: %s", f->s); +} + +static int +p_filter(Filter *f, Msg *m) +{ + XNecho *h; + + if(m->pe-m->ps < XNechosz) + return 0; + + h = (XNecho*)m->ps; + m->ps += XNechosz; + + switch(f->subop){ + case Oop: + return NetS(h->op) == f->ulv; + } + return 0; +} + +static int +p_seprint(Msg *m) +{ + XNecho *h; + char buf[40]; + int l; + + if(m->pe-m->ps < XNechosz) + return 0; + + h = (XNecho*)m->ps; + m->ps += XNechosz; + + /* no next protocol */ + m->pr = nil; + + l = m->pe-m->ps; + if(l > 39){ + memmove(buf, m->ps, 36); + memmove(buf+36, "...", 4); + }else{ + memmove(buf, m->ps, l); + buf[l] = 0; + } + + m->p = seprint(m->p, m->e, "op=%d data=[%s] dl=%d", NetS(h->op), buf, l); + return 0; +} + +Proto xnsecho = +{ + "xnsecho", + p_compile, + p_filter, + p_seprint, + nil, + nil, + p_fields, + defaultframer, +}; --- /sys/src/cmd/ip/snoopy/xnserror.c Thu Jan 1 00:00:00 1970 +++ /sys/src/cmd/ip/snoopy/xnserror.c Wed Jun 19 02:27:59 2013 @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include "dat.h" +#include "protos.h" + +enum{ + Oe, + Op, +}; + +static Field p_fields[] = +{ + {"e", Fnum, Oe, "errno", }, + {"p", Fnum, Op, "param", }, + {0} +}; + +static void +p_compile(Filter *f) +{ + if(f->op == '='){ + compile_cmp(xnserror.name, f, p_fields); + return; + } + sysfatal("unknown xnserror field: %s", f->s); +} + +static int +p_filter(Filter *f, Msg *m) +{ + XNerror *h; + + if(m->pe-m->ps < XNerrorsz) + return 0; + + h = (XNerror*)m->ps; + m->ps += XNerrorsz; + + switch(f->subop){ + case Oe: + return NetS(h->errno) == f->ulv; + case Op: + return NetS(h->parm) == f->ulv; + default: + return 0; + } +} + +static int +p_seprint(Msg *m) +{ + XNerror *h; + char buf[40]; + int l; + + if(m->pe-m->ps < XNerrorsz) + return 0; + + h = (XNerror*)m->ps; + m->ps += XNerrorsz; + + /* no next protocol */ + m->pr = nil; + + l = m->pe-m->ps; + if(l > 39){ + memmove(buf, m->ps, 36); + memmove(buf+36, "...", 4); + }else{ + memmove(buf, m->ps, l); + buf[l] = 0; + } + + m->p = seprint(m->p, m->e, "e=%d p=%d data=[%s] dl=%d", NetS(h->errno), NetS(h->parm), buf, l); + return 0; +} + +Proto xnserror = +{ + "xnserror", + p_compile, + p_filter, + p_seprint, + nil, + nil, + p_fields, + defaultframer, +}; --- /sys/src/cmd/ip/snoopy/xnsroute.c Thu Jan 1 00:00:00 1970 +++ /sys/src/cmd/ip/snoopy/xnsroute.c Wed Jun 19 02:27:59 2013 @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include "dat.h" +#include "protos.h" + +enum{ + Oop, +}; + +static Field p_fields[] = +{ + {"op", Fnum, Oop, "operation", }, + {0} +}; + +static void +p_compile(Filter *f) +{ + if(f->op == '='){ + compile_cmp(xnsroute.name, f, p_fields); + return; + } + sysfatal("unknown xnsroute field: %s", f->s); +} + +static int +p_filter(Filter *f, Msg *m) +{ + XNroute *h; + + if(m->pe-m->ps < XNroutesz) + return 0; + + h = (XNroute*)m->ps; + m->ps = m->pe; + + switch(f->subop){ + case Oop: + return NetS(h->op) == f->ulv; + default: + return 0; + } +} + +static int +p_seprint(Msg *m) +{ + XNroute *h; + int l; + + l = m->pe-m->ps; + if(l < 6) + return 0; + h = (XNroute*)m->ps; + + if(l%6){ + m->p = seprint(m->p, m->e, "op=%d ", NetS(h->op)); + m->ps += 2; + l -= 2; + } + if(l == 0){ + m->pr = nil; + return 0; + } + m->pr = &xnsroute; + m->p = seprint(m->p, m->e, "on=%ud hop=%d", NetL(m->ps), NetS(m->ps+4)); + m->ps += 4; + + return 0; +} + +Proto xnsroute = +{ + "xnsroute", + p_compile, + p_filter, + p_seprint, + nil, + nil, + p_fields, + defaultframer, +}; --- /sys/src/cmd/ip/snoopy/xnsspp.c Thu Jan 1 00:00:00 1970 +++ /sys/src/cmd/ip/snoopy/xnsspp.c Wed Jun 19 02:27:59 2013 @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include "dat.h" +#include "protos.h" + +enum{ + Ot, + Os, + Od, + Osd, +}; + +static Field p_fields[] = +{ + {"t", Fnum, Ot, "subtype", }, + {"s", Fnum, Os, "source connection", }, + {"d", Fnum, Od, "destination connection", }, + {"sd", Fnum, Osd, "source|dest connection", }, + {0} +}; + +static void +p_compile(Filter *f) +{ + if(f->op == '='){ + compile_cmp(xnsspp.name, f, p_fields); + return; + } + sysfatal("unknown xnsspp field: %s", f->s); +} + +static int +p_filter(Filter *f, Msg *m) +{ + XNspp *h; + + if(m->pe-m->ps < XNsppsz) + return 0; + + h = (XNspp*)m->ps; + m->ps += XNsppsz; + + switch(f->subop){ + case Ot: + return NetS(h->dstype) == f->ulv; + case Os: + return NetS(h->sconn) == f->ulv; + case Od: + return NetS(h->dconn) == f->ulv; + case Osd: + return NetS(h->sconn) == f->ulv || NetS(h->dconn) == f->ulv; + default: + return 0; + } +} + +static int +p_seprint(Msg *m) +{ + XNspp *h; + + if(m->pe-m->ps < XNsppsz) + return 0; + + h = (XNspp*)m->ps; + m->ps += XNsppsz; + + /* no next protocol */ + m->pr = &dump; + + m->p = seprint(m->p, m->e, + "c=%d t=%d s=%.4ux d=%.4ux seq=%.4ux ack=%.4ux alloc=%.4ux", + h->cctl, h->dstype, NetS(h->sconn), NetS(h->dconn), + NetS(h->seq), NetS(h->ack), NetS(h->alloc)); + return 0; +} + +Proto xnsspp = +{ + "xnsecho", + p_compile, + p_filter, + p_seprint, + nil, + nil, + p_fields, + defaultframer, +}; --- /sys/src/cmd/ip/snoopy/mkfile Wed Jun 19 02:27:59 2013 +++ /sys/src/cmd/ip/snoopy/mkfile Wed Jun 19 02:27:59 2013 @@ -43,6 +43,11 @@ tcp\ ttls\ udp\ + xns\ + xnsecho\ + xnserror\ + xnsroute\ + xnsspp\ POBJS=${PROTOS:%=%.$O}