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.
26 #define MAX_DEVICEID 18
27 #define MAX_SERVNAME 256
31 # define DNSSD_STDCALL
34 # if !defined(EFI32) && !defined(EFI64)
35 # define DNSSD_STDCALL __stdcall
37 # define DNSSD_STDCALL
40 typedef struct _DNSServiceRef_t
*DNSServiceRef
;
41 typedef union _TXTRecordRef_t
{ char PrivateData
[16]; char *ForceNaturalAlignment
; } TXTRecordRef
;
43 typedef uint32_t DNSServiceFlags
;
44 typedef int32_t DNSServiceErrorType
;
46 typedef void (DNSSD_STDCALL
*DNSServiceRegisterReply
)
49 DNSServiceFlags flags
,
50 DNSServiceErrorType errorCode
,
58 typedef DNSServiceErrorType (DNSSD_STDCALL
*DNSServiceRegister_t
)
61 DNSServiceFlags flags
,
62 uint32_t interfaceIndex
,
69 const void *txtRecord
,
70 DNSServiceRegisterReply callBack
,
73 typedef void (DNSSD_STDCALL
*DNSServiceRefDeallocate_t
)(DNSServiceRef sdRef
);
74 typedef void (DNSSD_STDCALL
*TXTRecordCreate_t
)
76 TXTRecordRef
*txtRecord
,
80 typedef void (DNSSD_STDCALL
*TXTRecordDeallocate_t
)(TXTRecordRef
*txtRecord
);
81 typedef DNSServiceErrorType (DNSSD_STDCALL
*TXTRecordSetValue_t
)
83 TXTRecordRef
*txtRecord
,
88 typedef uint16_t (DNSSD_STDCALL
*TXTRecordGetLength_t
)(const TXTRecordRef
*txtRecord
);
89 typedef const void * (DNSSD_STDCALL
*TXTRecordGetBytesPtr_t
)(const TXTRecordRef
*txtRecord
);
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
;
105 DNSServiceRef raopService
;
106 DNSServiceRef airplayService
;
112 dnssd_init(int *error
)
116 if (error
) *error
= DNSSD_ERROR_NOERROR
;
118 dnssd
= calloc(1, sizeof(dnssd_t
));
120 if (error
) *error
= DNSSD_ERROR_OUTOFMEM
;
125 dnssd
->module
= LoadLibraryA("dnssd.dll");
126 if (!dnssd
->module
) {
127 if (error
) *error
= DNSSD_ERROR_LIBNOTFOUND
;
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");
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
);
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
;
161 dnssd_destroy(dnssd_t
*dnssd
)
165 FreeLibrary(dnssd
->module
);
172 dnssd_register_raop(dnssd_t
*dnssd
, const char *name
, unsigned short port
, const char *hwaddr
, int hwaddrlen
)
174 TXTRecordRef txtRecord
;
175 char servname
[MAX_SERVNAME
];
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
);
199 /* Convert hardware address to string */
200 ret
= utils_hwaddr_raop(servname
, sizeof(servname
), hwaddr
, hwaddrlen
);
202 /* FIXME: handle better */
206 /* Check that we have bytes for 'hw@name' format */
207 if (sizeof(servname
) < strlen(servname
)+1+strlen(name
)+1) {
208 /* FIXME: handle better */
212 strncat(servname
, "@", sizeof(servname
)-strlen(servname
)-1);
213 strncat(servname
, name
, sizeof(servname
)-strlen(servname
)-1);
215 /* Register the service */
216 dnssd
->DNSServiceRegister(&dnssd
->raopService
, 0, 0,
217 servname
, "_raop._tcp",
220 dnssd
->TXTRecordGetLength(&txtRecord
),
221 dnssd
->TXTRecordGetBytesPtr(&txtRecord
),
224 /* Deallocate TXT record */
225 dnssd
->TXTRecordDeallocate(&txtRecord
);
230 dnssd_register_airplay(dnssd_t
*dnssd
, const char *name
, unsigned short port
, const char *hwaddr
, int hwaddrlen
)
232 TXTRecordRef txtRecord
;
233 char deviceid
[3*MAX_HWADDR_LEN
];
241 /* Convert hardware address to string */
242 ret
= utils_hwaddr_airplay(deviceid
, sizeof(deviceid
), hwaddr
, hwaddrlen
);
244 /* FIXME: handle better */
248 features
[sizeof(features
)-1] = '\0';
249 snprintf(features
, sizeof(features
)-1, "0x%x", GLOBAL_FEATURES
);
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
);
256 /* Register the service */
257 dnssd
->DNSServiceRegister(&dnssd
->airplayService
, 0, 0,
258 name
, "_airplay._tcp",
261 dnssd
->TXTRecordGetLength(&txtRecord
),
262 dnssd
->TXTRecordGetBytesPtr(&txtRecord
),
265 /* Deallocate TXT record */
266 dnssd
->TXTRecordDeallocate(&txtRecord
);
271 dnssd_unregister_raop(dnssd_t
*dnssd
)
275 if (!dnssd
->raopService
) {
279 dnssd
->DNSServiceRefDeallocate(dnssd
->raopService
);
280 dnssd
->raopService
= NULL
;
284 dnssd_unregister_airplay(dnssd_t
*dnssd
)
288 if (!dnssd
->airplayService
) {
292 dnssd
->DNSServiceRefDeallocate(dnssd
->airplayService
);
293 dnssd
->airplayService
= NULL
;