Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Jan 30, 2025
1 parent 92a5d91 commit 33a5f73
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions p2p/net/nat/internal/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type ErrNoNATFound struct {
Errs []error
}

func (e ErrNoNATFound) Unwrap() []error {
return e.Errs
}

func (e ErrNoNATFound) Error() string {
var errStrs []string
for _, err := range e.Errs {
Expand Down
12 changes: 12 additions & 0 deletions p2p/net/nat/internal/nat/upnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nat

import (
"context"
"fmt"
"net"
"net/url"
"strings"
Expand Down Expand Up @@ -52,6 +53,9 @@ func discoverUPNP_GenIGDev(ctx context.Context) (nats []NAT, errs []error) {
return
}

// Limit the number of InternetGateways we'll query. Normally we'd only
// expect 1 or 2, but in case of a weird network we also don't want to do
// too much work.
const maxIGDevs = 3
foundIGDevs := 0
for _, Service := range DeviceList {
Expand Down Expand Up @@ -79,6 +83,10 @@ func discoverUPNP_GenIGDev(ctx context.Context) (nats []NAT, errs []error) {
return
}

// serviceVisitor is a vistor function that visits all services of a root
// device and collects NATs.
//
// It works on InternetGateway V1 and V2 devices. For V1 devices, V2 services should not be encountered, and the visitor will collect an error in that case.
func serviceVisitor(ctx context.Context, rootDevice *goupnp.RootDevice, outNats *[]NAT, outErrs *[]error) func(srv *goupnp.Service) {
return func(srv *goupnp.Service) {
if ctx.Err() != nil {
Expand All @@ -99,6 +107,10 @@ func serviceVisitor(ctx context.Context, rootDevice *goupnp.RootDevice, outNats
}

case internetgateway2.URN_WANIPConnection_2:
if rootDevice.Device.DeviceType == internetgateway2.URN_WANConnectionDevice_1 {
*outErrs = append(*outErrs, fmt.Errorf("found V2 service on V1 device"))
return
}
client := &internetgateway2.WANIPConnection2{ServiceClient: goupnp.ServiceClient{
SOAPClient: srv.NewSOAPClient(),
RootDevice: rootDevice,
Expand Down

0 comments on commit 33a5f73

Please sign in to comment.