commit cdbe1d7d5e05803418c160eaac35442489398f49 from: Stefan Sperling via: Thomas Adam date: Sat Aug 06 20:57:29 2022 UTC move use of sin_len out of gotwebd's parse.y ok tracey commit - 82a8b7ecf5c23c6bb7dc0b1bf17d5608316834f9 commit + cdbe1d7d5e05803418c160eaac35442489398f49 blob - 1f6c99727d2651417486997179bd6c788573e6ca blob + 283b66d78660ef036a7c9e31cd7923e69844dbe3 --- gotwebd/parse.y +++ gotwebd/parse.y @@ -48,7 +48,7 @@ #include "proc.h" #include "gotwebd.h" -#include "got_compat.h" +#include "got_sockaddr.h" TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files); static struct file { @@ -1046,12 +1046,7 @@ host_v4(const char *s) if ((h = calloc(1, sizeof(*h))) == NULL) fatal(__func__); sain = (struct sockaddr_in *)&h->ss; -/* TA: Iffy... */ -#ifndef __linux__ - sain->sin_len = sizeof(struct sockaddr_in); -#endif - 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 @@ -1063,7 +1058,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)); @@ -1074,16 +1069,9 @@ host_v6(const char *s) if ((h = calloc(1, sizeof(*h))) == NULL) fatal(__func__); sa_in6 = (struct sockaddr_in6 *)&h->ss; -/* TA: Iffy... */ -#ifndef __linux__ - sa_in6->sin6_len = sizeof(struct sockaddr_in6); -#endif - 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 */ @@ -1146,21 +1134,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; -/* TA: Iffy... */ -#ifndef __linux__ - sain->sin_len = sizeof(struct sockaddr_in); -#endif - 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; -/* TA: Iffy... */ -#ifndef __linux__ - sin6->sin6_len = sizeof(struct sockaddr_in6); -#endif - 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); @@ -1218,23 +1200,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; -/* TA: Iffy... */ -#ifndef __linux__ - sain->sin_len = sizeof(struct sockaddr_in); -#endif - 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; -/* TA: Iffy... */ -#ifndef __linux__ - sin6->sin6_len = sizeof(struct sockaddr_in6); -#endif - 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 + * + * 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 + * + * 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 +#include + +#include + +#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; +}