免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2635 | 回复: 3

[C] cu 编程大赛第二题,求人分析bug啊 [复制链接]

论坛徽章:
0
发表于 2010-12-21 22:13 |显示全部楼层
本帖最后由 heut2009 于 2010-12-21 22:14 编辑

cu 编程大赛第二题, 自己尝试写了一个功能实现版的,第一次写这样的程序,没有经验,一直有段错误啊,希望牛人能给分析一下,给点意见,
  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. [root@bogon ~]# head -20 cu.c
  12. #include<stdio.h>
  13. #include<stdlib.h>
  14. #include<sys/socket.h>
  15. #include<netinet/in.h>
  16. #include<linux/if_ether.h>
  17. #include<signal.h>
  18. #include<sys/types.h>
  19. #include<linux/tcp.h>
  20. #include<linux/udp.h>
  21. #include<linux/ip.h>
  22. #include<unistd.h>
  23. #include<signal.h>
  24. #include<malloc.h>

  25. #define STREAMSIZE 10000
  26. #define BUFSIZE 2048
  27. #define CACHESIZE 20
  28. /*stream item info struct*/
  29. typedef struct {
  30.         unsigned short type;/*tcp or udp type */
  31.         unsigned int sport;
  32.         unsigned int dport;
  33.         unsigned int sip;
  34.         unsigned int dip;
  35.         unsigned long count;
  36.         unsigned long bytes;/*total bytes of a stream*/
  37. }stream;

  38. /*stream temp cache info struct*/
  39. typedef struct {
  40.         unsigned int type;
  41.         unsigned int weight;
  42.         unsigned int sport;
  43.         unsigned int dport;
  44.         unsigned int sip;
  45.         unsigned int dip;
  46. }cache_info;

  47. cache_info cache_item[CACHESIZE];/*streams last captured to make a queue*/
  48. stream *item[STREAMSIZE]; /*items are for stream info*/
  49. char interface[4];
  50. int capture=1;
  51. unsigned int item_max=0;/* current total items*/
  52. unsigned int proto,sip,dip,sport,dport,ip_h_len,n_read=0;

  53. void report()
  54. {
  55. int i=0,j;
  56. printf("    protocol\tsrc\t\tdest\t    sport  dport  packets\ttotal bytes\n");
  57. for(i=1;i<=item_max;i++)
  58. {
  59. 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);
  60. free(item[i]);
  61. }


  62. }
  63. void sig_handle(int signo)
  64. {
  65. capture=0;
  66. report();
  67. exit(1);
  68. }


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

  102. /*search the stream current captured in our cache_item.
  103. the cache_item contains an item "weight" to record
  104. how offen this stream being captured recently.when capture
  105. a stream,first check it in cache_item,if find a match one,good,we
  106. need not to search the big item array which contains all
  107. stream items we captured before.

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

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

  180. int main(int argc,char *argv[])
  181. {
  182. int i,ret,id,sockfd;
  183. char buf[BUFSIZE];
  184. struct tcphdr *tcp_head;
  185. struct udphdr *udp_head;
  186. struct iphdr *ip_head;
  187. char *p;

  188. for(i=0;i<CACHESIZE;i++)
  189. {
  190. cache_item[i].weight=0;
  191. }
  192. signal(SIGINT,sig_handle);
  193. if((sockfd = socket(PF_PACKET,SOCK_RAW,htons(ETH_P_IP)))<0)
  194. {
  195.         fprintf(stderr,"socket error\n");
  196.         printf("%d","sockfd");
  197.         exit(1);
  198. }

  199. while(capture)
  200. {
  201. n_read = recvfrom(sockfd,buf,BUFSIZE,0,NULL,NULL);
  202. ip_head = (struct iphdr *)(buf+14);
  203. /*14=6 source mac address
  204.      6 destnation mac address
  205.      2 type
  206. */
  207. proto=ip_head->protocol;
  208. sip=ip_head->saddr;
  209. dip=ip_head->daddr;
  210. ip_h_len=ip_head->ihl<<2;
  211. if(proto==IPPROTO_TCP)
  212. {
  213. tcp_head = (struct tcphdr *)(buf+14+ip_h_len);
  214. sport=tcp_head->source;
  215. dport=tcp_head->dest;
  216. if((id=search_cache_item())>=0)/*current stream is in cache*/
  217. {
  218. item[id]->count+=1;
  219. item[id]->bytes+=n_read;
  220. printf("use cache info\n");
  221. }
  222. else
  223. {
  224. insert_cache_item(); /*cache it */
  225. if((ret=search_item())<0)
  226. {
  227. add_to_item();
  228. }
  229. }
  230. }
  231. else if(proto==IPPROTO_UDP)
  232. {
  233. udp_head=(struct udphdr *)(buf+14+ip_h_len);
  234. sport=udp_head->source;
  235. dport=udp_head->dest;
  236. if(id=search_cache_item()>0)
  237. {
  238. item[id]->count+=1;
  239. item[id]->bytes+=n_read;
  240. }
  241. else
  242. {
  243. insert_cache_item();
  244. if(search_item()<0)
  245. add_to_item();
  246. }
  247. }
  248. }
  249. return 0;
  250. }
复制代码

论坛徽章:
0
发表于 2010-12-22 00:46 |显示全部楼层
还是放到gist.github吧,格式全丢了。

论坛徽章:
0
发表于 2010-12-22 11:11 |显示全部楼层
这个是什么程序?实现什么功能?

论坛徽章:
0
发表于 2010-12-22 14:05 |显示全部楼层
回复 3# sumland

题目是这个 :开发一个报文处理程序,捕获指定网口接收的数据包,统计每个会话(五元组,sip,dip,sport,dport,protocol)的流量:字节数,报文数。测试目标,以处理性能和统计精确度为准。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP