File: dnssd.c

package info (click to toggle)
lwip 2.2.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,008 kB
  • sloc: ansic: 109,524; cs: 6,714; sh: 115; makefile: 112; perl: 81
file content (164 lines) | stat: -rw-r--r-- 5,414 bytes parent folder | download | duplicates (3)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
 * @file
 * DNS-SD APIs used by LWIP_HOOK_NETCONN_EXTERNAL_RESOLVE
 *
 * This implementation assumes the DNS-SD API implementation (most likely provided by
 * mDNSResponder) is implemented in the same process space as LwIP and can directly
 * invoke the callback for DNSServiceGetAddrInfo.  This is the typical deployment in
 * an embedded environment where as a traditional OS requires pumping the callback results
 * through an IPC mechanism (see DNSServiceRefSockFD/DNSServiceProcessResult)
 *
 * @defgroup dnssd DNS-SD
 * @ingroup dns
 */

/*
 * Copyright (c) 2017 Joel Cunningham, Garmin International, Inc. <joel.cunningham@garmin.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 *
 * Author: Joel Cunningham <joel.cunningham@me.com>
 *
 */
#include "lwip/opt.h"

#include "lwip/err.h"
#include "lwip/inet.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"

#include "dnssd.h"

/* External headers */
#include <string.h>
#include <dns_sd.h>

/* This timeout should allow for multiple queries.
mDNSResponder has the following query timeline:
  Query 1: time = 0s
  Query 2: time = 1s
  Query 3: time = 4s
*/
#define GETADDR_TIMEOUT_MS  5000
#define LOCAL_DOMAIN        ".local"

/* Only consume .local hosts */
#ifndef CONSUME_LOCAL_ONLY
#define CONSUME_LOCAL_ONLY  1
#endif

struct addr_clbk_msg {
  sys_sem_t sem;
  struct sockaddr_storage addr;
  err_t err;
};

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);

int
lwip_dnssd_gethostbyname(const char *name, ip_addr_t *addr, u8_t addrtype, err_t *err)
{
  DNSServiceErrorType result;
  DNSServiceRef ref;
  struct addr_clbk_msg msg;
  char *p;

  /* @todo: use with IPv6 */
  LWIP_UNUSED_ARG(addrtype);

#if CONSUME_LOCAL_ONLY
  /* check if this is a .local host. If it is, then we consume the query */
  p = strstr(name, LOCAL_DOMAIN);
  if (p == NULL) {
    return 0; /* not consumed */
  }
  p += (sizeof(LOCAL_DOMAIN) - 1);
  /* check to make sure .local isn't a substring (only allow .local\0 or .local.\0) */
  if ((*p != '.' && *p != '\0') ||
      (*p == '.' && *(p + 1) != '\0')) {
    return 0; /* not consumed */
  }
#endif /* CONSUME_LOCAL_ONLY */

  msg.err = sys_sem_new(&msg.sem, 0);
  if (msg.err != ERR_OK) {
    goto query_done;
  }

  msg.err = ERR_TIMEOUT;
  result = DNSServiceGetAddrInfo(&ref, 0, 0, kDNSServiceProtocol_IPv4, name, addr_info_callback, &msg);
  if (result == kDNSServiceErr_NoError) {
    sys_arch_sem_wait(&msg.sem, GETADDR_TIMEOUT_MS);
    DNSServiceRefDeallocate(ref);

    /* We got a response */
    if (msg.err == ERR_OK) {
      struct sockaddr_in* addr_in = (struct sockaddr_in *)&msg.addr;
      if (addr_in->sin_family == AF_INET) {
        inet_addr_to_ip4addr(ip_2_ip4(addr), &addr_in->sin_addr);
      } else {
        /* @todo add IPv6 support */
        msg.err = ERR_VAL;
      }
    }
  }
  sys_sem_free(&msg.sem);

/* Query has been consumed and is finished */
query_done:
*err = msg.err;
return 1;
}

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)
{
  struct addr_clbk_msg* msg = (struct addr_clbk_msg*)context;
  struct sockaddr_in*  addr_in = (struct sockaddr_in *)address;

  LWIP_UNUSED_ARG(ref);
  LWIP_UNUSED_ARG(flags);
  LWIP_UNUSED_ARG(interface_index);
  LWIP_UNUSED_ARG(hostname);
  LWIP_UNUSED_ARG(ttl);
  LWIP_UNUSED_ARG(context);

  if ((error_code == kDNSServiceErr_NoError) &&
      (addr_in->sin_family == AF_INET)) {
    MEMCPY(&msg->addr, addr_in, sizeof(*addr_in));
    msg->err = ERR_OK;
  }
  else {
   /* @todo add IPv6 support */
   msg->err = ERR_VAL;
  }

  sys_sem_signal(&msg->sem);
} /* addr_info_callback() */