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)
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"];
83 [txtDict setValue:[NSString stringWithUTF8String:RAOP_PW] forKey:@"pw"];
84 [txtDict setValue:[NSString stringWithUTF8String:RAOP_VN] forKey:@"vn"];
85 [txtDict setValue:[NSString stringWithUTF8String:RAOP_TP] forKey:@"tp"];
86 [txtDict setValue:[NSString stringWithUTF8String:RAOP_MD] forKey:@"md"];
87 [txtDict setValue:[NSString stringWithUTF8String:GLOBAL_VERSION] forKey:@"vs"];
88 [txtDict setValue:[NSString stringWithUTF8String:RAOP_AM] forKey:@"am"];
89 [txtDict setValue:[NSString stringWithUTF8String:RAOP_SF] forKey:@"sf"];
90 txtData = [NSNetService dataFromTXTRecordDictionary:txtDict];
92 /* Create the service and publish it */
93 dnssd->raopService = [[NSNetService alloc] initWithDomain:@""
97 [dnssd->raopService setTXTRecordData:txtData];
98 [dnssd->raopService publish];
103 dnssd_register_airplay(dnssd_t *dnssd, const char *name, unsigned short port, const char *hwaddr, int hwaddrlen)
105 NSMutableDictionary *txtDict;
107 char deviceid[3*MAX_HWADDR_LEN];
113 if (dnssd->airplayService != nil) {
117 /* Convert hardware address to string */
118 ret = utils_hwaddr_airplay(deviceid, sizeof(deviceid), hwaddr, hwaddrlen);
123 memset(features, 0, sizeof(features));
124 snprintf(features, sizeof(features)-1, "0x%x", GLOBAL_FEATURES);
126 txtDict = [NSMutableDictionary dictionaryWithCapacity:0];
127 [txtDict setValue:[NSString stringWithUTF8String:deviceid] forKey:@"deviceid"];
128 [txtDict setValue:[NSString stringWithUTF8String:features] forKey:@"features"];
129 [txtDict setValue:[NSString stringWithUTF8String:GLOBAL_MODEL] forKey:@"model"];
130 txtData = [NSNetService dataFromTXTRecordDictionary:txtDict];
132 /* Create the service and publish it */
133 dnssd->airplayService = [[NSNetService alloc] initWithDomain:@""
134 type:@"_airplay._tcp"
135 name:[NSString stringWithUTF8String:name]
137 [dnssd->airplayService setTXTRecordData:txtData];
138 [dnssd->airplayService publish];
143 dnssd_unregister_raop(dnssd_t *dnssd)
147 if (dnssd->raopService == nil) {
151 [dnssd->raopService stop];
152 [dnssd->raopService release];
153 dnssd->raopService = nil;
157 dnssd_unregister_airplay(dnssd_t *dnssd)
161 if (dnssd->airplayService == nil) {
165 [dnssd->airplayService stop];
166 [dnssd->airplayService release];
167 dnssd->airplayService = nil;