dnl $Id: configure.in,v 1.9 2004/03/01 20:26:11 mike Exp $
dnl
dnl Libnet autoconfiguration information file
dnl Copyright (c) 1998 - 2002 Mike D. Schiffman <mike@infonexus.com>
dnl All rights reserved.
dnl
dnl Process this file with autoconf to produce a configure script.


AC_INIT(src/libnet_build_ip.c)
LIBNET_VERSION=`cat VERSION`
AC_MSG_RESULT(beginning autoconfiguration process for libnet-$LIBNET_VERSION...)
AC_CANONICAL_SYSTEM

AM_INIT_AUTOMAKE(libnet, $LIBNET_VERSION)
AM_CONFIG_HEADER(include/config.h)
AM_MAINTAINER_MODE

dnl
dnl Check for the usual programs
dnl
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_RANLIB

AC_CHECK_HEADERS(sys/sockio.h)

dnl
dnl And some custom things
dnl

AC_LIBNET_ENDIAN_CHECK
AC_LBL_UNALIGNED_ACCESS
dnl AC_LBL_LIBRARY_NET

AC_SUBST(ENDIANESS)
AC_SUBST(LIBNET_VERSION)
AC_SUBST(HAVE_PACKET_SOCKET)
AC_SUBST(ADDITIONAL_LIBS)
AC_SUBST(LIBNET_CONFIG_DEFINES)
AC_SUBST(LIBNET_CONFIG_LIBS)
AC_SUBST(LIBNET_CONFIG_CFLAGS)

dnl
dnl Check for typedefs, structures, and compiler characteristics
dnl
AC_PROG_GCC_TRADITIONAL
if test "$GCC" = yes ; then
    CFLAGS="$CFLAGS -Wall"
fi

dnl
dnl Set the root install directory
dnl
AC_PREFIX_DEFAULT(/usr)
dnl if test "$prefix" = "NONE"; then
dnl     prefix="/usr"
dnl fi

dnl
dnl Check for library functions.
dnl
AC_CHECK_FUNCS(strerror)

dnl
dnl Get link-layer interface type
dnl
AC_MSG_CHECKING(link-layer packet interface type)

if test -r /dev/bpf0 ; then
    AC_LIBOBJ([libnet_link_bpf])
    AC_MSG_RESULT(found bpf)
elif test -r /usr/include/net/pfilt.h ; then
    AC_LIBOBJ([libnet_link_pf])
    AC_MSG_RESULT(found pf)
elif test -r /dev/nit ; then
    AC_LIBOBJ([libnet_link_snit])
    AC_MSG_RESULT(found snit)
elif test -r /usr/include/sys/net/nit.h ; then
    AC_LIBOBJ([libnet_link_nit])
    AC_MSG_RESULT(found nit)
elif test -r /usr/include/net/raw.h ; then
    AC_LIBOBJ([libnet_link_snoop])
    AC_MSG_RESULT(found snoop)
elif test -r /usr/include/sys/dlpi.h ; then
    AC_LIBOBJ([libnet_link_dlpi])
    AC_MSG_RESULT(found dlpi)
    AC_DEFINE(HAVE_DLPI)
elif test -r /usr/include/linux/socket.h ; then
    AC_LIBOBJ([libnet_link_linux])
    AC_MSG_RESULT(found linux primitives)
    AC_LIBNET_CHECK_PF_PACKET
    AC_LIBNET_LINUX_PROCFS
elif test -c /dev/bpf0 ; then           # check again in case not readable
    AC_LIBOBJ([libnet_link_bpf])
    AC_MSG_RESULT(found bpf)
elif test -c /dev/nit ; then            # check again in case not readable
    AC_LIBOBJ([libnet_link_snit])
    AC_MSG_RESULT(found snit)
elif test "$target_os" = "cygwin" ; then
     AC_LIBOBJ([libnet_link_win32])
     AC_MSG_RESULT(found win32 wpcap)
else
    AC_LIBOBJ([libnet_link_none])
    AC_MSG_WARN(could not find a link-layer packet interface)
    AC_MSG_WARN(link-layer packet injection will not be available)
fi

dnl
dnl Inform user about the packet builders we've got
dnl
echo -n "scanning available packet construction modules: "
for dir in src/*build*c ; do
    echo $dir | sed -e /src\\/libnet_build_/s/// | sed -e /\\.c/s/// | tr \\n \\040
done
echo ""

dnl
dnl Check and set OS specific parameters
dnl

case "$target_os" in

*linux*)
    AC_DEFINE(_BSD_SOURCE)
    AC_DEFINE(__BSD_SOURCE)
    AC_DEFINE(__FAVOR_BSD)
    LIBNET_CONFIG_DEFINES="-D_BSD_SOURCE -D__BSD_SOURCE -D__FAVOR_BSD"
    AC_CHECK_HEADERS(net/ethernet.h, \
        LIBNET_CONFIG_DEFINES="$LIBNET_CONFIG_DEFINES -DHAVE_NET_ETHERNET_H")
    ;;

*freebsd*)
    AC_DEFINE(LIBNET_BSDISH_OS)
    AC_DEFINE(LIBNET_BSD_BYTE_SWAP)
    AC_DEFINE(HAVE_SOCKADDR_SA_LEN)
    LIBNET_CONFIG_DEFINES="-DLIBNET_BSDISH_OS -DLIBNET_BSD_BYTE_SWAP -DHAVE_SOCKADDR_SA_LEN"
    ;;

*netbsd*)
    AC_DEFINE(LIBNET_BSDISH_OS)
    AC_DEFINE(LIBNET_BSD_BYTE_SWAP)
    LIBNET_CONFIG_DEFINES="-DLIBNET_BSDISH_OS -DLIBNET_BSD_BYTE_SWAP"
    ;;

*openbsd*)
    AC_DEFINE(LIBNET_BSDISH_OS)
    AC_DEFINE(HAVE_SOCKADDR_SA_LEN)
    LIBNET_CONFIG_DEFINES="-DLIBNET_BSDISH_OS"
    ;;

*bsdi*)
    AC_DEFINE(LIBNET_BSDISH_OS)
    AC_DEFINE(LIBNET_BSD_BYTE_SWAP)
    LIBNET_CONFIG_DEFINES="-DLIBNET_BSDISH_OS -DLIBNET_BSD_BYTE_SWAP"
    ;;

*darwin*)
    AC_DEFINE(HAVE_SOCKADDR_SA_LEN)
    LIBNET_CONFIG_DEFINES="-DHAVE_SOCKADDR_SA_LEN"
    ;;

*solaris*)
    AC_DEFINE(HAVE_SOLARIS)
    AC_CHECK_HEADERS(sys/bufmod.h sys/dlpi_ext.h)
    AC_MSG_CHECKING(for /dev/dlpi device)
    if test -c /dev/dlpi; then
        AC_MSG_RESULT(yes)
        AC_DEFINE(HAVE_DEV_DLPI)
    else
        AC_MSG_RESULT(no)
        dir="/dev/dlpi"
        AC_MSG_CHECKING(for $dir directory)
        if test -d $dir ; then
            AC_MSG_RESULT(yes)
            AC_DEFINE_UNQUOTED(DLPI_DEV_PREFIX, "$dir")
        else
            AC_MSG_RESULT(no)
        fi
    fi
    AC_CHECK_LIB(socket, socket, ,AC_MSG_ERROR(libsocket not found.))
    AC_CHECK_LIB(nsl, gethostbyname, ,AC_MSG_ERROR(libnsl not found.))

dnl 
dnl Broken checksums?
dnl
    case "`uname -r`" in
    5.4)
        AC_DEFINE(STUPID_SOLARIS_CHECKSUM_BUG)
        ;;
    5.5*)
        AC_DEFINE(STUPID_SOLARIS_CHECKSUM_BUG)
        ;;
    5.8)
        AC_DEFINE(STUPID_SOLARIS_CHECKSUM_BUG)
    	AC_DEFINE(HAVE_SOLARIS_IPV6)
        ;;
    5.9)
        AC_DEFINE(STUPID_SOLARIS_CHECKSUM_BUG)
    	AC_DEFINE(HAVE_SOLARIS_IPV6)
        ;;

    esac

    ADDITIONAL_LIBS='-lsocket -lnsl'
    LIBNET_CONFIG_LIBS="$LIBNET_CONFIG_LIBS -lsocket -lnsl"
    ;;

*hpux11*)
    AC_DEFINE(HAVE_HPUX11)
    AC_CHECK_HEADERS(sys/dlpi_ext.h)
    ;;

*cygwin*)
     AC_DEFINE(LIBNET_BSDISH_OS)
     AC_DEFINE(NO_SNPRINTF)
     LIBNET_CONFIG_DEFINES="-DLIBNET_BSDISH_OS -DNO_SNPRINTF"
     CFLAGS="$CFLAGS -mno-cygwin -O0 -fnative-struct -DNO_SNPRINTF -I/usr/include/pcap"
     AC_CHECK_LIB(packet, PacketSetMode, ,AC_MSG_ERROR(packet lib not found.))
     AC_CHECK_LIB(wpcap, pcap_setmode, ,AC_MSG_ERROR(pcap lib not found.))
     LIBS="$LIBS -lws2_32"
     ;;

*)
    AC_MSG_WARN(apparently your OS is not officially supported yet)
    AC_MSG_WARN(this may not work)
    AC_MSG_RESULT(please send diffs to mike@infonexus.com)
    ;;

esac

AC_OUTPUT(Makefile src/Makefile include/Makefile \
    include/libnet/Makefile sample/Makefile version.h include/libnet.h \
    libnet-config, chmod +x libnet-config)
dnl EOF
