From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stephen Hemminger Subject: Re: [iproute2-next v3] tipc: support interface name when activating UDP bearer Date: Thu, 18 Oct 2018 13:02:52 -0700 Message-ID: <20181018130252.25070508@shemminger-XPS-13-9360> References: <20181018050323.5082-1-hoang.h.le@dektech.com.au> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: dsahern@gmail.com, jon.maloy@ericsson.com, maloy@donjonn.com, ying.xue@windriver.com, netdev@vger.kernel.org, tipc-discussion@lists.sourceforge.net To: Hoang Le Return-path: Received: from mail-pl1-f195.google.com ([209.85.214.195]:33735 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725139AbeJSEFe (ORCPT ); Fri, 19 Oct 2018 00:05:34 -0400 Received: by mail-pl1-f195.google.com with SMTP id s4-v6so14807852plp.0 for ; Thu, 18 Oct 2018 13:02:58 -0700 (PDT) In-Reply-To: <20181018050323.5082-1-hoang.h.le@dektech.com.au> Sender: netdev-owner@vger.kernel.org List-ID: On Thu, 18 Oct 2018 12:03:23 +0700 Hoang Le 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.