免费注册 查看新帖 |

Chinaunix

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

[问]关于网络安全开发包libnids的问题:编译的时候为什么要连接库:libgthread-2.0.so [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-17 16:31 |只看该作者 |倒序浏览
本帖最后由 bjtulq 于 2011-03-17 16:32 编辑

在编译的时候为什么要连接库:libgthread-2.0.so?能否讲一下libgthread-2.0.so的作用啊?
gcc -o show_tcp_connection tcp.c -lpcap -lnet -lnids -lgthread-2.0;不加-lgthread不能编译成功啊!
谢谢了!
  1. #include "nids.h"
  2. /* Libnids的头文件,必须包含 */
  3. char ascii_string[10000];
  4. char *char_to_ascii(char ch)
  5. /* 此函数的功能主要用于把协议数据进行显示 */
  6. {
  7.     char *string;
  8.     ascii_string[0] = 0;
  9.     string = ascii_string;
  10.     if (isgraph(ch))
  11.      /* 可打印字符 */
  12.     {
  13.         *string++ = ch;
  14.     }
  15.     else if (ch == ' ')
  16.      /* 空格 */
  17.     {
  18.         *string++ = ch;
  19.     }
  20.     else if (ch == '\n' || ch == '\r')
  21.      /* 回车和换行 */
  22.     {
  23.         *string++ = ch;
  24.     }
  25.     else
  26.      /* 其它字符以点"."表示 */
  27.     {
  28.         *string++ = '.';
  29.     }
  30.     *string = 0;
  31.     return ascii_string;
  32. }
  33. /*
  34. =======================================================================================================================
  35. 下面的函数是回调函数,用于分析TCP连接,分析TCP连接状态,对TCP协议传输的数据进行分析
  36. =======================================================================================================================
  37. */
  38. void tcp_protocol_callback(struct tcp_stream *tcp_connection, void **arg)
  39. {
  40.     int i;
  41.     char address_string[1024];
  42.     char content[65535];
  43.     char content_urgent[65535];
  44.     struct tuple4 ip_and_port = tcp_connection->addr;
  45.     /* 获取TCP连接的地址和端口对 */
  46.     strcpy(address_string, inet_ntoa(*((struct in_addr*) &(ip_and_port.saddr))));
  47.     /* 获取源地址 */
  48.     sprintf(address_string + strlen(address_string), " : %i", ip_and_port.source);
  49.     /* 获取源端口 */
  50.     strcat(address_string, " <---> ");
  51.     strcat(address_string, inet_ntoa(*((struct in_addr*) &(ip_and_port.daddr))));
  52.     /* 获取目的地址 */
  53.     sprintf(address_string + strlen(address_string), " : %i", ip_and_port.dest);
  54.     /* 获取目的端口 */
  55.     strcat(address_string, "\n");
  56.     switch (tcp_connection->nids_state) /* 判断LIBNIDS的状态 */
  57.     {
  58.         case NIDS_JUST_EST:
  59.             /* 表示TCP客户端和TCP服务器端建立连接状态 */
  60.             tcp_connection->client.collect++;
  61.             /* 客户端接收数据 */
  62.             tcp_connection->server.collect++;
  63.             /* 服务器接收数据 */
  64.             tcp_connection->server.collect_urg++;
  65.             /* 服务器接收紧急数据 */
  66.             tcp_connection->client.collect_urg++;
  67.             /* 客户端接收紧急数据 */
  68.             printf("%sTCP连接建立\n", address_string);
  69.             return ;
  70.         case NIDS_CLOSE:
  71.             /* 表示TCP连接正常关闭 */
  72.             printf("--------------------------------\n");
  73.             printf("%sTCP连接正常关闭\n", address_string);
  74.             return ;
  75.         case NIDS_RESET:
  76.             /* 表示TCP连接被RST关闭 */
  77.             printf("--------------------------------\n");
  78.             printf("%sTCP连接被RST关闭\n", address_string);
  79.             return ;
  80.         case NIDS_DATA:
  81.             /* 表示有新的数据到达 */
  82.             {
  83.                 struct half_stream *hlf;
  84.                 /* 表示TCP连接的一端的信息,可以是客户端,也可以是服务器端 */
  85.                 if (tcp_connection->server.count_new_urg)
  86.                 {
  87.                     /* 表示TCP服务器端接收到新的紧急数据 */
  88.                     printf("--------------------------------\n");
  89.                     strcpy(address_string, inet_ntoa(*((struct in_addr*) &(ip_and_port.saddr))));
  90.                     sprintf(address_string + strlen(address_string), " : %i", ip_and_port.source);
  91.                     strcat(address_string, " urgent---> ");
  92.                     strcat(address_string, inet_ntoa(*((struct in_addr*) &(ip_and_port.daddr))));
  93.                     sprintf(address_string + strlen(address_string), " : %i", ip_and_port.dest);
  94.                     strcat(address_string, "\n");
  95.                     address_string[strlen(address_string) + 1] = 0;
  96.                     address_string[strlen(address_string)] = tcp_connection->server.urgdata;
  97.                     printf("%s", address_string);
  98.                     return ;
  99.                 }
  100.                 if (tcp_connection->client.count_new_urg)
  101.                 {
  102.                     /* 表示TCP客户端接收到新的紧急数据 */
  103.                     printf("--------------------------------\n");
  104.                     strcpy(address_string, inet_ntoa(*((struct in_addr*) &(ip_and_port.saddr))));
  105.                     sprintf(address_string + strlen(address_string), " : %i", ip_and_port.source);
  106.                     strcat(address_string, " <--- urgent ");
  107.                     strcat(address_string, inet_ntoa(*((struct in_addr*) &(ip_and_port.daddr))));
  108.                     sprintf(address_string + strlen(address_string), " : %i", ip_and_port.dest);
  109.                     strcat(address_string, "\n");
  110.                     address_string[strlen(address_string) + 1] = 0;
  111.                     address_string[strlen(address_string)] = tcp_connection->client.urgdata;
  112.                     printf("%s", address_string);
  113.                     return ;
  114.                 }
  115.                 if (tcp_connection->client.count_new)
  116.                 {
  117.                     /* 表示客户端接收到新的数据 */
  118.                     hlf = &tcp_connection->client;
  119.                     /* 此时hlf表示的是客户端的TCP连接信息 */
  120.                     strcpy(address_string, inet_ntoa(*((struct in_addr*) &(ip_and_port.saddr))));
  121.                     sprintf(address_string + strlen(address_string), ":%i", ip_and_port.source);
  122.                     strcat(address_string, " <--- ");
  123.                     strcat(address_string, inet_ntoa(*((struct in_addr*) &(ip_and_port.daddr))));
  124.                     sprintf(address_string + strlen(address_string), ":%i", ip_and_port.dest);
  125.                     strcat(address_string, "\n");
  126.                     printf("--------------------------------\n");
  127.                     printf("%s", address_string);
  128.                     memcpy(content, hlf->data, hlf->count_new);
  129.                     content[hlf->count_new] = '\0';
  130.                     printf("客户端接收数据\n");
  131.                     for (i = 0; i < hlf->count_new; i++)
  132.                     {
  133.                         printf("%s", char_to_ascii(content[i]));
  134.                         /* 输出客户端接收的新的数据,以可打印字符进行显示 */
  135.                     }
  136.                     printf("\n");
  137.                 }
  138.                 else
  139.                 {
  140.                     /* 表示服务器端接收到新的数据 */
  141.                     hlf = &tcp_connection->server;
  142.                     /* 此时hlf表示服务器端的TCP连接信息 */
  143.                     strcpy(address_string, inet_ntoa(*((struct in_addr*) &(ip_and_port.saddr))));
  144.                     sprintf(address_string + strlen(address_string), ":%i", ip_and_port.source);
  145.                     strcat(address_string, " ---> ");
  146.                     strcat(address_string, inet_ntoa(*((struct in_addr*) &(ip_and_port.daddr))));
  147.                     sprintf(address_string + strlen(address_string), ":%i", ip_and_port.dest);
  148.                     strcat(address_string, "\n");
  149.                     printf("--------------------------------\n");
  150.                     printf("%s", address_string);
  151.                     memcpy(content, hlf->data, hlf->count_new);
  152.                     content[hlf->count_new] = '\0';
  153.                     printf("服务器端接收数据\n");
  154.                     for (i = 0; i < hlf->count_new; i++)
  155.                     {
  156.                         printf("%s", char_to_ascii(content[i]));
  157.                         /* 输出服务器接收到的新的数据 */
  158.                     }
  159.                     printf("\n");
  160.                 }
  161.             }
  162.         default:
  163.             break;
  164.     }
  165.     return ;
  166. }
  167. void main()
  168. {
  169.     if (!nids_init())
  170.      /* Libnids初始化 */
  171.     {
  172.         printf("出现错误:%s\n", nids_errbuf);
  173.         exit(1);
  174.     }
  175.     nids_register_tcp(tcp_protocol_callback);
  176.     /* 注册回调函数 */
  177.     nids_run();
  178.     /* Libnids进入循环捕获数据包状态 */
  179. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-02-29 15:55 |只看该作者
我也出现这种问题的,在ubuntu11.10编译时需要加上-lgthread-2.0,而在ubuntu10.04下却不需要,都不知道什么原因
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP