purple: build fix for *BSD
[siplcs.git] / src / purple / purple-dnsquery.c
blob0a37c418885a081c741b14c7d0516bbc90264c2d
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 #include "win32/win32dep.h" /* for inet_ntop() */
25 #include <ws2tcpip.h>
26 #else
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
29 #include <sys/socket.h>
30 #endif
32 #include <glib.h>
34 #include "dnsquery.h"
35 #include "dnssrv.h"
36 #include "version.h"
38 #include "sipe-common.h"
39 #include "sipe-backend.h"
40 #include "sipe-core.h"
42 #include "purple-private.h"
44 struct sipe_dns_query {
45 enum {
47 SRV
48 } type;
49 sipe_dns_resolved_cb callback;
50 gpointer extradata;
51 gpointer purple_query_data;
54 static void dns_a_response(GSList *hosts,
55 struct sipe_dns_query *query,
56 const char *error_message)
58 char ipstr[INET6_ADDRSTRLEN];
59 struct sockaddr *addr;
60 const void *addrdata;
61 int port;
63 if (error_message || !g_slist_next(hosts)) {
64 query->callback(query->extradata, NULL, 0);
65 g_slist_free(hosts);
66 return;
69 addr = g_slist_next(hosts)->data;
70 if (addr->sa_family == AF_INET6) {
71 /* OS provides addr so it must be properly aligned */
72 struct sockaddr_in6 *sin6 = (void *) addr;
73 addrdata = &sin6->sin6_addr;
74 port = sin6->sin6_port;
75 } else {
76 /* OS provides addr so it must be properly aligned */
77 struct sockaddr_in *sin = (void *) addr;
78 addrdata = &sin->sin_addr;
79 port = sin->sin_port;
82 inet_ntop(addr->sa_family, addrdata, ipstr, sizeof (ipstr));
84 query->callback(query->extradata, ipstr, port);
86 for (; hosts; hosts = g_slist_delete_link(hosts, hosts)) {
87 // Free the addrlen, no data in this link
88 hosts = g_slist_delete_link(hosts, hosts);
89 // Free the address
90 g_free(hosts->data);
93 g_free(query);
96 struct sipe_dns_query *sipe_backend_dns_query_a(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
97 const gchar *hostname,
98 int port,
99 sipe_dns_resolved_cb callback,
100 gpointer data)
102 struct sipe_dns_query *query = g_new(struct sipe_dns_query, 1);
103 #if PURPLE_VERSION_CHECK(3,0,0)
104 struct sipe_backend_private *purple_private = sipe_public->backend_private;
105 #endif
107 query->type = A;
108 query->callback = callback;
109 query->extradata = data;
110 query->purple_query_data = purple_dnsquery_a(
111 #if PURPLE_VERSION_CHECK(3,0,0)
112 purple_private->account,
113 #endif
114 hostname,
115 port,
116 (PurpleDnsQueryConnectFunction) dns_a_response,
117 query);
119 return query;
123 static void dns_srv_response(PurpleSrvResponse *resp,
124 int results,
125 struct sipe_dns_query *query)
127 if (results)
128 query->callback(query->extradata, resp->hostname, resp->port);
129 else
130 query->callback(query->extradata, NULL, 0);
132 g_free(query);
133 g_free(resp);
136 struct sipe_dns_query *sipe_backend_dns_query_srv(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
137 const gchar *protocol,
138 const gchar *transport,
139 const gchar *domain,
140 sipe_dns_resolved_cb callback,
141 gpointer data)
143 struct sipe_dns_query *query = g_new(struct sipe_dns_query, 1);
144 #if PURPLE_VERSION_CHECK(3,0,0)
145 struct sipe_backend_private *purple_private = sipe_public->backend_private;
146 #endif
148 query->type = SRV;
149 query->callback = callback;
150 query->extradata = data;
151 query->purple_query_data = purple_srv_resolve(
152 #if PURPLE_VERSION_CHECK(3,0,0)
153 purple_private->account,
154 #endif
155 protocol,
156 transport,
157 domain,
158 (PurpleSrvCallback) dns_srv_response,
159 query);
161 return query;
164 void sipe_backend_dns_query_cancel(struct sipe_dns_query *query)
166 switch (query->type) {
167 case A:
168 purple_dnsquery_destroy(query->purple_query_data);
169 break;
170 case SRV:
171 #if PURPLE_VERSION_CHECK(2,8,0) || PURPLE_VERSION_CHECK(3,0,0)
172 purple_srv_txt_query_destroy(query->purple_query_data);
173 #else
174 purple_srv_cancel(query->purple_query_data);
175 #endif
176 break;
179 g_free(query);
183 Local Variables:
184 mode: c
185 c-file-style: "bsd"
186 indent-tabs-mode: t
187 tab-width: 8
188 End: