xref: /freebsd/sbin/ping/utils.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
1*ff77ab83SAlan Somers /*-
2*ff77ab83SAlan Somers  * SPDX-License-Identifier: BSD-3-Clause
3*ff77ab83SAlan Somers  *
4*ff77ab83SAlan Somers  * Copyright (c) 1989, 1993
5*ff77ab83SAlan Somers  *	The Regents of the University of California.  All rights reserved.
6*ff77ab83SAlan Somers  *
7*ff77ab83SAlan Somers  * This code is derived from software contributed to Berkeley by
8*ff77ab83SAlan Somers  * Mike Muuss.
9*ff77ab83SAlan Somers  *
10*ff77ab83SAlan Somers  * Redistribution and use in source and binary forms, with or without
11*ff77ab83SAlan Somers  * modification, are permitted provided that the following conditions
12*ff77ab83SAlan Somers  * are met:
13*ff77ab83SAlan Somers  * 1. Redistributions of source code must retain the above copyright
14*ff77ab83SAlan Somers  *    notice, this list of conditions and the following disclaimer.
15*ff77ab83SAlan Somers  * 2. Redistributions in binary form must reproduce the above copyright
16*ff77ab83SAlan Somers  *    notice, this list of conditions and the following disclaimer in the
17*ff77ab83SAlan Somers  *    documentation and/or other materials provided with the distribution.
18*ff77ab83SAlan Somers  * 3. Neither the name of the University nor the names of its contributors
19*ff77ab83SAlan Somers  *    may be used to endorse or promote products derived from this software
20*ff77ab83SAlan Somers  *    without specific prior written permission.
21*ff77ab83SAlan Somers  *
22*ff77ab83SAlan Somers  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*ff77ab83SAlan Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*ff77ab83SAlan Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*ff77ab83SAlan Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*ff77ab83SAlan Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*ff77ab83SAlan Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*ff77ab83SAlan Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*ff77ab83SAlan Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*ff77ab83SAlan Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*ff77ab83SAlan Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*ff77ab83SAlan Somers  * SUCH DAMAGE.
33*ff77ab83SAlan Somers  */
34*ff77ab83SAlan Somers 
35*ff77ab83SAlan Somers #include <string.h>
36*ff77ab83SAlan Somers 
37*ff77ab83SAlan Somers #include "utils.h"
38*ff77ab83SAlan Somers 
39*ff77ab83SAlan Somers /*
40*ff77ab83SAlan Somers  * in_cksum --
41*ff77ab83SAlan Somers  *	Checksum routine for Internet Protocol family headers (C Version)
42*ff77ab83SAlan Somers  */
43*ff77ab83SAlan Somers u_short
in_cksum(u_char * addr,int len)44*ff77ab83SAlan Somers in_cksum(u_char *addr, int len)
45*ff77ab83SAlan Somers {
46*ff77ab83SAlan Somers 	int nleft, sum;
47*ff77ab83SAlan Somers 	u_char *w;
48*ff77ab83SAlan Somers 	union {
49*ff77ab83SAlan Somers 		u_short	us;
50*ff77ab83SAlan Somers 		u_char	uc[2];
51*ff77ab83SAlan Somers 	} last;
52*ff77ab83SAlan Somers 	u_short answer;
53*ff77ab83SAlan Somers 
54*ff77ab83SAlan Somers 	nleft = len;
55*ff77ab83SAlan Somers 	sum = 0;
56*ff77ab83SAlan Somers 	w = addr;
57*ff77ab83SAlan Somers 
58*ff77ab83SAlan Somers 	/*
59*ff77ab83SAlan Somers 	 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
60*ff77ab83SAlan Somers 	 * sequential 16 bit words to it, and at the end, fold back all the
61*ff77ab83SAlan Somers 	 * carry bits from the top 16 bits into the lower 16 bits.
62*ff77ab83SAlan Somers 	 */
63*ff77ab83SAlan Somers 	while (nleft > 1)  {
64*ff77ab83SAlan Somers 		u_short data;
65*ff77ab83SAlan Somers 
66*ff77ab83SAlan Somers 		memcpy(&data, w, sizeof(data));
67*ff77ab83SAlan Somers 		sum += data;
68*ff77ab83SAlan Somers 		w += sizeof(data);
69*ff77ab83SAlan Somers 		nleft -= sizeof(data);
70*ff77ab83SAlan Somers 	}
71*ff77ab83SAlan Somers 
72*ff77ab83SAlan Somers 	/* mop up an odd byte, if necessary */
73*ff77ab83SAlan Somers 	if (nleft == 1) {
74*ff77ab83SAlan Somers 		last.uc[0] = *w;
75*ff77ab83SAlan Somers 		last.uc[1] = 0;
76*ff77ab83SAlan Somers 		sum += last.us;
77*ff77ab83SAlan Somers 	}
78*ff77ab83SAlan Somers 
79*ff77ab83SAlan Somers 	/* add back carry outs from top 16 bits to low 16 bits */
80*ff77ab83SAlan Somers 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
81*ff77ab83SAlan Somers 	sum += (sum >> 16);			/* add carry */
82*ff77ab83SAlan Somers 	answer = ~sum;				/* truncate to 16 bits */
83*ff77ab83SAlan Somers 	return(answer);
84*ff77ab83SAlan Somers }
85