97f40f4
diff -up dhcp-3.1.0/omapip/dispatch.c.libdhcp4client dhcp-3.1.0/omapip/dispatch.c
97f40f4
--- dhcp-3.1.0/omapip/dispatch.c.libdhcp4client	2005-03-17 15:15:21.000000000 -0500
97f40f4
+++ dhcp-3.1.0/omapip/dispatch.c	2007-10-24 14:55:56.000000000 -0400
97f40f4
@@ -34,7 +34,7 @@
97f40f4
 
97f40f4
 #include <omapip/omapip_p.h>
97f40f4
 
97f40f4
-static omapi_io_object_t omapi_io_states;
97f40f4
+omapi_io_object_t omapi_io_states;
97f40f4
 TIME cur_time;
97f40f4
 
97f40f4
 OMAPI_OBJECT_ALLOC (omapi_io,
97f40f4
diff -up dhcp-3.1.0/omapip/errwarn.c.libdhcp4client dhcp-3.1.0/omapip/errwarn.c
97f40f4
--- dhcp-3.1.0/omapip/errwarn.c.libdhcp4client	2007-10-24 14:55:56.000000000 -0400
97f40f4
+++ dhcp-3.1.0/omapip/errwarn.c	2007-10-24 14:57:16.000000000 -0400
97f40f4
@@ -39,6 +39,11 @@ static char copyright[] =
97f40f4
 #include <omapip/omapip_p.h>
97f40f4
 #include <errno.h>
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+#include <isc-dhcp/libdhcp_control.h>
97f40f4
+extern LIBDHCP_Control *libdhcp_control;
97f40f4
+#endif
97f40f4
+
97f40f4
 #ifdef DEBUG
97f40f4
 int log_perror = -1;
97f40f4
 #else
97f40f4
@@ -48,7 +53,9 @@ int log_priority;
97f40f4
 void (*log_cleanup) (void);
97f40f4
 
97f40f4
 #define CVT_BUF_MAX 1023
97f40f4
+#ifndef LIBDHCP
97f40f4
 static char mbuf [CVT_BUF_MAX + 1];
97f40f4
+#endif
97f40f4
 static char fbuf [CVT_BUF_MAX + 1];
97f40f4
 
97f40f4
 /* Log an error message, then exit... */
97f40f4
@@ -58,6 +65,16 @@ void log_fatal (const char * fmt, ... )
97f40f4
   va_list list;
97f40f4
 
97f40f4
   do_percentm (fbuf, fmt);
97f40f4
+  
97f40f4
+#ifdef LIBDHCP
97f40f4
+  if (libdhcp_control && (libdhcp_control->eh)) {
97f40f4
+      va_start (list, fmt);
97f40f4
+      libdhcp_control->eh(libdhcp_control, LOG_FATAL, fbuf, list);
97f40f4
+      va_end(list);
97f40f4
+      libdhcp_control->finished = 1;
97f40f4
+      return;
97f40f4
+  }
97f40f4
+#else
97f40f4
 
97f40f4
   /* %Audit% This is log output. %2004.06.17,Safe%
97f40f4
    * If we truncate we hope the user can get a hint from the log.
97f40f4
@@ -91,6 +108,7 @@ void log_fatal (const char * fmt, ... )
97f40f4
   if (log_cleanup)
97f40f4
 	  (*log_cleanup) ();
97f40f4
   exit (1);
97f40f4
+#endif
97f40f4
 }
97f40f4
 
97f40f4
 /* Log an error message... */
97f40f4
@@ -101,6 +119,13 @@ int log_error (const char * fmt, ...)
97f40f4
 
97f40f4
   do_percentm (fbuf, fmt);
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+  if (libdhcp_control && libdhcp_control->eh) {
97f40f4
+      va_start (list, fmt);
97f40f4
+      libdhcp_control->eh(libdhcp_control, LOG_ERR, fbuf, list);
97f40f4
+      va_end(list);
97f40f4
+  }
97f40f4
+#else
97f40f4
   /* %Audit% This is log output. %2004.06.17,Safe%
97f40f4
    * If we truncate we hope the user can get a hint from the log.
97f40f4
    */
97f40f4
@@ -116,7 +141,7 @@ int log_error (const char * fmt, ...)
97f40f4
 	  write (STDERR_FILENO, mbuf, strlen (mbuf));
97f40f4
 	  write (STDERR_FILENO, "\n", 1);
97f40f4
   }
97f40f4
-
97f40f4
+#endif
97f40f4
   return 0;
97f40f4
 }
97f40f4
 
97f40f4
@@ -128,6 +153,13 @@ int log_info (const char *fmt, ...)
97f40f4
 
97f40f4
   do_percentm (fbuf, fmt);
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+  if (libdhcp_control && libdhcp_control->eh) {
97f40f4
+      va_start (list, fmt);
97f40f4
+      libdhcp_control->eh(libdhcp_control, LOG_INFO, fbuf, list);
97f40f4
+      va_end(list);
97f40f4
+  }
97f40f4
+#else
97f40f4
   /* %Audit% This is log output. %2004.06.17,Safe%
97f40f4
    * If we truncate we hope the user can get a hint from the log.
97f40f4
    */
97f40f4
@@ -143,7 +175,7 @@ int log_info (const char *fmt, ...)
97f40f4
 	  write (STDERR_FILENO, mbuf, strlen (mbuf));
97f40f4
 	  write (STDERR_FILENO, "\n", 1);
97f40f4
   }
97f40f4
-
97f40f4
+#endif
97f40f4
   return 0;
97f40f4
 }
97f40f4
 
97f40f4
@@ -154,7 +186,13 @@ int log_debug (const char *fmt, ...)
97f40f4
   va_list list;
97f40f4
 
97f40f4
   do_percentm (fbuf, fmt);
97f40f4
-
97f40f4
+#ifdef LIBDHCP
97f40f4
+  if (libdhcp_control && libdhcp_control->eh) {
97f40f4
+      va_start (list, fmt);
97f40f4
+      libdhcp_control->eh(libdhcp_control, LOG_DEBUG, fbuf, list);
97f40f4
+      va_end(list);
97f40f4
+  }
97f40f4
+#else
97f40f4
   /* %Audit% This is log output. %2004.06.17,Safe%
97f40f4
    * If we truncate we hope the user can get a hint from the log.
97f40f4
    */
97f40f4
@@ -170,7 +208,7 @@ int log_debug (const char *fmt, ...)
97f40f4
 	  write (STDERR_FILENO, mbuf, strlen (mbuf));
97f40f4
 	  write (STDERR_FILENO, "\n", 1);
97f40f4
   }
97f40f4
-
97f40f4
+#endif
97f40f4
   return 0;
97f40f4
 }
97f40f4
 
97f40f4
diff -up dhcp-3.1.0/omapip/alloc.c.libdhcp4client dhcp-3.1.0/omapip/alloc.c
97f40f4
--- dhcp-3.1.0/omapip/alloc.c.libdhcp4client	2006-02-24 18:16:30.000000000 -0500
97f40f4
+++ dhcp-3.1.0/omapip/alloc.c	2007-10-24 14:55:56.000000000 -0400
97f40f4
@@ -40,6 +40,33 @@ static char copyright[] =
97f40f4
 
97f40f4
 #include <omapip/omapip_p.h>
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+/* OK, we need a quick and dirty way of freeing all memory used by libdhcp. 
97f40f4
+   All pointers will be stored in a glibc tree on alloc, and removed on free.
97f40f4
+   This is not too expensive for light single-call library use.
97f40f4
+*/
97f40f4
+#include <search.h>  
97f40f4
+extern void tdestroy (void *root, void (*free_node)(void *nodep));
97f40f4
+static void *all_pointers=0L;
97f40f4
+static int ptr_comparator(const void *p1, const void *p2) {
97f40f4
+    return ((p1 == p2) ? 0 : ((p1 > p2) ? 1 : -1));
97f40f4
+}
97f40f4
+
97f40f4
+static void record_pointer(void *ptr) {
97f40f4
+    tsearch(ptr, &(all_pointers), ptr_comparator);
97f40f4
+}
97f40f4
+
97f40f4
+static void forget_pointer(void *ptr) {
97f40f4
+    tdelete(ptr, &(all_pointers), ptr_comparator);
97f40f4
+}
97f40f4
+
97f40f4
+void omapi_free_all_pointers(void) {
97f40f4
+    if (all_pointers != NULL)
97f40f4
+		tdestroy(all_pointers, free);
97f40f4
+    all_pointers = NULL;
97f40f4
+}
97f40f4
+#endif
97f40f4
+
97f40f4
 #if defined (DEBUG_MEMORY_LEAKAGE) || defined (DEBUG_MALLOC_POOL) || \
97f40f4
 		defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
97f40f4
 struct dmalloc_preamble *dmalloc_list;
97f40f4
@@ -78,7 +105,9 @@ VOIDPTR dmalloc (size, file, line)
97f40f4
 		return (VOIDPTR)0;
97f40f4
 
97f40f4
 	foo = malloc(len);
97f40f4
-
97f40f4
+#ifdef LIBDHCP
97f40f4
+	record_pointer(foo);
97f40f4
+#endif
97f40f4
 	if (!foo)
97f40f4
 		return (VOIDPTR)0;
97f40f4
 	bar = (VOIDPTR)(foo + DMDOFFSET);
97f40f4
@@ -200,6 +229,9 @@ void dfree (ptr, file, line)
97f40f4
 		     0, (unsigned char *)ptr + DMDOFFSET, 0, 1, RC_MALLOC);
97f40f4
 #endif
97f40f4
 	free (ptr);
97f40f4
+#ifdef LIBDHCP
97f40f4
+	forget_pointer(ptr);
97f40f4
+#endif
97f40f4
 }
97f40f4
 
97f40f4
 #if defined (DEBUG_MEMORY_LEAKAGE) || defined (DEBUG_MALLOC_POOL) || \
97f40f4
diff -up dhcp-3.1.0/configure.libdhcp4client dhcp-3.1.0/configure
97f40f4
--- dhcp-3.1.0/configure.libdhcp4client	2005-03-17 15:14:55.000000000 -0500
97f40f4
+++ dhcp-3.1.0/configure	2007-10-24 14:55:56.000000000 -0400
97f40f4
@@ -246,7 +246,7 @@ if [ ! -d $workname ]; then
97f40f4
 fi
97f40f4
 
97f40f4
 if [ x"$dirs" = x ]; then
97f40f4
-  dirs=". client server relay common omapip dhcpctl minires dst"
97f40f4
+  dirs=". client server relay common omapip dhcpctl minires dst libdhcp4client"
97f40f4
 fi
97f40f4
 
97f40f4
 for foo in $dirs; do
97f40f4
diff -up dhcp-3.1.0/dst/hmac_link.c.libdhcp4client dhcp-3.1.0/dst/hmac_link.c
97f40f4
--- dhcp-3.1.0/dst/hmac_link.c.libdhcp4client	2001-02-22 02:22:08.000000000 -0500
97f40f4
+++ dhcp-3.1.0/dst/hmac_link.c	2007-10-24 14:55:56.000000000 -0400
97f40f4
@@ -38,6 +38,10 @@ static const char rcsid[] = "$Header: /p
97f40f4
 
97f40f4
 #include "dst_internal.h"
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+extern void* dmalloc(size_t,char *,int);
97f40f4
+#endif
97f40f4
+
97f40f4
 #ifdef USE_MD5
97f40f4
 # include "md5.h"
97f40f4
 # ifndef _MD5_H_
97f40f4
@@ -86,7 +90,11 @@ dst_hmac_md5_sign(const int mode, DST_KE
97f40f4
 	MD5_CTX *ctx = NULL;
97f40f4
 
97f40f4
 	if (mode & SIG_MODE_INIT) 
97f40f4
+#ifdef LIBDHCP
97f40f4
+		ctx = (MD5_CTX *) dmalloc(sizeof(*ctx),__FILE__,__LINE__);
97f40f4
+#else
97f40f4
 		ctx = (MD5_CTX *) malloc(sizeof(*ctx));
97f40f4
+#endif
97f40f4
 	else if (context)
97f40f4
 		ctx = (MD5_CTX *) *context;
97f40f4
 	if (ctx == NULL) 
97f40f4
@@ -153,7 +161,11 @@ dst_hmac_md5_verify(const int mode, DST_
97f40f4
 	MD5_CTX *ctx = NULL;
97f40f4
 
97f40f4
 	if (mode & SIG_MODE_INIT) 
97f40f4
+#ifdef LIBDHCP
97f40f4
+		ctx = (MD5_CTX *) dmalloc(sizeof(*ctx),__FILE__,__LINE__);
97f40f4
+#else
97f40f4
 		ctx = (MD5_CTX *) malloc(sizeof(*ctx));
97f40f4
+#endif
97f40f4
 	else if (context)
97f40f4
 		ctx = (MD5_CTX *) *context;
97f40f4
 	if (ctx == NULL) 
97f40f4
@@ -217,8 +229,11 @@ dst_buffer_to_hmac_md5(DST_KEY *dkey, co
97f40f4
 
97f40f4
 	if (dkey == NULL || key == NULL || keylen < 0)
97f40f4
 		return (-1);
97f40f4
-
97f40f4
+#ifdef  LIBDHCP
97f40f4
+	if ((hkey = (HMAC_Key *) dmalloc(sizeof(HMAC_Key),__FILE__,__LINE__)) == NULL)
97f40f4
+#else
97f40f4
 	if ((hkey = (HMAC_Key *) malloc(sizeof(HMAC_Key))) == NULL)
97f40f4
+#endif
97f40f4
 		  return (-2);
97f40f4
 
97f40f4
 	memset(hkey->hk_ipad, 0, sizeof(hkey->hk_ipad));
97f40f4
@@ -347,7 +362,11 @@ dst_hmac_md5_key_from_file_format(DST_KE
97f40f4
 	if (eol == NULL)
97f40f4
 		return (-4);
97f40f4
 	len = eol - p;
97f40f4
+#ifdef LIBDHCP
97f40f4
+	tmp = dmalloc(len + 2,__FILE__,__LINE__);
97f40f4
+#else
97f40f4
 	tmp = malloc(len + 2);
97f40f4
+#endif
97f40f4
 	memcpy(tmp, p, len);
97f40f4
 	*(tmp + len) = 0x0;
97f40f4
 	key_len = b64_pton((char *)tmp, key, HMAC_LEN+1);	/* see above */
97f40f4
@@ -439,8 +458,11 @@ dst_hmac_md5_generate_key(DST_KEY *key, 
97f40f4
 		return(0);
97f40f4
 	
97f40f4
 	len = size > 64 ? 64 : size;
97f40f4
+#ifdef LIBDHCP
97f40f4
+	buff = dmalloc(len+8,__FILE__,__LINE__);
97f40f4
+#else
97f40f4
 	buff = malloc(len+8);
97f40f4
-
97f40f4
+#endif
97f40f4
 	n = dst_random(DST_RAND_SEMI, len, buff);
97f40f4
 	n += dst_random(DST_RAND_KEY, len, buff);
97f40f4
 	if (n <= len) {	/* failed getting anything */
97f40f4
@@ -463,7 +485,11 @@ dst_hmac_md5_init()
97f40f4
 {
97f40f4
 	if (dst_t_func[KEY_HMAC_MD5] != NULL)
97f40f4
 		return (1);
97f40f4
+#ifdef LIBDHCP
97f40f4
+	dst_t_func[KEY_HMAC_MD5] = dmalloc(sizeof(struct dst_func),__FILE__,__LINE__);
97f40f4
+#else
97f40f4
 	dst_t_func[KEY_HMAC_MD5] = malloc(sizeof(struct dst_func));
97f40f4
+#endif
97f40f4
 	if (dst_t_func[KEY_HMAC_MD5] == NULL)
97f40f4
 		return (0);
97f40f4
 	memset(dst_t_func[KEY_HMAC_MD5], 0, sizeof(struct dst_func));
97f40f4
diff -up dhcp-3.1.0/common/discover.c.libdhcp4client dhcp-3.1.0/common/discover.c
97f40f4
--- dhcp-3.1.0/common/discover.c.libdhcp4client	2006-11-07 18:41:39.000000000 -0500
97f40f4
+++ dhcp-3.1.0/common/discover.c	2007-10-24 14:55:56.000000000 -0400
97f40f4
@@ -121,6 +121,10 @@ isc_result_t interface_initialize (omapi
97f40f4
    register that interface with the network I/O software, figure out what
97f40f4
    subnet it's on, and add it to the list of interfaces. */
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+int have_setup_fallback = 0;
97f40f4
+#endif
97f40f4
+
97f40f4
 void discover_interfaces (state)
97f40f4
 	int state;
97f40f4
 {
97f40f4
@@ -141,7 +145,9 @@ void discover_interfaces (state)
97f40f4
 	char *s;
97f40f4
 #endif
97f40f4
 	isc_result_t status;
97f40f4
+#ifndef LIBDHCP
97f40f4
 	static int setup_fallback = 0;
97f40f4
+#endif
97f40f4
 	int wifcount = 0;
97f40f4
 
97f40f4
 	/* Create an unbound datagram socket to do the SIOCGIFADDR ioctl on. */
97f40f4
@@ -695,10 +701,17 @@ void discover_interfaces (state)
97f40f4
 		log_fatal ("Not configured to listen on any interfaces!");
97f40f4
 	}
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+	if (!have_setup_fallback) {
97f40f4
+		have_setup_fallback = 1;
97f40f4
+		maybe_setup_fallback ();
97f40f4
+	}
97f40f4
+#else
97f40f4
 	if (!setup_fallback) {
97f40f4
 		setup_fallback = 1;
97f40f4
 		maybe_setup_fallback ();
97f40f4
 	}
97f40f4
+#endif
97f40f4
 
97f40f4
 #if defined (HAVE_SETFD)
97f40f4
 	if (fallback_interface) {
97f40f4
diff -up dhcp-3.1.0/common/tree.c.libdhcp4client dhcp-3.1.0/common/tree.c
97f40f4
--- dhcp-3.1.0/common/tree.c.libdhcp4client	2007-02-14 17:41:22.000000000 -0500
97f40f4
+++ dhcp-3.1.0/common/tree.c	2007-10-24 14:55:56.000000000 -0400
97f40f4
@@ -41,7 +41,7 @@ static char copyright[] =
97f40f4
 #include <omapip/omapip_p.h>
97f40f4
 #include <ctype.h>
97f40f4
 
97f40f4
-struct binding_scope *global_scope;
97f40f4
+struct binding_scope __attribute__ ((visibility ("default"))) *global_scope;
97f40f4
 
97f40f4
 static int do_host_lookup PROTO ((struct data_string *,
97f40f4
 				  struct dns_host_entry *));
97f40f4
@@ -2761,6 +2761,7 @@ int evaluate_numeric_expression (result,
97f40f4
    result of that evaluation.   There should never be both an expression
97f40f4
    and a valid data_string. */
97f40f4
 
97f40f4
+__attribute__ ((visibility ("default")))
97f40f4
 int evaluate_option_cache (result, packet, lease, client_state,
97f40f4
 			   in_options, cfg_options, scope, oc, file, line)
97f40f4
 	struct data_string *result;
97f40f4
diff -up dhcp-3.1.0/common/options.c.libdhcp4client dhcp-3.1.0/common/options.c
97f40f4
--- dhcp-3.1.0/common/options.c.libdhcp4client	2007-05-23 15:26:22.000000000 -0400
97f40f4
+++ dhcp-3.1.0/common/options.c	2007-10-24 14:55:56.000000000 -0400
97f40f4
@@ -2501,6 +2501,7 @@ int fqdn_option_space_encapsulate (resul
97f40f4
 	return 1;
97f40f4
 }
97f40f4
 
97f40f4
+__attribute__ ((visibility ("default")))
97f40f4
 void option_space_foreach (struct packet *packet, struct lease *lease,
97f40f4
 			   struct client_state *client_state,
97f40f4
 			   struct option_state *in_options,
97f40f4
diff -up dhcp-3.1.0/common/dispatch.c.libdhcp4client dhcp-3.1.0/common/dispatch.c
97f40f4
--- dhcp-3.1.0/common/dispatch.c.libdhcp4client	2007-10-24 14:55:56.000000000 -0400
97f40f4
+++ dhcp-3.1.0/common/dispatch.c	2007-10-24 14:55:56.000000000 -0400
97f40f4
@@ -39,8 +39,24 @@ static char copyright[] =
97f40f4
 
97f40f4
 #include "dhcpd.h"
97f40f4
 
97f40f4
-struct timeout *timeouts;
97f40f4
-static struct timeout *free_timeouts;
97f40f4
+struct timeout {
97f40f4
+#ifndef LIBDHCP
97f40f4
+	struct timeout *next;
97f40f4
+#endif
97f40f4
+	TIME when;
97f40f4
+	void (*func) PROTO ((void *));
97f40f4
+	void *what;
97f40f4
+	tvref_t ref;
97f40f4
+	tvunref_t unref;
97f40f4
+};
97f40f4
+
97f40f4
+#ifdef LIBDHCP
97f40f4
+static struct timeout *timeouts = NULL;
97f40f4
+static int ntimeouts = 0;
97f40f4
+#else
97f40f4
+static struct timeout *timeouts = NULL;
97f40f4
+static struct timeout *free_timeouts = NULL;
97f40f4
+#endif
97f40f4
 
97f40f4
 void set_time(TIME t)
97f40f4
 {
97f40f4
@@ -53,9 +69,41 @@ void set_time(TIME t)
97f40f4
 
97f40f4
 struct timeval *process_outstanding_timeouts (struct timeval *tvp)
97f40f4
 {
97f40f4
+#ifdef LIBDHCP
97f40f4
+	int i;
97f40f4
+	struct timeout t = { 0 };
97f40f4
+#endif
97f40f4
 	/* Call any expired timeouts, and then if there's
97f40f4
 	   still a timeout registered, time out the select
97f40f4
 	   call then. */
97f40f4
+#ifdef LIBDHCP
97f40f4
+	if (!ntimeouts)
97f40f4
+		return NULL;
97f40f4
+
97f40f4
+	for (i = 0; i < ntimeouts && timeouts[i].when <= cur_time;) {
97f40f4
+		struct timeout *new_timeouts;
97f40f4
+		size_t n;
97f40f4
+
97f40f4
+		memmove(&t, &timeouts[i], sizeof (t));
97f40f4
+
97f40f4
+		n = (ntimeouts - i - 1) * sizeof (t);
97f40f4
+		memmove(&timeouts[i+1], &timeouts[i], n);
97f40f4
+
97f40f4
+		n = --ntimeouts * sizeof (t);
97f40f4
+		new_timeouts = realloc(timeouts, n);
97f40f4
+		/* XXX broken API, no way to return error here */
97f40f4
+		if (new_timeouts || !n)
97f40f4
+			timeouts = new_timeouts;
97f40f4
+
97f40f4
+		if (t.func)
97f40f4
+			t.func(t.what);
97f40f4
+		if (t.unref)
97f40f4
+			t.unref(t.what, MDL);
97f40f4
+	}
97f40f4
+	if (tvp && ntimeouts) {
97f40f4
+		tvp->tv_sec = timeouts[0].when;
97f40f4
+		tvp->tv_usec = 0;
97f40f4
+#else
97f40f4
       another:
97f40f4
 	if (timeouts) {
97f40f4
 		struct timeout *t;
97f40f4
@@ -73,9 +121,15 @@ struct timeval *process_outstanding_time
97f40f4
 			tvp -> tv_sec = timeouts -> when;
97f40f4
 			tvp -> tv_usec = 0;
97f40f4
 		}
97f40f4
+#endif
97f40f4
 		return tvp;
97f40f4
+#ifdef LIBDHCP
97f40f4
+	}
97f40f4
+	return NULL;
97f40f4
+#else
97f40f4
 	} else
97f40f4
 		return (struct timeval *)0;
97f40f4
+#endif
97f40f4
 }
97f40f4
 
97f40f4
 /* Wait for packets to come in using select().   When one does, call
97f40f4
@@ -104,13 +158,28 @@ void add_timeout (when, where, what, ref
97f40f4
 	tvref_t ref;
97f40f4
 	tvunref_t unref;
97f40f4
 {
97f40f4
+#ifdef LIBDHCP
97f40f4
+	struct timeout t = {
97f40f4
+		.when = when,
97f40f4
+		.func = where,
97f40f4
+		.what = what,
97f40f4
+		.ref = ref,
97f40f4
+		.unref = unref
97f40f4
+	};
97f40f4
+	struct timeout *new_timeouts;
97f40f4
+	int i, pos = 0;
97f40f4
+#else
97f40f4
 	struct timeout *t, *q;
97f40f4
+#endif
97f40f4
 
97f40f4
 	/* See if this timeout supersedes an existing timeout. */
97f40f4
+#ifdef LIBDHCP
97f40f4
+	for (i = 0; i < ntimeouts; i++) {
97f40f4
+		struct timeout *q = &timeouts[i];
97f40f4
+#else
97f40f4
 	t = (struct timeout *)0;
97f40f4
 	for (q = timeouts; q; q = q -> next) {
97f40f4
-		if ((where == NULL || q -> func == where) &&
97f40f4
-		    q -> what == what) {
97f40f4
+		if ((where == NULL || q -> func == where) && q -> what == what) {
97f40f4
 			if (t)
97f40f4
 				t -> next = q -> next;
97f40f4
 			else
97f40f4
@@ -119,7 +188,29 @@ void add_timeout (when, where, what, ref
97f40f4
 		}
97f40f4
 		t = q;
97f40f4
 	}
97f40f4
+#endif
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+		/* If this one is already in the list with a different time,
97f40f4
+		 * remove it and re-add */
97f40f4
+		if ((where == NULL || q->func == where) &&
97f40f4
+				q->what == what) {
97f40f4
+			size_t n = (--ntimeouts - i) * sizeof (*q);
97f40f4
+			memmove(&t, q, sizeof (t));
97f40f4
+
97f40f4
+			if (n)
97f40f4
+				memmove(&timeouts[i], &timeouts[i+1], n);
97f40f4
+
97f40f4
+			if (ntimeouts) {
97f40f4
+				new_timeouts = realloc(timeouts, ntimeouts * sizeof (*q));
97f40f4
+				/* XXX broken API, no way to return error here */
97f40f4
+				if (new_timeouts)
97f40f4
+					timeouts = new_timeouts;
97f40f4
+			} else {
97f40f4
+				timeouts = NULL;
97f40f4
+			}
97f40f4
+			add_timeout(when, where, what, ref, unref);
97f40f4
+#else
97f40f4
 	/* If we didn't supersede a timeout, allocate a timeout
97f40f4
 	   structure now. */
97f40f4
 	if (!q) {
97f40f4
@@ -128,7 +219,7 @@ void add_timeout (when, where, what, ref
97f40f4
 			free_timeouts = q -> next;
97f40f4
 		} else {
97f40f4
 			q = ((struct timeout *)
97f40f4
-			     dmalloc (sizeof (struct timeout), MDL));
97f40f4
+				dmalloc (sizeof (struct timeout), MDL));
97f40f4
 			if (!q)
97f40f4
 				log_fatal ("add_timeout: no memory!");
97f40f4
 		}
97f40f4
@@ -158,22 +249,76 @@ void add_timeout (when, where, what, ref
97f40f4
 		if (t -> next -> when > q -> when) {
97f40f4
 			q -> next = t -> next;
97f40f4
 			t -> next = q;
97f40f4
+#endif
97f40f4
 			return;
97f40f4
+#ifdef LIBDHCP
97f40f4
+		} else if (timeouts[i].when > when) {
97f40f4
+			pos = i;
97f40f4
+#endif
97f40f4
 		}
97f40f4
 	}
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+	/* If we didn't supersede an existing timeout, then pos is set
97f40f4
+	 * to the timeout which will post after this one.  Insert this
97f40f4
+	 * one before it. */
97f40f4
+
97f40f4
+	new_timeouts = realloc(timeouts, sizeof (t) * (ntimeouts+1));
97f40f4
+	/* XXX broken API, no way to return error here */
97f40f4
+	if (new_timeouts) {
97f40f4
+		/* ntimeouts = 10
97f40f4
+		 * pos = 3;
97f40f4
+		 * n = 10-3 * sizeof (t) = 7 * sizeof (t) 
97f40f4
+		 */
97f40f4
+		size_t n = (ntimeouts - pos) * sizeof (t);
97f40f4
+
97f40f4
+		timeouts = new_timeouts;
97f40f4
+		memmove(&timeouts[pos+1], &timeouts[pos], n);
97f40f4
+		memmove(&timeouts[pos], &t, sizeof (t));
97f40f4
+		ntimeouts++;
97f40f4
+	}
97f40f4
+#else
97f40f4
 	/* End of list. */
97f40f4
 	t -> next = q;
97f40f4
 	q -> next = (struct timeout *)0;
97f40f4
+#endif
97f40f4
 }
97f40f4
 
97f40f4
 void cancel_timeout (where, what)
97f40f4
 	void (*where) PROTO ((void *));
97f40f4
 	void *what;
97f40f4
 {
97f40f4
+#ifdef LIBDHCP
97f40f4
+	struct timeout t;
97f40f4
+	int i = 0;
97f40f4
+#else
97f40f4
 	struct timeout *t, *q;
97f40f4
+#endif
97f40f4
 
97f40f4
 	/* Look for this timeout on the list, and unlink it if we find it. */
97f40f4
+#ifdef LIBDHCP
97f40f4
+	for (i = 0; i < ntimeouts; i++) {
97f40f4
+		struct timeout *new_timeouts, *q = &timeouts[i];
97f40f4
+
97f40f4
+		if (q->func == where && q->what == what) {
97f40f4
+			size_t n;
97f40f4
+
97f40f4
+			memmove(&t, q, sizeof (t));
97f40f4
+
97f40f4
+			n = (ntimeouts - i - 1) * sizeof (t);
97f40f4
+			memmove(&timeouts[i+1], &timeouts[i], n);
97f40f4
+
97f40f4
+			n = --ntimeouts * sizeof (t);
97f40f4
+			new_timeouts = realloc(timeouts, n);
97f40f4
+			/* XXX broken API, no way to return error here */
97f40f4
+			if (new_timeouts || !n)
97f40f4
+				timeouts = new_timeouts;
97f40f4
+
97f40f4
+			if (t.unref)
97f40f4
+				t.unref(t.what, MDL);
97f40f4
+		}
97f40f4
+	}
97f40f4
+#else
97f40f4
 	t = (struct timeout *)0;
97f40f4
 	for (q = timeouts; q; q = q -> next) {
97f40f4
 		if (q -> func == where && q -> what == what) {
97f40f4
@@ -193,10 +338,15 @@ void cancel_timeout (where, what)
97f40f4
 		q -> next = free_timeouts;
97f40f4
 		free_timeouts = q;
97f40f4
 	}
97f40f4
+#endif
97f40f4
 }
97f40f4
 
97f40f4
 void cancel_all_timeouts ()
97f40f4
 {
97f40f4
+#ifdef LIBDHCP
97f40f4
+	cur_time = TIME_MAX;
97f40f4
+	process_outstanding_timeouts(NULL);
97f40f4
+#else
97f40f4
 	struct timeout *t, *n;
97f40f4
 	for (t = timeouts; t; t = n) {
97f40f4
 		n = t -> next;
97f40f4
@@ -205,13 +355,20 @@ void cancel_all_timeouts ()
97f40f4
 		t -> next = free_timeouts;
97f40f4
 		free_timeouts = t;
97f40f4
 	}
97f40f4
+#endif
97f40f4
 }
97f40f4
 
97f40f4
+__attribute__ ((visibility ("default")))
97f40f4
 void relinquish_timeouts ()
97f40f4
 {
97f40f4
+#ifdef LIBDHCP
97f40f4
+	while (ntimeouts)
97f40f4
+		cancel_timeout(timeouts[0].func, timeouts[0].what);
97f40f4
+#else
97f40f4
 	struct timeout *t, *n;
97f40f4
 	for (t = free_timeouts; t; t = n) {
97f40f4
 		n = t -> next;
97f40f4
 		dfree (t, MDL);
97f40f4
 	}
97f40f4
+#endif
97f40f4
 }
97f40f4
diff -up dhcp-3.1.0/common/alloc.c.libdhcp4client dhcp-3.1.0/common/alloc.c
97f40f4
--- dhcp-3.1.0/common/alloc.c.libdhcp4client	2006-06-01 16:23:17.000000000 -0400
97f40f4
+++ dhcp-3.1.0/common/alloc.c	2007-10-24 14:55:56.000000000 -0400
97f40f4
@@ -1013,7 +1013,11 @@ int executable_statement_reference (ptr,
97f40f4
 	return 1;
97f40f4
 }
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+struct packet *free_packets;
97f40f4
+#else
97f40f4
 static struct packet *free_packets;
97f40f4
+#endif
97f40f4
 
97f40f4
 #if defined (DEBUG_MEMORY_LEAKAGE) || \
97f40f4
 		defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
97f40f4
diff -up dhcp-3.1.0/includes/dhcpd.h.libdhcp4client dhcp-3.1.0/includes/dhcpd.h
97f40f4
--- dhcp-3.1.0/includes/dhcpd.h.libdhcp4client	2007-10-24 14:55:56.000000000 -0400
97f40f4
+++ dhcp-3.1.0/includes/dhcpd.h	2007-10-24 14:55:56.000000000 -0400
97f40f4
@@ -1000,14 +1000,6 @@ struct hardware_link {
97f40f4
 
97f40f4
 typedef void (*tvref_t)(void *, void *, const char *, int);
97f40f4
 typedef void (*tvunref_t)(void *, const char *, int);
97f40f4
-struct timeout {
97f40f4
-	struct timeout *next;
97f40f4
-	TIME when;
97f40f4
-	void (*func) PROTO ((void *));
97f40f4
-	void *what;
97f40f4
-	tvref_t ref;
97f40f4
-	tvunref_t unref;
97f40f4
-};
97f40f4
 
97f40f4
 struct protocol {
97f40f4
 	struct protocol *next;
97f40f4
@@ -1960,7 +1952,6 @@ extern void (*bootp_packet_handler) PROT
97f40f4
 					    struct dhcp_packet *, unsigned,
97f40f4
 					    unsigned int,
97f40f4
 					    struct iaddr, struct hardware *));
97f40f4
-extern struct timeout *timeouts;
97f40f4
 extern omapi_object_type_t *dhcp_type_interface;
97f40f4
 #if defined (TRACING)
97f40f4
 trace_type_t *interface_trace;
97f40f4
diff -up dhcp-3.1.0/client/dhclient.c.libdhcp4client dhcp-3.1.0/client/dhclient.c
97f40f4
--- dhcp-3.1.0/client/dhclient.c.libdhcp4client	2007-10-24 14:55:56.000000000 -0400
97f40f4
+++ dhcp-3.1.0/client/dhclient.c	2007-10-24 14:56:20.000000000 -0400
97f40f4
@@ -82,14 +82,50 @@ int quiet=0;
97f40f4
 int nowait=0;
97f40f4
 int bootp_broadcast_always = 0;
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+FILE *leaseFile = NULL;
97f40f4
+#endif
97f40f4
+
97f40f4
 extern u_int32_t default_requested_options[];
97f40f4
 
97f40f4
 static void usage PROTO ((void));
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+#include "isc-dhcp/libdhcp_control.h"
97f40f4
+LIBDHCP_Control *libdhcp_control;
97f40f4
+static void libdhcp_dispatch(void)
97f40f4
+{
97f40f4
+	struct timeval tv = { 0, 0 }, *tvp;
97f40f4
+	isc_result_t status;
97f40f4
+
97f40f4
+	/* Wait for a packet, or a timeout, or libdhcp being finished */
97f40f4
+	do {
97f40f4
+		tvp = process_outstanding_timeouts(&tv;;
97f40f4
+		status = omapi_one_dispatch(0, tvp);
97f40f4
+
97f40f4
+		if (libdhcp_control && ((status == ISC_R_TIMEDOUT) || (libdhcp_control->timeout && (time(NULL) >= (libdhcp_control->timeout + libdhcp_control->now))))) {
97f40f4
+			if (libdhcp_control->callback)
97f40f4
+				libdhcp_control->callback(libdhcp_control, DHC_TIMEDOUT, NULL);
97f40f4
+
97f40f4
+			break;
97f40f4
+		}
97f40f4
+	} while ((status != ISC_R_TIMEDOUT) && ((!libdhcp_control) || (!(libdhcp_control->finished))));
97f40f4
+}
97f40f4
+
97f40f4
+extern void omapi_free_all_pointers(void);
97f40f4
+
97f40f4
+__attribute__ ((visibility ("default")))
97f40f4
+int dhcpv4_client (libdhcp_ctl, argc, argv, envp)
97f40f4
+	LIBDHCP_Control *libdhcp_ctl;
97f40f4
+#else
97f40f4
 int main (argc, argv, envp)
97f40f4
+#endif
97f40f4
 	int argc;
97f40f4
 	char **argv, **envp;
97f40f4
 {
97f40f4
+#ifdef LIBDHCP
97f40f4
+	libdhcp_control = libdhcp_ctl;
97f40f4
+#endif
97f40f4
 	int fd;
97f40f4
 	int i;
97f40f4
 	struct servent *ent;
97f40f4
@@ -120,6 +156,7 @@ int main (argc, argv, envp)
97f40f4
 	char *arg_conf = NULL;
97f40f4
 	int arg_conf_len = 0;
97f40f4
 
97f40f4
+#ifndef LIBDHCP
97f40f4
         /* Make sure that file descriptors 0 (stdin), 1, (stdout), and
97f40f4
            2 (stderr) are open. To do this, we assume that when we
97f40f4
            open a file the lowest available file decriptor is used. */
97f40f4
@@ -143,6 +180,7 @@ int main (argc, argv, envp)
97f40f4
 #if !(defined (DEBUG) || defined (SYSLOG_4_2) || defined (__CYGWIN32__))
97f40f4
 	setlogmask (LOG_UPTO (LOG_INFO));
97f40f4
 #endif	
97f40f4
+#endif
97f40f4
 
97f40f4
 	/* Set up the OMAPI. */
97f40f4
 	status = omapi_init ();
97f40f4
@@ -439,8 +477,12 @@ int main (argc, argv, envp)
97f40f4
 		}
97f40f4
 	}
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+	if (libdhcp_control && (libdhcp_control->capability & DHCP_USE_PID_FILE))
97f40f4
+#endif
97f40f4
 	write_client_pid_file();
97f40f4
 
97f40f4
+#ifndef LIBDHCP
97f40f4
 	if (!quiet) {
97f40f4
 		log_info ("%s %s", message, DHCP_VERSION);
97f40f4
 		log_info (copyright);
97f40f4
@@ -449,6 +491,7 @@ int main (argc, argv, envp)
97f40f4
 		log_info ("%s", "");
97f40f4
 	} else
97f40f4
 		log_perror = 0;
97f40f4
+#endif
97f40f4
 
97f40f4
 	/* If we're given a relay agent address to insert, for testing
97f40f4
 	   purposes, figure out what it is. */
97f40f4
@@ -680,11 +723,17 @@ int main (argc, argv, envp)
97f40f4
 		arg_conf_len = 0;
97f40f4
 	}
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+	if (libdhcp_control && (libdhcp_control->capability & DHCP_USE_LEASE_DATABASE)) {
97f40f4
+#endif
97f40f4
 	/* Parse the lease database. */
97f40f4
 	read_client_leases ();
97f40f4
 
97f40f4
 	/* Rewrite the lease database... */
97f40f4
 	rewrite_client_leases ();
97f40f4
+#ifdef LIBDHCP
97f40f4
+	}
97f40f4
+#endif
97f40f4
 
97f40f4
 	/* XXX */
97f40f4
 /* 	config_counter(&snd_counter, &rcv_counter); */
97f40f4
@@ -703,7 +752,7 @@ int main (argc, argv, envp)
97f40f4
 		if (!persist) {
97f40f4
 			/* Nothing more to do. */
97f40f4
 			log_info ("No broadcast interfaces found - exiting.");
97f40f4
-			exit (0);
97f40f4
+			return (0);
97f40f4
 		}
97f40f4
 	} else if (!release_mode && !exit_mode) {
97f40f4
 		/* Call the script with the list of interfaces. */
97f40f4
@@ -799,6 +848,7 @@ int main (argc, argv, envp)
97f40f4
 	dmalloc_outstanding = 0;
97f40f4
 #endif
97f40f4
 
97f40f4
+#ifndef LIBDHCP
97f40f4
 	/* If we're not supposed to wait before getting the address,
97f40f4
 	   don't. */
97f40f4
 	if (nowait)
97f40f4
@@ -811,6 +861,126 @@ int main (argc, argv, envp)
97f40f4
 
97f40f4
 	/* Start dispatching packets and timeouts... */
97f40f4
 	dispatch ();
97f40f4
+#else
97f40f4
+	if (libdhcp_control) {
97f40f4
+		if (libdhcp_control->timeout)
97f40f4
+			libdhcp_control->now = time(NULL);
97f40f4
+		else
97f40f4
+			libdhcp_control->now = 0;
97f40f4
+	}
97f40f4
+
97f40f4
+	libdhcp_dispatch();
97f40f4
+
97f40f4
+	/* libdhcp is finished with us. */
97f40f4
+
97f40f4
+	/* close all file descriptors:  */
97f40f4
+	for (ip = interfaces; ip; ip = ip->next) {
97f40f4
+		shutdown(ip->wfdesc, SHUT_RDWR);
97f40f4
+		close(ip->wfdesc);
97f40f4
+
97f40f4
+		if (ip->rfdesc != ip->wfdesc)
97f40f4
+			close(ip->rfdesc);
97f40f4
+	}
97f40f4
+
97f40f4
+	if (fallback_interface != 0) {
97f40f4
+		ip = fallback_interface;
97f40f4
+		shutdown(ip->wfdesc, SHUT_RDWR);
97f40f4
+		close(ip->wfdesc);
97f40f4
+
97f40f4
+		if (ip->rfdesc != ip->wfdesc)
97f40f4
+			close(ip->rfdesc);
97f40f4
+	}
97f40f4
+
97f40f4
+	if (leaseFile)
97f40f4
+		fclose (leaseFile);
97f40f4
+
97f40f4
+	closelog();
97f40f4
+
97f40f4
+	char *current_pid_file = _PATH_DHCLIENT_PID;
97f40f4
+
97f40f4
+	/* Free ALL allocated memory: */
97f40f4
+	omapi_free_all_pointers();
97f40f4
+
97f40f4
+	/* Re-Initialize globals: */
97f40f4
+	client_env = 0;
97f40f4
+	client_env_count = 0;
97f40f4
+	default_lease_time = 43200;
97f40f4
+
97f40f4
+	dhcp_max_agent_option_packet_length = 0;
97f40f4
+	iaddr_any.len = 4;
97f40f4
+	memset(&(iaddr_any.iabuf[0]), '\0', 4);
97f40f4
+	iaddr_broadcast.len = 4;
97f40f4
+	memset(&(iaddr_broadcast.iabuf[0]), 0xff, 4);
97f40f4
+	interfaces_requested = 0;
97f40f4
+	leaseFile = 0;
97f40f4
+
97f40f4
+	libdhcp_control = 0;
97f40f4
+
97f40f4
+	local_port = 0;
97f40f4
+	no_daemon = 0;
97f40f4
+	nowait = 0;
97f40f4
+	onetry = 0;
97f40f4
+	quiet = 0;
97f40f4
+	max_lease_time = 86400;
97f40f4
+	path_dhclient_conf = _PATH_DHCLIENT_CONF;
97f40f4
+	path_dhclient_db = _PATH_DHCLIENT_DB;
97f40f4
+	path_dhclient_pid = _PATH_DHCLIENT_PID;
97f40f4
+	strcpy(&(path_dhclient_script_array[0]), _PATH_DHCLIENT_SCRIPT);
97f40f4
+	path_dhclient_script = path_dhclient_script_array;
97f40f4
+	remote_port = 0;
97f40f4
+	resolver_inited = 0;
97f40f4
+	log_perror = 1;
97f40f4
+	global_scope = NULL;
97f40f4
+	root_group = NULL;
97f40f4
+	group_name_hash = NULL;
97f40f4
+	interfaces = NULL;
97f40f4
+	dummy_interfaces = NULL;
97f40f4
+	fallback_interface = NULL;
97f40f4
+	extern int have_setup_fallback;
97f40f4
+	have_setup_fallback = 0;
97f40f4
+	quiet_interface_discovery = 1;
97f40f4
+#ifndef LIBDHCP
97f40f4
+	timeouts = NULL;
97f40f4
+#endif
97f40f4
+	dhcp_type_interface = NULL;
97f40f4
+	interface_vector = NULL;
97f40f4
+	interface_count = 0;
97f40f4
+	interface_max = 0;
97f40f4
+	name_servers = 0;
97f40f4
+	domains = 0;
97f40f4
+	dhcp_type_interface = NULL;
97f40f4
+	dhcp_type_group = NULL;
97f40f4
+	dhcp_type_shared_network = NULL;
97f40f4
+	dhcp_type_control = NULL;
97f40f4
+	memset(&dhcp_universe, '\0', sizeof(struct universe));
97f40f4
+	memset(&nwip_universe, '\0', sizeof(struct universe));
97f40f4
+	memset(&fqdn_universe, '\0', sizeof(struct universe));
97f40f4
+	universe_hash = 0;
97f40f4
+	universes = 0;
97f40f4
+	universe_count = 0;
97f40f4
+	universe_max = 0;
97f40f4
+	config_universe = 0; 
97f40f4
+	extern struct hash_bucket *free_hash_buckets;
97f40f4
+	free_hash_buckets = NULL;
97f40f4
+	extern struct dhcp_packet *dhcp_free_list;
97f40f4
+	dhcp_free_list = NULL;
97f40f4
+	extern struct packet *packet_free_list;
97f40f4
+	packet_free_list = NULL;
97f40f4
+	extern struct binding_value *free_binding_values;
97f40f4
+	free_binding_values = NULL;
97f40f4
+	extern struct expression *free_expressions;
97f40f4
+	free_expressions = NULL;
97f40f4
+	extern struct option_cache *free_option_caches;
97f40f4
+	free_option_caches = NULL;
97f40f4
+	extern  struct packet *free_packets;
97f40f4
+	free_packets = NULL;
97f40f4
+	extern  pair free_pairs;
97f40f4
+	free_pairs = NULL;
97f40f4
+	extern omapi_io_object_t omapi_io_states;
97f40f4
+	memset(&omapi_io_states, '\0', sizeof(omapi_io_states));
97f40f4
+	dhcp_control_object = NULL;
97f40f4
+	unlink(current_pid_file);
97f40f4
+#endif
97f40f4
 
97f40f4
 	/*NOTREACHED*/
97f40f4
 	return 0;
97f40f4
@@ -1203,7 +1373,20 @@ void dhcpack (packet)
97f40f4
 	if (client -> new -> rebind < cur_time)
97f40f4
 		client -> new -> rebind = TIME_MAX;
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+	/* We need the server's siaddr for the 'bootServer'
97f40f4
+	 * pump option
97f40f4
+	 */
97f40f4
+	u_int32_t set_siaddr = 0;
97f40f4
+	set_siaddr = client->packet.siaddr.s_addr;
97f40f4
+	client->packet.siaddr.s_addr = packet->raw->siaddr.s_addr;
97f40f4
+#endif
97f40f4
+
97f40f4
 	bind_lease (client);
97f40f4
+
97f40f4
+#ifdef LIBDHCP
97f40f4
+	client->packet.siaddr.s_addr = set_siaddr;
97f40f4
+#endif
97f40f4
 }
97f40f4
 
97f40f4
 void bind_lease (client)
97f40f4
@@ -1241,6 +1424,9 @@ void bind_lease (client)
97f40f4
 		return;
97f40f4
 	}
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+	if (libdhcp_control && (libdhcp_control->capability & DHCP_USE_LEASE_DATABASE))
97f40f4
+#endif
97f40f4
 	/* Write out the new lease. */
97f40f4
 	write_client_lease (client, client -> new, 0, 0);
97f40f4
 
97f40f4
@@ -1343,11 +1529,13 @@ int commit_leases ()
97f40f4
 	return 0;
97f40f4
 }
97f40f4
 
97f40f4
+#ifndef LIBDHCP
97f40f4
 int write_lease (lease)
97f40f4
 	struct lease *lease;
97f40f4
 {
97f40f4
 	return 0;
97f40f4
 }
97f40f4
+#endif
97f40f4
 
97f40f4
 int write_host (host)
97f40f4
 	struct host_decl *host;
97f40f4
@@ -1957,6 +2145,10 @@ void state_panic (cpp)
97f40f4
 	   tell the shell script that we failed to allocate an address,
97f40f4
 	   and try again later. */
97f40f4
 	if (onetry) {
97f40f4
+#ifdef LIBDHCP
97f40f4
+		script_init (client, "FAIL", (struct string_list *)0);
97f40f4
+		return;
97f40f4
+#endif
97f40f4
 		if (!quiet)
97f40f4
 			log_info ("Unable to obtain a lease on first try.%s",
97f40f4
 				  "  Exiting.");
97f40f4
@@ -2579,7 +2771,9 @@ void destroy_client_lease (lease)
97f40f4
 	free_client_lease (lease, MDL);
97f40f4
 }
97f40f4
 
97f40f4
+#ifndef LIBDHCP
97f40f4
 FILE *leaseFile;
97f40f4
+#endif
97f40f4
 
97f40f4
 void rewrite_client_leases ()
97f40f4
 {
97f40f4
@@ -2960,6 +3154,54 @@ void script_write_params (client, prefix
97f40f4
 int script_go (client)
97f40f4
 	struct client_state *client;
97f40f4
 {
97f40f4
+#ifdef LIBDHCP
97f40f4
+	struct string_list *sp;
97f40f4
+
97f40f4
+	if (libdhcp_control && libdhcp_control->callback) {
97f40f4
+		int dhcmsg;
97f40f4
+		char *reason="";
97f40f4
+
97f40f4
+		for (sp = client->env; sp; sp = sp->next)
97f40f4
+			if (strncmp(sp->string, "reason=", 7) == 0) {
97f40f4
+				reason = sp->string + 7;
97f40f4
+				break;
97f40f4
+			}
97f40f4
+
97f40f4
+		if (strcmp(reason,"NBI") == 0)
97f40f4
+			dhcmsg = DHC4_NBI;
97f40f4
+		else if (strcmp(reason,"PREINIT") == 0)
97f40f4
+			dhcmsg = DHC4_PREINIT;
97f40f4
+		else if (strcmp(reason,"BOUND") == 0)
97f40f4
+			dhcmsg = DHC4_BOUND;
97f40f4
+		else if (strcmp(reason,"RENEW") == 0)
97f40f4
+			dhcmsg = DHC4_RENEW;
97f40f4
+		else if (strcmp(reason,"REBOOT") == 0)
97f40f4
+			dhcmsg = DHC4_REBOOT;
97f40f4
+		else if (strcmp(reason,"REBIND") == 0)
97f40f4
+			dhcmsg = DHC4_REBIND;
97f40f4
+		else if (strcmp(reason,"STOP") == 0)
97f40f4
+			dhcmsg = DHC4_STOP;
97f40f4
+		else if (strcmp(reason,"MEDIUM") == 0)
97f40f4
+			dhcmsg = DHC4_MEDIUM;
97f40f4
+		else if (strcmp(reason,"TIMEOUT") == 0)
97f40f4
+			dhcmsg = DHC4_TIMEOUT;
97f40f4
+		else if (strcmp(reason,"FAIL") == 0)
97f40f4
+			dhcmsg = DHC4_FAIL;
97f40f4
+		else if (strcmp(reason,"EXPIRE") == 0)
97f40f4
+			dhcmsg = DHC4_EXPIRE;
97f40f4
+		else if (strcmp(reason,"RELEASE") == 0)
97f40f4
+			dhcmsg = DHC4_RELEASE;
97f40f4
+		else
97f40f4
+			dhcmsg = DHC4_NBI;
97f40f4
+
97f40f4
+		(*libdhcp_control->callback) (libdhcp_control, dhcmsg, client);
97f40f4
+
97f40f4
+		if (libdhcp_control->decline)
97f40f4
+			return 1;
97f40f4
+	}
97f40f4
+
97f40f4
+	return 0;
97f40f4
+#else
97f40f4
 	int rval;
97f40f4
 	char *scriptName;
97f40f4
 	char *argv [2];
97f40f4
@@ -3038,6 +3280,7 @@ int script_go (client)
97f40f4
 	GET_TIME (&cur_time);
97f40f4
 	return (WIFEXITED (wstatus) ?
97f40f4
 		WEXITSTATUS (wstatus) : -WTERMSIG (wstatus));
97f40f4
+#endif
97f40f4
 }
97f40f4
 
97f40f4
 void client_envadd (struct client_state *client,
97f40f4
@@ -3120,6 +3363,9 @@ void go_daemon ()
97f40f4
 
97f40f4
 	/* Don't become a daemon if the user requested otherwise. */
97f40f4
 	if (no_daemon) {
97f40f4
+#ifdef LIBDHCP
97f40f4
+		if (libdhcp_control && (libdhcp_control->capability & DHCP_USE_PID_FILE ))
97f40f4
+#endif
97f40f4
 		write_client_pid_file ();
97f40f4
 		return;
97f40f4
 	}
97f40f4
@@ -3129,6 +3375,10 @@ void go_daemon ()
97f40f4
 		return;
97f40f4
 	state = 1;
97f40f4
 
97f40f4
+#ifdef LIBDHCP
97f40f4
+	return;
97f40f4
+#endif
97f40f4
+
97f40f4
 	/* Stop logging to stderr... */
97f40f4
 	log_perror = 0;
97f40f4
 
97f40f4
diff -up dhcp-3.1.0/Makefile.dist.libdhcp4client dhcp-3.1.0/Makefile.dist
97f40f4
--- dhcp-3.1.0/Makefile.dist.libdhcp4client	2005-03-17 15:14:54.000000000 -0500
97f40f4
+++ dhcp-3.1.0/Makefile.dist	2007-10-24 14:55:56.000000000 -0400
97f40f4
@@ -22,7 +22,7 @@
97f40f4
 #   http://www.isc.org/
97f40f4
 
97f40f4
 
97f40f4
-SUBDIRS=	common $(MINIRES) dst omapip server client relay dhcpctl
97f40f4
+SUBDIRS=	common $(MINIRES) dst omapip server client relay dhcpctl libdhcp4client
97f40f4
 
97f40f4
 all:
97f40f4
 	@for dir in ${SUBDIRS}; do \