dhcp test

This commit is contained in:
insanity 2018-08-29 16:48:24 +09:00
parent 01b201266c
commit 69c2837a31
2 changed files with 57 additions and 105 deletions

View File

@ -5,41 +5,12 @@ import (
"encoding/binary"
"fmt"
"net"
"strings"
)
const (
MAGIC_COOKIE uint32 = 0x63825363
OPT_CODE_SERVER_IDENTIFIER uint8 = 54
OPT_CODE_SUBNET_MASK uint8 = 1
OPT_CODE_ROUTER uint8 = 3
OPT_CODE_DNS uint8 = 6
)
"github.com/google/gopacket/layers"
type dhcpDiscover struct {
MsgType byte
HwType byte
HwAddrLen byte
Hops byte
Xid uint32
Secs uint16
BootpFlags uint16
ClientIp uint32
YourIp uint32
NextServerIp uint32
RelayAgentIp uint32
ClientMacAddr [6]byte
ClientHwAddrPadding [10]byte
ServerHostName [64]byte
BootFileName [128]byte
MagicCookie uint32
Mtype byte
MtypeLen byte
MtypeVal byte
Opts [200]byte
End byte
Padding [16]byte
}
"github.com/google/gopacket"
gl "github.com/google/gopacket/layers"
)
func doDiscover(address string) (map[string]string, error) {
@ -51,13 +22,9 @@ func doDiscover(address string) (map[string]string, error) {
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
defer conn.Close()
sendDiscover(conn) // broadcast 255.255.255.255:67
sendDiscover(conn) // broadcast 255.255.255.255:67
return readOffer(conn) // unicast
}
@ -70,32 +37,27 @@ func sendDiscover(conn *net.UDPConn) error {
return err
}
dhcp := dhcpDiscover{
MsgType: 0x01,
HwType: 0x01,
HwAddrLen: 0x06,
Hops: 0x00,
Xid: 0x00000000,
Secs: 0x0000,
ClientIp: 0x00000000,
YourIp: 0x00000000,
NextServerIp: 0x00000000,
RelayAgentIp: 0x00000000,
MagicCookie: MAGIC_COOKIE,
Mtype: 0x35,
MtypeLen: 0x01,
MtypeVal: 0x01,
End: 0xff,
}
dhcp := &gl.DHCPv4{Operation: gl.DHCPOpRequest, HardwareType: gl.LinkTypeEthernet, Xid: 0x12345678,
ClientIP: net.IP{0, 0, 0, 0}, YourClientIP: net.IP{0, 0, 0, 0}, NextServerIP: net.IP{0, 0, 0, 0}, RelayAgentIP: net.IP{0, 0, 0, 0},
ClientHWAddr: net.HardwareAddr{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc},
ServerName: make([]byte, 64), File: make([]byte, 128)}
var flag uint16 = 0
dhcp.BootpFlags = ^flag // flag = unicast , ^flag = broadcast
dhcp.Options = append(dhcp.Options, gl.NewDHCPOption(gl.DHCPOptMessageType, []byte{byte(gl.DHCPMsgTypeDiscover)}))
dhcp.Options = append(dhcp.Options, gl.NewDHCPOption(gl.DHCPOptHostname, []byte{'o', 'v', 'e', 'r', 'f', 'l', 'o', 'w', '.', 'c', 'o', 'm'}))
dhcp.Options = append(dhcp.Options, gl.NewDHCPOption(gl.DHCPOptPad, nil))
dhcp.Options = append(dhcp.Options, gl.NewDHCPOption(gl.DHCPOptParamsRequest,
[]byte{byte(gl.DHCPOptSubnetMask), byte(gl.DHCPOptBroadcastAddr), byte(gl.DHCPOptTimeOffset),
byte(gl.DHCPOptRouter), byte(gl.DHCPOptDomainName), byte(gl.DHCPOptDNS), byte(gl.DHCPOptDomainSearch),
byte(gl.DHCPOptHostname), byte(gl.DHCPOptNetBIOSTCPNS), byte(gl.DHCPOptInterfaceMTU), byte(gl.DHCPOptClasslessStaticRoute),
byte(gl.DHCPOptNTPServers)}))
writer := new(bytes.Buffer)
if err := binary.Write(writer, binary.BigEndian, dhcp); err != nil {
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{FixLengths: true}
err = gopacket.SerializeLayers(buf, opts, dhcp)
if err != nil {
return err
}
_, err = conn.WriteToUDP(writer.Bytes(), serverAddr)
_, err = conn.WriteToUDP(buf.Bytes(), serverAddr)
if err != nil {
return err
}
@ -107,62 +69,52 @@ func readOffer(conn *net.UDPConn) (map[string]string, error) {
offerOptions := make(map[string]string)
buf := make([]byte, 1024)
_, _, err := conn.ReadFromUDP(buf)
n, _, err := conn.ReadFromUDP(buf)
if err != nil {
return nil, err
}
offer := dhcpDiscover{}
reader := new(bytes.Buffer)
reader.Write(buf)
if err := binary.Read(reader, binary.BigEndian, &offer); err != nil {
return nil, err
}
if offer.MagicCookie != MAGIC_COOKIE {
return nil, err
if n <= 0 {
return nil, fmt.Errorf("%s", "Emptry response.")
}
//option searching
r := new(bytes.Buffer)
r.Write(offer.Opts[:])
for i := 0; i < r.Len(); i++ {
v := r.Next(1)[0]
if v == OPT_CODE_SUBNET_MASK && r.Next(1)[0] == 4 {
ipStr := byteToIPString(r.Next(4))
offerOptions["SUBNETMASK"] = ipStr
}
if v == OPT_CODE_ROUTER && r.Next(1)[0] == 4 {
ipStr := byteToIPString(r.Next(4))
offerOptions["ROUTER"] = ipStr
}
if v == OPT_CODE_DNS {
len := r.Next(1)[0]
var dns []string
var ipStr string
ipStr = byteToIPString(r.Next(4))
dns = append(dns, ipStr)
if len == 8 {
ipStr = byteToIPString(r.Next(4))
dns = append(dns, ipStr)
var dhcp gl.DHCPv4
decoded := []gopacket.LayerType{}
parser := gopacket.NewDecodingLayerParser(gl.LayerTypeDHCPv4, &dhcp)
if err = parser.DecodeLayers(buf, &decoded); err != nil {
return nil, err
}
for _, layerType := range decoded {
switch layerType {
case layers.LayerTypeDHCPv4:
for _, opt := range dhcp.Options {
switch opt.Type {
case gl.DHCPOptServerID:
offerOptions["DHCP_SERVER"] = optDataToIPString(opt.Data)
break
case gl.DHCPOptLeaseTime:
var sec int32
if err = binary.Read(bytes.NewReader(opt.Data), binary.BigEndian, &sec); err == nil {
offerOptions["LEASE_TIME"] = fmt.Sprintf("%d", sec)
}
break
case gl.DHCPOptSubnetMask:
offerOptions["SUBNET_MASK"] = optDataToIPString(opt.Data)
break
case gl.DHCPOptRouter:
offerOptions["ROUTER"] = optDataToIPString(opt.Data)
break
case gl.DHCPOptDNS:
offerOptions["DNS_SERVER"] = optDataToIPString(opt.Data)
break
}
}
offerOptions["DNS"] = strings.Join(dns, ",")
}
if v == OPT_CODE_SERVER_IDENTIFIER && r.Next(1)[0] == 4 {
ipStr := byteToIPString(r.Next(4))
offerOptions["DHCP_SERVER"] = ipStr
}
}
return offerOptions, nil
}
func byteToIPString(b []byte) string {
func optDataToIPString(b []byte) string {
var ipStr string
len := len(b)
for i := 0; i < len; i++ {

View File

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