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 | #include <string.h> | |
17 | #include <stdio.h> | |
18 | #include <assert.h> | |
19 | ||
20 | #include "dnssd.h" | |
21 | #include "dnssdint.h" | |
22 | #include "global.h" | |
23 | #include "compat.h" | |
24 | #include "utils.h" | |
25 | ||
26 | #define MAX_DEVICEID 18 | |
27 | #define MAX_SERVNAME 256 | |
28 | ||
29 | #ifndef WIN32 | |
30 | # include <dns_sd.h> | |
427cd9fb | 31 | # define DNSSD_STDCALL |
2340bcd3 JVH |
32 | #else |
33 | # include <stdint.h> | |
34 | # if !defined(EFI32) && !defined(EFI64) | |
427cd9fb | 35 | # define DNSSD_STDCALL __stdcall |
2340bcd3 | 36 | # else |
427cd9fb | 37 | # define DNSSD_STDCALL |
2340bcd3 JVH |
38 | # endif |
39 | ||
40 | typedef struct _DNSServiceRef_t *DNSServiceRef; | |
41 | typedef union _TXTRecordRef_t { char PrivateData[16]; char *ForceNaturalAlignment; } TXTRecordRef; | |
42 | ||
43 | typedef uint32_t DNSServiceFlags; | |
44 | typedef int32_t DNSServiceErrorType; | |
45 | ||
427cd9fb | 46 | typedef void (DNSSD_STDCALL *DNSServiceRegisterReply) |
2340bcd3 JVH |
47 | ( |
48 | DNSServiceRef sdRef, | |
49 | DNSServiceFlags flags, | |
50 | DNSServiceErrorType errorCode, | |
51 | const char *name, | |
52 | const char *regtype, | |
53 | const char *domain, | |
54 | void *context | |
55 | ); | |
56 | #endif | |
57 | ||
427cd9fb | 58 | typedef DNSServiceErrorType (DNSSD_STDCALL *DNSServiceRegister_t) |
2340bcd3 JVH |
59 | ( |
60 | DNSServiceRef *sdRef, | |
61 | DNSServiceFlags flags, | |
62 | uint32_t interfaceIndex, | |
63 | const char *name, | |
64 | const char *regtype, | |
65 | const char *domain, | |
66 | const char *host, | |
67 | uint16_t port, | |
68 | uint16_t txtLen, | |
69 | const void *txtRecord, | |
70 | DNSServiceRegisterReply callBack, | |
71 | void *context | |
72 | ); | |
427cd9fb JVH |
73 | typedef void (DNSSD_STDCALL *DNSServiceRefDeallocate_t)(DNSServiceRef sdRef); |
74 | typedef void (DNSSD_STDCALL *TXTRecordCreate_t) | |
2340bcd3 JVH |
75 | ( |
76 | TXTRecordRef *txtRecord, | |
77 | uint16_t bufferLen, | |
78 | void *buffer | |
79 | ); | |
427cd9fb JVH |
80 | typedef void (DNSSD_STDCALL *TXTRecordDeallocate_t)(TXTRecordRef *txtRecord); |
81 | typedef DNSServiceErrorType (DNSSD_STDCALL *TXTRecordSetValue_t) | |
2340bcd3 JVH |
82 | ( |
83 | TXTRecordRef *txtRecord, | |
84 | const char *key, | |
85 | uint8_t valueSize, | |
86 | const void *value | |
87 | ); | |
427cd9fb JVH |
88 | typedef uint16_t (DNSSD_STDCALL *TXTRecordGetLength_t)(const TXTRecordRef *txtRecord); |
89 | typedef const void * (DNSSD_STDCALL *TXTRecordGetBytesPtr_t)(const TXTRecordRef *txtRecord); | |
2340bcd3 JVH |
90 | |
91 | ||
92 | struct dnssd_s { | |
93 | #ifdef WIN32 | |
94 | HMODULE module; | |
95 | #endif | |
96 | ||
97 | DNSServiceRegister_t DNSServiceRegister; | |
98 | DNSServiceRefDeallocate_t DNSServiceRefDeallocate; | |
99 | TXTRecordCreate_t TXTRecordCreate; | |
100 | TXTRecordSetValue_t TXTRecordSetValue; | |
101 | TXTRecordGetLength_t TXTRecordGetLength; | |
102 | TXTRecordGetBytesPtr_t TXTRecordGetBytesPtr; | |
103 | TXTRecordDeallocate_t TXTRecordDeallocate; | |
104 | ||
2340bcd3 JVH |
105 | DNSServiceRef raopService; |
106 | DNSServiceRef airplayService; | |
107 | }; | |
108 | ||
109 | ||
110 | ||
111 | dnssd_t * | |
067f00ef | 112 | dnssd_init(int *error) |
2340bcd3 JVH |
113 | { |
114 | dnssd_t *dnssd; | |
115 | ||
116 | if (error) *error = DNSSD_ERROR_NOERROR; | |
2340bcd3 JVH |
117 | |
118 | dnssd = calloc(1, sizeof(dnssd_t)); | |
119 | if (!dnssd) { | |
120 | if (error) *error = DNSSD_ERROR_OUTOFMEM; | |
121 | return NULL; | |
122 | } | |
123 | ||
124 | #ifdef WIN32 | |
125 | dnssd->module = LoadLibraryA("dnssd.dll"); | |
126 | if (!dnssd->module) { | |
127 | if (error) *error = DNSSD_ERROR_LIBNOTFOUND; | |
128 | free(dnssd); | |
129 | return NULL; | |
130 | } | |
131 | dnssd->DNSServiceRegister = (DNSServiceRegister_t)GetProcAddress(dnssd->module, "DNSServiceRegister"); | |
132 | dnssd->DNSServiceRefDeallocate = (DNSServiceRefDeallocate_t)GetProcAddress(dnssd->module, "DNSServiceRefDeallocate"); | |
133 | dnssd->TXTRecordCreate = (TXTRecordCreate_t)GetProcAddress(dnssd->module, "TXTRecordCreate"); | |
134 | dnssd->TXTRecordSetValue = (TXTRecordSetValue_t)GetProcAddress(dnssd->module, "TXTRecordSetValue"); | |
135 | dnssd->TXTRecordGetLength = (TXTRecordGetLength_t)GetProcAddress(dnssd->module, "TXTRecordGetLength"); | |
136 | dnssd->TXTRecordGetBytesPtr = (TXTRecordGetBytesPtr_t)GetProcAddress(dnssd->module, "TXTRecordGetBytesPtr"); | |
137 | dnssd->TXTRecordDeallocate = (TXTRecordDeallocate_t)GetProcAddress(dnssd->module, "TXTRecordDeallocate"); | |
138 | ||
139 | if (!dnssd->DNSServiceRegister || !dnssd->DNSServiceRefDeallocate || !dnssd->TXTRecordCreate || | |
140 | !dnssd->TXTRecordSetValue || !dnssd->TXTRecordGetLength || !dnssd->TXTRecordGetBytesPtr || | |
141 | !dnssd->TXTRecordDeallocate) { | |
142 | if (error) *error = DNSSD_ERROR_PROCNOTFOUND; | |
143 | FreeLibrary(dnssd->module); | |
144 | free(dnssd); | |
145 | return NULL; | |
146 | } | |
147 | #else | |
148 | dnssd->DNSServiceRegister = &DNSServiceRegister; | |
149 | dnssd->DNSServiceRefDeallocate = &DNSServiceRefDeallocate; | |
150 | dnssd->TXTRecordCreate = &TXTRecordCreate; | |
151 | dnssd->TXTRecordSetValue = &TXTRecordSetValue; | |
152 | dnssd->TXTRecordGetLength = &TXTRecordGetLength; | |
153 | dnssd->TXTRecordGetBytesPtr = &TXTRecordGetBytesPtr; | |
154 | dnssd->TXTRecordDeallocate = &TXTRecordDeallocate; | |
155 | #endif | |
156 | ||
2340bcd3 JVH |
157 | return dnssd; |
158 | } | |
159 | ||
160 | void | |
161 | dnssd_destroy(dnssd_t *dnssd) | |
162 | { | |
163 | if (dnssd) { | |
164 | #ifdef WIN32 | |
165 | FreeLibrary(dnssd->module); | |
166 | #endif | |
167 | free(dnssd); | |
168 | } | |
169 | } | |
170 | ||
171 | int | |
067f00ef | 172 | dnssd_register_raop(dnssd_t *dnssd, const char *name, unsigned short port, const char *hwaddr, int hwaddrlen) |
2340bcd3 JVH |
173 | { |
174 | TXTRecordRef txtRecord; | |
175 | char servname[MAX_SERVNAME]; | |
176 | int ret; | |
177 | ||
178 | assert(dnssd); | |
067f00ef JVH |
179 | assert(name); |
180 | assert(hwaddr); | |
2340bcd3 JVH |
181 | |
182 | dnssd->TXTRecordCreate(&txtRecord, 0, NULL); | |
183 | dnssd->TXTRecordSetValue(&txtRecord, "txtvers", strlen(RAOP_TXTVERS), RAOP_TXTVERS); | |
184 | dnssd->TXTRecordSetValue(&txtRecord, "ch", strlen(RAOP_CH), RAOP_CH); | |
185 | dnssd->TXTRecordSetValue(&txtRecord, "cn", strlen(RAOP_CN), RAOP_CN); | |
186 | dnssd->TXTRecordSetValue(&txtRecord, "et", strlen(RAOP_ET), RAOP_ET); | |
187 | dnssd->TXTRecordSetValue(&txtRecord, "sv", strlen(RAOP_SV), RAOP_SV); | |
188 | dnssd->TXTRecordSetValue(&txtRecord, "da", strlen(RAOP_DA), RAOP_DA); | |
189 | dnssd->TXTRecordSetValue(&txtRecord, "sr", strlen(RAOP_SR), RAOP_SR); | |
190 | dnssd->TXTRecordSetValue(&txtRecord, "ss", strlen(RAOP_SS), RAOP_SS); | |
191 | dnssd->TXTRecordSetValue(&txtRecord, "pw", strlen(RAOP_PW), RAOP_PW); | |
192 | dnssd->TXTRecordSetValue(&txtRecord, "vn", strlen(RAOP_VN), RAOP_VN); | |
193 | dnssd->TXTRecordSetValue(&txtRecord, "tp", strlen(RAOP_TP), RAOP_TP); | |
194 | dnssd->TXTRecordSetValue(&txtRecord, "md", strlen(RAOP_MD), RAOP_MD); | |
195 | dnssd->TXTRecordSetValue(&txtRecord, "vs", strlen(GLOBAL_VERSION), GLOBAL_VERSION); | |
196 | dnssd->TXTRecordSetValue(&txtRecord, "am", strlen(RAOP_AM), RAOP_AM); | |
197 | dnssd->TXTRecordSetValue(&txtRecord, "sf", strlen(RAOP_SF), RAOP_SF); | |
198 | ||
199 | /* Convert hardware address to string */ | |
067f00ef | 200 | ret = utils_hwaddr_raop(servname, sizeof(servname), hwaddr, hwaddrlen); |
2340bcd3 JVH |
201 | if (ret < 0) { |
202 | /* FIXME: handle better */ | |
203 | return -1; | |
204 | } | |
205 | ||
206 | /* Check that we have bytes for 'hw@name' format */ | |
207 | if (sizeof(servname) < strlen(servname)+1+strlen(name)+1) { | |
208 | /* FIXME: handle better */ | |
209 | return -2; | |
210 | } | |
211 | ||
212 | strncat(servname, "@", sizeof(servname)-strlen(servname)-1); | |
213 | strncat(servname, name, sizeof(servname)-strlen(servname)-1); | |
214 | ||
215 | /* Register the service */ | |
216 | dnssd->DNSServiceRegister(&dnssd->raopService, 0, 0, | |
217 | servname, "_raop._tcp", | |
218 | NULL, NULL, | |
219 | htons(port), | |
220 | dnssd->TXTRecordGetLength(&txtRecord), | |
221 | dnssd->TXTRecordGetBytesPtr(&txtRecord), | |
222 | NULL, NULL); | |
223 | ||
224 | /* Deallocate TXT record */ | |
225 | dnssd->TXTRecordDeallocate(&txtRecord); | |
226 | return 1; | |
227 | } | |
228 | ||
229 | int | |
067f00ef | 230 | dnssd_register_airplay(dnssd_t *dnssd, const char *name, unsigned short port, const char *hwaddr, int hwaddrlen) |
2340bcd3 JVH |
231 | { |
232 | TXTRecordRef txtRecord; | |
233 | char deviceid[3*MAX_HWADDR_LEN]; | |
234 | char features[16]; | |
235 | int ret; | |
236 | ||
237 | assert(dnssd); | |
067f00ef JVH |
238 | assert(name); |
239 | assert(hwaddr); | |
2340bcd3 JVH |
240 | |
241 | /* Convert hardware address to string */ | |
067f00ef | 242 | ret = utils_hwaddr_airplay(deviceid, sizeof(deviceid), hwaddr, hwaddrlen); |
2340bcd3 JVH |
243 | if (ret < 0) { |
244 | /* FIXME: handle better */ | |
245 | return -1; | |
246 | } | |
247 | ||
248 | features[sizeof(features)-1] = '\0'; | |
249 | snprintf(features, sizeof(features)-1, "0x%x", GLOBAL_FEATURES); | |
250 | ||
251 | dnssd->TXTRecordCreate(&txtRecord, 0, NULL); | |
252 | dnssd->TXTRecordSetValue(&txtRecord, "deviceid", strlen(deviceid), deviceid); | |
253 | dnssd->TXTRecordSetValue(&txtRecord, "features", strlen(features), features); | |
254 | dnssd->TXTRecordSetValue(&txtRecord, "model", strlen(GLOBAL_MODEL), GLOBAL_MODEL); | |
255 | ||
256 | /* Register the service */ | |
257 | dnssd->DNSServiceRegister(&dnssd->airplayService, 0, 0, | |
258 | name, "_airplay._tcp", | |
259 | NULL, NULL, | |
260 | htons(port), | |
261 | dnssd->TXTRecordGetLength(&txtRecord), | |
262 | dnssd->TXTRecordGetBytesPtr(&txtRecord), | |
263 | NULL, NULL); | |
264 | ||
265 | /* Deallocate TXT record */ | |
266 | dnssd->TXTRecordDeallocate(&txtRecord); | |
267 | return 0; | |
268 | } | |
269 | ||
270 | void | |
271 | dnssd_unregister_raop(dnssd_t *dnssd) | |
272 | { | |
273 | assert(dnssd); | |
274 | ||
275 | if (!dnssd->raopService) { | |
276 | return; | |
277 | } | |
278 | ||
279 | dnssd->DNSServiceRefDeallocate(dnssd->raopService); | |
280 | dnssd->raopService = NULL; | |
281 | } | |
282 | ||
283 | void | |
284 | dnssd_unregister_airplay(dnssd_t *dnssd) | |
285 | { | |
286 | assert(dnssd); | |
287 | ||
288 | if (!dnssd->airplayService) { | |
289 | return; | |
290 | } | |
291 | ||
292 | dnssd->DNSServiceRefDeallocate(dnssd->airplayService); | |
293 | dnssd->airplayService = NULL; | |
294 | } |