<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>1129</title>
	<atom:link href="http://www.vi1129.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vi1129.com</link>
	<description>学无止境 我心飞翔</description>
	<lastBuildDate>Mon, 05 Jul 2010 15:30:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>邻居发现(Neighbor Discovery)协议及攻击防范</title>
		<link>http://www.vi1129.com/2010/07/nd-protocol/</link>
		<comments>http://www.vi1129.com/2010/07/nd-protocol/#comments</comments>
		<pubDate>Mon, 05 Jul 2010 15:27:10 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[知识随记]]></category>
		<category><![CDATA[neighbor discovery]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=725</guid>
		<description><![CDATA[<p style="text-align: right;">&#60;转载请注明出处&#62;</p>
<p>1. ND协议介绍</p>
<p>邻居发现协议（Neighbor Discovery Protocol，以下称ND协议）是IPv6的一个关键协议，可以说，ND协议是IPv4某些协议在IPv6中综合起来的升级和改进，如ARP、ICMP路由器发现和ICMP重定向等协议。当然，作为IPv6的基础性协议，ND还提供了其他功能，如前缀发现、邻居不可达检测、重复地址检测、地址自动配置等。</p>
<p>1.1 IPv6协议格式</p>
<p></p>
<p>Version(4-bit):IP版本，该字段值为6</p>
<p>Traffic Class(8-bit):流量分类，与IPv4包头的TOS类似</p>
<p>Flow Label(20-bit):流标签，用来标记IPv6数据包的一个流</p>
<p>Payload Length(16-bit):有效载荷的长度，有效载荷为紧跟IPv6包头的数据包的其他部分</p>
<p>Next Header(8-bit):处理选项字段，分段，安全，移动性，松散源路由，记录路由等的新方式。该字段在承载ND报文时字段值为58（ICMPv6）。该字段详细字段值对应如下表</p>



Value
Type of Header


0
Hop-by-Hop Options Header


6
TCP


17
UDP


41
Encapsulated IPv6 Header 


43
Routing Header


44
Fragment Header


50
Encapsulating Security Payload


51
Authentication Header 


58
ICMPv6 


59
No next header


60
Destination Options Header 



<p>Hop Limit:(8-bit):定义IP数据包所能经过的最大跳数，每跳一次将此值减1</p>
<p>Source Address(128-bit):IP数据包的源地址</p>
<p>Destination Address(128-bit):IP数据包的目的地址</p>
<p>1.2 ND报文类型</p>
<p>ND协议定义的报文使用ICMP承载，其类型包括：路由器请求报文(RS)、路由器通告(RA)报文、邻居请求报文(NS)、邻居通告报文(NA)和重定向报文。</p>
<p>1.2.1 路由器请求报文RS Router Solicitation Message</p>
<p>主机启动后，通过RS消息向路由器发出请求，期望路由器立即发送RA消息响应。RS报文格式如下图</p>
<p></p>

Type 类型字段，值133
Code 代码字段，值0
Options 选项字段

源链路层地址选项 仅用于已编址的链路层可用于RS报文的只有源链路层地址选项，表明该报文发送者的链路层地址。如果IPv6头的源地址为未指定地址，则不能包括该选项



<p>同时，IPv6报文头中的字段要求如下：</p>

Hop Limit 值255，保证是本地链路上的报文
Source Address 发送接口的本地链路地址或未指定地址
Destination Address 本地链路中所有路由器的组播地址FF02::2

<p>1.2.2 路由器通告报文RA Router Advertisement Message</p>
<p>路由器周期性的发布RA消息，其中包括前缀和一些标志位的信息，或者以RA报文响应路由器请求报文RS。RA的报文格式如下图。</p>
<p></p>

Type 类型字段，值134
Code 代码字段，值0
M管理地址配置标识（Managed address <p><a href="http://www.vi1129.com/2010/07/nd-protocol/">继续阅读</a></p>


No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: right;"><em><span style="color: #999999;">&lt;转载请注明出处&gt;</span></em></p>
<p>1. ND协议介绍</p>
<p>邻居发现协议（Neighbor Discovery Protocol，以下称ND协议）是IPv6的一个关键协议，可以说，ND协议是IPv4某些协议在IPv6中综合起来的升级和改进，如ARP、ICMP路由器发现和ICMP重定向等协议。当然，作为IPv6的基础性协议，ND还提供了其他功能，如前缀发现、邻居不可达检测、重复地址检测、地址自动配置等。</p>
<p>1.1 IPv6协议格式</p>
<p><a href="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic1.jpg"><img class="aligncenter size-full wp-image-711" title="NDP-graphic1" src="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic1.jpg" alt="NDP-graphic1" width="547" height="341" /></a></p>
<p>Version(4-bit):IP版本，该字段值为6</p>
<p>Traffic Class(8-bit):流量分类，与IPv4包头的TOS类似</p>
<p>Flow Label(20-bit):流标签，用来标记IPv6数据包的一个流</p>
<p>Payload Length(16-bit):有效载荷的长度，有效载荷为紧跟IPv6包头的数据包的其他部分</p>
<p>Next Header(8-bit):处理选项字段，分段，安全，移动性，松散源路由，记录路由等的新方式。该字段在承载ND报文时字段值为58（ICMPv6）。该字段详细字段值对应如下表</p>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td width="29%" valign="top"><strong>Value</strong></td>
<td width="70%" valign="top"><strong>Type of Header</strong></td>
</tr>
<tr>
<td width="29%" valign="top"><strong>0</strong></td>
<td width="70%" valign="top"><strong>Hop-by-Hop Options Header</strong></td>
</tr>
<tr>
<td width="29%" valign="top"><strong>6</strong></td>
<td width="70%" valign="top"><strong>TCP</strong></td>
</tr>
<tr>
<td width="29%" valign="top"><strong>17</strong></td>
<td width="70%" valign="top"><strong>UDP</strong></td>
</tr>
<tr>
<td width="29%" valign="top"><strong>41</strong></td>
<td width="70%" valign="top"><strong>Encapsulated IPv6 Header </strong></td>
</tr>
<tr>
<td width="29%" valign="top"><strong>43</strong></td>
<td width="70%" valign="top"><strong>Routing Header</strong></td>
</tr>
<tr>
<td width="29%" valign="top"><strong>44</strong></td>
<td width="70%" valign="top"><strong>Fragment Header</strong></td>
</tr>
<tr>
<td width="29%" valign="top"><strong>50</strong></td>
<td width="70%" valign="top"><strong>Encapsulating Security Payload</strong></td>
</tr>
<tr>
<td width="29%" valign="top"><strong>51</strong></td>
<td width="70%" valign="top"><strong>Authentication Header </strong></td>
</tr>
<tr>
<td width="29%" valign="top"><strong>58</strong></td>
<td width="70%" valign="top"><strong>ICMPv6 </strong></td>
</tr>
<tr>
<td width="29%" valign="top"><strong>59</strong></td>
<td width="70%" valign="top"><strong>No next header</strong></td>
</tr>
<tr>
<td width="29%" valign="top"><strong>60</strong></td>
<td width="70%" valign="top"><strong>Destination Options Header </strong></td>
</tr>
</tbody>
</table>
<p>Hop Limit:(8-bit):定义IP数据包所能经过的最大跳数，每跳一次将此值减1</p>
<p>Source Address(128-bit):IP数据包的源地址</p>
<p>Destination Address(128-bit):IP数据包的目的地址<span id="more-725"></span></p>
<p>1.2 ND报文类型</p>
<p>ND协议定义的报文使用ICMP承载，其类型包括：路由器请求报文(RS)、路由器通告(RA)报文、邻居请求报文(NS)、邻居通告报文(NA)和重定向报文。</p>
<p><em>1.2.1 </em><em>路由器请求报文RS Router Solicitation Message</em></p>
<p>主机启动后，通过RS消息向路由器发出请求，期望路由器立即发送RA消息响应。RS报文格式如下图</p>
<p><a href="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic2.jpg"><img class="aligncenter size-full wp-image-712" title="NDP-graphic2" src="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic2.jpg" alt="NDP-graphic2" width="416" height="211" /></a></p>
<ul>
<li>Type 类型字段，值133</li>
<li>Code 代码字段，值0</li>
<li>Options 选项字段
<ul>
<li>源链路层地址选项 仅用于已编址的链路层可用于RS报文的只有源链路层地址选项，表明该报文发送者的链路层地址。如果IPv6头的源地址为未指定地址，则不能包括该选项</li>
</ul>
</li>
</ul>
<p>同时，IPv6报文头中的字段要求如下：</p>
<ul>
<li>Hop Limit 值255，保证是本地链路上的报文</li>
<li>Source Address 发送接口的本地链路地址或未指定地址</li>
<li>Destination Address 本地链路中所有路由器的组播地址FF02::2</li>
</ul>
<p><em>1.2.2 </em><em>路由器通告报文RA Router Advertisement Message</em></p>
<p>路由器周期性的发布RA消息，其中包括前缀和一些标志位的信息，或者以RA报文响应路由器请求报文RS。RA的报文格式如下图。</p>
<p><a href="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic3.jpg"><img class="aligncenter size-full wp-image-713" title="NDP-graphic3" src="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic3.jpg" alt="NDP-graphic3" width="416" height="287" /></a></p>
<ul>
<li>Type 类型字段，值134</li>
<li>Code 代码字段，值0</li>
<li>M管理地址配置标识（Managed address configuration）
<ul>
<li>0－无状态地址分配，客户端通过无状态协议（如ND）获得IPv6地址；</li>
<li>1－有状态地址分配，客户端通过有状态协议（如DHCPv6）获得IPv6地址。</li>
</ul>
</li>
<li>O 其它有状态配置标识（Other stateful configuration）
<ul>
<li>0－客户端通过无状态协议（如ND）获取除地址外的其他配置信息；</li>
<li>1－客户端通过有状态协议（如DHCPv6）获取除地址外的其他配置信息，如DNS、SIP服务器信息。协议规定，若M标记置为1，则O标记也应置为1，否则无意义。</li>
</ul>
</li>
<li>Router Lifetime 缺省路由器的生命周期（单位：秒）</li>
</ul>
<p>表示发送该RA报文的路由器作为缺省路由器的生命周期。Router Lifetime最长9000秒小时，缺省值30分钟。如果该字段为0时，表示该路由器不能作为缺省路由器，但RA报文的其他信息仍然有效。</p>
<ul>
<li>Reachable Time 可达时间（单位：毫秒）</li>
</ul>
<p>发送NS报文后，在得到邻居可达性确认后，认为邻居可达的时间，0表示不指定。路由器在接口上通过发送RA报文，让同一链路上的所有节点都使用相同的可达时间。若Reachable Time为0，表示路由器不指定该字段参数。该值可配置，RA报文中缺省值为0。</p>
<ul>
<li>Retrans Timer 重传定时器（单位：毫秒）</li>
</ul>
<p>重传NS报文的时间间隔，用于邻居不可达检测和地址解析。若该值为0，表示路由器不指定该字段参数。该值可配置，RA报文缺省值为0。</p>
<ul>
<li>Options 选项字段
<ul>
<li>源链路层地址选项 仅用于已编址的链路层。当路由器正在多个链路层地址上进行负荷分担时，要忽略该选项。</li>
<li>MTU选项 链路的可变MTU</li>
<li>前缀信息选项 用于地址自动配置的前缀信息，可包含多个</li>
<li>路由信息选项 用于主机生成缺省路由，表明前缀通过该路由器是可达的</li>
</ul>
</li>
</ul>
<p>同时，IPv6报文头中的字段要求如下：</p>
<ul>
<li>Source Address 必须是发送接口的本地链路地址</li>
<li>Destination Address 组播地址FF02::1，或发送RS请求报文的主机的单播地址</li>
</ul>
<p><em>1.2.3 </em><em>邻居请求报文NS Neighbor Solicitation Message</em></p>
<p>主机通过NS消息可以得到邻居的链路层地址、检查邻居是否可达、重复地址检测等。</p>
<p><a href="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic4.jpg"><img class="aligncenter size-full wp-image-714" title="NDP-graphic4" src="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic4.jpg" alt="NDP-graphic4" width="416" height="287" /></a></p>
<ul>
<li>Type 类型字段，值135</li>
<li>Code 代码字段，值0</li>
<li>Target Address 目标地址，16bytes</li>
</ul>
<p>请求目标的IP地址，不能是组播地址，可以是本地链路、本地站点、全局地址。</p>
<ul>
<li>Options 选项字段
<ul>
<li>源链路层地址选项 ，和RS报文的选项相同，可用于NS报文的只有源链路层地址选项。源链路层选项仅用于已编址的链路层，如果IPv6头的源地址为未指定地址，则不能包括该选项。</li>
</ul>
</li>
</ul>
<p>同时，IPv6报文头中字段要求如下：</p>
<ul>
<li>Source Address 发送接口的地址或未指定地址（DAD检测）</li>
<li>Destination Address 目的节点单播地址或目的节点地址对应的请求节点组播地址</li>
</ul>
<p>需要说明，如果源地址为未指定地址，则目的地址应为被请求节点的组播地址，并且没有源链路层地址选项。</p>
<p><em>1.2.4 </em><em>邻居通告报文</em><em>NA Neighbor Advertisement Message</em></p>
<p>NA报文是主机对NS的响应报文，同时主机在链路层地址变化时也可以主动发送NA消息，以通知相邻节点自己的链路层地址或者角色发生改变。</p>
<p><strong><em><a href="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic5.jpg"><img class="aligncenter size-full wp-image-715" title="NDP-graphic5" src="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic5.jpg" alt="NDP-graphic5" width="421" height="288" /></a></em></strong></p>
<ul>
<li>Type 类型字段，值136</li>
<li>Code 代码字段，值0</li>
<li>R 路由器标记（Router Flag）</li>
</ul>
<p>表示NA报文发送者的角色。置位表示发送者是路由器，复位表示发送者为主机。</p>
<ul>
<li>S 请求标记（Solicited Flag）</li>
</ul>
<p>置位表示为响应单播NS发送的NA报文。在NUD探测中，S标记作为邻居可达性确认标记；在组播通告或非请求单播通告中，S标记必须清零，如DAD检测中NS报文用的是被请求节点的组播地址，当地址冲突时，回应的NA报文的S标记需要清零。</p>
<ul>
<li>O 重载标记（Override Flag）</li>
</ul>
<p>1－表示需要用目标链路层地址选项中的链路层地址，来更新邻居缓存表。</p>
<p>0－只有在链路层地址未知时，才能用目标链路层地址选项来更新邻居缓存表。</p>
<ul>
<li>Target Address 目标地址，16bytes</li>
</ul>
<p>如果是针对NS而发送NA，该字段直接拷贝NS报文目标地址；如果不是针对NS发送的NA，该地址是链路层地址发生改变的网络节点的IP地址。目标地址不能是组播地址。</p>
<ul>
<li>Options 选项字段，只能是目的链路层地址选项，通告发送者的链路层地址。</li>
</ul>
<p>同时，IPv6报文头中字段要求如下：</p>
<ul>
<li>Source Address 必须是发送接口的单播地址</li>
<li>Destination Address 单播地址或者所有节点的组播地址（FF02::1）</li>
</ul>
<p>1.2.5 <em>重定向报文 Redirect Message</em></p>
<p>路由器通过重定向报文通知主机到目的地有更好的下一跳地址，或者通知主机目的地址为本网段邻居。</p>
<p><a href="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic6.jpg"><img class="aligncenter size-full wp-image-716" title="NDP-graphic6" src="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic6.jpg" alt="NDP-graphic6" width="421" height="372" /></a></p>
<ul>
<li>Type 类型字段，值137</li>
<li>Code 代码字段，值0</li>
<li>Target Address 目标地址，16bytes</li>
</ul>
<p>到达目的地址的下一跳地址。如果目标为路由器（本地链路外的报文），必须使用路由器的本地链路地址；如果是主机（本地链路报文），目标地址和目的地址必须一致。</p>
<ul>
<li>Destination Address 目的地址，即IPv6头部的目的地址，16bytes</li>
<li>Options 选项字段
<ul>
<li>目标链路层地址选项：新下一跳的链路层地址。</li>
<li>重定向头选项：触发报文尽可能多的部分，但不应使重定向报文超过1280bytes。</li>
</ul>
</li>
</ul>
<p>同时，IPv6报文头中字段要求如下：</p>
<ul>
<li>Source Address 发送接口的本地链路地址</li>
<li>Destination Address 触发重定向的数据报文的单播地址</li>
</ul>
<p>1.3 ND主要功能</p>
<p><em>1.3.1 </em><em>路由器发现</em></p>
<p>路由器发现是指主机怎样定位本地链路上路由器和确定其配置信息的过程，主要包含以下三方面的内容：</p>
<ul>
<li>路由器发现</li>
<li>前缀发现</li>
<li>参数发现</li>
</ul>
<p>在路由器通告报文RA中承载着路由器的相关信息，ND协议通过RS和RA的报文交互完成路由器发现、前缀发现和参数发现三大功能。协议交互主要有两种情况：主机请求触发路由器通告和路由器定期发送路由器通告。</p>
<p><em>1.3.2 </em><em>地址解析</em></p>
<p>地址解析是指某节点在知道目的节点IP地址的情况下，确定目的链路层地址的过程。</p>
<p>当一个节点需要得到同一本地链路上另外一个节点的链路层地址时，需要进行地址解析，该机制类似于IPv4中的ARP。 ND协议用NS和NA报文完成IPv6地址到链路层地址的解析，解析后的链路层地址和IP地址等信息用来建立节点的一个邻居缓存表项。</p>
<p><em>1.3.3 </em><em>邻居不可达检测NUD</em></p>
<p>邻居不可达检测是节点怎样确定邻居不可达状态的过程。ND协议用NS和NA报文来验证邻居节点的可达性。</p>
<p><em>1.3.4 </em><em>重复地址检测DAD</em></p>
<p>重复地址检测是节点确定即将使用的地址是否被另一节点使用的过程。</p>
<p>在节点自动配置某个接口的IPv6单播地址之前，必须在本地链路范围内验证要使用的临时地址是唯一的，并且未被其他节点使用过。ND协议使用NS/NA报文完成该功能。</p>
<p>2. ND常见攻击</p>
<p><em>2.1 重复地址检测DAD攻击</em></p>
<p>在结点使用无状态协议自动配置时发送NS报文，NS报文中附带了结点准备使用的地址target address,攻击者伪造NA报文，回复称该地址已被使用，迫使结点更换target address，如此反复，结点将无法完成无状态协议自动配置。</p>
<p>针对DAD攻击，可以采用ND snooping,DHCP snooping,ND detection技术防范。</p>
<p>DHCP snooping与IPv4一样，通过监听DHCP的交互进程，获得用户的IP,MAC,连接的交换机端口号的绑定表项。ND snooping与前者很类似，监听ND协议的DAD交互过程，获取用户的IP、MAC、Port的对应关系。</p>
<p>ND Detection是H3C在ND防攻击方面的创新技术，通过结合已经建立的绑定表项，这个绑定表项可以是使用ND snooping，DHCP snooping，手工来进行配置的，通过比较已经获取到的可信表项中的IP与MAC地址，对ND报文进行过滤，丢弃异常的报文。相比单纯使用ACL进行的用户绑定，ND Detection技术结合绑定表项对ND报文进行过滤，能够较好的防御ND攻击。</p>
<p><em>2.2 地址欺骗</em></p>
<p>地址欺骗类似IPv4中的ARP欺骗，攻击者伪造RS/NS/NA报文来修改受害主机或网关上受害主机的MAC地址，造成受害主机无法与网络进行正常的通信。</p>
<p>防范方法与DAD攻击防范方法类似。</p>
<p><em>2.3 网络配置参数攻击(RA伪造攻击)</em></p>
<p>RA能够携带很多网络配置信息，包括默认路由器，网络前缀列表，是否使用DHCP服务器进行有状态地址分配等网络配置的关键信息。如果受害者接收了虚假的RA信息，会造成网络配置错误，从而引发欺骗攻击。</p>
<p>防范方法为设置RA信任端口，将来自不信任端口的RA报文丢弃。</p>
<p><em>2.4 针对网关的泛洪攻击</em></p>
<p>通过发送大量的NS/RS报文，造成网关的表项溢出。</p>
<p>为了避免网关的ND表项被异常的ND报文打满溢出造成DoS攻击，在网关上能够针对端口配置最大的ND表项学习数量，能够缓解此类攻击。</p>
<p>3. ND snooping实现机制</p>
<p>以下为简单翻译结果，详细请参考 IETF草案Control Packet Snooping Based Binding draft-bi-savi-cps-00.txt 8.3节</p>
<p><em>3.1 DAD交互过程</em></p>
<p>根据本文1.3.4重复地址检测DAD的描述，主机在自动配置或手动配置IPv6地址时，会通过NS/NA报文检测是否存在重复地址。详细步骤如下：</p>
<ul>
<li>主机发送NS请求，并设置如下字段值
<ul>
<li>source address为0.0.0.0</li>
<li>desnation address为ff02::1(组播地址)</li>
<li>target address为主机准备配置的地址，DAD即检测这个地址是否在网络上有重复</li>
<li>Hop Limit字段值为255，主要是为了防止一跳以外的节点伪造ND报文</li>
</ul>
</li>
<li>链路内收到该NS报文的主机将检测自身地址是否和target address重复，不重复则忽略，重复则发送NA报文，如果target address也是自己准备配置的地址，则放弃该地址并忽略NS报文</li>
<li>回复的NA报文的target address是从NS报文target address段直接复制过来，destination address 为组播地址</li>
</ul>
<p><em>3.2 ND snooping监听过程</em></p>
<p>ND snooping则监听DAD交互过程，产生MAC-IP对应表filter table.步骤如下：</p>
<ul>
<li>交换机收到DAD过程中的NS报文，将NS中的target address插入监听列表BST(Binding State Table)（如果这个地址不在BST中的话）设置ND snooping状态为SAC_START，初始化计时器MAX_DAD_DELAY</li>
<li>MAX_DAD_DELAY超时前如果收到其他主机的NA回复报文，则删除BST中该地址信息，本次ND snooping监听结束，反之设置ND snooping状态为SAC_BOUND，初始化计时器MAX_SAC_LIFETIME，将该地址信息加入FT(filter table)</li>
<li>计时器MAX_SAC_LIFETIME超时后， 重设计时器MAX_DAD_PREPARE_DELAY，并发送NS报文到这个地址以期望得到它的MAC地址，在MAX_DAD_PREPARE_DELAY超时前如果没有收到NA回复，则删除BST,FT中的该地址信息，ND snooping监听结束，反之取出NA报文中的MAC地址，加入FT中，并设置计时器MAX_SAC_LIFETIME，ND snooping状态为SAC_QUERY</li>
<li>计时器MAX_SAC_LIFETIME超时后，交换机将返回上一步重新发送NS报文，以确认该地址是否正常，是否需要更新BST,FT。</li>
</ul>
<p><em>3.3 ND snooping流程图</em></p>
<p><a href="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic7.jpg"><img class="aligncenter size-full wp-image-720" title="NDP-graphic7" src="http://www.vi1129.com/wp-content/uploads/2010/07/NDP-graphic7.jpg" alt="NDP-graphic7" width="454" height="789" /></a></p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/07/nd-protocol/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>UNIX常用的IP表示掩码方式转换为字符串的程序实现方法</title>
		<link>http://www.vi1129.com/2010/05/unix%e5%b8%b8%e7%94%a8%e7%9a%84ip%e8%a1%a8%e7%a4%ba%e6%8e%a9%e7%a0%81%e6%96%b9%e5%bc%8f%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e7%a8%8b%e5%ba%8f%e5%ae%9e%e7%8e%b0%e6%96%b9/</link>
		<comments>http://www.vi1129.com/2010/05/unix%e5%b8%b8%e7%94%a8%e7%9a%84ip%e8%a1%a8%e7%a4%ba%e6%8e%a9%e7%a0%81%e6%96%b9%e5%bc%8f%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e7%a8%8b%e5%ba%8f%e5%ae%9e%e7%8e%b0%e6%96%b9/#comments</comments>
		<pubDate>Fri, 21 May 2010 11:22:02 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[程序人生]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/2010/05/unix%e5%b8%b8%e7%94%a8%e7%9a%84ip%e8%a1%a8%e7%a4%ba%e6%8e%a9%e7%a0%81%e6%96%b9%e5%bc%8f%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e7%a8%8b%e5%ba%8f%e5%ae%9e%e7%8e%b0%e6%96%b9/</guid>
		<description><![CDATA[<p>一种IP的表示方式为192.168.1.1/24，其数字24的含义为24位掩码，即掩码为255.255.255.0，程序中读入的类似192.168.1.1/24的字符串可能需要转换为IP地址和掩码两个字符串保存或处理，即将192.168.1.1/24转换为192.168.1.1和255.255.255.0，一种程序的实现方法如下</p>

?View Code C#include &#60;stdio.h&#62;
#include &#60;sys/socket.h&#62;
#include &#60;netinet/in.h&#62;
#include &#60;arpa/inet.h&#62;
#include &#60;string.h&#62;
int main&#40;&#41;
&#123;
	char ip&#91;&#93; = &#34;192.168.1.1/24&#34;;
	uint8_t *ipPrint = NULL;
	uint32_t ipAddr;
	uint32_t *ipPtr = &#38;ipAddr;
	uint32_t mask = 0;
	uint32_t value = 0;
	uint32_t i = 0, j = 0;
	uint8_t *ptr = NULL;
	struct in_addr in;
	ptr = strtok&#40;ip, &#34;/&#34;&#41;;
	inet_aton&#40;ptr, &#40;struct in_addr *&#41;ipPtr&#41;;
&#160;
	ptr = strtok&#40;NULL, &#34;/&#34;&#41;;
	value = atoi&#40;ptr&#41;;
&#160;
	for&#40;i = 0; i&#60;value; i++&#41;
	&#123;
		j = value-i-1;
		mask &#124;= 1&#60;&#60;j;
	&#125;
	printf&#40;&#34;%.8x\n%.8x\n&#34;,ipAddr,mask&#41;;
	in.s_addr <p><a href="http://www.vi1129.com/2010/05/unix%e5%b8%b8%e7%94%a8%e7%9a%84ip%e8%a1%a8%e7%a4%ba%e6%8e%a9%e7%a0%81%e6%96%b9%e5%bc%8f%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e7%a8%8b%e5%ba%8f%e5%ae%9e%e7%8e%b0%e6%96%b9/">继续阅读</a></p>


No related posts.]]></description>
			<content:encoded><![CDATA[<p>一种IP的表示方式为192.168.1.1/24，其数字24的含义为24位掩码，即掩码为255.255.255.0，程序中读入的类似192.168.1.1/24的字符串可能需要转换为IP地址和掩码两个字符串保存或处理，即将192.168.1.1/24转换为192.168.1.1和255.255.255.0，一种程序的实现方法如下</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p697code2'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6972"><td class="code" id="p697code2"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/socket.h&gt;</span>
<span style="color: #339933;">#include &lt;netinet/in.h&gt;</span>
<span style="color: #339933;">#include &lt;arpa/inet.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">char</span> ip<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;192.168.1.1/24&quot;</span><span style="color: #339933;">;</span>
	uint8_t <span style="color: #339933;">*</span>ipPrint <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	uint32_t ipAddr<span style="color: #339933;">;</span>
	uint32_t <span style="color: #339933;">*</span>ipPtr <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>ipAddr<span style="color: #339933;">;</span>
	uint32_t mask <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	uint32_t value <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	uint32_t i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	uint8_t <span style="color: #339933;">*</span>ptr <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	<span style="color: #993333;">struct</span> in_addr in<span style="color: #339933;">;</span>
	ptr <span style="color: #339933;">=</span> strtok<span style="color: #009900;">&#40;</span>ip<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	inet_aton<span style="color: #009900;">&#40;</span>ptr<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> in_addr <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>ipPtr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	ptr <span style="color: #339933;">=</span> strtok<span style="color: #009900;">&#40;</span>NULL<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	value <span style="color: #339933;">=</span> atoi<span style="color: #009900;">&#40;</span>ptr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>value<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		j <span style="color: #339933;">=</span> value<span style="color: #339933;">-</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
		mask <span style="color: #339933;">|=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">&lt;&lt;</span>j<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%.8x<span style="color: #000099; font-weight: bold;">\n</span>%.8x<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>ipAddr<span style="color: #339933;">,</span>mask<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	in.<span style="color: #202020;">s_addr</span> <span style="color: #339933;">=</span> ipAddr<span style="color: #339933;">;</span>
	ipPrint <span style="color: #339933;">=</span> inet_ntoa<span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>ipPrint<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	in.<span style="color: #202020;">s_addr</span> <span style="color: #339933;">=</span> mask<span style="color: #339933;">;</span>
	ipPrint <span style="color: #339933;">=</span> inet_ntoa<span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>ipPrint<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>



<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/05/unix%e5%b8%b8%e7%94%a8%e7%9a%84ip%e8%a1%a8%e7%a4%ba%e6%8e%a9%e7%a0%81%e6%96%b9%e5%bc%8f%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e7%a8%8b%e5%ba%8f%e5%ae%9e%e7%8e%b0%e6%96%b9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C hash table code</title>
		<link>http://www.vi1129.com/2010/04/c-hash-table-code/</link>
		<comments>http://www.vi1129.com/2010/04/c-hash-table-code/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 08:25:43 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[程序人生]]></category>
		<category><![CDATA[hash]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=680</guid>
		<description><![CDATA[<p>This code modified from internet,insert IP and MAC to hashtable. Key is the last 16bit of MAC.</p>
<p>too busy without search and delete function, you can add them if your interested.</p>

?Download zhash.h1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef _ZHASH_H_
#define _ZHASH_H_
#include &#60;stdio.h&#62;
#include &#60;string.h&#62;
#include &#60;stdlib.h&#62;
&#160;
#define MAXHASHSIZE	16	
#define ETH_ALEN	6
typedef struct _hashentry
&#123;
    void *key;
    void *ipaddr;
    void *mac;
 <p><a href="http://www.vi1129.com/2010/04/c-hash-table-code/">继续阅读</a></p>


关联文章:<ol><li><a href='http://www.vi1129.com/2009/06/make-timer-with-signal/' rel='bookmark' title='Permanent Link: 使用setitimer和signal创建一个计时器'>使用setitimer和signal创建一个计时器</a></li>
<li><a href='http://www.vi1129.com/2010/03/sock_raw-dump/' rel='bookmark' title='Permanent Link: 使用原始套接字SOCK_RAW捕捉网络数据包并简单分析'>使用原始套接字SOCK_RAW捕捉网络数据包并简单分析</a></li>
<li><a href='http://www.vi1129.com/2010/02/node/' rel='bookmark' title='Permanent Link: 自写一则单链表小程序'>自写一则单链表小程序</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>This code modified from internet,insert IP and MAC to hashtable. Key is the last 16bit of MAC.</p>
<p>too busy without search and delete function, you can add them if your interested.</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.vi1129.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=680&amp;download=zhash.h">zhash.h</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6807"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code" id="p680code7"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#ifndef _ZHASH_H_</span>
<span style="color: #339933;">#define _ZHASH_H_</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define MAXHASHSIZE	16	</span>
<span style="color: #339933;">#define ETH_ALEN	6</span>
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> _hashentry
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>key<span style="color: #339933;">;</span>
    <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>ipaddr<span style="color: #339933;">;</span>
    <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>mac<span style="color: #339933;">;</span>
    <span style="color: #993333;">struct</span> _hashentry <span style="color: #339933;">*</span>next<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>hashentry<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> _hashtable
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>gethash<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>compare<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*,</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> hashsize<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> count<span style="color: #339933;">;</span>
    hashentry <span style="color: #339933;">**</span>hashlist<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>hashtable<span style="color: #339933;">;</span>
<span style="color: #993333;">typedef</span> <span style="color: #000000; font-weight: bold;">enum</span> bool_t <span style="color: #009900;">&#123;</span>
    FALSE <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>
    TRUE
<span style="color: #009900;">&#125;</span>bool<span style="color: #339933;">;</span>
hashtable <span style="color: #339933;">*</span>hash_create_t<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>keyfunc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		       <span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>comparefunc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*,</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	  	       <span style="color: #993333;">int</span> size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> hash_insert<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>key<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>ipaddr<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>mac<span style="color: #339933;">,</span> hashtable <span style="color: #339933;">*</span>tab<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> lh_strhash<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>src<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> equal_str<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>k1<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>k2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> hash_print<span style="color: #009900;">&#40;</span>hashtable <span style="color: #339933;">*</span>tab<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//void help();</span>
<span style="color: #339933;">#define hash_create(size)	hash_create_t(lh_strhash,equal_str,size) </span>
<span style="color: #339933;">#define hashindex(key, tab)	((tab-&gt;gethash)(key)) % (tab-&gt;hashsize)</span>
<span style="color: #339933;">#endif</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.vi1129.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=680&amp;download=zhash.c">zhash.c</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6808"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
</pre></td><td class="code" id="p680code8"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;zhash.h&quot;</span>
&nbsp;
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> lh_strhash<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span> src<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> l<span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> ret <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span> <span style="color: #339933;">*</span>s<span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>str <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>src<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>str <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span>
	<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    l <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>strlen<span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
    s <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>str<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> l<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	ret <span style="color: #339933;">^=</span> s<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">&amp;</span><span style="color: #208080;">0x0f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>ret<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> equal_str<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>k1<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>k2<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">==</span> strcmp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>k1<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>k2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
hashentry <span style="color: #339933;">*</span>hlist_new<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>key<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>ipaddr<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>mac<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    hashentry <span style="color: #339933;">*</span>new <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>hashentry <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>hashentry<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    new<span style="color: #339933;">-&gt;</span>key <span style="color: #339933;">=</span> key<span style="color: #339933;">;</span> 
    new<span style="color: #339933;">-&gt;</span>ipaddr <span style="color: #339933;">=</span> ipaddr<span style="color: #339933;">;</span>
    new<span style="color: #339933;">-&gt;</span>mac <span style="color: #339933;">=</span> mac<span style="color: #339933;">;</span>
    new<span style="color: #339933;">-&gt;</span>next <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> new<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> hlist_append<span style="color: #009900;">&#40;</span>hashentry <span style="color: #339933;">**</span>root<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>key<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>ipaddr<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>mac<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    hashentry <span style="color: #339933;">*</span>new<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>pos<span style="color: #339933;">;</span>
    new <span style="color: #339933;">=</span> hlist_new<span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> ipaddr<span style="color: #339933;">,</span> mac<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>root <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	<span style="color: #339933;">*</span>root <span style="color: #339933;">=</span> new<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
	pos <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>root<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>pos<span style="color: #339933;">-&gt;</span>next <span style="color: #339933;">!=</span> NULL<span style="color: #009900;">&#41;</span>
	    pos <span style="color: #339933;">=</span> pos<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">;</span>
	pos<span style="color: #339933;">-&gt;</span>next <span style="color: #339933;">=</span> new<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
bool hlist_update<span style="color: #009900;">&#40;</span>hashentry <span style="color: #339933;">*</span>root<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>key<span style="color: #339933;">,</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>ipaddr<span style="color: #339933;">,</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>mac<span style="color: #339933;">,</span><span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>compare<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*,</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    hashentry <span style="color: #339933;">*</span>pos<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>pos <span style="color: #339933;">=</span> root<span style="color: #339933;">;</span> pos <span style="color: #339933;">!=</span> NULL<span style="color: #339933;">;</span> pos <span style="color: #339933;">=</span> pos<span style="color: #339933;">-&gt;</span>next<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>compare<span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span>pos<span style="color: #339933;">-&gt;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//	    free(pos-&gt;ipaddr);</span>
<span style="color: #666666; font-style: italic;">//	    free(pos-&gt;mac);</span>
	    pos<span style="color: #339933;">-&gt;</span>ipaddr <span style="color: #339933;">=</span> ipaddr<span style="color: #339933;">;</span>
	    pos<span style="color: #339933;">-&gt;</span>mac <span style="color: #339933;">=</span> mac<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//	    free(key);</span>
	    <span style="color: #b1b100;">return</span> TRUE<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> FALSE<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> hash_insert<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>key<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>ipaddr<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>mac<span style="color: #339933;">,</span> hashtable <span style="color: #339933;">*</span>tab<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> index <span style="color: #339933;">=</span> hashindex<span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> tab<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    hashentry <span style="color: #339933;">*</span>root <span style="color: #339933;">=</span> tab<span style="color: #339933;">-&gt;</span>hashlist<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>hlist_update<span style="color: #009900;">&#40;</span>root<span style="color: #339933;">,</span> key<span style="color: #339933;">,</span> ipaddr<span style="color: #339933;">,</span> mac<span style="color: #339933;">,</span> tab<span style="color: #339933;">-&gt;</span>compare<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	hlist_append<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tab<span style="color: #339933;">-&gt;</span>hashlist<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>key<span style="color: #339933;">,</span>ipaddr<span style="color: #339933;">,</span> mac<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	tab<span style="color: #339933;">-&gt;</span>count<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
hashtable <span style="color: #339933;">*</span>hash_create_t<span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>keyfunc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>comparefunc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*,</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #993333;">int</span> size<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> len <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>hashentry <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> size<span style="color: #339933;">;</span>
    hashtable <span style="color: #339933;">*</span>tab <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>hashtable <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>hashtable<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    memset <span style="color: #009900;">&#40;</span>tab<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>hashtable <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    tab<span style="color: #339933;">-&gt;</span>hashlist <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>hashentry<span style="color: #339933;">**</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span>len<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>tab<span style="color: #339933;">-&gt;</span>hashlist <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	free<span style="color: #009900;">&#40;</span>tab<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    memset<span style="color: #009900;">&#40;</span>tab<span style="color: #339933;">-&gt;</span>hashlist<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>len<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>size<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	tab<span style="color: #339933;">-&gt;</span>hashlist<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    tab<span style="color: #339933;">-&gt;</span>gethash <span style="color: #339933;">=</span> keyfunc<span style="color: #339933;">;</span>
    tab<span style="color: #339933;">-&gt;</span>compare <span style="color: #339933;">=</span> comparefunc<span style="color: #339933;">;</span>
    tab<span style="color: #339933;">-&gt;</span>hashsize <span style="color: #339933;">=</span> size<span style="color: #339933;">;</span>
    tab<span style="color: #339933;">-&gt;</span>count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> tab<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> hash_print<span style="color: #009900;">&#40;</span>hashtable <span style="color: #339933;">*</span>tab<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>p<span style="color: #339933;">;</span>
    hashentry <span style="color: #339933;">*</span>temp<span style="color: #339933;">;</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;index<span style="color: #000099; font-weight: bold;">\t</span>%-20s<span style="color: #000099; font-weight: bold;">\t</span>%-20s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;mac&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;ipaddr&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> MAXHASHSIZE<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	temp <span style="color: #339933;">=</span> tab<span style="color: #339933;">-&gt;</span>hashlist<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>temp <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span>
	    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%-d<span style="color: #000099; font-weight: bold;">\t</span>%-20s<span style="color: #000099; font-weight: bold;">\t</span>%-20s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>i<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;NULL&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;NULL&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span>
	    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>temp <span style="color: #339933;">!=</span> NULL<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	    p <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>temp<span style="color: #339933;">-&gt;</span>mac<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%.2x:%.2x:%.2x:%.2x:%.2x:%.2x<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    p <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>temp<span style="color: #339933;">-&gt;</span>ipaddr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>format <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>temp<span style="color: #339933;">-&gt;</span>next <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d.%d.%d.%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d.%d.%d.%d --&gt;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span>format<span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    temp <span style="color: #339933;">=</span> temp<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #808080; font-style: italic;">/*
void help()
{
    printf(&quot;command: \n&quot;);
    printf(&quot;  -insert\n&quot;);
    printf(&quot;\t--usage: insert mac ipaddr  eg. insert 00:11:22:33:44:55 192.168.0.1\n&quot;);
    printf(&quot;\t--description: insert mac,ip to hash table,mac divided by \&quot;:\&quot;,and ip by \&quot;.\&quot;\n&quot;);
    printf(&quot;  -del\n&quot;);
    printf(&quot;\t--usage:del key  eg.del 4455\n&quot;);
    printf(&quot;\t--description: delete from hash table,key is the last 16 bits of mac\n&quot;);
    printf(&quot;  -print\n&quot;);
    printf(&quot;\t--usage: print\n&quot;);
    printf(&quot;\t--description: print all over the hash table\n&quot;);
}*/</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.vi1129.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=680&amp;download=test.c">test.c</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6809"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p680code9"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;zhash.h&quot;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    hashtable <span style="color: #339933;">*</span>tab <span style="color: #339933;">=</span> hash_create<span style="color: #009900;">&#40;</span>MAXHASHSIZE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> mac<span style="color: #009900;">&#91;</span>ETH_ALEN<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span><span style="color: #208080;">0x00</span><span style="color: #339933;">,</span><span style="color: #208080;">0x68</span><span style="color: #339933;">,</span><span style="color: #208080;">0x30</span><span style="color: #339933;">,</span><span style="color: #208080;">0x3b</span><span style="color: #339933;">,</span><span style="color: #208080;">0x2a</span><span style="color: #339933;">,</span><span style="color: #208080;">0x56</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> ipaddr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span><span style="color: #208080;">0xC0</span><span style="color: #339933;">,</span><span style="color: #208080;">0xA8</span><span style="color: #339933;">,</span><span style="color: #208080;">0x01</span><span style="color: #339933;">,</span><span style="color: #208080;">0x01</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> mac1<span style="color: #009900;">&#91;</span>ETH_ALEN<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span><span style="color: #208080;">0x00</span><span style="color: #339933;">,</span><span style="color: #208080;">0x69</span><span style="color: #339933;">,</span><span style="color: #208080;">0x30</span><span style="color: #339933;">,</span><span style="color: #208080;">0x3b</span><span style="color: #339933;">,</span><span style="color: #208080;">0x2a</span><span style="color: #339933;">,</span><span style="color: #208080;">0x42</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> ipaddr1<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span><span style="color: #208080;">0xC0</span><span style="color: #339933;">,</span><span style="color: #208080;">0xA8</span><span style="color: #339933;">,</span><span style="color: #208080;">0x01</span><span style="color: #339933;">,</span><span style="color: #208080;">0x02</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> key<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//    char key1[5];</span>
    memset<span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//    memset(key1,0,5);</span>
    sprintf<span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%.2x%.2x&quot;</span><span style="color: #339933;">,</span>mac<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>mac<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    hash_insert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>key<span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>ipaddr<span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>mac<span style="color: #339933;">,</span>tab<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    sprintf<span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%.2x%.2x&quot;</span><span style="color: #339933;">,</span>mac1<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>mac1<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    hash_insert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>key<span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>ipaddr1<span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>mac1<span style="color: #339933;">,</span>tab<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    hash_print<span style="color: #009900;">&#40;</span>tab<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.vi1129.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=680&amp;download=makefile">makefile</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p68010"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p680code10"><pre class="c" style="font-family:monospace;">CC <span style="color: #339933;">=</span> gcc
CFLAGS <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>Wall <span style="color: #339933;">-</span>g 
TARGET <span style="color: #339933;">=</span> zhash
SOURCE <span style="color: #339933;">=</span> \
zhash.<span style="color: #202020;">c</span> \
test.<span style="color: #202020;">c</span>
OBJS <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>SOURCE<span style="color: #339933;">:%</span>.<span style="color: #202020;">c</span><span style="color: #339933;">=%</span>.<span style="color: #202020;">o</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;">%</span>.<span style="color: #202020;">o</span><span style="color: #339933;">:%</span>.<span style="color: #202020;">c</span>
	$<span style="color: #009900;">&#40;</span>CC<span style="color: #009900;">&#41;</span> $<span style="color: #009900;">&#40;</span>CFLAGS<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span>c $<span style="color: #339933;">&lt;</span> <span style="color: #339933;">-</span>o $@
&nbsp;
$<span style="color: #009900;">&#40;</span>TARGET<span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>$<span style="color: #009900;">&#40;</span>OBJS<span style="color: #009900;">&#41;</span>
	$<span style="color: #009900;">&#40;</span>CC<span style="color: #009900;">&#41;</span> $<span style="color: #009900;">&#40;</span>OBJS<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span>o $@ 
&nbsp;
clean<span style="color: #339933;">:</span>
	@echo <span style="color: #ff0000;">&quot;cleanning object...&quot;</span>
	<span style="color: #339933;">-</span>rm zhash <span style="color: #339933;">*</span>.<span style="color: #202020;">o</span>
	@echo <span style="color: #ff0000;">&quot;clean finished!&quot;</span>
.<span style="color: #202020;">PHONY</span><span style="color: #339933;">:</span>clean</pre></td></tr></table></div>



<p>关联文章:<ol><li><a href='http://www.vi1129.com/2009/06/make-timer-with-signal/' rel='bookmark' title='Permanent Link: 使用setitimer和signal创建一个计时器'>使用setitimer和signal创建一个计时器</a></li>
<li><a href='http://www.vi1129.com/2010/03/sock_raw-dump/' rel='bookmark' title='Permanent Link: 使用原始套接字SOCK_RAW捕捉网络数据包并简单分析'>使用原始套接字SOCK_RAW捕捉网络数据包并简单分析</a></li>
<li><a href='http://www.vi1129.com/2010/02/node/' rel='bookmark' title='Permanent Link: 自写一则单链表小程序'>自写一则单链表小程序</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/04/c-hash-table-code/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>位运算实现的基本操作收集</title>
		<link>http://www.vi1129.com/2010/04/%e4%bd%8d%e8%bf%90%e7%ae%97%e5%ae%9e%e7%8e%b0%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c%e6%94%b6%e9%9b%86/</link>
		<comments>http://www.vi1129.com/2010/04/%e4%bd%8d%e8%bf%90%e7%ae%97%e5%ae%9e%e7%8e%b0%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c%e6%94%b6%e9%9b%86/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 05:01:57 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[知识随记]]></category>
		<category><![CDATA[位运算]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=678</guid>
		<description><![CDATA[<p>收集于网络 </p>
<p>/* base.h：基本操作的位运算实现 */ 
#ifndef BASE_H  
#define BASE_H  
#define word   int  
#define uword  unsigned int  
/* 将最右侧的1位改成0位 */ 
#define right1to0(x)  ((x)&#38;((x)-1))  
/* 向右传播最右侧的1位 */ 
#define right1torig(x)  ((x)&#124;((x)-1))  
/* 将最右侧的连续1位串改成0位串 */ 
#define right1sto0s(x)  (((x)&#124;(x)-1)+1 &#38; (x))  
/* 检查无符号整数x是否为2的幂，注意&#38;的优先级低于==，需要括号 */ 
#define powof2u(x)  (((x)&#38;((x)-1))==0)  
/* 检查无符号整数x是否为2**n-1的形式 */ 
#define pow2sub1u(x)  (((x)&#38;((x)+1))==0)  
/* 检查无符号整数x是否为2**j-2**k形式 */ 
#define pow2subpow2u(x)  ((((x)&#124;(x)-1)+1 &#38; (x))==0)  
 
/* 下列掩码直接析出字中指定的位 */ 
/* 析出最右侧的1位 */ 
#define right1(x)  ((x) &#38; -(x))  
/* 析出最右侧的0位 */ 
#define right0(x)  (~(x)&#38;((x)+1))  
/* 析出后缀0 */ 
#define suffix0(x)  (((x)&#38;-(x))-1)  
/* 析出最右侧的1位和后缀0(即后缀10&#8230;0) <p><a href="http://www.vi1129.com/2010/04/%e4%bd%8d%e8%bf%90%e7%ae%97%e5%ae%9e%e7%8e%b0%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c%e6%94%b6%e9%9b%86/">继续阅读</a></p>


No related posts.]]></description>
			<content:encoded><![CDATA[<p><em><span style="color: #888888;">收集于网络</span></em> </p>
<blockquote><p>/* base.h：基本操作的位运算实现 */ <br />
#ifndef BASE_H  <br />
#define BASE_H  <br />
#define word   int  <br />
#define uword  unsigned int  <br />
/* 将最右侧的1位改成0位 */ <br />
#define right1to0(x)  ((x)&amp;((x)-1))  <br />
/* 向右传播最右侧的1位 */ <br />
#define right1torig(x)  ((x)|((x)-1))  <br />
/* 将最右侧的连续1位串改成0位串 */ <br />
#define right1sto0s(x)  (((x)|(x)-1)+1 &amp; (x))  <br />
/* 检查无符号整数x是否为2的幂，注意&amp;的优先级低于==，需要括号 */ <br />
#define powof2u(x)  (((x)&amp;((x)-1))==0)  <br />
/* 检查无符号整数x是否为2**n-1的形式 */ <br />
#define pow2sub1u(x)  (((x)&amp;((x)+1))==0)  <br />
/* 检查无符号整数x是否为2**j-2**k形式 */ <br />
#define pow2subpow2u(x)  ((((x)|(x)-1)+1 &amp; (x))==0)  <br />
 <br />
/* 下列掩码直接析出字中指定的位 */ <br />
/* 析出最右侧的1位 */ <br />
#define right1(x)  ((x) &amp; -(x))  <br />
/* 析出最右侧的0位 */ <br />
#define right0(x)  (~(x)&amp;((x)+1))  <br />
/* 析出后缀0 */ <br />
#define suffix0(x)  (((x)&amp;-(x))-1)  <br />
/* 析出最右侧的1位和后缀0(即后缀10&#8230;0) */ <br />
#define suffix10s0(x)  ((x) ^ (x)-1)  <br />
/* 下列掩码与字做&amp;运算，可以析出字中指定的字段 */ <br />
/* 析出高阶n位(注意n的值不能大于int型的宽度) */ <br />
#define high_ones(n) (-1&lt;&lt;32-(n))  <br />
/* 析出低阶n位 */ <br />
#define low_ones(n) (~(-1&lt;&lt;(n)))  <br />
/* 析出低阶offset位和高阶32-offset-width位 */ <br />
#define mid_zeros(width,offset)    \  <br />
        (-1&lt;&lt;(width)+(offset) | ~(-1&lt;&lt;(offset)))      <br />
/* 析出中间width位，它右侧有offset位 */ <br />
#define mid_ones(width,offset)     \  <br />
        (~(-1&lt;&lt;(width)+(offset)) &amp; (-1&lt;&lt;(offset)))  <br />
 <br />
/* 对无符号整数x，求比x大且与x中的位1个数相同的下一个整数： <br />
    例如对nnn0 1111 0000，则下一个整数为nnn1 0000 01111。 <br />
    可以用这个函数来遍历一个集合的所有子集 */ <br />
#define nextsame1u(x)  (right1(x)+(x) |      \  <br />
      (((x)^right1(x)+(x))&gt;&gt;2)/right1(x))  <br />
/* 字的绝对值函数：注意x&gt;&gt;31为0或-1，而x^0=x，x^-1=~x */ <br />
#define abs(x)  (((x)^((x)&gt;&gt;31))-((x)&gt;&gt;31))  <br />
/* 绝对值的负值 */ <br />
#define nabs(x)  (((x)&gt;&gt;31)-((x)^((x)&gt;&gt;31)))   <br />
/* 符号扩展：将第7位向左传播(位编号从0开始) */ <br />
#define prop7thtolef(x)  ((((x) &amp; 0&#215;000000FF) ^ 0&#215;00000080)-0&#215;00000080)  <br />
/* 对无符号整数实现算术右移：也可用更简单的((signed)(x)&gt;&gt;n) */ <br />
#define arithrshiftu(x,n)  ((((x)^0&#215;80000000)&gt;&gt;(n))-(0&#215;80000000&gt;&gt;(n)))       <br />
/* 对有符号整数实现逻辑右移：也可用更简单的((unsigned)(x)&gt;&gt;n) */ <br />
#define logicrshift(x,n)  ((((x)^0&#215;80000000)&gt;&gt;(n))+(1&lt;&lt;31-(n)))   <br />
/* 符号函数：x&gt;0返回1，x=0返回0，x&lt;0返回-1 */ <br />
#define sign(x)  (((x)&gt;0)-((x)&lt;0))  <br />
/* 三值比较函数：x&gt;y返回1，x=y返回0，x&lt;y返回-1 */ <br />
#define cmp(x,y)  (((x)&gt;(y))-((x)&lt;(y)))  <br />
/*  符号传递函数：返回采用y的符号后的x */ <br />
#define copysign(x,y)  ((abs(x)^((y)&gt;&gt;31))-((y)&gt;&gt;31))   <br />
/* 比较谓词：带符号整数 */ <br />
#define equal(x,y)     (~((x)-(y)|(y)-(x))&gt;&gt;31)  <br />
#define noteq(x,y)     (((x)-(y)|(y)-(x))&gt;&gt;31)  <br />
#define less(x,y)      ((((x)^(y)) &amp; ((x)-(y)^(x)) ^ (x)-(y))&gt;&gt;31)  <br />
#define larger(x,y)    ((((y)^(x)) &amp; ((y)-(x)^(y)) ^ (y)-(x))&gt;&gt;31)  <br />
#define lesseq(x,y)    ((((x)|~(y)) &amp; ((x)^(y) | ~((y)-(x))))&gt;&gt;31)  <br />
#define largereq(x,y)  ((((y)|~(x)) &amp; ((y)^(x) | ~((x)-(y))))&gt;&gt;31)  <br />
/* 比较谓词：无符号整数 */       <br />
#define equalu(x,y)     (~((x)-(y)|(y)-(x))&gt;&gt;31)  <br />
#define notequ(x,y)     (((x)-(y)|(y)-(x))&gt;&gt;31)  <br />
#define lessu(x,y)      ((~(x) &amp; (y) | ~((x)^(y)) &amp; (x)-(y))&gt;&gt;31)  <br />
#define largeru(x,y)    ((~(y) &amp; (x) | ~((y)^(x)) &amp; (y)-(x))&gt;&gt;31)  <br />
#define lessequ(x,y)    (((~(x)|(y)) &amp; (((x)^(y)) | ~((y)-(x))))&gt;&gt;31)  <br />
#define largerequ(x,y)  (((~(y)|(x)) &amp; (((y)^(x)) | ~((x)-(y))))&gt;&gt;31)  <br />
/* 溢出检测：带符号运算 */ <br />
#define addovf(x,y)  ((~((x)^(y))&amp;(((x)+(y))^(x)))&gt;&gt;31)  <br />
#define subovf(x,y)  ((((x)^(y))&amp;(((x)-(y))^(x)))&gt;&gt;31)  <br />
#define mulovf(x,y)  ((y)&lt;0 &amp;&amp; (x)==0&#215;80000000 || (y)!=0 &amp;&amp; (x)*(y)/(y)!=(x))  <br />
#define divovf(x,y)  ((y)==0 || (x)==0&#215;80000000 &amp;&amp; (y)==-1)  <br />
/* 溢出检测：无符号运算 */ <br />
#define addovfu(x,y)  (~(x)&lt;(y))  /* 也可用less(~(x),y) */  <br />
#define subovfu(x,y)  ((x)&lt;(y))   /* 也可用less(x,y) */  <br />
#define mulovfu(x,y)  ((y)!=0 &amp;&amp; (x) &gt; 0xffffffff/(y))  <br />
#define divovfu(x,y)  ((y)==0)  <br />
/* 循环移位：带符号整数 */ <br />
/* 循环左移n位 */ <br />
#define rotlshift(x,n)  ((x)&lt;&lt;(n) | (unsigned)(x)&gt;&gt;32-(n))  <br />
/* 循环右移n位 */ <br />
#define rotrshift(x,n)  ((unsigned)(x)&gt;&gt;(n) | (x)&lt;&lt;32-(n))  <br />
/* 循环移位：无符号整数 */ <br />
#define rotlshiftu(x,n)  ((x)&lt;&lt;(n) | (x)&gt;&gt;32-(n))  <br />
#define rotrshiftu(x,n)  ((x)&gt;&gt;(n) | (x)&lt;&lt;32-(n))  <br />
/* 正差函数：x&gt;=y时返回x-y，x&lt;y时返回0。注意&amp;优先级高于^，无需要括号 */ <br />
#define doz(x,y)  ((x)-(y)&amp;~((x)-(y)^((x)^(y))&amp;((x)-(y)^(x)))&gt;&gt;31)  <br />
/* 大值函数 */ <br />
#define max(x,y)  ((y)+doz(x,y))  <br />
/* 小值函数 */ <br />
#define min(x,y)  ((x)-doz(x,y))  <br />
/* 下面是无符号版本 */ <br />
#define dozu(x,y)  ((x)-(y)&amp; arithrshiftu(((x)|~(y))&amp;((x)^(y)|~((x)-(y))) ,31))  <br />
#define maxu(x,y)  ((y)+dozu(x,y))  <br />
#define minu(x,y)  ((x)-dozu(x,y))  <br />
/* 交换变量的值 */ <br />
#define swap(x,y)      \  <br />
    do { x=x^y; y=y^x; x=x^y; } while(0)  <br />
/* 根据掩码来交换变量的相应字段： <br />
    当m的第i位为1时，交换x,y的第i位；当m的第i位为0时，保留x,y的第i位不变  */ <br />
#define swapbits(x,y,m)     \  <br />
    do { x=x^y; y=y^(x&amp;(m)); x=x^y; } while(0)      <br />
/* 2个常量的循环赋值：当x为a时赋值b，当x为b时赋值a */ <br />
#define rottwo(x,a,b)  do{ x=(a)^(b)^x; }while(0)  <br />
/* 3个常量的循环赋值 */ <br />
#define rotthree(x,a,b,c)     \  <br />
    do{ x=(-(x==c)&amp;(a-c))+(-(x==a)&amp;(b-c))+c; }while(0)  <br />
#endif <br />
本文来自CSDN博客，原文出处：<a href="http://blog.csdn.net/zhoudaxia/archive/2010/03/28/5425619.aspx">http://blog.csdn.net/zhoudaxia/archive/2010/03/28/5425619.aspx</a>#</p></blockquote>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/04/%e4%bd%8d%e8%bf%90%e7%ae%97%e5%ae%9e%e7%8e%b0%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c%e6%94%b6%e9%9b%86/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New blog borns</title>
		<link>http://www.vi1129.com/2010/04/new-blog-borns/</link>
		<comments>http://www.vi1129.com/2010/04/new-blog-borns/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 04:44:38 +0000</pubDate>
		<dc:creator>doris</dc:creator>
				<category><![CDATA[随心所记]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=676</guid>
		<description><![CDATA[<p>New blog borns.</p>
<p>It&#8217;s a cloudy day!</p>
<p>But have a nice mood today!</p>


<p>No related posts.</p>


No related posts.]]></description>
			<content:encoded><![CDATA[<p>New blog borns.</p>
<p>It&#8217;s a cloudy day!</p>
<p>But have a nice mood today!</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/04/new-blog-borns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C的位运算及常见用法</title>
		<link>http://www.vi1129.com/2010/04/bit-operatin/</link>
		<comments>http://www.vi1129.com/2010/04/bit-operatin/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 06:47:21 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[知识随记]]></category>
		<category><![CDATA[位运算]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=671</guid>
		<description><![CDATA[收集于网络
位运算
    位运算的运算分量只能是整型或字符型数据，位运算把运算对象看作是由二进位组成的位串信息，按位完成指定的运算，得到位串信息的结果。
位运算符有：
    &#38;(按位与)、&#124;(按位或)、^(按位异或)、~ (按位取反)。
    其中，按位取反运算符是单目运算符，其余均为双目运算符。
    位运算符的优先级从高到低，依次为~、&#38;、^、&#124;，
    其中~的结合方向自右至左，且优先级高于算术运算符，其余运算符的结合方向都是自左至右，且优先级低于关系运算符。
   (1)按位与运算符(&#38;)
    按位与运算将两个运算分量的对应位按位遵照以下规则进行计算：
     0 &#38; 0 = 0, 0 &#38; 1 = 0, 1 &#38; 0 = 0, 1 &#38; 1 = 1。
即同为 1 的位，结果为 1，否则结果为 0。
    例如，设3的内部表示为
     00000011
    5的内部表示为
     00000101
    则3&#38;5的结果为
     00000001
    按位与运算有两种典型用法，一是取一个位串信息的某几位，如以下代码截取x的最低7位：x &#38; 0177。二是让某变量保留某几位，其余位置0，如以下代码让x只保留最低6位：x = x &#38; 077。以上用法都先要设计好一个常数，该常数只有需要的位是1，不需要的位是0。用它与指定的位串信息按位与。
   (2)按位或运算符(&#124;)
    按位或运算将两个运算分量的对应位按位遵照以下规则进行计算：
     0 &#124; 0 = 0, 0 &#124; 1 = 1, 1 &#124; 0 = 1, 1 &#124; 1 = 1
即只要有1个是1的位，结果为1，否则为0。
    例如，023 <p><a href="http://www.vi1129.com/2010/04/bit-operatin/">继续阅读</a></p>


No related posts.]]></description>
			<content:encoded><![CDATA[<div><span style="color: #888888;"><em>收集于网络</em></span></div>
<div><strong><span style="color: #660000;">位运算</span></strong></div>
<div><strong><span style="color: #000066;">    位运算的运算分量只能是整型或字符型数据，位运算把运算对象看作是由二进位组成的位串信息，按位完成指定的运算，得到位串信息的结果。</span></strong></div>
<div>位运算符有：</div>
<div><strong><span style="color: #660000;">    &amp;(按位与)、|(按位或)、^(按位异或)、~ (按位取反)。</span></strong></div>
<div>    其中，按位取反运算符是单目运算符，其余均为双目运算符。</div>
<div><strong><span style="color: #660000;">    位运算符的优先级从高到低，依次为~、&amp;、^、|，</span></strong></div>
<div>    其中~的结合方向自右至左，且优先级高于算术运算符，其余运算符的结合方向都是自左至右，且优先级低于关系运算符。</div>
<div><strong><span style="color: #660000;">   (1)按位与运算符(&amp;)</span></strong><br />
    按位与运算将两个运算分量的对应位按位遵照以下规则进行计算：<br />
     0 &amp; 0 = 0, 0 &amp; 1 = 0, 1 &amp; 0 = 0, 1 &amp; 1 = 1。<br />
即同为 1 的位，结果为 1，否则结果为 0。<br />
    例如，设3的内部表示为<br />
     00000011<br />
    5的内部表示为<br />
     00000101<br />
    则3&amp;5的结果为<br />
     00000001<br />
    按位与运算有两种典型用法，一是取一个位串信息的某几位，如以下代码截取x的最低7位：x &amp; 0177。二是让某变量保留某几位，其余位置0，如以下代码让x只保留最低6位：x = x &amp; 077。以上用法都先要设计好一个常数，该常数只有需要的位是1，不需要的位是0。用它与指定的位串信息按位与。</div>
<div><strong><span style="color: #660000;">   (2)按位或运算符(|)</span></strong><br />
    按位或运算将两个运算分量的对应位按位遵照以下规则进行计算：<br />
     0 | 0 = 0, 0 | 1 = 1, 1 | 0 = 1, 1 | 1 = 1<br />
即只要有1个是1的位，结果为1，否则为0。<br />
    例如，023 | 035 结果为037。<br />
    按位或运算的典型用法是将一个位串信息的某几位置成1。如将要获得最右4为1，其他位与变量j的其他位相同，可用逻辑或运算017|j。若要把这结果赋给变量j，可写成：<br />
     j = 017|j</div>
<div><strong><span style="color: #660000;">   (3)按位异或运算符(^)</span></strong><br />
    按位异或运算将两个运算分量的对应位按位遵照以下规则进行计算：<br />
     0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, 1 ^ 1 = 0<br />
即相应位的值相同的，结果为 0，不相同的结果为 1。<br />
    例如，013^035结果为026。<br />
    异或运算的意思是求两个运算分量相应位值是否相异，相异的为1，相同的为0。按位异或运算的典型用法是求一个位串信息的某几位信息的反。如欲求整型变量j的最右4位信息的反，用逻辑异或运算017^j，就能求得j最右4位的信息的反,即原来为1的位，结果是0,原来为0的位，结果是1。<span id="more-671"></span></div>
<div><strong><span style="color: #660000;">   (4)按位取反运算符(~)</span></strong><br />
    按位取反运算是单目运算，用来求一个位串信息按位的反，即哪些为0的位，结果是1，而哪些为1的位，结果是0。例如, ~7的结果为0xfff8。<br />
    取反运算常用来生成与系统实现无关的常数。如要将变量x最低6位置成0，其余位不变，可用代码x = x &amp; ~077实现。以上代码与整数x用2个字节还是用4个字节实现无关。<br />
    当两个长度不同的数据进行位运算时(例如long型数据与int型数据)，将两个运算分量的右端对齐进行位运算。如果短的数为正数，高位用0补满；如果短的数为负数，高位用1补满。如果短的为无符号整数，则高位总是用0补满。<br />
    位运算用来对位串信息进行运算，得到位串信息结果。如以下代码能取下整型变量k的位串信息的最右边为1的信息位：((k-1)^k) &amp; k。</div>
<div><strong><span style="color: #660000;">移位运算</span></strong></div>
<div>    移位运算用来<strong><span style="color: #660000;">将整型或字符型数据作为二进位信息串作整体移动</span></strong>。有两个运算符：<br />
<strong><span style="color: #660000;">     &lt;&lt; (左移) 和 &gt;&gt; (右移)</span></strong><br />
移位运算是双目运算，有两个运算分量,左分量为移位数据对象，右分量的值为移位位数。移位运算将左运算分量视作由二进位组成的位串信息,对其作向左或向右移位，得到新的位串信息。<br />
    移位运算符的优先级低于算术运算符，高于关系运算符，它们的结合方向是自左至右。</div>
<div><strong><span style="color: #660000;">   (1)左移运算符(&lt;&lt;)</span></strong><br />
    左移运算将一个位串信息向左移指定的位，右端空出的位用0补充。例如014&lt;&lt;2,结果为060,即48。<br />
    左移时，空出的右端用0补充，左端移出的位的信息就被丢弃。在二进制数运算中，在信息没有因移动而丢失的情况下，每左移1位相当于乘2。如4 &lt;&lt; 2，结果为16。</div>
<div><strong><span style="color: #660000;">   (2)右移运算符(&gt;&gt;)</span></strong><br />
    右移运算将一个位串信息向右移指定的位，右端移出的位的信息被丢弃。例如12&gt;&gt;2,结果为3。与左移相反，对于小整数，每右移1位，相当于除以2。在右移时，需要注意符号位问题。对无符号数据，右移时，左端空出的位用0补充。对于带符号的数据，如果移位前符号位为0(正数)，则左端也是用0补充；如果移位前符号位为1(负数)，则左端用0或用1补充，取决于计算机系统。对于负数右移，称用0 补充的系统为“逻辑右移”，用1补充的系统为“算术右移”。以下代码能说明读者上机的系统所采用的右移方法：<br />
     printf(&#8221;%d\n\n\n&#8221;, -2&gt;&gt;4);<br />
若输出结果为-1，是采用算术右移；输出结果为一个大整数，则为逻辑右移。<br />
    移位运算与位运算结合能实现许多与位串运算有关的复杂计算。设变量的位自右至左顺序编号，自0位至15位，有关指定位的表达式是不超过15的正整数。以下各代码分别有它们右边注释所示的意义：<br />
<strong><span style="color: #660000;">     ~（~0 &lt;&lt; n）</span></strong> /* 实现最低n位为1，其余位为0的位串信息 */<br />
<strong><span style="color: #660000;">     (x &gt;&gt; (1+p-n)) &amp; ~(~0 &lt;&lt; n) </span></strong>/* 截取变量x自p位开始的右边n位的信息 */<br />
<strong><span style="color: #660000;">     new |= ((old &gt;&gt; row) &amp; 1) &lt;&lt; (15 – k) </span></strong>/* 截取old变量第row位，并将该位信息装配到变量new的第15-k位 */<br />
<strong><span style="color: #660000;">     s &amp;= ~(1 &lt;&lt; j) </span></strong>/* 将变量s的第j位置成0，其余位不变 */<br />
<strong><span style="color: #660000;">     for(j = 0; ((1 &lt;&lt; j) &amp; s) == 0; j++)</span></strong> ; /* 设s不等于全0，代码寻找最右边为1的位的序号j */</div>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/04/bit-operatin/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>使用原始套接字SOCK_RAW捕捉网络数据包并简单分析</title>
		<link>http://www.vi1129.com/2010/03/sock_raw-dump/</link>
		<comments>http://www.vi1129.com/2010/03/sock_raw-dump/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 04:44:32 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[程序人生]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[socket]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=663</guid>
		<description><![CDATA[<p>协议的分析需要参考前一篇文章以太网帧格式，IP包头，TCP头格式说明。</p>
<p>抓取网络上的数据包需要设置网卡为混杂模式，调用recvfrom在创建的SOCK_RAW类型的socket上接收来自kernel的信息，然后再按照帧格式，IP头，TCP头格式，指针移动到相应位置并分析。</p>
<p>附上的小程序由于其他原因还在UDP9001端口监听了来自客户端的消息，这与本文无关。</p>

?Download zhao_sock.h1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
&#160;
#include &#60;stdio.h&#62;  //for printf
#include &#60;stdlib.h&#62; //for exit
#include &#60;string.h&#62; // for strcmp
#include &#60;sys/socket.h&#62; //for socket,address
#include &#60;sys/types.h&#62;
#include &#60;netinet/in.h&#62;
#include &#60;linux/if_ether.h&#62;
#include &#60;net/if.h&#62;
#include &#60;sys/ioctl.h&#62; //for ioctl
#include &#60;pthread.h&#62;
&#160;
#define INTERFACE &#34;eth0&#34;
#define BUFFLEN 2048
#define UDPPORT 9001
#define REQUEST 0
#define RESPONSE 1
#define RESERVE 2 
#define z_print(fmt,args...) \
 printf(&#34;[%s %d]&#34;fmt&#34;\n&#34;,__FILE__,__LINE__,##args)
typedef struct &#123;
 uint8_t version;
 uint8_t type;
 uint8_t len;
 uint16_t number;
 char data&#91;255&#93;;
&#125; datasend;
&#160;
void* ch_hanlder&#40;&#41;;
uint8_t <p><a href="http://www.vi1129.com/2010/03/sock_raw-dump/">继续阅读</a></p>


关联文章:<ol><li><a href='http://www.vi1129.com/2010/04/c-hash-table-code/' rel='bookmark' title='Permanent Link: C hash table code'>C hash table code</a></li>
<li><a href='http://www.vi1129.com/2010/02/node/' rel='bookmark' title='Permanent Link: 自写一则单链表小程序'>自写一则单链表小程序</a></li>
<li><a href='http://www.vi1129.com/2009/06/make-timer-with-signal/' rel='bookmark' title='Permanent Link: 使用setitimer和signal创建一个计时器'>使用setitimer和signal创建一个计时器</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>协议的分析需要参考前一篇文章以太网帧格式，IP包头，TCP头格式说明。</p>
<p>抓取网络上的数据包需要设置网卡为混杂模式，调用recvfrom在创建的SOCK_RAW类型的socket上接收来自kernel的信息，然后再按照帧格式，IP头，TCP头格式，指针移动到相应位置并分析。</p>
<p>附上的小程序由于其他原因还在UDP9001端口监听了来自客户端的消息，这与本文无关。</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.vi1129.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=663&amp;download=zhao_sock.h">zhao_sock.h</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66314"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p663code14"><pre class="c" style="font-family:monospace;">&nbsp;
<span style="color: #339933;">#include &lt;stdio.h&gt;  //for printf</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt; //for exit</span>
<span style="color: #339933;">#include &lt;string.h&gt; // for strcmp</span>
<span style="color: #339933;">#include &lt;sys/socket.h&gt; //for socket,address</span>
<span style="color: #339933;">#include &lt;sys/types.h&gt;</span>
<span style="color: #339933;">#include &lt;netinet/in.h&gt;</span>
<span style="color: #339933;">#include &lt;linux/if_ether.h&gt;</span>
<span style="color: #339933;">#include &lt;net/if.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/ioctl.h&gt; //for ioctl</span>
<span style="color: #339933;">#include &lt;pthread.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define INTERFACE &quot;eth0&quot;</span>
<span style="color: #339933;">#define BUFFLEN 2048</span>
<span style="color: #339933;">#define UDPPORT 9001</span>
<span style="color: #339933;">#define REQUEST 0</span>
<span style="color: #339933;">#define RESPONSE 1</span>
<span style="color: #339933;">#define RESERVE 2 </span>
<span style="color: #339933;">#define z_print(fmt,args...) \
 printf(&quot;[%s %d]&quot;fmt&quot;\n&quot;,__FILE__,__LINE__,##args)</span>
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
 uint8_t version<span style="color: #339933;">;</span>
 uint8_t type<span style="color: #339933;">;</span>
 uint8_t len<span style="color: #339933;">;</span>
 uint16_t number<span style="color: #339933;">;</span>
 <span style="color: #993333;">char</span> data<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">255</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> datasend<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span><span style="color: #339933;">*</span> ch_hanlder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
uint8_t randvalue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.vi1129.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=663&amp;download=zhao_sock.c">zhao_sock.c</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66315"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
</pre></td><td class="code" id="p663code15"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;zhao_sock.h&quot;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> sockfd<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> len<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> proto<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> port_s<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> port_d<span style="color: #339933;">;</span>
	<span style="color: #993333;">char</span> recvbuff<span style="color: #009900;">&#91;</span>BUFFLEN<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> type<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>ethhead <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>iphead <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	<span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>p <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	<span style="color: #993333;">struct</span> ifreq ifr<span style="color: #339933;">;</span>
	pthread_t thread_ch<span style="color: #339933;">;</span>
&nbsp;
	pthread_create<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>thread_ch<span style="color: #339933;">,</span>NULL<span style="color: #339933;">,&amp;</span>ch_hanlder<span style="color: #339933;">,</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	sockfd <span style="color: #339933;">=</span> socket<span style="color: #009900;">&#40;</span>PF_PACKET<span style="color: #339933;">,</span>SOCK_RAW<span style="color: #339933;">,</span>htons<span style="color: #009900;">&#40;</span>ETH_P_ALL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>sockfd <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span> 
		z_print<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;socket create error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	strcpy<span style="color: #009900;">&#40;</span>ifr.<span style="color: #202020;">ifr_name</span><span style="color: #339933;">,</span>INTERFACE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ioctl<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span>SIOCGIFFLAGS<span style="color: #339933;">,&amp;</span>ifr<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		z_print<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;set PROMISC fail!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	ifr.<span style="color: #202020;">ifr_flags</span> <span style="color: #339933;">|=</span> IFF_PROMISC<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ioctl<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span>SIOCGIFFLAGS<span style="color: #339933;">,&amp;</span>ifr<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		z_print<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;set PROMISC fail!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		len <span style="color: #339933;">=</span> recvfrom<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span>recvbuff<span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>recvbuff<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>NULL<span style="color: #339933;">,</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>len <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">42</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			z_print<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;receive error!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		ethhead <span style="color: #339933;">=</span> recvbuff<span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//frame head point</span>
		p <span style="color: #339933;">=</span> ethhead<span style="color: #339933;">;</span>
		sprintf<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%02x%02x&quot;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">12</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">13</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//frame type</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>strcmp<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;0800&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>  <span style="color: #666666; font-style: italic;">//0800 is IP frame</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>strcmp<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;0806&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
				<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: ARP<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>strcmp<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;8035&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
				<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: RARP<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span>
				<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: unknow<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MAC:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x==&gt;%.2x:%.2x:%.2x:%.2x:%.2x:%.2x<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> \
				p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">8</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">11</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//mac address</span>
			<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		iphead <span style="color: #339933;">=</span> ethhead <span style="color: #339933;">+</span> <span style="color: #0000dd;">14</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//ip head point</span>
		p <span style="color: #339933;">=</span> iphead <span style="color: #339933;">+</span> <span style="color: #0000dd;">12</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//ip address</span>
		<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;IP:%d.%d.%d.%d==&gt;%d.%d.%d.%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> \
			p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">7</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		proto <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>iphead <span style="color: #339933;">+</span> <span style="color: #0000dd;">9</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//ip protocol</span>
		p <span style="color: #339933;">=</span> iphead <span style="color: #339933;">+</span> <span style="color: #0000dd;">20</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//ip port </span>
		<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>proto<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">case</span> IPPROTO_ICMP<span style="color: #339933;">:</span>
			<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: ICMP<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> IPPROTO_IGMP<span style="color: #339933;">:</span>
			<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: IGMP<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> IPPROTO_IPIP<span style="color: #339933;">:</span>
			<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: IPIP<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> IPPROTO_TCP<span style="color: #339933;">:</span>
			<span style="color: #b1b100;">case</span> IPPROTO_UDP<span style="color: #339933;">:</span>
			<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span>proto <span style="color: #339933;">==</span> IPPROTO_TCP<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #ff0000;">&quot;TCP&quot;</span><span style="color: #339933;">:</span><span style="color: #ff0000;">&quot;UDP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			port_s <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span><span style="color: #208080;">0XFF00</span> <span style="color: #339933;">|</span> p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span><span style="color: #208080;">0XFF</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//source port</span>
			port_d <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span><span style="color: #208080;">0XFF00</span> <span style="color: #339933;">|</span> p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&amp;</span><span style="color: #208080;">0XFF</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// dest port</span>
			<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;source port:%u,&quot;</span><span style="color: #339933;">,</span>port_s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;dest port:%u<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>port_d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>port_s <span style="color: #339933;">==</span> <span style="color: #0000dd;">80</span> <span style="color: #339933;">||</span> port_d <span style="color: #339933;">==</span> <span style="color: #0000dd;">80</span><span style="color: #009900;">&#41;</span>
				<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: HTTP<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>port_s <span style="color: #339933;">==</span> <span style="color: #0000dd;">67</span> <span style="color: #339933;">||</span> port_d <span style="color: #339933;">==</span> <span style="color: #0000dd;">67</span><span style="color: #009900;">&#41;</span>
				<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: DHCP<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>port_s <span style="color: #339933;">==</span> <span style="color: #0000dd;">21</span> <span style="color: #339933;">||</span> port_d <span style="color: #339933;">==</span> <span style="color: #0000dd;">21</span><span style="color: #009900;">&#41;</span>
				<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: FTP<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>port_s <span style="color: #339933;">==</span> <span style="color: #0000dd;">23</span> <span style="color: #339933;">||</span> port_d <span style="color: #339933;">==</span> <span style="color: #0000dd;">23</span><span style="color: #009900;">&#41;</span>
				<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: TELNET<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>port_s <span style="color: #339933;">==</span> <span style="color: #0000dd;">53</span> <span style="color: #339933;">||</span> port_d <span style="color: #339933;">==</span> <span style="color: #0000dd;">53</span><span style="color: #009900;">&#41;</span>
				<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: DNS<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>port_s <span style="color: #339933;">==</span> <span style="color: #0000dd;">137</span> <span style="color: #339933;">||</span> port_d <span style="color: #339933;">==</span> <span style="color: #0000dd;">137</span> <span style="color: #339933;">||</span> port_s <span style="color: #339933;">==</span> <span style="color: #0000dd;">138</span> <span style="color: #339933;">||</span> port_d <span style="color: #339933;">==</span> <span style="color: #0000dd;">138</span><span style="color: #009900;">&#41;</span>
				<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: NetBIOS/SMB<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span>
				<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol: other<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">case</span> IPPROTO_RAW<span style="color: #339933;">:</span>
			<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;RAW<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
			<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;protocol:unknow(%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>proto<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span><span style="color: #339933;">*</span> ch_hanlder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> sockfd<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> len<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> flag_close <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	socklen_t addrlen<span style="color: #339933;">;</span>
	datasend msg_send<span style="color: #339933;">;</span>
	datasend msg_recv<span style="color: #339933;">;</span>
	<span style="color: #993333;">struct</span> sockaddr_in servaddr<span style="color: #339933;">;</span>
&nbsp;
	memset<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>msg_send<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>datasend<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	memset<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>msg_recv<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>datasend<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	bzero<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>servaddr<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>servaddr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	msg_send.<span style="color: #202020;">version</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	msg_send.<span style="color: #202020;">type</span> <span style="color: #339933;">=</span> RESPONSE<span style="color: #339933;">;</span>
	sprintf<span style="color: #009900;">&#40;</span>msg_send.<span style="color: #202020;">data</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;received!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	sockfd <span style="color: #339933;">=</span> socket<span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,</span> SOCK_DGRAM<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
	servaddr.<span style="color: #202020;">sin_family</span> <span style="color: #339933;">=</span> AF_INET<span style="color: #339933;">;</span>
	servaddr.<span style="color: #202020;">sin_addr</span>.<span style="color: #202020;">s_addr</span> <span style="color: #339933;">=</span> htonl<span style="color: #009900;">&#40;</span>INADDR_ANY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	servaddr.<span style="color: #202020;">sin_port</span> <span style="color: #339933;">=</span> htons<span style="color: #009900;">&#40;</span>UDPPORT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	addrlen <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>bind<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>servaddr<span style="color: #339933;">,</span> addrlen<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		perror<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;bind error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		len <span style="color: #339933;">=</span> recvfrom<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>msg_recv<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>datasend<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>servaddr<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>addrlen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;version:%3d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">version</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;type:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;len:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">len</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;number:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;data:%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>strcmp<span style="color: #009900;">&#40;</span>msg_recv.<span style="color: #202020;">data</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;close&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			sprintf<span style="color: #009900;">&#40;</span>msg_send.<span style="color: #202020;">data</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;close&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			flag_close <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		msg_send.<span style="color: #202020;">number</span> <span style="color: #339933;">=</span> msg_recv.<span style="color: #202020;">number</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
		msg_send.<span style="color: #202020;">len</span> <span style="color: #339933;">=</span> randvalue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		sendto<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,&amp;</span>msg_send<span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>datasend<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>servaddr<span style="color: #339933;">,</span>addrlen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>flag_close<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
uint8_t randvalue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	srand<span style="color: #009900;">&#40;</span>time<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>rand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.vi1129.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=663&amp;download=zhao_sock_send.c">zhao_sock_send.c</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code" id="p663code16"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;zhao_sock.h&quot;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> sockfd<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> len<span style="color: #339933;">;</span>
	socklen_t addrlen<span style="color: #339933;">;</span>
	<span style="color: #993333;">struct</span> sockaddr_in cliaddr<span style="color: #339933;">;</span>
	FILE <span style="color: #339933;">*</span>fd<span style="color: #339933;">;</span>
	datasend msg_send<span style="color: #339933;">;</span>
	datasend msg_recv<span style="color: #339933;">;</span>
&nbsp;
	fd <span style="color: #339933;">=</span> fopen<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/root/zhao_log.txt&quot;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	memset<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>msg_send<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>datasend<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	memset<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>msg_recv<span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>datasend<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	bzero<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>cliaddr<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>cliaddr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	msg_send.<span style="color: #202020;">version</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	msg_send.<span style="color: #202020;">type</span> <span style="color: #339933;">=</span> REQUEST<span style="color: #339933;">;</span>
	msg_send.<span style="color: #202020;">number</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	msg_send.<span style="color: #202020;">len</span> <span style="color: #339933;">=</span> randvalue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	sockfd <span style="color: #339933;">=</span> socket<span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,</span> SOCK_DGRAM<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	cliaddr.<span style="color: #202020;">sin_family</span> <span style="color: #339933;">=</span> AF_INET<span style="color: #339933;">;</span>
	cliaddr.<span style="color: #202020;">sin_port</span> <span style="color: #339933;">=</span> htons<span style="color: #009900;">&#40;</span>UDPPORT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	cliaddr.<span style="color: #202020;">sin_addr</span>.<span style="color: #202020;">s_addr</span> <span style="color: #339933;">=</span> inet_addr<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;127.0.0.1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	addrlen <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;please input send msg:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span>msg_send.<span style="color: #202020;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	sendto<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,&amp;</span>msg_send<span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>datasend<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>cliaddr<span style="color: #339933;">,</span>addrlen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	len <span style="color: #339933;">=</span> recvfrom<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>msg_recv<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>datasend<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>cliaddr<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>addrlen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	msg_send.<span style="color: #202020;">number</span> <span style="color: #339933;">=</span> msg_recv.<span style="color: #202020;">number</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	msg_send.<span style="color: #202020;">len</span> <span style="color: #339933;">=</span> randvalue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;version:%3d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">version</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;type:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;len:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">len</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;number:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;data:%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	fprintf<span style="color: #009900;">&#40;</span>fd<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;version:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">version</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	fprintf<span style="color: #009900;">&#40;</span>fd<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;type:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	fprintf<span style="color: #009900;">&#40;</span>fd<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;len:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">len</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	fprintf<span style="color: #009900;">&#40;</span>fd<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;number:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">number</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	fprintf<span style="color: #009900;">&#40;</span>fd<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;data:%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>msg_recv.<span style="color: #202020;">data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>strcmp<span style="color: #009900;">&#40;</span>msg_recv.<span style="color: #202020;">data</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;close&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	fclose<span style="color: #009900;">&#40;</span>fd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
uint8_t randvalue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	srand<span style="color: #009900;">&#40;</span>time<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>rand<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #0000dd;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>



<p>关联文章:<ol><li><a href='http://www.vi1129.com/2010/04/c-hash-table-code/' rel='bookmark' title='Permanent Link: C hash table code'>C hash table code</a></li>
<li><a href='http://www.vi1129.com/2010/02/node/' rel='bookmark' title='Permanent Link: 自写一则单链表小程序'>自写一则单链表小程序</a></li>
<li><a href='http://www.vi1129.com/2009/06/make-timer-with-signal/' rel='bookmark' title='Permanent Link: 使用setitimer和signal创建一个计时器'>使用setitimer和signal创建一个计时器</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/03/sock_raw-dump/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>以太网帧格式，IP头，TCP头格式说明</title>
		<link>http://www.vi1129.com/2010/03/eth-framehead/</link>
		<comments>http://www.vi1129.com/2010/03/eth-framehead/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 04:22:38 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[知识随记]]></category>
		<category><![CDATA[帧格式]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=658</guid>
		<description><![CDATA[本文转自网络，感谢原作者！
圖三、乙太網路的 MAC 訊框
在這個 MAC 當中，最重要的就是那個 6 Bytes 的目的與來源位址了！ 事實上，在所有的乙太網路卡當中都有一個獨一無二的網路卡卡號， 那就是上頭的『目的與來源位址』，這個位址是硬體位址( hardware address )， 共有 6 bytes ，分別由 00:00:00:00:00:00 到 FF:FF:FF:FF:FF:FF， 這 6 bytes 當中，前 3bytes 為廠商的代碼，後 3bytes 則是該廠商自行設定的裝置碼了。 在 Linux 當中，你可以使用 ifconfig 這個指令來查閱你的網路卡卡號喔！ 不過，由於 MAC 主要是與網路卡卡號有關，所以我們也常常將 MAC 作為網路卡卡號的代稱。 特別注意，在這個 MAC 的傳送中，他僅在區域網路內生效， 如果跨過不同的網域 (這個後面 IP 的部分時會介紹)，那麼來源與目的的位址就會跟著改變了。 這是因為變成不同網路卡之間的交流了嘛！所以卡號當然不同了！如下所示：
<p style="MARGIN: 15px">
圖四、在不同主機間持續傳送相同資料的 MAC 訊框變化
例如上面的圖示，我的資料要由電腦 A 通過 B 後才送達 C <p><a href="http://www.vi1129.com/2010/03/eth-framehead/">继续阅读</a></p>


No related posts.]]></description>
			<content:encoded><![CDATA[<div style="MARGIN: 15px"><em>本文转自网络，感谢原作者！</em><a href="http://www.vi1129.com/wp-content/uploads/2010/03/1.bmp"></a></div>
<div style="MARGIN: 15px"><a href="http://www.vi1129.com/wp-content/uploads/2010/03/frame-0.png"><img class="aligncenter size-full wp-image-659" title="frame-0" src="http://www.vi1129.com/wp-content/uploads/2010/03/frame-0.png" alt="frame-0" width="517" height="62" /></a>圖三、乙太網路的 MAC 訊框<br />
在這個 MAC 當中，最重要的就是那個 6 Bytes 的目的與來源位址了！ 事實上，在所有的乙太網路卡當中都有一個獨一無二的網路卡卡號， 那就是上頭的『目的與來源位址』，這個位址是硬體位址( hardware address )， 共有 6 bytes ，分別由 00:00:00:00:00:00 到 FF:FF:FF:FF:FF:FF， 這 6 bytes 當中，前 3bytes 為廠商的代碼，後 3bytes 則是該廠商自行設定的裝置碼了。 在 Linux 當中，你可以使用 ifconfig 這個指令來查閱你的網路卡卡號喔！ 不過，由於 MAC 主要是與網路卡卡號有關，所以我們也常常將 MAC 作為網路卡卡號的代稱。 <span><span style="color: #000088;">特別注意，在這個 MAC 的傳送中，他僅在區域網路內生效， 如果跨過不同的網域 (這個後面 IP 的部分時會介紹)，那麼來源與目的的位址就會跟著改變了。 這是因為變成不同網路卡之間的交流了嘛！所以卡號當然不同了！</span></span>如下所示：</div>
<p style="MARGIN: 15px"><a href="http://www.vi1129.com/wp-content/uploads/2010/03/1.bmp"><img class="aligncenter size-full wp-image-653" title="frame-1" src="http://www.vi1129.com/wp-content/uploads/2010/03/1.bmp" alt="frame-1" /></a><br />
圖四、在不同主機間持續傳送相同資料的 MAC 訊框變化<br />
例如上面的圖示，我的資料要由電腦 A 通過 B 後才送達 C ，而 B 電腦有兩塊網路卡， 其中 MAC-2 與 A 電腦的 MAC-1 互通，至於 MAC-3 則與 C 電腦的 MAC-4 互通。 但是 MAC-1 不能與 MAC-3 與 MAC-4 互通，為啥？因為 MAC-1 這塊網路卡並沒有與 MAC-3 及 MAC-4 使用同樣的 switch/hub 相接嘛！所以，資料的流通會變成：</p>
<div style="MARGIN: 15px">
<ol>
<li>先由 MAC-1 傳送到 MAC-2 ，此時來源是 MAC-1 而目的地是 MAC-2；</li>
<li>B 電腦接收後，察看該訊框，發現目標其實是 C 電腦，而為了與 C 電腦溝通， 所以他會將訊框內的來源 MAC 改為 MAC-3 ，而目的改為 MAC-4 ，如此就可以直接傳送到 C 電腦了。</li>
</ol>
</div>
<p>也就是說，只要透過 B (就是路由器) 才將封包送到另一個網域 (IP 部分會講) 去的時候， 那麼訊框內的硬體位址就會被改變，然後才能夠在同一個網域裡面直接進行 frame 的流通啊！<br />
MAC包大小:旧为1900bytes,大为9000bytes</p>
<div><strong><span style="font-size: medium;"><span style="color: #0000bb;"><span>IP 封包的表頭</span><br />
</span></span></strong></div>
<div>現在我們知道 IP 這個資料封包 (packet) 是需要放置在 MAC 訊框裡面的，所以當然不能比 MAC 所能容許的最大資料量還大！但是 IP 封包其實可以到 65535 bytes 那麼大的吶！ 那麼 IP 封包除了資料之外，他的表頭資料 (head) 是長怎樣呢？ 在<a href="http://linux.vbird.org/linux_server/0110network_basic.php#fig_mac"><span style="color: #0000ff;">圖三的 MAC 訊框表頭</span></a>裡面最重要的莫過於那個網路卡硬體位址， 那麼在 IP 表頭裡面當然就以來源與目標的 IP 位址為最重要囉！ 除此之外， IP 表頭裡面還含有哪些重要資料呢？如底下所示：(下圖第一行為每個欄位的 <span><strong><span style="color: #000088;">bit</span></strong></span> 數)<br />
<a href="http://www.vi1129.com/wp-content/uploads/2010/03/2.png"><img class="aligncenter size-full wp-image-654" title="frame-2" src="http://www.vi1129.com/wp-content/uploads/2010/03/2.png" alt="frame-2" width="492" height="206" /></a><br />
圖八、IP 封包的表頭資料<br />
在上面的圖示中有個地方要注意，那就是『<span><span style="color: #000088;">每一行所佔用的位元數為 32 bits</span></span>』， 也就是說， IP 封包的表頭資料是 32 bits 的倍數喔！那各個表頭的內容分別介紹如下：<span id="more-658"></span></div>
<ul>
<li><strong><span style="color: #000088;"><span>Version(版本)</span><br />
</span></strong>宣告這個 IP 封包的版本，例如目前慣用的還是 IPv4 這個版本，在這裡宣告的。</li>
<li><strong><span style="color: #000088;"><span>IHL(Internet Header Length, IP表頭的長度)</span><br />
</span></strong>告知這個 IP 封包的表頭長度，單位為位元組(bytes)。 此 IHL 長度的範圍為 5~15。</li>
<li><strong><span style="color: #000088;"><span>Type of Service(服務類型)</span><br />
</span></strong>這個項目的內容為『PPPDTRUU』，表示這個 IP 封包的服務類型，主要分為：<br />
PPP：表示此 IP 封包的優先度； D：若為 0 表示一般延遲(delay)，若為 1 表示為低延遲；<br />
T：若為 0 表示為一般傳輸量 (throughput)，若為 1 表示為高傳輸量；<br />
R：若為 0 表示為一般可靠度(reliability)，若為 1 表示高可靠度。<br />
UU：保留尚未被使用。<br />
我們前面談到 gigabit 乙太網路時曾提到 Jumbo frame 對吧！可以提高 MTU， 由於 gigabit 乙太網路的種種相關規格可以讓這個 IP 封包加速且降低延遲， 某些特殊的標誌就是在這裡說明的。</li>
<li><strong><span style="color: #000088;"><span>Total Length(總長度)</span><br />
</span></strong>指這個 IP 封包的總容量，包括表頭與內容 (Data) 部分。最大可達 65535 bytes。</li>
<li><strong><span style="color: #000088;"><span>Identification(辨別碼)</span><br />
</span></strong>我們前面提到 IP 袋子必須要放在 MAC 袋子當中。不過，如果 IP 袋子太大的話， 就得先要將 IP 再重組成較小的袋子然後再放到 MAC 當中。而當 IP 被重組時， 每個來自同一筆資料的小 IP 就得要有個識別碼以告知接收端這些小 IP 其實是來自同一個封包才行。 也就是說，假如 IP 封包其實是 65536 那麼大 (前一個 Total Length 有規定)， 那麼這個 IP 就得要再被分成更小的 IP 分段後才能塞進 MAC 訊框中。那麼每個小 IP 分段是否來自同一個 IP 資料，呵呵！這裡就是那個識別碼啦！</li>
<li><strong><span style="color: #000088;"><span>Flags(特殊旗標)</span><br />
</span></strong>這個地方的內容為『0DM』，其意義為：<br />
D：若為 0 表示可以分段，若為 1 表示不可分段<br />
M：若為 0 表示此 IP 為最後分段，若為 1 表示非最後分段。</li>
<li><strong><span style="color: #000088;"><span>Fragment Offset(分段偏移)</span><br />
</span></strong>表示目前這個 IP 分段在原始的 IP 封包中所佔的位置。 就有點像是序號啦，有這個序號才能將所有的小 IP 分段組合成為原本的 IP 封包大小嘛！ 透過 Total Length, Identification, Flags 以及這個 Fragment Offset 就能夠將小 IP 分段在收受端組合起來囉！</li>
<li><strong><span style="color: #000088;"><span>Time To Live(TTL, 存活時間)</span><br />
</span></strong>表示這個 IP 封包的存活時間，範圍為 0-255。當這個 IP 封包通過一個路由器時， TTL 就會減一，當 TTL 為 0 時，這個封包將會被直接丟棄。說實在的，要讓 IP 封包通過 255 個路由器，還挺難的～ ^_^</li>
<li><strong><span style="color: #000088;"><span>Protocol Number(協定代碼)</span><br />
</span></strong>由於網路上面的封包協定太多了，每個協定都是裝在 IP 當中的， 所以 IP 當然就得在表頭上面告知收受端，這個 IP 內含有的資料是什麼協定才行。 一般常見的網路協定如下所示：<a href="http://www.vi1129.com/wp-content/uploads/2010/03/3.png"><img class="aligncenter size-full wp-image-655" title="frame-3" src="http://www.vi1129.com/wp-content/uploads/2010/03/3.png" alt="frame-3" width="525" height="185" /></a><br />
當然啦，我們比較常見到的還是那個 TCP, UDP, ICMP 說！</li>
<li><strong><span style="color: #000088;"><span>Header Checksum(表頭檢查碼)</span><br />
</span></strong>用來檢查這個 IP 表頭的錯誤檢驗之用。</li>
<li><strong><span style="color: #000088;"><span>Source Address</span><br />
</span></strong>還用講嗎？當然是來源的 IP 位址，相關的 IP 我們之前提過囉！</li>
<li><strong><span style="color: #000088;"><span>Destination Address</span><br />
</span></strong>有來源還需要有目標才能傳送，這裡就是目標的 IP 位址。</li>
<li><strong><span style="color: #000088;"><span>Options (其他參數)</span><br />
</span></strong>這個是額外的功能，提供包括安全處理機制、路由紀錄、時間戳記、 嚴格與寬鬆之來源路由等。</li>
<li><strong><span style="color: #000088;"><span>Padding(補齊項目)</span><br />
</span></strong>由於 Options 的內容不一定有多大，但是我們知道 IP 每個資料都必須要是 32 bits， 所以，若 Options 的資料不足 32 bits 時，則由 padding 主動補齊。</li>
</ul>
<p>你只要知道 IP 表頭裡面還含有： TTL, Protocol, 來源 IP 與目標 IP 也就夠了！ 而這個 IP 表頭的來源與目標 IP ，以及那個判斷通過多少路由器的 TTL ，就能瞭解到這個 IP 將被如何傳送到目的端吶。下一節我們將介紹一下那麼 IP 封包是如何被傳送到目的地？</p>
<div><strong><span style="font-size: medium;"><span style="color: #0000bb;"><span>TCP 協定</span><br />
</span></span></strong></div>
<div>在前幾個小節內談到的 IP 與路由的相關說明中，我們知道 IP 與路由僅能將資料封包傳送到正確的目標而已， 但是這個目的地是否真的能夠收下來這個封包？那可就不一定了。要確認該資料能否正確的被目的端所接收， 就必須要在資料封包上面多加一些參數來判斷才行。</div>
<p>在前面的 OSI 七層協定當中，在網路層的 IP 之上則是傳送層，而傳送層的資料打包成什麼？ 最常見的就是 TCP 封包了。這個 TCP 封包資料必須要能夠放到 IP 的資料袋當中才行喔！ 所以，我們可以將 MAC, IP 與 TCP 的封包資料這樣看：<br />
<a href="http://www.vi1129.com/wp-content/uploads/2010/03/4.png"><img class="aligncenter size-full wp-image-656" title="frame-4" src="http://www.vi1129.com/wp-content/uploads/2010/03/4.png" alt="frame-4" width="520" height="125" /></a><br />
圖十一、各封包之間的相關性<br />
所以說，IP 除了表頭之外的 Data 內容其實就是 TCP 封包的表頭與內容；而 MAC 的 Data 內容， 就是一個完整的 IP 封包資料！這也是我們上頭提到的，最終還是得以 MAC 能夠支援的最大容許容量， 才能夠決定 IP 與 TCP 封包是否需要再進行分段的工作。那麼既然 MAC 與 IP 都有表頭資料， 想當然爾，TCP 也有表頭資料來記錄該封包的相關資訊囉？？沒錯啦～ TCP 封包的表頭是長這個樣子的：</p>
<p><a href="http://www.vi1129.com/wp-content/uploads/2010/03/5.png"><img class="aligncenter size-full wp-image-657" title="frame-5" src="http://www.vi1129.com/wp-content/uploads/2010/03/5.png" alt="frame-5" width="531" height="212" /></a>圖十二、TCP 封包的表頭資料<br />
上圖就是一個 TCP 封包的表頭資料，各個項目以 Source Port, Destination Port 及 Code 算是比較重要的項目，底下我們就分別來談一談各個表頭資料的內容吧！</p>
<ul>
<li><strong><span style="color: #000088;"><span>Source Port &amp; Destination Port ( 來源埠口 &amp; 目標埠口 )</span><br />
</span></strong>什麼是埠口(port)？我們知道 IP 封包的傳送主要是藉由 IP 位址連接兩端， 但是到底這個連線的通道是連接到哪裡去呢？沒錯！就是連接到 port 上頭啦！ 舉例來說，鳥站 (http://linux.vbird.org) 有開放 WWW 伺服器， 這表示鳥站的主機必須要啟動一個可以讓 client 端連接的端口，這個端口就是 port ， 中文翻譯成為埠口。同樣的，用戶端想要連接到鳥哥的鳥站時，就必須要在 client 主機上面啟動一個 port ，這樣這兩個主機才能夠利用這條『通道』來傳遞封包資料喔！ 這個目標與來源 port 的紀錄，可以說是 TCP 封包上最重要的參數了！ 下個小單元我們還會繼續介紹。</li>
<li><strong><span style="color: #000088;"><span>Sequence Number ( 封包序號 )</span><br />
</span></strong>由於 TCP 封包必須要帶入 IP 封包當中，所以如果 TCP 資料太大時(大於 IP 封包的容許程度)， 就得要進行分段。這個 Sequence Number 就是記錄每個封包的序號， 可以讓收受端重新將 TCP 的資料組合起來。</li>
<li><strong><span style="color: #000088;"><span>Acknowledge Number ( 回應序號 ) </span><br />
</span></strong>為了確認主機端確實有收到我們 client 端所送出的封包資料，我們 client 端當然希望能夠收到主機方面的回應，那就是這個 Acknowledge Number 的用途了。 當 client 端收到這個確認碼時，就能夠確定之前傳遞的封包已經被正確的收下了。</li>
<li><strong><span style="color: #000088;"><span>Data Offset (資料補償)</span><br />
</span></strong>在圖十二倒數第二行有個 Options 欄位對吧！那個 Options 的欄位長度是非固定的， 而為了要確認整個 TCP 封包的大小，就需要這個標誌來說明整個封包區段的起始位置。</li>
<li><strong><span style="color: #000088;"><span>Reserved (保留)</span><br />
</span></strong>未使用的保留欄位。</li>
<li><strong><span style="color: #000088;"><span>Code (Control Flag, 控制標誌碼)</span><br />
</span></strong>當我們在進行網路連線的時候，必須要說明這個連線的狀態，好讓接收端瞭解這個封包的主要動作。 這可是一個非常重要的控制碼喔！這個欄位共有 6 個 bits ，分別代表 6 個控制碼，若為 1 則為啟動。分別說明如下：</li>
</ul>
<ul>
<li><span><span style="color: #000088;">URG(Urgent)</span></span>：若為 1 則代表該封包為緊急封包， 接收端應該要緊急處理，且圖十二當中的 Urgent Pointer 欄位也會被啟用。</li>
<li><span><span style="color: #000088;">ACK(Acknowledge)</span></span>：若為 1 代表這個封包為回應封包， 則與上面提到的 Acknowledge Number 有關。</li>
<li><span><span style="color: #000088;">PSH(Push function)</span></span>：若為 1 時， 代表要求對方立即傳送緩衝區內的其他對應封包，而無須等待緩衝區滿了才送。</li>
<li><span><span style="color: #000088;">RST(Reset)</span></span>：如果 RST 為 1 的時候， 表示連線會被馬上結束，而無需等待終止確認手續。這也就是說，這是個強制結束的連線， 且發送端已斷線。</li>
<li><span><span style="color: #000088;">SYN(Synchronous)</span></span>：若為 1 ， 表示發送端希望雙方建立同步處理，也就是要求建立連線。通常帶有 SYN 標誌的封包表示『主動』要連接到對方的意思。</li>
<li><span><span style="color: #000088;">FIN(Finish)</span></span>：若為 1 ，表示傳送結束， 所以通知對方資料傳畢，是否同意斷線，只是發送者還在等待對方的回應而已。</li>
</ul>
<p>其中比較常見到的應該是 ACK/SYN/FIN 等，這三個控制碼是務必要記下來的， 這樣未來在談到防火牆的時候，您才會比較清楚為啥每個 TCP 封包都有所謂的『狀態』條件！ 那就是因為連線方向的不同所致啊！底下我們會進一步討論喔！</p>
<li><strong><span style="color: #000088;"><span>Window (滑動視窗)</span><br />
</span></strong>主要是用來控制封包的流量的，可以告知對方目前本身有的緩衝器容量(Receive Buffer) 還可以接收封包。當 Window=0 時，代表緩衝器已經額滿，所以應該要暫停傳輸資料。 Window 的單位是 byte。</li>
<li><strong><span style="color: #000088;"><span>Checksum(確認檢查碼)</span><br />
</span></strong>當資料要由發送端送出前，會進行一個檢驗的動作，並將該動作的檢驗值標注在這個欄位上； 而接收者收到這個封包之後，會再次的對封包進行驗證，並且比對原發送的 Checksum 值是否相符，如果相符就接受，若不符就會假設該封包已經損毀，進而要求對方重新發送此封包！</li>
<li><strong><span style="color: #000088;"><span>Urgent Pointer(緊急資料)</span><br />
</span></strong>這個欄位是在 Code 欄位內的 URG = 1 時才會產生作用。可以告知緊急資料所在的位置。</li>
<li><strong><span style="color: #000088;"><span>Options(任意資料)</span><br />
</span></strong>目前此欄位僅應用於表示接收端可以接收的最大資料區段容量，若此欄位不使用， 表示可以使用任意資料區段的大小。這個欄位較少使用。</li>
<li><strong><span style="color: #000088;"><span>Padding(補足欄位)</span><br />
</span></strong>如同 IP 封包需要有固定的 32bits 表頭一樣， Options 由於欄位為非固定， 所以也需要 Padding 欄位來加以補齊才行。同樣也是 32 bits 的整數。</li>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/03/eth-framehead/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>自写一则单链表小程序</title>
		<link>http://www.vi1129.com/2010/02/node/</link>
		<comments>http://www.vi1129.com/2010/02/node/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 10:22:17 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[程序人生]]></category>
		<category><![CDATA[链表]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=639</guid>
		<description><![CDATA[<p>发现很多公司面试都喜欢考链表，实际项目中尽管需要自己写链表的时候比较少，但还是会经常用到，链表是一种常用的数据结构。有必要整理一下自己的思路，索性写了一个。包括了链表的初始化，插入，删除，查找，销毁，打印等基本功能，当然考虑了头尾中间等位置情况。仓促完成的简单代码，bug比较多，链表中只存储了一个整形的变量，没有做数据类型的判断，有时候会段错误也难免。已知的一个问题就是创建的链表在有销毁前如果再创建一个的话，会造成内存泄露，因为之前的链表没有销毁头指针便指向了新的位置，旧链表就永远找不到哦。这些问题留在以后实际用到的时候再考虑吧，应付面试这个足矣！</p>

?Download node.c1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include &#60;stdio.h&#62;
#include &#60;stdlib.h&#62;
typedef struct node_t &#123;
	int value;
	struct node_t *next;
&#125; node;
#define LENGTH sizeof(node)
&#160;
node *init_node&#40;&#41;&#123;
  int node_length,n=0;
  node *head=NULL;
  node *node_temp1,*node_temp2;
  node_temp2=&#40;node *&#41;malloc&#40;LENGTH&#41;;
  printf&#40;&#34;node length want to create:&#34;&#41;;
  scanf&#40;&#34;%d&#34;,&#38;node_length&#41;;
  if&#40;node_length&#60;1&#41;&#123;
    printf&#40;&#34;no node to create!&#34;&#41;;
    exit&#40;0&#41;;
  &#125;
  while&#40;n&#60;node_length&#41;&#123;
    node_temp1=&#40;node <p><a href="http://www.vi1129.com/2010/02/node/">继续阅读</a></p>


关联文章:<ol><li><a href='http://www.vi1129.com/2010/03/sock_raw-dump/' rel='bookmark' title='Permanent Link: 使用原始套接字SOCK_RAW捕捉网络数据包并简单分析'>使用原始套接字SOCK_RAW捕捉网络数据包并简单分析</a></li>
<li><a href='http://www.vi1129.com/2010/01/ioctl-getifaddrs-ipv46/' rel='bookmark' title='Permanent Link: ioctl及getifaddrs读取IPv4,IPv6网卡信息'>ioctl及getifaddrs读取IPv4,IPv6网卡信息</a></li>
<li><a href='http://www.vi1129.com/2010/04/c-hash-table-code/' rel='bookmark' title='Permanent Link: C hash table code'>C hash table code</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>发现很多公司面试都喜欢考链表，实际项目中尽管需要自己写链表的时候比较少，但还是会经常用到，链表是一种常用的数据结构。有必要整理一下自己的思路，索性写了一个。包括了链表的初始化，插入，删除，查找，销毁，打印等基本功能，当然考虑了头尾中间等位置情况。仓促完成的简单代码，bug比较多，链表中只存储了一个整形的变量，没有做数据类型的判断，有时候会段错误也难免。已知的一个问题就是创建的链表在有销毁前如果再创建一个的话，会造成内存泄露，因为之前的链表没有销毁头指针便指向了新的位置，旧链表就永远找不到哦。这些问题留在以后实际用到的时候再考虑吧，应付面试这个足矣！</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.vi1129.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=639&amp;download=node.c">node.c</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p63918"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
</pre></td><td class="code" id="p639code18"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> node_t <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> value<span style="color: #339933;">;</span>
	<span style="color: #993333;">struct</span> node_t <span style="color: #339933;">*</span>next<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> node<span style="color: #339933;">;</span>
<span style="color: #339933;">#define LENGTH sizeof(node)</span>
&nbsp;
node <span style="color: #339933;">*</span>init_node<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> node_length<span style="color: #339933;">,</span>n<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
  node <span style="color: #339933;">*</span>head<span style="color: #339933;">=</span>NULL<span style="color: #339933;">;</span>
  node <span style="color: #339933;">*</span>node_temp1<span style="color: #339933;">,*</span>node_temp2<span style="color: #339933;">;</span>
  node_temp2<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>node <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span>LENGTH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;node length want to create:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,&amp;</span>node_length<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>node_length<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;no node to create!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    exit<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">&lt;</span>node_length<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    node_temp1<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>node <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span>LENGTH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;input NO.%d value:&quot;</span><span style="color: #339933;">,</span>n<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,&amp;</span>node_temp1<span style="color: #339933;">-&gt;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    node_temp1<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">=</span>NULL<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">==</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
      head<span style="color: #339933;">=</span>node_temp1<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">else</span>
      node_temp2<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">=</span>node_temp1<span style="color: #339933;">;</span>
    node_temp2<span style="color: #339933;">=</span>node_temp1<span style="color: #339933;">;</span>
    n<span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> head<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
node <span style="color: #339933;">*</span>insert_node<span style="color: #009900;">&#40;</span>node <span style="color: #339933;">*</span>head<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> insert_value<span style="color: #339933;">;</span>
  node <span style="color: #339933;">*</span>temp_node1<span style="color: #339933;">=</span>head<span style="color: #339933;">;</span>
  node <span style="color: #339933;">*</span>temp_node2<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>node <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span>LENGTH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  node <span style="color: #339933;">*</span>temp_node3<span style="color: #339933;">=</span>NULL<span style="color: #339933;">;</span>
  <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;insert value:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,&amp;</span>insert_value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>temp_node1<span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>temp_node1<span style="color: #339933;">-&gt;</span>value<span style="color: #339933;">&lt;</span>insert_value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      temp_node3<span style="color: #339933;">=</span>temp_node1<span style="color: #339933;">;</span>
      temp_node1<span style="color: #339933;">=</span>temp_node1<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  temp_node2<span style="color: #339933;">-&gt;</span>value<span style="color: #339933;">=</span>insert_value<span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>temp_node3<span style="color: #339933;">==</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #808080; font-style: italic;">/*locate the first node*/</span>
    temp_node3<span style="color: #339933;">=</span>head<span style="color: #339933;">;</span>
    temp_node2<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">=</span>temp_node3<span style="color: #339933;">;</span>
    head<span style="color: #339933;">=</span>temp_node2<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>temp_node1<span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  <span style="color: #808080; font-style: italic;">/*locate the medium of head,insert between temp3 and temp1*/</span>    
    temp_node2<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">=</span>temp_node3<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">;</span>
    temp_node3<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">=</span>temp_node2<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>  <span style="color: #808080; font-style: italic;">/*locate the end of head*/</span>
    temp_node2<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">=</span>NULL<span style="color: #339933;">;</span>
    temp_node3<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">=</span>temp_node2<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> head<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
node <span style="color: #339933;">*</span>delete_node<span style="color: #009900;">&#40;</span>node <span style="color: #339933;">*</span>head<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> del_value<span style="color: #339933;">;</span>
  node <span style="color: #339933;">*</span>temp_node1<span style="color: #339933;">=</span>head<span style="color: #339933;">;</span>
  node <span style="color: #339933;">*</span>temp_node2<span style="color: #339933;">;</span>
  <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;input the delete value:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,&amp;</span>del_value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>head<span style="color: #339933;">-&gt;</span>value<span style="color: #339933;">==</span>del_value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #808080; font-style: italic;">/* delete the head of node*/</span>
    temp_node2<span style="color: #339933;">=</span>head<span style="color: #339933;">;</span>
    head<span style="color: #339933;">=</span>head<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> head<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>temp_node1<span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>temp_node1<span style="color: #339933;">-&gt;</span>value<span style="color: #339933;">!=</span>del_value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      temp_node2<span style="color: #339933;">=</span>temp_node1<span style="color: #339933;">;</span>
      temp_node1<span style="color: #339933;">=</span>temp_node1<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>temp_node1<span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    temp_node2<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">=</span>temp_node1<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">;</span>
    free<span style="color: #009900;">&#40;</span>temp_node1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;not find what you want to delete!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> head<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> search_node<span style="color: #009900;">&#40;</span>node <span style="color: #339933;">*</span>head<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> search_value<span style="color: #339933;">,</span>n<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
  node <span style="color: #339933;">*</span>temp_node<span style="color: #339933;">=</span>head<span style="color: #339933;">;</span>
  <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;insert the value which you find:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,&amp;</span>search_value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>temp_node<span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>temp_node<span style="color: #339933;">-&gt;</span>value<span style="color: #339933;">!=</span>search_value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      temp_node<span style="color: #339933;">=</span>temp_node<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">;</span>
      n<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>temp_node<span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;finded what you want.<span style="color: #000099; font-weight: bold;">\n</span>the %d node`s value:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>n<span style="color: #339933;">,</span>temp_node<span style="color: #339933;">-&gt;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">else</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;not find!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> print_node<span style="color: #009900;">&#40;</span>node <span style="color: #339933;">*</span>head<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>head<span style="color: #339933;">==</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;blank node!create first please!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  node <span style="color: #339933;">*</span>temp_node<span style="color: #339933;">=</span>head<span style="color: #339933;">;</span> 
  <span style="color: #b1b100;">do</span><span style="color: #009900;">&#123;</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;value:%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>temp_node<span style="color: #339933;">-&gt;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    temp_node<span style="color: #339933;">=</span>temp_node<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>temp_node<span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
node <span style="color: #339933;">*</span>destroy_node<span style="color: #009900;">&#40;</span>node <span style="color: #339933;">*</span>head<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>head<span style="color: #339933;">==</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;blank node!create first please!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  node <span style="color: #339933;">*</span>temp_node<span style="color: #339933;">;</span>
  <span style="color: #b1b100;">do</span><span style="color: #009900;">&#123;</span>
    temp_node<span style="color: #339933;">=</span>head<span style="color: #339933;">-&gt;</span>next<span style="color: #339933;">;</span>
    free<span style="color: #009900;">&#40;</span>head<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    head<span style="color: #339933;">=</span>temp_node<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>head<span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> head<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> options<span style="color: #339933;">;</span>
  node <span style="color: #339933;">*</span>head<span style="color: #339933;">=</span>NULL<span style="color: #339933;">;</span>
loop<span style="color: #339933;">:</span>
  <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;select options:<span style="color: #000099; font-weight: bold;">\t</span>1:create 2:insert 3:delete 4:find 5:print 6:destory 7:exit<span style="color: #000099; font-weight: bold;">\n</span>input options:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,&amp;</span>options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span>
      head<span style="color: #339933;">=</span>init_node<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">:</span>
      head<span style="color: #339933;">=</span>insert_node<span style="color: #009900;">&#40;</span>head<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">:</span>
      head<span style="color: #339933;">=</span>delete_node<span style="color: #009900;">&#40;</span>head<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">:</span>
      search_node<span style="color: #009900;">&#40;</span>head<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">5</span><span style="color: #339933;">:</span>
      print_node<span style="color: #009900;">&#40;</span>head<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">6</span><span style="color: #339933;">:</span>
      head<span style="color: #339933;">=</span>destroy_node<span style="color: #009900;">&#40;</span>head<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">:</span>
      <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;byebye!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
      <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;invalid options!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">goto</span> loop<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>



<p>关联文章:<ol><li><a href='http://www.vi1129.com/2010/03/sock_raw-dump/' rel='bookmark' title='Permanent Link: 使用原始套接字SOCK_RAW捕捉网络数据包并简单分析'>使用原始套接字SOCK_RAW捕捉网络数据包并简单分析</a></li>
<li><a href='http://www.vi1129.com/2010/01/ioctl-getifaddrs-ipv46/' rel='bookmark' title='Permanent Link: ioctl及getifaddrs读取IPv4,IPv6网卡信息'>ioctl及getifaddrs读取IPv4,IPv6网卡信息</a></li>
<li><a href='http://www.vi1129.com/2010/04/c-hash-table-code/' rel='bookmark' title='Permanent Link: C hash table code'>C hash table code</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/02/node/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>那点简历&#8212;越活越真实</title>
		<link>http://www.vi1129.com/2010/02/realresume/</link>
		<comments>http://www.vi1129.com/2010/02/realresume/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 03:31:33 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[随心所记]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/2010/02/%e9%82%a3%e7%82%b9%e7%ae%80%e5%8e%86-%e8%b6%8a%e6%b4%bb%e8%b6%8a%e7%9c%9f%e5%ae%9e/</guid>
		<description><![CDATA[<p>如果上次的更换工作不算跳槽的话，这次的得算了。
简历改了一遍又一遍，拿着改满意的简历再和刚毕业那会的对比，发现了很多的不同。
主修过N多的课程统统的删掉了，因为发现很多名字都已经有些生疏了；带熟悉字眼的也统统干掉了，其实自己什么都不懂，难道会写个hello就说英语牛X了；精通的字眼大都改成了熟悉、了解，真佩服自己当年的肺活量，太能吹了；实话实说的写了一段自己做过什么，这才是最真实的一段了。
进了这个圈才知道，自己能圈住的原来寥寥无几。随着慢慢的圈住一些东西，才懂得自己应该圈住哪些，外面的世界太大了，抓住适合自己的，真正为我所用的才是关键。
虽说简历要展现自己，做的多么华丽，写的多么坚决，但现在看起来，这些指导让我颇有些心虚，为难。
拿着这份简历面试了一下午，这都是好几周前的事了。现在敢写出来也是因为已经不属于那里，也就不用怎么顾忌。其实人家看的也就是自己觉得最真实的那段。聊的很多东西都是关于具体的项目，职业的规划。看来刚毕业的一大段只能归结为凑字数。
曾经就听到之前的经理闲谈之间说到，凡是说精通的，顶多算个了解，说了解的，大概也就知道个名字。真是悲哀，大家心知肚明，却也只能这样。
真实过后，等我再去吹嘘的时候，应该能吹一个更大，大好几倍的泡泡，想想都觉得刺激。继续努力，鼓励自己了！</p>


<p>No related posts.</p>


No related posts.]]></description>
			<content:encoded><![CDATA[<p>如果上次的更换工作不算跳槽的话，这次的得算了。<br />
简历改了一遍又一遍，拿着改满意的简历再和刚毕业那会的对比，发现了很多的不同。<br />
主修过N多的课程统统的删掉了，因为发现很多名字都已经有些生疏了；带熟悉字眼的也统统干掉了，其实自己什么都不懂，难道会写个hello就说英语牛X了；精通的字眼大都改成了熟悉、了解，真佩服自己当年的肺活量，太能吹了；实话实说的写了一段自己做过什么，这才是最真实的一段了。<br />
进了这个圈才知道，自己能圈住的原来寥寥无几。随着慢慢的圈住一些东西，才懂得自己应该圈住哪些，外面的世界太大了，抓住适合自己的，真正为我所用的才是关键。<br />
虽说简历要展现自己，做的多么华丽，写的多么坚决，但现在看起来，这些指导让我颇有些心虚，为难。<br />
拿着这份简历面试了一下午，这都是好几周前的事了。现在敢写出来也是因为已经不属于那里，也就不用怎么顾忌。其实人家看的也就是自己觉得最真实的那段。聊的很多东西都是关于具体的项目，职业的规划。看来刚毕业的一大段只能归结为凑字数。<br />
曾经就听到之前的经理闲谈之间说到，凡是说精通的，顶多算个了解，说了解的，大概也就知道个名字。真是悲哀，大家心知肚明，却也只能这样。<br />
真实过后，等我再去吹嘘的时候，应该能吹一个更大，大好几倍的泡泡，想想都觉得刺激。继续努力，鼓励自己了！</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/02/realresume/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
