Blame view

RIOT/tests/gnrc_sock_dns/main.c 1.46 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  /*
   * Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     tests
   * @{
   *
   * @file
   * @brief       sock DNS client test application
   *
   * @author      Kaspar Schleiser <kaspar@schleiser.de>
   *
   * @}
   */
  
  #include <stddef.h>
  #include <stdio.h>
  
  #include <arpa/inet.h>
  
  #include "net/sock/dns.h"
  #include "net/sock/util.h"
  #include "xtimer.h"
  
  #ifndef TEST_NAME
  #define TEST_NAME   "example.org"
  #endif
  
  #ifndef DNS_SERVER
  #define DNS_SERVER  "[2001:db8::1]:53"
  #endif
  
  /* global DNS server UDP endpoint */
  sock_udp_ep_t sock_dns_server;
  
  /* import "ifconfig" shell command, used for printing addresses */
  
  extern int _netif_config(int argc, char **argv);
  
  int main(void)
  {
      uint8_t addr[16] = {0};
  
      sock_udp_str2ep(&sock_dns_server, DNS_SERVER);
  
      puts("waiting for router advertisement...");
      xtimer_usleep(1U*1000000);
  
      /* print network addresses */
      puts("Configured network interfaces:");
      _netif_config(0, NULL);
  
      int res = sock_dns_query(TEST_NAME, addr, AF_UNSPEC);
      if (res > 0) {
          char addrstr[INET6_ADDRSTRLEN];
          inet_ntop(res == 4 ? AF_INET : AF_INET6, addr, addrstr, sizeof(addrstr));
          printf("%s resolves to %s\n", TEST_NAME, addrstr);
      }
      else {
          printf("error resolving %s\n", TEST_NAME);
      }
  
      return 0;
  }