Commit Diff


commit - 76e495bdc01c788b25c5f79d55da328e0b5d7d94
commit + 86b4b772a2c289053be29f6df2ad411dc853e05a
blob - 4c0096efb52bb79e4a8f7e5f09c1b1d953009636
blob + 20032d2e4a12c2f228cca01ced963187b2238556
--- gotwebd/Makefile
+++ gotwebd/Makefile
@@ -17,7 +17,7 @@ SRCS +=		blame.c commit_graph.c delta.c diff.c \
 		gotconfig.c diff_main.c diff_atomize_text.c diff_myers.c \
 		diff_output.c diff_output_plain.c diff_output_unidiff.c \
 		diff_output_edscript.c diff_patience.c bloom.c murmurhash2.c \
-		worktree_open.c patch.c sigs.c date.c
+		worktree_open.c patch.c sigs.c date.c sockaddr.c
 
 MAN =		${PROG}.conf.5 ${PROG}.8
 
blob - 635453ffdb88b39fa87eb284d74cc6612f67f7f8
blob + e624bc6b2182b136d6389e4baad9c214e8c5c4fd
--- gotwebd/parse.y
+++ gotwebd/parse.y
@@ -49,6 +49,7 @@
 
 #include "proc.h"
 #include "gotwebd.h"
+#include "got_sockaddr.h"
 
 TAILQ_HEAD(files, file)		 files = TAILQ_HEAD_INITIALIZER(files);
 static struct file {
@@ -1046,9 +1047,7 @@ host_v4(const char *s)
 	if ((h = calloc(1, sizeof(*h))) == NULL)
 		fatal(__func__);
 	sain = (struct sockaddr_in *)&h->ss;
-	sain->sin_len = sizeof(struct sockaddr_in);
-	sain->sin_family = AF_INET;
-	sain->sin_addr.s_addr = ina.s_addr;
+	got_sockaddr_inet_init(sain, &ina);
 	if (sain->sin_addr.s_addr == INADDR_ANY)
 		h->prefixlen = 0; /* 0.0.0.0 address */
 	else
@@ -1060,7 +1059,7 @@ struct address *
 host_v6(const char *s)
 {
 	struct addrinfo hints, *res;
-	struct sockaddr_in6 *sa_in6;
+	struct sockaddr_in6 *sa_in6, *ra;
 	struct address *h = NULL;
 
 	memset(&hints, 0, sizeof(hints));
@@ -1071,13 +1070,9 @@ host_v6(const char *s)
 		if ((h = calloc(1, sizeof(*h))) == NULL)
 			fatal(__func__);
 		sa_in6 = (struct sockaddr_in6 *)&h->ss;
-		sa_in6->sin6_len = sizeof(struct sockaddr_in6);
-		sa_in6->sin6_family = AF_INET6;
-		memcpy(&sa_in6->sin6_addr,
-		    &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
-		    sizeof(sa_in6->sin6_addr));
-		sa_in6->sin6_scope_id =
-		    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
+		ra = (struct sockaddr_in6 *)res->ai_addr;
+		got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr,
+		    ra->sin6_scope_id);
 		if (memcmp(&sa_in6->sin6_addr, &in6addr_any,
 		    sizeof(sa_in6->sin6_addr)) == 0)
 			h->prefixlen = 0; /* any address */
@@ -1140,15 +1135,15 @@ host_dns(const char *s, struct addresslist *al, int ma
 		h->prefixlen = -1; /* host address */
 
 		if (res->ai_family == AF_INET) {
+			struct sockaddr_in *ra;
 			sain = (struct sockaddr_in *)&h->ss;
-			sain->sin_len = sizeof(struct sockaddr_in);
-			sain->sin_addr.s_addr = ((struct sockaddr_in *)
-			    res->ai_addr)->sin_addr.s_addr;
+			ra = (struct sockaddr_in *)res->ai_addr;
+			got_sockaddr_inet_init(sain, &ra->sin_addr);
 		} else {
+			struct sockaddr_in6 *ra;
 			sin6 = (struct sockaddr_in6 *)&h->ss;
-			sin6->sin6_len = sizeof(struct sockaddr_in6);
-			memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
-			    res->ai_addr)->sin6_addr, sizeof(struct in6_addr));
+			ra = (struct sockaddr_in6 *)res->ai_addr;
+			got_sockaddr_inet6_init(sin6, &ra->sin6_addr, 0);
 		}
 
 		TAILQ_INSERT_HEAD(al, h, entry);
@@ -1206,17 +1201,16 @@ host_if(const char *s, struct addresslist *al, int max
 		h->prefixlen = -1; /* host address */
 
 		if (af == AF_INET) {
+			struct sockaddr_in *ra;
 			sain = (struct sockaddr_in *)&h->ss;
-			sain->sin_len = sizeof(struct sockaddr_in);
-			sain->sin_addr.s_addr = ((struct sockaddr_in *)
-			    p->ifa_addr)->sin_addr.s_addr;
+			ra = (struct sockaddr_in *)p->ifa_addr;
+			got_sockaddr_inet_init(sain, &ra->sin_addr);
 		} else {
+			struct sockaddr_in6 *ra;
 			sin6 = (struct sockaddr_in6 *)&h->ss;
-			sin6->sin6_len = sizeof(struct sockaddr_in6);
-			memcpy(&sin6->sin6_addr, &((struct sockaddr_in6 *)
-			    p->ifa_addr)->sin6_addr, sizeof(struct in6_addr));
-			sin6->sin6_scope_id = ((struct sockaddr_in6 *)
-			    p->ifa_addr)->sin6_scope_id;
+			ra = (struct sockaddr_in6 *)p->ifa_addr;
+			got_sockaddr_inet6_init(sin6, &ra->sin6_addr,
+			    ra->sin6_scope_id);
 		}
 
 		TAILQ_INSERT_HEAD(al, h, entry);
blob - /dev/null
blob + 809c5c9b65985619eabf4ad948d80348f606762c (mode 644)
--- /dev/null
+++ include/got_sockaddr.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+void got_sockaddr_inet_init(struct sockaddr_in *in, struct in_addr *ina);
+void got_sockaddr_inet6_init(struct sockaddr_in6 *in6, struct in6_addr *in6a,
+    uint32_t sin6_scope_id);
blob - /dev/null
blob + 7a76593a59f98468b3891e3823c6d7fd0cfa960d (mode 644)
--- /dev/null
+++ lib/sockaddr.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include <string.h>
+
+#include "got_sockaddr.h"
+
+/*
+ * These interfaces wrap BSD-specific internals of internet address
+ * data structures in a single compilation unit, allowing got-portable
+ * to override them as needed, without a need for #ifdef macros.
+ */
+
+void
+got_sockaddr_inet_init(struct sockaddr_in *in, struct in_addr *ina)
+{
+	in->sin_len = sizeof(struct sockaddr_in); /* BSD-specific */
+	in->sin_family = AF_INET;
+	in->sin_addr.s_addr = ina->s_addr;
+}
+
+void
+got_sockaddr_inet6_init(struct sockaddr_in6 *in6, struct in6_addr *in6a,
+    uint32_t sin6_scope_id)
+{
+	in6->sin6_len = sizeof(struct sockaddr_in6); /* BSD-specific */
+	in6->sin6_family = AF_INET6;
+	memcpy(&in6->sin6_addr, in6a, sizeof(in6->sin6_addr));
+	in6->sin6_scope_id = sin6_scope_id;
+}