dns_sd.h
Go to the documentation of this file.
1 /**
2  * @file dns_sd.h
3  * @brief DNS-SD (DNS-Based Service Discovery)
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _DNS_SD_H
30 #define _DNS_SD_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "dns/dns_common.h"
35 #include "mdns/mdns_common.h"
36 
37 //DNS-SD support
38 #ifndef DNS_SD_SUPPORT
39  #define DNS_SD_SUPPORT DISABLED
40 #elif (DNS_SD_SUPPORT != ENABLED && DNS_SD_SUPPORT != DISABLED)
41  #error DNS_SD_SUPPORT parameter is not valid
42 #endif
43 
44 //DNS-SD tick interval
45 #ifndef DNS_SD_TICK_INTERVAL
46  #define DNS_SD_TICK_INTERVAL 250
47 #elif (DNS_SD_TICK_INTERVAL < 10)
48  #error DNS_SD_TICK_INTERVAL parameter is not valid
49 #endif
50 
51 //Maximum number of registered services
52 #ifndef DNS_SD_SERVICE_LIST_SIZE
53  #define DNS_SD_SERVICE_LIST_SIZE 2
54 #elif (DNS_SD_SERVICE_LIST_SIZE < 1)
55  #error DNS_SD_SERVICE_LIST_SIZE parameter is not valid
56 #endif
57 
58 //Maximum length of service name
59 #ifndef DNS_SD_MAX_SERVICE_NAME_LEN
60  #define DNS_SD_MAX_SERVICE_NAME_LEN 16
61 #elif (DNS_SD_MAX_SERVICE_NAME_LEN < 1)
62  #error DNS_SD_MAX_SERVICE_NAME_LEN parameter is not valid
63 #endif
64 
65 //Maximum length of instance name
66 #ifndef DNS_SD_MAX_INSTANCE_NAME_LEN
67  #define DNS_SD_MAX_INSTANCE_NAME_LEN 32
68 #elif (DNS_SD_MAX_INSTANCE_NAME_LEN < 1)
69  #error DNS_SD_MAX_INSTANCE_NAME_LEN parameter is not valid
70 #endif
71 
72 //Maximum length of the discovery-time metadata (TXT record)
73 #ifndef DNS_SD_MAX_METADATA_LEN
74  #define DNS_SD_MAX_METADATA_LEN 128
75 #elif (DNS_SD_MAX_METADATA_LEN < 1)
76  #error DNS_SD_MAX_METADATA_LEN parameter is not valid
77 #endif
78 
79 //Default resource record TTL (cache lifetime)
80 #ifndef DNS_SD_DEFAULT_RR_TTL
81  #define DNS_SD_DEFAULT_RR_TTL 120
82 #elif (DNS_SD_DEFAULT_RR_TTL < 1)
83  #error DNS_SD_DEFAULT_RR_TTL parameter is not valid
84 #endif
85 
86 //Forward declaration of DnsSdContext structure
87 struct _DnsSdContext;
88 #define DnsSdContext struct _DnsSdContext
89 
90 //C++ guard
91 #ifdef __cplusplus
92  extern "C" {
93 #endif
94 
95 
96 /**
97  * @brief FSM state change callback
98  **/
99 
100 typedef void (*DnsSdStateChangeCallback)(DnsSdContext *context,
101  NetInterface *interface, MdnsState state);
102 
103 
104 /**
105  * @brief DNS-SD settings
106  **/
107 
108 typedef struct
109 {
110  NetInterface *interface; ///<Underlying network interface
111  uint_t numAnnouncements; ///<Number of announcement packets
112  uint32_t ttl; ///<TTL resource record
113  DnsSdStateChangeCallback stateChangeEvent; ///<FSM state change event
114 } DnsSdSettings;
115 
116 
117 /**
118  * @brief DNS-SD service descriptor
119  **/
120 
121 typedef struct
122 {
123  char_t name[DNS_SD_MAX_SERVICE_NAME_LEN + 1]; ///<Service name
124  uint16_t priority; ///<Priority of the target host
125  uint16_t weight; ///<Server selection mechanism
126  uint16_t port; ///<Port on the target host of this service
127  uint8_t metadata[DNS_SD_MAX_METADATA_LEN]; ///<Discovery-time metadata (TXT record)
128  size_t metadataLength; ///<Length of the metadata
129 } DnsSdService;
130 
131 
132 /**
133  * @brief DNS-SD context
134  **/
135 
137 {
138  DnsSdSettings settings; ///<DNS-SD settings
139  bool_t running; ///<DNS-SD is currently running
140  MdnsState state; ///<FSM state
141  bool_t conflict; ///<Conflict detected
142  bool_t tieBreakLost; ///<Tie-break lost
143  systime_t timestamp; ///<Timestamp to manage retransmissions
144  systime_t timeout; ///<Timeout value
145  uint_t retransmitCount; ///<Retransmission counter
146  char_t instanceName[DNS_SD_MAX_INSTANCE_NAME_LEN + 1]; ///<Service instance name
147  DnsSdService serviceList[DNS_SD_SERVICE_LIST_SIZE]; ///<List of registered services
148 };
149 
150 
151 //Tick counter to handle periodic operations
153 
154 //DNS-SD related functions
155 void dnsSdGetDefaultSettings(DnsSdSettings *settings);
156 error_t dnsSdInit(DnsSdContext *context, const DnsSdSettings *settings);
158 error_t dnsSdStop(DnsSdContext *context);
160 
161 error_t dnsSdSetInstanceName(DnsSdContext *context, const char_t *instanceName);
162 
163 error_t dnsSdRegisterService(DnsSdContext *context, const char_t *serviceName,
164  uint16_t priority, uint16_t weight, uint16_t port, const char_t *metadata);
165 
166 error_t dnsSdUnregisterService(DnsSdContext *context, const char_t *serviceName);
167 
170 
171 void dnsSdTick(DnsSdContext *interface);
172 void dnsSdLinkChangeEvent(DnsSdContext *interface);
173 
174 void dnsSdChangeState(DnsSdContext *context,
175  MdnsState newState, systime_t delay);
176 
178 
181 error_t dnsSdSendGoodbye(DnsSdContext *context, const DnsSdService *service);
182 
183 error_t dnsSdParseQuestion(NetInterface *interface, const MdnsMessage *query,
184  size_t offset, const DnsQuestion *question, MdnsMessage *response);
185 
186 void dnsSdParseNsRecord(NetInterface *interface, const MdnsMessage *query,
187  size_t offset, const DnsResourceRecord *record);
188 
189 void dnsSdParseAnRecord(NetInterface *interface, const MdnsMessage *response,
190  size_t offset, const DnsResourceRecord *record);
191 
193  MdnsMessage *response, bool_t legacyUnicast);
194 
196  MdnsMessage *message, const DnsSdService *service, uint32_t ttl);
197 
199  MdnsMessage *message, const DnsSdService *service, uint32_t ttl);
200 
202  const DnsSdService *service, bool_t cacheFlush, uint32_t ttl);
203 
205  const DnsSdService *service, bool_t cacheFlush, uint32_t ttl);
206 
208  const DnsSdService *service, bool_t cacheFlush, uint32_t ttl);
209 
210 //C++ guard
211 #ifdef __cplusplus
212  }
213 #endif
214 
215 #endif
uint16_t priority
Definition: dns_common.h:219
__start_packed struct @151 DnsQuestion
Question format.
uint32_t systime_t
Definition: compiler_port.h:44
error_t dnsSdAddServiceEnumPtrRecord(NetInterface *interface, MdnsMessage *message, const DnsSdService *service, uint32_t ttl)
Add PTR record to a mDNS message (in response to a meta-query)
Definition: dns_sd.c:1561
char char_t
Definition: compiler_port.h:41
error_t dnsSdAddTxtRecord(NetInterface *interface, MdnsMessage *message, const DnsSdService *service, bool_t cacheFlush, uint32_t ttl)
Add TXT record to a mDNS message.
Definition: dns_sd.c:1813
TCP/IP stack core.
void dnsSdParseNsRecord(NetInterface *interface, const MdnsMessage *query, size_t offset, const DnsResourceRecord *record)
Parse a resource record from the Authority Section.
Definition: dns_sd.c:1242
bool_t conflict
Conflict detected.
Definition: dns_sd.h:141
error_t dnsSdParseQuestion(NetInterface *interface, const MdnsMessage *query, size_t offset, const DnsQuestion *question, MdnsMessage *response)
Parse a question.
Definition: dns_sd.c:1083
void(* DnsSdStateChangeCallback)(DnsSdContext *context, NetInterface *interface, MdnsState state)
FSM state change callback.
Definition: dns_sd.h:100
uint_t numAnnouncements
Number of announcement packets.
Definition: dns_sd.h:111
uint8_t message[]
Definition: chap.h:150
mDNS message
Definition: mdns_common.h:75
void dnsSdGenerateAdditionalRecords(NetInterface *interface, MdnsMessage *response, bool_t legacyUnicast)
Additional record generation.
Definition: dns_sd.c:1407
error_t dnsSdStop(DnsSdContext *context)
Stop mDNS responder.
Definition: dns_sd.c:157
systime_t timeout
Timeout value.
Definition: dns_sd.h:144
void dnsSdParseAnRecord(NetInterface *interface, const MdnsMessage *response, size_t offset, const DnsResourceRecord *record)
Parse a resource record from the Answer Section.
Definition: dns_sd.c:1337
uint_t retransmitCount
Retransmission counter.
Definition: dns_sd.h:145
DNS-SD settings.
Definition: dns_sd.h:108
bool_t tieBreakLost
Tie-break lost.
Definition: dns_sd.h:142
uint16_t port
Port on the target host of this service.
Definition: dns_sd.h:126
error_t dnsSdUnregisterService(DnsSdContext *context, const char_t *serviceName)
Unregister a DNS-SD service.
Definition: dns_sd.c:406
uint16_t priority
Priority of the target host.
Definition: dns_sd.h:124
error_t dnsSdSendAnnouncement(DnsSdContext *context)
Send announcement packet.
Definition: dns_sd.c:914
uint_t dnsSdGetNumServices(DnsSdContext *context)
Get the number of registered services.
Definition: dns_sd.c:448
void dnsSdGetDefaultSettings(DnsSdSettings *settings)
Initialize settings with default values.
Definition: dns_sd.c:63
char_t instanceName[DNS_SD_MAX_INSTANCE_NAME_LEN+1]
Service instance name.
Definition: dns_sd.h:146
#define DNS_SD_MAX_SERVICE_NAME_LEN
Definition: dns_sd.h:60
void dnsSdChangeState(DnsSdContext *context, MdnsState newState, systime_t delay)
Update FSM state.
Definition: dns_sd.c:688
char_t name[]
uint32_t ttl
Definition: dns_common.h:203
DNS-SD context.
Definition: dns_sd.h:136
error_t dnsSdSetInstanceName(DnsSdContext *context, const char_t *instanceName)
Set service instance name.
Definition: dns_sd.c:212
uint16_t weight
Definition: dns_common.h:220
error_t dnsSdStartProbing(DnsSdContext *context)
Restart probing process.
Definition: dns_sd.c:483
MdnsState
mDNS responder states
uint16_t weight
Server selection mechanism.
Definition: dns_sd.h:125
DnsSdStateChangeCallback stateChangeEvent
FSM state change event.
Definition: dns_sd.h:113
void dnsSdChangeInstanceName(DnsSdContext *context)
Programmatically change the service instance name.
Definition: dns_sd.c:723
void dnsSdLinkChangeEvent(DnsSdContext *interface)
Callback function for link change event.
Definition: dns_sd.c:669
error_t dnsSdSendGoodbye(DnsSdContext *context, const DnsSdService *service)
Send goodbye packet.
Definition: dns_sd.c:1003
bool_t running
DNS-SD is currently running.
Definition: dns_sd.h:139
NetInterface * interface
Underlying network interface.
Definition: dns_sd.h:110
#define DNS_SD_MAX_METADATA_LEN
Definition: dns_sd.h:74
error_t
Error codes.
Definition: error.h:40
MdnsState state
FSM state.
Definition: dns_sd.h:140
unsigned int uint_t
Definition: compiler_port.h:43
#define DNS_SD_SERVICE_LIST_SIZE
Definition: dns_sd.h:53
uint32_t ttl
TTL resource record.
Definition: dns_sd.h:112
DnsSdSettings settings
DNS-SD settings.
Definition: dns_sd.h:138
#define NetInterface
Definition: net.h:34
systime_t timestamp
Timestamp to manage retransmissions.
Definition: dns_sd.h:143
MdnsState dnsSdGetState(DnsSdContext *context)
Retrieve current state.
Definition: dns_sd.c:188
uint16_t port
Definition: dns_common.h:221
error_t dnsSdSendProbe(DnsSdContext *context)
Send probe packet.
Definition: dns_sd.c:798
#define DNS_SD_MAX_INSTANCE_NAME_LEN
Definition: dns_sd.h:67
__start_packed struct @152 DnsResourceRecord
Resource record format.
error_t dnsSdAddPtrRecord(NetInterface *interface, MdnsMessage *message, const DnsSdService *service, uint32_t ttl)
Add PTR record to a mDNS message.
Definition: dns_sd.c:1630
Definitions common to mDNS client and mDNS responder.
systime_t dnsSdTickCounter
Definition: dns_sd.c:55
error_t dnsSdRegisterService(DnsSdContext *context, const char_t *serviceName, uint16_t priority, uint16_t weight, uint16_t port, const char_t *metadata)
Register a DNS-SD service.
Definition: dns_sd.c:265
error_t dnsSdAddNsecRecord(NetInterface *interface, MdnsMessage *message, const DnsSdService *service, bool_t cacheFlush, uint32_t ttl)
Add NSEC record to a mDNS message.
Definition: dns_sd.c:1896
error_t dnsSdStart(DnsSdContext *context)
Start mDNS responder.
Definition: dns_sd.c:126
error_t dnsSdAddSrvRecord(NetInterface *interface, MdnsMessage *message, const DnsSdService *service, bool_t cacheFlush, uint32_t ttl)
Add SRV record to a mDNS message.
Definition: dns_sd.c:1715
size_t metadataLength
Length of the metadata.
Definition: dns_sd.h:128
Common DNS routines.
int bool_t
Definition: compiler_port.h:47
DNS-SD service descriptor.
Definition: dns_sd.h:121
error_t dnsSdInit(DnsSdContext *context, const DnsSdSettings *settings)
DNS-DS initialization.
Definition: dns_sd.c:84
DnsSdService serviceList[DNS_SD_SERVICE_LIST_SIZE]
List of registered services.
Definition: dns_sd.h:147
void dnsSdTick(DnsSdContext *interface)
DNS-SD responder timer handler.
Definition: dns_sd.c:506
#define DnsSdContext
Definition: dns_sd.h:88