<?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>Fri, 05 Mar 2010 04:46:43 +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>使用原始套接字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/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/02/node/' rel='bookmark' title='Permanent Link: 自写一则单链表小程序'>自写一则单链表小程序</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>
</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="p6634"><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="p663code4"><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="p6635"><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="p663code5"><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="p6636"><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="p663code6"><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/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/02/node/' rel='bookmark' title='Permanent Link: 自写一则单链表小程序'>自写一则单链表小程序</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>
</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/2009/06/snmp-trap/' rel='bookmark' title='Permanent Link: 一种IP改变唤醒SNMP TRAP的实现源码'>一种IP改变唤醒SNMP TRAP的实现源码</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="p6398"><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="p639code8"><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/2009/06/snmp-trap/' rel='bookmark' title='Permanent Link: 一种IP改变唤醒SNMP TRAP的实现源码'>一种IP改变唤醒SNMP TRAP的实现源码</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>8</slash:comments>
		</item>
		<item>
		<title>今天离职了</title>
		<link>http://www.vi1129.com/2010/02/resign/</link>
		<comments>http://www.vi1129.com/2010/02/resign/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 08:28:26 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[随心所记]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/2010/02/%e4%bb%8a%e5%a4%a9%e7%a6%bb%e8%81%8c%e4%ba%86/</guid>
		<description><![CDATA[<p>刚刚办完了离职手续，再有一会就离开这里了
一年多的时光，接触到了很多领域，还是非常感谢公司对我的培养！
祝愿公司日益强大，越来越好！
朝着自己的方向，继续前进了！</p>


<p>No related posts.</p>


No related posts.]]></description>
			<content:encoded><![CDATA[<p>刚刚办完了离职手续，再有一会就离开这里了<br />
一年多的时光，接触到了很多领域，还是非常感谢公司对我的培养！<br />
祝愿公司日益强大，越来越好！<br />
朝着自己的方向，继续前进了！</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/02/resign/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>IPv4组播通信原理</title>
		<link>http://www.vi1129.com/2010/01/multicast-ipv4/</link>
		<comments>http://www.vi1129.com/2010/01/multicast-ipv4/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 06:09:53 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[知识随记]]></category>
		<category><![CDATA[组播]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=623</guid>
		<description><![CDATA[<p>摘自网络，感谢原作者
摘要：
本文试图成为学习TCP/IP网络组播技术的入门材料。文中介绍了组播通信的概念及原理，以及用于组播应用编程的Linux API的详细资料。为了使读者更加完整的了解Linux 组播的整体概念，文中对实现该技术的核心函数也做了介绍。在文章的最后给出了一个简单的C语言套接字编程例子，说明如何创建组播应用程序。</p>
一、导言
<p>在网络中，主机间可以用三种不同的地址进行通信：</p>
<p>单播地址（unicast）：即在子网中主机的唯一地址（接口）。如IP地址：192.168.100.9或MAC地址：80:C0:F6:A0:4A:B1。</p>
<p>广播地址：这种类型的地址用来向子网内的所有主机（接口）发送数据。如广播IP地址是192.168.100.255，MAC广播地址：FF:FF:FF:FF:FF。</p>
<p>组播地址：通过该地址向子网内的多个主机即主机群（接口）发送数据。</p>
<p>如果只是向子网内的部分主机发送报文，组播地址就很有用处了；在需要向多个主机发送多媒体信息（如实时音频、视频）的情况下，考虑到其所需的带宽，分别向每一客户端主机发送数据并不是个好办法，如果发送主机与某些接收端的客户主机不在子网之内，采用广播方式也不是一个好的解决方案。</p>
二、组播地址
<p>大家知道，IP地址空间被划分为A、B、C三类。第四类即D类地址被保留用做组播地址。在第四版的IP协议（IPv4）中，从224.0.0.0到239.255.255.255间的所有IP地址都属于D类地址。</p>
<p>组播地址中最重要的是第24位到27位间的这四位，对应到十进制是224到239，其它28位保留用做组播的组标识，如下图</p>
<p style="text-align: center;">所示：
</p>
<p style="text-align: center;">图1 组播地址示意图</p>
<p>IPv4的组播地址在网络层要转换成网络物理地址。对一个单播的网络地址，通过ARP协议可以获取与IP地址对应的物理地址。但在组播方式下ARP协议无法完成类似功能，必须得用其它的方法获取物理地址。在下面列出的RFC文档中提出了完成这个转换过程的方法：</p>
<p>RFC1112：Multicast IPv4 to Ethernet physical address correspondence</p>
<p>RFC1390：Correspondence to FDDI</p>
<p>RFC1469：Correspondence to Token-Ring networks</p>
<p>在最大的以太网地址范围内，转换过程是这样的：将以太网地址的前24位最固定为01:00:5E，这几位是重要的标志位。紧接着的一位固定为0，其它23位用IPv4组播地址中的低23位来填充。该转换过程如下图所示：</p>
<p style="text-align: center;"></p>
<p style="text-align: center;">图2 地址转换示意图</p>
<p>例如，组播地址为224.0.0.5其以太网物理地址为01:00:5E:00:00:05。</p>
<p>还有一些特殊的IPv4组播地址：</p>
<p>224.0.0.1：标识子网中的所有主机。同一个子网中具有组播功能的主机都是这个组的成员。</p>
<p>224.0.0.2：该地址用来标识网络中每个具有组播功有的路由器。</p>
<p>224.0.0.0&#8212;-224.0.0.255范围内的地址被分配给了低层次的协议。向这些范围内的地址发送数据包，有组播功能的路由器将不会为其提供路由。</p>
<p>239.0.0.0&#8212;-239.255.255.255间的地址分配用做管理用途。这些地址被分配给局部的每一个组织，但不可以分配到组织外部，组织内的路由器不向在组织外的地址提供路由。</p>
<p>除了上面列出的部分组播地址外，还有许多的组播地址。在最新版本的RFC文档“Assinged Numbers”中有完整的介绍。</p>
<p>下面的表中列出了全部的组播地址空间，同时还列出了相应的地址段的常用名称及其TTL（IP包的存活时间）。在IPv4组播方式下，TTL有双重意义：正如大家所知的，TTL原本用来控制数据包在网络中的存活时间，防止由于路由器配置错误导致出现数据包传播的死循环；在组播方式下，它还代表了数据包的活动范围，如：数据包在网络中能够传送多远？这样就可以基于数据包的分类来定义其传送范围。</p>
<p>范围 TTL 地址区间 描述</p>
<p>节点(Node) 0 只能向本机发送的数据包，不能向网络中的其它接口传送</p>
<p>链路(Link) 1 224.0.0.0-224.0.0.255 只能在发送主机所在的一个子网内的传送，不会通过路由器转发。</p>
<p>部门 32 239.255.0.0-239.255.255.255 只在整个组织下的一个部门内(Department) 传送</p>
<p>组织 64 239.192.0.0&#8211;239.195.255.255 在整个组织内传送(Organization)</p>
<p>全局(Global)255 224.0.1.0&#8211;238.255.255.255 没有限制，可全局范围内传送</p>
三、组播的工作过程
<p>在局域网内，主机的网络接口将到目的主机的数据包发送到高层，这些数据包中的目的地址是物理接口地址或广播地址。</p>
<p>如果主机已经加入到一个组播组中，主机的网络接口就会识别出发送到该组成员的数据包。</p>
<p>因此，如果主机接口的物理地址为80:C0:F6:A0:4A:B1，其加入的组播组为224.0.1.10，则发送给主机的数据包中的目的地址必是下面三种类型之一：</p>
<p>接口地址：80:C0:F6:A0:4A:B1</p>
<p>广播地址：FF:FF:FF:FF:FF:FF:FF:FF</p>
<p>组播地址：01:00:5E:00:01:0A</p>
<p>广域网中，路由器必须支持组播路由。当主机中运行的进程加入到某个组播组中时，主机向子网中的所有组播路由器发送IGMP（Internet分组管理协议）报文，告诉路由器凡是发送到这个组播组的组播报文都必须发送到本地的子网中，这样主机的进程就可以接收到报文了。子网中的路由器再通知其它的路由器，这些路由器就知道该将组播报文转发到哪些子网中去。</p>
<p>子网中的路由器也向224.0.0.1发送一个IGMP报文（224.0.0.1 代表组中的全部主机），要求组中的主机提供组的相关信息。组中的主机收到这个报文后，都各将计数器的值设为随机值，当计数器递减为0时再向路由器发送应答。这样就防止了组中所有的主机同时向路由器发送应答，造成网络拥塞。主机向组播地址发送一个报文做为对路由器的应答，组中的其它主机一旦看到这个应答报文，就不再发送应答报文了，因为组中的主机向路由器提供的都是相同的信息，所以子网路由器只需得到组中一个主机提供的信息就可以了。</p>
<p>如果组中的主机都退出了，路由器就收不到应答，因此路由器认为该组目前没有主机加入，遂停止到该子网报文的路由。IGMPv2的解决方案是：组中的主机在退出时向224.0.0.2 发送报文通知组播路由器。</p>
四、应用编程接口（API）
<p>如果你有套接字编程的经验，就会发现，对组播选项所进行的操作只需五个新的套接字操作。函数setsockopt()及getsockopt()用来建立和读取这五个选项的值。下表中列出了组播的可选项，并列出其数据类型和描述：</p>
<p>IPv4 选项 数据类型 描 述</p>
<p>IP_ADD_MEMBERSHIP struct ip_mreq 加入到组播组中</p>
<p>IP_ROP_MEMBERSHIP struct ip_mreq 从组播组中退出</p>
<p>IP_MULTICAST_IF struct <p><a href="http://www.vi1129.com/2010/01/multicast-ipv4/">继续阅读</a></p>


No related posts.]]></description>
			<content:encoded><![CDATA[<p><em>摘自网络，感谢原作者</em><br />
摘要：<br />
本文试图成为学习TCP/IP网络组播技术的入门材料。文中介绍了组播通信的概念及原理，以及用于组播应用编程的Linux API的详细资料。为了使读者更加完整的了解Linux 组播的整体概念，文中对实现该技术的核心函数也做了介绍。在文章的最后给出了一个简单的C语言套接字编程例子，说明如何创建组播应用程序。</p>
<h4>一、导言</h4>
<p>在网络中，主机间可以用三种不同的地址进行通信：</p>
<p>单播地址（unicast）：即在子网中主机的唯一地址（接口）。如IP地址：192.168.100.9或MAC地址：80:C0:F6:A0:4A:B1。</p>
<p>广播地址：这种类型的地址用来向子网内的所有主机（接口）发送数据。如广播IP地址是192.168.100.255，MAC广播地址：FF:FF:FF:FF:FF。</p>
<p>组播地址：通过该地址向子网内的多个主机即主机群（接口）发送数据。</p>
<p>如果只是向子网内的部分主机发送报文，组播地址就很有用处了；在需要向多个主机发送多媒体信息（如实时音频、视频）的情况下，考虑到其所需的带宽，分别向每一客户端主机发送数据并不是个好办法，如果发送主机与某些接收端的客户主机不在子网之内，采用广播方式也不是一个好的解决方案。<span id="more-623"></span></p>
<h4>二、组播地址</h4>
<p>大家知道，IP地址空间被划分为A、B、C三类。第四类即D类地址被保留用做组播地址。在第四版的IP协议（IPv4）中，从224.0.0.0到239.255.255.255间的所有IP地址都属于D类地址。</p>
<p>组播地址中最重要的是第24位到27位间的这四位，对应到十进制是224到239，其它28位保留用做组播的组标识，如下图</p>
<p style="text-align: center;">所示：<br />
<a href="http://www.vi1129.com/wp-content/uploads/2010/01/multicast-1.gif"><img class="size-full wp-image-621   aligncenter" title="multicast-1" src="http://www.vi1129.com/wp-content/uploads/2010/01/multicast-1.gif" alt="multicast-1" width="402" height="134" /></a></p>
<p style="text-align: center;">图1 组播地址示意图</p>
<p>IPv4的组播地址在网络层要转换成网络物理地址。对一个单播的网络地址，通过ARP协议可以获取与IP地址对应的物理地址。但在组播方式下ARP协议无法完成类似功能，必须得用其它的方法获取物理地址。在下面列出的RFC文档中提出了完成这个转换过程的方法：</p>
<p>RFC1112：Multicast IPv4 to Ethernet physical address correspondence</p>
<p>RFC1390：Correspondence to FDDI</p>
<p>RFC1469：Correspondence to Token-Ring networks</p>
<p>在最大的以太网地址范围内，转换过程是这样的：将以太网地址的前24位最固定为01:00:5E，这几位是重要的标志位。紧接着的一位固定为0，其它23位用IPv4组播地址中的低23位来填充。该转换过程如下图所示：</p>
<p style="text-align: center;"><a href="http://www.vi1129.com/wp-content/uploads/2010/01/multicast-1.gif"><img class="size-full wp-image-621 aligncenter" title="multicast-1" src="http://www.vi1129.com/wp-content/uploads/2010/01/multicast-1.gif" alt="multicast-1" width="402" height="134" /></a></p>
<p style="text-align: center;">图2 地址转换示意图</p>
<p>例如，组播地址为224.0.0.5其以太网物理地址为01:00:5E:00:00:05。</p>
<p>还有一些特殊的IPv4组播地址：</p>
<p>224.0.0.1：标识子网中的所有主机。同一个子网中具有组播功能的主机都是这个组的成员。</p>
<p>224.0.0.2：该地址用来标识网络中每个具有组播功有的路由器。</p>
<p>224.0.0.0&#8212;-224.0.0.255范围内的地址被分配给了低层次的协议。向这些范围内的地址发送数据包，有组播功能的路由器将不会为其提供路由。</p>
<p>239.0.0.0&#8212;-239.255.255.255间的地址分配用做管理用途。这些地址被分配给局部的每一个组织，但不可以分配到组织外部，组织内的路由器不向在组织外的地址提供路由。</p>
<p>除了上面列出的部分组播地址外，还有许多的组播地址。在最新版本的RFC文档“Assinged Numbers”中有完整的介绍。</p>
<p>下面的表中列出了全部的组播地址空间，同时还列出了相应的地址段的常用名称及其TTL（IP包的存活时间）。在IPv4组播方式下，TTL有双重意义：正如大家所知的，TTL原本用来控制数据包在网络中的存活时间，防止由于路由器配置错误导致出现数据包传播的死循环；在组播方式下，它还代表了数据包的活动范围，如：数据包在网络中能够传送多远？这样就可以基于数据包的分类来定义其传送范围。</p>
<p>范围 TTL 地址区间 描述</p>
<p>节点(Node) 0 只能向本机发送的数据包，不能向网络中的其它接口传送</p>
<p>链路(Link) 1 224.0.0.0-224.0.0.255 只能在发送主机所在的一个子网内的传送，不会通过路由器转发。</p>
<p>部门 32 239.255.0.0-239.255.255.255 只在整个组织下的一个部门内(Department) 传送</p>
<p>组织 64 239.192.0.0&#8211;239.195.255.255 在整个组织内传送(Organization)</p>
<p>全局(Global)255 224.0.1.0&#8211;238.255.255.255 没有限制，可全局范围内传送</p>
<h4>三、组播的工作过程</h4>
<p>在局域网内，主机的网络接口将到目的主机的数据包发送到高层，这些数据包中的目的地址是物理接口地址或广播地址。</p>
<p>如果主机已经加入到一个组播组中，主机的网络接口就会识别出发送到该组成员的数据包。</p>
<p>因此，如果主机接口的物理地址为80:C0:F6:A0:4A:B1，其加入的组播组为224.0.1.10，则发送给主机的数据包中的目的地址必是下面三种类型之一：</p>
<p>接口地址：80:C0:F6:A0:4A:B1</p>
<p>广播地址：FF:FF:FF:FF:FF:FF:FF:FF</p>
<p>组播地址：01:00:5E:00:01:0A</p>
<p>广域网中，路由器必须支持组播路由。当主机中运行的进程加入到某个组播组中时，主机向子网中的所有组播路由器发送IGMP（Internet分组管理协议）报文，告诉路由器凡是发送到这个组播组的组播报文都必须发送到本地的子网中，这样主机的进程就可以接收到报文了。子网中的路由器再通知其它的路由器，这些路由器就知道该将组播报文转发到哪些子网中去。</p>
<p>子网中的路由器也向224.0.0.1发送一个IGMP报文（224.0.0.1 代表组中的全部主机），要求组中的主机提供组的相关信息。组中的主机收到这个报文后，都各将计数器的值设为随机值，当计数器递减为0时再向路由器发送应答。这样就防止了组中所有的主机同时向路由器发送应答，造成网络拥塞。主机向组播地址发送一个报文做为对路由器的应答，组中的其它主机一旦看到这个应答报文，就不再发送应答报文了，因为组中的主机向路由器提供的都是相同的信息，所以子网路由器只需得到组中一个主机提供的信息就可以了。</p>
<p>如果组中的主机都退出了，路由器就收不到应答，因此路由器认为该组目前没有主机加入，遂停止到该子网报文的路由。IGMPv2的解决方案是：组中的主机在退出时向224.0.0.2 发送报文通知组播路由器。</p>
<h4>四、应用编程接口（API）</h4>
<p>如果你有套接字编程的经验，就会发现，对组播选项所进行的操作只需五个新的套接字操作。函数setsockopt()及getsockopt()用来建立和读取这五个选项的值。下表中列出了组播的可选项，并列出其数据类型和描述：</p>
<p>IPv4 选项 数据类型 描 述</p>
<p>IP_ADD_MEMBERSHIP struct ip_mreq 加入到组播组中</p>
<p>IP_ROP_MEMBERSHIP struct ip_mreq 从组播组中退出</p>
<p>IP_MULTICAST_IF struct ip_mreq 指定提交组播报文的接口</p>
<p>IP_MULTICAST_TTL u_char 指定提交组播报文的TTL</p>
<p>IP_MULTICAST_LOOP u_char 使组播报文环路有效或无效</p>
<p>在头文件中定义了ip_mreq结构：</p>
<p>struct ip_mreq {</p>
<p>struct in_addr imr_multiaddr; /* IP multicast address of group */</p>
<p>struct in_addr imr_interface; /* local IP address of interface */</p>
<p>};</p>
<p>在头文件中组播选项的值为：</p>
<p>#define IP_MULTICAST_IF 32</p>
<p>#define IP_MULTICAST_TTL 33</p>
<p>#define IP_MULTICAST_LOOP 34</p>
<p>#define IP_ADD_MEMBERSHIP 35</p>
<p>#define IP_DROP_MEMBERSHIP 36</p>
<p>IP_ADD_MEMBERSHIP</p>
<p>若进程要加入到一个组播组中，用soket的setsockopt()函数发送该选项。该选项类型是ip_mreq结构，它的第一个字段imr_multiaddr指定了组播组的地址，第二个字段imr_interface指定了接口的IPv4地址。</p>
<p>IP_DROP_MEMBERSHIP</p>
<p>该选项用来从某个组播组中退出。数据结构ip_mreq的使用方法与上面相同。</p>
<p>IP_MULTICAST_IF</p>
<p>该选项可以修改网络接口，在结构ip_mreq中定义新的接口。</p>
<p>IP_MULTICAST_TTL</p>
<p>设置组播报文的数据包的TTL（生存时间）。默认值是1，表示数据包只能在本地的子网中传送。</p>
<p>IP_MULTICAST_LOOP</p>
<p>组播组中的成员自己也会收到它向本组发送的报文。这个选项用于选择是否激活这种状态。</p>
<h4>五、一个组播通信的例子</h4>

<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=623&amp;download=mcastclient.c">mcastclient.c</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p62311"><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
</pre></td><td class="code" id="p623code11"><pre class="c" style="font-family:monospace;">include <span style="color: #339933;">&lt;</span>sys<span style="color: #339933;">/</span>types.<span style="color: #202020;">h</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">#include &lt;sys/socket.h&gt;</span>
<span style="color: #339933;">#include &lt;arpa/inet.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
<span style="color: #339933;">#include &lt;unistd.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define BUFLEN 255</span>
<span style="color: #808080; font-style: italic;">/*********************************************************************
*filename: mcastclient.c
*purpose: 演示组播编程的基本步骤，其实这就是一个基本的UDP客户端程序
*tidied by: zhoulifa(zhoulifa@163.com) 周立发(http://zhoulifa.bokee.com)
Linux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言
*date time:2007-01-25 13:10:00
*Note: 任何人可以任意复制代码并运用这些文档，当然包括你的商业用途
* 但请遵循GPL
*Thanks to: Google.com
*Hope:希望越来越多的人贡献自己的力量，为科学技术发展出力
* 科技站在巨人的肩膀上进步更快！感谢有开源前辈的贡献！
*********************************************************************/</span>
<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>
<span style="color: #993333;">struct</span> sockaddr_in peeraddr<span style="color: #339933;">,</span> myaddr<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> sockfd<span style="color: #339933;">;</span>
<span style="color: #993333;">char</span> recmsg<span style="color: #009900;">&#91;</span>BUFLEN <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> socklen<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 创建 socket 用于UDP通讯 */</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>
<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>
<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;socket creating error<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>
socklen <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: #808080; font-style: italic;">/* 设置对方的端口和IP信息 */</span>
memset<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>peeraddr<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> socklen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
peeraddr.<span style="color: #202020;">sin_family</span> <span style="color: #339933;">=</span> AF_INET<span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
peeraddr.<span style="color: #202020;">sin_port</span> <span style="color: #339933;">=</span> htons<span style="color: #009900;">&#40;</span>atoi<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
peeraddr.<span style="color: #202020;">sin_port</span> <span style="color: #339933;">=</span> htons<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">7838</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #808080; font-style: italic;">/* 注意这里设置的对方地址是指组播地址，而不是对方的实际IP地址 */</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>inet_pton<span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>peeraddr.<span style="color: #202020;">sin_addr</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">0</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;wrong group address!<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;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</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 group address!<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;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 设置自己的端口和IP信息 */</span>
memset<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>myaddr<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> socklen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myaddr.<span style="color: #202020;">sin_family</span> <span style="color: #339933;">=</span> AF_INET<span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
myaddr.<span style="color: #202020;">sin_port</span> <span style="color: #339933;">=</span> htons<span style="color: #009900;">&#40;</span>atoi<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
myaddr.<span style="color: #202020;">sin_port</span> <span style="color: #339933;">=</span> htons<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">23456</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">#if 0</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>argv<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: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>inet_pton<span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>myaddr.<span style="color: #202020;">sin_addr</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">0</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;self ip address error!<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;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> 
<span style="color: #b1b100;">else</span>
<span style="color: #339933;">#endif</span>
&nbsp;
  myaddr.<span style="color: #202020;">sin_addr</span>.<span style="color: #202020;">s_addr</span> <span style="color: #339933;">=</span> INADDR_ANY<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 绑定自己的端口和IP信息到socket上 */</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>myaddr<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: #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>
<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;Bind error<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;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 循环接受用户输入的消息发送组播消息 */</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">;;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #808080; font-style: italic;">/* 接受用户输入 */</span>
sleep<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
bzero<span style="color: #009900;">&#40;</span>recmsg<span style="color: #339933;">,</span> BUFLEN <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
strcpy<span style="color: #009900;">&#40;</span>recmsg<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;I will rock u!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#if 0</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>fgets<span style="color: #009900;">&#40;</span>recmsg<span style="color: #339933;">,</span> BUFLEN<span style="color: #339933;">,</span> stdin<span style="color: #009900;">&#41;</span> <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> EOF<span style="color: #009900;">&#41;</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: #339933;">#endif </span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 发送消息 */</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>sendto
<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span> recmsg<span style="color: #339933;">,</span> strlen<span style="color: #009900;">&#40;</span>recmsg<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>peeraddr<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: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</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;sendto error!<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;">3</span><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;'%s' send ok<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> recmsg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</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=623&amp;download=mcastserver.c">mcastserver.c</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p62312"><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
</pre></td><td class="code" id="p623code12"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;sys/types.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/socket.h&gt;</span>
<span style="color: #339933;">#include &lt;arpa/inet.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
<span style="color: #339933;">#include &lt;netdb.h&gt;</span>
<span style="color: #339933;">#include &lt;errno.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define BUFLEN 255</span>
<span style="color: #808080; font-style: italic;">/*********************************************************************
*filename: mcastserver.c
*purpose: 演示组播编程的基本步骤，组播服务器端，关键在于加入组
*tidied by: zhoulifa(zhoulifa@163.com) 周立发(http://zhoulifa.bokee.com)
Linux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言
*date time:2007-01-25 13:20:00
*Note: 任何人可以任意复制代码并运用这些文档，当然包括你的商业用途
* 但请遵循GPL
*Thanks to: Google.com
*Hope:希望越来越多的人贡献自己的力量，为科学技术发展出力
* 科技站在巨人的肩膀上进步更快！感谢有开源前辈的贡献！
*********************************************************************/</span>
<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>
<span style="color: #993333;">struct</span> sockaddr_in peeraddr<span style="color: #339933;">;</span>
<span style="color: #993333;">struct</span> in_addr ia<span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> sockfd<span style="color: #339933;">;</span>
<span style="color: #993333;">char</span> recmsg<span style="color: #009900;">&#91;</span>BUFLEN <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> socklen<span style="color: #339933;">,</span> n<span style="color: #339933;">;</span>
<span style="color: #993333;">struct</span> hostent <span style="color: #339933;">*</span>group<span style="color: #339933;">;</span>
<span style="color: #993333;">struct</span> ip_mreq mreq<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 创建 socket 用于UDP通讯 */</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>
<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>
<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;socket creating err in udptalk<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>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 设置要加入组播的地址 */</span>
bzero<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>mreq<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> ip_mreq<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>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>group <span style="color: #339933;">=</span> gethostbyname<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> hostent <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #0000dd;">0</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;gethostbyname&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
exit<span style="color: #009900;">&#40;</span>errno<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</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;you should give me a group address, 224.0.0.0-239.255.255.255<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>errno<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
bcopy<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> group<span style="color: #339933;">-&gt;</span>h_addr<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> <span style="color: #339933;">&amp;</span>ia<span style="color: #339933;">,</span> group<span style="color: #339933;">-&gt;</span>h_length<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #808080; font-style: italic;">/* 设置组地址 */</span>
bcopy<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>ia<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>mreq.<span style="color: #202020;">imr_multiaddr</span>.<span style="color: #202020;">s_addr</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> in_addr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 设置发送组播消息的源主机的地址信息 */</span>
mreq.<span style="color: #202020;">imr_interface</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>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 把本机加入组播地址，即本机网卡作为组播成员，只有加入组才能收到组播消息 */</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>setsockopt
<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span> IPPROTO_IP<span style="color: #339933;">,</span> IP_ADD_MEMBERSHIP<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>mreq<span style="color: #339933;">,</span>
<span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> ip_mreq<span style="color: #009900;">&#41;</span><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;setsockopt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
exit<span style="color: #009900;">&#40;</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: #009900;">&#125;</span>
&nbsp;
socklen <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>
memset<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>peeraddr<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> socklen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
peeraddr.<span style="color: #202020;">sin_family</span> <span style="color: #339933;">=</span> AF_INET<span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
peeraddr.<span style="color: #202020;">sin_port</span> <span style="color: #339933;">=</span> htons<span style="color: #009900;">&#40;</span>atoi<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span>
peeraddr.<span style="color: #202020;">sin_port</span> <span style="color: #339933;">=</span> htons<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">7838</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>inet_pton<span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,</span> argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>peeraddr.<span style="color: #202020;">sin_addr</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">0</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;Wrong dest IP address!<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;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</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 group address given, 224.0.0.0-239.255.255.255<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>errno<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 绑定自己的端口和IP信息到socket上 */</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>peeraddr<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: #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>
<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;Bind error<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;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 循环接收网络上来的组播消息 */</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">;;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
bzero<span style="color: #009900;">&#40;</span>recmsg<span style="color: #339933;">,</span> BUFLEN <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
n <span style="color: #339933;">=</span> recvfrom<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span> recmsg<span style="color: #339933;">,</span> BUFLEN<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>peeraddr<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>socklen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</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;recvfrom err in udptalk!<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;">4</span><span style="color: #009900;">&#41;</span><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;">/* 成功接收到数据报 */</span>
recmsg<span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</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;peer:%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> recmsg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</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/01/multicast-ipv4/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ioctl及getifaddrs读取IPv4,IPv6网卡信息</title>
		<link>http://www.vi1129.com/2010/01/ioctl-getifaddrs-ipv46/</link>
		<comments>http://www.vi1129.com/2010/01/ioctl-getifaddrs-ipv46/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 09:14:30 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[程序人生]]></category>
		<category><![CDATA[getifaddrs]]></category>
		<category><![CDATA[ioctl]]></category>
		<category><![CDATA[IPv6]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=617</guid>
		<description><![CDATA[<p>使用ioctl的SIOCGIFCONF可以读取所有网卡信息。ioctl调用后返回指向ifconf的结构链表，其中包含了指向ifreq的结构指针。ifconf及ifreq定义在net/if.h中。</p>
<p>《UNIX网络编程》中提供了get_ifi_info函数的实现方法，使用这种方式来获取网络信息。在LINUX下，这种方式不能获得IPV6的网卡信息。《UNIX网络编程》中有如下描述：</p>
<p>在支持IPV6的系统中，没有关于对SIOCGIFCONF请求是否返回IPV6地址的标准。我们给支持IPV6的新系统增加了一个case语句， 这是为了预防万一。问题在于ifreq中的联合把返回的地址定义成一个通用的16字节套接口地址结构，适合16字节的IPV4 socket_in结构，但对于24字节的IPV6 socket_in6结构太小了。如果返回IPV6地址，将可能破环现有的在每个ifreq结构中采用固定大小的套接口地址结构的代码。</p>
<p>经测试，在fedor6-2.6.18kernel中无法返回ipv6地址，事实上，返回的地址簇总是AF_INET,而并非AF_INET6。
这种方法的实现代码如下：

net_if.h</p>

?View Code 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
#ifndef __NET_INF_H
#define __NET_INF_H
&#160;
#include &#60;net/if.h&#62;
#include &#60;stdio.h&#62;
#include &#60;errno.h&#62;
#include &#60;unistd.h&#62;
#include &#60;netinet/in.h&#62;
#include &#60;sys/ioctl.h&#62;
#include &#60;stdlib.h&#62;
#include &#60;string.h&#62;
#include &#60;sys/types.h&#62;
#include &#60;sys/socket.h&#62;
#include &#60;arpa/inet.h&#62;
&#160;
#define IFI_NAME 16
#define IFI_HADDR 8
&#160;
typedef struct ifi_info
&#123;
  char ifi_name&#91;IFI_NAME&#93;;
  u_char ifi_haddr&#91;IFI_HADDR&#93;;
  u_short ifi_hlen;
  short ifi_flags;
  short ifi_myflags;
  struct sockaddr *ifi_addr;
  struct sockaddr *ifi_brdaddr;
  struct sockaddr *ifi_dstaddr;
  struct ifi_info <p><a href="http://www.vi1129.com/2010/01/ioctl-getifaddrs-ipv46/">继续阅读</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/02/node/' rel='bookmark' title='Permanent Link: 自写一则单链表小程序'>自写一则单链表小程序</a></li>
<li><a href='http://www.vi1129.com/2009/06/snmp-trap/' rel='bookmark' title='Permanent Link: 一种IP改变唤醒SNMP TRAP的实现源码'>一种IP改变唤醒SNMP TRAP的实现源码</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>使用ioctl的SIOCGIFCONF可以读取所有网卡信息。ioctl调用后返回指向ifconf的结构链表，其中包含了指向ifreq的结构指针。ifconf及ifreq定义在net/if.h中。</p>
<p>《UNIX网络编程》中提供了get_ifi_info函数的实现方法，使用这种方式来获取网络信息。在LINUX下，这种方式不能获得IPV6的网卡信息。《UNIX网络编程》中有如下描述：</p>
<blockquote><p>在支持IPV6的系统中，没有关于对SIOCGIFCONF请求是否返回IPV6地址的标准。我们给支持IPV6的新系统增加了一个case语句， 这是为了预防万一。问题在于ifreq中的联合把返回的地址定义成一个通用的16字节套接口地址结构，适合16字节的IPV4 socket_in结构，但对于24字节的IPV6 socket_in6结构太小了。如果返回IPV6地址，将可能破环现有的在每个ifreq结构中采用固定大小的套接口地址结构的代码。</p></blockquote>
<p>经测试，在fedor6-2.6.18kernel中无法返回ipv6地址，事实上，返回的地址簇总是AF_INET,而并非AF_INET6。<br />
这种方法的实现代码如下：<br />
<span id="more-617"></span><br />
net_if.h</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('p617code17'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61717"><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
</pre></td><td class="code" id="p617code17"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#ifndef __NET_INF_H</span>
<span style="color: #339933;">#define __NET_INF_H</span>
&nbsp;
<span style="color: #339933;">#include &lt;net/if.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;errno.h&gt;</span>
<span style="color: #339933;">#include &lt;unistd.h&gt;</span>
<span style="color: #339933;">#include &lt;netinet/in.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/ioctl.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/types.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/socket.h&gt;</span>
<span style="color: #339933;">#include &lt;arpa/inet.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define IFI_NAME 16</span>
<span style="color: #339933;">#define IFI_HADDR 8</span>
&nbsp;
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> ifi_info
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">char</span> ifi_name<span style="color: #009900;">&#91;</span>IFI_NAME<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  u_char ifi_haddr<span style="color: #009900;">&#91;</span>IFI_HADDR<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  u_short ifi_hlen<span style="color: #339933;">;</span>
  <span style="color: #993333;">short</span> ifi_flags<span style="color: #339933;">;</span>
  <span style="color: #993333;">short</span> ifi_myflags<span style="color: #339933;">;</span>
  <span style="color: #993333;">struct</span> sockaddr <span style="color: #339933;">*</span>ifi_addr<span style="color: #339933;">;</span>
  <span style="color: #993333;">struct</span> sockaddr <span style="color: #339933;">*</span>ifi_brdaddr<span style="color: #339933;">;</span>
  <span style="color: #993333;">struct</span> sockaddr <span style="color: #339933;">*</span>ifi_dstaddr<span style="color: #339933;">;</span>
  <span style="color: #993333;">struct</span> ifi_info <span style="color: #339933;">*</span>ifi_next<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>ifi_info<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">#define IFI_ALIAS 1</span>
&nbsp;
<span style="color: #993333;">struct</span> ifi_info <span style="color: #339933;">*</span>get_ifi_info<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> free_ifi_info<span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> ifi_info <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">#endif</span></pre></td></tr></table></div>

<p>net_if.c</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('p617code18'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61718"><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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
</pre></td><td class="code" id="p617code18"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;net_if.h&quot;</span>
&nbsp;
ifi_info <span style="color: #339933;">*</span>get_ifi_info<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> family<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> doaliases<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  ifi_info <span style="color: #339933;">*</span>ifi<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>ifihead<span style="color: #339933;">,</span> <span style="color: #339933;">**</span>ifipnext<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> sockfd<span style="color: #339933;">,</span> len<span style="color: #339933;">,</span> lastlen<span style="color: #339933;">,</span> flags<span style="color: #339933;">,</span> myflags<span style="color: #339933;">;</span>
  <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>ptr<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>buf<span style="color: #339933;">,</span> lastname<span style="color: #009900;">&#91;</span>IFNAMSIZ<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #339933;">*</span>cptr<span style="color: #339933;">;</span>
  <span style="color: #993333;">struct</span> ifconf ifc<span style="color: #339933;">;</span>
  <span style="color: #993333;">struct</span> ifreq <span style="color: #339933;">*</span>ifr<span style="color: #339933;">,</span> ifrcopy<span style="color: #339933;">;</span>
  <span style="color: #993333;">struct</span> sockaddr_in <span style="color: #339933;">*</span>sinptr<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">=</span>socket<span style="color: #009900;">&#40;</span>family<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: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #0000dd;">0</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;socket error.<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>
&nbsp;
  lastlen <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
  len <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">*</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> ifreq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>
    buf <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>malloc<span style="color: #009900;">&#40;</span>len<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ifc.<span style="color: #202020;">ifc_len</span> <span style="color: #339933;">=</span> len<span style="color: #339933;">;</span>
    ifc.<span style="color: #202020;">ifc_buf</span> <span style="color: #339933;">=</span> buf<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> SIOCGIFCONF<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>ifc<span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>errno<span style="color: #339933;">!=</span>EINVAL<span style="color: #339933;">||</span>lastlen<span style="color: #339933;">!=</span><span style="color: #0000dd;">0</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;ioctl error.<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>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifc.<span style="color: #202020;">ifc_len</span> <span style="color: #339933;">==</span> lastlen<span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      lastlen <span style="color: #339933;">=</span> ifc.<span style="color: #202020;">ifc_len</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    len <span style="color: #339933;">+=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">*</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> ifreq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    free<span style="color: #009900;">&#40;</span>buf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  ifihead <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
  ifipnext <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>ifihead<span style="color: #339933;">;</span>
  lastname<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: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>ptr <span style="color: #339933;">=</span> buf<span style="color: #339933;">;</span> ptr<span style="color: #339933;">&lt;</span>buf<span style="color: #339933;">+</span>ifc.<span style="color: #202020;">ifc_len</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    ifr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> ifreq<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>ptr<span style="color: #339933;">;</span>
<span style="color: #339933;">#ifdef HAVE_SOCKADDR_SA_LEN</span>
    len <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr<span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span>ifr<span style="color: #339933;">-&gt;</span>ifr_addr.<span style="color: #202020;">sa_len</span><span style="color: #339933;">?</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr<span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>ifr<span style="color: #339933;">-&gt;</span>ifr_addr.<span style="color: #202020;">sa_len</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#else</span>
    <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span>ifr<span style="color: #339933;">-&gt;</span>ifr_addr.<span style="color: #202020;">sa_family</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
<span style="color: #339933;">#ifdef IPV6</span>
    <span style="color: #b1b100;">case</span> AF_INET6<span style="color: #339933;">:</span>
      len <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in6<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: #339933;">#endif</span>
    <span style="color: #b1b100;">case</span> AF_INET<span style="color: #339933;">:</span>
    <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
      len <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr<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: #009900;">&#125;</span>
<span style="color: #339933;">#endif</span>
&nbsp;
    ptr <span style="color: #339933;">+=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>ifr<span style="color: #339933;">-&gt;</span>ifr_name<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> len<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifr<span style="color: #339933;">-&gt;</span>ifr_addr.<span style="color: #202020;">sa_family</span> <span style="color: #339933;">!=</span> family<span style="color: #009900;">&#41;</span>
      <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
&nbsp;
    myflags <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>cptr<span style="color: #339933;">=</span>strchr<span style="color: #009900;">&#40;</span>ifr<span style="color: #339933;">-&gt;</span>ifr_name<span style="color: #339933;">,</span> <span style="color: #ff0000;">':'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">*</span>cptr <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>strncmp<span style="color: #009900;">&#40;</span>lastname<span style="color: #339933;">,</span> ifr<span style="color: #339933;">-&gt;</span>ifr_name<span style="color: #339933;">,</span> IFNAMSIZ<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>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>doaliases <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
      myflags <span style="color: #339933;">=</span> IFI_ALIAS<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    memcpy<span style="color: #009900;">&#40;</span>lastname<span style="color: #339933;">,</span> ifr<span style="color: #339933;">-&gt;</span>ifr_name<span style="color: #339933;">,</span> IFNAMSIZ<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    ifrcopy <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>ifr<span style="color: #339933;">;</span>
    ioctl<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span> SIOCGIFFLAGS<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>ifrcopy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    flags <span style="color: #339933;">=</span> ifrcopy.<span style="color: #202020;">ifr_flags</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>flags<span style="color: #339933;">&amp;</span>IFF_UP<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: #b1b100;">continue</span><span style="color: #339933;">;</span>
    <span style="color: #808080; font-style: italic;">/*
    if ((flags&amp;IFF_BROADCAST)==0)
      continue;
    */</span>
    ifi <span style="color: #339933;">=</span> calloc<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> ifi_info<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #339933;">*</span>ifipnext <span style="color: #339933;">=</span> ifi<span style="color: #339933;">;</span>
    ifipnext <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_next<span style="color: #339933;">;</span>
    ifi<span style="color: #339933;">-&gt;</span>ifi_flags <span style="color: #339933;">=</span> flags<span style="color: #339933;">;</span>
    ifi<span style="color: #339933;">-&gt;</span>ifi_myflags <span style="color: #339933;">=</span> myflags<span style="color: #339933;">;</span>
    memcpy<span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_name<span style="color: #339933;">,</span> ifr<span style="color: #339933;">-&gt;</span>ifr_name<span style="color: #339933;">,</span> IFI_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ifi<span style="color: #339933;">-&gt;</span>ifi_name<span style="color: #009900;">&#91;</span>IFI_NAME<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span>ifr<span style="color: #339933;">-&gt;</span>ifr_addr.<span style="color: #202020;">sa_family</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">case</span> AF_INET<span style="color: #339933;">:</span>
      sinptr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>ifr<span style="color: #339933;">-&gt;</span>ifr_addr<span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_addr <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        ifi<span style="color: #339933;">-&gt;</span>ifi_addr <span style="color: #339933;">=</span> calloc<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><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: #009900;">&#41;</span><span style="color: #339933;">;</span>
        memcpy<span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_addr<span style="color: #339933;">,</span> sinptr<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#ifdef SIOCGIFBRDADDR</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>flags <span style="color: #339933;">&amp;</span> IFF_BROADCAST<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          ioctl<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span> SIOCGIFBRDADDR<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>ifrcopy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          sinptr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>ifrcopy.<span style="color: #202020;">ifr_broadaddr</span><span style="color: #339933;">;</span>
          ifi<span style="color: #339933;">-&gt;</span>ifi_brdaddr <span style="color: #339933;">=</span> calloc<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><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: #009900;">&#41;</span><span style="color: #339933;">;</span>
          memcpy<span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_brdaddr<span style="color: #339933;">,</span> sinptr<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">#endif</span>
<span style="color: #339933;">#ifdef SIOCGIFDSTADDR</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>flags <span style="color: #339933;">&amp;</span> IFF_POINTOPOINT<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          ioctl<span style="color: #009900;">&#40;</span>sockfd<span style="color: #339933;">,</span> SIOCGIFDSTADDR<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>ifrcopy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          sinptr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>ifrcopy.<span style="color: #202020;">ifr_dstaddr</span><span style="color: #339933;">;</span>
          ifi<span style="color: #339933;">-&gt;</span>ifi_dstaddr <span style="color: #339933;">=</span> calloc<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><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: #009900;">&#41;</span><span style="color: #339933;">;</span>
          memcpy<span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_dstaddr<span style="color: #339933;">,</span> sinptr<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">#endif</span>
      <span style="color: #009900;">&#125;</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>
      <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  free<span style="color: #009900;">&#40;</span>buf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>ifihead<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> free_ifi_info<span style="color: #009900;">&#40;</span>ifi_info <span style="color: #339933;">*</span>ifihead<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  ifi_info <span style="color: #339933;">*</span>ifi<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>ifinext<span style="color: #339933;">;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">=</span>ifihead<span style="color: #339933;">;</span> ifi<span style="color: #339933;">!=</span>NULL<span style="color: #339933;">;</span> ifi<span style="color: #339933;">=</span>ifinext<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_addr<span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span>
      free<span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_addr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_brdaddr<span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span>
      free<span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_brdaddr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_dstaddr<span style="color: #339933;">!=</span>NULL<span style="color: #009900;">&#41;</span>
      free<span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_dstaddr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ifinext <span style="color: #339933;">=</span> ifi<span style="color: #339933;">-&gt;</span>ifi_next<span style="color: #339933;">;</span>
&nbsp;
    free<span style="color: #009900;">&#40;</span>ifi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">char</span> <span style="color: #339933;">*</span>sock_ntop<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">struct</span> sockaddr <span style="color: #339933;">*</span>sa<span style="color: #339933;">,</span> socklen_t salen<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">char</span> portstr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #993333;">static</span> <span style="color: #993333;">char</span> str<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">128</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span>sa<span style="color: #339933;">-&gt;</span>sa_family<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">case</span> AF_INET<span style="color: #339933;">:</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #993333;">struct</span> sockaddr_in <span style="color: #339933;">*</span>sin <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>sa<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>inet_ntop<span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>sin<span style="color: #339933;">-&gt;</span>sin_addr<span style="color: #339933;">,</span> str<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span>NULL<span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ntohs<span style="color: #009900;">&#40;</span>sin<span style="color: #339933;">-&gt;</span>sin_port<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>
        snprintf<span style="color: #009900;">&#40;</span>portstr<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>portstr<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;.%d&quot;</span><span style="color: #339933;">,</span> ntohs<span style="color: #009900;">&#40;</span>sin<span style="color: #339933;">-&gt;</span>sin_port<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        strcat<span style="color: #009900;">&#40;</span>str<span style="color: #339933;">,</span> portstr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">return</span> str<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">case</span> AF_INET6<span style="color: #339933;">:</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #993333;">struct</span> sockaddr_in6 <span style="color: #339933;">*</span>sin <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in6 <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>sa<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>inet_ntop<span style="color: #009900;">&#40;</span>AF_INET6<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>sin<span style="color: #339933;">-&gt;</span>sin6_addr<span style="color: #339933;">,</span> str<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span>NULL<span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ntohs<span style="color: #009900;">&#40;</span>sin<span style="color: #339933;">-&gt;</span>sin6_port<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>
        snprintf<span style="color: #009900;">&#40;</span>portstr<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>portstr<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;.%d&quot;</span><span style="color: #339933;">,</span> ntohs<span style="color: #009900;">&#40;</span>sin<span style="color: #339933;">-&gt;</span>sin6_port<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        strcat<span style="color: #009900;">&#40;</span>str<span style="color: #339933;">,</span> portstr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">return</span> str<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</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>
    <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</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>
  ifi_info <span style="color: #339933;">*</span>ifi<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>ifihead<span style="color: #339933;">;</span>
  <span style="color: #993333;">struct</span> sockaddr <span style="color: #339933;">*</span>sa<span style="color: #339933;">;</span>
  u_char <span style="color: #339933;">*</span>ptr<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> family<span style="color: #339933;">,</span> doaliases<span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argc<span style="color: #339933;">!=</span><span style="color: #0000dd;">3</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;usage: ./prifinfo &lt;inet4 | inet 6&gt; &lt;doaliases&gt;&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>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>strcmp<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;inet4&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>
    family <span style="color: #339933;">=</span> AF_INET<span style="color: #339933;">;</span>
<span style="color: #339933;">#ifdef IPV6</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>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;inet6&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>
    family <span style="color: #339933;">=</span>AF_INET6<span style="color: #339933;">;</span>
<span style="color: #339933;">#endif</span>
  <span style="color: #b1b100;">else</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;invalid &lt;address-family&gt;&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>
&nbsp;
  doaliases <span style="color: #339933;">=</span> atoi<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>ifihead <span style="color: #339933;">=</span> ifi <span style="color: #339933;">=</span> get_ifi_info<span style="color: #009900;">&#40;</span>family<span style="color: #339933;">,</span> doaliases<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      ifi<span style="color: #339933;">!=</span>NULL<span style="color: #339933;">;</span>ifi<span style="color: #339933;">=</span>ifi<span style="color: #339933;">-&gt;</span>ifi_next<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;%s:&lt;&quot;</span><span style="color: #339933;">,</span> ifi<span style="color: #339933;">-&gt;</span>ifi_name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_flags<span style="color: #339933;">&amp;</span>IFF_UP<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;UP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_flags<span style="color: #339933;">&amp;</span>IFF_BROADCAST<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;BCAST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_flags<span style="color: #339933;">&amp;</span>IFF_MULTICAST<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;MCAST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_flags<span style="color: #339933;">&amp;</span>IFF_LOOPBACK<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;LOOP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_flags<span style="color: #339933;">&amp;</span>IFF_POINTOPOINT<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;P2P&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;&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span>ifi<span style="color: #339933;">-&gt;</span>ifi_hlen<span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      ptr <span style="color: #339933;">=</span> ifi<span style="color: #339933;">-&gt;</span>ifi_haddr<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;%s%x&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">==</span>ifi<span style="color: #339933;">-&gt;</span>ifi_hlen<span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #339933;">:</span><span style="color: #ff0000;">&quot;:&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">*</span>ptr<span style="color: #339933;">++</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: #339933;">--</span>i<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">0</span><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;<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: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>sa<span style="color: #339933;">=</span>ifi<span style="color: #339933;">-&gt;</span>ifi_addr<span style="color: #009900;">&#41;</span><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; IP addr: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
             sock_ntop<span style="color: #009900;">&#40;</span>sa<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>sa<span style="color: #009900;">&#41;</span><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><span style="color: #009900;">&#40;</span>sa<span style="color: #339933;">=</span>ifi<span style="color: #339933;">-&gt;</span>ifi_brdaddr<span style="color: #009900;">&#41;</span><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; broadcast addr: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
             sock_ntop<span style="color: #009900;">&#40;</span>sa<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>sa<span style="color: #009900;">&#41;</span><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><span style="color: #009900;">&#40;</span>sa<span style="color: #339933;">=</span>ifi<span style="color: #339933;">-&gt;</span>ifi_dstaddr<span style="color: #009900;">&#41;</span><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; destnation addr: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
             sock_ntop<span style="color: #009900;">&#40;</span>sa<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>sa<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  free_ifi_info<span style="color: #009900;">&#40;</span>ifihead<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  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></pre></td></tr></table></div>

<p>使用gcc net_if.c -o net_if -DIPV6编译，在IPV4模式下运行输出为：</p>
<blockquote><p>[root@localhost net_if]./net_if inet4 1<br />
lo:<UPLOOP><br />
IP addr: 127.0.0.1<br />
eth1:<UPBCASTMCAST><br />
IP addr: 192.168.1.2<br />
broadcast addr: 192.168.1.255<br />
eth0:<UPBCASTMCAST><br />
IP addr: 192.168.125.99<br />
broadcast addr: 192.168.125.255</p></blockquote>
<p>执行./net_if inet6 1在输出为空。</p>
<p>第二种方式是使用getifaddrs函数获取，需要包含ifaddrs.h头文件，这种方式可以获得IPV6地址，改写的《UNIX网络编程》中的get_ifi_info函数如下所示：</p>
<p>znet.h</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('p617code19'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61719"><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
</pre></td><td class="code" id="p617code19"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#ifndef __ZNET_H__</span>
<span style="color: #339933;">#define __ZNET_H__</span>
&nbsp;
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/socket.h&gt;</span>
<span style="color: #339933;">#include &lt;arpa/inet.h&gt;</span>
<span style="color: #339933;">#include &lt;net/if.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/ioctl.h&gt;</span>
<span style="color: #339933;">#include &lt;ifaddrs.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define	IFI_NAME	16			/* same as IFNAMSIZ in &lt;net/if.h&gt; */</span>
<span style="color: #339933;">#define	IFI_HADDR	 8			/* allow for 64-bit EUI-64 in future */</span>
&nbsp;
<span style="color: #993333;">struct</span> ifi_info <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">char</span>    ifi_name<span style="color: #009900;">&#91;</span>IFI_NAME<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>	<span style="color: #808080; font-style: italic;">/* interface name, null-terminated */</span>
  <span style="color: #993333;">short</span>   ifi_index<span style="color: #339933;">;</span>			<span style="color: #808080; font-style: italic;">/* interface index */</span>
  <span style="color: #993333;">short</span>   ifi_flags<span style="color: #339933;">;</span>			<span style="color: #808080; font-style: italic;">/* IFF_xxx constants from &lt;net/if.h&gt; */</span>
  <span style="color: #993333;">struct</span> sockaddr  <span style="color: #339933;">*</span>ifi_addr<span style="color: #339933;">;</span>	<span style="color: #808080; font-style: italic;">/* primary address */</span>
  <span style="color: #993333;">struct</span> sockaddr  <span style="color: #339933;">*</span>ifi_brdaddr<span style="color: #339933;">;</span><span style="color: #808080; font-style: italic;">/* broadcast address */</span>
  <span style="color: #993333;">struct</span> ifi_info  <span style="color: #339933;">*</span>ifi_next<span style="color: #339933;">;</span>	<span style="color: #808080; font-style: italic;">/* next of these structures */</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">struct</span> ifi_info<span style="color: #339933;">*</span> get_ifi_info<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> free_ifi_info<span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> ifi_info <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">#endif</span></pre></td></tr></table></div>

<p>znet.c</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('p617code20'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61720"><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
</pre></td><td class="code" id="p617code20"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;znet.h&quot;</span>
&nbsp;
<span style="color: #993333;">struct</span> ifi_info<span style="color: #339933;">*</span> get_ifi_info<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> family<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> doaliases<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">struct</span> ifi_info		<span style="color: #339933;">*</span>ifi<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>ifihead<span style="color: #339933;">,</span> <span style="color: #339933;">**</span>ifipnext<span style="color: #339933;">,*</span>p<span style="color: #339933;">;</span>
	<span style="color: #993333;">struct</span> sockaddr_in	<span style="color: #339933;">*</span>sinptr<span style="color: #339933;">;</span>
	<span style="color: #993333;">struct</span> sockaddr_in6	<span style="color: #339933;">*</span>sin6ptr<span style="color: #339933;">;</span>
	<span style="color: #993333;">struct</span> ifaddrs <span style="color: #339933;">*</span>ifas<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//	char addr[128];</span>
	<span style="color: #993333;">int</span> sockfd<span style="color: #339933;">;</span>
&nbsp;
	ifihead <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	ifipnext <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>ifihead<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>getifaddrs<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>ifas<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: #b1b100;">return</span> <span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">;</span>ifas<span style="color: #339933;">!=</span>NULL<span style="color: #339933;">;</span>ifas<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>ifas<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">ifa_next</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>ifas<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">ifa_addr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>sa_family <span style="color: #339933;">!=</span> family<span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// ignore if not desired address family</span>
<span style="color: #808080; font-style: italic;">/*
		printf(&quot;%s %d\n&quot;,(*ifas).ifa_name,((*ifas).ifa_addr)-&gt;sa_family);
		if(((*ifas).ifa_addr)-&gt;sa_family!=AF_INET6)
			inet_ntop(AF_INET,&amp;(((struct sockaddr_in *)((*ifas).ifa_addr))-&gt;sin_addr),addr,sizeof(addr));
		else
			inet_ntop(AF_INET6,&amp;(((struct sockaddr_in6 *)((*ifas).ifa_addr))-&gt;sin6_addr),addr,sizeof(addr));
		printf(&quot;%s\t&quot;,addr);
		printf(&quot;\n&quot;);
*/</span>		
		ifi <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> ifi_info<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>calloc<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> ifi_info<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #339933;">*</span>ifipnext <span style="color: #339933;">=</span> ifi<span style="color: #339933;">;</span>			
		ifipnext <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_next<span style="color: #339933;">;</span>	
&nbsp;
		ifi<span style="color: #339933;">-&gt;</span>ifi_flags <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>ifas<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">ifa_flags</span><span style="color: #339933;">;</span>
		memcpy<span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_name<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>ifas<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">ifa_name</span><span style="color: #339933;">,</span> IFI_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ifi<span style="color: #339933;">-&gt;</span>ifi_name<span style="color: #009900;">&#91;</span>IFI_NAME<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>ifas<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">ifa_addr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>sa_family<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">case</span> AF_INET<span style="color: #339933;">:</span> 
				sinptr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>ifas<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">ifa_addr</span><span style="color: #339933;">;</span>
				ifi<span style="color: #339933;">-&gt;</span>ifi_addr <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>calloc<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><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: #009900;">&#41;</span><span style="color: #339933;">;</span>
				memcpy<span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_addr<span style="color: #339933;">,</span> sinptr<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: #009900;">&#41;</span><span style="color: #339933;">;</span>			
<span style="color: #339933;">#ifdef	SIOCGIFBRDADDR</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_flags <span style="color: #339933;">&amp;</span> IFF_BROADCAST<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					sinptr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>ifas<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">ifa_broadaddr</span><span style="color: #339933;">;</span>
					ifi<span style="color: #339933;">-&gt;</span>ifi_brdaddr <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>calloc<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><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: #009900;">&#41;</span><span style="color: #339933;">;</span>
					memcpy<span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_brdaddr<span style="color: #339933;">,</span> sinptr<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">#endif</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span> 
&nbsp;
			<span style="color: #b1b100;">case</span> AF_INET6<span style="color: #339933;">:</span> 
				sin6ptr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in6 <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>ifas<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">ifa_addr</span><span style="color: #339933;">;</span>
				ifi<span style="color: #339933;">-&gt;</span>ifi_addr <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>calloc<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in6<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				memcpy<span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_addr<span style="color: #339933;">,</span> sin6ptr<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in6<span style="color: #009900;">&#41;</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> 
&nbsp;
			<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	freeifaddrs<span style="color: #009900;">&#40;</span>ifas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
	<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span>ifihead<span style="color: #009900;">&#41;</span><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: #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> family<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argc<span style="color: #339933;">!=</span><span style="color: #0000dd;">2</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;usage: ./znet &lt;inet4 | inet 6&gt;<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>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>strcmp<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;inet4&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>
		family <span style="color: #339933;">=</span> AF_INET<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>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;inet6&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>
		family <span style="color: #339933;">=</span>AF_INET6<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</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;invalid &lt;address-family&gt;<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>
&nbsp;
	<span style="color: #993333;">char</span> addr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">128</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">struct</span> ifi_info	<span style="color: #339933;">*</span>ifi<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>ifihead<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;name<span style="color: #000099; font-weight: bold;">\t</span>flag<span style="color: #000099; font-weight: bold;">\t</span>IP<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>broadcastaddr<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;">for</span> <span style="color: #009900;">&#40;</span>ifihead <span style="color: #339933;">=</span> ifi <span style="color: #339933;">=</span> get_ifi_info<span style="color: #009900;">&#40;</span>family<span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> ifi <span style="color: #339933;">!=</span> NULL<span style="color: #339933;">;</span> ifi <span style="color: #339933;">=</span> ifi<span style="color: #339933;">-&gt;</span>ifi_next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;%s<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span>ifi<span style="color: #339933;">-&gt;</span>ifi_name<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;%d<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span>ifi<span style="color: #339933;">-&gt;</span>ifi_flags<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: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_addr<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>sa_family<span style="color: #339933;">!=</span>AF_INET6<span style="color: #009900;">&#41;</span>
			inet_ntop<span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_addr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>sin_addr<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>addr<span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>addr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span>
			inet_ntop<span style="color: #009900;">&#40;</span>AF_INET6<span style="color: #339933;">,&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in6 <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_addr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>sin6_addr<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>addr<span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>addr<span style="color: #009900;">&#41;</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;%s<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span>addr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#ifdef	SIOCGIFBRDADDR</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_flags <span style="color: #339933;">&amp;</span> IFF_BROADCAST<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_addr<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>sa_family<span style="color: #339933;">!=</span>AF_INET6<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			inet_ntop<span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> sockaddr_in <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>ifi<span style="color: #339933;">-&gt;</span>ifi_brdaddr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>sin_addr<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>addr<span style="color: #339933;">,</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>addr<span style="color: #009900;">&#41;</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;%s<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span>addr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">#endif			</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>+++++++++++++++++++++++++++++++++++++++++++<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: #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>这段代码输出如下：</p>
<blockquote><p>[root@localhost net_if]./znet inet4<br />
name    flag    IP    broadcastaddr<br />
lo         73     127.0.0.1<br />
++++++++++++++++++++++++++++++<br />
eth1     4099   192.168.1.2     192.168.1.255<br />
++++++++++++++++++++++++++++++<br />
eth0     4163   192.168.125.99     192.168.125.255<br />
++++++++++++++++++++++++++++++<br />
[root@localhost net_if]./znet inet6<br />
name    flag    IP    broadcastaddr<br />
lo         73     ::1<br />
++++++++++++++++++++++++++++++<br />
eth1     4163   2001:250:1800:1::1<br />
++++++++++++++++++++++++++++++<br />
eth0     4163   2001:250:1888:1::1<br />
++++++++++++++++++++++++++++++</p></blockquote>


<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/02/node/' rel='bookmark' title='Permanent Link: 自写一则单链表小程序'>自写一则单链表小程序</a></li>
<li><a href='http://www.vi1129.com/2009/06/snmp-trap/' rel='bookmark' title='Permanent Link: 一种IP改变唤醒SNMP TRAP的实现源码'>一种IP改变唤醒SNMP TRAP的实现源码</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/01/ioctl-getifaddrs-ipv46/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>无语的今天</title>
		<link>http://www.vi1129.com/2010/01/nowords/</link>
		<comments>http://www.vi1129.com/2010/01/nowords/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 15:53:56 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[随心所记]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=613</guid>
		<description><![CDATA[<p>今天还真是郁闷，坐公交没有零钱了，就跟司机师傅说了一声，放5块钱进去，司机师傅同意让我在门口收三块零钱。收了两块还顺利，就差一块钱了，比较背的事情就发生了。一个女孩上来正要投硬币，我说了句找零钱，给我一个。大概是她没听清，也怪我没说清了。好像犹豫了一下还是把两个硬币都扔进了收款的柜子。我几乎无语，也再没说什么。谁知她开始翻自己的包，我还正纳闷她干嘛呢，只见她抬起头来说了一句没零的了，向后边走去了。</p>
<p>愣了我半天，憋出来一句喃喃了一下，把我当什么了？有穿成这样站司机旁边大摇大摆乞讨的么？</p>


<p>No related posts.</p>


No related posts.]]></description>
			<content:encoded><![CDATA[<p>今天还真是郁闷，坐公交没有零钱了，就跟司机师傅说了一声，放5块钱进去，司机师傅同意让我在门口收三块零钱。收了两块还顺利，就差一块钱了，比较背的事情就发生了。一个女孩上来正要投硬币，我说了句找零钱，给我一个。大概是她没听清，也怪我没说清了。好像犹豫了一下还是把两个硬币都扔进了收款的柜子。我几乎无语，也再没说什么。谁知她开始翻自己的包，我还正纳闷她干嘛呢，只见她抬起头来说了一句没零的了，向后边走去了。</p>
<p>愣了我半天，憋出来一句喃喃了一下，把我当什么了？有穿成这样站司机旁边大摇大摆乞讨的么？</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/01/nowords/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>网络桥接的使用方法</title>
		<link>http://www.vi1129.com/2010/01/bridge/</link>
		<comments>http://www.vi1129.com/2010/01/bridge/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 02:15:43 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[知识随记]]></category>
		<category><![CDATA[brctl]]></category>
		<category><![CDATA[桥接]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=610</guid>
		<description><![CDATA[<p>连接两个局域网的方法除了路由外，比较简单的一个方法就是网络桥接了。这里将列出linux下创建网络桥接的一般步骤。</p>
<p>如下图的所示的网络环境中，中间的桥接计算机具有无线和有线网卡各一块，连接了两个局域网。在这个网络环境中两个局域网处于同一个网段，它可能是由桥接计算机上的DHCP自动分配的地址。这样做，最终产生的情况将是无线终端连接到了桥接计算机的无线网卡ath0(ath0工作在AP模式)，有线网局域网通过交换机连接到了桥接计算机的eth0(有线网卡)，连接在不同的两个物理网络上，通过桥接，使他们工作在同一个局域网中，同时，可以隔离两个物理网络，这是网络隔离中比较常见的手段。</p>
<p>linux下使用桥接功能必须确保已经安装了bridge-utils，桥接方法如下:</p>
<p>1.创建br0: brctl addbr br0</p>
<p>2.添加物理网口：brctl addif br0 eth0; brctl addif br0 ath0</p>
<p>3.配置br0 IP: ifconfig br0 192.168.1.100 netmask 255.255.255.0 up</p>
<p>4. 启用物理网络：ifconfig ath0 up; ifconfig eth0 up</p>
<p>5.修改DHCP配置，使之在br0网口上分配地址。</p>
<p>如需要，可以用iptables来隔离两个物理网络。</p>


<p>No related posts.</p>


No related posts.]]></description>
			<content:encoded><![CDATA[<p>连接两个局域网的方法除了路由外，比较简单的一个方法就是网络桥接了。这里将列出linux下创建网络桥接的一般步骤。</p>
<p>如下图的所示的网络环境中，中间的桥接计算机具有无线和有线网卡各一块，连接了两个局域网。在这个网络环境中两个局域网处于同一个网段，它可能是由桥接计算机上的DHCP自动分配的地址。这样做，最终产生的情况将是无线终端连接到了桥接计算机的无线网卡ath0(ath0工作在AP模式)，有线网局域网通过交换机连接到了桥接计算机的eth0(有线网卡)，连接在不同的两个物理网络上，通过桥接，使他们工作在同一个局域网中，同时，可以隔离两个物理网络，这是网络隔离中比较常见的手段。</p>
<p><a href="http://www.vi1129.com/wp-content/uploads/2010/01/brctl1.gif"><img class="alignleft size-full wp-image-609" title="brctl" src="http://www.vi1129.com/wp-content/uploads/2010/01/brctl1.gif" alt="brctl" width="360" height="171" /></a>linux下使用桥接功能必须确保已经安装了bridge-utils，桥接方法如下:</p>
<p>1.创建br0: brctl addbr br0</p>
<p>2.添加物理网口：brctl addif br0 eth0; brctl addif br0 ath0</p>
<p>3.配置br0 IP: ifconfig br0 192.168.1.100 netmask 255.255.255.0 up</p>
<p>4. 启用物理网络：ifconfig ath0 up; ifconfig eth0 up</p>
<p>5.修改DHCP配置，使之在br0网口上分配地址。</p>
<p>如需要，可以用iptables来隔离两个物理网络。</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/01/bridge/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>关于const与指针搭配使用的一点记录</title>
		<link>http://www.vi1129.com/2010/01/const-ptr/</link>
		<comments>http://www.vi1129.com/2010/01/const-ptr/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 05:26:22 +0000</pubDate>
		<dc:creator>keykey</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[程序人生]]></category>
		<category><![CDATA[const]]></category>
		<category><![CDATA[指针]]></category>

		<guid isPermaLink="false">http://www.vi1129.com/?p=601</guid>
		<description><![CDATA[<p>const限定符指定了一个变量为只读变量，是不允许被改变的，因此const变量在定义时就必须初始化。
const在与指针搭配时，使用将变的复杂和微妙。简单的说const搭配指针就会出现以下三种情况：</p>

指向const变量(对象)的指针
const指针
指向const变量(对象)的const指针

<p>1.指向const变量(对象)的指针
指针指向了const变量，例如 const int *ptr或者int const *ptr，这两种写法含义一样，这表示const限定了ptr所指向的数据类型，而并非ptr本身。即ptr本身并不是const.可以对ptr重新赋值，无需在定义时初始化。
指向const变量的指针可以指向一个const变量，也可以指向一个非const变量，当然指针类型与变量类型要一致。不管指向了一个const变量还是非const变量，任何企图通过这个指针去修改变量的值都会导致编译错误。同时，const变量只能赋给指向const变量的指针，赋给一个普通变量也是不允许的。
如（代码仅供理解const,不要刻意追求是否正确可执行）：</p>
<p>const int *ptr;
int *ptr_1;
const int a=12;
const int b=13;
int c=14;
ptr=&#38;a;/*指向const变量*/
ptr=&#38;b;/*重新赋值指向const变量*/
*ptr=20;/*error,不能通过ptr修改*/
ptr=&#38;c;/*重新赋值指向非const变量*/
ptr_1=&#38;c;/*普通指针指向普通变量*/
ptr_1=&#38;a;/*error，普通指针不能指向const变量*/</p>
<p>《C++ Primer》中对此有详细的解释，尽管不能通过指向const对象的指针去修改基础对象，然而如果该指针指向的是一个非const对象，可以用其他方法修改所指的对象。如下的例子：</p>
<p>const int *ptr;
int *ptr_1;
int c=14;
ptr=&#38;c;
printf(&#8221;c=%d\n&#8221;,*ptr);
ptr_1=&#38;c;
*ptr_1=18/*通过非指向const的指针修改*/
printf(&#8221;c=%d\n&#8221;,*ptr);</p>
<p>输出结果可以看到两次输出是不一样的。对此，《C++ Primer》中说，如果把指向const的指针理解为“自以为指向const的指针”，这可能会对理解有所帮助。</p>
<p>2.const指针</p>
<p>int a=12;
int *const ptr=&#38;a</p>
<p>这意味着ptr这个指针被定义成const变量，必须在定义时初始化，并不能重新对ptr赋值，即不能将ptr再指向其他变量。但是，*ptr=13，这样的操作完全取决与a是不是一个const变量，在这个例子中不是，所以*ptr=13是正确的，假如a这样定义，const int a=12,*ptr=13会导致错误。</p>
<p>3.指向const变量(对象)的const指针</p>
<p>const int a=12;
const int *const ptr=&#38;a;</p>
<p>这就意味着ptr不能被重新赋值，因为它是const指针，定义时需初始化；同时不能通过ptr修改a，因为a也是const变量。</p>
<p>4.有趣的typedef</p>
<p>typedef string *pstring;
const pstring cstr;</p>
<p>这时，cstr是一个指向string类型的const指针，而不是一个指向const string的普通指针。因为const pstring cstr;,const修饰的是pstring这个类型，而这个类型是一个string指针。其实：</p>
<p>string s;
typedef string *pstring;
/*以下三个定义相同，都是指向string的const指针*/
const pstring cstr1=&#38;a;
pstring const cstr2=&#38;a;
string *const cstr3=&#38;a;</p>
<p>5.记忆方法
记忆方法其实很简单，只需看清const和*的位置就可以，const在前，则是指向const变量的指针，如：</p>
<p>const int *ptr;int const *ptr;</p>
<p>*在前，则是const指针，如：</p>
<p>int *const ptr;</p>
<p>两个const，则是指向const的const指针了，如：</p>
<p>const int *const ptr;</p>


<p>关联文章:strftime和strptime使用
MD5应用的一点理解及Linux实现源码
使用原始套接字SOCK_RAW捕捉网络数据包并简单分析
</p>


关联文章:<ol><li><a href='http://www.vi1129.com/2009/07/strftime-and-strptime-usage/' rel='bookmark' title='Permanent Link: strftime和strptime使用'>strftime和strptime使用</a></li>
<li><a href='http://www.vi1129.com/2009/11/linux-md5/' rel='bookmark' title='Permanent Link: MD5应用的一点理解及Linux实现源码'>MD5应用的一点理解及Linux实现源码</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>
</ol>]]></description>
			<content:encoded><![CDATA[<p>const限定符指定了一个变量为只读变量，是不允许被改变的，因此const变量在定义时就必须初始化。<br />
const在与指针搭配时，使用将变的复杂和微妙。简单的说const搭配指针就会出现以下三种情况：</p>
<ul>
<li>指向const变量(对象)的指针</li>
<li>const指针</li>
<li>指向const变量(对象)的const指针</li>
</ul>
<p><strong>1.指向const变量(对象)的指针</strong><br />
指针指向了const变量，例如 const int *ptr或者int const *ptr，这两种写法含义一样，这表示const限定了ptr所指向的数据类型，而并非ptr本身。即ptr本身并不是const.可以对ptr重新赋值，无需在定义时初始化。<br />
指向const变量的指针可以指向一个const变量，也可以指向一个非const变量，当然指针类型与变量类型要一致。不管指向了一个const变量还是非const变量，任何企图通过这个指针去修改变量的值都会导致编译错误。同时，const变量只能赋给指向const变量的指针，赋给一个普通变量也是不允许的。<span id="more-601"></span><br />
如（代码仅供理解const,不要刻意追求是否正确可执行）：</p>
<blockquote><p>const int *ptr;<br />
int *ptr_1;<br />
const int a=12;<br />
const int b=13;<br />
int c=14;<br />
ptr=&amp;a;/*指向const变量*/<br />
ptr=&amp;b;/*重新赋值指向const变量*/<br />
*ptr=20;/*error,不能通过ptr修改*/<br />
ptr=&amp;c;/*重新赋值指向非const变量*/<br />
ptr_1=&amp;c;/*普通指针指向普通变量*/<br />
ptr_1=&amp;a;/*error，普通指针不能指向const变量*/</p></blockquote>
<p>《C++ Primer》中对此有详细的解释，尽管不能通过指向const对象的指针去修改基础对象，然而如果该指针指向的是一个非const对象，可以用其他方法修改所指的对象。如下的例子：</p>
<blockquote><p>const int *ptr;<br />
int *ptr_1;<br />
int c=14;<br />
ptr=&amp;c;<br />
printf(&#8221;c=%d\n&#8221;,*ptr);<br />
ptr_1=&amp;c;<br />
*ptr_1=18/*通过非指向const的指针修改*/<br />
printf(&#8221;c=%d\n&#8221;,*ptr);</p></blockquote>
<p>输出结果可以看到两次输出是不一样的。对此，《C++ Primer》中说，如果把指向const的指针理解为“自以为指向const的指针”，这可能会对理解有所帮助。</p>
<p><strong>2.const指针</strong></p>
<blockquote><p>int a=12;<br />
int *const ptr=&amp;a</p></blockquote>
<p>这意味着ptr这个指针被定义成const变量，必须在定义时初始化，并不能重新对ptr赋值，即不能将ptr再指向其他变量。但是，*ptr=13，这样的操作完全取决与a是不是一个const变量，在这个例子中不是，所以*ptr=13是正确的，假如a这样定义，const int a=12,*ptr=13会导致错误。</p>
<p><strong>3.指向const变量(对象)的const指针</strong></p>
<blockquote><p>const int a=12;<br />
const int *const ptr=&amp;a;</p></blockquote>
<p>这就意味着ptr不能被重新赋值，因为它是const指针，定义时需初始化；同时不能通过ptr修改a，因为a也是const变量。</p>
<p><strong>4.有趣的typedef</strong></p>
<blockquote><p>typedef string *pstring;<br />
const pstring cstr;</p></blockquote>
<p>这时，cstr是一个指向string类型的const指针，而不是一个指向const string的普通指针。因为const pstring cstr;,const修饰的是pstring这个类型，而这个类型是一个string指针。其实：</p>
<blockquote><p>string s;<br />
typedef string *pstring;<br />
/*以下三个定义相同，都是指向string的const指针*/<br />
const pstring cstr1=&amp;a;<br />
pstring const cstr2=&amp;a;<br />
string *const cstr3=&amp;a;</p></blockquote>
<p><strong>5.记忆方法</strong><br />
记忆方法其实很简单，只需看清const和*的位置就可以，const在前，则是指向const变量的指针，如：</p>
<blockquote><p>const int *ptr;int const *ptr;</p></blockquote>
<p>*在前，则是const指针，如：</p>
<blockquote><p>int *const ptr;</p></blockquote>
<p>两个const，则是指向const的const指针了，如：</p>
<blockquote><p>const int *const ptr;</p></blockquote>


<p>关联文章:<ol><li><a href='http://www.vi1129.com/2009/07/strftime-and-strptime-usage/' rel='bookmark' title='Permanent Link: strftime和strptime使用'>strftime和strptime使用</a></li>
<li><a href='http://www.vi1129.com/2009/11/linux-md5/' rel='bookmark' title='Permanent Link: MD5应用的一点理解及Linux实现源码'>MD5应用的一点理解及Linux实现源码</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>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.vi1129.com/2010/01/const-ptr/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
