summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAkshay Navale <navale@fb.com>2019-04-02 15:45:56 -0700
committerinsomniac <insomniacslk@users.noreply.github.com>2019-04-03 00:17:07 +0100
commitd862affa8b379318605a75c0f6f8fd00f8fed238 (patch)
tree6c4c8e6fa5227d5e7a214c66d33d884109c74a6f
parent83151f88146676ea6a0e60d52521a0bc342af746 (diff)
Modifying the writeIP function to always write IPv4 in 4 byte long format.
-rw-r--r--dhcpv4/dhcpv4.go2
-rw-r--r--dhcpv4/dhcpv4_test.go10
2 files changed, 12 insertions, 0 deletions
diff --git a/dhcpv4/dhcpv4.go b/dhcpv4/dhcpv4.go
index 57342d8..24e3ed0 100644
--- a/dhcpv4/dhcpv4.go
+++ b/dhcpv4/dhcpv4.go
@@ -429,6 +429,8 @@ func writeIP(b *uio.Lexer, ip net.IP) {
if ip == nil {
b.WriteBytes(zeros[:])
} else {
+ // Converting IP to 4 byte format
+ ip = ip.To4()
b.WriteBytes(ip[:net.IPv4len])
}
}
diff --git a/dhcpv4/dhcpv4_test.go b/dhcpv4/dhcpv4_test.go
index 8ee0cdb..8d6ec54 100644
--- a/dhcpv4/dhcpv4_test.go
+++ b/dhcpv4/dhcpv4_test.go
@@ -7,6 +7,7 @@ import (
"github.com/insomniacslk/dhcp/iana"
"github.com/stretchr/testify/require"
+ "github.com/u-root/u-root/pkg/uio"
)
func TestGetExternalIPv4Addrs(t *testing.T) {
@@ -336,3 +337,12 @@ func TestSummary(t *testing.T) {
" DHCP Message Type: INFORM\n"
require.Equal(t, want, packet.Summary())
}
+
+func Test_withIP(t *testing.T) {
+ buff := uio.NewBigEndianBuffer(make([]byte, 0, 20))
+ // Converting a string into IP, ip1 will be in 16 byte format
+ ip1 := net.ParseIP("10.0.0.1")
+ writeIP(buff, ip1)
+ b := buff.Buffer
+ require.Equal(t, b.Len(), 4, "Testing no of bytes written by writeIP func")
+}