register const struct tcphdr *tp,
register u_int len)
{
- struct phdr {
- u_int32_t src;
- u_int32_t dst;
- u_char mbz;
- u_char proto;
- u_int16_t len;
- } ph;
- struct cksum_vec vec[2];
-
- /* pseudo-header.. */
- ph.len = htons((u_int16_t)len);
- ph.mbz = 0;
- ph.proto = IPPROTO_TCP;
- memcpy(&ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
- if (IP_HL(ip) == 5)
- memcpy(&ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
- else
- ph.dst = ip_finddst(ip);
-
- vec[0].ptr = (const u_int8_t *)(void *)&ph;
- vec[0].len = sizeof(ph);
- vec[1].ptr = (const u_int8_t *)tp;
- vec[1].len = len;
- return in_cksum(vec, 2);
+ return (nextproto4_cksum(ip, (const u_int8_t *)tp, len,
+ IPPROTO_TCP));
}
void
return;
}
- if (IP_V(ip) == 4 && vflag && !Kflag && !fragmented) {
+ if (vflag && !Kflag && !fragmented) {
+ /* Check the checksum, if possible. */
u_int16_t sum, tcp_sum;
- if (TTEST2(tp->th_sport, length)) {
- sum = tcp_cksum(ip, tp, length);
- (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
- if (sum != 0) {
+ if (IP_V(ip) == 4) {
+ if (TTEST2(tp->th_sport, length)) {
+ sum = tcp_cksum(ip, tp, length);
tcp_sum = EXTRACT_16BITS(&tp->th_sum);
- (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum, sum));
- } else
- (void)printf(" (correct)");
+
+ (void)printf(", cksum 0x%04x", tcp_sum);
+ if (sum != 0)
+ (void)printf(" (incorrect -> 0x%04x)",
+ in_cksum_shouldbe(tcp_sum, sum));
+ else
+ (void)printf(" (correct)");
+ }
}
- }
#ifdef INET6
- if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !Kflag && !fragmented) {
- u_int16_t sum,tcp_sum;
- if (TTEST2(tp->th_sport, length)) {
- sum = nextproto6_cksum(ip6, (u_short *)tp, length, IPPROTO_TCP);
- (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
- if (sum != 0) {
+ else if (IP_V(ip) == 6 && ip6->ip6_plen) {
+ if (TTEST2(tp->th_sport, length)) {
+ sum = nextproto6_cksum(ip6, (const u_int8_t *)tp, length, IPPROTO_TCP);
tcp_sum = EXTRACT_16BITS(&tp->th_sum);
- (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum, sum));
- } else
- (void)printf(" (correct)");
+ (void)printf(", cksum 0x%04x", tcp_sum);
+ if (sum != 0)
+ (void)printf(" (incorrect -> 0x%04x)",
+ in_cksum_shouldbe(tcp_sum, sum));
+ else
+ (void)printf(" (correct)");
+
+ }
}
- }
#endif
+ }
length -= hlen;
if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST)) {