[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20181018130252.25070508@shemminger-XPS-13-9360>
Date: Thu, 18 Oct 2018 13:02:52 -0700
From: Stephen Hemminger <stephen@...workplumber.org>
To: Hoang Le <hoang.h.le@...tech.com.au>
Cc: dsahern@...il.com, jon.maloy@...csson.com, maloy@...jonn.com,
ying.xue@...driver.com, netdev@...r.kernel.org,
tipc-discussion@...ts.sourceforge.net
Subject: Re: [iproute2-next v3] tipc: support interface name when activating
UDP bearer
On Thu, 18 Oct 2018 12:03:23 +0700
Hoang Le <hoang.h.le@...tech.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.
Powered by blists - more mailing lists