static void pkt_handler_en10mb(u_char *usr, const struct pcap_pkthdr *pkt, const u_char *d) { struct machdr *mac = mac_hdr(d); sb_reset(&sb); eth_print(mac, &sb); sb_append_str(&sb, "; "); etherframe_print(usr, pkt, d + ETH_HLEN, mac->type); fprintf(stdout, "pkt: %s\n", sb.buf); }
int iphdr_print(struct iphdr *iph, struct strbuf *sb) { const char *s; int len; char ihl[16]; char flags[10]; if (iph->version == 6) { const char msg[] = "IPv6 headers are not yet supported"; size_t msglen = sizeof(msg); if (sb_room(sb) < msglen) return -ENOBUFS;; sb_append_str(sb, msg); return 0; } len = sprintf(ihl, "hdrlen: %d ", iph->ihl * 4); len += sprintf(flags, "[DF%dMF%d] ", ip_df(iph), ip_mf(iph)); if (sb_room(sb) < (INET_ADDRSTRLEN * 2) + 3 + len) return -ENOBUFS; sb_append_str(sb, ihl); sb->len += sprintf(sb_curr(sb), "tot: %u ", ntohs(iph->tot_len)); sb_append_str(sb, flags); s = inet_ntop(AF_INET, &iph->saddr, sb_curr(sb), INET_ADDRSTRLEN); if (!s) return -errno; sb->len += strlen(s); sb_append_str(sb, "->"); s = inet_ntop(AF_INET, &iph->daddr, sb_curr(sb), INET_ADDRSTRLEN); if (!s) return -errno; sb->len += strlen(s); return 0; }
/** * Appends the formatted string to the given string builder */ void sb_append_strf(stringbuilder* sb, const char* fmt, ...) { char *str; va_list arglist; va_start(arglist, fmt); xp_vasprintf(&str, fmt, arglist); va_end(arglist); if (!str) { return; } sb_append_str(sb, str); free(str); }
/** * Breaks up the input into multiple quoted lines, making sure each line doesn't go over the 80 column * limit. This is for aesthetic reasons as well as because there are compilers that can't tolerate * incredibly long source lines */ static void _breakup_lines_modifier_cb(const char* name, const char* args, const char* marker, const char* value, stringbuilder* out_sb) { char* p; p = (char*)value; int i; i = 0; while(*p) { sb_append_ch(out_sb, *p++); // Attempt to keep everything under 80 cols by splitting up lines if (i++ > 70 && *p && *(p-1) != '\\') { i = 0; sb_append_str(out_sb, "\"\n \""); } } sb_append_ch(out_sb, '\0'); }
static void etherframe_print(u_char *usr, const struct pcap_pkthdr *pkt, const u_char *d, uint16_t ethtype) { struct iphdr *ip; struct tcphdr *th; struct udphdr *uh; uint16_t ethtype_le; ethtype_le = ntohs(ethtype); switch (ethtype_le) { case ETH_P_IP: ip = ip_hdr(d); sb_append_str(&sb, "IP: "); iphdr_print(ip, &sb); switch (ip->protocol) { case IPPROTO_TCP: th = tcp_hdr(d + ip_hdrlen(ip)); sb_append_str(&sb, "; TCP: "); tcp_print(th, &sb); break; case IPPROTO_UDP: uh = udp_hdr(d + ip_hdrlen(ip)); sb_append_str(&sb, "; UDP: "); udp_print(uh, &sb); break; default: sb_append_char(&sb, ' '); sb_append_str(&sb, ipproto_str(ip->protocol)); } break; default: /* FIXME: This code is open to buffer overrun errors */ sb_append_str(&sb, "ether type: "); sb.len += sprintf(sb_curr(&sb), "0x%04x ", ethtype_le); sb_append_str(&sb, ethertype_to_str(ethtype_le)); } }