commit 3e5c48603914ede7e4c60e357e333c473bd3630e
parent 116283f86777d20cf3318f7e388a9f32562fbc48
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date: Wed, 24 Jun 2020 20:14:41 +1000
Remove a couple casts and fix formatting
Thanks indent(1) for having really bad defaults :).
Also added a notice for broken data highlighting in regex mode.
Diffstat:
M | ngrep.c | | | 108 | ++++++++++++++++++++++++++++++++++++++++++++++--------------------------------- |
1 file changed, 63 insertions(+), 45 deletions(-)
diff --git a/ngrep.c b/ngrep.c
@@ -585,8 +585,8 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p)
uint8_t vlan_offset = include_vlan ?
vlan_frame_count(p, h->caplen) * VLANHDR_SIZE : 0;
- struct ip *ip4_pkt = (struct ip *) (p + link_offset + vlan_offset);
- struct ip6_hdr *ip6_pkt = (struct ip6_hdr *) (p + link_offset + vlan_offset);
+ struct ip *ip4_pkt = (struct ip *)(p + link_offset + vlan_offset);
+ struct ip6_hdr *ip6_pkt = (struct ip6_hdr *)(p + link_offset + vlan_offset);
uint32_t ip_ver;
@@ -607,8 +607,8 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p)
seen_frames++;
if (radiotap_present) {
- uint16_t radio_len = ((struct NGREP_rtaphdr_t *) (p))->it_len;
- ip4_pkt = (struct ip *) (p + link_offset + radio_len);
+ uint16_t radio_len = ((struct NGREP_rtaphdr_t *)p)->it_len;
+ ip4_pkt = (struct ip *)(p + link_offset + radio_len);
len -= radio_len;
}
ip_ver = ip4_pkt->ip_v;
@@ -623,8 +623,8 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p)
frag_offset = (fragmented) ? (ip_off & IP_OFFMASK) * 8 : 0;
frag_id = ntohs(ip4_pkt->ip_id);
- inet_ntop(AF_INET, (const void *) &ip4_pkt->ip_src, ip_src, sizeof(ip_src));
- inet_ntop(AF_INET, (const void *) &ip4_pkt->ip_dst, ip_dst, sizeof(ip_dst));
+ inet_ntop(AF_INET, &ip4_pkt->ip_src, ip_src, sizeof(ip_src));
+ inet_ntop(AF_INET, &ip4_pkt->ip_dst, ip_dst, sizeof(ip_dst));
} break;
case 6:{
ip_hl = sizeof(struct ip6_hdr);
@@ -633,7 +633,7 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p)
if (ip_proto == IPPROTO_FRAGMENT) {
struct ip6_frag *ip6_fraghdr;
- ip6_fraghdr = (struct ip6_frag *) ((unsigned char *) (ip6_pkt) + ip_hl);
+ ip6_fraghdr = (struct ip6_frag *)((unsigned char *)ip6_pkt + ip_hl);
ip_hl += sizeof(struct ip6_frag);
ip_proto = ip6_fraghdr->ip6f_nxt;
@@ -641,8 +641,8 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p)
frag_offset = ntohs(ip6_fraghdr->ip6f_offlg & IP6F_OFF_MASK);
frag_id = ntohl(ip6_fraghdr->ip6f_ident);
}
- inet_ntop(AF_INET6, (const void *) &ip6_pkt->ip6_src, ip_src, sizeof(ip_src));
- inet_ntop(AF_INET6, (const void *) &ip6_pkt->ip6_dst, ip_dst, sizeof(ip_dst));
+ inet_ntop(AF_INET6, &ip6_pkt->ip6_src, ip_src, sizeof(ip_src));
+ inet_ntop(AF_INET6, &ip6_pkt->ip6_dst, ip_dst, sizeof(ip_dst));
} break;
}
@@ -653,87 +653,96 @@ process(UNUSED u_char *d, struct pcap_pkthdr *h, u_char *p)
switch (ip_proto) {
case IPPROTO_TCP:{
- struct tcphdr *tcp_pkt = (struct tcphdr *) ((unsigned char *) (ip4_pkt) + ip_hl);
- uint16_t tcphdr_offset = (frag_offset) ? 0 : (tcp_pkt->th_off * 4);
+ struct tcphdr *tcp_pkt
+ = (struct tcphdr *)((unsigned char *)ip4_pkt + ip_hl);
+ uint16_t tcphdr_offset = frag_offset ? 0 : tcp_pkt->th_off * 4;
- data = (unsigned char *) (tcp_pkt) + tcphdr_offset;
+ data = (unsigned char *)tcp_pkt + tcphdr_offset;
len -= link_offset + ip_hl + tcphdr_offset;
if ((int32_t) len < 0)
len = 0;
- dump_packet(h, p, ip_proto, data, len,
- ip_src, ip_dst, ntohs(tcp_pkt->th_sport), ntohs(tcp_pkt->th_dport), tcp_pkt->th_flags,
- tcphdr_offset, fragmented, frag_offset, frag_id);
+ dump_packet(h, p, ip_proto, data, len, ip_src, ip_dst,
+ ntohs(tcp_pkt->th_sport),
+ ntohs(tcp_pkt->th_dport), tcp_pkt->th_flags,
+ tcphdr_offset, fragmented, frag_offset, frag_id);
} break;
case IPPROTO_UDP:{
- struct udphdr *udp_pkt = (struct udphdr *) ((unsigned char *) (ip4_pkt) + ip_hl);
- uint16_t udphdr_offset = (frag_offset) ? 0 : sizeof(*udp_pkt);
+ struct udphdr *udp_pkt
+ = (struct udphdr *)((unsigned char *)ip4_pkt + ip_hl);
+ uint16_t udphdr_offset = frag_offset ? 0 : sizeof(*udp_pkt);
- data = (unsigned char *) (udp_pkt) + udphdr_offset;
+ data = (unsigned char *)udp_pkt + udphdr_offset;
len -= link_offset + ip_hl + udphdr_offset;
if ((int32_t) len < 0)
len = 0;
dump_packet(h, p, ip_proto, data, len, ip_src, ip_dst,
- ntohs(udp_pkt->uh_sport), ntohs(udp_pkt->uh_dport), 0,
- udphdr_offset, fragmented, frag_offset, frag_id);
+ ntohs(udp_pkt->uh_sport),
+ ntohs(udp_pkt->uh_dport), 0, udphdr_offset,
+ fragmented, frag_offset, frag_id);
} break;
case IPPROTO_ICMP:{
- struct icmp *icmp4_pkt = (struct icmp *) ((unsigned char *) (ip4_pkt) + ip_hl);
- uint16_t icmp4hdr_offset = (frag_offset) ? 0 : 4;
+ struct icmp *icmp4_pkt
+ = (struct icmp *)((unsigned char *)ip4_pkt + ip_hl);
+ uint16_t icmp4hdr_offset = frag_offset ? 0 : 4;
- data = (unsigned char *) (icmp4_pkt) + icmp4hdr_offset;
+ data = (unsigned char *)icmp4_pkt + icmp4hdr_offset;
len -= link_offset + ip_hl + icmp4hdr_offset;
if ((int32_t) len < 0)
len = 0;
- dump_packet(h, p, ip_proto, data, len,
- ip_src, ip_dst, icmp4_pkt->icmp_type, icmp4_pkt->icmp_code, 0,
- icmp4hdr_offset, fragmented, frag_offset, frag_id);
+ dump_packet(h, p, ip_proto, data, len, ip_src, ip_dst,
+ icmp4_pkt->icmp_type, icmp4_pkt->icmp_code, 0,
+ icmp4hdr_offset, fragmented, frag_offset,
+ frag_id);
} break;
case IPPROTO_ICMPV6:{
- struct icmp6_hdr *icmp6_pkt = (struct icmp6_hdr *) ((unsigned char *) (ip6_pkt) + ip_hl);
- uint16_t icmp6hdr_offset = (frag_offset) ? 0 : 4;
+ struct icmp6_hdr *icmp6_pkt
+ = (struct icmp6_hdr *)((unsigned char *)ip6_pkt + ip_hl);
+ uint16_t icmp6hdr_offset = frag_offset ? 0 : 4;
- data = (unsigned char *) (icmp6_pkt) + icmp6hdr_offset;
+ data = (unsigned char *)icmp6_pkt + icmp6hdr_offset;
len -= link_offset + ip_hl + icmp6hdr_offset;
if ((int32_t) len < 0)
len = 0;
- dump_packet(h, p, ip_proto, data, len,
- ip_src, ip_dst, icmp6_pkt->icmp6_type, icmp6_pkt->icmp6_code, 0,
- icmp6hdr_offset, fragmented, frag_offset, frag_id);
+ dump_packet(h, p, ip_proto, data, len, ip_src, ip_dst,
+ icmp6_pkt->icmp6_type, icmp6_pkt->icmp6_code,
+ 0, icmp6hdr_offset, fragmented, frag_offset,
+ frag_id);
} break;
case IPPROTO_IGMP:{
- struct igmp *igmp_pkt = (struct igmp *) ((unsigned char *) (ip4_pkt) + ip_hl);
- uint16_t igmphdr_offset = (frag_offset) ? 0 : 4;
+ struct igmp *igmp_pkt
+ = (struct igmp *)((unsigned char *)ip4_pkt + ip_hl);
+ uint16_t igmphdr_offset = frag_offset ? 0 : 4;
- data = (unsigned char *) (igmp_pkt) + igmphdr_offset;
+ data = (unsigned char *)igmp_pkt + igmphdr_offset;
len -= link_offset + ip_hl + igmphdr_offset;
if ((int32_t) len < 0)
len = 0;
dump_packet(h, p, ip_proto, data, len,
- ip_src, ip_dst, igmp_pkt->igmp_type, igmp_pkt->igmp_code, 0,
- igmphdr_offset, fragmented, frag_offset, frag_id);
+ ip_src, ip_dst, igmp_pkt->igmp_type,
+ igmp_pkt->igmp_code, 0, igmphdr_offset,
+ fragmented, frag_offset, frag_id);
} break;
default:{
- data = (unsigned char *) (ip4_pkt) + ip_hl;
+ data = (unsigned char *)ip4_pkt + ip_hl;
len -= link_offset + ip_hl;
if ((int32_t) len < 0)
len = 0;
- dump_packet(h, p, ip_proto, data, len,
- ip_src, ip_dst, 0, 0, 0,
- 0, fragmented, frag_offset, frag_id);
+ dump_packet(h, p, ip_proto, data, len, ip_src, ip_dst,
+ 0, 0, 0, 0, fragmented, frag_offset, frag_id);
} break;
}
@@ -847,8 +856,8 @@ int8_t
re_match_func(unsigned char *data, uint32_t len, uint16_t *mindex,
uint16_t *msize)
{
- static int sub [2];
- int did_match = pcre_exec(pattern, 0, (const char *)data, (int32_t)len,
+ static int sub[2];
+ int did_match = pcre_exec(pattern, 0, (const char *)data, (int)len,
0, 0, 0, 0);
if (did_match < 0) {
switch (did_match) {
@@ -864,6 +873,14 @@ re_match_func(unsigned char *data, uint32_t len, uint16_t *mindex,
}
}
+ /*
+ * XXX: This doesn't actually do anything!
+ * We need to save the pattern offset/length for the first match in the
+ * pcre output vector.
+ *
+ * Pos = ovec[0]
+ * Len = ovec[1] - ovec[0]
+ */
*mindex = sub[0];
*msize = sub[1] - sub[0];
matches++;
@@ -1059,7 +1076,8 @@ get_filter_from_file(void)
warn("read %s", filter_file);
clean_exit(-1);
} else if (flen != st.st_size) {
- warn("short read of %s: expected %lld, got %zu", filter_file, st.st_size, flen);
+ warn("short read of %s: expected %lld, got %zu",
+ filter_file, st.st_size, flen);
clean_exit(-1);
}
for (char *s = fstr + 2; *s; s++)
@@ -1119,7 +1137,7 @@ print_time_absolute(struct pcap_pkthdr * h)
printf("%02u/%02u/%02u %02u:%02u:%02u.%06u ",
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour,
- t->tm_min, t->tm_sec, (uint32_t) h->ts.tv_usec);
+ t->tm_min, t->tm_sec, (uint32_t)h->ts.tv_usec);
}
void