@@ -2,11 +2,12 @@ package dhcp
22
33import (
44 "context"
5- "golang.org/x/sys/windows"
65 "net"
76 "regexp"
87 "time"
98
9+ "golang.org/x/sys/windows"
10+
1011 "github.com/pkg/errors"
1112 "go.uber.org/zap"
1213)
@@ -21,7 +22,7 @@ const (
2122)
2223
2324var (
24- dummyIPAddress = net .IPv4 (169 , 254 , 128 , 10 )
25+ dummyIPAddress = net .IPv4 (169 , 254 , 128 , 10 ) // nolint
2526 // matches if the string fully consists of zero or more alphanumeric, dots, dashes, parentheses, spaces, or underscores
2627 allowedInput = regexp .MustCompile (`^[a-zA-Z0-9._\-\(\) ]*$` )
2728)
@@ -41,7 +42,6 @@ func NewSocket(destAddr windows.SockaddrInet4) (*Socket, error) {
4142 if err != nil {
4243 return ret , errors .Wrap (err , "error creating socket" )
4344 }
44- defer windows .Closesocket (fd )
4545
4646 // Set IP_HDRINCL to indicate that we are including our own IP header
4747 err = windows .SetsockoptInt (fd , windows .IPPROTO_IP , windows .IP_HDRINCL , 1 )
@@ -67,6 +67,7 @@ func (s *Socket) Write(packetBytes []byte) (int, error) {
6767 }
6868 return len (packetBytes ), nil
6969}
70+
7071func (s * Socket ) Read (p []byte ) (n int , err error ) {
7172 n , _ , innerErr := windows .Recvfrom (s .fd , p , 0 )
7273 if innerErr != nil {
@@ -81,7 +82,7 @@ func (s *Socket) Close() error {
8182 return nil
8283 }
8384 // Ensure the file descriptor is closed when done
84- if err := windows .Close (s .fd ); err != nil {
85+ if err := windows .Closesocket (s .fd ); err != nil {
8586 return errors .Wrap (err , "error closing dhcp windows socket" )
8687 }
8788 return nil
@@ -108,8 +109,8 @@ func (c *DHCP) DiscoverRequest(ctx context.Context, macAddress net.HardwareAddr,
108109 }
109110 // ensure we always remove the dummy ip we added from the interface
110111 defer func () {
111- ret , err := c .execClient .ExecuteCommand (ctx , "netsh" , "interface" , "ipv4" , "delete" , "address" , ifName , dummyIPAddressStr )
112- if err != nil {
112+ ret , cleanupErr := c .execClient .ExecuteCommand (ctx , "netsh" , "interface" , "ipv4" , "delete" , "address" , ifName , dummyIPAddressStr )
113+ if cleanupErr != nil {
113114 c .logger .Info ("Could not remove dummy ip on leaving function" , zap .String ("output" , ret ), zap .Error (err ))
114115 }
115116 }()
@@ -139,7 +140,7 @@ func (c *DHCP) DiscoverRequest(ctx context.Context, macAddress net.HardwareAddr,
139140
140141 destAddr := windows.SockaddrInet4 {
141142 Addr : [4 ]byte {255 , 255 , 255 , 255 }, // Destination IP
142- Port : 67 , // Destination Port
143+ Port : dhcpServerPort , // Destination Port
143144 }
144145 // create new socket for writing and reading
145146 sock , err := NewSocket (destAddr )
0 commit comments