Development discussion of WireGuard
 help / color / mirror / Atom feed
From: Hiram Chirino <hiram@hiramchirino.com>
To: wireguard@lists.zx2c4.com
Subject: [PATCH] conn: set the SO_REUSEADDR and SO_REUSEPORT socket options
Date: Fri, 7 Apr 2023 16:26:38 -0400	[thread overview]
Message-ID: <CAGZ-RDJVoCz++x9XV3QhcY3zmfJtc7+6kxN2YeY9+3dpzasVeA@mail.gmail.com> (raw)

Setting the SO_REUSEADDR and SO_REUSEPORT socket options allows other
applications to reuse the wg UDP port. This can be super handy to do
Things like sending stun requests to stun servers from the wg port.
This lets you discover the wg’s public IP:port mapping if you're behind a
NAT router.

Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
---
conn/controlfns_reuseport_unix.go | 32 ++++++++++++++++++++++++++++
conn/controlfns_reuseport_windows.go | 24 +++++++++++++++++++++
2 files changed, 56 insertions(+)
create mode 100644 conn/controlfns_reuseport_unix.go
create mode 100644 conn/controlfns_reuseport_windows.go

diff --git a/conn/controlfns_reuseport_unix.go
b/conn/controlfns_reuseport_unix.go
new file mode 100644
index 0000000..44d617b
--- /dev/null
+++ b/conn/controlfns_reuseport_unix.go
@@ -0,0 +1,32 @@
+//go:build !plan9 && !windows && !wasm && !js
+
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
+ */
+
+package conn
+
+import (
+ "golang.org/x/sys/unix"
+ "syscall"
+)
+
+func init() {
+
+ controlFns = append(controlFns,
+ func(network, address string, c syscall.RawConn) error {
+ return c.Control(func(fd uintptr) {
+ _ = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
+ })
+ },
+ )
+ controlFns = append(controlFns,
+ func(network, address string, c syscall.RawConn) error {
+ return c.Control(func(fd uintptr) {
+ _ = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
+ })
+ },
+ )
+
+}
diff --git a/conn/controlfns_reuseport_windows.go
b/conn/controlfns_reuseport_windows.go
new file mode 100644
index 0000000..2f8a7bf
--- /dev/null
+++ b/conn/controlfns_reuseport_windows.go
@@ -0,0 +1,24 @@
+//go:build windows
+
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
+ */
+
+package conn
+
+import (
+ "syscall"
+
+ "golang.org/x/sys/windows"
+)
+
+func init() {
+ controlFns = append(controlFns,
+ func(network, address string, c syscall.RawConn) error {
+ return c.Control(func(fd uintptr) {
+ _ = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET,
windows.SO_REUSEADDR, 1)
+ })
+ },
+ )
+}
-- 
2.37.1 (Apple Git-137.1)

                 reply	other threads:[~2023-04-11 10:30 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAGZ-RDJVoCz++x9XV3QhcY3zmfJtc7+6kxN2YeY9+3dpzasVeA@mail.gmail.com \
    --to=hiram@hiramchirino.com \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).