aboutsummaryrefslogtreecommitdiffstats
path: root/conn/conn.go
diff options
context:
space:
mode:
authorDavid Crawshaw <crawshaw@tailscale.com>2019-11-07 11:13:05 -0500
committerDavid Crawshaw <crawshaw@tailscale.com>2019-12-08 20:28:38 -0500
commit82d6ac1f2d93eb9eb561fabf3e789387179a7c7d (patch)
tree80488aed7eff8e075aa935adb9e812fb7cbf29bb /conn/conn.go
parentrwcancel: fix build on macOS (diff)
downloadwireguard-go-82d6ac1f2d93eb9eb561fabf3e789387179a7c7d.tar.xz
wireguard-go-82d6ac1f2d93eb9eb561fabf3e789387179a7c7d.zip
conn: new package that splits out the Bind and Endpoint typesdc/branch
The sticky socket code stays in the device package for now, as it reaches deeply into the peer list. Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
Diffstat (limited to 'conn/conn.go')
-rw-r--r--conn/conn.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/conn/conn.go b/conn/conn.go
new file mode 100644
index 0000000..58a1c5a
--- /dev/null
+++ b/conn/conn.go
@@ -0,0 +1,71 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package conn
+
+import (
+ "errors"
+ "net"
+ "strings"
+)
+
+const (
+ ConnRoutineNumber = 2
+)
+
+/* A Bind handles listening on a port for both IPv6 and IPv4 UDP traffic
+ */
+type Bind interface {
+ LastMark() uint32
+ SetMark(value uint32) error
+ ReceiveIPv6(buff []byte) (int, Endpoint, error)
+ ReceiveIPv4(buff []byte) (int, Endpoint, error)
+ Send(buff []byte, end Endpoint) error
+ Close() error
+}
+
+/* An Endpoint maintains the source/destination caching for a peer
+ *
+ * dst : the remote address of a peer ("endpoint" in uapi terminology)
+ * src : the local address from which datagrams originate going to the peer
+ */
+type Endpoint interface {
+ ClearSrc() // clears the source address
+ SrcToString() string // returns the local source address (ip:port)
+ DstToString() string // returns the destination address (ip:port)
+ DstToBytes() []byte // used for mac2 cookie calculations
+ DstIP() net.IP
+ SrcIP() net.IP
+}
+
+func parseEndpoint(s string) (*net.UDPAddr, error) {
+ // ensure that the host is an IP address
+
+ host, _, err := net.SplitHostPort(s)
+ if err != nil {
+ return nil, err
+ }
+ if i := strings.LastIndexByte(host, '%'); i > 0 && strings.IndexByte(host, ':') >= 0 {
+ // Remove the scope, if any. ResolveUDPAddr below will use it, but here we're just
+ // trying to make sure with a small sanity test that this is a real IP address and
+ // not something that's likely to incur DNS lookups.
+ host = host[:i]
+ }
+ if ip := net.ParseIP(host); ip == nil {
+ return nil, errors.New("Failed to parse IP address: " + host)
+ }
+
+ // parse address and port
+
+ addr, err := net.ResolveUDPAddr("udp", s)
+ if err != nil {
+ return nil, err
+ }
+ ip4 := addr.IP.To4()
+ if ip4 != nil {
+ addr.IP = ip4
+ }
+ return addr, err
+}