2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2008 Roy Marples <roy@marples.name>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #define THIRTY_YEARS_IN_SECONDS 946707779
42 get_duid(unsigned char *duid
, const struct interface
*iface
)
50 unsigned char *p
= duid
;
54 /* If we already have a DUID then use it as it's never supposed
55 * to change once we have one even if the interfaces do */
56 if ((f
= fopen(DUID
, "r"))) {
57 while ((line
= get_line(f
))) {
58 len
= hwaddr_aton(NULL
, line
);
59 if (len
&& len
<= DUID_LEN
) {
60 hwaddr_aton(duid
, line
);
73 /* No file? OK, lets make one based on our interface */
74 if (!(f
= fopen(DUID
, "w")))
76 type
= htons(1); /* DUI-D-LLT */
79 hw
= htons(iface
->family
);
82 /* time returns seconds from jan 1 1970, but DUID-LLT is
83 * seconds from jan 1 2000 modulo 2^32 */
84 t
= time(NULL
) - THIRTY_YEARS_IN_SECONDS
;
85 ul
= htonl(t
& 0xffffffff);
88 /* Finally, add the MAC address of the interface */
89 memcpy(p
, iface
->hwaddr
, iface
->hwlen
);
92 x
= fprintf(f
, "%s\n", hwaddr_ntoa(duid
, len
));
94 /* Failed to write the duid? scrub it, we cannot use it */