]> git.ipfire.org Git - thirdparty/shairport-sync.git/blob - mdns.c
Update RELEASENOTES-DEVELOPMENT.md
[thirdparty/shairport-sync.git] / mdns.c
1 /*
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
5 * All rights reserved.
6 *
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:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
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.
26 */
27
28 #include "mdns.h"
29 #include "common.h"
30 #include "config.h"
31 #include <memory.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #ifdef CONFIG_AVAHI
37 extern mdns_backend mdns_avahi;
38 #endif
39 #ifdef CONFIG_DNS_SD
40 extern mdns_backend mdns_dns_sd;
41 #endif
42 #ifdef CONFIG_TINYSVCMDNS
43 extern mdns_backend mdns_tinysvcmdns;
44 #endif
45
46 #ifdef CONFIG_EXTERNAL_MDNS
47 extern mdns_backend mdns_external_avahi;
48 extern mdns_backend mdns_external_dns_sd;
49 #endif
50
51 static mdns_backend *mdns_backends[] = {
52 #ifdef CONFIG_AVAHI
53 &mdns_avahi,
54 #endif
55 #ifdef CONFIG_DNS_SD
56 &mdns_dns_sd,
57 #endif
58 #ifdef CONFIG_TINYSVCMDNS
59 &mdns_tinysvcmdns,
60 #endif
61 #ifdef CONFIG_EXTERNAL_MDNS
62 &mdns_external_avahi,
63 &mdns_external_dns_sd,
64 #endif
65 NULL};
66
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;
70 int i;
71 for (i = 0; i < 6; i++) {
72 snprintf(p, 3, "%02X", config.ap1_prefix[i]);
73 p += 2;
74 }
75 *p++ = '@';
76 strcpy(p, config.service_name);
77
78 mdns_backend **b = NULL;
79
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
83 continue;
84 int error = (*b)->mdns_register(ap1_service_name, config.service_name, config.port,
85 txt_records, secondary_txt_records);
86 if (error >= 0) {
87 config.mdns = *b;
88 }
89 break;
90 }
91
92 if (*b == NULL)
93 warn("%s mDNS backend not found");
94 } else {
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);
99 if (error >= 0) {
100 config.mdns = *b;
101 break;
102 }
103 }
104 }
105
106 if (config.mdns == NULL)
107 die("Could not establish mDNS advertisement!");
108
109 mdns_dacp_monitor_start(); // create a dacp monitor thread
110 }
111
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);
115 } else
116 debug(1, "Can't mdns_update -- no mdns_update registered.");
117 }
118
119 void mdns_unregister(void) {
120 mdns_dacp_monitor_stop();
121 if (config.mdns) {
122 config.mdns->mdns_unregister();
123 }
124 }
125
126 void mdns_dacp_monitor_start(void) {
127 if ((config.mdns) && (config.mdns->mdns_dacp_monitor_start)) {
128 config.mdns->mdns_dacp_monitor_start();
129 } else
130 debug(3, "Can't start a DACP monitor -- no mdns_dacp_monitor start registered.");
131 }
132
133 void mdns_dacp_monitor_stop() {
134 if ((config.mdns) && (config.mdns->mdns_dacp_monitor_stop)) {
135 config.mdns->mdns_dacp_monitor_stop();
136 } else
137 debug(3, "Can't stop a DACP monitor -- no mdns_dacp_monitor_stop registered.");
138 }
139
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);
143 } else
144 debug(3, "Can't set dacp_id -- no mdns_dacp_set_id registered.");
145 }
146
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);
152 }
153 }