void dump_muss_payload(char *buf, size_t buflen, const u8 *src, size_t srclen) { assert(srclen >= 8); snprintf(buf, buflen, "........"); if (buflen > 9) dump_chars_buf(buf+8, buflen-8, (char *)(src+8), srclen-8); }
static void fprint2str(char *buf, size_t len, const echo_fingerprint *f) { int used = snprintf(buf, len, "%s,ttl=%u,idz=%u,df=%u,bytes=%u,payload=", PerMsg[f->type].shortname, f->ttl, f->idz, f->df, f->len); if (used > 0) { if (is_muuss_payload(f->payload, f->len)) dump_muss_payload(buf+used, len-used, f->payload, f->len); else dump_chars_buf(buf+used, len-used, (char *)f->payload, f->len); } }
size_t http_dump_headers(const http_headers *h, int opt, FILE *out) { static char buf[4096]; # define DUMP_KEY_ALIGN 24 static const char Dots[DUMP_KEY_ALIGN] = "........................"; int bytes = 0; unsigned i, j; for (i = 0; i < h->cnt; i++) { const char *ks = h->h[i].key.start; const size_t kl = h->h[i].key.len, kdlen = dump_chars_buf(buf, sizeof buf, ks, kl); bytes += fprintf(out, " %.*s%.*s", (int)kdlen, buf, (int)(kdlen > DUMP_KEY_ALIGN ? 0 : DUMP_KEY_ALIGN - kdlen), Dots); for (j = 0; j < h->h[i].val.cnt; j++) { const char *vs = h->h[i].val.p[j].start; const size_t vl = h->h[i].val.p[j].len, vdlen = dump_chars_buf(buf, sizeof buf, vs, vl); bytes += fprintf(out, "%.*s", (int)vdlen, buf); } fputc('\n', out), bytes++; } return (size_t)bytes; }
static size_t dump_qd(const char *buf, size_t len, FILE *out) { char namebuf[1024]; const char *name = (const char *)buf; size_t namel = memcspn(name, len, "\x00", 1); int bytes = 0; const dns_query *q = (dns_query *)(buf + namel + 1); (void)dump_chars_buf(namebuf, sizeof namebuf, buf, namel); #if 0 printf("len=%u name[namel=%u]=%s name[10]=", (unsigned)len, (unsigned)namel, namebuf); dump_chars(name, 10, stdout); fputc('\n', stdout); #endif bytes = fprintf(out, " qd name=%s type=%hu(%s) class=%hu(%s)\n", namebuf, ntohs(q->type), type2str(ntohs(q->type)), ntohs(q->class_), class2str(ntohs(q->class_))); return bytes; }
static size_t dump_rr(enum DNS_RR rr, const parse_frame *f, const char *buf, size_t len, FILE *out) { char namebuf[256], targetbuf[256]; const char *name = buf; size_t namelen = dns_calc_len_name(name, len); const dns_answer *a = (dns_answer *)(buf + namelen); int bytes; (void)dump_chars_buf(namebuf, sizeof namebuf, name, namelen-1); bytes = fprintf(out, " %s name=%s type=%hu(%s) class=%hu(%s) ttl=%ld target=%s\n", RR[rr].name, namebuf, ntohs(a->type), type2str(ntohs(a->type)), a->class_, class2str(a->class_), (long)ntohl(a->ttl), addrformat(ntohs(a->type), targetbuf, sizeof targetbuf, (char *)a + sizeof *a, ntohs(a->rrlen))); #ifndef TEST /* TODO: split this block off to another function */ if (DNS_RR_AN == rr && DNS_Type_TXT == ntohs(a->type)) { /* is a TXT record answer; usually supplemental information that can * contain some interesting stuff */ const char *addrtype = NULL; char ipbuf[64]; const parse_frame *fi = f-2; if (PROT_IPv4 == fi->id) { const ipv4 *i = fi->off; addrtype = "4"; ipv4_addr_format(ipbuf, sizeof ipbuf, i->src); } else if (PROT_IPv6 == fi->id) { const ipv6 *i = fi->off; addrtype = "6"; ipv6_addr_format(ipbuf, sizeof ipbuf, i->src); } if (addrtype) { (void)dump_chars_buf(namebuf, sizeof namebuf, name, strip_c0(namebuf, namelen)); (void)dump_chars_buf(targetbuf, sizeof targetbuf, (char *)a + sizeof *a, strip_c0((char *)a + sizeof *a, ntohs(a->rrlen))); if ('\0' != namebuf[0]) rep_hint(addrtype, ipbuf, "DNS.TXT", namebuf, -1); if ('\0' != targetbuf[0]) rep_hint(addrtype, ipbuf, "DNS.TXT", targetbuf, -1); } } else if (DNS_RR_AN == rr && (DNS_Type_A == ntohs(a->type) || DNS_Type_AAAA == ntohs(a->type)) && str_endswith(namebuf, "\\x05local")) { /* we're looking for ".local" addresses; which may identify the machine */ char ipbuf[64]; const parse_frame *fi = f-2; const char *addrtype = NULL; if (PROT_IPv4 == fi->id) { const ipv4 *i = fi->off; addrtype = "4"; ipv4_addr_format(ipbuf, sizeof ipbuf, i->src); } else if (PROT_IPv6 == fi->id) { const ipv6 *i = fi->off; addrtype = "6"; ipv6_addr_format(ipbuf, sizeof ipbuf, i->src); } (void)dump_chars_buf(namebuf, sizeof namebuf, name, namelen-1); if ('\0' != namebuf[0]) { rep_hint(addrtype, ipbuf, "DNS.LOCAL", namebuf, -1); } { char localname[64]; /* first char is length of actual name */ if (name[0] > 0 && (unsigned)name[0] < sizeof localname) { strlcpy(localname, name+1, (size_t)name[0]+1); localname[(unsigned)name[0]] = '\0'; } else { strlcpy(localname, name+1, sizeof localname); } rep_addr(addrtype, ipbuf, "D", localname, "DNS.LOCAL", 1); } } #endif return (size_t)bytes; }