#########################################
##
# Checks for header files.
##
#########################################

##
#   Standard checks:
##
#  Core:
AC_HEADER_STDC          #  <stdlib.h>, <stdarg.h>,   <string.h>  & <float.h>
AC_HEADER_DIRENT        #  <dirent.h>, <sys/ndir.h>, <sys/dir.h> & <ndir.h>
#  Apps:
# Don't use <sys/wait.h> when using Windows sockets
case x$with_socklib in
  xwinsock2)
    ;;
  *)
    AC_HEADER_SYS_WAIT  #  <sys/wait.h>
esac

# allow for quick module/library/etc processing checks
if test "x$NETSNMP_QUICK_CHECK" = "x" ; then

##
#   Stand-alone headers:
##
#  Core:
AC_CHECK_HEADERS([getopt.h   pthread.h  regex.h      ] dnl
                 [string.h   syslog.h   unistd.h     ] dnl
                 [stdint.h   inttypes.h              ] dnl
                 [process.h          ] dnl
                 [sys/param.h        ] dnl
                 [sys/select.h       ] dnl
                 [sys/syslog.h       ] dnl
                 [sys/time.h         ] dnl
                 [sys/timeb.h        ])

#  Library and Agent:
AC_CHECK_HEADERS([nlist.h],,,[
AC_INCLUDES_DEFAULT
[
#ifndef LIBBSD_DISABLE_DEPRECATED
#define LIBBSD_DISABLE_DEPRECATED 1
#endif
]])

#  Library:
AC_CHECK_HEADERS([crt_externs.h                        ] dnl
                 [fcntl.h    io.h       kstat.h        ] dnl
                 [limits.h   locale.h                  ] dnl
                 [mach-o/dyld.h                        ] dnl
                 [sys/file.h       sys/ioctl.h         ] dnl
                 [sys/sockio.h     sys/stat.h          ] dnl
                 [sys/systemcfg.h  sys/systeminfo.h    ] dnl
                 [sys/times.h      sys/uio.h           ] dnl
                 [sys/utsname.h      ] dnl
                 [netipx/ipx.h       ])

#  Agent:
AC_CHECK_HEADERS([dlfcn.h    err.h      fcntl.h    fstab.h      ] dnl
                 [           grp.h      io.h                    ] dnl
                 [ioctls.h   kstat.h    kvm.h      limits.h     ] dnl
                 [           mntent.h   mtab.h                  ] dnl
                 [pkglocs.h  pwd.h      spawn.h                 ] dnl
                 [           com_err.h             et/com_err.h ] dnl
                 [           utmpx.h    utsname.h               ])

AC_CHECK_HEADERS([sys/diskio.h  sys/dkio.h                                 ] dnl
                 [sys/file.h    sys/filio.h   sys/fixpoint.h               ] dnl
                 [sys/fs.h      sys/ioctl.h   sys/loadavg.h   sys/mntent.h ] dnl
                 [sys/mnttab.h  sys/pool.h    sys/protosw.h   sys/pstat.h  ] dnl
                 [sys/sockio.h  sys/stat.h    sys/statfs.h    sys/statvfs.h] dnl
                 [sys/stream.h  sys/sysget.h  sys/sysmacros.h sys/sysmp.h  ] dnl
                 [sys/tcpipstats.h            sys/utsname.h  sys/vfs.h     ] dnl
                 [sys/vm.h      sys/vmmac.h   sys/vmmeter.h  sys/vmparam.h ] dnl
                 [sys/vmsystm.h                                            ])

AC_CHECK_HEADERS([asm/page.h asm/types.h               ] dnl
                 [inet/mib2.h                          ] dnl
                 [linux/hdreg.h linux/tasks.h          ] dnl
                 [machine/param.h                      ] dnl
                 [ufs/fs.h                             ] dnl
                 [uvm/uvm_extern.h uvm/uvm_param.h     ] dnl
                 [vm/vm.h vm/vm_extern.h vm/vm_param.h ])

#  Apps:
AC_CHECK_HEADERS([fcntl.h    grp.h      pwd.h      ] dnl
                 [sys/cdefs.h   sys/ioctl.h        ] dnl
                 [sys/time.h                       ])

#  Not-Used:
AC_CHECK_HEADERS([search.h   sgtty.h    xti.h      ] dnl
                 [sys/dmap.h sys/poll.h            ] dnl
                 [sys/timeout.h                    ] dnl
                 [machine/pte.h                    ] dnl
                 [pci/pci.h                        ])


# Valgrind
AC_CHECK_HEADERS([valgrind/valgrind.h valgrind/memcheck.h])
# OpenSSL
AC_CHECK_HEADERS([openssl/aes.h ])     #  (Core)
AC_CHECK_HEADERS([openssl/dh.h  ])     #  (Apps)
#  Not-Used:
AC_CHECK_HEADERS([openssl/des.h  openssl/hmac.h ] dnl
                 [openssl/evp.h  security/cryptoki.h])

# RPM subdirectory path                   (Agent)
if test "x$with_rpm" != "xno"; then
  AC_CHECK_HEADERS([rpm/rpmts.h   rpm/rpmfileutil.h], [], [],
		   [[#include <rpm/rpmlib.h>]])
fi


##
#   Pre-requisite on other headers:
##

#       requiring <sys/param.h>  (MAXPATHLEN)           (OpenBSD 3.4)
#  Agent:
#
AC_CHECK_HEADERS(sys/swap.h,,,
    AC_INCLUDES_DEFAULT([])
    [
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
    ])

#       requiring <sys/param.h>                         (FreeBSD)
#
AC_CHECK_HEADERS([malloc.h        osreldate.h                   ] dnl
                 [machine/types.h ] dnl
                 [sys/dkstat.h    sys/conf.h    sys/mbuf.h      ] dnl
                 [sys/mount.h     sys/proc.h    sys/queue.h     ] dnl
                 [sys/user.h                                    ],,,
    [[
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
    ]])

case "$target_os" in
    linux*)
	# <sys/sysctl.h> is considered deprecated on Linux and including that
	# header file triggers a compiler warning. See also
	# https://savannah.gnu.org/forum/forum.php?forum_id=9515.
        ;;
    *)
	AC_CHECK_HEADERS([sys/sysctl.h])
        ;;
esac

#       requiring <sys/sema.h>                          (IRIX 6.5)
#  Agent:
#
AC_CHECK_HEADERS([sys/sema.h])
AC_CHECK_HEADERS(sys/hashing.h,,,
    AC_INCLUDES_DEFAULT([])
    [
#ifdef HAVE_SYS_SEMA_H
#include <sys/sema.h>
#endif
    ])

#       requiring _KMEMUSER and <sys/types.h>           (BSDi3/IRIX)
#  Agent:
#
AC_CHECK_HEADERS(sys/vnode.h,,,
    [
#define _KMEMUSER 1
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
    ])

#       requiring <machine/types.h>                     (NetBSD)
#  Agent:
#
AC_CHECK_HEADERS(sys/disklabel.h,,,
    [[
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_MACHINE_TYPES_H
#include <machine/types.h>
#endif
    ]])

#	QNX
AC_CHECK_HEADERS([nbutil.h])

#       UFS headers
#  Agent:
#
AC_CHECK_HEADERS([ufs/ufs/dinode.h ufs/ufs/quota.h ufs/ufs/inode.h ufs/ffs/fs.h],,,
    [[
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UFS_UFS_DINODE_H
#include <ufs/ufs/dinode.h>
#endif
#ifdef HAVE_UFS_UFS_QUOTA_H
#include <ufs/ufs/quota.h>
#endif
    ]])

#       requiring <vm/vm.h>                             (BSDi2)
#  Agent:
#
AC_CHECK_HEADERS(vm/swap_pager.h,,,
    [[
#ifdef HAVE_VM_VM_H
#include <vm/vm.h>
#endif
    ]])

#       netlink/rtnetlink                               (Linux)
#  Agent:
#
AC_CHECK_HEADERS([linux/netlink.h  linux/rtnetlink.h],,,
    [[
#ifdef HAVE_ASM_TYPES_H
#include <asm/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_LINUX_NETLINK_H
#include <linux/netlink.h>
#endif
    ]])

#
# Avoid that on MinGW any networking headers get included that are
# incompatible with <winsock2.h> by only testing for certain headers if not
# building for one of these two platforms.
#

case x$with_socklib in
  xwinsock2)
    ;;
  *)

AC_CHECK_HEADERS([sys/socket.h]dnl
                 [sys/un.h]dnl
                 [netinet/tcp_fsm.h]dnl
                 [sys/sockio.h]dnl
                 [sys/socketvar.h]dnl
                 [net/if_dl.h]dnl
                 [net/if_types.h]dnl
                 [arpa/inet.h]dnl
                 [net/if.h]dnl
                 [netinet/in.h]dnl
                 [netdb.h]dnl
                 [net/if_arp.h]dnl
                 [net/if_mib.h]dnl
                 [net/if_var.h]dnl
                 [netinet/if_ether.h]dnl
                 [netinet/in_systm.h]dnl
                 [netinet/in_var.h]dnl
                 [netinet/ip.h]dnl
                 [netinet/ip_icmp.h]dnl
                 [netinet/icmp_var.h]dnl
                 [netinet/ip_var.h]dnl
                 [netinet/tcp.h]dnl
                 [netinet/tcp_timer.h]dnl
                 [netinet/tcp_var.h]dnl
                 [netinet/tcpip.h]dnl
                 [netinet/udp.h]dnl
                 [netinet/udp_var.h]dnl
                 [net/route.h]dnl
                 [netinet/icmp6.h]dnl
                 [netinet/in_pcb.h]dnl
                 [netinet/ip6.h]dnl
                 [netinet6/in6_var.h]dnl
                 [netinet6/in6_pcb.h]dnl
                 [netinet6/ip6_var.h]dnl
                 [netinet6/nd6.h]dnl
                 [netinet6/tcp6.h]dnl
                 [netinet6/tcp6_fsm.h]dnl
                 [netinet6/tcp6_timer.h]dnl
                 [netinet6/tcp6_var.h]dnl
                 ,,,
    [[
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_TIMEOUT_H
#include <sys/timeout.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_SYS_SOCKETVAR_H
#include <sys/socketvar.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETINET_IN_SYSTM_H
#include <netinet/in_systm.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NETINET_IP_H
#include <netinet/ip.h>
#endif
#ifdef HAVE_NETINET_IP_H
#include <netinet/ip_icmp.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_NET_IF_VAR_H
#include <net/if_var.h>
#endif
#ifdef HAVE_NETINET_IP_VAR_H
#include <netinet/ip_var.h>
#endif
#ifdef HAVE_NETINET_IP_ICMP_H
#include <netinet/ip_icmp.h>
#endif
#ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h>
#endif
#ifdef HAVE_NETINET_UDP_H
#include <netinet/udp.h>
#endif
#ifdef HAVE_NETINET_TCP_TIMER_H
#include <netinet/tcp_timer.h>
#endif
#ifdef HAVE_NETINET_IP6_H
#include <netinet/ip6.h>
#endif
#ifdef HAVE_NETINET_ICMP6_H
#include <netinet/icmp6.h>
#endif
    ]])

esac

##
#   Pre-requisite on specific types
##

#       Special hack to get around various problems on older Linux kernels
#       (major ugh!)
#  Agent:
#
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <linux/ethtool.h>],[])],
		  AC_CHECK_HEADERS([linux/ethtool.h]),
AC_CHECK_HEADERS([linux/ethtool.h],,,
[[
#include <linux/types.h>
typedef __u64 u64;         /* hack, so we may include kernel's ethtool.h */
typedef __u32 u32;         /* ditto */
typedef __u16 u16;         /* ditto */
typedef __u8 u8;           /* ditto */
]])
AC_DEFINE([HAVE_LINUX_ETHTOOL_NEEDS_U64], 1,
          [Define if __u8 .. __u64 must be declared explicitly before including <linux/ethtool.h>]))

##
#   Headers requiring particular libraries
##

#       for HostRes                                     (Solaris 2.x)
#  Agent:
#
AC_CHECK_HEADERS(pkginfo.h,
    NETSNMP_SEARCH_LIBS(pkginfo, adm,
        AC_DEFINE(HAVE_PKGINFO, 1, [define if you have pkginfo]),,,LMIBLIBS))

AC_CHECK_HEADERS(pkg.h,
    NETSNMP_SEARCH_LIBS(pkg_init, pkg,
        AC_DEFINE(HAVE_LIBPKG, 1, [define if you have BSD pkg-ng])))

if test $ac_cv_header_pkg_h = no
then
    unset ac_cv_header_pkg_h
    netsnmp_save_CPPFLAGS="$CPPFLAGS"
    netsnmp_save_LDFLAGS="$LDFLAGS"
    CPPFLAGS="$CPPFLAGS -I/usr/local/include"
    LDFLAGS="$LDFLAGS -L/usr/local/lib"
    AC_CHECK_HEADERS(pkg.h,
        NETSNMP_SEARCH_LIBS(pkg_init, pkg,
	    AC_DEFINE(HAVE_LIBPKG, 1, [define if you have BSD pkg-ng])))
    if test $ac_cv_header_pkg_h = no
    then
        CPPFLAGS="$netsnmp_save_CPPFLAGS"
        LDFLAGS="$netsnmp_save_LDFLAGS"
    fi
fi

case $target_os in
    aix*)
        #       for CPU/memory statistics               (AIX)
        #  Agent:
        #
        AC_CHECK_HEADERS(libperfstat.h,
            NETSNMP_SEARCH_LIBS(perfstat_cpu_total, perfstat,
                AC_DEFINE(HAVE_PERFSTAT, 1, [on aix, if you have perfstat]),
                AC_MSG_ERROR([

*** To monitor CPU/memory values in AIX you need to install
*** libperfstat which can be found in bos.perf
]),,
                LMIBLIBS),,
    [[
#ifdef HAVE_SYS_PROTOSW_H
#include <sys/protosw.h>
#endif
    ]])
        ;;
    hpux*)
        #       HP-UX agent needs open_mib       (XXX - not a header check!)
        #
        NETSNMP_SEARCH_LIBS(open_mib, nm,
            AC_DEFINE(HAVE_LIBNM, 1, [define if you have libnm]),,,LMIBLIBS)
        ;;
    *)
        ;;
esac

#       for MIB-2 info                                  (Cygwin and MinGW)
#  Core:
#
AC_CHECK_HEADERS([windows.h iphlpapi.h], [], [], [
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
])

AC_CHECK_HEADERS([lm.h], [LAGENTLIBS="$LAGENTLIBS -lnetapi32"], [], [
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
])

#       for MIB-2 info                                  (MinGW)
#  Core:
#
if test x$with_socklib = xwinsock2; then
  AC_CHECK_HEADERS([winsock.h winsock2.h ws2tcpip.h],,,[#include <windows.h>])
fi

#
# for ucd-snmp/lmsensorsMib
#
AC_CHECK_HEADERS([sensors/sensors.h picl.h])

##  This is here to avoid loads of redefinition warnings during
##  module configuration

##
#   rtentry structure checks:
##

# Check struct rtentry for common fields
#   Agent:
#
AC_CHECK_MEMBERS([struct rtentry.rt_dst,
                  struct rtentry.rt_hash,
                  struct rtentry.rt_next,
                  struct rtentry.rt_refcnt,
                  struct rtentry.rt_unit,
                  struct rtentry.rt_use],,,
    [
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <sys/types.h>
#ifdef NETSNMP_IFNET_NEEDS_KERNEL_STRUCTURES
#define _KERNEL_STRUCTURES 1
#endif
#ifdef HAVE_SYS_SOCKET_H
#ifdef NETSNMP_IFNET_NEEDS_KERNEL
#define KERNEL
#if !defined(__DragonFly__)
#define _KERNEL 1
#endif
#endif
#include <sys/socket.h>
#undef KERNEL
#undef _KERNEL
#endif
#ifdef HAVE_NET_ROUTE_H
#include <net/route.h>
#endif
    ])

fi # NETSNMP_QUICK_CHECK
