﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-I believe I can make a miracle-随笔分类-代码摘抄</title><link>http://www.cnitblog.com/wujian-IT/category/6356.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 28 Sep 2011 11:28:41 GMT</lastBuildDate><pubDate>Wed, 28 Sep 2011 11:28:41 GMT</pubDate><ttl>60</ttl><item><title>并发性单线程服务器</title><link>http://www.cnitblog.com/wujian-IT/archive/2007/11/05/35910.html</link><dc:creator>吴剑</dc:creator><author>吴剑</author><pubDate>Mon, 05 Nov 2007 14:10:00 GMT</pubDate><guid>http://www.cnitblog.com/wujian-IT/archive/2007/11/05/35910.html</guid><wfw:comment>http://www.cnitblog.com/wujian-IT/comments/35910.html</wfw:comment><comments>http://www.cnitblog.com/wujian-IT/archive/2007/11/05/35910.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/wujian-IT/comments/commentRss/35910.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/wujian-IT/services/trackbacks/35910.html</trackback:ping><description><![CDATA[// File: prg6_9.c<br>&nbsp;&nbsp; &nbsp;#include &lt;stdio.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* These are the usual header files */ <br>&nbsp;&nbsp; &nbsp;#include &lt;strings.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* for bzero() */<br>&nbsp;&nbsp; &nbsp;#include &lt;unistd.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* for close() */<br>&nbsp;&nbsp; &nbsp;#include &lt;sys/types.h&gt; <br>&nbsp;&nbsp; &nbsp;#include &lt;sys/socket.h&gt; <br>&nbsp;&nbsp; &nbsp;#include &lt;netinet/in.h&gt; <br>&nbsp;&nbsp; &nbsp;#include &lt;arpa/inet.h&gt;<br>&nbsp;&nbsp; &nbsp;#include &lt;sys/time.h&gt;<br>&nbsp;&nbsp; &nbsp;#include &lt;stdlib.h&gt;<br><br>&nbsp;&nbsp; &nbsp;#define PORT 1234&nbsp;&nbsp; /* Port that will be opened */ <br>&nbsp;&nbsp; &nbsp;#define BACKLOG 5&nbsp;&nbsp; /* Number of allowed connections */ <br>&nbsp;&nbsp; &nbsp;#define MAXDATASIZE 1000 <br>&nbsp;&nbsp; &nbsp;typedef struct CLIENT{<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; int&nbsp;&nbsp; &nbsp; fd;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; char*&nbsp; name;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; struct sockaddr_in addr; /* client's address information */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; char* data;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;}; &nbsp;<br>&nbsp;&nbsp; &nbsp;void process_cli(CLIENT *client, char* recvbuf, int len);<br>&nbsp;&nbsp; &nbsp;void savedata(char* recvbuf, int len, char* data);<br><br>&nbsp;&nbsp; &nbsp;main() <br>&nbsp;&nbsp; &nbsp;{ <br>&nbsp;&nbsp; &nbsp;int&nbsp;&nbsp; &nbsp;i, maxi, maxfd,sockfd;<br>&nbsp;&nbsp; &nbsp;int&nbsp;&nbsp; &nbsp;nready;<br>&nbsp;&nbsp; &nbsp;ssize_t&nbsp;&nbsp; &nbsp;n;<br>&nbsp;&nbsp; &nbsp;fd_set&nbsp;&nbsp; &nbsp;rset, allset;<br>&nbsp;&nbsp; &nbsp;int listenfd, connectfd; /* socket descriptors */&nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;struct sockaddr_in server; /* server's address information */ <br>&nbsp;&nbsp; &nbsp;/* client's information */ <br>&nbsp;&nbsp; &nbsp;CLIENT client[FD_SETSIZE];<br>&nbsp;&nbsp; &nbsp;char recvbuf[MAXDATASIZE];<br>&nbsp;&nbsp; &nbsp;int sin_size; <br><br>&nbsp;&nbsp; &nbsp;/* Create TCP socket&nbsp; */<br>&nbsp;&nbsp; &nbsp;if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /* handle exception */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; perror("Creating socket failed.");<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; exit(1);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp; &nbsp;int opt = SO_REUSEADDR;<br>&nbsp;&nbsp; &nbsp;setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &amp;opt, sizeof(opt));<br><br>&nbsp;&nbsp; &nbsp;bzero(&amp;server,sizeof(server));<br>&nbsp;&nbsp; &nbsp;server.sin_family=AF_INET; <br>&nbsp;&nbsp; &nbsp;server.sin_port=htons(PORT); <br>&nbsp;&nbsp; &nbsp;server.sin_addr.s_addr = htonl (INADDR_ANY); <br>&nbsp;&nbsp; &nbsp;if (bind(listenfd, (struct sockaddr *)&amp;server, sizeof(struct sockaddr)) == -1) { <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /* handle exception */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; perror("Bind error.");<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; exit(1); <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; &nbsp;<br><br>&nbsp;&nbsp; &nbsp;if(listen(listenfd,BACKLOG) == -1){&nbsp; /* calls listen() */ <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; perror("listen() error\n"); <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; exit(1); <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } <br><br>&nbsp;&nbsp; &nbsp;sin_size=sizeof(struct sockaddr_in); <br>&nbsp;&nbsp; &nbsp;/*initialize for select */<br>&nbsp;&nbsp; &nbsp;maxfd = listenfd;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;maxi = -1;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;for (i = 0; i &lt; FD_SETSIZE; i++) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; client[i].fd = -1;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; &nbsp;FD_ZERO(&amp;allset);<br>&nbsp;&nbsp; &nbsp;FD_SET(listenfd, &amp;allset);<br><br>&nbsp;&nbsp; &nbsp;while(1)<br>&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp; &nbsp;struct sockaddr_in addr;<br>&nbsp;&nbsp; &nbsp;rset = allset;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;nready = select(maxfd+1, &amp;rset, NULL, NULL, NULL);<br><br>&nbsp;&nbsp; &nbsp;if (FD_ISSET(listenfd, &amp;rset)) {&nbsp;&nbsp; &nbsp;/* new client connection */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /* Accept connection */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if ((connectfd = accept(listenfd,(struct sockaddr *)&amp;addr,&amp;sin_size))==-1) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror("accept() error\n"); <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; continue; <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /* Put new fd to client */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (i = 0; i &lt; FD_SETSIZE; i++)<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (client[i].fd &lt; 0) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client[i].fd = connectfd;&nbsp;&nbsp; &nbsp;/* save descriptor */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client[i].name = new char[MAXDATASIZE];<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client[i].addr = addr;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client[i].data = new char[MAXDATASIZE];<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client[i].name[0] = '\0';<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client[i].data[0] = '\0';<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("You got a connection from %s.&nbsp; ",inet_ntoa(client[i].addr.sin_addr) ); <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i == FD_SETSIZE)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; printf("too many clients\n");<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FD_SET(connectfd, &amp;allset);&nbsp;&nbsp; &nbsp;/* add new descriptor to set */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (connectfd &gt; maxfd)&nbsp; maxfd = connectfd;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i &gt; maxi)&nbsp;&nbsp; &nbsp;maxi = i;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (--nready &lt;= 0) continue;&nbsp;&nbsp; &nbsp;/* no more readable descriptors */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (i = 0; i &lt;= maxi; i++) {&nbsp;&nbsp; &nbsp;/* check all clients for data */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( (sockfd = client[i].fd) &lt; 0)&nbsp;&nbsp; &nbsp;continue;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (FD_ISSET(sockfd, &amp;rset)) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( (n = recv(sockfd, recvbuf, MAXDATASIZE,0)) == 0) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*connection closed by client */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; close(sockfd);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Client( %s ) closed connection. User's data: %s\n",client[i].name,client[i].data);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FD_CLR(sockfd, &amp;allset);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client[i].fd = -1;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete client[i].name;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete client[i].data;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; process_cli(&amp;client[i], recvbuf, n);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (--nready &lt;= 0)&nbsp;&nbsp; &nbsp;break;&nbsp;&nbsp; &nbsp;/* no more readable descriptors */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;close(listenfd);&nbsp;&nbsp; /* close listenfd */&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;} <br><br>&nbsp;&nbsp; &nbsp;void process_cli(CLIENT *client, char* recvbuf, int len)<br>&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp; &nbsp;char sendbuf[MAXDATASIZE];<br><br>&nbsp;&nbsp; &nbsp;recvbuf[len-1] = '\0';<br>&nbsp;&nbsp; &nbsp;if (strlen(client-&gt;name) == 0) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; /* Got client's name from client */<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; memcpy(client-&gt;name,recvbuf, len);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("Client's name is %s.\n",client-&gt;name);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp; &nbsp;/* save client's data */<br>&nbsp;&nbsp; &nbsp;printf("Received client( %s ) message: %s\n",client-&gt;name, recvbuf);<br>&nbsp;&nbsp; &nbsp;/* save user's data */<br>&nbsp;&nbsp; &nbsp;savedata(recvbuf,len, client-&gt;data);<br>&nbsp;&nbsp; &nbsp;/* reverse usr's data */<br>&nbsp;&nbsp; &nbsp;for (int i1 = 0; i1 &lt; len - 1; i1++) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sendbuf[i1] = recvbuf[len - i1 -2];<br>&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;sendbuf[len - 1] = '\0';<br><br>&nbsp;&nbsp; &nbsp;send(client-&gt;fd,sendbuf,strlen(sendbuf),0); <br>&nbsp;&nbsp; &nbsp;}<br><br>&nbsp;&nbsp; &nbsp;void savedata(char* recvbuf, int len, char* data)<br>&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp; &nbsp;int start = strlen(data);<br>&nbsp;&nbsp; &nbsp;for (int i = 0; i &lt; len; i++) {<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; data[start + i] = recvbuf[i];<br>&nbsp;&nbsp; &nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;}<br><br><br>
<br><img src ="http://www.cnitblog.com/wujian-IT/aggbug/35910.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/wujian-IT/" target="_blank">吴剑</a> 2007-11-05 22:10 <a href="http://www.cnitblog.com/wujian-IT/archive/2007/11/05/35910.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符串转化成16进制</title><link>http://www.cnitblog.com/wujian-IT/archive/2007/10/21/35155.html</link><dc:creator>吴剑</dc:creator><author>吴剑</author><pubDate>Sun, 21 Oct 2007 15:44:00 GMT</pubDate><guid>http://www.cnitblog.com/wujian-IT/archive/2007/10/21/35155.html</guid><wfw:comment>http://www.cnitblog.com/wujian-IT/comments/35155.html</wfw:comment><comments>http://www.cnitblog.com/wujian-IT/archive/2007/10/21/35155.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/wujian-IT/comments/commentRss/35155.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/wujian-IT/services/trackbacks/35155.html</trackback:ping><description><![CDATA[//********************************************************* &nbsp; <br> &nbsp; // &nbsp;  &nbsp;  &nbsp; <br>&nbsp; &nbsp; // &nbsp;  &nbsp;  &nbsp;
<p><nobr id="key2" style="border-bottom: 0px dotted; text-decoration: underline; color: #6600ff; background-color: transparent;" onclick="return kwc();" target="_blank" oncontextmenu="return false;" onmouseover="kwE(event,2, this);" onmouseout="kwL(event, this);" onmousemove="kwM(2);"></nobr>//字符串转化成16进制存入CHAR数组中 &nbsp; <br> &nbsp; // &nbsp;  &nbsp;  &nbsp; 作者:QQTCC &nbsp; <br> &nbsp; // &nbsp;  &nbsp;  &nbsp; 日期:2006.12.7 &nbsp; <br> &nbsp; // &nbsp; <br> &nbsp; //********************************************************* &nbsp; <br> &nbsp; #include &nbsp; &lt;iostream&gt; &nbsp; <br> &nbsp; using &nbsp; namespace &nbsp; std; &nbsp; <br> &nbsp; bool &nbsp; isNumber(char &nbsp; *p) &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; //字符串位是否数字或字母判断; &nbsp; <br> &nbsp; { &nbsp; <br> &nbsp; 	if(*p&gt;='0'&amp;&amp; &nbsp; *p&lt;='9') &nbsp; <br> &nbsp; 	{return &nbsp; true;} &nbsp; <br> &nbsp; 	else &nbsp; return &nbsp; false; &nbsp; <br> &nbsp; } &nbsp; <br> &nbsp;  &nbsp; <br> &nbsp; void &nbsp; convert(char &nbsp; *p,unsigned &nbsp; char &nbsp; str[]) &nbsp; <br> &nbsp; { &nbsp; <br> &nbsp; 	int &nbsp; high,low,i=0; &nbsp; <br> &nbsp; 	while(*p!='\0') &nbsp; <br> &nbsp; 	{ &nbsp; <br> &nbsp; 		if(isNumber(p)) &nbsp; <br> &nbsp; 		{high=(*p-48)&lt;&lt;4; &nbsp; } &nbsp;  &nbsp;  &nbsp; //按ASCII转换成整数并左移4位 &nbsp; <br> &nbsp; 		else &nbsp; <br> &nbsp; 		{high=(*p-55)&lt;&lt;4; &nbsp; } &nbsp;  &nbsp;  &nbsp; //ASCII转换成整数左移4位 &nbsp; <br> &nbsp; 		high=high&amp;0xF0; &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; //低4位屏蔽 &nbsp; <br> &nbsp; 		++p; &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; //读低4位 &nbsp; <br> &nbsp; 		if(isNumber(p)) &nbsp; <br> &nbsp; 		{low=*p-48; &nbsp; } &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; //左移4位 &nbsp; <br> &nbsp; 		else &nbsp; <br> &nbsp; 		{low=*p-55; &nbsp;  &nbsp; } &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; //左移4位 &nbsp; <br> &nbsp; 		low=low&amp;0x0F; &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; //高4位屏蔽 &nbsp; <br> &nbsp; 		str[i++]=low|high; &nbsp; <br> &nbsp; 		++p; &nbsp; <br> &nbsp; 	} &nbsp; <br> &nbsp; } &nbsp; <br> &nbsp; void &nbsp; main() &nbsp; <br> &nbsp; { &nbsp; <br> &nbsp; 	char &nbsp; str[]="1234567890ABCDEF"; &nbsp; <br> &nbsp; 	unsigned &nbsp; char &nbsp; dest[8]; &nbsp; <br> &nbsp; 	char &nbsp; *p=str; &nbsp; <br> &nbsp; 	convert(p,dest);	 &nbsp; <br> &nbsp; 	for(int &nbsp; j=0;j&lt;8;j++) &nbsp; <br> &nbsp; 	{cout&lt;&lt;hex&lt;&lt;dest[j]&lt;&lt;endl;}	//结果扩展ASCII,我查表验证过. &nbsp; <br> &nbsp; }</p>
<br><img src ="http://www.cnitblog.com/wujian-IT/aggbug/35155.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/wujian-IT/" target="_blank">吴剑</a> 2007-10-21 23:44 <a href="http://www.cnitblog.com/wujian-IT/archive/2007/10/21/35155.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>main函数执行后执行的代码</title><link>http://www.cnitblog.com/wujian-IT/archive/2007/10/16/34968.html</link><dc:creator>吴剑</dc:creator><author>吴剑</author><pubDate>Tue, 16 Oct 2007 13:57:00 GMT</pubDate><guid>http://www.cnitblog.com/wujian-IT/archive/2007/10/16/34968.html</guid><wfw:comment>http://www.cnitblog.com/wujian-IT/comments/34968.html</wfw:comment><comments>http://www.cnitblog.com/wujian-IT/archive/2007/10/16/34968.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/wujian-IT/comments/commentRss/34968.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/wujian-IT/services/trackbacks/34968.html</trackback:ping><description><![CDATA[<p>如果你需要加入一段在main退出后执行的代码，可以使用atexit()函数，注册一个函数。</p>
<p>#include &lt;stdio.h&gt;<br>
#include &lt;stdlib.h&gt;</p>
<p>void fn1( void ), fn2( void ), fn3( void ), fn4( void );</p>
<p>// atexit()以栈的方式注册函数，先注册的函数会后执行。</p>
<p>void main( void )<br>
{<br>
atexit( fn1 );<br>
atexit( fn2 );<br>
atexit( fn3 );<br>
atexit( fn4 );</p>
<p>printf( &#8220;This is executed first.\n&#8221; );</p>
<p>return;<br>
}</p>
<p>void fn1()<br>
{<br>
printf( &#8220;next.\n&#8221; );<br>
}</p>
<p>void fn2()<br>
{<br>
printf( &#8220;executed &#8221; );<br>
}</p>
<p>void fn3()<br>
{<br>
printf( &#8220;is &#8221; );<br>
}</p>
<p>void fn4()<br>
{<br>
printf( &#8220;This &#8221; );<br>
}</p>
<p>结果：<br>
This is executed first.<br>
This is executed next.</p>
<br><img src ="http://www.cnitblog.com/wujian-IT/aggbug/34968.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/wujian-IT/" target="_blank">吴剑</a> 2007-10-16 21:57 <a href="http://www.cnitblog.com/wujian-IT/archive/2007/10/16/34968.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>另一个sinff抓包程序</title><link>http://www.cnitblog.com/wujian-IT/archive/2007/10/15/34907.html</link><dc:creator>吴剑</dc:creator><author>吴剑</author><pubDate>Mon, 15 Oct 2007 14:59:00 GMT</pubDate><guid>http://www.cnitblog.com/wujian-IT/archive/2007/10/15/34907.html</guid><wfw:comment>http://www.cnitblog.com/wujian-IT/comments/34907.html</wfw:comment><comments>http://www.cnitblog.com/wujian-IT/archive/2007/10/15/34907.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/wujian-IT/comments/commentRss/34907.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/wujian-IT/services/trackbacks/34907.html</trackback:ping><description><![CDATA[<table width="100%">
    <tbody>
        <tr>
            <td align="center"><strong><font size="3">linux下用socket的抓包程序</font></strong>
            </td>
        </tr>
        <tr>
            <td align="center">
            <br></td>
        </tr>
    </tbody>
</table>
<font class="b1">
<p>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;sys/socket.h&gt;&nbsp; <br>#include &lt;netinet/in.h&gt;&nbsp; <br>#include &lt;arpa/inet.h&gt;&nbsp;&nbsp; <br>#include &lt;netinet/ip.h&gt;&nbsp; <br>#include &lt;string.h&gt;&nbsp; <br>#include &lt;netdb.h&gt;&nbsp; <br>#include &lt;netinet/tcp.h&gt;&nbsp; <br>#include &lt;netinet/udp.h&gt; <br>#include &lt;stdlib.h&gt;&nbsp; <br>#include &lt;unistd.h&gt;&nbsp; <br>#include &lt;signal.h&gt;&nbsp; <br>#include &lt;net/if.h&gt;&nbsp; <br>#include &lt;sys/ioctl.h&gt;&nbsp; <br>#include &lt;sys/stat.h&gt;&nbsp; <br>#include &lt;fcntl.h&gt;&nbsp; <br>#include &lt;linux/if_ether.h&gt; <br>#include &lt;net/ethernet.h&gt;</p>
<p><br>void die(char *why, int n)&nbsp; <br>{&nbsp; <br>&nbsp; perror(why);&nbsp; <br>&nbsp; exit(n);&nbsp; <br>}&nbsp; </p>
<p>int do_promisc(char *nif, int sock )&nbsp; <br>{&nbsp; <br>struct ifreq ifr;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>strncpy(ifr.ifr_name, nif,strlen(nif)+1);&nbsp; <br>&nbsp;&nbsp; if((ioctl(sock, SIOCGIFFLAGS, &amp;ifr) == -1))&nbsp; //获得flag<br>&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp; die("ioctl", 2);&nbsp; <br>&nbsp;&nbsp; }&nbsp; <br>&nbsp;&nbsp; <br>&nbsp;&nbsp; ifr.ifr_flags |= IFF_PROMISC;&nbsp; //重置flag标志<br>&nbsp; <br>&nbsp;&nbsp; if(ioctl(sock, SIOCSIFFLAGS, &amp;ifr) == -1 )&nbsp; //改变模式<br>&nbsp;&nbsp; { <br>&nbsp;&nbsp;&nbsp;&nbsp; die("ioctl", 3);&nbsp; <br>&nbsp;&nbsp; }&nbsp; <br>}&nbsp; <br>//修改网卡成PROMISC(混杂)模式</p>
<p>char buf[40960];&nbsp; </p>
<p>main()&nbsp; <br>{&nbsp; <br>struct sockaddr_in addr; <br>struct ether_header *peth; <br>struct iphdr *pip;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>struct tcphdr *ptcp; <br>struct udphdr *pudp; </p>
<p>char mac[16];<br>int i,sock, r, len;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>char *data; <br>char *ptemp; <br>char ss[32],dd[32];</p>
<p>if((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)&nbsp; //建立socket<br>//man socket可以看到上面几个宏的意思<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; die("socket", 1);&nbsp; <br>}</p>
<p>do_promisc("eth0", sock);&nbsp;&nbsp;&nbsp; //eth0为网卡名称</p>
<p>system("ifconfig");</p>
<p>for(;;)&nbsp; <br>{&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp; len = sizeof(addr);&nbsp; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; r = recvfrom(sock,(char *)buf,sizeof(buf), 0, (struct sockaddr *)&amp;addr,&amp;len);&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp; //调试的时候可以增加一个输出r的语句判断是否抓到包<br>&nbsp;&nbsp;&nbsp;&nbsp; buf[r] = 0;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp; ptemp = buf; <br>&nbsp;&nbsp;&nbsp;&nbsp; peth = (struct ether_header *)ptemp; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; ptemp += sizeof(struct ether_header); //指针后移eth头的长度<br>&nbsp;&nbsp;&nbsp;&nbsp; pip = (struct ip *)ptemp; //pip指向ip层的包头</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; ptemp += sizeof(struct ip);//指针后移ip头的长度&nbsp; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; switch(pip-&gt;protocol)&nbsp;&nbsp; //根据不同协议判断指针类型<br>&nbsp;&nbsp;&nbsp;&nbsp; { <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case IPPROTO_TCP: <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptcp = (struct tcphdr *)ptemp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //ptcp指向tcp头部<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("TCP pkt :FORM:[%s]:[%d]\n",inet_ntoa(*(struct in_addr*)&amp;(pip-&gt;saddr)),ntohs(ptcp-&gt;source)); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("TCP pkt :TO:[%s]:[%d]\n",inet_ntoa(*(struct in_addr*)&amp;(pip-&gt;daddr)),ntohs(ptcp-&gt;dest));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case IPPROTO_UDP: <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pudp = (struct udphdr *)ptemp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //ptcp指向udp头部&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("UDP pkt:\n len:%d payload len:%d from %s:%d to %s:%d\n",&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r,&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ntohs(pudp-&gt;len), <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inet_ntoa(*(struct in_addr*)&amp;(pip-&gt;saddr)), <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ntohs(pudp-&gt;source), <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inet_ntoa(*(struct in_addr*)&amp;(pip-&gt;daddr)), <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ntohs(pudp-&gt;dest) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case&nbsp; IPPROTO_ICMP: <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("ICMP pkt:%s\n",inet_ntoa(*(struct in_addr*)&amp;(pip-&gt;saddr)));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case&nbsp; IPPROTO_IGMP: <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("IGMP pkt:\n"); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default: <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Unkown pkt, protocl:%d\n", pip-&gt;protocol); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break; <br>&nbsp;&nbsp;&nbsp; } //end switch</p>
<p>perror("dump");<br>&nbsp;} <br>&nbsp;<br>}</p>
<p>/*<br>[playmud@fc3 test]$ gcc -v<br>Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.2/specs<br>Configured
with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk
--host=i386-redhat-linux<br>Thread model: posix<br>gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)</p>
<p>************************eth的结构**************************************<br>struct ether_header<br>{<br>&nbsp; u_int8_t&nbsp; ether_dhost[ETH_ALEN];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // destination eth addr <br>&nbsp; u_int8_t&nbsp; ether_shost[ETH_ALEN];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // source ether addr&nbsp;&nbsp;&nbsp; <br>&nbsp; u_int16_t ether_type;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // packet type ID field <br>} __attribute__ ((__packed__));</p>
<p>***********************IP的结构***********************************<br>struct iphdr<br>&nbsp; {<br>#if __BYTE_ORDER == __LITTLE_ENDIAN<br>&nbsp;&nbsp;&nbsp; unsigned int ihl:4;<br>&nbsp;&nbsp;&nbsp; unsigned int version:4;<br>#elif __BYTE_ORDER == __BIG_ENDIAN<br>&nbsp;&nbsp;&nbsp; unsigned int version:4;<br>&nbsp;&nbsp;&nbsp; unsigned int ihl:4;<br>#else<br># error "Please fix &lt;bits/endian.h&gt;"<br>#endif<br>&nbsp;&nbsp;&nbsp; u_int8_t tos;<br>&nbsp;&nbsp;&nbsp; u_int16_t tot_len;<br>&nbsp;&nbsp;&nbsp; u_int16_t id;<br>&nbsp;&nbsp;&nbsp; u_int16_t frag_off;<br>&nbsp;&nbsp;&nbsp; u_int8_t ttl;<br>&nbsp;&nbsp;&nbsp; u_int8_t protocol;<br>&nbsp;&nbsp;&nbsp; u_int16_t check;<br>&nbsp;&nbsp;&nbsp; u_int32_t saddr;<br>&nbsp;&nbsp;&nbsp; u_int32_t daddr;<br>&nbsp; };</p>
<p>***********************TCP的结构****************************<br>struct tcphdr<br>&nbsp; {<br>&nbsp;&nbsp;&nbsp; u_int16_t source;<br>&nbsp;&nbsp;&nbsp; u_int16_t dest;<br>&nbsp;&nbsp;&nbsp; u_int32_t seq;<br>&nbsp;&nbsp;&nbsp; u_int32_t ack_seq;<br>#&nbsp; if __BYTE_ORDER == __LITTLE_ENDIAN<br>&nbsp;&nbsp;&nbsp; u_int16_t res1:4;<br>&nbsp;&nbsp;&nbsp; u_int16_t doff:4;<br>&nbsp;&nbsp;&nbsp; u_int16_t fin:1;<br>&nbsp;&nbsp;&nbsp; u_int16_t syn:1;<br>&nbsp;&nbsp;&nbsp; u_int16_t rst:1;<br>&nbsp;&nbsp;&nbsp; u_int16_t psh:1;<br>&nbsp;&nbsp;&nbsp; u_int16_t ack:1;<br>&nbsp;&nbsp;&nbsp; u_int16_t urg:1;<br>&nbsp;&nbsp;&nbsp; u_int16_t res2:2;<br>#&nbsp; elif __BYTE_ORDER == __BIG_ENDIAN<br>&nbsp;&nbsp;&nbsp; u_int16_t doff:4;<br>&nbsp;&nbsp;&nbsp; u_int16_t res1:4;<br>&nbsp;&nbsp;&nbsp; u_int16_t res2:2;<br>&nbsp;&nbsp;&nbsp; u_int16_t urg:1;<br>&nbsp;&nbsp;&nbsp; u_int16_t ack:1;<br>&nbsp;&nbsp;&nbsp; u_int16_t psh:1;<br>&nbsp;&nbsp;&nbsp; u_int16_t rst:1;<br>&nbsp;&nbsp;&nbsp; u_int16_t syn:1;<br>&nbsp;&nbsp;&nbsp; u_int16_t fin:1;<br>#&nbsp; else<br>#&nbsp;&nbsp; error "Adjust your &lt;bits/endian.h&gt; defines"<br>#&nbsp; endif<br>&nbsp;&nbsp;&nbsp; u_int16_t window;<br>&nbsp;&nbsp;&nbsp; u_int16_t check;<br>&nbsp;&nbsp;&nbsp; u_int16_t urg_ptr;<br>};<br>***********************UDP的结构*****************************<br>struct udphdr<br>{<br>&nbsp; u_int16_t source;<br>&nbsp; u_int16_t dest;<br>&nbsp; u_int16_t len;<br>&nbsp; u_int16_t check;<br>};</p>
<p>*************************************************************<br>*/</p>
</font>
<br><img src ="http://www.cnitblog.com/wujian-IT/aggbug/34907.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/wujian-IT/" target="_blank">吴剑</a> 2007-10-15 22:59 <a href="http://www.cnitblog.com/wujian-IT/archive/2007/10/15/34907.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>sniff抓包程序片段</title><link>http://www.cnitblog.com/wujian-IT/archive/2007/10/15/34906.html</link><dc:creator>吴剑</dc:creator><author>吴剑</author><pubDate>Mon, 15 Oct 2007 14:58:00 GMT</pubDate><guid>http://www.cnitblog.com/wujian-IT/archive/2007/10/15/34906.html</guid><wfw:comment>http://www.cnitblog.com/wujian-IT/comments/34906.html</wfw:comment><comments>http://www.cnitblog.com/wujian-IT/archive/2007/10/15/34906.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/wujian-IT/comments/commentRss/34906.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/wujian-IT/services/trackbacks/34906.html</trackback:ping><description><![CDATA[<br>#define INTERFACE "eth0"<br>#define MAX_SIZE 65535<br><br>int init_raw_socket();<br>int open_promisc(char *interface, int sockfd);<br><br>int main()<br>{<br>&nbsp;&nbsp; &nbsp;int sockfd;<br>&nbsp;&nbsp; &nbsp;int bytes_recv;<br>&nbsp;&nbsp; &nbsp;int addr_len;<br><br>&nbsp;&nbsp; &nbsp;char recv_buff[MAX_SIZE];<br>&nbsp;&nbsp; &nbsp;struct sockaddr_in from_addr;<br>&nbsp;&nbsp; &nbsp;struct ip *ip;<br>&nbsp;&nbsp; &nbsp;struct tcp *tcp;<br><br>&nbsp;&nbsp; &nbsp;sockfd = init_raw_socket();<br>&nbsp;&nbsp; &nbsp;open_promisc(INTERFACE, sockfd);<br><br>&nbsp;&nbsp; &nbsp;addr_len = sizeof(from_addr);<br><br>&nbsp;&nbsp; &nbsp;while (1)<br>&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bytes_recv = recvfrom(sockfd, recv_buff, MAX_SIZE - 1, 0,<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;(struct sockaddr_in*)&amp;from_addr, &amp;addr_len));<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (bytes_recv &lt; 0)<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;perror("recvfrom error");<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;exit(EXIT_FAILURE);<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("receive %d bytes from %s\n", bytes_recv, <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;inet_ntoa(from_addr.sin_addr));<br><br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ip = (struct ip*)recv_buff;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (ip-&gt;ip_protocol == 6)<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("IP header length ::: %d\n",ip-&gt;ip_length); &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("Protocol ::: %d\n",ip-&gt;ip_protocol); &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;tcp = (struct tcp *)(buffer + (4*ip-&gt;ip_length)); &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("Source port ::: %d\n",ntohs(tcp-&gt;tcp_source_port)); &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("Dest port ::: %d\n",ntohs(tcp-&gt;tcp_dest_port)); &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;return 0;<br>}<br><br>int init_raw_socket()<br>{<br>&nbsp;&nbsp; &nbsp;int sockfd;<br>&nbsp;&nbsp; &nbsp;sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);<br>&nbsp;&nbsp; &nbsp;if (sockfd &lt; 0)<br>&nbsp;&nbsp; &nbsp;{<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;perror("create socker error");<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;exit(EXIT_FAILURE);<br>&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp;&nbsp; return sockfd;<br>}<br><br>int open_promisc(char *interface, int sockfd)<br>{<br>&nbsp;&nbsp;&nbsp; struct ifreq ifr;<br>&nbsp;&nbsp;&nbsp; strncpy(ifr.ifr_name, interface, strlen(interface) + 1);<br>&nbsp;&nbsp;&nbsp; if (ioctl(sockfd, SIOCGIFFLAGS, &amp;ifr) == -1)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; perror("couldn't rettrive flags for the interface");<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; exit(EXIT_FAILURE);<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; ifr.ifr_flags |= IFF_PROMISC;<br>&nbsp;&nbsp;&nbsp; if (ioctl(sockfd, SIOCSIFFLAGS, &amp;ifr) == -1)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; perror("couldn't set the promisc flags");<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; exit(EXIT_FAILURE);<br>&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; printf("set interface: %s to promisc\n", interface);<br>&nbsp;&nbsp;&nbsp; return 0;<br>} <br><br><br>关于open_promisc打开网口混合模式<br><strong>#include &lt;<a href="file:///usr/include/sys/ioctl.h">sys/ioctl.h</a>&gt;</strong> <br><strong>#include &lt;<a href="file:///usr/include/net/if.h">net/if.h</a>&gt;</strong>
<p>Linux 支持 一些 配置 网络设备 的 标准 ioctl. 他们 用于 任意的 套接字 描述符, 而 无须 了解 其 类型 或 系列. 他们 传递 一个 <strong>ifreq </strong>结构:
</p>
<pre>struct ifreq<br>{<br>    char            ifr_name[IFNAMSIZ];   /* Interface name */<br>    union {<br>                    struct sockaddr       ifr_addr;<br>                    struct sockaddr       ifr_dstaddr;<br>                    struct sockaddr       ifr_broadaddr;<br>                    struct sockaddr       ifr_netmask;<br>                    struct sockaddr       ifr_hwaddr;<br>                    short                 ifr_flags;<br>                    int                   ifr_ifindex;<br>                    int                   ifr_metric;<br>                    int                   ifr_mtu;<br>                    struct ifmap          ifr_map;<br>                    char                  ifr_slave[IFNAMSIZ];<br>                    char                  ifr_newname[IFNAMSIZ];<br>                    char *                ifr_data;<br>    };<br>}<br><br>struct ifconf <br>{ <br>    int ifc_len;                          /* size of buffer */<br>    union {            <br>                    char *                ifc_buf; /* buffer address */ <br>                    struct ifreq *ifc_req; /* array of structures */<br>    };  <br>};     <br><br></pre>
<p>一般说来, ioctl 通过 把 <strong>ifr_name</strong> 设置为 接口 的 名字 来 指定 将要 操作 的 设备. 结构的 其他成员 可以 分享 内存.
</p>
<p><a name="lbAE"></a>
</p>
如果 某个 ioctl 标记为 特权操作, 那么 操作时 需要 有效uid 为 0, 或者 拥有 <strong>CAP_NET_ADMIN</strong> 能力. 否则 将 返回 <strong>EPERM .</strong>
<p>
</p>
<dl compact="compact"><dt><strong>SIOCGIFNAME</strong>
</dt><dd>给定 <strong>ifr_ifindex,</strong> 返回 <strong>ifr_name</strong> 中 的 接口名字. 这是 唯一 返回 <strong>ifr_name</strong> 内容 的 ioctl.
</dd><dt><strong>SIOCGIFINDEX</strong>
</dt><dd>把 接口 的 索引 存入 <strong>ifr_ifindex</strong>.
</dd><dt><strong>SIOCGIFFLAGS</strong>, <strong>SIOCSIFFLAGS</strong>
</dt><dd>读取 或 设置 设备的 活动标志字. <strong>ifr_flags</strong> 包含 下列值 的 屏蔽位:
<p>
<table>
    <tbody>
        <tr valign="top">
            <td colspan="2" align="center">设备标志<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_UP</td>
            <td>接口正在运行.<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_BROADCAST</td>
            <td>有效的广播地址集.<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_DEBUG</td>
            <td>内部调试标志.<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_LOOPBACK</td>
            <td>这是自环接口.<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_POINTOPOINT</td>
            <td>这是点到点的链路接口.<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_RUNNING</td>
            <td>资源已分配.<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_NOARP</td>
            <td>无arp协议, 没有设置第二层目的地址.<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_PROMISC</td>
            <td>接口为杂凑(promiscuous)模式.<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_NOTRAILERS</td>
            <td>避免使用trailer .<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_ALLMULTI</td>
            <td>接收所有组播(multicast)报文.<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_MASTER</td>
            <td>主负载平衡群(bundle).<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_SLAVE</td>
            <td>从负载平衡群(bundle).<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_MULTICAST</td>
            <td>支持组播(multicast).<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_PORTSEL</td>
            <td>可以通过ifmap选择介质(media)类型.<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_AUTOMEDIA</td>
            <td>自动选择介质.<br></td>
        </tr>
        <tr valign="top">
            <td>IFF_DYNAMIC</td>
            <td>接口关闭时丢弃地址.<br></td>
        </tr>
    </tbody>
</table>
</p>
<p>设置 活动标志字 是 特权操作, 但是 任何进程 都可以 读取 标志字.
</p>
</dd><dt><strong>SIOCGIFMETRIC</strong>, <strong>SIOCSIFMETRIC</strong>
</dt><dd>使用 <strong>ifr_metric</strong> 读取 或 设置 设备的 metric 值. 该功能 目前 还没有 实现. 读取操作 使 <strong>ifr_metric</strong> 置 0, 而 设置操作 则 返回 <strong>EOPNOTSUPP.</strong>
</dd><dt><strong>SIOCGIFMTU</strong>, <strong>SIOCSIFMTU</strong>
</dt><dd>使用 <strong>ifr_mtu</strong> 读取 或 设置 设备的 MTU(最大传输单元). 设置 MTU 是 特权操作. 过小的 MTU 可能 导致 内核 崩溃.
</dd><dt><strong>SIOCGIFHWADDR</strong>, <strong>SIOCSIFHWADDR</strong>
</dt><dd>使用 <strong>ifr_hwaddr</strong> 读取 或 设置 设备的 硬件地址. 设置 硬件地址 是 特权操作.
</dd><dt><strong>SIOCSIFHWBROADCAST</strong>
</dt><dd>使用 <strong>ifr_hwaddr</strong> 读取 或 设置 设备的 硬件广播地址. 这是个 特权操作.
</dd><dt><strong>SIOCGIFMAP</strong>, <strong>SIOCSIFMAP</strong>
</dt><dd>使用 <strong>ifr_map</strong> 读取 或 设置 接口的 硬件参数. 设置 这个参数 是 特权操作.
<pre>struct ifmap <br>{<br>    unsigned long   mem_start;<br>    unsigned long   mem_end;<br>    unsigned short  base_addr; <br>    unsigned char   irq;                  <br>    unsigned char   dma; <br>    unsigned char   port; <br>};<br><br></pre>
<p>对 ifmap 结构 的 解释 取决于 设备驱动程序 和 体系结构.
</p>
</dd><dt><strong>SIOCADDMULTI</strong>, <strong>SIOCDELMULTI</strong>
</dt><dd>使用 <strong>ifr_hwaddr</strong> 在 设备的 链路层 组播过滤器 (multicase filter) 中 添加 或 删除 地址. 这些是 特权操作.&nbsp;
</dd><dt><strong>SIOCGIFTXQLEN</strong>, <strong>SIOCSIFTXQLEN</strong>
</dt><dd>使用 <strong>ifr_qlen</strong> 读取 或 设置 设备的 传输队列长度. 设置 传输队列长度 是 特权操作.
</dd><dt><strong>SIOCSIFNAME</strong>
</dt><dd>把 <strong>ifr_ifindex</strong> 中 指定的 接口名字 改成 <strong>ifr_newname</strong>. 这是个 特权操作.
</dd><dt><strong>SIOCGIFCONF</strong>
</dt><dd>返回 接口地址(传输层) 列表. 出于 兼容性, 目前 只代表 AF_INET 地址. 用户 传送 一个 <strong>ifconf</strong> 结构 作为 ioctl 的 参数. 其中 <strong>ifc_req</strong> 包含 一个 指针 指向 <em>ifreq</em> 结构数组, 他的 长度 以字节 为单位 存放在 <strong>ifc_len </strong>中. 内核 用 所有 当前的 L3(第三层?) 接口地址 填充 ifreqs, 这些 接口 正在 运行: <em>ifr_name </em>存放 接口名字 (eth0:1等), <em>ifr_addr</em> 存放 地址. 内核 在 <em>ifc_len</em> 中 返回 实际长度; 如果 他 等于 初始长度, 表示 溢出了, 用户 应该 换一个 大些的 缓冲区 重试 一下. 没有 发生 错误时 ioctl 返回 0, 否则 返回 -1, 溢出 不算 错误.</dd></dl>
<br>相关结构体：<br><font style="color: #000000;" color="#cccccc">/*structure&nbsp;of&nbsp;an&nbsp;ip&nbsp;header*/　&nbsp;&nbsp;
<br>struct&nbsp;ip&nbsp;{&nbsp;　　&nbsp;&nbsp;
<br>unsigned&nbsp;int&nbsp;ip_length:4;&nbsp;/*little-endian*/　&nbsp;&nbsp;
<br>unsigned&nbsp;int&nbsp;ip_version:4;&nbsp;&nbsp;
<br>unsigned&nbsp;char&nbsp;ip_tos;　&nbsp;&nbsp;
<br>unsigned&nbsp;short&nbsp;ip_total_length;&nbsp;　&nbsp;&nbsp;
<br>unsigned&nbsp;short&nbsp;ip_id;&nbsp;　&nbsp;&nbsp;
<br>unsigned&nbsp;short&nbsp;ip_flags;&nbsp;&nbsp;
<br>unsigned&nbsp;char&nbsp;ip_ttl;&nbsp;&nbsp;
<br>unsigned&nbsp;char&nbsp;ip_protocol;&nbsp;&nbsp;
<br>unsigned&nbsp;short&nbsp;ip_cksum;&nbsp;&nbsp;
<br>unsigned&nbsp;int&nbsp;ip_source;&nbsp;unsigned&nbsp;int&nbsp;ip_dest;&nbsp;　&nbsp;&nbsp;
<br>};&nbsp;&nbsp;
<br>&nbsp;&nbsp;&nbsp;&nbsp;
<br>/*&nbsp;Structure&nbsp;of&nbsp;a&nbsp;TCP&nbsp;header&nbsp;*/&nbsp;&nbsp;
<br>struct&nbsp;tcp&nbsp;{&nbsp;&nbsp;
<br>unsigned&nbsp;short&nbsp;tcp_source_port;&nbsp;&nbsp;
<br>unsigned&nbsp;short&nbsp;tcp_dest_port;&nbsp;&nbsp;
<br>unsigned&nbsp;int&nbsp;tcp_seqno;&nbsp;　&nbsp;&nbsp;
<br>unsigned&nbsp;int&nbsp;tcp_ackno;&nbsp;&nbsp;
<br>unsigned&nbsp;int&nbsp;tcp_res1:4,&nbsp;/*little-endian*/&nbsp;&nbsp;
<br>tcp_hlen:4,&nbsp;&nbsp;
<br>tcp_fin:1,&nbsp;&nbsp;
<br>tcp_syn:1,&nbsp;&nbsp;
<br>tcp_rst:1,&nbsp;&nbsp;
<br>tcp_psh:1,&nbsp;&nbsp;
<br>tcp_ack:1,&nbsp;&nbsp;
<br>tcp_urg:1,&nbsp;&nbsp;
<br>tcp_res2:2;&nbsp;&nbsp;
<br>unsigned&nbsp;short&nbsp;tcp_winsize;&nbsp;&nbsp;
<br>unsigned&nbsp;short&nbsp;tcp_cksum;&nbsp;&nbsp;
<br>unsigned&nbsp;short&nbsp;tcp_urgent;&nbsp;&nbsp;
<br>};&nbsp;&nbsp;
<br>/*********************EOF***********************************/&nbsp;&nbsp;
<br>
</font>
<br><img src ="http://www.cnitblog.com/wujian-IT/aggbug/34906.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/wujian-IT/" target="_blank">吴剑</a> 2007-10-15 22:58 <a href="http://www.cnitblog.com/wujian-IT/archive/2007/10/15/34906.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>