Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <linux/if.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include "setup_interfaces.h"
- #include "sysfs_reader.h"
- struct net_iface* get_all_interfaces()
- {
- struct ifreq *ifr;
- struct ifconf ifc;
- int s;
- int numif;
- memset(&ifc, 0, sizeof(ifc));
- ifc.ifc_ifcu.ifcu_req = NULL;
- ifc.ifc_len = 0;
- if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- perror("socket");
- return NULL;
- }
- if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
- perror("ioctl");
- return NULL;
- }
- if ((ifr = malloc(ifc.ifc_len)) == NULL) {
- perror("malloc");
- return NULL;
- }
- ifc.ifc_ifcu.ifcu_req = ifr;
- if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
- perror("ioctl");
- return NULL;
- }
- close(s);
- numif = ifc.ifc_len / sizeof(struct ifreq);
- if (!numif) {
- free(ifr);
- return NULL;
- }
- struct net_iface* iflist;
- if ((iflist = malloc(sizeof(struct net_iface) * numif)) == NULL) {
- perror("malloc");
- return NULL;
- }
- int sysfs_avail = is_sysfs_available();
- int i;
- for (i = 0; i < numif; i++) {
- struct ifreq *r = &ifr[i];
- struct sockaddr_in *sin = (struct sockaddr_in *)&r->ifr_addr;
- iflist[i].if_idx = r->ifr_ifindex;
- strncpy(iflist[i].if_name, r->ifr_name, IFNAMSIZ);
- if (sysfs_avail) {
- iflist[i].get_stat = get_stat_sysfs;
- }
- inet_ntop(AF_INET, &sin->sin_addr, iflist[i].str_ip4_addr, INET_ADDRSTRLEN);
- if (i) {
- iflist[i-1].next = &iflist[i];
- }
- }
- free(ifr);
- return iflist;
- }
Add Comment
Please, Sign In to add comment