akaros/tests/getifaddrs.c
<<
>>
Prefs
   1/* This file is part of the UCB release of Plan 9. It is subject to the license
   2 * terms in the LICENSE file found in the top-level directory of this
   3 * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
   4 * part of the UCB release of Plan 9, including this file, may be copied,
   5 * modified, propagated, or distributed except according to the terms contained
   6 * in the LICENSE file. */
   7
   8#include <stdlib.h>
   9#include <stdio.h>
  10#include <parlib/parlib.h>
  11#include <unistd.h>
  12#include <signal.h>
  13#include <iplib/iplib.h>
  14#include <iplib/icmp.h>
  15#include <ctype.h>
  16#include <pthread.h>
  17#include <parlib/spinlock.h>
  18#include <parlib/timing.h>
  19#include <parlib/tsc-compat.h>
  20#include <parlib/printf-ext.h>
  21#include <parlib/alarm.h>
  22#include <ndblib/ndb.h>
  23#include <ifaddrs.h>
  24#include <netpacket/packet.h>
  25#include <arpa/inet.h>
  26
  27static void print_eth(struct ifaddrs *ifa)
  28{
  29        struct sockaddr_ll *sa_ll = (struct sockaddr_ll*)ifa->ifa_addr;
  30
  31        printf("\tAddr: ");
  32        for (int i = 0; i < sa_ll->sll_halen; i++) {
  33                printf("%02x", sa_ll->sll_addr[i]);
  34                if (i < sa_ll->sll_halen - 1)
  35                        printf(":");
  36        }
  37        printf("\n");
  38        printf("\tNIC %d\n", sa_ll->sll_ifindex);
  39}
  40
  41static void print_inet(struct ifaddrs *ifa)
  42{
  43        struct sockaddr_in *sa_in = (struct sockaddr_in*)ifa->ifa_addr;
  44        struct sockaddr_in *mask_in = (struct sockaddr_in*)ifa->ifa_netmask;
  45        char buf[INET_ADDRSTRLEN];
  46
  47        printf("\tAddr: %s\n", inet_ntop(AF_INET, &sa_in->sin_addr, buf,
  48                                         sizeof(buf)));
  49        if (mask_in)
  50                printf("\tMask: %s\n", inet_ntop(AF_INET, &mask_in->sin_addr,
  51                                                 buf, sizeof(buf)));
  52}
  53
  54static void print_inet6(struct ifaddrs *ifa)
  55{
  56        struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6*)ifa->ifa_addr;
  57        char buf[INET6_ADDRSTRLEN];
  58
  59        printf("\tAddr: %s\n", inet_ntop(AF_INET6, &sa_in6->sin6_addr, buf,
  60                                         sizeof(buf)));
  61}
  62
  63int main(int argc, char **argv)
  64{
  65        int family;
  66        struct ifaddrs *ifaddrs, *ifa;
  67
  68        if (getifaddrs(&ifaddrs) != 0) {
  69                perror("getifaddr");
  70                exit(-1);
  71        }
  72
  73        for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
  74                printf("%s: ", ifa->ifa_name);
  75                if (!ifa->ifa_addr) {
  76                        printf("No addr\n");
  77                        continue;
  78                } else {
  79                        printf("\n");
  80                }
  81                family = ifa->ifa_addr->sa_family;
  82                printf("\tFamily: %s\n", (family == AF_PACKET) ? "AF_PACKET" :
  83                                         (family == AF_INET) ? "AF_INET" :
  84                                         (family == AF_INET6) ? "AF_INET6" :
  85                                         "Unknown");
  86                switch (family) {
  87                case AF_PACKET:
  88                        print_eth(ifa);
  89                        break;
  90                case AF_INET:
  91                        print_inet(ifa);
  92                        break;
  93                case AF_INET6:
  94                        print_inet6(ifa);
  95                        break;
  96                }
  97        }
  98        freeifaddrs(ifaddrs);
  99}
 100