From 2703cae2a6ff8630eda5524bb672b08ed2dd5fbd Mon Sep 17 00:00:00 2001 From: Vlad Badoiu Date: Fri, 4 Feb 2022 17:43:45 +0200 Subject: [PATCH] Add getifaddrs and freeitaddrs --- Makefile.uk | 2 ++ getifaddrs.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 getifaddrs.c diff --git a/Makefile.uk b/Makefile.uk index 11283e0..658442a 100644 --- a/Makefile.uk +++ b/Makefile.uk @@ -94,6 +94,8 @@ LIBLWIP_SRCS-y += $(LIBLWIP_BASE)/serv.c LIBLWIP_SRCS-y += $(LIBLWIP_BASE)/inet.c|unikraft LIBLWIP_SRCS-$(CONFIG_LWIP_DNS) += $(LIBLWIP_BASE)/getnameinfo.c|unikraft endif + +LIBLWIP_SRCS-y += $(LIBLWIP_BASE)/getifaddrs.c|unikraft LIBLWIP_SRCS-$(CONFIG_LWIP_THREADS) += $(LIBLWIP_BASE)/mutex.c|unikraft LIBLWIP_SRCS-$(CONFIG_LWIP_THREADS) += $(LIBLWIP_BASE)/semaphore.c|unikraft LIBLWIP_SRCS-$(CONFIG_LWIP_THREADS) += $(LIBLWIP_BASE)/mailbox.c|unikraft diff --git a/getifaddrs.c b/getifaddrs.c new file mode 100644 index 0000000..d975638 --- /dev/null +++ b/getifaddrs.c @@ -0,0 +1,77 @@ +#include +#include + +static void +sockaddr_from_ip_addr( + struct sockaddr *sockaddr, + const ip_addr_t *addr) +{ + + memset(sockaddr, 0, sizeof(struct sockaddr_in)); + ((struct sockaddr_in *)sockaddr)->sin_len = sizeof(struct sockaddr_in); + ((struct sockaddr_in *)sockaddr)->sin_family = AF_INET; + inet_addr_from_ip4addr(&((struct sockaddr_in *)sockaddr)->sin_addr, ip_2_ip4(addr)); +} + + +int getifaddrs(struct ifaddrs **ifap) +{ + struct netif *netif; + int is_up; + uint8_t flags; + + struct ifaddrs *prev = NULL, *ifs; + struct ifaddrs *head; + + int ok = 0; + for (netif = netif_list; netif != NULL; netif = netif->next) { + ifs = malloc(sizeof(struct ifaddrs)); + + ifs->ifa_next = NULL; + if (ok == 0) { + ok = 1; + head = ifs; + } + + is_up = netif_is_up(netif); + flags = netif->flags; + printf("%s%d\n", netif->name, netif->num); + + ifs->ifa_flags = flags; + + char *name = malloc(3); + sprintf(name, "%c%c%d", netif->name[0], netif->name[1], netif->num); + ifs->ifa_name = name; + + struct sockaddr *t = malloc(sizeof(struct sockaddr)); + sockaddr_from_ip_addr(t, &netif->ip_addr); + ifs->ifa_addr = t; + + + struct sockaddr *netmask = malloc(sizeof(struct sockaddr)); + sockaddr_from_ip_addr(t, &netif->netmask); + ifs->ifa_netmask = netmask; + + + //t->sa_family = AF_INET; + + //printf("%s\n", ipaddr_ntoa(&netif->ip_addr)); + + // struct sockaddr *ifa_addr; // &netif->ip_addr + // struct sockaddr *ifa_netmask // &netif->netmask + // ifa_ifu.ifu_dstaddr // netif->gw + ifs->ifa_data = NULL; + + if (prev) + prev->ifa_next = ifs; + + prev = ifs; + } + *ifap = head; + return 0; +} + +void freeifaddrs(struct ifaddrs *ifa) +{ + return 0; +}