i18n: update bug tracker URL in script
[siplcs.git] / src / purple / purple-dnsquery.c
blob30a728590235d120abf17c15e051887a416a0d38
1 /**
2 * @file purple-dnsquery.c
4 * pidgin-sipe
6 * Copyright (C) 2010-12 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"
38 #include "version.h"
40 #include "sipe-common.h"
41 #include "sipe-backend.h"
42 #include "sipe-core.h"
44 #include "purple-private.h"
46 struct sipe_dns_query {
47 enum {
49 SRV
50 } type;
51 sipe_dns_resolved_cb callback;
52 gpointer extradata;
53 gpointer purple_query_data;
56 static void dns_a_response(GSList *hosts,
57 struct sipe_dns_query *query,
58 const char *error_message)
60 char ipstr[INET6_ADDRSTRLEN];
61 struct sockaddr *addr;
62 const void *addrdata;
63 int port;
65 if (error_message || !g_slist_next(hosts)) {
66 query->callback(query->extradata, NULL, 0);
67 g_slist_free(hosts);
68 return;
71 addr = g_slist_next(hosts)->data;
72 if (addr->sa_family == AF_INET6) {
73 /* OS provides addr so it must be properly aligned */
74 struct sockaddr_in6 *sin6 = (void *) addr;
75 addrdata = &sin6->sin6_addr;
76 port = sin6->sin6_port;
77 } else {
78 /* OS provides addr so it must be properly aligned */
79 struct sockaddr_in *sin = (void *) addr;
80 addrdata = &sin->sin_addr;
81 port = sin->sin_port;
84 inet_ntop(addr->sa_family, addrdata, ipstr, sizeof (ipstr));
86 query->callback(query->extradata, ipstr, port);
88 for (; hosts; hosts = g_slist_delete_link(hosts, hosts)) {
89 // Free the addrlen, no data in this link
90 hosts = g_slist_delete_link(hosts, hosts);
91 // Free the address
92 g_free(hosts->data);
95 g_free(query);
98 struct sipe_dns_query *sipe_backend_dns_query_a(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
99 const gchar *hostname,
100 guint port,
101 sipe_dns_resolved_cb callback,
102 gpointer data)
104 struct sipe_dns_query *query = g_new(struct sipe_dns_query, 1);
105 #if PURPLE_VERSION_CHECK(2,8,0) || PURPLE_VERSION_CHECK(3,0,0)
106 struct sipe_backend_private *purple_private = sipe_public->backend_private;
107 #endif
109 query->type = A;
110 query->callback = callback;
111 query->extradata = data;
112 query->purple_query_data =
113 #if PURPLE_VERSION_CHECK(3,0,0)
114 purple_dnsquery_a(
115 purple_private->account,
116 #elif PURPLE_VERSION_CHECK(2,8,0)
117 purple_dnsquery_a_account(
118 purple_private->account,
119 #else
120 purple_dnsquery_a(
121 #endif
122 hostname,
123 port,
124 (PurpleDnsQueryConnectFunction) dns_a_response,
125 query);
127 return query;
131 static void dns_srv_response(PurpleSrvResponse *resp,
132 int results,
133 struct sipe_dns_query *query)
135 if (results)
136 query->callback(query->extradata, resp->hostname, resp->port);
137 else
138 query->callback(query->extradata, NULL, 0);
140 g_free(query);
141 g_free(resp);
144 struct sipe_dns_query *sipe_backend_dns_query_srv(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
145 const gchar *protocol,
146 const gchar *transport,
147 const gchar *domain,
148 sipe_dns_resolved_cb callback,
149 gpointer data)
151 struct sipe_dns_query *query = g_new(struct sipe_dns_query, 1);
152 #if PURPLE_VERSION_CHECK(2,8,0) || PURPLE_VERSION_CHECK(3,0,0)
153 struct sipe_backend_private *purple_private = sipe_public->backend_private;
154 #endif
156 query->type = SRV;
157 query->callback = callback;
158 query->extradata = data;
159 query->purple_query_data =
160 #if PURPLE_VERSION_CHECK(3,0,0)
161 purple_srv_resolve(
162 purple_private->account,
163 #elif PURPLE_VERSION_CHECK(2,8,0)
164 purple_srv_resolve_account(
165 purple_private->account,
166 #else
167 purple_srv_resolve(
168 #endif
169 protocol,
170 transport,
171 domain,
172 (PurpleSrvCallback) dns_srv_response,
173 query);
175 return query;
178 void sipe_backend_dns_query_cancel(struct sipe_dns_query *query)
180 switch (query->type) {
181 case A:
182 purple_dnsquery_destroy(query->purple_query_data);
183 break;
184 case SRV:
185 #if PURPLE_VERSION_CHECK(2,8,0) || PURPLE_VERSION_CHECK(3,0,0)
186 purple_srv_txt_query_destroy(query->purple_query_data);
187 #else
188 purple_srv_cancel(query->purple_query_data);
189 #endif
190 break;
193 g_free(query);
197 Local Variables:
198 mode: c
199 c-file-style: "bsd"
200 indent-tabs-mode: t
201 tab-width: 8
202 End: