-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change unicast connection of dhcpv4 client as a raw socket
If the network manager in the operating system uses DHCP to obtain an IP for the network card, it will occupy the DHCP client port. In this case the release function will fail because the unicast connection with UDP socket needs the same port. Use a raw udp socket instead of a datagram udp socket to solve it. Signed-off-by: yaocw2020 <[email protected]>
- Loading branch information
Showing
5 changed files
with
201 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package nclient4 | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
const ( | ||
linkName = "neigh0" | ||
ipStr = "10.99.0.1" | ||
macStr = "aa:bb:cc:dd:00:01" | ||
) | ||
|
||
func TestGetHwAddrFromLocalCache(t *testing.T) { | ||
mac, err := net.ParseMAC(macStr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
ip := net.ParseIP(ipStr) | ||
|
||
if err := addNeigh(ip, mac); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
if err := delNeigh(ip, mac); err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
ifc, err := net.InterfaceByName(linkName) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if hw, err := getHwAddr(ifc, ip); err != nil && hw != nil && hw.String() == macStr { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestGetHwAddrOfLoopback(t *testing.T) { | ||
lo, err := net.InterfaceByName("lo") | ||
if err != nil { | ||
t.Fatalf("get the loopback interface failed, err: %s", err.Error()) | ||
} | ||
if _, err := getHwAddr(lo, net.ParseIP("127.0.0.1")); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func addNeigh(ip net.IP, mac net.HardwareAddr) error { | ||
dummy := netlink.Dummy{LinkAttrs: netlink.LinkAttrs{Name: linkName}} | ||
if err := netlink.LinkAdd(&dummy); err != nil { | ||
return err | ||
} | ||
newlink, err := netlink.LinkByName(dummy.Name) | ||
if err != nil { | ||
return err | ||
} | ||
dummy.Index = newlink.Attrs().Index | ||
|
||
return netlink.NeighAdd(&netlink.Neigh{ | ||
LinkIndex: dummy.Index, | ||
State: netlink.NUD_REACHABLE, | ||
IP: ip, | ||
HardwareAddr: mac, | ||
}) | ||
} | ||
|
||
func delNeigh(ip net.IP, mac net.HardwareAddr) error { | ||
dummy, err := netlink.LinkByName(linkName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return netlink.NeighDel(&netlink.Neigh{ | ||
LinkIndex: dummy.Attrs().Index, | ||
State: netlink.NUD_REACHABLE, | ||
IP: ip, | ||
HardwareAddr: mac, | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters