Skip to content

Commit

Permalink
ping,rdisc: Use macro to get odd byte when checksumming.
Browse files Browse the repository at this point in the history
Commit 636727f ("ping,rdisc: Optimize checksumming.") introduced
le16toh(), but it was not available on old systems.

Signed-off-by: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
  • Loading branch information
yoshfuji committed Nov 28, 2012
1 parent 636727f commit 83b1ff9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 10 additions & 1 deletion ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,15 @@ parse_reply(struct msghdr *msg, int cc, void *addr, struct timeval *tv)
return 0;
}


#if BYTE_ORDER == LITTLE_ENDIAN
# define ODDBYTE(v) (v)
#elif BYTE_ORDER == BIG_ENDIAN
# define ODDBYTE(v) ((u_short)(v) << 8)
#else
# define ODDBYTE(v) htons((u_short)(v) << 8)
#endif

u_short
in_cksum(const u_short *addr, register int len, u_short csum)
{
Expand All @@ -881,7 +890,7 @@ in_cksum(const u_short *addr, register int len, u_short csum)

/* mop up an odd byte, if necessary */
if (nleft == 1)
sum += le16toh((u_short)*(u_char *)w);
sum += ODDBYTE(*(u_char *)w); /* le16toh() may be unavailable on old systems */

/*
* add back carry outs from top 16 bits to low 16 bits
Expand Down
10 changes: 9 additions & 1 deletion rdisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,14 @@ pr_pack(char *buf, int cc, struct sockaddr_in *from)
* Checksum routine for Internet Protocol family headers (C Version)
*
*/
#if BYTE_ORDER == LITTLE_ENDIAN
# define ODDBYTE(v) (v)
#elif BYTE_ORDER == BIG_ENDIAN
# define ODDBYTE(v) ((u_short)(v) << 8)
#else
# define ODDBYTE(v) htons((u_short)(v) << 8)
#endif

u_short in_cksum(u_short *addr, int len)
{
register int nleft = len;
Expand All @@ -930,7 +938,7 @@ u_short in_cksum(u_short *addr, int len)

/* mop up an odd byte, if necessary */
if( nleft == 1 )
sum += le16toh(*(u_char *)w);
sum += ODDBYTE(*(u_char *)w); /* le16toh() may be unavailable on old systems */

/*
* add back carry outs from top 16 bits to low 16 bits
Expand Down

0 comments on commit 83b1ff9

Please sign in to comment.