2 * Copyright (C) 2011-2012 Juho Vähä-Herttua
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
22 #import <Foundation/Foundation.h>
24 #define MAX_SERVNAME 256
27 NSNetService *raopService;
28 NSNetService *airplayService;
32 dnssd_init(int *error)
36 if (error) *error = DNSSD_ERROR_NOERROR;
38 dnssd = calloc(1, sizeof(dnssd_t));
40 if (error) *error = DNSSD_ERROR_OUTOFMEM;
47 dnssd_destroy(dnssd_t *dnssd)
53 dnssd_register_raop(dnssd_t *dnssd, const char *name, unsigned short port, const char *hwaddr, int hwaddrlen, int password)
55 char hwaddrstr[MAX_SERVNAME];
56 NSString *serviceString;
57 NSMutableDictionary *txtDict;
63 if (dnssd->raopService != nil) {
67 /* Convert the hardware address to string */
68 ret = utils_hwaddr_raop(hwaddrstr, sizeof(hwaddrstr), hwaddr, hwaddrlen);
72 serviceString = [NSString stringWithFormat:@"%s@%s", hwaddrstr, name];
74 txtDict = [NSMutableDictionary dictionaryWithCapacity:0];
75 [txtDict setValue:[NSString stringWithUTF8String:RAOP_TXTVERS] forKey:@"txtvers"];
76 [txtDict setValue:[NSString stringWithUTF8String:RAOP_CH] forKey:@"ch"];
77 [txtDict setValue:[NSString stringWithUTF8String:RAOP_CN] forKey:@"cn"];
78 [txtDict setValue:[NSString stringWithUTF8String:RAOP_ET] forKey:@"et"];
79 [txtDict setValue:[NSString stringWithUTF8String:RAOP_SV] forKey:@"sv"];
80 [txtDict setValue:[NSString stringWithUTF8String:RAOP_DA] forKey:@"da"];
81 [txtDict setValue:[NSString stringWithUTF8String:RAOP_SR] forKey:@"sr"];
82 [txtDict setValue:[NSString stringWithUTF8String:RAOP_SS] forKey:@"ss"];
84 [txtDict setValue:@"true" forKey:@"pw"];
86 [txtDict setValue:@"false" forKey:@"pw"];
88 [txtDict setValue:[NSString stringWithUTF8String:RAOP_VN] forKey:@"vn"];
89 [txtDict setValue:[NSString stringWithUTF8String:RAOP_TP] forKey:@"tp"];
90 [txtDict setValue:[NSString stringWithUTF8String:RAOP_MD] forKey:@"md"];
91 [txtDict setValue:[NSString stringWithUTF8String:GLOBAL_VERSION] forKey:@"vs"];
92 [txtDict setValue:[NSString stringWithUTF8String:RAOP_AM] forKey:@"am"];
93 [txtDict setValue:[NSString stringWithUTF8String:RAOP_SF] forKey:@"sf"];
94 txtData = [NSNetService dataFromTXTRecordDictionary:txtDict];
96 /* Create the service and publish it */
97 dnssd->raopService = [[NSNetService alloc] initWithDomain:@""
101 [dnssd->raopService setTXTRecordData:txtData];
102 [dnssd->raopService publish];
107 dnssd_register_airplay(dnssd_t *dnssd, const char *name, unsigned short port, const char *hwaddr, int hwaddrlen)
109 NSMutableDictionary *txtDict;
111 char deviceid[3*MAX_HWADDR_LEN];
117 if (dnssd->airplayService != nil) {
121 /* Convert hardware address to string */
122 ret = utils_hwaddr_airplay(deviceid, sizeof(deviceid), hwaddr, hwaddrlen);
127 memset(features, 0, sizeof(features));
128 snprintf(features, sizeof(features)-1, "0x%x", GLOBAL_FEATURES);
130 txtDict = [NSMutableDictionary dictionaryWithCapacity:0];
131 [txtDict setValue:[NSString stringWithUTF8String:deviceid] forKey:@"deviceid"];
132 [txtDict setValue:[NSString stringWithUTF8String:features] forKey:@"features"];
133 [txtDict setValue:[NSString stringWithUTF8String:GLOBAL_MODEL] forKey:@"model"];
134 txtData = [NSNetService dataFromTXTRecordDictionary:txtDict];
136 /* Create the service and publish it */
137 dnssd->airplayService = [[NSNetService alloc] initWithDomain:@""
138 type:@"_airplay._tcp"
139 name:[NSString stringWithUTF8String:name]
141 [dnssd->airplayService setTXTRecordData:txtData];
142 [dnssd->airplayService publish];
147 dnssd_unregister_raop(dnssd_t *dnssd)
151 if (dnssd->raopService == nil) {
155 [dnssd->raopService stop];
156 [dnssd->raopService release];
157 dnssd->raopService = nil;
161 dnssd_unregister_airplay(dnssd_t *dnssd)
165 if (dnssd->airplayService == nil) {
169 [dnssd->airplayService stop];
170 [dnssd->airplayService release];
171 dnssd->airplayService = nil;