Morse Micro IoT SDK  2.9.7
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
49
56static void dns_lookup(const char *hostname, int ai_family)
57{
58 /*
59 * Hints is optional and can be specified as NULL in getaddrinfo()
60 * if you just want to lookup an IP. But if you intend to use the returned data
61 * to actually connect (as you would in a real use case), then you need to specify
62 * hints so that the correct protocol and ports are returned in addr_list below.
63 */
64 struct addrinfo hints = { 0 };
65 hints.ai_family = ai_family;
66 hints.ai_socktype = SOCK_STREAM;
67 hints.ai_protocol = IPPROTO_TCP;
68
69 struct addrinfo *addr_list, *cur;
70 int ret = getaddrinfo(hostname, NULL, &hints, &addr_list);
71 if (ret == 0)
72 {
73 /*
74 * Enumerate all addresses returned.
75 * Technically this API can return multiple addresses including combinations
76 * of IPv4 and IPv6 addresses (depending on the hints given).
77 */
78 for (cur = addr_list; cur != NULL; cur = cur->ai_next)
79 {
80 /*
81 * In a typical use case, you can directly use the first element in addr_list
82 * to open a socket and connect as shown below:
83 *
84 * int fd = socket(addr_list->ai_family, addr_list->ai_socktype, addr_list->ai_protocol);
85 * connect(fd, addr_list->ai_addr, addr_list->ai_addrlen);
86 *
87 * However in this example we want to print the list of IP addeesses found
88 * so we enumerate the addr_list, and do some conversions to convert and print the
89 * IP addresses.
90 */
91
92 char addr_str[MMIPAL_IPADDR_STR_MAXLEN];
93 const char *result = NULL;
94
95 if (cur->ai_family == AF_INET)
96 {
97#if MMIPAL_IPV4_ENABLED
98 const struct sockaddr_in *sockaddr = (const struct sockaddr_in *)cur->ai_addr;
99 result = inet_ntop(cur->ai_family, &sockaddr->sin_addr,
100 addr_str, sizeof(addr_str));
101#endif
102 }
103#if MMIPAL_IPV6_ENABLED
104 else if (cur->ai_family == AF_INET6)
105 {
106 const struct sockaddr_in6 *sockaddr = (const struct sockaddr_in6 *)cur->ai_addr;
107 result = inet_ntop(cur->ai_family, &sockaddr->sin6_addr,
108 addr_str, sizeof(addr_str));
109 }
110#endif
111
112 if (result != NULL)
113 {
114 printf(" %s\n", result);
115 }
116 else
117 {
118 printf("Error: Failed to convert IP address to string\n");
119 }
120 }
121 freeaddrinfo(addr_list);
122 }
123 else
124 {
125 const char *family_str;
126 switch (ai_family)
127 {
128 case AF_INET:
129 family_str = "IPv4";
130 break;
131
132 case AF_INET6:
133 family_str = "IPv6";
134 break;
135
136 default:
137 family_str = "??";
138 break;
139 }
140
141 printf("Could not resolve %s address for hostname %s! (Error code %d)\n",
142 family_str, hostname, ret);
143 }
144}
145
146
151void app_init(void)
152{
153 static char hostname[DNS_MAX_NAME_LENGTH + 1];
154
155 printf("\n\nMorse DNS client Demo (Built " __DATE__ " " __TIME__ ")\n\n");
156
157 /* Initialize and connect to Wi-Fi, blocks till connected */
160
161 /* Get the hostname to lookup, if not provided use the default */
162 strncpy(hostname, DEFAULT_LOOKUP, sizeof(hostname));
163 (void)mmconfig_read_string("dns.lookup", hostname, sizeof(hostname));
164
165 printf("Hostname %s resolves to:\n", hostname);
166 dns_lookup(hostname, AF_INET);
167 dns_lookup(hostname, AF_INET6);
168
169 /* Disconnect from Wi-Fi */
171}
#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:56
#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:151
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:27
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.