/* * Example Server :D */ func ExampleServer() { //Create a Lease Pool We're going to use a memory pool //Remember the memory is cleared on restart so you will reissue the same IP Addresses. myMemoryLeasePool := memorypool.MemoryPool{} //Lets add a list of IPs to the pool these will be served to the clients so make sure they work for you. // So Create Array of IPs 192.168.1.1 to 192.168.1.30 for i := 0; i < 30; i++ { err := myMemoryLeasePool.AddLease(leasepool.Lease{IP: dhcp4.IPAdd(net.IPv4(192, 168, 1, 1), i)}) if err != nil { log.Fatalln("Error Adding IP to pool:" + err.Error()) } } // We set the port numbers to over 1024 (1067 & 1068) as the automated test don't have root access tServer, err := dhcp4server.New( net.IPv4(192, 168, 1, 201), &myMemoryLeasePool, dhcp4server.SetLocalAddr(net.UDPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 1067}), dhcp4server.SetRemoteAddr(net.UDPAddr{IP: net.IPv4bcast, Port: 1068}), ) if err != nil { log.Fatalln("Error Configuring Server:" + err.Error()) } //Start the Server... err = tServer.ListenAndServe() if err != nil { log.Fatalln("Error Starting Server:" + err.Error()) } }
func getTestLeasePool() *memorypool.MemoryPool { //Create a Lease Pool We're going to use a memory pool //Remember the memory is cleared on restart so you will reissue the same IP Addresses. myMemoryLeasePool := memorypool.MemoryPool{} //Lets add a list of IPs to the pool these will be served to the clients so make sure they work for you. // So Create Array of IPs 192.168.1.1 to 192.168.1.30 for i := 0; i < 30; i++ { err := myMemoryLeasePool.AddLease(leasepool.Lease{IP: dhcp4.IPAdd(net.IPv4(192, 168, 1, 1), i)}) if err != nil { log.Fatalln("Error Adding IP to pool:" + err.Error()) } } return &myMemoryLeasePool }
/* * Benchmark the ServeDHCP Function */ func BenchmarkServeDHCP(test *testing.B) { //Create a Lease Pool We're going to use a memory pool //Remember the memory is cleared on restart so you will reissue the same IP Addresses. myMemoryLeasePool := memorypool.MemoryPool{} //Lets add a list of IPs to the pool these will be served to the clients so make sure they work for you. // So Create Array of IPs 192.168.1.1 to 192.168.1.30 for i := 0; i < test.N; i++ { err := myMemoryLeasePool.AddLease(leasepool.Lease{IP: dhcp4.IPAdd(net.IPv4(192, 168, 1, 1), i)}) if err != nil { log.Fatalln("Error Adding IP to pool:" + err.Error()) } } //Setup the Server myServer, err := dhcp4server.New( net.IPv4(127, 0, 0, 1), &myMemoryLeasePool, ) if err != nil { test.Error("Error: Can't Configure Server " + err.Error()) } //Setup A Client // Although We Won't send the packets over the network we'll use the client to create the requests. c, err := dhcp4client.NewInetSock(dhcp4client.SetLocalAddr(net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1068}), dhcp4client.SetRemoteAddr(net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 1067})) if err != nil { test.Error("Client Conection Generation:" + err.Error()) } client, err := dhcp4client.New(dhcp4client.Connection(c)) if err != nil { test.Error("Error: Can't Configure Client " + err.Error()) } defer client.Close() test.ResetTimer() for i := 0; i < test.N; i++ { test.StopTimer() //Generate Hardware Address HardwareMACAddress, err := hardwareaddr.GenerateEUI48() if err != nil { test.Error("Error: Can't Generate Valid MACAddress" + err.Error()) } client.SetOption(dhcp4client.HardwareAddr(HardwareMACAddress)) discovery := client.DiscoverPacket() //Run the Discovery On the Server test.StartTimer() offer, err := myServer.ServeDHCP(discovery) if err != nil { test.Error("Discovery Error:" + err.Error()) } if len(offer) == 0 { test.Error("No Valid Offer") } else { request := client.RequestPacket(&offer) _, err := myServer.ServeDHCP(request) if err != nil { test.Error("Acknowledge Error:" + err.Error()) } } } }