免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: send_linux
打印 上一主题 下一主题

迎接ChinaUnix九周年庆技术实践之二----C/C++编程大赛!-结果公布! [复制链接]

论坛徽章:
0
71 [报告]
发表于 2010-12-02 08:47 |只看该作者
我也占个位先

论坛徽章:
0
72 [报告]
发表于 2010-12-02 09:27 |只看该作者
回复  surpass_li


    第一贴上面有阿,发邮件!!
rubylc_unix 发表于 2010-12-01 12:38



    代码已经发给您了

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
73 [报告]
发表于 2010-12-02 14:00 |只看该作者
第一题难度也不高。已基本完成。晚上提交。

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
74 [报告]
发表于 2010-12-02 21:48 |只看该作者
提交了第一题答案,在cygwin环境下通过。
不知道在其他环境下会不会有问题啊。

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
75 [报告]
发表于 2010-12-03 09:38 |只看该作者
二四题的难度太高了。都没找到参考资料。难哦。

论坛徽章:
0
76 [报告]
发表于 2010-12-03 10:06 |只看该作者
可惜不太懂PHP

论坛徽章:
0
77 [报告]
发表于 2010-12-03 14:01 |只看该作者
努力查找资料 域名取IP 做出来了,晚上提交代码:)

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
78 [报告]
发表于 2010-12-03 14:22 |只看该作者
回复 77# surpass_li


    厉害。哥昨天就提交代码了。你比我慢一步。

论坛徽章:
0
79 [报告]
发表于 2010-12-03 14:38 |只看该作者
看起来不难,做起来,想做好还需要费一番脑子。

论坛徽章:
0
80 [报告]
发表于 2010-12-03 16:42 |只看该作者
本帖最后由 heut2009 于 2010-12-03 16:45 编辑

第二题
写出个有bug的半成品,望各位给指点指点,给点意见,我好想拿个奖啊。


  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<sys/socket.h>
  4. #include<netinet/in.h>
  5. #include<linux/if_ether.h>
  6. #include<signal.h>
  7. #include<sys/types.h>
  8. #include<linux/tcp.h>
  9.   #include<linux/udp.h>
  10. #include<linux/ip.h>
  11. #include<unistd.h>
  12. #include<signal.h>
  13. #include<pthread.h>
  14. #include<malloc.h>

  15. #define STREAMSIZE 10000
  16. #define BUFSIZE 2048
  17. #define CACHESIZE 20
  18. /*stream item info struct*/
  19. typedef struct {
  20.         unsigned short type;/*tcp or udp type */
  21.         unsigned int sport;
  22.         unsigned int dport;
  23.         unsigned int sip;
  24.         unsigned int dip;
  25.         unsigned long count;
  26.         unsigned long bytes;/*total bytes of a stream*/
  27. }stream;

  28. /*stream temp cache info struct*/
  29. typedef struct {
  30.         unsigned int type;
  31.         unsigned int weight;
  32.         unsigned int sport;
  33.         unsigned int dport;
  34.         unsigned int sip;
  35.         unsigned int dip;
  36. }cache_info;

  37. cache_info cache_item[CACHESIZE];     /*streams last captured to make a queue*/
  38. stream *item[STREAMSIZE];                 /*items are for stream info*/
  39. int capture=1;
  40. unsigned int item_max=0;                 /* current total items*/
  41. unsigned int proto,sip,dip,sport,dport,ip_h_len,n_read=0;

  42. void report()
  43. {
  44. int i=0,j;
  45. char pro_type[3];
  46. printf("    protocol\tsrc\t\tdest\t    sport  dport  packets\ttotal bytes\n");
  47. for(i=1;i<=item_max;i++)
  48. {
  49. printf("\t%d\t%s\t%s  %d   %d   %d\t\t%d\n",item[i]->type,inet_ntoa(item[i]->sip),inet_ntoa(item[i]->dip),ntohs(item[i]->sport),ntohs(item[i]->dport),item[i]->count,item[i]->bytes);
  50. }


  51. }
  52. void sig_handle(int signo)
  53. {
  54. capture=0;
  55. report();
  56. exit(1);
  57. }


  58. /*function for caching items that did not find in cache queue*/
  59. void insert_cache_item()
  60. {
  61. printf("insert cache_item");
  62. int i,id=11;
  63. for(i=0;i<CACHESIZE;i++)
  64. {
  65. id=(id < cache_item[i].weight)? id : (cache_item[i].weight);
  66. if(cache_item[i].weight==0)
  67. {
  68. cache_item[i].type=proto;
  69. printf(" %d\n",i);
  70. cache_item[i].sport=sport;
  71. cache_item[i].dport=dport;
  72. cache_item[i].sip=sip;
  73. cache_item[i].dip=dip;
  74. cache_item[i].weight+=2;
  75. break;
  76. }
  77. }
  78. if(id>0)                   /*in the cache_item,all weight is great than 0,but we
  79.                            find the smallest weight is id,so cache_item[id]will be
  80.                             recoverd by the new stream*/
  81. {
  82. printf(" %d\n",id);
  83. cache_item[id].type=proto;
  84. cache_item[id].sport=sport;
  85. cache_item[id].dport=dport;
  86. cache_item[id].sip=sip;
  87. cache_item[id].dip=dip;
  88. cache_item[id].weight=+1;
  89. }
  90. }

  91. /*search the stream current captured in our cache_item.
  92. the cache_item contains an item "weight" to record
  93. how offen this stream being captured recently.when capture
  94. a stream,first check it in cache_item,if find a match one,good,we
  95. need not to search the big item array which contains all
  96. stream items we captured before.

  97. the algorithm of the weight is sample,weight is bettween 0-10,
  98. when first insert into cache_item or the current captured stream
  99. has been in cache_item,we add 2 to the stream weight.and at the
  100. same time sub 1 to other stream item's weight.when the cache_item
  101. is full,the item which has the smallest weight will be coverd.and
  102. this is done by the insert_cache_item function*/

  103. int search_cache_item()
  104. {
  105. int i,j=-1;
  106. for(i=0;i<CACHESIZE;++i)
  107. {
  108. if(cache_item[i].type=proto&&cache_item[i].sport==sport&&cache_item[i].dport==dport&&cache_item[i].sip==sip&&cache_item[i].dip==dip)
  109. {
  110. if(cache_item[i].weight<=10)
  111. {
  112. cache_item[i].weight+=2;
  113. }
  114. j=i;
  115. }/*we hava find one that match the current stream*/
  116. else
  117. {
  118. if(cache_item[i].weight>=1)
  119. cache_item[i].weight-=1;
  120. }
  121. }
  122. printf("search_cache_item result is %d\n",j);
  123. return j;
  124. }
  125. /*function to search the stream current captured
  126. in the item[] which contains all the streams captured
  127. before.*/
  128. int search_item()
  129. {
  130. printf(" search in the item \n");
  131. int i=0,j;
  132. for(i=0;i<item_max;i++)
  133. {
  134. if(item[i]->sip==sip&&item[i]->sport==sport&&item[i]->dport==dport&&item[i]->type==proto&&item[i]->dip==dip)
  135. {
  136. item[i]->count+=1;
  137. item[i]->bytes+=n_read;
  138. break;
  139. }
  140. }
  141. j=(i<item_max)? 1:-1;
  142. printf("search in the item result is %d\n",j);
  143. return j;
  144. }
  145. /*function to add the current stream captured to
  146. the stream struct list*/
  147. int add_to_item()
  148. {
  149. if(item_max<STREAMSIZE)
  150. {
  151. printf("item_max is %d\n",item_max);
  152. item[item_max]=(stream *)(malloc(sizeof(stream)));
  153. memset(item[item_max],0,sizeof(stream));
  154. item[item_max]->type=proto;
  155. item[item_max]->sip=sip;
  156. item[item_max]->dip=dip;
  157. item[item_max]->sport=sport;
  158. item[item_max]->dport=dport;
  159. item[item_max]->bytes+=n_read;
  160. item[item_max]->count=+1;
  161. item_max=item_max+1;
  162. }
  163. else
  164. {
  165. fprintf(stderr,"stream is to large please change the STREAMSIZE\n");
  166. exit(1);
  167. }
  168. }

  169. void main(int argc,char *argv[])
  170. {
  171. int i,ret,id,sockfd;
  172. char buf[BUFSIZE];
  173. struct tcphdr *tcp_head;
  174. struct udphdr *udp_head;
  175. struct iphdr *ip_head;
  176. char *p;

  177. for(i=0;i<CACHESIZE;i++)
  178. {
  179. cache_item[i].weight=0;
  180. }
  181. signal(SIGINT,sig_handle);
  182. if((sockfd = socket(PF_PACKET,SOCK_RAW,htons(ETH_P_IP)))<0)
  183. {
  184.         fprintf(stderr,"socket error\n");
  185.         printf("%d","sockfd");
  186.         exit(1);
  187. }

  188. while(capture)
  189. {
  190. n_read = recvfrom(sockfd,buf,BUFSIZE,0,NULL,NULL);
  191. ip_head = (struct iphdr *)(buf+14);
  192. /*14=6 source mac address
  193.      6 destnation mac address
  194.      2 type
  195. */
  196. proto=ip_head->protocol;
  197. sip=ip_head->saddr;
  198. dip=ip_head->daddr;
  199. ip_h_len=ip_head->ihl<<2;
  200. if(proto==IPPROTO_TCP)
  201. {
  202. tcp_head = (struct tcphdr *)(buf+14+ip_h_len);
  203. sport=tcp_head->source;
  204. dport=tcp_head->dest;
  205. if((id=search_cache_item())>=0)/*current stream is in cache*/
  206. {
  207. item[id]->count+=1;
  208. item[id]->bytes+=n_read;
  209. printf("use cache info\n");
  210. }
  211. else
  212. {
  213. insert_cache_item(); /*cache it */
  214. if((ret=search_item())<0)
  215. {
  216. add_to_item();
  217. }
  218. }
  219. }
  220. else if(proto==IPPROTO_UDP)
  221. {
  222. udp_head=(struct udphdr *)(buf+14+ip_h_len);
  223. sport=udp_head->source;
  224. dport=udp_head->dest;
  225. if(id=search_cache_item()>0)
  226. {
  227. item[id]->count+=1;
  228. item[id]->bytes+=n_read;
  229. }
  230. else
  231. {
  232. insert_cache_item();
  233. if(search_item()<0)
  234. add_to_item();
  235. }
  236. }
  237. }
  238. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP