#include #include #include #include "fixed-ctype.h" #include "format.h" typedef struct priv PRIV; struct priv { unsigned char *buf; int len; int buflen; } ; static const char *skip_timestamp(const char *l) { char *el; if (! FIXdigit(*l)) return(l); if ( FIXdigit(l[1]) && (l[2] == ':') && FIXdigit(l[3]) && FIXdigit(l[4]) && (l[5] == ':') && FIXdigit(l[6]) && FIXdigit(l[7]) && (l[8] == '.') && FIXdigit(l[9]) && FIXdigit(l[10]) && FIXdigit(l[11]) && FIXdigit(l[12]) && FIXdigit(l[13]) && FIXdigit(l[14]) && (l[15] == ' ') ) return(l+16); strtol(l,&el,0); if ( (el[0] == '.') && FIXdigit(el[1]) && FIXdigit(el[2]) && FIXdigit(el[3]) && FIXdigit(el[4]) && FIXdigit(el[5]) && FIXdigit(el[6]) && (el[7] == ' ') ) return(el+8); return(l); } static const char *skip_eheader(const char *l) { static const char delim[12] = "::::: ::::: "; const char *l0; char *el; int i; int v; l0 = l; for (i=0;i<12;i++) { v = strtol(l,&el,16); if (el == l) return(l0); if (*el != delim[i]) return(l0); if ((v < 0) || (v > 0xff)) return(l0); l = el + 1; } while (*l != ':') { if (! *l) return(l0); l ++; } if (l[1] != ' ') return(l0); return(l+2); } static void force(void **privvp) { PRIV *p; p = malloc(sizeof(PRIV)); p->buflen = 64; p->len = 0; p->buf = malloc(p->buflen); *privvp = p; } static int hdrtest(const char *line, void **privvp) { char junk[2]; line = skip_timestamp(line); line = skip_eheader(line); if (sscanf(line,"%*[^.].%*[^ ] > %*[^:]: %1s",&junk[0]) != 1) return(0); force(privvp); return(1); } static void getx(const char *l, PRIV *p, int maxn) { unsigned int v; for (;maxn>0;maxn--) { while (*l && FIXspace(*l)) l ++; if (!FIXxdigit(l[0]) || !FIXxdigit(l[1])) break; sscanf(l,"%2x",&v); if (p->len >= p->buflen) p->buf = realloc(p->buf,p->buflen=p->len+64); p->buf[p->len++] = v; l += 2; } } static void line(const char *l, void *pvp) { PRIV *p; char junk[2]; p = pvp; if (l && ((l[0] == ' ') || (l[0] == '\t'))) { if (sscanf(l," 0x%*x: %1s",&junk[0]) == 1) { char *t; t = index(l,':'); if (t) { getx(t+1,p,16); return; } } getx(l,p,16); } else { if (p->len > 0) dumpit(p->buf,p->len); p->len = 0; if (l) printf("%s\n",l); } } FORMAT format_tcpdump = { "tcpdump", hdrtest, force, line };