2 * Embedded dns-sd client. This file is part of Shairport.
3 * Copyright (c) Paul Lietar 2013
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
29 #include <arpa/inet.h>
34 static DNSServiceRef service
;
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
};
44 #ifdef CONFIG_METADATA
45 if (config
.metadata_enabled
)
46 record
= recordwithmetadata
;
49 record
= recordwithoutmetadata
;
54 // Concatenate string contained i record into buf.
56 for (field
= record
; *field
; field
++) {
57 length
+= strlen(*field
) + 1; // One byte for length each time
60 char *buf
= malloc(length
* sizeof(char));
62 warn("dns_sd: buffer record allocation failed");
68 for (field
= record
; *field
; field
++) {
69 char *newp
= stpcpy(p
+ 1, *field
);
74 DNSServiceErrorType error
;
75 error
= DNSServiceRegister(&service
, 0, kDNSServiceInterfaceIndexAny
, ap1name
, config
.regtype
, "",
76 NULL
, htons((uint16_t)port
), length
, buf
, NULL
, NULL
);
80 if (error
== kDNSServiceErr_NoError
)
83 warn("dns-sd: DNSServiceRegister error %d", error
);
88 static void mdns_dns_sd_unregister(void) {
90 DNSServiceRefDeallocate(service
);
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
};