func (c *client) isCompletePacket(p *dhcp.Packet) bool { complete := !ip.IsUnspecifiedIP(p.YourIP()) && !ip.IsUnspecifiedIP(p.ServerIP()) if !complete { return false } for _, param := range c.params { switch dhcp4.OptionCode(param) { case dhcp4.OptionSubnetMask: ones, bits := p.SubnetMask().Size() if ones == 0 || bits == 0 { return false } case dhcp4.OptionRouter: if ip.IsUnspecifiedIP(p.Gateway()) { return false } case dhcp4.OptionDomainNameServer: if len(p.DNS()) == 0 { return false } } } if p.LeaseTime().Seconds() == 0 { return false } return true }
func (t *BaseOperations) dhcpLoop(stop chan bool, e *NetworkEndpoint, ack *dhcp.Packet, id client.ID) { exp := time.After(ack.LeaseTime() / 2) for { select { case <-stop: // release the ip log.Infof("releasing IP address for network %s", e.Name) t.dhcpClient.Release(ack) return case <-exp: log.Infof("renewing IP address for network %s", e.Name) newack, err := t.dhcpClient.Renew(id, ack) if err != nil { log.Errorf("failed to renew ip address for network %s: %s", e.Name, err) continue } ack = newack log.Infof("successfully renewed ip address: IP=%s, SubnetMask=%s, Gateway=%s, DNS=%s, Lease Time=%s", ack.YourIP(), ack.SubnetMask(), ack.Gateway(), ack.DNS(), ack.LeaseTime()) e.DHCP = &DHCPInfo{ Assigned: net.IPNet{IP: ack.YourIP(), Mask: ack.SubnetMask()}, Gateway: net.IPNet{IP: ack.Gateway(), Mask: ack.SubnetMask()}, Nameservers: ack.DNS(), } t.Apply(e) if err = t.config.UpdateNetworkEndpoint(e); err != nil { log.Error(err) } // update any endpoints that share this NIC for _, d := range t.dynEndpoints[e.ID] { if e == d { continue } d.DHCP = e.DHCP t.Apply(d) if err = t.config.UpdateNetworkEndpoint(d); err != nil { log.Error(err) } } t.config.Flush() exp = time.After(ack.LeaseTime() / 2) } } }
func isCompletePacket(p *dhcp.Packet) bool { complete := !ip.IsUnspecifiedIP(p.Gateway()) && !ip.IsUnspecifiedIP(p.YourIP()) && !ip.IsUnspecifiedIP(p.ServerIP()) if !complete { return false } ones, bits := p.SubnetMask().Size() if ones == 0 || bits == 0 { return false } if p.LeaseTime().Seconds() == 0 { return false } return true }
func getDynamicIP(t Netlink, link netlink.Link, endpoint *NetworkEndpoint) (client.Client, error) { var ack *dhcp.Packet var err error // use dhcp to acquire address dc, err := client.NewClient(link.Attrs().Index, link.Attrs().HardwareAddr) if err != nil { return nil, err } params := []byte{byte(dhcp4.OptionSubnetMask)} if ip.IsUnspecifiedIP(endpoint.Network.Gateway.IP) { params = append(params, byte(dhcp4.OptionRouter)) } if len(endpoint.Network.Nameservers) == 0 { params = append(params, byte(dhcp4.OptionDomainNameServer)) } dc.SetParameterRequestList(params...) err = dc.Request() if err != nil { log.Errorf("error sending dhcp request: %s", err) return nil, err } ack = dc.LastAck() if ack.YourIP() == nil || ack.SubnetMask() == nil { err = fmt.Errorf("dhcp assigned nil ip or subnet mask") log.Error(err) return nil, err } log.Infof("DHCP response: IP=%s, SubnetMask=%s, Gateway=%s, DNS=%s, Lease Time=%s", ack.YourIP(), ack.SubnetMask(), ack.Gateway(), ack.DNS(), ack.LeaseTime()) defer func() { if err != nil && ack != nil { dc.Release() } }() return dc, nil }
func getDynamicIP(t Netlink, link netlink.Link, dc client.Client) (*dhcp.Packet, error) { var ack *dhcp.Packet var err error // use dhcp to acquire address id, err := client.NewID(link.Attrs().Index, link.Attrs().HardwareAddr) if err != nil { return nil, err } ack, err = dc.Request(id) if err != nil { log.Errorf("error sending dhcp request: %s", err) return nil, err } if ack.YourIP() == nil || ack.SubnetMask() == nil { err = fmt.Errorf("dhcp assigned nil ip or subnet mask") log.Error(err) return nil, err } log.Infof("DHCP response: IP=%s, SubnetMask=%s, Gateway=%s, DNS=%s, Lease Time=%s", ack.YourIP(), ack.SubnetMask(), ack.Gateway(), ack.DNS(), ack.LeaseTime()) defer func() { if err != nil && ack != nil { dc.Release(ack) } }() return ack, nil }