ping: Clean old requests
This commit is contained in:
parent
e6c64e3f18
commit
79e2d3b56d
1 changed files with 20 additions and 10 deletions
|
|
@ -27,7 +27,7 @@ type Destination struct {
|
||||||
routeContext tun.DirectRouteContext
|
routeContext tun.DirectRouteContext
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
requestAccess sync.Mutex
|
requestAccess sync.Mutex
|
||||||
requests map[pingRequest]bool
|
requests map[pingRequest]time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type pingRequest struct {
|
type pingRequest struct {
|
||||||
|
|
@ -68,7 +68,7 @@ func ConnectDestination(
|
||||||
destination: destination,
|
destination: destination,
|
||||||
routeContext: routeContext,
|
routeContext: routeContext,
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
requests: make(map[pingRequest]bool),
|
requests: make(map[pingRequest]time.Time),
|
||||||
}
|
}
|
||||||
go d.loopRead()
|
go d.loopRead()
|
||||||
return d, nil
|
return d, nil
|
||||||
|
|
@ -107,7 +107,8 @@ func (d *Destination) loopRead() {
|
||||||
var requestExists bool
|
var requestExists bool
|
||||||
request := pingRequest{Source: ipHdr.DestinationAddr(), Destination: ipHdr.SourceAddr(), Identifier: icmpHdr.Ident(), Sequence: icmpHdr.Sequence()}
|
request := pingRequest{Source: ipHdr.DestinationAddr(), Destination: ipHdr.SourceAddr(), Identifier: icmpHdr.Ident(), Sequence: icmpHdr.Sequence()}
|
||||||
d.requestAccess.Lock()
|
d.requestAccess.Lock()
|
||||||
if d.requests[request] {
|
_, loaded := d.requests[request]
|
||||||
|
if loaded {
|
||||||
requestExists = true
|
requestExists = true
|
||||||
delete(d.requests, request)
|
delete(d.requests, request)
|
||||||
}
|
}
|
||||||
|
|
@ -133,7 +134,8 @@ func (d *Destination) loopRead() {
|
||||||
var requestExists bool
|
var requestExists bool
|
||||||
request := pingRequest{Source: ipHdr.DestinationAddr(), Destination: ipHdr.SourceAddr(), Identifier: icmpHdr.Ident(), Sequence: icmpHdr.Sequence()}
|
request := pingRequest{Source: ipHdr.DestinationAddr(), Destination: ipHdr.SourceAddr(), Identifier: icmpHdr.Ident(), Sequence: icmpHdr.Sequence()}
|
||||||
d.requestAccess.Lock()
|
d.requestAccess.Lock()
|
||||||
if d.requests[request] {
|
_, loaded := d.requests[request]
|
||||||
|
if loaded {
|
||||||
requestExists = true
|
requestExists = true
|
||||||
delete(d.requests, request)
|
delete(d.requests, request)
|
||||||
}
|
}
|
||||||
|
|
@ -161,9 +163,7 @@ func (d *Destination) WritePacket(packet *buf.Buffer) error {
|
||||||
return E.New("invalid ICMPv4 header")
|
return E.New("invalid ICMPv4 header")
|
||||||
}
|
}
|
||||||
icmpHdr := header.ICMPv4(ipHdr.Payload())
|
icmpHdr := header.ICMPv4(ipHdr.Payload())
|
||||||
d.requestAccess.Lock()
|
d.registerRequest(pingRequest{Source: ipHdr.SourceAddr(), Destination: ipHdr.DestinationAddr(), Identifier: icmpHdr.Ident(), Sequence: icmpHdr.Sequence()})
|
||||||
d.requests[pingRequest{Source: ipHdr.SourceAddr(), Destination: ipHdr.DestinationAddr(), Identifier: icmpHdr.Ident(), Sequence: icmpHdr.Sequence()}] = true
|
|
||||||
d.requestAccess.Unlock()
|
|
||||||
d.logger.TraceContext(d.ctx, "write ICMPv4 echo request from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr(), " id ", icmpHdr.Ident(), " seq ", icmpHdr.Sequence())
|
d.logger.TraceContext(d.ctx, "write ICMPv4 echo request from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr(), " id ", icmpHdr.Ident(), " seq ", icmpHdr.Sequence())
|
||||||
} else {
|
} else {
|
||||||
ipHdr := header.IPv6(packet.Bytes())
|
ipHdr := header.IPv6(packet.Bytes())
|
||||||
|
|
@ -174,14 +174,24 @@ func (d *Destination) WritePacket(packet *buf.Buffer) error {
|
||||||
return E.New("invalid ICMPv6 header")
|
return E.New("invalid ICMPv6 header")
|
||||||
}
|
}
|
||||||
icmpHdr := header.ICMPv6(ipHdr.Payload())
|
icmpHdr := header.ICMPv6(ipHdr.Payload())
|
||||||
d.requestAccess.Lock()
|
d.registerRequest(pingRequest{Source: ipHdr.SourceAddr(), Destination: ipHdr.DestinationAddr(), Identifier: icmpHdr.Ident(), Sequence: icmpHdr.Sequence()})
|
||||||
d.requests[pingRequest{Source: ipHdr.SourceAddr(), Destination: ipHdr.DestinationAddr(), Identifier: icmpHdr.Ident(), Sequence: icmpHdr.Sequence()}] = true
|
|
||||||
d.requestAccess.Unlock()
|
|
||||||
d.logger.TraceContext(d.ctx, "write ICMPv6 echo request from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr(), " id ", icmpHdr.Ident(), " seq ", icmpHdr.Sequence())
|
d.logger.TraceContext(d.ctx, "write ICMPv6 echo request from ", ipHdr.SourceAddr(), " to ", ipHdr.DestinationAddr(), " id ", icmpHdr.Ident(), " seq ", icmpHdr.Sequence())
|
||||||
}
|
}
|
||||||
return d.conn.WriteIP(packet)
|
return d.conn.WriteIP(packet)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Destination) registerRequest(request pingRequest) {
|
||||||
|
d.requestAccess.Lock()
|
||||||
|
defer d.requestAccess.Unlock()
|
||||||
|
now := time.Now()
|
||||||
|
for oldRequest, createdAt := range d.requests {
|
||||||
|
if now.Sub(createdAt) > d.timeout {
|
||||||
|
delete(d.requests, oldRequest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
d.requests[request] = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Destination) Close() error {
|
func (d *Destination) Close() error {
|
||||||
return d.conn.Close()
|
return d.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue