File: mdns_dns_sd.c

package info (click to toggle)
shairport-sync 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,916 kB
  • sloc: ansic: 14,041; cpp: 1,612; xml: 840; makefile: 157; sh: 143
file content (97 lines) | stat: -rw-r--r-- 3,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Embedded dns-sd client. This file is part of Shairport.
 * Copyright (c) Paul Lietar 2013
 * All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "mdns.h"
#include "common.h"
#include <arpa/inet.h>
#include <dns_sd.h>
#include <stdlib.h>
#include <string.h>

static DNSServiceRef service;

static int mdns_dns_sd_register(char *apname, int port) {
  char *recordwithoutmetadata[] = {MDNS_RECORD_WITHOUT_METADATA, NULL};
#ifdef CONFIG_METADATA
  char *recordwithmetadata[] = {MDNS_RECORD_WITH_METADATA, NULL};
#endif
  char **record;
#ifdef CONFIG_METADATA
  if (config.metadata_enabled)
    record = recordwithmetadata;
  else
#endif
    record = recordwithoutmetadata;

  uint16_t length = 0;
  char **field;

  // Concatenate string contained i record into buf.

  for (field = record; *field; field++) {
    length += strlen(*field) + 1; // One byte for length each time
  }

  char *buf = malloc(length * sizeof(char));
  if (buf == NULL) {
    warn("dns_sd: buffer record allocation failed");
    return -1;
  }

  char *p = buf;

  for (field = record; *field; field++) {
    char *newp = stpcpy(p + 1, *field);
    *p = newp - p - 1;
    p = newp;
  }

  DNSServiceErrorType error;
  error = DNSServiceRegister(&service, 0, kDNSServiceInterfaceIndexAny, apname, config.regtype, "",
                             NULL, htons((uint16_t)port), length, buf, NULL, NULL);

  free(buf);

  if (error == kDNSServiceErr_NoError)
    return 0;
  else {
    warn("dns-sd: DNSServiceRegister error %d", error);
    return -1;
  }
}

static void mdns_dns_sd_unregister(void) {
  if (service) {
    DNSServiceRefDeallocate(service);
    service = NULL;
  }
}

mdns_backend mdns_dns_sd = {.name = "dns-sd",
                            .mdns_register = mdns_dns_sd_register,
                            .mdns_unregister = mdns_dns_sd_unregister,
                            .mdns_dacp_monitor = NULL,
                            .mdns_dacp_dont_monitor = NULL};