This commit is contained in:
insanity 2018-08-29 17:21:08 +09:00
parent 69c2837a31
commit 12395bf397
2 changed files with 19 additions and 6 deletions

View File

@ -5,6 +5,8 @@ import (
"encoding/binary"
"fmt"
"net"
"strings"
"time"
"github.com/google/gopacket/layers"
@ -12,7 +14,7 @@ import (
gl "github.com/google/gopacket/layers"
)
func doDiscover(address string) (map[string]string, error) {
func doDiscoverV4(address string) (map[string]string, error) {
localAddr, err := net.ResolveUDPAddr("udp", address+":68")
if err != nil {
@ -22,10 +24,13 @@ func doDiscover(address string) (map[string]string, error) {
if err != nil {
return nil, err
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(time.Second * 3))
defer func() {
conn.Close()
}()
sendDiscover(conn) // broadcast 255.255.255.255:67
return readOffer(conn) // unicast
return readOffer(conn)
}
@ -104,7 +109,15 @@ func readOffer(conn *net.UDPConn) (map[string]string, error) {
offerOptions["ROUTER"] = optDataToIPString(opt.Data)
break
case gl.DHCPOptDNS:
offerOptions["DNS_SERVER"] = optDataToIPString(opt.Data)
cnt := len(opt.Data) / 4
var dns []string
ipStr := optDataToIPString(opt.Data[0:4])
dns = append(dns, ipStr)
if cnt > 1 {
ipStr := optDataToIPString(opt.Data[4:8])
dns = append(dns, ipStr)
}
offerOptions["DOMAIN_NAME_SERVER"] = strings.Join(dns, ",")
break
}
}

View File

@ -5,7 +5,7 @@ import (
)
func TestDHCP(t *testing.T) {
offer, err := doDiscover("192.168.1.203")
offer, err := doDiscoverV4("192.168.1.203")
if err != nil {
t.Errorf("ERROR : %s", err.Error())
}