Fix #87, add broadcast discover message support

This commit is contained in:
wweir 2019-02-02 05:15:01 +08:00 committed by Dave Anderson
parent a62f978630
commit 01f30467ac
2 changed files with 18 additions and 6 deletions

View File

@ -29,14 +29,18 @@ var (
dhcpClientPort = 68
)
const dhcpServerPort = 67
// txType describes how a Packet should be sent on the wire.
type txType int
// The various transmission strategies described in RFC 2131. "MUST",
// "MUST NOT", "SHOULD" and "MAY" are as specified in RFC 2119.
const (
// Packet MUST be broadcast.
txBroadcast txType = iota
// Packet MUST be broadcast from client side.
txClientBroadcast txType = iota
// Packet MUST be broadcast from server side.
txServerBroadcast
// Packet MUST be unicasted to port 67 of RelayAddr
txRelayAddr
// Packet MUST be unicasted to port 68 of ClientAddr
@ -169,7 +173,13 @@ func (c *Conn) SendDHCP(pkt *Packet, intf *net.Interface) error {
}
switch pkt.txType() {
case txBroadcast, txHardwareAddr:
case txClientBroadcast:
addr := net.UDPAddr{
IP: net.IPv4bcast,
Port: dhcpServerPort,
}
return c.conn.Send(b, &addr, intf.Index)
case txServerBroadcast, txHardwareAddr:
addr := net.UDPAddr{
IP: net.IPv4bcast,
Port: dhcpClientPort,
@ -178,7 +188,7 @@ func (c *Conn) SendDHCP(pkt *Packet, intf *net.Interface) error {
case txRelayAddr:
addr := net.UDPAddr{
IP: pkt.RelayAddr,
Port: 67,
Port: dhcpServerPort,
}
return c.conn.Send(b, &addr, 0)
case txClientAddr:

View File

@ -91,11 +91,13 @@ func (p *Packet) txType() txType {
case p.RelayAddr != nil && p.RelayAddr.IsGlobalUnicast():
return txRelayAddr
case p.Type == MsgNack:
return txBroadcast
return txServerBroadcast
case p.ClientAddr != nil && (p.ClientAddr.IsGlobalUnicast() || p.ClientAddr.IsLoopback()):
return txClientAddr
case p.Broadcast && p.Type == MsgDiscover:
return txClientBroadcast
case p.Broadcast:
return txBroadcast
return txServerBroadcast
default:
return txHardwareAddr
}