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