Development discussion of WireGuard
 help / color / mirror / Atom feed
From: "Daniel Gröber" <dxld@darkboxed.org>
To: wireguard@lists.zx2c4.com
Cc: "Jason A . Donenfeld" <Jason@zx2c4.com>,
	"Daniel Gröber" <dxld@darkboxed.org>
Subject: [PATCH] Check sockaddr_inet field offsets against system sockaddr structs
Date: Sun, 20 Aug 2023 20:10:34 +0200	[thread overview]
Message-ID: <20230820181034.2385772-1-dxld@darkboxed.org> (raw)

Some systems may have the sockaddr fields in a different arrangement and
need #ifdef'ing this makes this obvious to any future porters.

Signed-off-by: Daniel Gröber <dxld@darkboxed.org>
---
 src/containers.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/src/containers.h b/src/containers.h
index 2f3d88f..357df77 100644
--- a/src/containers.h
+++ b/src/containers.h
@@ -8,6 +8,8 @@
 
 #include <stdint.h>
 #include <stdlib.h>
+#include <stddef.h>
+#include <assert.h>
 #include <time.h>
 #include <sys/socket.h>
 #include <net/if.h>
@@ -44,6 +46,25 @@ struct sockaddr_inet {
 	};
 };
 
+#if defined(_MSC_VER)
+#define static_assert(x) static_assert(x, #x)
+#elif !defined(static_assert)
+#define static_assert(x) _Static_assert((x), #x)
+#endif
+
+static_assert(offsetof(struct sockaddr_in, sin_port) ==
+	      offsetof(struct sockaddr_inet, sinet_port));
+static_assert(offsetof(struct sockaddr_in6, sin6_port) ==
+	      offsetof(struct sockaddr_inet, sinet_port));
+
+#define assert_offsets_match(tyx, tyy, field) \
+	static_assert(offsetof(tyx, field) == offsetof(tyy, field))
+
+assert_offsets_match(struct sockaddr_in, struct sockaddr_inet, sin_addr);
+assert_offsets_match(struct sockaddr_in6, struct sockaddr_inet, sin6_flowinfo);
+assert_offsets_match(struct sockaddr_in6, struct sockaddr_inet, sin6_addr);
+assert_offsets_match(struct sockaddr_in6, struct sockaddr_inet, sin6_scope_id);
+
 struct wgallowedip {
 	uint16_t family;
 	union {
-- 
2.39.2


                 reply	other threads:[~2023-08-20 18:13 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=20230820181034.2385772-1-dxld@darkboxed.org \
    --to=dxld@darkboxed.org \
    --cc=Jason@zx2c4.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).