krolaw / dhcp4

DHCP4 library written in Go.
BSD 3-Clause "New" or "Revised" License
327 stars 95 forks source link

Reply not sent to DHCP Relay when Broadcast flag set #35

Open galthaus opened 7 years ago

galthaus commented 7 years ago

We use parts of this library with a custom handler implementation for: https://github.com/digitalrebar/provision

We did this before some of your more recent changes to interface binding. We may re-evaluate that later to see if we can switch back out of the custom interface code we use.

Regardless, we encountered in the field an issue that appears in the current code base. A DHCP client sets the Broadcast flag bit in a request, but a DHCP relay is used to forward the packet to our server. In this case, the reply code will generate a broadcast message instead of sending back to the giaddr field as per the spec.

The issue is around line 70 in server.go. To address the issue, we do this:

                    port, _ := strconv.Atoi(portStr)
                    if req.GIAddr().Equal(net.IPv4zero) {
                            if net.ParseIP(ipStr).Equal(net.IPv4zero) || req.Broadcast() {
                                    addr = &net.UDPAddr{IP: net.IPv4bcast, Port: port}
                            }
                    } else {
                            addr = &net.UDPAddr{IP: req.GIAddr(), Port: port}
                    }

Hope this helps. Thanks for your work.

krolaw commented 7 years ago

Thanks galthaus, I never thought of that particular use case for this library. Please have a look at the giaddr branch and and tell me what you think of conn/giaddr.go (untested).