Commit | Line | Data |
---|---|---|
23e7e3ae JVH |
1 | /** |
2 | * Copyright (C) 2011-2012 Juho Vähä-Herttua | |
3 | * | |
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. | |
8 | * | |
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. | |
13 | */ | |
14 | ||
2340bcd3 JVH |
15 | #include <stdlib.h> |
16 | ||
17 | #include "dnssd.h" | |
18 | #include "dnssdint.h" | |
19 | #include "global.h" | |
20 | #include "utils.h" | |
21 | ||
22 | #import <Foundation/Foundation.h> | |
23 | ||
24 | #define MAX_SERVNAME 256 | |
25 | ||
26 | struct dnssd_s { | |
2340bcd3 JVH |
27 | NSNetService *raopService; |
28 | NSNetService *airplayService; | |
29 | }; | |
30 | ||
31 | dnssd_t * | |
067f00ef | 32 | dnssd_init(int *error) |
2340bcd3 JVH |
33 | { |
34 | dnssd_t *dnssd; | |
35 | ||
36 | if (error) *error = DNSSD_ERROR_NOERROR; | |
2340bcd3 JVH |
37 | |
38 | dnssd = calloc(1, sizeof(dnssd_t)); | |
39 | if (!dnssd) { | |
40 | if (error) *error = DNSSD_ERROR_OUTOFMEM; | |
41 | return NULL; | |
42 | } | |
2340bcd3 JVH |
43 | return dnssd; |
44 | } | |
45 | ||
46 | void | |
47 | dnssd_destroy(dnssd_t *dnssd) | |
48 | { | |
49 | free(dnssd); | |
50 | } | |
51 | ||
52 | int | |
067f00ef | 53 | dnssd_register_raop(dnssd_t *dnssd, const char *name, unsigned short port, const char *hwaddr, int hwaddrlen) |
2340bcd3 JVH |
54 | { |
55 | char hwaddrstr[MAX_SERVNAME]; | |
56 | NSString *serviceString; | |
57 | NSMutableDictionary *txtDict; | |
58 | NSData *txtData; | |
59 | int ret; | |
60 | ||
61 | assert(dnssd); | |
62 | ||
63 | if (dnssd->raopService != nil) { | |
64 | return -1; | |
65 | } | |
66 | ||
67 | /* Convert the hardware address to string */ | |
067f00ef | 68 | ret = utils_hwaddr_raop(hwaddrstr, sizeof(hwaddrstr), hwaddr, hwaddrlen); |
2340bcd3 JVH |
69 | if (ret < 0) { |
70 | return -2; | |
71 | } | |
72 | serviceString = [NSString stringWithFormat:@"%s@%s", hwaddrstr, name]; | |
73 | ||
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]; | |
91 | ||
92 | /* Create the service and publish it */ | |
93 | dnssd->raopService = [[NSNetService alloc] initWithDomain:@"" | |
94 | type:@"_raop._tcp" | |
95 | name:serviceString | |
96 | port:port]; | |
97 | [dnssd->raopService setTXTRecordData:txtData]; | |
98 | [dnssd->raopService publish]; | |
99 | return 1; | |
100 | } | |
101 | ||
102 | int | |
067f00ef | 103 | dnssd_register_airplay(dnssd_t *dnssd, const char *name, unsigned short port, const char *hwaddr, int hwaddrlen) |
2340bcd3 JVH |
104 | { |
105 | NSMutableDictionary *txtDict; | |
106 | NSData *txtData; | |
107 | char deviceid[3*MAX_HWADDR_LEN]; | |
108 | char features[16]; | |
109 | int ret; | |
110 | ||
111 | assert(dnssd); | |
112 | ||
113 | if (dnssd->airplayService != nil) { | |
114 | return -1; | |
115 | } | |
116 | ||
117 | /* Convert hardware address to string */ | |
067f00ef | 118 | ret = utils_hwaddr_airplay(deviceid, sizeof(deviceid), hwaddr, hwaddrlen); |
2340bcd3 JVH |
119 | if (ret < 0) { |
120 | return -2; | |
121 | } | |
122 | ||
123 | memset(features, 0, sizeof(features)); | |
124 | snprintf(features, sizeof(features)-1, "0x%x", GLOBAL_FEATURES); | |
125 | ||
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]; | |
131 | ||
132 | /* Create the service and publish it */ | |
133 | dnssd->airplayService = [[NSNetService alloc] initWithDomain:@"" | |
134 | type:@"_airplay._tcp" | |
135 | name:[NSString stringWithUTF8String:name] | |
136 | port:port]; | |
137 | [dnssd->airplayService setTXTRecordData:txtData]; | |
138 | [dnssd->airplayService publish]; | |
139 | return 1; | |
140 | } | |
141 | ||
142 | void | |
143 | dnssd_unregister_raop(dnssd_t *dnssd) | |
144 | { | |
145 | assert(dnssd); | |
146 | ||
147 | if (dnssd->raopService == nil) { | |
148 | return; | |
149 | } | |
150 | ||
151 | [dnssd->raopService stop]; | |
152 | [dnssd->raopService release]; | |
153 | dnssd->raopService = nil; | |
154 | } | |
155 | ||
156 | void | |
157 | dnssd_unregister_airplay(dnssd_t *dnssd) | |
158 | { | |
159 | assert(dnssd); | |
160 | ||
161 | if (dnssd->airplayService == nil) { | |
162 | return; | |
163 | } | |
164 | ||
165 | [dnssd->airplayService stop]; | |
166 | [dnssd->airplayService release]; | |
167 | dnssd->airplayService = nil; | |
168 | } |