libpurple/protocols/bonjour/dns_sd_proxy.c

changeset 42670
a3b862b8dcde
parent 42669
6d41b29637ef
child 42671
68cc8544b438
equal deleted inserted replaced
42669:6d41b29637ef 42670:a3b862b8dcde
1 /*
2 * Purple is the legal property of its developers, whose names are too numerous
3 * to list here. Please refer to the COPYRIGHT file distributed with this
4 * source distribution.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301, USA.
19 */
20
21 #include <gmodule.h>
22
23 #include <purple.h>
24
25 #include "dns_sd_proxy.h"
26
27 static DNSServiceErrorType (DNSSD_API* _DNSServiceAddRecord)(DNSServiceRef sdRef, DNSRecordRef *RecordRef, DNSServiceFlags flags,
28 uint16_t rrtype, uint16_t rdlen, const void *rdata, uint32_t ttl);
29 static DNSServiceErrorType (DNSSD_API* _DNSServiceBrowse)(DNSServiceRef *sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
30 const char *regtype, const char *domain, DNSServiceBrowseReply callBack, void *context);
31 static int (DNSSD_API* _DNSServiceConstructFullName)(char *fullName, const char *service, const char *regtype, const char *domain);
32 static DNSServiceErrorType (DNSSD_API* _DNSServiceGetAddrInfo)(DNSServiceRef *sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
33 DNSServiceProtocol protocol, const char *hostname, DNSServiceGetAddrInfoReply callBack, void *context);
34 static DNSServiceErrorType (DNSSD_API* _DNSServiceProcessResult)(DNSServiceRef sdRef);
35 static DNSServiceErrorType (DNSSD_API* _DNSServiceQueryRecord)(DNSServiceRef *sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
36 const char *fullname, uint16_t rrtype, uint16_t rrclass, DNSServiceQueryRecordReply callBack, void *context);
37 static void (DNSSD_API* _DNSServiceRefDeallocate)(DNSServiceRef sdRef);
38 static int (DNSSD_API* _DNSServiceRefSockFD)(DNSServiceRef sdRef);
39 static DNSServiceErrorType (DNSSD_API* _DNSServiceRegister)(DNSServiceRef *sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
40 const char *name, const char *regtype, const char *domain, const char *host, uint16_t port, uint16_t txtLen,
41 const void *txtRecord, DNSServiceRegisterReply callBack, void *context);
42 static DNSServiceErrorType (DNSSD_API* _DNSServiceResolve)(DNSServiceRef *sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, const char *name,
43 const char *regtype, const char *domain, DNSServiceResolveReply callBack, void *context);
44 static DNSServiceErrorType (DNSSD_API* _DNSServiceRemoveRecord)(DNSServiceRef sdRef, DNSRecordRef RecordRef, DNSServiceFlags flags);
45 static DNSServiceErrorType (DNSSD_API* _DNSServiceUpdateRecord)(DNSServiceRef sdRef, DNSRecordRef RecordRef, DNSServiceFlags flags,
46 uint16_t rdlen, const void *rdata, uint32_t ttl);
47 static void (DNSSD_API* _TXTRecordCreate)(TXTRecordRef *txtRecord, uint16_t bufferLen, void *buffer);
48 static void (DNSSD_API* _TXTRecordDeallocate)(TXTRecordRef *txtRecord);
49 static const void * (DNSSD_API* _TXTRecordGetBytesPtr)(const TXTRecordRef *txtRecord);
50 static int16_t (DNSSD_API* _TXTRecordGetLength)(const TXTRecordRef *txtRecord);
51 static const void * (DNSSD_API* _TXTRecordGetValuePtr)(uint16_t txtLen, const void *txtRecord, const char *key, uint8_t *valueLen);
52 static DNSServiceErrorType (DNSSD_API* _TXTRecordSetValue)(TXTRecordRef *txtRecord, const char *key, uint8_t valueSize, const void *value);
53
54
55 gboolean dns_sd_available(void) {
56 static gboolean initialized = FALSE;
57 static gboolean loaded = FALSE;
58
59 if (!initialized) {
60 GModule *dnssd = NULL;
61 initialized = TRUE;
62 if((dnssd = g_module_open("dnssd.dll", 0)) != NULL &&
63 g_module_symbol(dnssd, "DNSServiceAddRecord", (gpointer *)&_DNSServiceAddRecord) &&
64 g_module_symbol(dnssd, "DNSServiceBrowse", (gpointer *)&_DNSServiceBrowse) &&
65 g_module_symbol(dnssd, "DNSServiceConstructFullName", (gpointer *)&_DNSServiceConstructFullName) &&
66 g_module_symbol(dnssd, "DNSServiceGetAddrInfo", (gpointer *)&_DNSServiceGetAddrInfo) &&
67 g_module_symbol(dnssd, "DNSServiceProcessResult", (gpointer *)&_DNSServiceProcessResult) &&
68 g_module_symbol(dnssd, "DNSServiceQueryRecord", (gpointer *)&_DNSServiceQueryRecord) &&
69 g_module_symbol(dnssd, "DNSServiceRefDeallocate", (gpointer *)&_DNSServiceRefDeallocate) &&
70 g_module_symbol(dnssd, "DNSServiceRefSockFD", (gpointer *)&_DNSServiceRefSockFD) &&
71 g_module_symbol(dnssd, "DNSServiceRegister", (gpointer *)&_DNSServiceRegister) &&
72 g_module_symbol(dnssd, "DNSServiceResolve", (gpointer *)&_DNSServiceResolve) &&
73 g_module_symbol(dnssd, "DNSServiceRemoveRecord", (gpointer *)&_DNSServiceRemoveRecord) &&
74 g_module_symbol(dnssd, "DNSServiceUpdateRecord", (gpointer *)&_DNSServiceUpdateRecord) &&
75 g_module_symbol(dnssd, "TXTRecordCreate", (gpointer *)&_TXTRecordCreate) &&
76 g_module_symbol(dnssd, "TXTRecordDeallocate", (gpointer *)&_TXTRecordDeallocate) &&
77 g_module_symbol(dnssd, "TXTRecordGetBytesPtr", (gpointer *)&_TXTRecordGetBytesPtr) &&
78 g_module_symbol(dnssd, "TXTRecordGetLength", (gpointer *)&_TXTRecordGetLength) &&
79 g_module_symbol(dnssd, "TXTRecordGetValuePtr", (gpointer *)&_TXTRecordGetValuePtr) &&
80 g_module_symbol(dnssd, "TXTRecordSetValue", (gpointer *)&_TXTRecordSetValue))
81 {
82 /* TODO: The dnssd module leaks, but there's not a good
83 * place to put the cleanup yet, as there's quite a bit
84 * of abstraction between mDNS API here to the actual
85 * plugin unload. */
86 loaded = TRUE;
87 } else {
88 g_clear_pointer(&dnssd, g_module_close);
89 }
90 }
91 return loaded;
92 }
93
94
95 DNSServiceErrorType _wpurple_DNSServiceAddRecord(DNSServiceRef sdRef, DNSRecordRef *RecordRef, DNSServiceFlags flags,
96 uint16_t rrtype, uint16_t rdlen, const void *rdata, uint32_t ttl) {
97 g_return_val_if_fail(_DNSServiceAddRecord != NULL, kDNSServiceErr_Unknown);
98 return (_DNSServiceAddRecord)(sdRef, RecordRef, flags, rrtype, rdlen, rdata, ttl);
99 }
100
101 DNSServiceErrorType _wpurple_DNSServiceBrowse(DNSServiceRef *sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
102 const char *regtype, const char *domain, DNSServiceBrowseReply callBack, void *context) {
103 g_return_val_if_fail(_DNSServiceBrowse != NULL, kDNSServiceErr_Unknown);
104 return (_DNSServiceBrowse)(sdRef, flags, interfaceIndex, regtype, domain, callBack, context);
105 }
106
107 int _wpurple_DNSServiceConstructFullName(char *fullName, const char *service, const char *regtype, const char *domain) {
108 g_return_val_if_fail(_DNSServiceConstructFullName != NULL, 0);
109 return (_DNSServiceConstructFullName)(fullName, service, regtype, domain);
110 }
111
112 DNSServiceErrorType _wpurple_DNSServiceGetAddrInfo(DNSServiceRef *sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
113 DNSServiceProtocol protocol, const char *hostname, DNSServiceGetAddrInfoReply callBack, void *context) {
114 g_return_val_if_fail(_DNSServiceGetAddrInfo != NULL, 0);
115 return (_DNSServiceGetAddrInfo)(sdRef, flags, interfaceIndex, protocol, hostname, callBack, context);
116 }
117
118 DNSServiceErrorType _wpurple_DNSServiceProcessResult(DNSServiceRef sdRef) {
119 g_return_val_if_fail(_DNSServiceProcessResult != NULL, kDNSServiceErr_Unknown);
120 return (_DNSServiceProcessResult)(sdRef);
121 }
122
123
124 DNSServiceErrorType _wpurple_DNSServiceQueryRecord(DNSServiceRef *sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
125 const char *fullname, uint16_t rrtype, uint16_t rrclass, DNSServiceQueryRecordReply callBack, void *context) {
126 g_return_val_if_fail(_DNSServiceQueryRecord != NULL, kDNSServiceErr_Unknown);
127 return (_DNSServiceQueryRecord)(sdRef, flags, interfaceIndex, fullname, rrtype, rrclass, callBack, context);
128 }
129
130 void _wpurple_DNSServiceRefDeallocate(DNSServiceRef sdRef) {
131 g_return_if_fail(_DNSServiceRefDeallocate != NULL);
132 (_DNSServiceRefDeallocate)(sdRef);
133 }
134
135 int _wpurple_DNSServiceRefSockFD(DNSServiceRef sdRef) {
136 g_return_val_if_fail(_DNSServiceRefSockFD != NULL, -1);
137 return (_DNSServiceRefSockFD)(sdRef);
138 }
139
140 DNSServiceErrorType _wpurple_DNSServiceRegister(DNSServiceRef *sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
141 const char *name, const char *regtype, const char *domain, const char *host, uint16_t port, uint16_t txtLen,
142 const void *txtRecord, DNSServiceRegisterReply callBack, void *context) {
143 g_return_val_if_fail(_DNSServiceRegister != NULL, kDNSServiceErr_Unknown);
144 return (_DNSServiceRegister)(sdRef, flags, interfaceIndex, name, regtype, domain, host, port, txtLen, txtRecord, callBack, context);
145 }
146
147 DNSServiceErrorType _wpurple_DNSServiceResolve(DNSServiceRef *sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, const char *name,
148 const char *regtype, const char *domain, DNSServiceResolveReply callBack, void *context) {
149 g_return_val_if_fail(_DNSServiceResolve != NULL, kDNSServiceErr_Unknown);
150 return (_DNSServiceResolve)(sdRef, flags, interfaceIndex, name, regtype, domain, callBack, context);
151 }
152
153 DNSServiceErrorType _wpurple_DNSServiceRemoveRecord(DNSServiceRef sdRef, DNSRecordRef RecordRef, DNSServiceFlags flags) {
154 g_return_val_if_fail(_DNSServiceRemoveRecord != NULL, kDNSServiceErr_Unknown);
155 return (_DNSServiceRemoveRecord)(sdRef, RecordRef, flags);
156 }
157
158 DNSServiceErrorType _wpurple_DNSServiceUpdateRecord(DNSServiceRef sdRef, DNSRecordRef RecordRef, DNSServiceFlags flags,
159 uint16_t rdlen, const void *rdata, uint32_t ttl) {
160 g_return_val_if_fail(_DNSServiceUpdateRecord != NULL, kDNSServiceErr_Unknown);
161 return (_DNSServiceUpdateRecord)(sdRef, RecordRef, flags, rdlen, rdata, ttl);
162 }
163
164 void _wpurple_TXTRecordCreate(TXTRecordRef *txtRecord, uint16_t bufferLen, void *buffer) {
165 g_return_if_fail(_TXTRecordCreate != NULL);
166 (_TXTRecordCreate)(txtRecord, bufferLen, buffer);
167 }
168
169 void _wpurple_TXTRecordDeallocate(TXTRecordRef *txtRecord) {
170 g_return_if_fail(_TXTRecordDeallocate != NULL);
171 (_TXTRecordDeallocate)(txtRecord);
172 }
173
174 const void * _wpurple_TXTRecordGetBytesPtr(const TXTRecordRef *txtRecord) {
175 g_return_val_if_fail(_TXTRecordGetBytesPtr != NULL, NULL);
176 return (_TXTRecordGetBytesPtr)(txtRecord);
177 }
178
179 uint16_t _wpurple_TXTRecordGetLength(const TXTRecordRef *txtRecord) {
180 g_return_val_if_fail(_TXTRecordGetLength != NULL, 0);
181 return (_TXTRecordGetLength)(txtRecord);
182 }
183
184 const void * _wpurple_TXTRecordGetValuePtr(uint16_t txtLen, const void *txtRecord, const char *key, uint8_t *valueLen) {
185 g_return_val_if_fail(_TXTRecordGetValuePtr != NULL, NULL);
186 return (_TXTRecordGetValuePtr)(txtLen, txtRecord, key, valueLen);
187 }
188
189 DNSServiceErrorType _wpurple_TXTRecordSetValue(TXTRecordRef *txtRecord, const char *key, uint8_t valueSize, const void *value) {
190 g_return_val_if_fail(_TXTRecordSetValue != NULL, kDNSServiceErr_Unknown);
191 return (_TXTRecordSetValue)(txtRecord, key, valueSize, value);
192 }
193

mercurial