From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2 3/4] Add socket address initialization functions to tst_net library
Date: Fri, 4 Oct 2019 14:42:35 +0200 [thread overview]
Message-ID: <20191004124235.GC5442@rei.lan> (raw)
In-Reply-To: <20190926151331.25070-4-mdoucha@suse.cz>
Hi!
> Signed-off-by: Martin Doucha <mdoucha@suse.cz>
> ---
> include/tst_net.h | 16 +++++++++++
> lib/tst_net.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 87 insertions(+)
>
> diff --git a/include/tst_net.h b/include/tst_net.h
> index 740f25bac..2c958da13 100644
> --- a/include/tst_net.h
> +++ b/include/tst_net.h
> @@ -17,6 +17,9 @@
>
> #include <arpa/inet.h>
> #include <stdio.h>
> +#include <sys/types.h>
> +#include <netinet/in.h>
> +#include <netinet/ip.h>
>
> #define MAX_IPV4_PREFIX 32
> #define MAX_IPV6_PREFIX 128
> @@ -49,3 +52,16 @@ int safe_atoi(const char *s, int *ret_i);
> int get_prefix(const char *ip_str, int is_ipv6);
> void get_in_addr(const char *ip_str, struct in_addr *ip);
> void get_in6_addr(const char *ip_str, struct in6_addr *ip6);
> +
> +/*
> + * Find valid connection address for a given bound socket
> + */
> +socklen_t get_connect_address(int sock, struct sockaddr_storage *addr);
> +
> +/*
> + * Initialize AF_INET/AF_INET6 socket address structure with address and port
> + */
> +void init_sockaddr_inet(struct sockaddr_in *sa, const char *ip_str, uint16_t port);
> +void init_sockaddr_inet_bin(struct sockaddr_in *sa, uint32_t ip_val, uint16_t port);
> +void init_sockaddr_inet6(struct sockaddr_in6 *sa, const char *ip_str, uint16_t port);
> +void init_sockaddr_inet6_bin(struct sockaddr_in6 *sa, const struct in6_addr *ip_val, uint16_t port);
> diff --git a/lib/tst_net.c b/lib/tst_net.c
> index 4166641f1..4ccd81eb9 100644
> --- a/lib/tst_net.c
> +++ b/lib/tst_net.c
> @@ -1,5 +1,6 @@
> /*
> * Copyright (c) 2017 Petr Vorel <pvorel@suse.cz>
> + * Copyright (c) 2019 Martin Doucha <mdoucha@suse.cz>
> *
> * This program is free software: you can redistribute it and/or modify
> * it under the terms of the GNU General Public License as published by
> @@ -132,3 +133,73 @@ void get_in6_addr(const char *ip_str, struct in6_addr *ip6)
> if (inet_pton(AF_INET6, ip_str, ip6) <= 0)
> tst_brk_comment("bad IPv6 address: '%s'", ip_str);
> }
> +
> +socklen_t get_connect_address(int sock, struct sockaddr_storage *addr)
> +{
> + struct sockaddr_in *inet_ptr;
> + struct sockaddr_in6 *inet6_ptr;
> + size_t tmp_size;
> + socklen_t ret = sizeof(*addr);
> +
> + SAFE_GETSOCKNAME(sock, (struct sockaddr*)addr, &ret);
> +
> + // Sanitize wildcard addresses
> + switch (addr->ss_family) {
> + case AF_INET:
> + inet_ptr = (struct sockaddr_in*)addr;
> +
> + switch (ntohl(inet_ptr->sin_addr.s_addr)) {
> + case INADDR_ANY:
> + case INADDR_BROADCAST:
> + inet_ptr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
> + break;
> + }
> +
> + break;
> +
> + case AF_INET6:
> + inet6_ptr = (struct sockaddr_in6*)addr;
> + tmp_size = sizeof(struct in6_addr);
> +
> + if (!memcmp(&inet6_ptr->sin6_addr, &in6addr_any, tmp_size)) {
> + memcpy(&inet6_ptr->sin6_addr, &in6addr_loopback,
> + tmp_size);
> + }
> +
> + break;
> + }
> +
> + return ret;
> +}
> +
> +void init_sockaddr_inet(struct sockaddr_in *sa, const char *ip_str, uint16_t port)
> +{
> + memset(sa, 0, sizeof(struct sockaddr_in));
> + sa->sin_family = AF_INET;
> + sa->sin_port = htons(port);
> + get_in_addr(ip_str, &sa->sin_addr);
> +}
> +
> +void init_sockaddr_inet_bin(struct sockaddr_in *sa, uint32_t ip_val, uint16_t port)
> +{
> + memset(sa, 0, sizeof(struct sockaddr_in));
> + sa->sin_family = AF_INET;
> + sa->sin_port = htons(port);
> + sa->sin_addr.s_addr = htonl(ip_val);
> +}
> +
> +void init_sockaddr_inet6(struct sockaddr_in6 *sa, const char *ip_str, uint16_t port)
> +{
> + memset(sa, 0, sizeof(struct sockaddr_in6));
> + sa->sin6_family = AF_INET6;
> + sa->sin6_port = htons(port);
> + get_in6_addr(ip_str, &sa->sin6_addr);
> +}
> +
> +void init_sockaddr_inet6_bin(struct sockaddr_in6 *sa, const struct in6_addr *ip_val, uint16_t port)
> +{
> + memset(sa, 0, sizeof(struct sockaddr_in6));
> + sa->sin6_family = AF_INET6;
> + sa->sin6_port = htons(port);
> + memcpy(&sa->sin6_addr, ip_val, sizeof(struct in6_addr));
> +}
These init functions should really be just static inline functions
instead, there is no point in putting that code into a library code.
--
Cyril Hrubis
chrubis@suse.cz
next prev parent reply other threads:[~2019-10-04 12:42 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-26 15:13 [LTP] [PATCH v2 0/4] Increase bind() converage - GH#538 Martin Doucha
2019-09-26 15:13 ` [LTP] [PATCH v2 1/4] Update syscalls/bind02 to new API Martin Doucha
2019-10-04 12:36 ` Cyril Hrubis
2019-09-26 15:13 ` [LTP] [PATCH v2 2/4] Create separate .c file for include/tst_net.h Martin Doucha
2019-10-04 12:40 ` Cyril Hrubis
2019-09-26 15:13 ` [LTP] [PATCH v2 3/4] Add socket address initialization functions to tst_net library Martin Doucha
2019-10-04 12:42 ` Cyril Hrubis [this message]
2019-09-26 15:13 ` [LTP] [PATCH v2 4/4] Add connection tests for bind() Martin Doucha
2019-10-04 13:03 ` Cyril Hrubis
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=20191004124235.GC5442@rei.lan \
--to=chrubis@suse.cz \
--cc=ltp@lists.linux.it \
/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