dnl
dnl configure.in for libnids.
dnl
dnl Dug Song <dugsong@monkey.org>

AC_INIT(src/libnids.c)

AC_CONFIG_HEADER(src/config.h)

dnl Initialize prefix.
if test "$prefix" = "NONE" ; then
   prefix="/usr/local"
fi

dnl Checks for programs.
AC_PROG_CC
AC_PROG_RANLIB
AC_PROG_INSTALL

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(sys/time.h syslog.h unistd.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_C_INLINE
AC_HEADER_TIME
dnl XXX - Linux sux.
CFLAGS="$CFLAGS -D_BSD_SOURCE"

dnl Checks for library functions.
AC_CHECK_FUNCS(gettimeofday)
dnl XXX - Solaris sux.
AC_CHECK_LIB(socket, socket)
AC_CHECK_LIB(nsl, gethostbyname)

dnl Checks for libpcap
AC_MSG_CHECKING(for libpcap)
AC_ARG_WITH(libpcap,
[  --with-libpcap=DIR      use libpcap build directory],
[ case "$withval" in
  yes|no)
     AC_MSG_RESULT(no)
     ;;
  *)
     AC_MSG_RESULT($withval)
     if test -f $withval/pcap.h -a -f $withval/libpcap.a; then
        owd=`pwd`
        if cd $withval; then withval=`pwd`; cd $owd; fi
	PCAPINC="-I$withval -I$withval/bpf"
	PCAPLIB="-L$withval -lpcap"
        LDFLAGS="$LDFLAGS -L$withval"
     else
        AC_ERROR(pcap.h or libpcap.a not found in $withval)
     fi
     ;;
  esac ],
[ if test -f ${prefix}/include/pcap.h; then
     PCAPINC="-I${prefix}/include"
     PCAPLIB="-L${prefix}/lib -lpcap"
     LDFLAGS="$LDFLAGS -L${prefix}/lib"
  elif test -f /usr/include/pcap/pcap.h; then
     PCAPINC="-I/usr/include/pcap"
     PCAPLIB="-lpcap"
  elif test -f /usr/include/pcap.h; then
     PCAPLIB="-lpcap"
  else
     AC_ERROR(libpcap not found)
  fi
  AC_MSG_RESULT(yes) ]
)
AC_SUBST(PCAPINC)
AC_SUBST(PCAPLIB)


AC_MSG_CHECKING(for new libpcap)
AC_CHECK_LIB(pcap,pcap_open_live_new,NEW_PCAP=1,NEW_PCAP=0)
AC_SUBST(NEW_PCAP)


dnl Checks for libnet
AC_MSG_CHECKING(for libnet)
AC_ARG_WITH(libnet,
[  --with-libnet=DIR       use libnet build directory],
[ case "$withval" in
  yes|no)
     AC_MSG_RESULT(no)
     ;;
  *)
     AC_MSG_RESULT($withval)
     if test -f $withval/include/libnet.h -a -f $withval/lib/libnet.a; then
        owd=`pwd`
        if cd $withval; then withval=`pwd`; cd $owd; fi
	CFLAGS="$CFLAGS `$withval/libnet-config --defines`"
	LNETINC="-I$withval/include"
	LNETLIB="-L$withval/lib -lnet"
	LDFLAGS="$LDFLAGS -L$withval/lib"
     else
        AC_ERROR(libnet.h or libnet.a not found in $withval)
     fi
     ;;
  esac ],
[ if test -f ${prefix}/include/libnet.h; then
     CFLAGS="$CFLAGS `${prefix}/bin/libnet-config --defines`"
     LNETINC="-I${prefix}/include"
     LNETLIB="-L${prefix}/lib -lnet"
     LDFLAGS="$LDFLAGS -L${prefix}/lib"
  elif test -f /usr/include/libnet.h; then
     CFLAGS="$CFLAGS `libnet-config --defines`"
     LNETLIB="-lnet"
  else
     AC_ERROR(libnet not found)
  fi
  AC_MSG_RESULT(yes) ]
)
AC_SUBST(LNETINC)
AC_SUBST(LNETLIB)

AC_MSG_CHECKING(the name of struct icmp)
AC_TRY_COMPILE([#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>], struct icmphdr h;int c=h.type, ICMPHEADER=1,ICMPHEADER=0)
AC_SUBST(ICMPHEADER)
if test $ICMPHEADER = 1 ; then 
	AC_MSG_RESULT(struct icmphdr) ; else AC_MSG_RESULT(struct icmp) 
fi
AC_MSG_CHECKING(if tcp states are defined)
AC_TRY_COMPILE([#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>], int c=TCP_ESTABLISHED,TCPSTATES=1,TCPSTATES=0)
AC_SUBST(TCPSTATES)
if test $TCPSTATES = 1 ; then 
	AC_MSG_RESULT(yes) ; else AC_MSG_RESULT(no) 
fi

AC_MSG_CHECKING(for bsd-ish struct udphdr)
AC_TRY_COMPILE([#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/udp.h>], struct udphdr h;int c=h.uh_ulen,HAVE_BSD_UDPHDR=1,HAVE_BSD_UDPHDR=0)
AC_SUBST(HAVE_BSD_UDPHDR)
if test $HAVE_BSD_UDPHDR = 1 ; then 
	AC_MSG_RESULT(yes) ; else AC_MSG_RESULT(no) 
fi


AC_OUTPUT(Makefile src/Makefile src/nids.h samples/Makefile)
