From c41aa7926550e6138d4c8aef021ede01fc5e02d4 Mon Sep 17 00:00:00 2001 From: Matt Layher Date: Sat, 3 Feb 2018 08:42:56 -0500 Subject: [PATCH] dhcp4: parse transaction ID as a byte slice --- dhcp4/conn_test.go | 2 +- dhcp4/packet.go | 6 +++--- dhcp4/testdata/dhcp.parsed | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/dhcp4/conn_test.go b/dhcp4/conn_test.go index 2bce235..8070946 100644 --- a/dhcp4/conn_test.go +++ b/dhcp4/conn_test.go @@ -36,7 +36,7 @@ func testConn(t *testing.T, impl conn, addr string) { p := &Packet{ Type: MsgDiscover, - TransactionID: "1234", + TransactionID: []byte("1234"), Broadcast: true, HardwareAddr: mac, } diff --git a/dhcp4/packet.go b/dhcp4/packet.go index bb0ccc6..114874b 100644 --- a/dhcp4/packet.go +++ b/dhcp4/packet.go @@ -67,7 +67,7 @@ func (mt MessageType) String() string { // Packet represents a DHCP packet. type Packet struct { Type MessageType - TransactionID string + TransactionID []byte // Always 4 bytes Broadcast bool HardwareAddr net.HardwareAddr // Only ethernet supported at the moment @@ -175,7 +175,7 @@ func (p *Packet) Marshal() ([]byte, error) { // Hops = 0 ret.WriteByte(0) // Transaction ID - ret.WriteString(p.TransactionID) + ret.Write(p.TransactionID) // Seconds elapsed ret.Write([]byte{0, 0}) // Broadcast flag @@ -267,7 +267,7 @@ func Unmarshal(bs []byte) (*Packet, error) { return nil, fmt.Errorf("packet has unsupported hardware address type/length %d/%d", bs[1], bs[2]) } ret.HardwareAddr = net.HardwareAddr(bs[28:34]) - ret.TransactionID = string(bs[4:8]) + ret.TransactionID = bs[4:8] if binary.BigEndian.Uint16(bs[10:12])&0x8000 != 0 { ret.Broadcast = true } diff --git a/dhcp4/testdata/dhcp.parsed b/dhcp4/testdata/dhcp.parsed index e1f9bf7..d129287 100644 --- a/dhcp4/testdata/dhcp.parsed +++ b/dhcp4/testdata/dhcp.parsed @@ -1,5 +1,5 @@ DHCPDISCOVER - "\x9bN\x05W" + []byte{0x9b, 0x4e, 0x5, 0x57} Broadcast MAC: d0:50:99:4e:05:57 ClientIP: 0.0.0.0 @@ -19,7 +19,7 @@ DHCPDISCOVER 97: []byte{0x0, 0x0, 0x2, 0x0, 0x3, 0x0, 0x4, 0x0, 0x5, 0x0, 0x6, 0x0, 0x7, 0x0, 0x8, 0x0, 0x9} ====== DHCPOFFER - "\x9bN\x05W" + []byte{0x9b, 0x4e, 0x5, 0x57} Broadcast MAC: d0:50:99:4e:05:57 ClientIP: 0.0.0.0 @@ -37,7 +37,7 @@ DHCPOFFER 97: []byte{0x0, 0x0, 0x2, 0x0, 0x3, 0x0, 0x4, 0x0, 0x5, 0x0, 0x6, 0x0, 0x7, 0x0, 0x8, 0x0, 0x9} ====== DHCPOFFER - "\x9bN\x05W" + []byte{0x9b, 0x4e, 0x5, 0x57} Broadcast MAC: d0:50:99:4e:05:57 ClientIP: 0.0.0.0 @@ -61,7 +61,7 @@ DHCPOFFER 59: []byte{0x0, 0x0, 0xc, 0x4e} ====== DHCPREQUEST - "R\xcf\xf0\a" + []byte{0x52, 0xcf, 0xf0, 0x7} Unicast MAC: 00:24:d7:ba:0b:20 ClientIP: 0.0.0.0