buddy search: when ms-dlx query fails, retry using Active Directory
[siplcs.git] / src / purple / purple-dnsquery.c
blob5fe786df7890cb84ebbc19f9ad586e80fde496e9
1 /**
2 * @file purple-dnsquery.c
4 * pidgin-sipe
6 * Copyright (C) 2010 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 #include "win32/win32dep.h" /* for inet_ntop() */
25 #include <ws2tcpip.h>
26 #else
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
29 #endif
31 #include <glib.h>
33 #include "dnsquery.h"
34 #include "dnssrv.h"
35 #include "version.h"
37 #include "sipe-common.h"
38 #include "sipe-backend.h"
39 #include "sipe-core.h"
41 #include "purple-private.h"
43 struct sipe_dns_query {
44 enum {
46 SRV
47 } type;
48 sipe_dns_resolved_cb callback;
49 gpointer extradata;
50 gpointer purple_query_data;
53 static void dns_a_response(GSList *hosts,
54 struct sipe_dns_query *query,
55 const char *error_message)
57 char ipstr[INET6_ADDRSTRLEN];
58 struct sockaddr *addr;
59 const void *addrdata;
60 int port;
62 if (error_message || !g_slist_next(hosts)) {
63 query->callback(query->extradata, NULL, 0);
64 g_slist_free(hosts);
65 return;
68 addr = g_slist_next(hosts)->data;
69 if (addr->sa_family == AF_INET6) {
70 /* OS provides addr so it must be properly aligned */
71 struct sockaddr_in6 *sin6 = (void *) addr;
72 addrdata = &sin6->sin6_addr;
73 port = sin6->sin6_port;
74 } else {
75 /* OS provides addr so it must be properly aligned */
76 struct sockaddr_in *sin = (void *) addr;
77 addrdata = &sin->sin_addr;
78 port = sin->sin_port;
81 inet_ntop(addr->sa_family, addrdata, ipstr, sizeof (ipstr));
83 query->callback(query->extradata, ipstr, port);
85 for (; hosts; hosts = g_slist_delete_link(hosts, hosts)) {
86 // Free the addrlen, no data in this link
87 hosts = g_slist_delete_link(hosts, hosts);
88 // Free the address
89 g_free(hosts->data);
92 g_free(query);
95 struct sipe_dns_query *sipe_backend_dns_query_a(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
96 const gchar *hostname,
97 int port,
98 sipe_dns_resolved_cb callback,
99 gpointer data)
101 struct sipe_dns_query *query = g_new(struct sipe_dns_query, 1);
102 #if PURPLE_VERSION_CHECK(3,0,0)
103 struct sipe_backend_private *purple_private = sipe_public->backend_private;
104 #endif
106 query->type = A;
107 query->callback = callback;
108 query->extradata = data;
109 query->purple_query_data = purple_dnsquery_a(
110 #if PURPLE_VERSION_CHECK(3,0,0)
111 purple_private->account,
112 #endif
113 hostname,
114 port,
115 (PurpleDnsQueryConnectFunction) dns_a_response,
116 query);
118 return query;
122 static void dns_srv_response(PurpleSrvResponse *resp,
123 int results,
124 struct sipe_dns_query *query)
126 if (results)
127 query->callback(query->extradata, resp->hostname, resp->port);
128 else
129 query->callback(query->extradata, NULL, 0);
131 g_free(query);
132 g_free(resp);
135 struct sipe_dns_query *sipe_backend_dns_query_srv(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
136 const gchar *protocol,
137 const gchar *transport,
138 const gchar *domain,
139 sipe_dns_resolved_cb callback,
140 gpointer data)
142 struct sipe_dns_query *query = g_new(struct sipe_dns_query, 1);
143 #if PURPLE_VERSION_CHECK(3,0,0)
144 struct sipe_backend_private *purple_private = sipe_public->backend_private;
145 #endif
147 query->type = SRV;
148 query->callback = callback;
149 query->extradata = data;
150 query->purple_query_data = purple_srv_resolve(
151 #if PURPLE_VERSION_CHECK(3,0,0)
152 purple_private->account,
153 #endif
154 protocol,
155 transport,
156 domain,
157 (PurpleSrvCallback) dns_srv_response,
158 query);
160 return query;
163 void sipe_backend_dns_query_cancel(struct sipe_dns_query *query)
165 switch (query->type) {
166 case A:
167 purple_dnsquery_destroy(query->purple_query_data);
168 break;
169 case SRV:
170 #if PURPLE_VERSION_CHECK(2,8,0) || PURPLE_VERSION_CHECK(3,0,0)
171 purple_srv_txt_query_destroy(query->purple_query_data);
172 #else
173 purple_srv_cancel(query->purple_query_data);
174 #endif
175 break;
178 g_free(query);
182 Local Variables:
183 mode: c
184 c-file-style: "bsd"
185 indent-tabs-mode: t
186 tab-width: 8
187 End: