• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * DNS-SD APIs used by LWIP_HOOK_NETCONN_EXTERNAL_RESOLVE
4  *
5  * This implementation assumes the DNS-SD API implementation (most likely provided by
6  * mDNSResponder) is implemented in the same process space as LwIP and can directly
7  * invoke the callback for DNSServiceGetAddrInfo.  This is the typical deployment in
8  * an embedded environment where as a traditional OS requires pumping the callback results
9  * through an IPC mechanism (see DNSServiceRefSockFD/DNSServiceProcessResult)
10  *
11  * @defgroup dnssd DNS-SD
12  * @ingroup dns
13  */
14 
15 /*
16  * Copyright (c) 2017 Joel Cunningham, Garmin International, Inc. <joel.cunningham@garmin.com>
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without modification,
20  * are permitted provided that the following conditions are met:
21  *
22  * 1. Redistributions of source code must retain the above copyright notice,
23  *    this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright notice,
25  *    this list of conditions and the following disclaimer in the documentation
26  *    and/or other materials provided with the distribution.
27  * 3. The name of the author may not be used to endorse or promote products
28  *    derived from this software without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
31  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
33  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
35  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
38  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
39  * OF SUCH DAMAGE.
40  *
41  * This file is part of the lwIP TCP/IP stack.
42  *
43  * Author: Joel Cunningham <joel.cunningham@me.com>
44  *
45  */
46 #include "lwip/opt.h"
47 
48 #include "lwip/err.h"
49 #include "lwip/inet.h"
50 #include "lwip/sockets.h"
51 #include "lwip/sys.h"
52 
53 #include "dnssd.h"
54 
55 /* External headers */
56 #include <string.h>
57 #include <dns_sd.h>
58 
59 /* This timeout should allow for multiple queries.
60 mDNSResponder has the following query timeline:
61   Query 1: time = 0s
62   Query 2: time = 1s
63   Query 3: time = 4s
64 */
65 #define GETADDR_TIMEOUT_MS  5000
66 #define LOCAL_DOMAIN        ".local"
67 
68 /* Only consume .local hosts */
69 #ifndef CONSUME_LOCAL_ONLY
70 #define CONSUME_LOCAL_ONLY  1
71 #endif
72 
73 struct addr_clbk_msg {
74   sys_sem_t sem;
75   struct sockaddr_storage addr;
76   err_t err;
77 };
78 
79 static void addr_info_callback(DNSServiceRef ref, DNSServiceFlags flags, u32_t interface_index,
80                                DNSServiceErrorType error_code, char const* hostname,
81                                const struct sockaddr* address, u32_t ttl, void* context);
82 
83 int
lwip_dnssd_gethostbyname(const char * name,ip_addr_t * addr,u8_t addrtype,err_t * err)84 lwip_dnssd_gethostbyname(const char *name, ip_addr_t *addr, u8_t addrtype, err_t *err)
85 {
86   DNSServiceErrorType result;
87   DNSServiceRef ref;
88   struct addr_clbk_msg msg;
89   char *p;
90 
91   /* @todo: use with IPv6 */
92   LWIP_UNUSED_ARG(addrtype);
93 
94 #if CONSUME_LOCAL_ONLY
95   /* check if this is a .local host. If it is, then we consume the query */
96   p = strstr(name, LOCAL_DOMAIN);
97   if (p == NULL) {
98     return 0; /* not consumed */
99   }
100   p += (sizeof(LOCAL_DOMAIN) - 1);
101   /* check to make sure .local isn't a substring (only allow .local\0 or .local.\0) */
102   if ((*p != '.' && *p != '\0') ||
103       (*p == '.' && *(p + 1) != '\0')) {
104     return 0; /* not consumed */
105   }
106 #endif /* CONSUME_LOCAL_ONLY */
107 
108   msg.err = sys_sem_new(&msg.sem, 0);
109   if (msg.err != ERR_OK) {
110     goto query_done;
111   }
112 
113   msg.err = ERR_TIMEOUT;
114   result = DNSServiceGetAddrInfo(&ref, 0, 0, kDNSServiceProtocol_IPv4, name, addr_info_callback, &msg);
115   if (result == kDNSServiceErr_NoError) {
116     sys_arch_sem_wait(&msg.sem, GETADDR_TIMEOUT_MS);
117     DNSServiceRefDeallocate(ref);
118 
119     /* We got a response */
120     if (msg.err == ERR_OK) {
121       struct sockaddr_in* addr_in = (struct sockaddr_in *)&msg.addr;
122       if (addr_in->sin_family == AF_INET) {
123         inet_addr_to_ip4addr(ip_2_ip4(addr), &addr_in->sin_addr);
124       } else {
125         /* @todo add IPv6 support */
126         msg.err = ERR_VAL;
127       }
128     }
129   }
130   sys_sem_free(&msg.sem);
131 
132 /* Query has been consumed and is finished */
133 query_done:
134 *err = msg.err;
135 return 1;
136 }
137 
138 static void
addr_info_callback(DNSServiceRef ref,DNSServiceFlags flags,u32_t interface_index,DNSServiceErrorType error_code,char const * hostname,const struct sockaddr * address,u32_t ttl,void * context)139 addr_info_callback(DNSServiceRef ref, DNSServiceFlags flags, u32_t interface_index,
140                    DNSServiceErrorType error_code, char const* hostname,
141                    const struct sockaddr* address, u32_t ttl, void* context)
142 {
143   struct addr_clbk_msg* msg = (struct addr_clbk_msg*)context;
144   struct sockaddr_in*  addr_in = (struct sockaddr_in *)address;
145 
146   LWIP_UNUSED_ARG(ref);
147   LWIP_UNUSED_ARG(flags);
148   LWIP_UNUSED_ARG(interface_index);
149   LWIP_UNUSED_ARG(hostname);
150   LWIP_UNUSED_ARG(ttl);
151   LWIP_UNUSED_ARG(context);
152 
153   if ((error_code == kDNSServiceErr_NoError) &&
154       (addr_in->sin_family == AF_INET)) {
155     MEMCPY(&msg->addr, addr_in, sizeof(*addr_in));
156     msg->err = ERR_OK;
157   }
158   else {
159    /* @todo add IPv6 support */
160    msg->err = ERR_VAL;
161   }
162 
163   sys_sem_signal(&msg->sem);
164 } /* addr_info_callback() */
165