aboutsummaryrefslogtreecommitdiffstats
path: root/ipc
diff options
context:
space:
mode:
Diffstat (limited to 'ipc')
-rw-r--r--ipc/uapi_bsd.go74
-rw-r--r--ipc/uapi_linux.go75
-rw-r--r--ipc/uapi_unix.go63
-rw-r--r--ipc/uapi_windows.go2
-rw-r--r--ipc/winpipe/file.go2
-rw-r--r--ipc/winpipe/mksyscall.go2
-rw-r--r--ipc/winpipe/pipe.go2
7 files changed, 71 insertions, 149 deletions
diff --git a/ipc/uapi_bsd.go b/ipc/uapi_bsd.go
index 75cc0e3..ee05cb7 100644
--- a/ipc/uapi_bsd.go
+++ b/ipc/uapi_bsd.go
@@ -2,32 +2,20 @@
/* SPDX-License-Identifier: MIT
*
- * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
*/
package ipc
import (
"errors"
- "fmt"
"net"
"os"
- "path"
"unsafe"
"golang.org/x/sys/unix"
)
-var socketDirectory = "/var/run/wireguard"
-
-const (
- IpcErrorIO = -int64(unix.EIO)
- IpcErrorProtocol = -int64(unix.EPROTO)
- IpcErrorInvalid = -int64(unix.EINVAL)
- IpcErrorPortInUse = -int64(unix.EADDRINUSE)
- socketName = "%s.sock"
-)
-
type UAPIListener struct {
listener net.Listener // unix socket listener
connNew chan net.Conn
@@ -84,10 +72,7 @@ func UAPIListen(name string, file *os.File) (net.Listener, error) {
unixListener.SetUnlinkOnClose(true)
}
- socketPath := path.Join(
- socketDirectory,
- fmt.Sprintf(socketName, name),
- )
+ socketPath := sockPath(name)
// watch for deletion of socket
@@ -146,58 +131,3 @@ func UAPIListen(name string, file *os.File) (net.Listener, error) {
return uapi, nil
}
-
-func UAPIOpen(name string) (*os.File, error) {
-
- // check if path exist
-
- err := os.MkdirAll(socketDirectory, 0755)
- if err != nil && !os.IsExist(err) {
- return nil, err
- }
-
- // open UNIX socket
-
- socketPath := path.Join(
- socketDirectory,
- fmt.Sprintf(socketName, name),
- )
-
- addr, err := net.ResolveUnixAddr("unix", socketPath)
- if err != nil {
- return nil, err
- }
-
- oldUmask := unix.Umask(0077)
- listener, err := func() (*net.UnixListener, error) {
-
- // initial connection attempt
-
- listener, err := net.ListenUnix("unix", addr)
- if err == nil {
- return listener, nil
- }
-
- // check if socket already active
-
- _, err = net.Dial("unix", socketPath)
- if err == nil {
- return nil, errors.New("unix socket in use")
- }
-
- // cleanup & attempt again
-
- err = os.Remove(socketPath)
- if err != nil {
- return nil, err
- }
- return net.ListenUnix("unix", addr)
- }()
- unix.Umask(oldUmask)
-
- if err != nil {
- return nil, err
- }
-
- return listener.File()
-}
diff --git a/ipc/uapi_linux.go b/ipc/uapi_linux.go
index a3c95ca..bda19e9 100644
--- a/ipc/uapi_linux.go
+++ b/ipc/uapi_linux.go
@@ -1,31 +1,18 @@
/* SPDX-License-Identifier: MIT
*
- * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
*/
package ipc
import (
- "errors"
- "fmt"
"net"
"os"
- "path"
"golang.org/x/sys/unix"
"golang.zx2c4.com/wireguard/rwcancel"
)
-var socketDirectory = "/var/run/wireguard"
-
-const (
- IpcErrorIO = -int64(unix.EIO)
- IpcErrorProtocol = -int64(unix.EPROTO)
- IpcErrorInvalid = -int64(unix.EINVAL)
- IpcErrorPortInUse = -int64(unix.EADDRINUSE)
- socketName = "%s.sock"
-)
-
type UAPIListener struct {
listener net.Listener // unix socket listener
connNew chan net.Conn
@@ -84,10 +71,7 @@ func UAPIListen(name string, file *os.File) (net.Listener, error) {
// watch for deletion of socket
- socketPath := path.Join(
- socketDirectory,
- fmt.Sprintf(socketName, name),
- )
+ socketPath := sockPath(name)
uapi.inotifyFd, err = unix.InotifyInit()
if err != nil {
@@ -143,58 +127,3 @@ func UAPIListen(name string, file *os.File) (net.Listener, error) {
return uapi, nil
}
-
-func UAPIOpen(name string) (*os.File, error) {
-
- // check if path exist
-
- err := os.MkdirAll(socketDirectory, 0755)
- if err != nil && !os.IsExist(err) {
- return nil, err
- }
-
- // open UNIX socket
-
- socketPath := path.Join(
- socketDirectory,
- fmt.Sprintf(socketName, name),
- )
-
- addr, err := net.ResolveUnixAddr("unix", socketPath)
- if err != nil {
- return nil, err
- }
-
- oldUmask := unix.Umask(0077)
- listener, err := func() (*net.UnixListener, error) {
-
- // initial connection attempt
-
- listener, err := net.ListenUnix("unix", addr)
- if err == nil {
- return listener, nil
- }
-
- // check if socket already active
-
- _, err = net.Dial("unix", socketPath)
- if err == nil {
- return nil, errors.New("unix socket in use")
- }
-
- // cleanup & attempt again
-
- err = os.Remove(socketPath)
- if err != nil {
- return nil, err
- }
- return net.ListenUnix("unix", addr)
- }()
- unix.Umask(oldUmask)
-
- if err != nil {
- return nil, err
- }
-
- return listener.File()
-}
diff --git a/ipc/uapi_unix.go b/ipc/uapi_unix.go
new file mode 100644
index 0000000..2e0813e
--- /dev/null
+++ b/ipc/uapi_unix.go
@@ -0,0 +1,63 @@
+// +build linux darwin freebsd openbsd
+
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
+ */
+
+package ipc
+
+import (
+ "errors"
+ "fmt"
+ "net"
+ "os"
+
+ "golang.org/x/sys/unix"
+)
+
+const (
+ IpcErrorIO = -int64(unix.EIO)
+ IpcErrorProtocol = -int64(unix.EPROTO)
+ IpcErrorInvalid = -int64(unix.EINVAL)
+ IpcErrorPortInUse = -int64(unix.EADDRINUSE)
+)
+
+var socketDirectory = "/var/run/wireguard"
+
+func sockPath(iface string) string {
+ return fmt.Sprintf("%s/%s.sock", socketDirectory, iface)
+}
+
+func UAPIOpen(name string) (*os.File, error) {
+ if err := os.MkdirAll(socketDirectory, 0755); err != nil {
+ return nil, err
+ }
+
+ socketPath := sockPath(name)
+ addr, err := net.ResolveUnixAddr("unix", socketPath)
+ if err != nil {
+ return nil, err
+ }
+
+ oldUmask := unix.Umask(0077)
+ defer unix.Umask(oldUmask)
+
+ listener, err := net.ListenUnix("unix", addr)
+ if err == nil {
+ return listener.File()
+ }
+
+ // Test socket, if not in use cleanup and try again.
+ if _, err := net.Dial("unix", socketPath); err == nil {
+ return nil, errors.New("unix socket in use")
+ }
+ if err := os.Remove(socketPath); err != nil {
+ return nil, err
+ }
+ listener, err = net.ListenUnix("unix", addr)
+ if err != nil {
+ return nil, err
+ }
+ return listener.File()
+}
diff --git a/ipc/uapi_windows.go b/ipc/uapi_windows.go
index ead0dc5..7fa4f38 100644
--- a/ipc/uapi_windows.go
+++ b/ipc/uapi_windows.go
@@ -1,6 +1,6 @@
/* SPDX-License-Identifier: MIT
*
- * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
*/
package ipc
diff --git a/ipc/winpipe/file.go b/ipc/winpipe/file.go
index 29d02a7..09f2f1c 100644
--- a/ipc/winpipe/file.go
+++ b/ipc/winpipe/file.go
@@ -3,7 +3,7 @@
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2005 Microsoft
- * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
*/
package winpipe
diff --git a/ipc/winpipe/mksyscall.go b/ipc/winpipe/mksyscall.go
index 19ac03a..3675af7 100644
--- a/ipc/winpipe/mksyscall.go
+++ b/ipc/winpipe/mksyscall.go
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2005 Microsoft
- * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
*/
package winpipe
diff --git a/ipc/winpipe/pipe.go b/ipc/winpipe/pipe.go
index 06b3037..c587227 100644
--- a/ipc/winpipe/pipe.go
+++ b/ipc/winpipe/pipe.go
@@ -3,7 +3,7 @@
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2005 Microsoft
- * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
*/
package winpipe