public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Richard Palethorpe <rpalethorpe@suse.com>
To: ltp@lists.linux.it
Subject: [LTP] [RFC PATCH v5 2/3] network: Add tool for setup IP variables
Date: Thu, 4 May 2017 16:41:36 +0200	[thread overview]
Message-ID: <20170504164136.46444e27@linux-v3j5> (raw)
In-Reply-To: <20170503160751.1634-3-pvorel@suse.cz>

Hi,

Just a couple of minor comments, I haven't properly read it.

On Wed,  3 May 2017 18:07:50 +0200
"Petr Vorel" <pvorel@suse.cz> wrote:

> +/*
> + * Function prefix2mask is from ipcalc project, ipcalc.c.
> + */
> +struct in_addr prefix2mask(int prefix)
> +{
> +	struct in_addr mask;
> +
> +	memset(&mask, 0, sizeof(mask));

FYI for these memsets you could use 'struct foo bar = { 0 }' instead. Although
it generates warnings on older compilers if the struct starts with another
composite data type.

> +
> +static int read_prefix(const char *ip_str, int is_ipv6)
> +{
> +	uint8_t family = is_ipv6 ? AF_INET6 : AF_INET;
> +
> +	char buf[16384];
> +	int len;
> +
> +	struct {
> +		struct nlmsghdr nlhdr;
> +		struct ifaddrmsg addrmsg;
> +	} msg;
> +
> +	struct nlmsghdr *retmsg;
> +
> +	int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
> +
> +	memset(&msg, 0, sizeof(msg));
> +	msg.nlhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
> +	msg.nlhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
> +	msg.nlhdr.nlmsg_type = RTM_GETADDR;
> +	msg.addrmsg.ifa_family = family;
> +
> +	send(sock, &msg, msg.nlhdr.nlmsg_len, 0);
> +	len = recv(sock, buf, sizeof(buf), 0);
> +	retmsg = (struct nlmsghdr *)buf;

No error checking? I suppose it is unlikely to fail, but it can't hurt.

> +
> +	while NLMSG_OK(retmsg, len) {
> +		struct ifaddrmsg *retaddr;
> +		struct rtattr *retrta;
> +		char pradd[128];
> +		int attlen;
> +
> +		retaddr = (struct ifaddrmsg *)NLMSG_DATA(retmsg);
> +		retrta = (struct rtattr *)IFA_RTA(retaddr);
> +		attlen = IFA_PAYLOAD(retmsg);
> +
> +		while RTA_OK(retrta, attlen) {
> +			if (retrta->rta_type == IFA_ADDRESS) {
> +				inet_ntop(family, RTA_DATA(retrta), pradd, sizeof(pradd));
> +				if (!strcmp(pradd, ip_str))
> +					return retaddr->ifa_prefixlen;
> +			}
> +			retrta = RTA_NEXT(retrta, attlen);
> +
> +		}
> +		retmsg = NLMSG_NEXT(retmsg, len);
> +	}
> +
> +	return -1;
> +}
> +

Thank you,
Richard.

  reply	other threads:[~2017-05-04 14:41 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-03 16:07 [LTP] [RFC PATCH v5 0/3] Simplify network setup Petr Vorel
2017-05-03 16:07 ` [LTP] [RFC PATCH v5 1/3] gitignore: Prefixing filenames Petr Vorel
2017-05-03 16:07 ` [LTP] [RFC PATCH v5 2/3] network: Add tool for setup IP variables Petr Vorel
2017-05-04 14:41   ` Richard Palethorpe [this message]
2017-05-04 19:51     ` Petr Vorel
2017-05-03 16:07 ` [LTP] [RFC PATCH v5 3/3] network: Use tst_net_vars to set up IPv4 and IPv6 related variables Petr Vorel
2017-05-04 11:09 ` [LTP] [RFC PATCH v5 0/3] Simplify network setup Alexey Kodanev
2017-05-04 12:42   ` Alexey Kodanev
2017-05-04 14:03     ` Petr Vorel
2017-05-04 13:46   ` Petr Vorel
2017-05-04 14:52     ` Alexey Kodanev
2017-05-04 19:25       ` Petr Vorel
2017-05-04 11:27 ` Richard Palethorpe
2017-05-04 12:35   ` Alexey Kodanev
2017-05-04 13:38     ` Petr Vorel

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=20170504164136.46444e27@linux-v3j5 \
    --to=rpalethorpe@suse.com \
    --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