Skip to content

Commit 175e84f

Browse files
authored
Merge pull request #565 from pallotron/GetDUIDLL
feat(dhcpv6): add GetDUIDLL helper to generate a DUID
2 parents da879a2 + afa241b commit 175e84f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

dhcpv6/iputils.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package dhcpv6
33
import (
44
"fmt"
55
"net"
6+
7+
"github.com/insomniacslk/dhcp/iana"
68
)
79

810
// InterfaceAddresses is used to fetch addresses of an interface with given name
@@ -102,3 +104,32 @@ func ExtractMAC(packet DHCPv6) (net.HardwareAddr, error) {
102104
}
103105
return nil, fmt.Errorf("failed to extract MAC")
104106
}
107+
108+
// GetDUIDLL generates a DUID-LL based on the MAC address of the first
109+
// available, up, non-loopback network interface. This provides a stable,
110+
// predictable DUID for the server.
111+
func GetDUIDLL() (*DUIDLL, error) {
112+
ifaces, err := net.Interfaces()
113+
if err != nil {
114+
return nil, err
115+
}
116+
117+
for _, iface := range ifaces {
118+
// Skip loopback and down interfaces
119+
if iface.Flags&net.FlagLoopback != 0 || iface.Flags&net.FlagUp == 0 {
120+
continue
121+
}
122+
// Skip interfaces without a MAC address
123+
if len(iface.HardwareAddr) == 0 {
124+
continue
125+
}
126+
127+
// Found a suitable interface
128+
return &DUIDLL{
129+
HWType: iana.HWTypeEthernet,
130+
LinkLayerAddr: iface.HardwareAddr,
131+
}, nil
132+
}
133+
134+
return nil, fmt.Errorf("no suitable network interface found to generate a DUID")
135+
}

0 commit comments

Comments
 (0)