diff --git a/lib/interfaces.c b/lib/interfaces.c new file mode 100644 index 0000000..0d09a9b --- /dev/null +++ b/lib/interfaces.c @@ -0,0 +1,54 @@ +/// GNU Guix DHCP Client. +/// +/// Copyright 2015 Free Software Foundation, Inc. +/// +/// This program is free software; you can redistribute it and/or modify it +/// under the terms of the GNU General Public License as published by +/// the Free Software Foundation; either version 3 of the License, or (at +/// your option) any later version. +/// +/// This program is distributed in the hope that it will be useful, but +/// WITHOUT ANY WARRANTY; without even the implied warranty of +/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +/// GNU General Public License for more details. +/// +/// You should have received a copy of the GNU General Public License +/// along with this program. If not, see . + +#include +#include +#include + +void print_interfaces() { + struct ifaddrs *ifaddr, *itr; + if (getifaddrs(&ifaddr) == -1) { + return; + } + for (itr = ifaddr; itr; itr = itr->ifa_next) { + if (itr->ifa_addr == NULL) { + continue; + } + int family = itr->ifa_addr->sa_family; + if (family == AF_INET || family == AF_INET6) { + printf("Name: %s, ", itr->ifa_name); + printf("Address: %s, ", family == AF_INET ? "AF_INET" : family == AF_INET6 ? "AF_INET6" : ""); + printf("Flags: %d\n", itr->ifa_flags); + } + } +} + +char* get_sockaddr_data(struct ifaddrs* ifaddr) { + return ifaddr->ifa_addr->sa_data; +} + +struct ifaddrs* get_first_interface_ptr() { + struct ifaddrs *ifaddr, *itr; + if (getifaddrs(&ifaddr) == -1) { + return NULL; + } + return ifaddr; +} + +void free_interfaces(struct ifaddrs *ifaddr) { + freeifaddrs(ifaddr); +}