bug fix: omapi lease retreival given hardware address

rhirst at linuxcare.com rhirst at linuxcare.com
Sat Jan 10 15:06:32 UTC 2004


Given a hardware address, I wish to use omapi to find the IP address
assigned to it.  dhcp_lease_lookup() creates a hash of the 6 byte
hardware address and fails to find a match.  It should create a hash of
7 bytes, the first byte being hardware-type, and next six the hardware
address.  The attached patch appears to fix it; I basically just copied
the code from dhcp_host_lookup().
A second issue is that if I create a host specifying ip-address and
hardware-address (and type), I can't use omapi to look that up using the
ip-address as a key.  That appears to be because the code tries to find
a lease for the ip-address, then from the hardware address in the lease,
it attempts to find a host.  As I don't have a lease, that approach
doesn't work.

Cheers,
     Richard




-- Attached file included as plaintext by Ecartis --
-- File: omapi-lease-from-mac.diff

--- dhcp-3.0.1rc12/server/omapi.c-	2004-01-08 21:04:05.000000000 +0000
+++ dhcp-3.0.1rc12/server/omapi.c	2004-01-08 23:12:21.000000000 +0000
@@ -774,12 +774,54 @@
 	/* Now look for a hardware address. */
 	status = omapi_get_value_str (ref, id, "hardware-address", &tv);
 	if (status == ISC_R_SUCCESS) {
-		lease = (struct lease *)0;
-		lease_hash_lookup (&lease, lease_hw_addr_hash,
-				   tv -> value -> u.buffer.value,
-				   tv -> value -> u.buffer.len, MDL);
+		unsigned char *haddr;
+		unsigned int len;
+
+		len = tv -> value -> u.buffer.len + 1;
+		haddr = dmalloc (len, MDL);
+		if (!haddr) {
+			omapi_value_dereference (&tv, MDL);
+			return ISC_R_NOMEMORY;
+		}
+
+		memcpy (haddr + 1, tv -> value -> u.buffer.value, len - 1);
 		omapi_value_dereference (&tv, MDL);
-			
+
+		status = omapi_get_value_str (ref, id, "hardware-type", &tv);
+		if (status == ISC_R_SUCCESS) {
+			if (tv -> value -> type == omapi_datatype_data) {
+				if ((tv -> value -> u.buffer.len != 4) ||
+				    (tv -> value -> u.buffer.value[0] != 0) ||
+				    (tv -> value -> u.buffer.value[1] != 0) ||
+				    (tv -> value -> u.buffer.value[2] != 0)) {
+					omapi_value_dereference (&tv, MDL);
+					dfree (haddr, MDL);
+					return ISC_R_INVALIDARG;
+				}
+
+				haddr[0] = tv -> value -> u.buffer.value[3];
+			} else if (tv -> value -> type == omapi_datatype_int) {
+				haddr[0] = (unsigned char)
+					tv -> value -> u.integer;
+			} else {
+				omapi_value_dereference (&tv, MDL);
+				dfree (haddr, MDL);
+				return ISC_R_INVALIDARG;
+			}
+
+			omapi_value_dereference (&tv, MDL);
+		} else {
+			/* If no hardware-type is specified, default to
+			   ethernet.  This may or may not be a good idea,
+			   but Telus is currently relying on this behavior.
+			   - DPN */
+			haddr[0] = HTYPE_ETHER;
+		}
+
+		lease = (struct lease *)0;
+		lease_hash_lookup (&lease, lease_hw_addr_hash, haddr, len, MDL);
+		dfree (haddr, MDL);
+
 		if (*lp && *lp != (omapi_object_t *)lease) {
 			omapi_object_dereference (lp, MDL);
 			lease_dereference (&lease, MDL);






More information about the dhcp-hackers mailing list