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

View File

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