Merge branch 'mob' of git+ssh://repo.or.cz/srv/git/siplcs into mob
[siplcs.git] / src / purple / purple-dnsquery.c
blob9dba69fdb765f1f1c556364ccad2552664cf7941
1 /**
2 * @file purple-dnsquery.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef _WIN32
24 /* wrappers for write() & friends for socket handling */
25 #include "win32/win32dep.h"
26 #include <ws2tcpip.h>
27 #else
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #endif
34 #include <glib.h>
36 #include "dnsquery.h"
37 #include "dnssrv.h"
39 #include "sipe-common.h"
40 #include "sipe-backend.h"
41 #include "sipe-core.h"
43 #include "purple-private.h"
45 struct sipe_dns_query {
46 enum {
48 SRV
49 } type;
50 struct sipe_backend_private *purple_private;
51 sipe_dns_resolved_cb callback;
52 gpointer extradata;
53 gpointer purple_query_data;
54 gboolean is_valid;
57 static void dns_a_response(GSList *hosts,
58 struct sipe_dns_query *query,
59 const char *error_message)
61 char ipstr[INET6_ADDRSTRLEN];
62 struct sockaddr *addr;
63 const void *addrdata;
64 int port;
66 /* Ignore spurious responses after disconnect */
67 if (query->is_valid) {
68 struct sipe_backend_private *purple_private = query->purple_private;
70 purple_private->dns_queries = g_slist_remove(purple_private->dns_queries,
71 query);
73 if (error_message || !g_slist_next(hosts)) {
74 query->callback(query->extradata, NULL, 0);
75 g_slist_free(hosts);
76 return;
79 addr = g_slist_next(hosts)->data;
80 if (addr->sa_family == AF_INET6) {
81 /* OS provides addr so it must be properly aligned */
82 struct sockaddr_in6 *sin6 = (void *) addr;
83 addrdata = &sin6->sin6_addr;
84 port = sin6->sin6_port;
85 } else {
86 /* OS provides addr so it must be properly aligned */
87 struct sockaddr_in *sin = (void *) addr;
88 addrdata = &sin->sin_addr;
89 port = sin->sin_port;
92 inet_ntop(addr->sa_family, addrdata, ipstr, sizeof (ipstr));
94 query->callback(query->extradata, ipstr, port);
96 g_free(query);
99 for (; hosts; hosts = g_slist_delete_link(hosts, hosts)) {
100 // Free the addrlen, no data in this link
101 hosts = g_slist_delete_link(hosts, hosts);
102 // Free the address
103 g_free(hosts->data);
107 struct sipe_dns_query *sipe_backend_dns_query_a(struct sipe_core_public *sipe_public,
108 const gchar *hostname,
109 guint port,
110 sipe_dns_resolved_cb callback,
111 gpointer data)
113 struct sipe_dns_query *query = g_new(struct sipe_dns_query, 1);
114 struct sipe_backend_private *purple_private = sipe_public->backend_private;
116 query->type = A;
117 query->purple_private = purple_private;
118 query->callback = callback;
119 query->extradata = data;
120 query->is_valid = TRUE;
122 purple_private->dns_queries = g_slist_prepend(purple_private->dns_queries,
123 query);
125 query->purple_query_data =
126 #if PURPLE_VERSION_CHECK(3,0,0)
127 purple_dnsquery_a(
128 purple_private->account,
129 #elif PURPLE_VERSION_CHECK(2,8,0)
130 purple_dnsquery_a_account(
131 purple_private->account,
132 #else
133 purple_dnsquery_a(
134 #endif
135 hostname,
136 port,
137 (PurpleDnsQueryConnectFunction) dns_a_response,
138 query);
140 return query;
144 static void dns_srv_response(PurpleSrvResponse *resp,
145 int results,
146 struct sipe_dns_query *query)
148 /* Ignore spurious responses after disconnect */
149 if (query->is_valid) {
150 struct sipe_backend_private *purple_private = query->purple_private;
152 purple_private->dns_queries = g_slist_remove(purple_private->dns_queries,
153 query);
155 if (results)
156 query->callback(query->extradata, resp->hostname, resp->port);
157 else
158 query->callback(query->extradata, NULL, 0);
160 g_free(query);
163 g_free(resp);
166 struct sipe_dns_query *sipe_backend_dns_query_srv(struct sipe_core_public *sipe_public,
167 const gchar *protocol,
168 const gchar *transport,
169 const gchar *domain,
170 sipe_dns_resolved_cb callback,
171 gpointer data)
173 struct sipe_dns_query *query = g_new(struct sipe_dns_query, 1);
174 struct sipe_backend_private *purple_private = sipe_public->backend_private;
176 query->type = SRV;
177 query->purple_private = purple_private;
178 query->callback = callback;
179 query->extradata = data;
180 query->is_valid = TRUE;
182 purple_private->dns_queries = g_slist_prepend(purple_private->dns_queries,
183 query);
185 query->purple_query_data =
186 #if PURPLE_VERSION_CHECK(3,0,0)
187 purple_srv_resolve(
188 purple_private->account,
189 #elif PURPLE_VERSION_CHECK(2,8,0)
190 purple_srv_resolve_account(
191 purple_private->account,
192 #else
193 purple_srv_resolve(
194 #endif
195 protocol,
196 transport,
197 domain,
198 (PurpleSrvCallback) dns_srv_response,
199 query);
201 return query;
204 static gboolean dns_query_deferred_destroy(gpointer user_data)
207 * All pending events on query have been processed.
208 * Now it is safe to destroy the data structure.
210 SIPE_DEBUG_INFO("dns_query_deferred_destroy: %p", user_data);
211 g_free(user_data);
212 return(FALSE);
215 void sipe_backend_dns_query_cancel(struct sipe_dns_query *query)
217 SIPE_DEBUG_INFO("sipe_backend_dns_query_cancel: %p", query);
219 if (query->is_valid) {
220 struct sipe_backend_private *purple_private = query->purple_private;
221 purple_private->dns_queries = g_slist_remove(purple_private->dns_queries,
222 query);
224 switch (query->type) {
225 case A:
226 purple_dnsquery_destroy(query->purple_query_data);
227 break;
228 case SRV:
229 #if PURPLE_VERSION_CHECK(2,8,0) || PURPLE_VERSION_CHECK(3,0,0)
230 purple_srv_txt_query_destroy(query->purple_query_data);
231 #else
232 purple_srv_cancel(query->purple_query_data);
233 #endif
234 break;
237 /* defer deletion of query data structure to idle callback */
238 query->is_valid = FALSE;
239 g_idle_add(dns_query_deferred_destroy, query);
243 void sipe_purple_dns_query_cancel_all(struct sipe_backend_private *purple_private)
245 GSList *entry;
246 SIPE_DEBUG_INFO_NOFORMAT("sipe_purple_dns_query_cancel_all: entered");
247 while ((entry = purple_private->dns_queries) != NULL)
248 sipe_backend_dns_query_cancel(entry->data);
253 Local Variables:
254 mode: c
255 c-file-style: "bsd"
256 indent-tabs-mode: t
257 tab-width: 8
258 End: