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 char hwaddr[MAX_HWADDR_LEN];
30 NSNetService *raopService;
31 NSNetService *airplayService;
35 dnssd_init(const char *hwaddr, int hwaddrlen, int *error)
39 if (error) *error = DNSSD_ERROR_NOERROR;
40 if (hwaddrlen > MAX_HWADDR_LEN) {
41 if (error) *error = DNSSD_ERROR_HWADDRLEN;
45 dnssd = calloc(1, sizeof(dnssd_t));
47 if (error) *error = DNSSD_ERROR_OUTOFMEM;
50 memcpy(dnssd->hwaddr, hwaddr, hwaddrlen);
51 dnssd->hwaddrlen = hwaddrlen;
57 dnssd_destroy(dnssd_t *dnssd)
63 dnssd_register_raop(dnssd_t *dnssd, const char *name, unsigned short port)
65 char hwaddrstr[MAX_SERVNAME];
66 NSString *serviceString;
67 NSMutableDictionary *txtDict;
73 if (dnssd->raopService != nil) {
77 /* Convert the hardware address to string */
78 ret = utils_hwaddr_raop(hwaddrstr, sizeof(hwaddrstr), dnssd->hwaddr, dnssd->hwaddrlen);
82 serviceString = [NSString stringWithFormat:@"%s@%s", hwaddrstr, name];
84 txtDict = [NSMutableDictionary dictionaryWithCapacity:0];
85 [txtDict setValue:[NSString stringWithUTF8String:RAOP_TXTVERS] forKey:@"txtvers"];
86 [txtDict setValue:[NSString stringWithUTF8String:RAOP_CH] forKey:@"ch"];
87 [txtDict setValue:[NSString stringWithUTF8String:RAOP_CN] forKey:@"cn"];
88 [txtDict setValue:[NSString stringWithUTF8String:RAOP_ET] forKey:@"et"];
89 [txtDict setValue:[NSString stringWithUTF8String:RAOP_SV] forKey:@"sv"];
90 [txtDict setValue:[NSString stringWithUTF8String:RAOP_DA] forKey:@"da"];
91 [txtDict setValue:[NSString stringWithUTF8String:RAOP_SR] forKey:@"sr"];
92 [txtDict setValue:[NSString stringWithUTF8String:RAOP_SS] forKey:@"ss"];
93 [txtDict setValue:[NSString stringWithUTF8String:RAOP_PW] forKey:@"pw"];
94 [txtDict setValue:[NSString stringWithUTF8String:RAOP_VN] forKey:@"vn"];
95 [txtDict setValue:[NSString stringWithUTF8String:RAOP_TP] forKey:@"tp"];
96 [txtDict setValue:[NSString stringWithUTF8String:RAOP_MD] forKey:@"md"];
97 [txtDict setValue:[NSString stringWithUTF8String:GLOBAL_VERSION] forKey:@"vs"];
98 [txtDict setValue:[NSString stringWithUTF8String:RAOP_AM] forKey:@"am"];
99 [txtDict setValue:[NSString stringWithUTF8String:RAOP_SF] forKey:@"sf"];
100 txtData = [NSNetService dataFromTXTRecordDictionary:txtDict];
102 /* Create the service and publish it */
103 dnssd->raopService = [[NSNetService alloc] initWithDomain:@""
107 [dnssd->raopService setTXTRecordData:txtData];
108 [dnssd->raopService publish];
113 dnssd_register_airplay(dnssd_t *dnssd, const char *name, unsigned short port)
115 NSMutableDictionary *txtDict;
117 char deviceid[3*MAX_HWADDR_LEN];
123 if (dnssd->airplayService != nil) {
127 /* Convert hardware address to string */
128 ret = utils_hwaddr_airplay(deviceid, sizeof(deviceid), dnssd->hwaddr, dnssd->hwaddrlen);
133 memset(features, 0, sizeof(features));
134 snprintf(features, sizeof(features)-1, "0x%x", GLOBAL_FEATURES);
136 txtDict = [NSMutableDictionary dictionaryWithCapacity:0];
137 [txtDict setValue:[NSString stringWithUTF8String:deviceid] forKey:@"deviceid"];
138 [txtDict setValue:[NSString stringWithUTF8String:features] forKey:@"features"];
139 [txtDict setValue:[NSString stringWithUTF8String:GLOBAL_MODEL] forKey:@"model"];
140 txtData = [NSNetService dataFromTXTRecordDictionary:txtDict];
142 /* Create the service and publish it */
143 dnssd->airplayService = [[NSNetService alloc] initWithDomain:@""
144 type:@"_airplay._tcp"
145 name:[NSString stringWithUTF8String:name]
147 [dnssd->airplayService setTXTRecordData:txtData];
148 [dnssd->airplayService publish];
153 dnssd_unregister_raop(dnssd_t *dnssd)
157 if (dnssd->raopService == nil) {
161 [dnssd->raopService stop];
162 [dnssd->raopService release];
163 dnssd->raopService = nil;
167 dnssd_unregister_airplay(dnssd_t *dnssd)
171 if (dnssd->airplayService == nil) {
175 [dnssd->airplayService stop];
176 [dnssd->airplayService release];
177 dnssd->airplayService = nil;