All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2 2/4] Create separate .c file for include/tst_net.h
Date: Fri, 4 Oct 2019 14:40:58 +0200	[thread overview]
Message-ID: <20191004124057.GB5442@rei.lan> (raw)
In-Reply-To: <20190926151331.25070-3-mdoucha@suse.cz>

Hi!
> Signed-off-by: Martin Doucha <mdoucha@suse.cz>
> ---
>  include/tst_net.h | 118 +++-------------------------------------
>  lib/tst_net.c     | 134 ++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 142 insertions(+), 110 deletions(-)
>  create mode 100644 lib/tst_net.c

What is the reason for this change?

With this you are polluting the LTP test library with symbols that are
not prefixed by tst_. If nothing else these functions has to be prefixed
in order to avoid conflicts with functions declared in the testcases.

> diff --git a/include/tst_net.h b/include/tst_net.h
> index cb97b7b61..740f25bac 100644
> --- a/include/tst_net.h
> +++ b/include/tst_net.h
> @@ -16,7 +16,7 @@
>   */
>  
>  #include <arpa/inet.h>
> -#include <errno.h>
> +#include <stdio.h>
>  
>  #define MAX_IPV4_PREFIX 32
>  #define MAX_IPV6_PREFIX 128
> @@ -42,112 +42,10 @@ static inline void print_svar_change(const char *name, const char *val)
>  		printf("export %s=\"${%s:-%s}\"\n", name, name, val);
>  }
>  
> -/*
> - * Function bit_count is from ipcalc project, ipcalc.c.
> - */
> -static int bit_count(uint32_t i)
> -{
> -	int c = 0;
> -	unsigned int seen_one = 0;
> -
> -	while (i > 0) {
> -		if (i & 1) {
> -			seen_one = 1;
> -			c++;
> -		} else {
> -			if (seen_one)
> -				return -1;
> -		}
> -		i >>= 1;
> -	}
> -
> -	return c;
> -}
> -
> -/*
> - * Function mask2prefix is from ipcalc project, ipcalc.c.
> - */
> -static int mask2prefix(struct in_addr mask)
> -{
> -	return bit_count(ntohl(mask.s_addr));
> -}
> -
> -/*
> - * Function ipv4_mask_to_int is from ipcalc project, ipcalc.c.
> - */
> -static int ipv4_mask_to_int(const char *prefix)
> -{
> -	int ret;
> -	struct in_addr in;
> -
> -	ret = inet_pton(AF_INET, prefix, &in);
> -	if (ret == 0)
> -		return -1;
> -
> -	return mask2prefix(in);
> -}
> -
> -/*
> - * Function safe_atoi is from ipcalc project, ipcalc.c.
> - */
> -static int safe_atoi(const char *s, int *ret_i)
> -{
> -	char *x = NULL;
> -	long l;
> -
> -	errno = 0;
> -	l = strtol(s, &x, 0);
> -
> -	if (!x || x == s || *x || errno)
> -		return errno > 0 ? -errno : -EINVAL;
> -
> -	if ((long)(int)l != l)
> -		return -ERANGE;
> -
> -	*ret_i = (int)l;
> -
> -	return 0;
> -}
> -
> -/*
> - * Function get_prefix use code from ipcalc project, str_to_prefix/ipcalc.c.
> - */
> -static int get_prefix(const char *ip_str, int is_ipv6)
> -{
> -	char *prefix_str = NULL;
> -	int prefix = -1, r;
> -
> -	prefix_str = strchr(ip_str, '/');
> -	if (!prefix_str)
> -		return -1;
> -
> -	*(prefix_str++) = '\0';
> -
> -	if (!is_ipv6 && strchr(prefix_str, '.'))
> -		prefix = ipv4_mask_to_int(prefix_str);
> -	else {
> -		r = safe_atoi(prefix_str, &prefix);
> -		if (r != 0)
> -			tst_brk_comment("conversion error: '%s' is not integer",
> -					prefix_str);
> -	}
> -
> -	if (prefix < 0 || ((is_ipv6 && prefix > MAX_IPV6_PREFIX) ||
> -		(!is_ipv6 && prefix > MAX_IPV4_PREFIX)))
> -		tst_brk_comment("bad %s prefix: %s", is_ipv6 ?  "IPv6" : "IPv4",
> -				prefix_str);
> -
> -	return prefix;
> -}
> -
> -static void get_in_addr(const char *ip_str, struct in_addr *ip)
> -{
> -	if (inet_pton(AF_INET, ip_str, ip) <= 0)
> -		tst_brk_comment("bad IPv4 address: '%s'", ip_str);
> -}
> -
> -static 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);
> -}
> +int bit_count(uint32_t i);
> +int mask2prefix(struct in_addr mask);
> +int ipv4_mask_to_int(const char *prefix);
> +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);
> diff --git a/lib/tst_net.c b/lib/tst_net.c
> new file mode 100644
> index 000000000..4166641f1
> --- /dev/null
> +++ b/lib/tst_net.c
> @@ -0,0 +1,134 @@
> +/*
> + * Copyright (c) 2017 Petr Vorel <pvorel@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
> + * the Free Software Foundation, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program. If not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#include <errno.h>
> +#include <string.h>
> +#include <stdlib.h>
> +
> +#define TST_NO_DEFAULT_MAIN
> +#include "tst_test.h"
> +#include "tst_net.h"
> +
> +/*
> + * Function bit_count is from ipcalc project, ipcalc.c.
> + */
> +int bit_count(uint32_t i)
> +{
> +	int c = 0;
> +	unsigned int seen_one = 0;
> +
> +	while (i > 0) {
> +		if (i & 1) {
> +			seen_one = 1;
> +			c++;
> +		} else {
> +			if (seen_one)
> +				return -1;
> +		}
> +		i >>= 1;
> +	}
> +
> +	return c;
> +}
> +
> +/*
> + * Function mask2prefix is from ipcalc project, ipcalc.c.
> + */
> +int mask2prefix(struct in_addr mask)
> +{
> +	return bit_count(ntohl(mask.s_addr));
> +}
> +
> +/*
> + * Function ipv4_mask_to_int is from ipcalc project, ipcalc.c.
> + */
> +int ipv4_mask_to_int(const char *prefix)
> +{
> +	int ret;
> +	struct in_addr in;
> +
> +	ret = inet_pton(AF_INET, prefix, &in);
> +	if (ret == 0)
> +		return -1;
> +
> +	return mask2prefix(in);
> +}
> +
> +/*
> + * Function safe_atoi is from ipcalc project, ipcalc.c.
> + */
> +int safe_atoi(const char *s, int *ret_i)
> +{
> +	char *x = NULL;
> +	long l;
> +
> +	errno = 0;
> +	l = strtol(s, &x, 0);
> +
> +	if (!x || x == s || *x || errno)
> +		return errno > 0 ? -errno : -EINVAL;
> +
> +	if ((long)(int)l != l)
> +		return -ERANGE;
> +
> +	*ret_i = (int)l;
> +
> +	return 0;
> +}
> +
> +/*
> + * Function get_prefix use code from ipcalc project, str_to_prefix/ipcalc.c.
> + */
> +int get_prefix(const char *ip_str, int is_ipv6)
> +{
> +	char *prefix_str = NULL;
> +	int prefix = -1, r;
> +
> +	prefix_str = strchr(ip_str, '/');
> +	if (!prefix_str)
> +		return -1;
> +
> +	*(prefix_str++) = '\0';
> +
> +	if (!is_ipv6 && strchr(prefix_str, '.'))
> +		prefix = ipv4_mask_to_int(prefix_str);
> +	else {
> +		r = safe_atoi(prefix_str, &prefix);
> +		if (r != 0)
> +			tst_brk_comment("conversion error: '%s' is not integer",
> +					prefix_str);
> +	}
> +
> +	if (prefix < 0 || ((is_ipv6 && prefix > MAX_IPV6_PREFIX) ||
> +		(!is_ipv6 && prefix > MAX_IPV4_PREFIX)))
> +		tst_brk_comment("bad %s prefix: %s", is_ipv6 ?  "IPv6" : "IPv4",
> +				prefix_str);
> +
> +	return prefix;
> +}
> +
> +void get_in_addr(const char *ip_str, struct in_addr *ip)
> +{
> +	if (inet_pton(AF_INET, ip_str, ip) <= 0)
> +		tst_brk_comment("bad IPv4 address: '%s'", ip_str);
> +}
> +
> +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);
> +}
> -- 
> 2.23.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

  reply	other threads:[~2019-10-04 12:40 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 [this message]
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
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=20191004124057.GB5442@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.