Morse Micro IoT SDK  2.10.4
dns_client.c
Go to the documentation of this file.
1/*
2 * Copyright 2023 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
30#include <string.h>
31#include "mmosal.h"
32#include "mmwlan.h"
33#include "mmconfig.h"
34
35#include "mmipal.h"
36#include "netdb.h"
37#include "sys/socket.h"
38
39#include "mm_app_common.h"
40
42#define DEFAULT_LOOKUP "www.morsemicro.com"
43
44#ifndef DNS_MAX_NAME_LENGTH
46#define DNS_MAX_NAME_LENGTH (256)
47#endif
48
55static void dns_lookup(const char *hostname, int ai_family)
56{
57 /*
58 * Hints is optional and can be specified as NULL in getaddrinfo()
59 * if you just want to lookup an IP. But if you intend to use the returned data
60 * to actually connect (as you would in a real use case), then you need to specify
61 * hints so that the correct protocol and ports are returned in addr_list below.
62 */
63 struct addrinfo hints = { 0 };
64 hints.ai_family = ai_family;
65 hints.ai_socktype = SOCK_STREAM;
66 hints.ai_protocol = IPPROTO_TCP;
67
68 struct addrinfo *addr_list, *cur;
69 int ret = getaddrinfo(hostname, NULL, &hints, &addr_list);
70 if (ret == 0)
71 {
72 /*
73 * Enumerate all addresses returned.
74 * Technically this API can return multiple addresses including combinations
75 * of IPv4 and IPv6 addresses (depending on the hints given).
76 */
77 for (cur = addr_list; cur != NULL; cur = cur->ai_next)
78 {
79 /*
80 * In a typical use case, you can directly use the first element in addr_list
81 * to open a socket and connect as shown below:
82 *
83 * int fd = socket(addr_list->ai_family, addr_list->ai_socktype,
84 * addr_list->ai_protocol); connect(fd, addr_list->ai_addr, addr_list->ai_addrlen);
85 *
86 * However in this example we want to print the list of IP addeesses found
87 * so we enumerate the addr_list, and do some conversions to convert and print the
88 * IP addresses.
89 */
90
91 char addr_str[MMIPAL_IPADDR_STR_MAXLEN];
92 const char *result = NULL;
93
94 if (cur->ai_family == AF_INET)
95 {
96#if MMIPAL_IPV4_ENABLED
97 const struct sockaddr_in *sockaddr = (const struct sockaddr_in *)cur->ai_addr;
98 result = inet_ntop(cur->ai_family, &sockaddr->sin_addr, addr_str, sizeof(addr_str));
99#endif
100 }
101#if MMIPAL_IPV6_ENABLED
102 else if (cur->ai_family == AF_INET6)
103 {
104 const struct sockaddr_in6 *sockaddr = (const struct sockaddr_in6 *)cur->ai_addr;
105 result =
106 inet_ntop(cur->ai_family, &sockaddr->sin6_addr, addr_str, sizeof(addr_str));
107 }
108#endif
109
110 if (result != NULL)
111 {
112 printf(" %s\n", result);
113 }
114 else
115 {
116 printf("Error: Failed to convert IP address to string\n");
117 }
118 }
119 freeaddrinfo(addr_list);
120 }
121 else
122 {
123 const char *family_str;
124 switch (ai_family)
125 {
126 case AF_INET:
127 family_str = "IPv4";
128 break;
129
130 case AF_INET6:
131 family_str = "IPv6";
132 break;
133
134 default:
135 family_str = "??";
136 break;
137 }
138
139 printf("Could not resolve %s address for hostname %s! (Error code %d)\n",
140 family_str,
141 hostname,
142 ret);
143 }
144}
145
150void app_init(void)
151{
152 static char hostname[DNS_MAX_NAME_LENGTH + 1];
153
154 printf("\n\nMorse DNS client Demo (Built " __DATE__ " " __TIME__ ")\n\n");
155
156 /* Initialize and connect to Wi-Fi, blocks till connected */
159
160 /* Get the hostname to lookup, if not provided use the default */
161 strncpy(hostname, DEFAULT_LOOKUP, sizeof(hostname));
162 (void)mmconfig_read_string("dns.lookup", hostname, sizeof(hostname));
163
164 printf("Hostname %s resolves to:\n", hostname);
165 dns_lookup(hostname, AF_INET);
166 dns_lookup(hostname, AF_INET6);
167
168 /* Disconnect from Wi-Fi */
170}
#define DNS_MAX_NAME_LENGTH
Maximum supported length of hostname for DNS lookup.
Definition: dns_client.c:46
static void dns_lookup(const char *hostname, int ai_family)
Perform a DNS lookup for the given hostname and print the results.
Definition: dns_client.c:55
#define DEFAULT_LOOKUP
This is the default hostname to lookup if none are specified in config store.
Definition: dns_client.c:42
void app_init(void)
Main entry point to the application.
Definition: dns_client.c:150
int mmconfig_read_string(const char *key, char *buffer, int bufsize)
Returns the persistent store string value identified by the key.
#define MMIPAL_IPADDR_STR_MAXLEN
Maximum length of an IP address string, including null-terminator.
Definition: mmipal.h:28
Morse Micro application helper routines for initializing/de-initializing the Wireless LAN interface a...
void app_wlan_stop(void)
Disconnects from Wi-Fi and de-initializes the WLAN interface.
void app_wlan_init(void)
Initializes the WLAN interface (and dependencies) using settings specified in the config store.
void app_wlan_start(void)
Starts the WLAN interface and connects to Wi-Fi using settings specified in the config store.