func dhcpSetup(cfg *Config) chan error { cfg.db.InitDHCP() exit := make(chan error, 1) go func() { d := &DHCPService{ ip: cfg.DHCPIP(), leaseDuration: cfg.DHCPLeaseDuration(), db: cfg.db, subnet: cfg.Subnet(), guestPool: cfg.DHCPSubnet(), domain: cfg.Domain(), defaultOptions: dhcp4.Options{ dhcp4.OptionSubnetMask: net.IP(cfg.Subnet().Mask), dhcp4.OptionRouter: cfg.Gateway(), dhcp4.OptionDomainNameServer: cfg.DHCPIP(), }, } dhcpTFTP := cfg.DHCPTFTP() if dhcpTFTP != "" { d.defaultOptions[dhcp4.OptionTFTPServerName] = []byte(dhcpTFTP) } exit <- dhcp4.ListenAndServeIf(cfg.DHCPNIC(), d) }() return exit }
func main() { if len(os.Args) != 2 { panic("not enough args") } intf, err := net.InterfaceByName(os.Args[1]) if err != nil { panic(err) } addrs, err := intf.Addrs() if err != nil { panic(err) } if len(addrs) == 0 { panic("no addresses on interface") } myIP, _, _ := net.ParseCIDR(addrs[0].String()) dhcpHandler := &DHCPHandler{ myIP: myIP, leases: make(map[string]net.IP), allocator: net.IPNet{IP: net.IPv4(172, 16, 55, 0), Mask: net.CIDRMask(24, 32)}, allocatorMutex: new(sync.Mutex), } handler := &HTTPHandler{dhcpHandler} go dhcp.ListenAndServeIf(os.Args[1], dhcpHandler) http.ListenAndServe(":8080", handler) }
// StartDHCP ListenAndServe for dhcp on port 67, binds on interface=ifName if it's // not empty func StartDHCP(ifName string, serverIP net.IP, datasource datasource.DataSource) error { handler := &Handler{ ifName: ifName, serverIP: serverIP, datasource: datasource, bootMessage: fmt.Sprintf("Blacksmith (%s)", datasource.SelfInfo().Version), } log.WithFields(log.Fields{ "where": "dhcp.StartDHCP", "action": "announce", }).Infof("Listening on %s:67 (interface: %s)", serverIP.String(), ifName) var err error if ifName != "" { err = dhcp4.ListenAndServeIf(ifName, handler) } else { err = dhcp4.ListenAndServe(handler) } // https://groups.google.com/forum/#!topic/coreos-user/Qbn3OdVtrZU if len(datasource.ClusterName()) > 50 { // 63 - 12(mac) - 1(.) log.WithField("where", "dhcp.StartDHCP").Warn( "Warning: ClusterName is too long. It may break the behaviour of the DHCP clients") } rand.Seed(time.Now().UTC().UnixNano()) return err }
func RunDhcpHandler(dhcpInfo *DataTracker, intf net.Interface, myIp string) { log.Println("Starting on interface: ", intf.Name, " with server ip: ", myIp) serverIP, _, _ := net.ParseCIDR(myIp) serverIP = serverIP.To4() handler := &DHCPHandler{ ip: serverIP, intf: intf, info: dhcpInfo, } log.Fatal(dhcp.ListenAndServeIf(intf.Name, handler)) }
func dhcpServer(c *Config, l *Store) { handler := &DHCPHandler{ ip: c.BaseIP, config: c, leaseDuration: 24 * time.Hour, leaseRange: 50, leases: l, options: dhcp.Options{ dhcp.OptionSubnetMask: []byte(c.Subnet.To4()), dhcp.OptionBootFileName: []byte("undionly.kpxe"), dhcp.OptionRouter: []byte(c.Gateway.To4()), dhcp.OptionDomainNameServer: []byte(c.DNSServer.To4()), }, } logger.Error("%v", dhcp.ListenAndServeIf(c.Interf, handler)) }
func ServeDHCP(settings *DHCPSetting, leasePool *LeasePool) error { handler, err := newDHCPHandler(settings, leasePool) if err != nil { logging.Debug("DHCP", "Error in connecting etcd - %s", err.Error()) return err } logging.Log("DHCP", "Listening on :67 - with server IP %s", settings.ServerIP.String()) if settings.IFName != "" { err = dhcp4.ListenAndServeIf(settings.IFName, handler) } else { err = dhcp4.ListenAndServe(handler) } if err != nil { logging.Debug("DHCP", "Error in server - %s", err.Error()) } return err }
func ServeDHCP(settings *DHCPSetting, datasource datasource.DataSource) error { handler, err := newDHCPHandler(settings, datasource) if err != nil { logging.Debug("DHCP", "Error in connecting etcd - %s", err.Error()) return err } logging.Log("DHCP", "Listening on %s:67 (interface: %s)", settings.ServerIP.String(), settings.IFName) if settings.IFName != "" { err = dhcp4.ListenAndServeIf(settings.IFName, handler) } else { err = dhcp4.ListenAndServe(handler) } if err != nil { logging.Debug("DHCP", "Error in server - %s", err.Error()) } rand.Seed(time.Now().UTC().UnixNano()) return err }