2 * mDNS registration handler. This file is part of Shairport.
3 * Copyright (c) James Laird 2013
4 * Modifications, updates and additions (c) Mike Brady 2014 -- 2020
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use,
11 * copy, modify, merge, publish, distribute, sublicense, and/or
12 * sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
37 extern mdns_backend mdns_avahi
;
40 extern mdns_backend mdns_dns_sd
;
42 #ifdef CONFIG_TINYSVCMDNS
43 extern mdns_backend mdns_tinysvcmdns
;
46 #ifdef CONFIG_EXTERNAL_MDNS
47 extern mdns_backend mdns_external_avahi
;
48 extern mdns_backend mdns_external_dns_sd
;
51 static mdns_backend
*mdns_backends
[] = {
58 #ifdef CONFIG_TINYSVCMDNS
61 #ifdef CONFIG_EXTERNAL_MDNS
63 &mdns_external_dns_sd
,
67 void mdns_register(char **txt_records
, char **secondary_txt_records
) {
68 char *ap1_service_name
= alloca(strlen(config
.service_name
) + 14);
69 char *p
= ap1_service_name
;
71 for (i
= 0; i
< 6; i
++) {
72 snprintf(p
, 3, "%02X", config
.ap1_prefix
[i
]);
76 strcpy(p
, config
.service_name
);
78 mdns_backend
**b
= NULL
;
80 if (config
.mdns_name
!= NULL
) {
81 for (b
= mdns_backends
; *b
; b
++) {
82 if (strcmp((*b
)->name
, config
.mdns_name
) != 0) // Not the one we are looking for
84 int error
= (*b
)->mdns_register(ap1_service_name
, config
.service_name
, config
.port
,
85 txt_records
, secondary_txt_records
);
93 warn("%s mDNS backend not found");
95 // default -- pick the first back end
96 for (b
= mdns_backends
; *b
; b
++) {
97 int error
= (*b
)->mdns_register(ap1_service_name
, config
.service_name
, config
.port
,
98 txt_records
, secondary_txt_records
);
106 if (config
.mdns
== NULL
)
107 die("Could not establish mDNS advertisement!");
109 mdns_dacp_monitor_start(); // create a dacp monitor thread
112 void mdns_update(char **txt_records
, char **secondary_txt_records
) {
113 if ((config
.mdns
) && (config
.mdns
->mdns_update
)) {
114 config
.mdns
->mdns_update(txt_records
, secondary_txt_records
);
116 debug(1, "Can't mdns_update -- no mdns_update registered.");
119 void mdns_unregister(void) {
120 mdns_dacp_monitor_stop();
122 config
.mdns
->mdns_unregister();
126 void mdns_dacp_monitor_start(void) {
127 if ((config
.mdns
) && (config
.mdns
->mdns_dacp_monitor_start
)) {
128 config
.mdns
->mdns_dacp_monitor_start();
130 debug(3, "Can't start a DACP monitor -- no mdns_dacp_monitor start registered.");
133 void mdns_dacp_monitor_stop() {
134 if ((config
.mdns
) && (config
.mdns
->mdns_dacp_monitor_stop
)) {
135 config
.mdns
->mdns_dacp_monitor_stop();
137 debug(3, "Can't stop a DACP monitor -- no mdns_dacp_monitor_stop registered.");
140 void mdns_dacp_monitor_set_id(const char *dacp_id
) {
141 if ((config
.mdns
) && (config
.mdns
->mdns_dacp_monitor_set_id
)) {
142 config
.mdns
->mdns_dacp_monitor_set_id(dacp_id
);
144 debug(3, "Can't set dacp_id -- no mdns_dacp_set_id registered.");
147 void mdns_ls_backends(void) {
148 mdns_backend
**b
= NULL
;
149 printf("Available mDNS backends: \n");
150 for (b
= mdns_backends
; *b
; b
++) {
151 printf(" %s\n", (*b
)->name
);