/************************************************************************
          Copyright 1988, 1991 by Carnegie Mellon University

                          All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation, and that the name of Carnegie Mellon University not be used
in advertising or publicity pertaining to distribution of the software
without specific, written prior permission.

CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
************************************************************************/


/*
 * bootpd.h -- common header file for all the modules of the bootpd program.
 */

#ifndef BOOTPD_H_
#define BOOTPD_H_
#include "hash.h"
#include "hashf.h"
#include "hwaddr.h"

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

#ifndef PRIVATE
#define PRIVATE static
#endif

#ifndef SIGUSR1
#define SIGUSR1			 30	/* From 4.3 <signal.h> */
#endif

#define MAXSTRINGLEN		 80	/* Max string length */
#define MAX_MSG_SIZE		(3*512) /* Maximum packet size */
#define MAX_INETADDR_STRING_LEN	64		/* Max length of inet addr written in dotted decimal notation, incl trailing null */
#define MAX_FQDN_LEN		255

#define PROTO_FLAVOR_BOOTP 1
#define PROTO_FLAVOR_DHCP  2

/* Local definitions: */


/*
 * Return pointer to static string which gives full network error message.
 */
#define get_network_errmsg get_errmsg


/*
 * Data structure used to hold an arbitrary-lengthed list of IP addresses.
 * The list may be shared among multiple hosts by setting the linkcount
 * appropriately.
 */

struct in_addr_list {
    unsigned int	linkcount, addrcount;
    struct in_addr	addr[1];		/* Dynamically extended */
};


/*
 * Data structures used to hold shared strings and shared binary data.
 * The linkcount must be set appropriately.
 */

struct shared_string {
    unsigned int	linkcount;
    char		string[1];		/* Dynamically extended */
};

struct shared_bindata {
    unsigned int	linkcount, length;
    byte		data[1];		/* Dynamically extended */
};
/*	A "flat" version of a struct shared_bindata is an array of unsigned char consisting of each of the members
	of a struct lastbind (in the order above) EXCEPT for linkcount.  Any holes in the
	struct shared_bindata are not present in the  flat version.
	As it happens, we only use shared_bindata structs to store information that from sources that limit 
	the the data field so that its length can always fit into one byte.  (E.g. often it comes from
	a vendor options field that has a a one-byte length field followed by a data field.) 
	Knowing this tells us the maximum size a flattened struct shared_bindata may be.
*/
#define SHARED_BINDATA_FLAT_MAXLEN  (sizeof(unsigned int) + UCHAR_MAX)


/*
 * Flag structure containing one bit per vendor option.
 */

struct flag {
    unsigned	bootfile	:1,
		bootserver	:1,
		bootsize	:1,
		bootsize_auto	:1,
		cookie_server	:1,
		domain_server	:1,
		gateway		:1,
		generic		:1,
		haddr		:1,
		homedir		:1,
		htype		:1,
		impress_server	:1,
		iaddr		:1,
		log_server	:1,
		lpr_server	:1,
		name_server	:1,
		name_switch	:1,
		rlp_server	:1,
		send_name	:1,
		subnet_mask	:1,
		tftpdir		:1,
		time_offset	:1,
		time_server	:1,
		dump_file	:1,
		domain_name	:1,
		swap_server	:1,
		root_path	:1,
		exten_file	:1,
		reply_addr	:1,
		nis_domain	:1,
		nis_server	:1,
		ntp_server	:1,
		exec_file	:1,
		msg_size	:1,
		min_wait	:1,
		nbns_server :1,
		nb_scope :1,
		node_type	:1,
		dynamic		:1,
		roaming		:1,
		lease	:1,	
		expire :1,	
		expire_prior_to_offer	:1,
		renew	:1,	
		rebind	:1,
		cliid		:1,
		complete		:1,
		not_rq	:1,
		cable :1,
		max_lease :1,
		vm_cookie	:1,
		broadcast_addr	:1,
		deny_client	:1,
		perform_router_discovery :1,
		static_route :1,
		client_fqdn :1,
		deny_client_dynamic :1,
		ethernet_encapsulation :1,
		nbdd_server :1,
		smtp_server :1,
		pop3_server :1,
		nntp_server :1,
		www_server :1,
		finger_server :1,
		irc_server :1,
		bootp_exclude :1, /* string version from bootptab */
		bootp_exclude_vector :1, /* bit vector version resulting from parsing string version */
		bootp_include :1, /* string version from bootptab */
		bootp_include_vector :1, /* bit vector version resulting from parsing string version */
		ip_forwarding :1,
		ip_source_routing :1,
		ip_ttl :1,
		all_subnets_are_local :1,
		perform_mask_discovery :1,
		mask_supplier :1,
		router_solicitation_addr :1,
		trailer_encapsulation :1,
		tcp_ttl :1,
		tcp_keepalive_garbage :1,
		xwindows_font_server :1,
		xwindows_display_manager :1,
		nisplus_domain :1,
		nisplus_server :1,
		offerred_but_not_yet_accepted_by_client :1; /* XXX we fail to preserve this in on-disk version of lease. */
		/* XXX - Add new tags here */
};



/*
 * In most cases, the flags element contains TRUE flags for all the fields which
 * are considered valid, regardless of whether they were explicitly
 * specified or indirectly inferred from another entry.
 * (There are some flags that aren't associated with fields, but are just other
 * booleans we need to carry along as part of the lease.)
 *
 * The gateway and the various server fields all point to a shared list of
 * IP addresses.
 *
 * The hostname, home directory, and bootfile are all shared strings.
 *
 * The generic data field is a shared binary data structure.  It is used to
 * hold future RFC1048 vendor data until bootpd is updated to understand it.
 *
 * The vm_cookie field specifies the four-octet vendor magic cookie to use
 * if it is desired to always send the same response to a given host.
 *
 * Hopefully, the rest is self-explanatory.
 */

struct host {
    unsigned		    linkcount;		/* hash list inserts */
    struct flag		    flags;		/* flags indicates which symbols have been defined (given values) for a given host. */
	unsigned			iphashcode;
	struct in_addr_list	    *cookie_server,
			    *domain_server,
			    *gateway,
			    *impress_server,
			    *log_server,
			    *lpr_server,
			    *name_server,
			    *rlp_server,
			    *time_server,
			    *nis_server,
				*nbns_server,
			    *ntp_server,
			    *static_route,
			    *nbdd_server,
			    *smtp_server,
			    *pop3_server,
			    *nntp_server,
			    *www_server,
			    *finger_server,
			    *irc_server,
			    *xwindows_font_server,
			    *xwindows_display_manager,
			    *nisplus_server;
    struct shared_string    *bootfile,
			    *hostname,
			    *domain_name,
			    *homedir,
			    *tftpdir,
			    *dump_file,
			    *exten_file,
			    *root_path,
			    *nis_domain,
			    *nb_scope,
				*cable,
			    *exec_file,
				*bootp_exclude, /* string version from bootptab */
				*bootp_include, /* string version from bootptab */
				*nisplus_domain;
    struct shared_bindata   *cl,
				*cliid,
				*generic,
				*bootp_exclude_vector, /* data points to bit vector version resulting from parsing bootp_exclude */
				*bootp_include_vector; /* data points to bit vector version resulting from parsing bootp_include */
	byte		code,node_type;
    byte		    vm_cookie[4],
			    htype,  /* RFC826 says this should be 16-bits but
				       RFC951 only allocates 1 byte. . . */
			    haddr[MAXHADDRLEN];
    int32		    time_offset,
		renew	,							/* absolute time that the lease reaches renew time (T1) */
		rebind	;							/* absolute time that the lease reaches rebind time (T2) */
	unsigned short	xid;
    unsigned int32	    bootsize,
			    msg_size,
				expire,						/* absolute time that the lease (or offer) expires/
											   This isn't sent to the client; it's needed by the server so the server
											   server knows when the lease expires.
											*/
				expire_prior_to_offer,		/* Value of 'expire' field prior to our converting an
											   unexpired lease into an offer, since doing so overwrites 'expire'.
											   Used to let us convert an unselected offer back to the original
											   lease (restore the original expiration time).
											*/
				max_lease,
			    min_wait;
    unsigned int32	    lease;				/* lease duration in seconds.  Not an absolute time.  This is also sent to the client.  */
    struct in_addr	    bootserver,
			    iaddr,
			    swap_server,
			    reply_addr,
			    subnet_mask,
				broadcast_addr,
				router_solicitation_addr;
	byte	perform_router_discovery,
			client_fqdn,
			ethernet_encapsulation,
			ip_forwarding,
			ip_source_routing,
			ip_ttl,
			all_subnets_are_local,
			perform_mask_discovery,
			mask_supplier,
			trailer_encapsulation,
			tcp_ttl,
			tcp_keepalive_garbage;
    /* XXX - Add new tags here (or above as appropriate) */
};



/*
 * Variables shared among modules.
 */

extern int debug;
extern int do_readtab;
extern int do_readconf;
extern int do_flush;
extern char *bootptab;
extern char *bootpd_dump;
extern char *bootpd_dumpbind;
extern char *bootpd_dumpstats;
extern char *conffile;
extern char *bindingsdir;
extern char *pidfile;
extern char *progname;
extern boolean dont_fork;
extern boolean renew_unbound_statics;
extern int log_assignments;
extern boolean log_expirations;
extern boolean compact_messages;
extern boolean ignore_cliid_option;
extern int client_steering_hash_source;
extern int client_steering_hash_function;
extern boolean cliid_must_match_chaddr;
extern boolean check_file_access;
extern boolean allow_serveripopts_in_rebind;
extern boolean enable_snmp_agent;
extern boolean lone_dhcp_server;
extern boolean offer_terminates_unexpired_lease;
extern boolean log_lease_offer_conversions;
extern uid_t runas_uid;
extern gid_t runas_gid;
extern boolean princeton_log_inits_with_unexpired_lease;
extern boolean princeton_log_lease_offer_conversions;
extern boolean princeton_log_client_steering;

extern u_char vm_cmu[4];
extern u_char vm_rfc1048[4];

extern hash_tbl *hwhashtable;
extern hash_tbl *iphashtable;
extern hash_tbl *nmhashtable;
extern hash_tbl *clhashtable;
extern struct in_addr my_ip_addr;
extern unsigned int bootp_requests_received;
extern unsigned int bootp_replys_sent;
extern unsigned long starttime;
extern char *version;
extern int init;
extern int pktlen;
extern int interface_max;		/* index of last interface */
extern u_char enet_all_zeros[6];    /* the all-zeros enet address */
extern u_char htype_ethernet;	/* the IANA hardware type for 10MB Ethernet */

#endif
