]> git.ipfire.org Git - thirdparty/shairport-sync.git/blob - mdns_dns_sd.c
Update check_classic_systemd_full.yml
[thirdparty/shairport-sync.git] / mdns_dns_sd.c
1 /*
2 * Embedded dns-sd client. This file is part of Shairport.
3 * Copyright (c) Paul Lietar 2013
4 * All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 #include "common.h"
28 #include "mdns.h"
29 #include <arpa/inet.h>
30 #include <dns_sd.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 static DNSServiceRef service;
35
36 static int mdns_dns_sd_register(char *ap1name, __attribute__((unused)) char *ap2name, int port,
37 __attribute__((unused)) char **txt_records,
38 __attribute__((unused)) char **secondary_txt_records) {
39 char *recordwithoutmetadata[] = {MDNS_RECORD_WITHOUT_METADATA, NULL};
40 #ifdef CONFIG_METADATA
41 char *recordwithmetadata[] = {MDNS_RECORD_WITH_METADATA, NULL};
42 #endif
43 char **record;
44 #ifdef CONFIG_METADATA
45 if (config.metadata_enabled)
46 record = recordwithmetadata;
47 else
48 #endif
49 record = recordwithoutmetadata;
50
51 uint16_t length = 0;
52 char **field;
53
54 // Concatenate string contained i record into buf.
55
56 for (field = record; *field; field++) {
57 length += strlen(*field) + 1; // One byte for length each time
58 }
59
60 char *buf = malloc(length * sizeof(char));
61 if (buf == NULL) {
62 warn("dns_sd: buffer record allocation failed");
63 return -1;
64 }
65
66 char *p = buf;
67
68 for (field = record; *field; field++) {
69 char *newp = stpcpy(p + 1, *field);
70 *p = newp - p - 1;
71 p = newp;
72 }
73
74 DNSServiceErrorType error;
75 error = DNSServiceRegister(&service, 0, kDNSServiceInterfaceIndexAny, ap1name, config.regtype, "",
76 NULL, htons((uint16_t)port), length, buf, NULL, NULL);
77
78 free(buf);
79
80 if (error == kDNSServiceErr_NoError)
81 return 0;
82 else {
83 warn("dns-sd: DNSServiceRegister error %d", error);
84 return -1;
85 }
86 }
87
88 static void mdns_dns_sd_unregister(void) {
89 if (service) {
90 DNSServiceRefDeallocate(service);
91 service = NULL;
92 }
93 }
94
95 mdns_backend mdns_dns_sd = {.name = "dns-sd",
96 .mdns_register = mdns_dns_sd_register,
97 .mdns_unregister = mdns_dns_sd_unregister,
98 .mdns_dacp_monitor_start = NULL,
99 .mdns_dacp_monitor_set_id = NULL,
100 .mdns_dacp_monitor_stop = NULL};