Skip to content

Commit

Permalink
udp: ignore refused errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Dec 14, 2024
1 parent 5c4eb49 commit c9cfd04
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
10 changes: 7 additions & 3 deletions face/stream-transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
enc "github.com/zjkmxy/go-ndn/pkg/encoding"
)

func readStreamTransport(
func readTlvStream(
reader io.Reader,
frameCb func([]byte),
onFrame func([]byte),
ignoreError func(error) bool,
) error {
recvBuf := make([]byte, defn.MaxNDNPacketSize*32)
recvOff := 0
Expand All @@ -20,6 +21,9 @@ func readStreamTransport(
readSize, err := reader.Read(recvBuf[recvOff:])
recvOff += readSize
if err != nil {
if ignoreError != nil && ignoreError(err) {
continue
}
if errors.Is(err, io.EOF) {
return nil
}
Expand All @@ -46,7 +50,7 @@ func readStreamTransport(

if recvOff-tlvOff >= tlvSize {
// Packet was successfully received, send up to link service
frameCb(recvBuf[tlvOff : tlvOff+tlvSize])
onFrame(recvBuf[tlvOff : tlvOff+tlvSize])
tlvOff += tlvSize
} else if recvOff-tlvOff > defn.MaxNDNPacketSize {
// Invalid packet, something went wrong
Expand Down
4 changes: 2 additions & 2 deletions face/unicast-tcp-transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ func (t *UnicastTCPTransport) runReceive() {
// The connection can be nil if the initial connection attempt
// failed for a persistent face. In that case we will reconnect.
if t.conn != nil {
err := readStreamTransport(t.conn, func(b []byte) {
err := readTlvStream(t.conn, func(b []byte) {
t.nInBytes += uint64(len(b))
*t.expirationTime = time.Now().Add(tcpLifetime)
t.linkService.handleIncomingFrame(b)
})
}, nil)
if err == nil {
break // EOF
}
Expand Down
7 changes: 6 additions & 1 deletion face/unicast-udp-transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"net"
"strconv"
"strings"
"time"

"github.com/named-data/YaNFD/core"
Expand Down Expand Up @@ -126,10 +127,14 @@ func (t *UnicastUDPTransport) sendFrame(frame []byte) {
func (t *UnicastUDPTransport) runReceive() {
defer t.Close()

err := readStreamTransport(t.conn, func(b []byte) {
err := readTlvStream(t.conn, func(b []byte) {
t.nInBytes += uint64(len(b))
*t.expirationTime = time.Now().Add(udpLifetime)
t.linkService.handleIncomingFrame(b)
}, func(err error) bool {
// Ignore since UDP is a connectionless protocol
// This happens if the other side is not listening (ICMP)
return strings.Contains(err.Error(), "connection refused")
})
if err != nil {
core.LogWarn(t, "Unable to read from socket (", err, ") - Face DOWN")
Expand Down
4 changes: 2 additions & 2 deletions face/unix-stream-transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func (t *UnixStreamTransport) sendFrame(frame []byte) {
func (t *UnixStreamTransport) runReceive() {
defer t.Close()

err := readStreamTransport(t.conn, func(b []byte) {
err := readTlvStream(t.conn, func(b []byte) {
t.nInBytes += uint64(len(b))
t.linkService.handleIncomingFrame(b)
})
}, nil)
if err != nil {
core.LogWarn(t, "Unable to read from socket (", err, ") - Face DOWN")
}
Expand Down

0 comments on commit c9cfd04

Please sign in to comment.