]> git.ipfire.org Git - thirdparty/shairport-sync.git/blame - mdns_dns_sd.c
Update RELEASENOTES-DEVELOPMENT.md
[thirdparty/shairport-sync.git] / mdns_dns_sd.c
CommitLineData
ce77feec
PL
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
c8b0be30 27#include "common.h"
54d761ff 28#include "mdns.h"
064bd293
MB
29#include <arpa/inet.h>
30#include <dns_sd.h>
31#include <stdlib.h>
32#include <string.h>
ce77feec
PL
33
34static DNSServiceRef service;
35
54e228cb
MB
36static 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) {
49371cb9 39 char *recordwithoutmetadata[] = {MDNS_RECORD_WITHOUT_METADATA, NULL};
75f3f912 40#ifdef CONFIG_METADATA
49371cb9 41 char *recordwithmetadata[] = {MDNS_RECORD_WITH_METADATA, NULL};
87a0475c
MB
42#endif
43 char **record;
75f3f912 44#ifdef CONFIG_METADATA
6ae067af 45 if (config.metadata_enabled)
87a0475c
MB
46 record = recordwithmetadata;
47 else
75f3f912 48#endif
87a0475c 49 record = recordwithoutmetadata;
ce77feec 50
87a0475c 51 uint16_t length = 0;
49371cb9 52 char **field;
ce77feec 53
87a0475c 54 // Concatenate string contained i record into buf.
ce77feec 55
87a0475c
MB
56 for (field = record; *field; field++) {
57 length += strlen(*field) + 1; // One byte for length each time
58 }
ce77feec 59
87a0475c
MB
60 char *buf = malloc(length * sizeof(char));
61 if (buf == NULL) {
62 warn("dns_sd: buffer record allocation failed");
63 return -1;
64 }
ce77feec 65
87a0475c 66 char *p = buf;
ce77feec 67
87a0475c
MB
68 for (field = record; *field; field++) {
69 char *newp = stpcpy(p + 1, *field);
70 *p = newp - p - 1;
71 p = newp;
72 }
ce77feec 73
87a0475c 74 DNSServiceErrorType error;
54e228cb 75 error = DNSServiceRegister(&service, 0, kDNSServiceInterfaceIndexAny, ap1name, config.regtype, "",
87a0475c 76 NULL, htons((uint16_t)port), length, buf, NULL, NULL);
ce77feec 77
87a0475c 78 free(buf);
ce77feec 79
87a0475c
MB
80 if (error == kDNSServiceErr_NoError)
81 return 0;
82 else {
83 warn("dns-sd: DNSServiceRegister error %d", error);
84 return -1;
85 }
ce77feec
PL
86}
87
88static void mdns_dns_sd_unregister(void) {
87a0475c
MB
89 if (service) {
90 DNSServiceRefDeallocate(service);
91 service = NULL;
92 }
ce77feec
PL
93}
94
87a0475c
MB
95mdns_backend mdns_dns_sd = {.name = "dns-sd",
96 .mdns_register = mdns_dns_sd_register,
139c18bf 97 .mdns_unregister = mdns_dns_sd_unregister,
72079ada
MB
98 .mdns_dacp_monitor_start = NULL,
99 .mdns_dacp_monitor_set_id = NULL,
100 .mdns_dacp_monitor_stop = NULL};