From: Stephen Hemminger <stephen@networkplumber.org>
To: Hoang Le <hoang.h.le@dektech.com.au>
Cc: dsahern@gmail.com, jon.maloy@ericsson.com, maloy@donjonn.com,
ying.xue@windriver.com, netdev@vger.kernel.org,
tipc-discussion@lists.sourceforge.net
Subject: Re: [iproute2-next v3] tipc: support interface name when activating UDP bearer
Date: Thu, 18 Oct 2018 13:02:52 -0700 [thread overview]
Message-ID: <20181018130252.25070508@shemminger-XPS-13-9360> (raw)
In-Reply-To: <20181018050323.5082-1-hoang.h.le@dektech.com.au>
On Thu, 18 Oct 2018 12:03:23 +0700
Hoang Le <hoang.h.le@dektech.com.au> wrote:
>
> +static int cmd_bearer_validate_and_get_addr(const char *name, char *straddr)
> +{
> + struct ifreq ifc = {};
> + struct sockaddr_in *ip4addr;
> + struct sockaddr_in6 *ip6addr;
> + int fd;
> +
> + if (!name || !straddr || get_ifname(ifc.ifr_name, name))
> + return 0;
> +
> + fd = socket(PF_INET, SOCK_DGRAM, 0);
> + if (fd <= 0) {
> + fprintf(stderr, "Failed to create socket\n");
> + return 0;
> + }
> +
> + if (ioctl(fd, SIOCGIFADDR, &ifc) < 0) {
> + fprintf(stderr, "ioctl failed: %s\n", strerror(errno));
> + close(fd);
> + return 0;
> + }
> +
> + ip4addr = (struct sockaddr_in *)&ifc.ifr_addr;
> + if (inet_ntop(AF_INET, &ip4addr->sin_addr, straddr,
> + INET_ADDRSTRLEN) == NULL) {
> + ip6addr = (struct sockaddr_in6 *)&ifc.ifr_addr;
> + if (inet_ntop(AF_INET6, &ip6addr->sin6_addr, straddr,
> + INET6_ADDRSTRLEN) == NULL) {
> + fprintf(stderr, "UDP local address error\n");
> + close(fd);
> + return 0;
> + }
> + }
> + close(fd);
> + return 1;
> +}
The concept of this is good, but not sure if it is implemented in
the best way.
Using legacy SIOCGIFADDR has many limitations. It doesn't handle multiple
addresses per interface for example. Why not use netlink like rest of iproute2?
Also trying twice for IPv4 than IPv6 seems
unnecessary. Perhaps use getnameinfo or look at address famliy.
Remember Linux is weak host model so you might not get what you expect.
prev parent reply other threads:[~2018-10-19 4:05 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-18 5:03 [iproute2-next v3] tipc: support interface name when activating UDP bearer Hoang Le
2018-10-18 20:02 ` Stephen Hemminger [this message]
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=20181018130252.25070508@shemminger-XPS-13-9360 \
--to=stephen@networkplumber.org \
--cc=dsahern@gmail.com \
--cc=hoang.h.le@dektech.com.au \
--cc=jon.maloy@ericsson.com \
--cc=maloy@donjonn.com \
--cc=netdev@vger.kernel.org \
--cc=tipc-discussion@lists.sourceforge.net \
--cc=ying.xue@windriver.com \
/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