mirror of https://github.com/libp2p/go-libp2p.git
Will
5 years ago
committed by
GitHub
9 changed files with 64 additions and 180 deletions
@ -1,10 +0,0 @@ |
|||
// +build linux
|
|||
|
|||
package libp2pquic |
|||
|
|||
import "golang.org/x/sys/unix" |
|||
|
|||
// We just need netlink_route here.
|
|||
// note: We should avoid the use of netlink_xfrm or netlink_netfilter has it is
|
|||
// not allowed by Android in his base policy.
|
|||
var SupportedNlFamilies = []int{unix.NETLINK_ROUTE} |
@ -1,9 +0,0 @@ |
|||
// +build !linux
|
|||
// +build !windows
|
|||
|
|||
package libp2pquic |
|||
|
|||
import "github.com/vishvananda/netlink/nl" |
|||
|
|||
// SupportedNlFamilies is the default netlink families used by the netlink package
|
|||
var SupportedNlFamilies = nl.SupportedNlFamilies |
@ -1,42 +0,0 @@ |
|||
// +build linux
|
|||
|
|||
package libp2pquic |
|||
|
|||
import ( |
|||
"net" |
|||
|
|||
. "github.com/onsi/ginkgo" |
|||
. "github.com/onsi/gomega" |
|||
) |
|||
|
|||
var _ = Describe("Reuse (on Linux)", func() { |
|||
var reuse *reuse |
|||
|
|||
BeforeEach(func() { |
|||
var err error |
|||
reuse, err = newReuse(nil) |
|||
Expect(err).ToNot(HaveOccurred()) |
|||
}) |
|||
|
|||
Context("creating and reusing connections", func() { |
|||
AfterEach(func() { closeAllConns(reuse) }) |
|||
|
|||
It("reuses a connection it created for listening on a specific interface", func() { |
|||
raddr, err := net.ResolveUDPAddr("udp4", "1.1.1.1:1234") |
|||
Expect(err).ToNot(HaveOccurred()) |
|||
ips, err := reuse.getSourceIPs("udp4", raddr) |
|||
Expect(err).ToNot(HaveOccurred()) |
|||
Expect(ips).ToNot(BeEmpty()) |
|||
// listen
|
|||
addr, err := net.ResolveUDPAddr("udp4", ips[0].String()+":0") |
|||
Expect(err).ToNot(HaveOccurred()) |
|||
lconn, err := reuse.Listen("udp4", addr) |
|||
Expect(err).ToNot(HaveOccurred()) |
|||
Expect(lconn.GetCount()).To(Equal(1)) |
|||
// dial
|
|||
conn, err := reuse.Dial("udp4", raddr) |
|||
Expect(err).ToNot(HaveOccurred()) |
|||
Expect(conn.GetCount()).To(Equal(2)) |
|||
}) |
|||
}) |
|||
}) |
@ -1,68 +0,0 @@ |
|||
// +build !windows
|
|||
|
|||
package libp2pquic |
|||
|
|||
import ( |
|||
"net" |
|||
|
|||
filter "github.com/libp2p/go-maddr-filter" |
|||
|
|||
"github.com/vishvananda/netlink" |
|||
) |
|||
|
|||
type reuse struct { |
|||
reuseBase |
|||
|
|||
handle *netlink.Handle // Only set on Linux. nil on other systems.
|
|||
} |
|||
|
|||
func newReuse(filters *filter.Filters) (*reuse, error) { |
|||
handle, err := netlink.NewHandle(SupportedNlFamilies...) |
|||
if err == netlink.ErrNotImplemented { |
|||
handle = nil |
|||
} else if err != nil { |
|||
return nil, err |
|||
} |
|||
return &reuse{ |
|||
reuseBase: newReuseBase(filters), |
|||
handle: handle, |
|||
}, nil |
|||
} |
|||
|
|||
// Get the source IP that the kernel would use for dialing.
|
|||
// This only works on Linux.
|
|||
// On other systems, this returns an empty slice of IP addresses.
|
|||
func (r *reuse) getSourceIPs(network string, raddr *net.UDPAddr) ([]net.IP, error) { |
|||
if r.handle == nil { |
|||
return nil, nil |
|||
} |
|||
|
|||
routes, err := r.handle.RouteGet(raddr.IP) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
ips := make([]net.IP, 0, len(routes)) |
|||
for _, route := range routes { |
|||
ips = append(ips, route.Src) |
|||
} |
|||
return ips, nil |
|||
} |
|||
|
|||
func (r *reuse) Dial(network string, raddr *net.UDPAddr) (*reuseConn, error) { |
|||
ips, err := r.getSourceIPs(network, raddr) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
r.mutex.Lock() |
|||
defer r.mutex.Unlock() |
|||
|
|||
conn, err := r.dialLocked(network, raddr, ips) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
conn.IncreaseCount() |
|||
r.maybeStartGarbageCollector() |
|||
return conn, nil |
|||
} |
@ -1,30 +0,0 @@ |
|||
// +build windows
|
|||
|
|||
package libp2pquic |
|||
|
|||
import ( |
|||
"net" |
|||
|
|||
filter "github.com/libp2p/go-maddr-filter" |
|||
) |
|||
|
|||
type reuse struct { |
|||
reuseBase |
|||
} |
|||
|
|||
func newReuse(filters *filter.Filters) (*reuse, error) { |
|||
return &reuse{reuseBase: newReuseBase(filters)}, nil |
|||
} |
|||
|
|||
func (r *reuse) Dial(network string, raddr *net.UDPAddr) (*reuseConn, error) { |
|||
r.mutex.Lock() |
|||
defer r.mutex.Unlock() |
|||
|
|||
conn, err := r.dialLocked(network, raddr, nil) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
conn.IncreaseCount() |
|||
r.maybeStartGarbageCollector() |
|||
return conn, nil |
|||
} |
Loading…
Reference in new issue