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.
30 #define MAX_DEVICEID 18
31 #define MAX_SERVNAME 256
33 #define USE_LIBDL (defined(HAVE_LIBDL) && !defined(__APPLE__))
35 #if defined(WIN32) || USE_LIBDL
38 # if !defined(EFI32) && !defined(EFI64)
39 # define DNSSD_STDCALL __stdcall
41 # define DNSSD_STDCALL
45 # define DNSSD_STDCALL
48 typedef struct _DNSServiceRef_t
*DNSServiceRef
;
49 typedef union _TXTRecordRef_t
{ char PrivateData
[16]; char *ForceNaturalAlignment
; } TXTRecordRef
;
51 typedef uint32_t DNSServiceFlags
;
52 typedef int32_t DNSServiceErrorType
;
54 typedef void (DNSSD_STDCALL
*DNSServiceRegisterReply
)
57 DNSServiceFlags flags
,
58 DNSServiceErrorType errorCode
,
67 # define DNSSD_STDCALL
70 typedef DNSServiceErrorType (DNSSD_STDCALL
*DNSServiceRegister_t
)
73 DNSServiceFlags flags
,
74 uint32_t interfaceIndex
,
81 const void *txtRecord
,
82 DNSServiceRegisterReply callBack
,
85 typedef void (DNSSD_STDCALL
*DNSServiceRefDeallocate_t
)(DNSServiceRef sdRef
);
86 typedef void (DNSSD_STDCALL
*TXTRecordCreate_t
)
88 TXTRecordRef
*txtRecord
,
92 typedef void (DNSSD_STDCALL
*TXTRecordDeallocate_t
)(TXTRecordRef
*txtRecord
);
93 typedef DNSServiceErrorType (DNSSD_STDCALL
*TXTRecordSetValue_t
)
95 TXTRecordRef
*txtRecord
,
100 typedef uint16_t (DNSSD_STDCALL
*TXTRecordGetLength_t
)(const TXTRecordRef
*txtRecord
);
101 typedef const void * (DNSSD_STDCALL
*TXTRecordGetBytesPtr_t
)(const TXTRecordRef
*txtRecord
);
111 DNSServiceRegister_t DNSServiceRegister
;
112 DNSServiceRefDeallocate_t DNSServiceRefDeallocate
;
113 TXTRecordCreate_t TXTRecordCreate
;
114 TXTRecordSetValue_t TXTRecordSetValue
;
115 TXTRecordGetLength_t TXTRecordGetLength
;
116 TXTRecordGetBytesPtr_t TXTRecordGetBytesPtr
;
117 TXTRecordDeallocate_t TXTRecordDeallocate
;
119 DNSServiceRef raopService
;
120 DNSServiceRef airplayService
;
126 dnssd_init(int *error
)
130 if (error
) *error
= DNSSD_ERROR_NOERROR
;
132 dnssd
= calloc(1, sizeof(dnssd_t
));
134 if (error
) *error
= DNSSD_ERROR_OUTOFMEM
;
139 dnssd
->module
= LoadLibraryA("dnssd.dll");
140 if (!dnssd
->module
) {
141 if (error
) *error
= DNSSD_ERROR_LIBNOTFOUND
;
145 dnssd
->DNSServiceRegister
= (DNSServiceRegister_t
)GetProcAddress(dnssd
->module
, "DNSServiceRegister");
146 dnssd
->DNSServiceRefDeallocate
= (DNSServiceRefDeallocate_t
)GetProcAddress(dnssd
->module
, "DNSServiceRefDeallocate");
147 dnssd
->TXTRecordCreate
= (TXTRecordCreate_t
)GetProcAddress(dnssd
->module
, "TXTRecordCreate");
148 dnssd
->TXTRecordSetValue
= (TXTRecordSetValue_t
)GetProcAddress(dnssd
->module
, "TXTRecordSetValue");
149 dnssd
->TXTRecordGetLength
= (TXTRecordGetLength_t
)GetProcAddress(dnssd
->module
, "TXTRecordGetLength");
150 dnssd
->TXTRecordGetBytesPtr
= (TXTRecordGetBytesPtr_t
)GetProcAddress(dnssd
->module
, "TXTRecordGetBytesPtr");
151 dnssd
->TXTRecordDeallocate
= (TXTRecordDeallocate_t
)GetProcAddress(dnssd
->module
, "TXTRecordDeallocate");
153 if (!dnssd
->DNSServiceRegister
|| !dnssd
->DNSServiceRefDeallocate
|| !dnssd
->TXTRecordCreate
||
154 !dnssd
->TXTRecordSetValue
|| !dnssd
->TXTRecordGetLength
|| !dnssd
->TXTRecordGetBytesPtr
||
155 !dnssd
->TXTRecordDeallocate
) {
156 if (error
) *error
= DNSSD_ERROR_PROCNOTFOUND
;
157 FreeLibrary(dnssd
->module
);
162 dnssd
->module
= dlopen("libdns_sd.so", RTLD_LAZY
);
163 if (!dnssd
->module
) {
164 if (error
) *error
= DNSSD_ERROR_LIBNOTFOUND
;
168 dnssd
->DNSServiceRegister
= (DNSServiceRegister_t
)dlsym(dnssd
->module
, "DNSServiceRegister");
169 dnssd
->DNSServiceRefDeallocate
= (DNSServiceRefDeallocate_t
)dlsym(dnssd
->module
, "DNSServiceRefDeallocate");
170 dnssd
->TXTRecordCreate
= (TXTRecordCreate_t
)dlsym(dnssd
->module
, "TXTRecordCreate");
171 dnssd
->TXTRecordSetValue
= (TXTRecordSetValue_t
)dlsym(dnssd
->module
, "TXTRecordSetValue");
172 dnssd
->TXTRecordGetLength
= (TXTRecordGetLength_t
)dlsym(dnssd
->module
, "TXTRecordGetLength");
173 dnssd
->TXTRecordGetBytesPtr
= (TXTRecordGetBytesPtr_t
)dlsym(dnssd
->module
, "TXTRecordGetBytesPtr");
174 dnssd
->TXTRecordDeallocate
= (TXTRecordDeallocate_t
)dlsym(dnssd
->module
, "TXTRecordDeallocate");
176 if (!dnssd
->DNSServiceRegister
|| !dnssd
->DNSServiceRefDeallocate
|| !dnssd
->TXTRecordCreate
||
177 !dnssd
->TXTRecordSetValue
|| !dnssd
->TXTRecordGetLength
|| !dnssd
->TXTRecordGetBytesPtr
||
178 !dnssd
->TXTRecordDeallocate
) {
179 if (error
) *error
= DNSSD_ERROR_PROCNOTFOUND
;
180 dlclose(dnssd
->module
);
185 dnssd
->DNSServiceRegister
= &DNSServiceRegister
;
186 dnssd
->DNSServiceRefDeallocate
= &DNSServiceRefDeallocate
;
187 dnssd
->TXTRecordCreate
= &TXTRecordCreate
;
188 dnssd
->TXTRecordSetValue
= &TXTRecordSetValue
;
189 dnssd
->TXTRecordGetLength
= &TXTRecordGetLength
;
190 dnssd
->TXTRecordGetBytesPtr
= &TXTRecordGetBytesPtr
;
191 dnssd
->TXTRecordDeallocate
= &TXTRecordDeallocate
;
198 dnssd_destroy(dnssd_t
*dnssd
)
202 FreeLibrary(dnssd
->module
);
204 dlclose(dnssd
->module
);
211 dnssd_register_raop(dnssd_t
*dnssd
, const char *name
, unsigned short port
, const char *hwaddr
, int hwaddrlen
, int password
)
213 TXTRecordRef txtRecord
;
214 char servname
[MAX_SERVNAME
];
221 dnssd
->TXTRecordCreate(&txtRecord
, 0, NULL
);
222 dnssd
->TXTRecordSetValue(&txtRecord
, "txtvers", strlen(RAOP_TXTVERS
), RAOP_TXTVERS
);
223 dnssd
->TXTRecordSetValue(&txtRecord
, "ch", strlen(RAOP_CH
), RAOP_CH
);
224 dnssd
->TXTRecordSetValue(&txtRecord
, "cn", strlen(RAOP_CN
), RAOP_CN
);
225 dnssd
->TXTRecordSetValue(&txtRecord
, "et", strlen(RAOP_ET
), RAOP_ET
);
226 dnssd
->TXTRecordSetValue(&txtRecord
, "sv", strlen(RAOP_SV
), RAOP_SV
);
227 dnssd
->TXTRecordSetValue(&txtRecord
, "da", strlen(RAOP_DA
), RAOP_DA
);
228 dnssd
->TXTRecordSetValue(&txtRecord
, "sr", strlen(RAOP_SR
), RAOP_SR
);
229 dnssd
->TXTRecordSetValue(&txtRecord
, "ss", strlen(RAOP_SS
), RAOP_SS
);
231 dnssd
->TXTRecordSetValue(&txtRecord
, "pw", strlen("true"), "true");
233 dnssd
->TXTRecordSetValue(&txtRecord
, "pw", strlen("false"), "false");
235 dnssd
->TXTRecordSetValue(&txtRecord
, "vn", strlen(RAOP_VN
), RAOP_VN
);
236 dnssd
->TXTRecordSetValue(&txtRecord
, "tp", strlen(RAOP_TP
), RAOP_TP
);
237 dnssd
->TXTRecordSetValue(&txtRecord
, "md", strlen(RAOP_MD
), RAOP_MD
);
238 dnssd
->TXTRecordSetValue(&txtRecord
, "vs", strlen(GLOBAL_VERSION
), GLOBAL_VERSION
);
239 dnssd
->TXTRecordSetValue(&txtRecord
, "sm", strlen(RAOP_SM
), RAOP_SM
);
240 dnssd
->TXTRecordSetValue(&txtRecord
, "ek", strlen(RAOP_EK
), RAOP_EK
);
242 /* Convert hardware address to string */
243 ret
= utils_hwaddr_raop(servname
, sizeof(servname
), hwaddr
, hwaddrlen
);
245 /* FIXME: handle better */
249 /* Check that we have bytes for 'hw@name' format */
250 if (sizeof(servname
) < strlen(servname
)+1+strlen(name
)+1) {
251 /* FIXME: handle better */
255 strncat(servname
, "@", sizeof(servname
)-strlen(servname
)-1);
256 strncat(servname
, name
, sizeof(servname
)-strlen(servname
)-1);
258 /* Register the service */
259 dnssd
->DNSServiceRegister(&dnssd
->raopService
, 0, 0,
260 servname
, "_raop._tcp",
263 dnssd
->TXTRecordGetLength(&txtRecord
),
264 dnssd
->TXTRecordGetBytesPtr(&txtRecord
),
267 /* Deallocate TXT record */
268 dnssd
->TXTRecordDeallocate(&txtRecord
);
273 dnssd_register_airplay(dnssd_t
*dnssd
, const char *name
, unsigned short port
, const char *hwaddr
, int hwaddrlen
)
275 TXTRecordRef txtRecord
;
276 char deviceid
[3*MAX_HWADDR_LEN
];
284 /* Convert hardware address to string */
285 ret
= utils_hwaddr_airplay(deviceid
, sizeof(deviceid
), hwaddr
, hwaddrlen
);
287 /* FIXME: handle better */
291 features
[sizeof(features
)-1] = '\0';
292 snprintf(features
, sizeof(features
)-1, "0x%x", GLOBAL_FEATURES
);
294 dnssd
->TXTRecordCreate(&txtRecord
, 0, NULL
);
295 dnssd
->TXTRecordSetValue(&txtRecord
, "deviceid", strlen(deviceid
), deviceid
);
296 dnssd
->TXTRecordSetValue(&txtRecord
, "features", strlen(features
), features
);
297 dnssd
->TXTRecordSetValue(&txtRecord
, "model", strlen(GLOBAL_MODEL
), GLOBAL_MODEL
);
299 /* Register the service */
300 dnssd
->DNSServiceRegister(&dnssd
->airplayService
, 0, 0,
301 name
, "_airplay._tcp",
304 dnssd
->TXTRecordGetLength(&txtRecord
),
305 dnssd
->TXTRecordGetBytesPtr(&txtRecord
),
308 /* Deallocate TXT record */
309 dnssd
->TXTRecordDeallocate(&txtRecord
);
314 dnssd_unregister_raop(dnssd_t
*dnssd
)
318 if (!dnssd
->raopService
) {
322 dnssd
->DNSServiceRefDeallocate(dnssd
->raopService
);
323 dnssd
->raopService
= NULL
;
327 dnssd_unregister_airplay(dnssd_t
*dnssd
)
331 if (!dnssd
->airplayService
) {
335 dnssd
->DNSServiceRefDeallocate(dnssd
->airplayService
);
336 dnssd
->airplayService
= NULL
;