icmp_cksum.c

来自「压缩包中包含LINUX下多个命令的源码」· C语言 代码 · 共 49 行

C
49
字号
/* Copyright (C) 1998, 2001 Free Software Foundation, Inc.   This file is part of GNU Inetutils.   GNU Inetutils is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 2, or (at your option)   any later version.   GNU Inetutils is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR PARTICULAR PURPOSE.  See the   GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with GNU Inetutils; see the file COPYING.  If not, write to   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,   Boston, MA 02111-1307, USA. */#ifdef HAVE_CONFIG_H# include <config.h>#endif#include <sys/types.h>#include <unistd.h>u_shorticmp_cksum (u_char *addr, int len){  register int sum = 0;  u_short answer = 0;  u_short *wp;  for (wp = (u_short*)addr; len > 1; wp++, len -= 2)    sum += *wp;  /* Take in an odd byte if present */  if (len == 1)    {      *(u_char *)&answer = *(u_char*)wp;      sum += answer;    }  sum = (sum >> 16) + (sum & 0xffff);	/* add high 16 to low 16 */  sum += (sum >> 16);			/* add carry */  answer = ~sum;			/* truncate to 16 bits */  return answer;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?