-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check IPv4 gateway by resolving gateway MAC in underlay subnets
- Loading branch information
1 parent
90f62fd
commit 3837b0a
Showing
6 changed files
with
138 additions
and
42 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
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,52 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/mdlayher/arp" | ||
) | ||
|
||
func Arping(nic, srcIP, dstIP string, timeout time.Duration, maxRetry int) (net.HardwareAddr, int, error) { | ||
target := net.ParseIP(dstIP) | ||
if target == nil { | ||
return nil, 0, fmt.Errorf("%s is not a valid IP address", dstIP) | ||
} | ||
|
||
var count int | ||
var err error | ||
var ifi *net.Interface | ||
for ; count < maxRetry; count++ { | ||
if ifi, err = net.InterfaceByName(nic); err == nil { | ||
break | ||
} | ||
time.Sleep(timeout) | ||
} | ||
if err != nil { | ||
return nil, count, fmt.Errorf("failed to get interface %s: %v", nic, err) | ||
} | ||
|
||
var client *arp.Client | ||
for ; count < maxRetry; count++ { | ||
if client, err = arp.Dial(ifi); err == nil { | ||
defer client.Close() | ||
break | ||
} | ||
time.Sleep(timeout) | ||
} | ||
if err != nil { | ||
return nil, count, fmt.Errorf("failed to set up ARP client: %v", err) | ||
} | ||
|
||
for ; count < maxRetry; count++ { | ||
if err = client.SetDeadline(time.Now().Add(timeout)); err != nil { | ||
continue | ||
} | ||
if mac, err := client.Resolve(target); err == nil { | ||
return mac, count + 1, nil | ||
} | ||
} | ||
|
||
return nil, count, fmt.Errorf("resolve MAC address of %s timeout: %v", dstIP, err) | ||
} |