/*
 * ----------------------------------------------------------------
 * Night Light Defines (defines.h)
 * ----------------------------------------------------------------
 * Copyright (C) 1997-2007 Jonas Kvinge <jonas@night-light.net>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Last modified by:
 * Jonas Kvinge (12.04.2007)
 *
 */

#ifdef DEFINES_H
#warning "defines.h already included."
#else
#define DEFINES_H

struct hostent;

/* DEFINES */

#ifndef TRUE
  #define TRUE			1
#endif
#ifndef FALSE
  #define FALSE			0
#endif

#define SUCCESS			0
#define ERROR			-1

#define FOREVER			-1
#define NEVER			0
#define NONE			-1

#define FD_NONE			-1

#define HOST_UNRESOLVED		"<unresolved>"

#define UNKNOWNUSER		"<unknown>"
#define UNKNOWNHOST		"<unknown>"

#define SERVER_PORT_MIN		1
#define SERVER_PORT_MAX		1023

#define CLIENT_PORT_MIN		1024
#define CLIENT_PORT_MAX		0xFFFF

#define PASSWD_FILE		"/etc/passwd"
#define GROUP_FILE		"/etc/group"

#define STDINMSGLEN		512
#define DATESTAMPLEN		12
#define TIMESTAMPLEN		10
#define DTSTAMPLEN		21
#define HOURLEN			2
#define MINLEN			2
#define SECLEN			2
#define STRDURATIONLEN		200
#define FILELEN			1024
#define USERLEN			12
#define HOSTLEN			254
#define RECVBUFFERLEN		2048
#define SENDBUFFERLEN		2048
#define LINELEN			4000
#define TEXTLEN			150
#define SPACE10			"          "
#define SPACE15			"               "
#define SPACE20			"                    "
#define ONESECOND		1
#define ONEMINUTE		60
#define ONEHOUR			3600
#define ONEDAY			86400
#define FILECHARS		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.!@{}[]~:,"
#define STRHOSTCHARS		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.?*"

#define FAKELOOP for (FakeLoop = TRUE ; FakeLoop == TRUE ; FakeLoop = FALSE)
#define FOREVERLOOP while(TRUE)

#ifndef __FILE__
  #define __FILE__ "unknown"
#endif
#ifndef __LINE__
  #define __LINE__ "unknown"
#endif
#ifndef __FUNCTION__
  #define __FUNCTION__ "unknown"
#endif

#ifndef INADDR_NONE
  #define INADDR_NONE 0xffffffff
#endif

#ifndef MSG_NOSIGNAL
  #define MSG_NOSIGNAL 0
#endif

#define PRINT_SIZE_T		signed long int
#define PRINT_PID_T		unsigned long int
#define PRINT_TIME_T		signed long int
#define PRINT_UID_T		unsigned long int
#define PRINT_GID_T		unsigned long int

#if WIN32
  #define LINEFEED "\r\n"
#else
  #define LINEFEED "\n"
#endif

#if !HAVE_SIZE_T
  typedef int size_t;
#endif

#if !HAVE_PID_T
  typedef int pid_t;
#endif

#if !HAVE_UID_T
  typedef long uid_t;
#endif

#if !HAVE_GID_T
  typedef long gid_t;
#endif

#if !HAVE_SOCKLEN_T
  typedef size_t socklen_t;
#endif

#if !HAVE_SETEUID
  #define seteuid setuid
#endif
#if !HAVE_SETEGID
  #define setegid setgid
#endif

#ifndef accept_addrlen_type
#if OS_BSD || OS_LINUX || OS_WIN32 || WIN32
  #define accept_addrlen_type socklen_t
  #define getsockopt_optlen_type socklen_t
  #define getsockname_namelen_type socklen_t
  #define getpeername_namelen_type socklen_t
#elif OS_SOLARIS
  #define accept_addrlen_type socklen_t
  #define getsockopt_optlen_type int
  #define getsockname_namelen_type socklen_t
  #define getpeername_namelen_type socklen_t
#elif OS_AIX
  #define accept_addrlen_type int
  #define getsockopt_optlen_type int
  #define getsockname_namelen_type socklen_t
  #define getpeername_namelen_type socklen_t
#elif OS_HPUNIX
  #define accept_addrlen_type socklen_t
  #define getsockopt_optlen_type socklen_t
  #define getsockname_namelen_type socklen_t
  #define getpeername_namelen_type socklen_t
#elif OS_OSF1
  #define accept_addrlen_type int
  #define getsockopt_optlen_type int
  #define getsockname_namelen_type int
  #define getpeername_namelen_type int
#else
  #define accept_addrlen_type socklen_t
  #define getsockopt_optlen_type socklen_t
  #define getsockname_namelen_type socklen_t
  #define getpeername_namelen_type socklen_t
#endif
#endif

#if HAVE_SNPRINTF
  #if !HAVE_DECL_SNPRINTF || OS_OSF1
    int snprintf(char *str, size_t size, const char *format, ...);
  #endif
#else
  #warning "This system does not have snprintf(). Using sprintf() for snprintf()!"
  #define snprintf(S, Z, F, ...) sprintf(S, F, ...)
#endif

#if HAVE_VSNPRINTF
  #if !HAVE_DECL_VSNPRINTF || OS_OSF1
    int vsnprintf(char *str, size_t size, const char *format, va_list ap);
  #endif
#else
  #warning "This system does not have vsnprintf(). Using vsprintf() for vsnprintf()!"
  #define vsnprintf(S, Z, F, ...) vsprintf(S, F, ...)
#endif

#if !HAVE_INET_ATON
  #if HAVE_INET_PTON
    #define inet_aton(C, A) inet_pton(AF_INET, C, A)
  #else
    #define inet_aton nl_net_aton
  #endif
#endif

#ifndef NBLOCK_SYSV
  #ifndef NBLOCK_BSD
    #ifndef NBLOCK_POSIX
      #define NBLOCK_POSIX 1
    #endif
  #endif
#endif

#if ARES
  #if defined(ARES_VERSION)
    #if ARES_VERSION >= 0x010500
      #define HAVE_CARES_CALLBACK_TIMEOUTS 1
    #endif
  #endif
  #if HAVE_ARES_VERSION_H
    #define res_strerror(X) ares_strerror(X)
  #else
    #define res_strerror(X) ares_strerror(X, NULL)
  #endif
#else
  typedef void (*ares_host_callback)(void *arg, int status,  int timeouts, struct hostent *hostent);
  #define res_strerror(X) strerror(X)
#endif

#if ARES && defined(MAIN_C) && !HAVE_ARES_CANCELQUERY
  #warning "Your version of ares does not have ares_cancelquery()."
#endif

/* MACROS */

#undef FREE
#define FREE(x) \
                free((x)); \
                (x) = NULL

#ifndef DEBUGPRINT
  #if DEBUG
    #define DEBUGPRINT(X, ...) if (X & BITMASK_DEBUG_OPTIONS) debugprint(__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
  #else
    #define DEBUGPRINT(X, ...)
  #endif
#endif

#define Seconds(x)		(((x) / ONESECOND) % ONEMINUTE)
#define Minutes(x)		(((x) / ONEMINUTE) % ONEMINUTE)
#define Hours(x)		(((x) / ONEHOUR) % 24)
#define Days(x)			(((x) / ONEDAY))
#define OneOrMore(x)		(((x) == 1) ? "" : "s")

#define StrMovePastToken(S, T)	\
				S = strchr(S, T); \
				if (S != NULL) { \
				  *S = '\0'; \
				  ++S; \
				  while (S[0] == T) { ++S; } \
				}

#define StrPrintErrorIfNull(S, F, L, M) \
				if ((S == NULL) || (S[0] == '\0')) { \
				  sysprint(BITMASK_ERROR, M, F, L); \
				  continue; \
				} \
				while ((S[0] == ' ') || (S[0] == '\t')) { ++S; }

/* VARAIBLES */

#if defined(LIBRARY)
  #define NOW time(NULL)
#else
  #if defined(MAIN_C) || defined(MKPASSWD_C)
    time_t NOW = 0;
    unsigned short int FakeLoop = TRUE;
  #else
    extern time_t NOW;
    extern unsigned short int FakeLoop;
  #endif
#endif


/* AERRNO DEFINES */

#define AESUCCESS					0
#define AEINVALID					1
#define AEEXISTS					2
#define AEMALLOC					3
#define AENOMATCH					4
#define AENORESOURCE					5
#define AEINPROGRESS					6


/* HOST DEFINES */

#define HOSTRESOLVING					1
#define HOSTTOIP					2
#define HOSTIPTONAME					4
#define HOSTNAMETOIP					8
#define HOSTRESOLVED					16


/* HOST MACROS */

#define Host_SetResolving(x)				((x) |= HOSTRESOLVING)
#define Host_SetHostToIP(x)				((x) |= HOSTTOIP)
#define Host_SetHostIPToName(x)				((x) |= HOSTIPTONAME)
#define Host_SetHostNameToIP(x)				((x) |= HOSTNAMETOIP)
#define Host_SetResolved(x)				((x) |= HOSTRESOLVED)

#define Host_ClearResolving(x)				((x) &= ~HOSTRESOLVING)
#define Host_ClearHostToIP(x)				((x) &= ~HOSTTOIP)
#define Host_ClearHostIPToName(x)			((x) &= ~HOSTIPTONAME)
#define Host_ClearHostNameToIP(x)			((x) &= ~HOSTNAMETOIP)
#define Host_ClearResolved(x)				((x) &= ~HOSTRESOLVED)

#define Host_IsResolving(x)				((x) & HOSTRESOLVING)
#define Host_IsHostToIP(x)				((x) & HOSTTOIP)
#define Host_IsHostIPToName(x)				((x) & HOSTIPTONAME)
#define Host_IsHostNameToIP(x)				((x) & HOSTNAMETOIP)
#define Host_IsResolved(x)				((x) & HOSTRESOLVED)

#endif


syntax highlighted by Code2HTML, v. 0.9.1