1 /*
2  * Copyright (C) 2017-2019 Alibaba Group Holding Limited
3  */
4 #if AOS_COMP_CLI
5 #include <string.h>
6 #include <aos/cli.h>
7 #include <lwip/netdb.h>
8 
9 static void dns_command(char *buffer, int32_t buf_len, int32_t argc, char **argv);
10 
11 struct cli_command dns_cmd[] = {
12     { "dns", "dns app", dns_command},
13 };
14 
dns_help_command(void)15 static void dns_help_command(void)
16 {
17     LWIP_DEBUGF( DNSCLI_DEBUG, ("Usage: dns destination\n"));
18     LWIP_DEBUGF( DNSCLI_DEBUG, ("Eample:\n"));
19     LWIP_DEBUGF( DNSCLI_DEBUG, ("dns www.aliyun.com\n"));
20 }
21 
dns_req_command(char * name)22 static void dns_req_command(char *name)
23 {
24     struct hostent *hostent;
25     uint8_t index = 0;
26 
27     hostent = lwip_gethostbyname(name);
28 
29     if (hostent) {
30         LWIP_DEBUGF( DNSCLI_DEBUG, ("hostent.h_name %s\n", hostent->h_name));
31         if (hostent->h_addr_list != NULL) {
32             for (index = 0; hostent->h_addr_list[index]; index++) {
33                 LWIP_DEBUGF( DNSCLI_DEBUG, ("hostent.h_addr_list[%i]->== %s\n",
34                         index, ipaddr_ntoa((ip_addr_t *)hostent->h_addr_list[index])));
35             }
36         }
37     } else {
38         LWIP_DEBUGF( DNSCLI_DEBUG, ("lwip_gethostbyname failed, try again\n"));
39     }
40 }
41 
dns_command(char * buffer,int32_t buf_len,int32_t argc,char ** argv)42 static void dns_command(char *buffer, int32_t buf_len, int32_t argc, char **argv)
43 {
44     if (argc < 2) {
45         LWIP_DEBUGF( DNSCLI_DEBUG, ("%s, invalid command\n", __func__));
46         dns_help_command();
47         return;
48     }
49 
50     if (strcmp(argv[1], "-h") == 0) {
51         dns_help_command();
52     } else {
53         dns_req_command(argv[1]);
54     }
55 }
56 
dns_cli_register(void)57 int32_t dns_cli_register(void)
58 {
59     if (0 == aos_cli_register_commands(dns_cmd, 1)) {
60         return 0;
61     }
62 
63     return -1;
64 }
65 #endif /* AOS_COMP_CLI */
66