"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Tracey Emery <tracey@traceyemery.net>
Subject:
Re: move use of sin_len out of gotwebd's parse.y
To:
gameoftrees@openbsd.org
Date:
Fri, 29 Jul 2022 07:34:48 -0600

Download raw body.

Thread
On Fri, Jul 29, 2022 at 12:43:47PM +0200, Stefan Sperling wrote:
> This should make gotwebd's parse.y easier for -potable.
> 
> Some use of sin_len remains elsewhere but that requires some
> additional work to fix.
> 
> ok?

ok

>  
> diff b95ef6ae73ab42b2a473eb68c926c77efa14ca29 06ba6b54c127173003258cd28d72fd0b8cf6ffbf
> commit - b95ef6ae73ab42b2a473eb68c926c77efa14ca29
> commit + 06ba6b54c127173003258cd28d72fd0b8cf6ffbf
> blob - 2411823014ec40f7de384de2b1a3b1f749884415
> blob + 3de2c1d5d79beee64f701c9a244762aa0a6c9806
> --- 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;
> +}
> 

-- 

Tracey Emery