]> git.ipfire.org Git - thirdparty/shairport-sync.git/blame - mdns.c
Update check_classic_mac_basic.yml
[thirdparty/shairport-sync.git] / mdns.c
CommitLineData
cf8401db
JL
1/*
2 * mDNS registration handler. This file is part of Shairport.
3 * Copyright (c) James Laird 2013
3a02b79a 4 * Modifications, updates and additions (c) Mike Brady 2014 -- 2020
cf8401db
JL
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
064bd293
MB
28#include "mdns.h"
29#include "common.h"
30#include "config.h"
04b142db 31#include <memory.h>
8207b882
MB
32#include <stdio.h>
33#include <stdlib.h>
064bd293 34#include <string.h>
38969090 35
f6eddb84 36#ifdef CONFIG_AVAHI
38969090 37extern mdns_backend mdns_avahi;
f64f5982 38#endif
c9b3d2a2 39#ifdef CONFIG_DNS_SD
ce77feec 40extern mdns_backend mdns_dns_sd;
f64f5982
MB
41#endif
42#ifdef CONFIG_TINYSVCMDNS
43extern mdns_backend mdns_tinysvcmdns;
ce77feec 44#endif
3162977c
MB
45
46#ifdef CONFIG_EXTERNAL_MDNS
d5402548 47extern mdns_backend mdns_external_avahi;
f21b5faf 48extern mdns_backend mdns_external_dns_sd;
3162977c 49#endif
ce77feec 50
38969090 51static mdns_backend *mdns_backends[] = {
f6eddb84 52#ifdef CONFIG_AVAHI
064bd293 53 &mdns_avahi,
ce77feec 54#endif
c9b3d2a2 55#ifdef CONFIG_DNS_SD
064bd293 56 &mdns_dns_sd,
f64f5982 57#endif
8207b882 58#ifdef CONFIG_TINYSVCMDNS
592119bc 59 &mdns_tinysvcmdns,
8207b882 60#endif
3162977c 61#ifdef CONFIG_EXTERNAL_MDNS
d5402548 62 &mdns_external_avahi,
f21b5faf 63 &mdns_external_dns_sd,
3162977c 64#endif
87a0475c 65 NULL};
f6eddb84 66
e1034e11
MB
67void 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;
87a0475c
MB
70 int i;
71 for (i = 0; i < 6; i++) {
03d291f4 72 snprintf(p, 3, "%02X", config.ap1_prefix[i]);
87a0475c
MB
73 p += 2;
74 }
75 *p++ = '@';
8896bd10 76 strcpy(p, config.service_name);
04b142db 77
87a0475c 78 mdns_backend **b = NULL;
958fceb6 79
87a0475c
MB
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;
a109b587
MB
84 int error = (*b)->mdns_register(ap1_service_name, config.service_name, config.port,
85 txt_records, secondary_txt_records);
87a0475c
MB
86 if (error >= 0) {
87 config.mdns = *b;
88 }
89 break;
958fceb6 90 }
87a0475c
MB
91
92 if (*b == NULL)
93 warn("%s mDNS backend not found");
94 } else {
e1034e11 95 // default -- pick the first back end
87a0475c 96 for (b = mdns_backends; *b; b++) {
a109b587
MB
97 int error = (*b)->mdns_register(ap1_service_name, config.service_name, config.port,
98 txt_records, secondary_txt_records);
87a0475c
MB
99 if (error >= 0) {
100 config.mdns = *b;
101 break;
102 }
38969090 103 }
87a0475c 104 }
04b142db 105
87a0475c
MB
106 if (config.mdns == NULL)
107 die("Could not establish mDNS advertisement!");
72079ada
MB
108
109 mdns_dacp_monitor_start(); // create a dacp monitor thread
38969090 110}
04b142db 111
2ab97d98 112void mdns_update(char **txt_records, char **secondary_txt_records) {
f5e21ade 113 if ((config.mdns) && (config.mdns->mdns_update)) {
2ab97d98
MB
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
38969090 119void mdns_unregister(void) {
72079ada 120 mdns_dacp_monitor_stop();
87a0475c
MB
121 if (config.mdns) {
122 config.mdns->mdns_unregister();
123 }
38969090 124}
04b142db 125
72079ada
MB
126void 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.");
139c18bf
MB
131}
132
72079ada
MB
133void mdns_dacp_monitor_stop() {
134 if ((config.mdns) && (config.mdns->mdns_dacp_monitor_stop)) {
135 config.mdns->mdns_dacp_monitor_stop();
139c18bf 136 } else
72079ada 137 debug(3, "Can't stop a DACP monitor -- no mdns_dacp_monitor_stop registered.");
139c18bf 138}
72079ada
MB
139
140void 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
958fceb6 147void mdns_ls_backends(void) {
87a0475c
MB
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 }
958fceb6 153}