Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Vladb/getifaddrs #16

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile.uk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 77 additions & 0 deletions getifaddrs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <lwip/tcp.h>
#include <ifaddrs.h>

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;
}