dhcp/lib/interfaces.c

55 lines
1.7 KiB
C

/// GNU Guix DHCP Client.
///
/// Copyright © 2015 Rohan Prinja <rohan.prinja@gmail.com>
///
/// 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 <http://www.gnu.org/licenses/>.
#include <sys/types.h>
#include <ifaddrs.h>
#include <stdio.h>
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);
}