免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5060 | 回复: 6
打印 上一主题 下一主题

libnids 库调用出错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-03 23:57 |只看该作者 |倒序浏览
libnids库调用出错在网上找了baidu,google都搜了,都没有找到相应的解决办法!
希望高手们指点指点。
环境如下:
系统:fedora core 6
第三方库已经安装成功,libnids, libpcap, libnet
代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "nids.h"
  5. typedef struct in_addr in_addr; //用于存储TCP 源/目的端口
  6. typedef struct half_stream tcp_half_content; //用于存储 TCP一端的数据信息
  7. char ascii_string[1000];
  8. /*
  9.                 转换字符为asc码
  10. */
  11. char *char_to_ascii(char ch)
  12. {
  13.         char *string;
  14.         ascii_string[0] = 0;
  15.         string = ascii_string;
  16.         if(isgraph(ch))
  17.         {
  18.                 *string++ = ch;
  19.         }       
  20.         else if(ch == ' ')
  21.         {
  22.                 *string++ = ch;
  23.         }
  24.         else if(ch == '\n' || ch == '\r')
  25.         {
  26.                 *string++ = ch;
  27.         }
  28.         else
  29.         {
  30.                 *string++ = '.';
  31.         }
  32.         *string = 0;
  33.         return ascii_string;
  34. }
  35. /*
  36. *         TCP 回调函数,来分析TCP 的连接状态
  37. */
  38. void tcp_protocol_callback(struct tcp_stream *tcp_conn, void **arg)
  39. {

  40.         char address_buf[128];
  41.         char content[65535];
  42.         int i = 0;
  43.         /*
  44.          *         获取TCP连接的目的地址于原地址
  45.          */
  46.         struct tuple4 tuple = tcp_conn->addr;
  47.         strcpy(address_buf, (char *)inet_ntoa(tuple.saddr));
  48.         sprintf(address_buf + strlen(address_buf), ":%i", tuple.source);
  49.         strcat(address_buf, "------>");
  50.         strcat(address_buf, (char *)inet_ntoa(tuple.daddr));
  51.         sprintf(address_buf + strlen(address_buf), ":%i", tuple.daddr);
  52.         strcat(address_buf, "\n");
  53.         /*
  54.          * 判断TCP 的连接状态
  55.          */
  56.         switch(tcp_conn->nids_state)
  57.         {
  58.                 case NIDS_JUST_EST:  // 表示客户端与TCP服务器建立连接状态
  59.                         tcp_conn->client.collect++;
  60.                         tcp_conn->server.collect++;
  61.                         tcp_conn->client.collect_urg++;
  62.                         tcp_conn->server.collect_urg++;
  63.                         printf("%s 建立连接\n", address_buf);
  64.                         break;
  65.                 case NIDS_CLOSE: // 表示TCP连接关闭
  66.                         printf("-------------------------------\n");
  67.                         printf("%s 连接关闭\n", address_buf);
  68.                         break;
  69.                 case NIDS_RESET: //表示TCP 连接被RST 关闭
  70.                         printf("-------------------------------\n");
  71.                         printf("%s 连接被RST关闭\n", address_buf);
  72.                         break;
  73.                 case NIDS_DATA: // 表示有信的数据到达
  74.                         {
  75.                                 tcp_half_content *tcp_half_con;
  76.                                 /*
  77.                                  *         表示客户端有新的紧急数据到达
  78.                                  */
  79.                                 if(tcp_conn->client.count_new_urg)
  80.                                 {
  81.                                         printf("-------------------------------\n");
  82.                                         printf("----------client urgent------------\n");
  83.                                         address_buf[strlen(address_buf) + 1] = 0;
  84.                                         address_buf[strlen(address_buf)] = tcp_conn->client.urgdata;
  85.                                         printf("%s \n", address_buf);
  86.                                         break;       
  87.                                 }       
  88.                                 /*
  89.                                  *         表示服务端有新的紧急数据到达
  90.                                  */
  91.                                 if(tcp_conn->server.count_new_urg)
  92.                                 {
  93.                                         printf("-------------------------------\n");
  94.                                         printf("----------server urgent------------\n");
  95.                                         address_buf[strlen(address_buf) + 1] = 0;
  96.                                         address_buf[strlen(address_buf)] = tcp_conn->server.urgdata;
  97.                                         printf("%s \n", address_buf);
  98.                                         break;       
  99.                                 }
  100.                                 /*
  101.                                  *         表示客户端有新的数据到达
  102.                                  */
  103.                                 if(tcp_conn->client.count_new)
  104.                                 {
  105.                                         tcp_half_con = &tcp_conn->client;
  106.                                         printf("-------------------------------\n");
  107.                                         printf("----------client---------------\n");
  108.                                         printf("%s \n", address_buf);
  109.                                         memcpy(content,tcp_half_con->data, tcp_half_con->count_new);
  110.                                         content[tcp_half_con->count_new] = '\0';
  111.                                         /*
  112.                                          * 打印客户端接收的数据
  113.                                          */
  114.                                         for(i = 0; i < tcp_half_con->count_new; i++)
  115.                                         {
  116.                                                 printf("%s", char_to_ascii(content[i]));
  117.                                         }
  118.                                         printf("\n");
  119.                                         break;       
  120.                                 }
  121.                                 /*
  122.                                  *         表示服务端有新的数据到达
  123.                                  */
  124.                                 if(tcp_conn->server.count_new)
  125.                                 {
  126.                                         tcp_half_con = &tcp_conn->server;
  127.                                         printf("-------------------------------\n");
  128.                                         printf("----------server---------------\n");
  129.                                         printf("%s \n", address_buf);
  130.                                         memcpy(content,tcp_half_con->data, tcp_half_con->count_new);
  131.                                         content[tcp_half_con->count_new] = '\0';
  132.                                         /*
  133.                                          * 打印服务器端接收的数据
  134.                                          */
  135.                                         for(i = 0; i < tcp_half_con->count_new; i++)
  136.                                         {
  137.                                                 printf("%s", char_to_ascii(content[i]));
  138.                                         }
  139.                                         printf("\n");
  140.                                         break;       
  141.                                 }
  142.                                 break;
  143.                         }
  144.                 default:
  145.                                 break;
  146.         }
  147. }
  148. int main(void)
  149. {
  150.         if(!nids_init())//对libnids初始化
  151.         {
  152.                 printf("初始化错误\n");
  153.                 exit(1);
  154.         }
  155.         nids_register_tcp(tcp_protocol_callback);//注册一个TCP连接的回掉函数
  156.         nids_run();//运行Libnids,进入循环捕获数据包状态
  157.        
  158. }
复制代码


编译后出现如下错误:


  1. [root@localhost snort]# gcc -o protocolor protocolor_parse.c -lnids -lpcap -lnet
  2. /usr/local/lib/libnids.a(libnids.o): In function `nids_dispatch':
  3. /home/xihua/tools/libnids-1.22/src/libnids.c:759: undefined reference to `g_async_queue_push'
  4. /home/xihua/tools/libnids-1.22/src/libnids.c:753: undefined reference to `g_thread_create_full'
  5. /usr/local/lib/libnids.a(libnids.o): In function `cap_queue_process_thread':
  6. /home/xihua/tools/libnids-1.22/src/libnids.c:560: undefined reference to `g_async_queue_pop'
  7. /home/xihua/tools/libnids-1.22/src/libnids.c:566: undefined reference to `g_thread_exit'
  8. /usr/local/lib/libnids.a(libnids.o): In function `nids_exit':
  9. /home/xihua/tools/libnids-1.22/src/libnids.c:701: undefined reference to `g_async_queue_length'
  10. /usr/local/lib/libnids.a(libnids.o): In function `nids_run':
  11. /home/xihua/tools/libnids-1.22/src/libnids.c:682: undefined reference to `g_thread_create_full'
  12. /home/xihua/tools/libnids-1.22/src/libnids.c:685: undefined reference to `g_async_queue_push'
  13. /usr/local/lib/libnids.a(libnids.o): In function `nids_init':
  14. /home/xihua/tools/libnids-1.22/src/libnids.c:665: undefined reference to `g_thread_init'
  15. /home/xihua/tools/libnids-1.22/src/libnids.c:666: undefined reference to `g_async_queue_new'
  16. /usr/local/lib/libnids.a(libnids.o): In function `nids_pcap_handler':
  17. /home/xihua/tools/libnids-1.22/src/libnids.c:335: undefined reference to `g_async_queue_lock'
  18. /home/xihua/tools/libnids-1.22/src/libnids.c:337: undefined reference to `g_async_queue_length_unlocked'
  19. /home/xihua/tools/libnids-1.22/src/libnids.c:343: undefined reference to `g_async_queue_push_unlocked'
  20. /usr/local/lib/libnids.a(libnids.o): In function `nids_next':
  21. /home/xihua/tools/libnids-1.22/src/libnids.c:739: undefined reference to `g_thread_create_full'
  22. /home/xihua/tools/libnids-1.22/src/libnids.c:741: undefined reference to `g_async_queue_push'
  23. /usr/local/lib/libnids.a(libnids.o): In function `nids_pcap_handler':
  24. /home/xihua/tools/libnids-1.22/src/libnids.c:345: undefined reference to `g_async_queue_unlock'
  25. collect2: ld 返回 1
复制代码

我是libnids 初学者!
高手大侠们,指点指点!

论坛徽章:
0
2 [报告]
发表于 2007-09-04 14:53 |只看该作者
自己顶一下!
指点指点把!

论坛徽章:
0
3 [报告]
发表于 2007-09-04 20:31 |只看该作者
up

论坛徽章:
0
4 [报告]
发表于 2007-09-04 20:42 |只看该作者
高手如云!
没人知道吗?

论坛徽章:
0
5 [报告]
发表于 2007-09-04 23:29 |只看该作者
恩!
自己解决了!
呵呵!
原来是安装libnids版本太高了!原来安装的是1.22现在改成是1.20就没问题了!
呵呵!

论坛徽章:
0
6 [报告]
发表于 2009-03-28 16:20 |只看该作者
libnids 1.22以后使用了,glib2库中的gthread-2.0来实现多线程提高效率,因此在编译除了
-lnids -lpcap -lnet 以外还要加上 -lthread-2.0

论坛徽章:
0
7 [报告]
发表于 2009-03-28 16:23 |只看该作者
不好意思,是 -lgthread-2.0
当然因该先安装glib2.x的rpm包和相关的devel和head包
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP