setting mac address for a host in C (dhcpctl.h)

Peter Rathlev peter at rathlev.dk
Tue Jun 28 22:01:42 UTC 2011


On Tue, 2011-06-28 at 18:35 +0530, Johnson koil Raj wrote:
> I am working on dhcpd api by using dhcpctl.h. I am trying to assign 
> static IPs for machines when its hostname and mac-address was provided. 
> I have tried so far. When I tried to add a new host detail. The 
> mac-address alone not assigned properly. Some junk value it is giving. 
> The code what I have used is below.

I don't want to sound harsh, but have you considered if you're really
doing the right thing here? Trying to hack together some C code yourself
can be dangerous if you don't master the basic concepts.

>      dhcpctl_data_string macaddrstring = NULL;
>      memset (&macaddrstring, 0, sizeof macaddrstring);
>      omapi_data_string_new (&macaddrstring, 16, MDL);
>      memcpy(macaddrstring->value, "00:0c:26:22:bf:c3", 16);
>      status = dhcpctl_set_value (host, macaddrstring, "hardware-address");
> 
> I am seeing following value in lease file.
> 
> host testomapihost {
>    dynamic;
>    hardware ethernet 31:30:30:3a:30:63:3a:32:36:3a:32:32:3a:62:66:3a;
>    fixed-address 192.168.10.123;
> }

This should give you the clue. Look at what the string "00:0c:26..."
translates to in hex: "30 30 3a 30 63 3a 32 36 ...". That's probably not
a coincidence.

Looking closer at the lines above:

     memcpy(macaddrstring->value, "00:0c:26:22:bf:c3", 16);

That string isn't 16 bytes. It's 18 bytes including the terminating nul
character. And keep in mind that "correcting" the length parameter will
not help you.

     status = dhcpctl_set_value (host, macaddrstring, "hardware-address");

You probably want to use dhcpctl_set_string_value instead.

Beware that you're probably in the process of creating a piece of
software that you should never ever try to run as root on anything.

-- 
Peter





More information about the dhcp-users mailing list