mirror of
https://github.com/soypat/lneto.git
synced 2026-08-20 14:39:02 +00:00
Add ICMPv4 and lneto.StackNode and deprecate internet.StackNode (#65)
* begin adding icmp client * rely on anon structs * add tests * tests passing * icmp fleshed out * rework icmp to include ip addr * rename StackAsync.Demux/Encapsulate to RecvEthernet and SendEthernet * remove legacy unreachable TCP tests * remove uses of deprecated internet.StackNode in preference of lneto.StackNode * documentation * rename methods to signal no I/O happening
This commit is contained in:
@@ -2,9 +2,11 @@ package xnet
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/soypat/lneto"
|
||||
"github.com/soypat/lneto/dhcpv4"
|
||||
"github.com/soypat/lneto/tcp"
|
||||
)
|
||||
@@ -58,6 +60,33 @@ func (s StackBlocking) DoDHCPv4(reqAddr [4]byte, timeout time.Duration) (*DHCPRe
|
||||
return s.async.ResultDHCP()
|
||||
}
|
||||
|
||||
func (s StackBlocking) DoPing(hostAddr netip.Addr, timeout time.Duration) (roundtrip time.Duration, err error) {
|
||||
if !hostAddr.Is4() {
|
||||
return 0, lneto.ErrInvalidAddr
|
||||
}
|
||||
var buf [16]byte
|
||||
s.async.prandRead(buf[:])
|
||||
key, err := s.async.icmp.PingStart(hostAddr.As4(), buf[:], 56) // size=56 so ICMP size is 64, like linux.
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
start := time.Now()
|
||||
sleep := timeout / maxIter
|
||||
for i := 0; i < maxIter; i++ {
|
||||
time.Sleep(sleep)
|
||||
elapsed := time.Since(start)
|
||||
completed, exists := s.async.icmp.PingPop(key)
|
||||
if !exists {
|
||||
return 0, net.ErrClosed // lneto.ErrAborted
|
||||
} else if completed {
|
||||
return elapsed, nil
|
||||
} else if elapsed > timeout {
|
||||
break
|
||||
}
|
||||
}
|
||||
return 0, errDeadlineExceed
|
||||
}
|
||||
|
||||
func (s StackBlocking) DoNTP(hostAddr netip.Addr, timeout time.Duration) (offset time.Duration, err error) {
|
||||
err = s.async.StartNTP(hostAddr)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user