vmm: Use VMM_CTL to set VMM flags (XCC)
[akaros.git] / tests / getifaddrs.c
index c8e2277..e3982ae 100644 (file)
 #include <parlib/timing.h>
 #include <parlib/tsc-compat.h>
 #include <parlib/printf-ext.h>
-#include <benchutil/alarm.h>
+#include <parlib/alarm.h>
 #include <ndblib/ndb.h>
 #include <ifaddrs.h>
+#include <netpacket/packet.h>
+#include <arpa/inet.h>
+
+static void print_eth(struct ifaddrs *ifa)
+{
+       struct sockaddr_ll *sa_ll = (struct sockaddr_ll*)ifa->ifa_addr;
+
+       printf("\tAddr: ");
+       for (int i = 0; i < sa_ll->sll_halen; i++) {
+               printf("%02x", sa_ll->sll_addr[i]);
+               if (i < sa_ll->sll_halen - 1)
+                       printf(":");
+       }
+       printf("\n");
+       printf("\tNIC %d\n", sa_ll->sll_ifindex);
+}
+
+static void print_inet(struct ifaddrs *ifa)
+{
+       struct sockaddr_in *sa_in = (struct sockaddr_in*)ifa->ifa_addr;
+       struct sockaddr_in *mask_in = (struct sockaddr_in*)ifa->ifa_netmask;
+       char buf[INET_ADDRSTRLEN];
+
+       printf("\tAddr: %s\n", inet_ntop(AF_INET, &sa_in->sin_addr, buf,
+                                        sizeof(buf)));
+       if (mask_in)
+               printf("\tMask: %s\n", inet_ntop(AF_INET, &mask_in->sin_addr, buf,
+                                                sizeof(buf)));
+}
+
+static void print_inet6(struct ifaddrs *ifa)
+{
+       struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6*)ifa->ifa_addr;
+       char buf[INET6_ADDRSTRLEN];
+
+       printf("\tAddr: %s\n", inet_ntop(AF_INET6, &sa_in6->sin6_addr, buf,
+                                        sizeof(buf)));
+}
 
 int main(int argc, char **argv)
 {
-       int i, naddr, o;
-       uint8_t *cp;
-       struct ifaddrs *ifa;
+       int family;
+       struct ifaddrs *ifaddrs, *ifa;
 
-       naddr = getifaddrs(&ifa);
+       if (getifaddrs(&ifaddrs) != 0) {
+               perror("getifaddr");
+               exit(-1);
+       }
 
-       for (naddr = 0; ifa; ifa = ifa->ifa_next, naddr++) {
+       for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
                printf("%s: ", ifa->ifa_name);
-               cp = ifa->ifa_data;
-               for (o = 0; o < 6; o++) {
-                       printf("%02x", cp[o]);
-                       if (o < 5)
-                               printf(":");
+               if (!ifa->ifa_addr) {
+                       printf("No addr\n");
+                       continue;
+               } else {
+                       printf("\n");
+               }
+               family = ifa->ifa_addr->sa_family;
+               printf("\tFamily: %s\n", (family == AF_PACKET) ? "AF_PACKET" :
+                                        (family == AF_INET) ? "AF_INET" :
+                                        (family == AF_INET6) ? "AF_INET6" :
+                                        "Unknown");
+               switch (family) {
+               case AF_PACKET:
+                       print_eth(ifa);
+                       break;
+               case AF_INET:
+                       print_inet(ifa);
+                       break;
+               case AF_INET6:
+                       print_inet6(ifa);
+                       break;
                }
-               printf("\n");
        }
-       printf("%d ifaddrs\n", naddr);
-       freeifaddrs(ifa);
+       freeifaddrs(ifaddrs);
 }