akaros/tests/gai.c
<<
>>
Prefs
   1/* Copyright (c) 2015 Google, Inc.
   2 * Barret Rhoden <brho@cs.berkeley.edu>
   3 * See LICENSE for details. */
   4
   5#include <stdlib.h>
   6#include <stdio.h>
   7#include <parlib/parlib.h>
   8#include <unistd.h>
   9#include <signal.h>
  10
  11#include <sys/socket.h>
  12#include <netdb.h>
  13#include <sys/types.h>
  14#include <arpa/inet.h>
  15
  16static void print_gai(struct addrinfo *ai, const char *info)
  17{
  18        struct sockaddr_in *ipv4sa;
  19
  20        printf("%s: fam %d, sock %d, prot %d ", info, ai->ai_family,
  21               ai->ai_socktype, ai->ai_protocol);
  22        char buf[128];
  23
  24        ipv4sa = (struct sockaddr_in*)ai->ai_addr;
  25        const char *ipv4n = inet_ntop(AF_INET, &ipv4sa->sin_addr, buf, 128);
  26
  27        assert(buf == ipv4n);
  28        printf("addr %s, port %d\n", buf, ntohs(ipv4sa->sin_port));
  29}
  30
  31static void test_gai(const char *node, const char *serv, struct addrinfo *hints,
  32                     const char *info)
  33{
  34        struct addrinfo *_ai_res;
  35        int ret = getaddrinfo(node, serv, hints, &_ai_res);
  36
  37        if (ret) {
  38                printf("%s: GAI failed, %d, %d %s\n", info, ret, errno,
  39                       errstr());
  40        } else {
  41                print_gai(_ai_res, info);
  42                freeaddrinfo(_ai_res);
  43        }
  44}
  45
  46int main(int argc, char **argv)
  47{
  48        char name[100];
  49        char serv[100];
  50
  51        struct addrinfo hints = {0};
  52
  53        test_gai("10.0.2.1", "80", 0, "IP and 80");
  54
  55        test_gai("10.0.2.1.dicks", "80", 0, "Non-number name");
  56
  57        test_gai("10.0.2.2", "http", 0, "http serv");
  58
  59        hints.ai_family = AF_UNSPEC;
  60        hints.ai_socktype = SOCK_DGRAM;
  61        hints.ai_protocol = 0;
  62        test_gai("10.0.2.3", "12345", &hints, "SOCK_DGRAM");
  63
  64        hints.ai_family = AF_UNSPEC;
  65        hints.ai_socktype = SOCK_RAW;
  66        hints.ai_protocol = 0;
  67        test_gai("10.0.2.3", "12345", &hints, "SOCK_RAW");
  68
  69        hints.ai_family = AF_UNSPEC;
  70        hints.ai_socktype = 0;
  71        hints.ai_protocol = IPPROTO_ICMP;
  72        test_gai("10.0.2.3", "12345", &hints, "ICMP");
  73
  74        hints.ai_family = AF_UNSPEC;
  75        hints.ai_socktype = SOCK_DGRAM;
  76        hints.ai_protocol = IPPROTO_TCP;
  77        test_gai("10.0.2.3", "12345", &hints, "Impossible hint");
  78}
  79