]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Add a routine to do the "checksum with pseudo-header" stuff for IPv4.
authorGuy Harris <[email protected]>
Fri, 17 Jun 2011 08:09:16 +0000 (01:09 -0700)
committerGuy Harris <[email protected]>
Fri, 17 Jun 2011 08:09:16 +0000 (01:09 -0700)
Clean up some other stuff while we're at it.

ip.h
netdissect.h
print-dccp.c
print-ip.c
print-tcp.c
print-udp.c

diff --git a/ip.h b/ip.h
index fd53503e426c9683ceb9ae60d518d411dbbf3848..8a97632e515f13778b2d8b80380d0df6c567e6a8 100644 (file)
--- a/ip.h
+++ b/ip.h
@@ -161,4 +161,4 @@ struct      ip_timestamp {
 #define        IP_MSS          576             /* default maximum segment size */
 
 /* in print-ip.c */
-extern u_int32_t ip_finddst(const struct ip *);
+extern int nextproto4_cksum(const struct ip *, const u_int8_t *, u_int, u_int);
index 5363988f569242b837ae942e727b8402d9c2befb..700c71a424e08c098baef4b26cc44c0409d763fe 100644 (file)
@@ -352,6 +352,7 @@ extern void igmp_print(netdissect_options *,
                       register const u_char *, u_int);
 extern void igrp_print(netdissect_options *,const u_char *, u_int,
                       const u_char *);
+extern int nextproto4_cksum(const struct ip *, const u_int8_t *, u_int, u_int);
 extern void ipN_print(netdissect_options *,const u_char *, u_int);
 extern void ipx_print(netdissect_options *,const u_char *, u_int);
 extern void isoclns_print(netdissect_options *,const u_char *,
index 96aa7ab270c13fe97275d15d24f6e1a3528038ec..79ea5f72ee5fb0d2709402e8e21964e9c8cec418 100644 (file)
@@ -60,7 +60,7 @@ static const char *dccp_feature_nums[] = {
        "check data checksum",
 };
 
-static inline int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
+static inline u_int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
 {
        u_int cov;
        
@@ -73,31 +73,8 @@ static inline int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
 static int dccp_cksum(const struct ip *ip,
        const struct dccp_hdr *dh, u_int len)
 {
-       int cov = dccp_csum_coverage(dh, 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.mbz = 0;
-       ph.len = htons(len);
-       ph.proto = IPPROTO_DCCP;
-       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 *)(void *)dh;
-       vec[1].len = cov;
-       return in_cksum(vec, 2);
+       return nextproto4_cksum(ip, (const u_int8_t *)(void *)dh,
+           dccp_csum_coverage(dh, len), IPPROTO_DCCP);
 }
 
 #ifdef INET6
index 21f83c10eb92c2768bf17ac3d52e7a02a692f94d..d2df0a898c999fb46ed497be53d9b1fadb924e1e 100644 (file)
@@ -87,7 +87,7 @@ ip_printroute(register const u_char *cp, u_int length)
  * This is used for UDP and TCP pseudo-header in the checksum
  * calculation.
  */
-u_int32_t
+static u_int32_t
 ip_finddst(const struct ip *ip)
 {
        int length;
@@ -129,6 +129,39 @@ trunc:
        return retval;
 }
 
+/*
+ * Compute a V4-style checksum by building a pseudoheader.
+ */
+int
+nextproto4_cksum(const struct ip *ip, const u_int8_t *data,
+                u_int len, u_int next_proto)
+{
+       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 = next_proto;
+       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 = data;
+       vec[1].len = len;
+       return (in_cksum(vec, 2));
+}
+
 static void
 ip_printts(register const u_char *cp, u_int length)
 {
index 82840419181cf9d0ab6d7a48b6ef777e743f1fd9..88b461576df9a2cfe8d12d74ca87f91faff141cd 100644 (file)
@@ -129,30 +129,8 @@ static int tcp_cksum(register const struct ip *ip,
                     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
index 02a1e6ec5f46bb285a2cc21893a9bd7ba2f3b1f2..da6cfd388e799a695caf26d13e7ec26e1f5d0d07 100644 (file)
@@ -286,30 +286,8 @@ static int udp_cksum(register const struct ip *ip,
                     register const struct udphdr *up,
                     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_UDP;
-       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 *)(void *)up;
-       vec[1].len = len;
-       return (in_cksum(vec, 2));
+       return (nextproto4_cksum(ip, (const u_int8_t *)(void *)up, len,
+           IPPROTO_UDP));
 }
 
 #ifdef INET6