From: Alan T. DeKok Date: Mon, 25 May 2020 21:03:19 +0000 (-0400) Subject: add fr_interface_to_ipaddr() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=60f0e9b2c6cbcbcbc102da3b3f99bcbc2fdc38b2;p=thirdparty%2Ffreeradius-server.git add fr_interface_to_ipaddr() which grabs an IP from an interface --- diff --git a/src/lib/util/socket.c b/src/lib/util/socket.c index 4080864c622..5b5bf9913c4 100644 --- a/src/lib/util/socket.c +++ b/src/lib/util/socket.c @@ -1244,3 +1244,37 @@ char *fr_ipaddr_to_interface(TALLOC_CTX *ctx, fr_ipaddr_t *ipaddr) freeifaddrs(list); return interface; } + +int fr_interface_to_ipaddr(char const *interface, fr_ipaddr_t *ipaddr, int af, bool link_local) +{ + struct ifaddrs *list = NULL; + struct ifaddrs *i; + int rcode = -1; + + if (getifaddrs(&list) < 0) return -1; + + for (i = list; i != NULL; i = i->ifa_next) { + fr_ipaddr_t my_ipaddr; + + if (!i->ifa_addr || !i->ifa_name || (af != i->ifa_addr->sa_family)) continue; + if (strcmp(i->ifa_name, interface) != 0) continue; + + fr_ipaddr_from_sockaddr((struct sockaddr_storage *)i->ifa_addr, sizeof(struct sockaddr_in6), &my_ipaddr, NULL); + + /* + * If we ask for a link local address, then give + * it to them. + */ + if (link_local) { + if (af != AF_INET6) continue; + if (!IN6_IS_ADDR_LINKLOCAL(&my_ipaddr.addr.v6)) continue; + } + + *ipaddr = my_ipaddr; + rcode = 0; + break; + } + + freeifaddrs(list); + return rcode; +} diff --git a/src/lib/util/socket.h b/src/lib/util/socket.h index 701b6188137..4e7c989ed2c 100644 --- a/src/lib/util/socket.h +++ b/src/lib/util/socket.h @@ -54,6 +54,7 @@ int fr_socket_server_tcp(fr_ipaddr_t const *ipaddr, uint16_t *port, char const int fr_socket_bind(int sockfd, fr_ipaddr_t const *ipaddr, uint16_t *port, char const *interface); int fr_cap_net_raw(void); char *fr_ipaddr_to_interface(TALLOC_CTX *ctx, fr_ipaddr_t *ipaddr); +int fr_interface_to_ipaddr(char const *interface, fr_ipaddr_t *ipaddr, int af, bool link_local); #ifdef __cplusplus }