Robert8000 发表于 2007-07-27 08:28

初学libnids库问题

:em16: 请各路高手帮忙解决问题:
当前环境:redhat linux 9.0
第三方库:libpcap0.9.5、libnet1.0.2a、libnids1.21已经安装成功
测试程序为libnids库中带的实例代码如下:
include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include "nids.h"

#define int_ntoa(x)        inet_ntoa(*((struct in_addr *)&x))

// struct tuple4 contains addresses and port numbers of the TCP connections
// the following auxiliary function produces a string looking like
// 10.0.0.1,1024,10.0.0.2,23
char *
adres (struct tuple4 addr)
{
static char buf;
strcpy (buf, int_ntoa (addr.saddr));
sprintf (buf + strlen (buf), ",%i,", addr.source);
strcat (buf, int_ntoa (addr.daddr));
sprintf (buf + strlen (buf), ",%i", addr.dest);
return buf;
}

void
tcp_callback (struct tcp_stream *a_tcp, void ** this_time_not_needed)
{
char buf;
strcpy (buf, adres (a_tcp->addr)); // we put conn params into buf
if (a_tcp->nids_state == NIDS_JUST_EST)
    {
    // connection described by a_tcp is established
    // here we decide, if we wish to follow this stream
    // sample condition: if (a_tcp->addr.dest!=23) return;
    // in this simple app we follow each stream, so..
      a_tcp->client.collect++; // we want data received by a client
      a_tcp->server.collect++; // and by a server, too
      a_tcp->server.collect_urg++; // we want urgent data received by a
                                 // server
#ifdef WE_WANT_URGENT_DATA_RECEIVED_BY_A_CLIENT
      a_tcp->client.collect_urg++; // if we don't increase this value,
                                 // we won't be notified of urgent data
                                 // arrival
#endif
      fprintf (stderr, "%s established\n", buf);
      return;
    }
if (a_tcp->nids_state == NIDS_CLOSE)
    {
      // connection has been closed normally
      fprintf (stderr, "%s closing\n", buf);
      return;
    }
if (a_tcp->nids_state == NIDS_RESET)
    {
      // connection has been closed by RST
      fprintf (stderr, "%s reset\n", buf);
      return;
    }

if (a_tcp->nids_state == NIDS_DATA)
    {
      // new data has arrived; gotta determine in what direction
      // and if it's urgent or not

      struct half_stream *hlf;

      if (a_tcp->server.count_new_urg)
      {
      // new byte of urgent data has arrived
      strcat(buf,"(urgent->)");
      buf=0;
      buf=a_tcp->server.urgdata;
      write(1,buf,strlen(buf));
      return;
      }
      // We don't have to check if urgent data to client has arrived,
      // because we haven't increased a_tcp->client.collect_urg variable.
      // So, we have some normal data to take care of.
      if (a_tcp->client.count_new)
        {
          // new data for the client
          hlf = &a_tcp->client; // from now on, we will deal with hlf var,
                              // which will point to client side of conn
          strcat (buf, "(<-)"); // symbolic direction of data
        }
      else
        {
          hlf = &a_tcp->server; // analogical
          strcat (buf, "(->)");
        }
    fprintf(stderr,"%s",buf); // we print the connection parameters
                              // (saddr, daddr, sport, dport) accompanied
                              // by data flow direction (-> or <-)

   write(2,hlf->data,hlf->count_new); // we print the newly arrived data
      
    }
return ;
}

int
main ()
{
// here we can alter libnids params, for instance:
// nids_params.n_hosts=256;
if (!nids_init ())
{
        fprintf(stderr,"%s\n",nids_errbuf);
        exit(1);
}
nids_register_tcp (tcp_callback);
nids_run ();
return 0;
}

编译命令:gcc -g -o sniffer main.c -lnids

报错如下:
/usr/local/lib/libnids.a(libnids.o)(.text+0x376): In function `nids_pcap_handler':
/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:335: undefined reference to `g_async_queue_lock'
/usr/local/lib/libnids.a(libnids.o)(.text+0x382):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:337: undefined reference to `g_async_queue_length_unlocked'
/usr/local/lib/libnids.a(libnids.o)(.text+0x3c5):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:343: undefined reference to `g_async_queue_push_unlocked'
/usr/local/lib/libnids.a(libnids.o)(.text+0x8d6): In function `open_live':
/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:515: undefined reference to `pcap_open_live'
/usr/local/lib/libnids.a(libnids.o)(.text+0x979):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:505: undefined reference to `pcap_lookupdev'
/usr/local/lib/libnids.a(libnids.o)(.text+0x9a2): In function `cap_queue_process_thread':
/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:560: undefined reference to `g_async_queue_pop'
/usr/local/lib/libnids.a(libnids.o)(.text+0x9da):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:566: undefined reference to `g_thread_exit'
/usr/local/lib/libnids.a(libnids.o)(.text+0xa1f): In function `nids_init':
/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:594: undefined reference to `pcap_compile'
/usr/local/lib/libnids.a(libnids.o)(.text+0xa37):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:596: undefined reference to `pcap_setfilter'
/usr/local/lib/libnids.a(libnids.o)(.text+0xa4d):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:599: undefined reference to `pcap_datalink'
/usr/local/lib/libnids.a(libnids.o)(.text+0xb06):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:665: undefined reference to `g_thread_init'
/usr/local/lib/libnids.a(libnids.o)(.text+0xb0b):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:666: undefined reference to `g_async_queue_new'
/usr/local/lib/libnids.a(libnids.o)(.text+0xbfb):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:584: undefined reference to `pcap_open_offline'
/usr/local/lib/libnids.a(libnids.o)(.text+0xc6b): In function `nids_run':
/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:682: undefined reference to `g_thread_create_full'
/usr/local/lib/libnids.a(libnids.o)(.text+0xc86):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:683: undefined reference to `pcap_loop'
/usr/local/lib/libnids.a(libnids.o)(.text+0xcaf):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:686: undefined reference to `g_async_queue_push'
/usr/local/lib/libnids.a(libnids.o)(.text+0xd2e): In function `nids_exit':
/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:701: undefined reference to `g_async_queue_length'
/usr/local/lib/libnids.a(libnids.o)(.text+0xd7e):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:709: undefined reference to `pcap_geterr'
/usr/local/lib/libnids.a(libnids.o)(.text+0xdb4):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:711: undefined reference to `pcap_close'
/usr/local/lib/libnids.a(libnids.o)(.text+0xdf1): In function `nids_getfd':
/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:721: undefined reference to `pcap_fileno'
/usr/local/lib/libnids.a(libnids.o)(.text+0xe2d): In function `nids_next':
/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:733: undefined reference to `pcap_next'
/usr/local/lib/libnids.a(libnids.o)(.text+0xe5e):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:739: undefined reference to `g_thread_create_full'
/usr/local/lib/libnids.a(libnids.o)(.text+0xe95):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:742: undefined reference to `g_async_queue_push'
/usr/local/lib/libnids.a(libnids.o)(.text+0xef9):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:735: undefined reference to `pcap_geterr'
/usr/local/lib/libnids.a(libnids.o)(.text+0xf57): In function `nids_dispatch':
/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:753: undefined reference to `g_thread_create_full'
/usr/local/lib/libnids.a(libnids.o)(.text+0xf73):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:754: undefined reference to `pcap_dispatch'
/usr/local/lib/libnids.a(libnids.o)(.text+0xf9f):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:760: undefined reference to `g_async_queue_push'
/usr/local/lib/libnids.a(libnids.o)(.text+0xfcb):/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:757: undefined reference to `pcap_geterr'
/usr/local/lib/libnids.a(libnids.o)(.text+0x3b6): In function `nids_pcap_handler':
/home/lius/hx_sniffer/libnids-1.22/src/libnids.c:345: undefined reference to `g_async_queue_unlock'
/usr/local/lib/libnids.a(killtcp.o)(.text+0x43): In function `nids_killtcp_seq':
/home/lius/hx_sniffer/libnids-1.22/src/killtcp.c:23: undefined reference to `libnet_build_ip'
/usr/local/lib/libnids.a(killtcp.o)(.text+0x84):/home/lius/hx_sniffer/libnids-1.22/src/killtcp.c:25: undefined reference to `libnet_build_tcp'
/usr/local/lib/libnids.a(killtcp.o)(.text+0x91):/home/lius/hx_sniffer/libnids-1.22/src/killtcp.c:30: undefined reference to `libnet_do_checksum'
/usr/local/lib/libnids.a(killtcp.o)(.text+0xa2):/home/lius/hx_sniffer/libnids-1.22/src/killtcp.c:31: undefined reference to `libnet_write_ip'
/usr/local/lib/libnids.a(killtcp.o)(.text+0xc4):/home/lius/hx_sniffer/libnids-1.22/src/killtcp.c:33: undefined reference to `libnet_build_ip'
/usr/local/lib/libnids.a(killtcp.o)(.text+0x105):/home/lius/hx_sniffer/libnids-1.22/src/killtcp.c:35: undefined reference to `libnet_build_tcp'
/usr/local/lib/libnids.a(killtcp.o)(.text+0x112):/home/lius/hx_sniffer/libnids-1.22/src/killtcp.c:41: undefined reference to `libnet_do_checksum'
/usr/local/lib/libnids.a(killtcp.o)(.text+0x123):/home/lius/hx_sniffer/libnids-1.22/src/killtcp.c:42: undefined reference to `libnet_write_ip'
/usr/local/lib/libnids.a(killtcp.o)(.text+0x15c): In function `raw_init':
/home/lius/hx_sniffer/libnids-1.22/src/killtcp.c:51: undefined reference to `libnet_open_raw_sock'
collect2: ld returned 1 exit status
make: *** Error 1

还望高手指点迷津,小弟不胜感激!

Robert8000 发表于 2007-07-27 08:31

小弟在线等,还请有用过libnids库的朋友能慷慨相助!

fcloudf 发表于 2007-07-30 00:44

老大 还是把代码放到code里面吧:em02: :em02: :em02:

zhoujunyi 发表于 2007-08-01 15:24

关于libnids库的问题

libnids库编译的时候需要pcap和net库,所以你的编译参数不对

应该为 gcc -g -o sniffer main.c -lnids -lpcap -lnet

yyrwkk 发表于 2023-04-02 19:54

我也遇到了这样的问题,解决方法
要将 -lnet -lpcap放在 -lnids 后面
g++ test.c -lnids -lnet -lpcap -lgthread-2.0 -lglib-2.0


页: [1]
查看完整版本: 初学libnids库问题