⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 177.htm

📁 unix高级编程原吗
💻 HTM
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>123</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center">               ● UNIX网络编程                       (BM: clown)                </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p   align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="155.htm">上一层</a>][<a href="178.htm">下一篇</a>]
<hr><p align="left"><small>发信人: digger (欧阳疯), 信区: Socket <br>

标  题: RAW sockets example code: in_cksum.c <br>

发信站: 华南网木棉站 (Wed Aug  5 19:57:33 1998), 转信 <br>

  <br>

/* <br>

 * Copyright (c) 1989, 1993 <br>

 *      The Regents of the University of California.  All rights reserved. <br>

 * <br>

 * This code is derived from software contributed to Berkeley by <br>

 * Mike Muuss. <br>

 * <br>

 * Redistribution and use in source and binary forms, with or without <br>

 * modification, are permitted provided that the following conditions <br>

 * are met: <br>

 * 1. Redistributions of source code must retain the above copyright <br>

 *    notice, this list of conditions and the following disclaimer. <br>

 * 2. Redistributions in binary form must reproduce the above copyright <br>

 *    notice, this list of conditions and the following disclaimer in the <br>

 *    documentation and/or other materials provided with the distribution. <br>

 * 3. All advertising materials mentioning features or use of this software <br>

 *    must display the following acknowledgement: <br>

 *      This product includes software developed by the University of <br>

 *      California, Berkeley and its contributors. <br>



 * 4. Neither the name of the University nor the names of its contributors <br>

 *    may be used to endorse or promote products derived from this software <br>

 *    without specific prior written permission. <br>

 * <br>

 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND <br>

 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE <br>

 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE <br>

 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE <br>

 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL <br>

 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS <br>

 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) <br>

 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT <br>

 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY <br>

 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF <br>

 * SUCH DAMAGE. <br>

 */ <br>

  <br>

#include <stdlib.h> <br>

#include <stdio.h> <br>

#include <sys/types.h> <br>

  <br>

/* <br>

/* <br>

 * in_cksum -- <br>

 *      Checksum routine for Internet Protocol family headers (C Version) <br>

 */ <br>

unsigned short in_cksum(unsigned short *addr,int len) <br>

{ <br>

        register int sum = 0; <br>

        u_short answer = 0; <br>

        register u_short *w = addr; <br>

        register int nleft = len; <br>

  <br>

        /* <br>

         * Our algorithm is simple, using a 32 bit accumulator (sum), we add <br>

         * sequential 16 bit words to it, and at the end, fold back all the <br>

         * carry bits from the top 16 bits into the lower 16 bits. <br>

         */ <br>

        while (nleft > 1)  { <br>

                sum += *w++; <br>

                nleft -= 2; <br>

        } <br>

  <br>

        /* mop up an odd byte, if necessary */ <br>

        if (nleft == 1) { <br>



                *(u_char *)(&answer) = *(u_char *)w ; <br>

                sum += answer; <br>

        } <br>

  <br>

        /* add back carry outs from top 16 bits to low 16 bits */ <br>

        sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */ <br>

        sum += (sum >> 16);                     /* add carry */ <br>

        answer = ~sum;                          /* truncate to 16 bits */ <br>

        return(answer); <br>

} <br>

  <br>

-- <br>

                #################### <br>

                #  遥  眺  淑  女  # <br>

                ####################################### <br>

                                   #  君  子  好  球  # <br>

                                   #################### <br>

</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="155.htm">上一层</a>][<a href="178.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</table>
</body>
</html>

⌨️ 快捷键说明

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