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-2024 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 2.4.0
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 //C++ guard
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 #endif
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
Common DNS routines.
uint16_t priority
Definition: dns_common.h:265
uint16_t weight
Definition: dns_common.h:266
uint16_t port
Definition: dns_common.h:267
error_t dnsSdStartProbing(DnsSdContext *context)
Restart probing process.
Definition: dns_sd.c:491
void dnsSdLinkChangeEvent(DnsSdContext *interface)
Callback function for link change event.
Definition: dns_sd.c:680
error_t dnsSdUnregisterService(DnsSdContext *context, const char_t *serviceName)
Unregister a DNS-SD service.
Definition: dns_sd.c:414
error_t dnsSdStop(DnsSdContext *context)
Stop mDNS responder.
Definition: dns_sd.c:156
uint_t dnsSdGetNumServices(DnsSdContext *context)
Get the number of registered services.
Definition: dns_sd.c:456
#define DNS_SD_MAX_SERVICE_NAME_LEN
Definition: dns_sd.h:62
void dnsSdGetDefaultSettings(DnsSdSettings *settings)
Initialize settings with default values.
Definition: dns_sd.c:62
void dnsSdTick(DnsSdContext *interface)
DNS-SD responder timer handler.
Definition: dns_sd.c:514
#define DNS_SD_SERVICE_LIST_SIZE
Definition: dns_sd.h:55
#define DnsSdContext
Definition: dns_sd.h:90
error_t dnsSdSetInstanceName(DnsSdContext *context, const char_t *instanceName)
Set service instance name.
Definition: dns_sd.c:211
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
MdnsState dnsSdGetState(DnsSdContext *context)
Retrieve current state.
Definition: dns_sd.c:187
error_t dnsSdStart(DnsSdContext *context)
Start mDNS responder.
Definition: dns_sd.c:125
void(* DnsSdStateChangeCallback)(DnsSdContext *context, NetInterface *interface, MdnsState state)
FSM state change callback.
Definition: dns_sd.h:102
#define DNS_SD_MAX_METADATA_LEN
Definition: dns_sd.h:76
error_t dnsSdInit(DnsSdContext *context, const DnsSdSettings *settings)
DNS-DS initialization.
Definition: dns_sd.c:83
#define DNS_SD_MAX_INSTANCE_NAME_LEN
Definition: dns_sd.h:69
systime_t dnsSdTickCounter
Definition: dns_sd.c:54
error_t
Error codes.
Definition: error.h:43
Definitions common to mDNS client and mDNS responder.
MdnsState
mDNS responder states
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
uint32_t systime_t
System time.
char_t name[]
DNS-SD context.
Definition: dns_sd.h:139
systime_t timestamp
Timestamp to manage retransmissions.
Definition: dns_sd.h:145
bool_t tieBreakLost
Tie-break lost.
Definition: dns_sd.h:144
bool_t running
DNS-SD is currently running.
Definition: dns_sd.h:141
bool_t conflict
Conflict detected.
Definition: dns_sd.h:143
uint_t retransmitCount
Retransmission counter.
Definition: dns_sd.h:147
DnsSdService serviceList[DNS_SD_SERVICE_LIST_SIZE]
List of registered services.
Definition: dns_sd.h:149
DnsSdSettings settings
DNS-SD settings.
Definition: dns_sd.h:140
char_t instanceName[DNS_SD_MAX_INSTANCE_NAME_LEN+1]
Service instance name.
Definition: dns_sd.h:148
systime_t timeout
Timeout value.
Definition: dns_sd.h:146
MdnsState state
FSM state.
Definition: dns_sd.h:142
DNS-SD service descriptor.
Definition: dns_sd.h:124
uint16_t priority
Priority of the target host.
Definition: dns_sd.h:126
uint16_t weight
Server selection mechanism.
Definition: dns_sd.h:127
size_t metadataLength
Length of the metadata.
Definition: dns_sd.h:130
uint16_t port
Port on the target host of this service.
Definition: dns_sd.h:128
DNS-SD settings.
Definition: dns_sd.h:111
DnsSdStateChangeCallback stateChangeEvent
FSM state change event.
Definition: dns_sd.h:115
uint32_t ttl
TTL resource record.
Definition: dns_sd.h:114
uint_t numAnnouncements
Number of announcement packets.
Definition: dns_sd.h:113
NetInterface * interface
Underlying network interface.
Definition: dns_sd.h:112