win32: fix broken libc calls
[siplcs.git] / src / purple / purple-dnsquery.c
blobe9dcc6da732528bc2920d6d95e40b29e4e1583ed
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 <arpa/inet.h>
29 #include <netinet/in.h>
30 #include <sys/socket.h>
31 #endif
33 #include <glib.h>
35 #include "dnsquery.h"
36 #include "dnssrv.h"
37 #include "version.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 sipe_dns_resolved_cb callback;
51 gpointer extradata;
52 gpointer purple_query_data;
55 static void dns_a_response(GSList *hosts,
56 struct sipe_dns_query *query,
57 const char *error_message)
59 char ipstr[INET6_ADDRSTRLEN];
60 struct sockaddr *addr;
61 const void *addrdata;
62 int port;
64 if (error_message || !g_slist_next(hosts)) {
65 query->callback(query->extradata, NULL, 0);
66 g_slist_free(hosts);
67 return;
70 addr = g_slist_next(hosts)->data;
71 if (addr->sa_family == AF_INET6) {
72 /* OS provides addr so it must be properly aligned */
73 struct sockaddr_in6 *sin6 = (void *) addr;
74 addrdata = &sin6->sin6_addr;
75 port = sin6->sin6_port;
76 } else {
77 /* OS provides addr so it must be properly aligned */
78 struct sockaddr_in *sin = (void *) addr;
79 addrdata = &sin->sin_addr;
80 port = sin->sin_port;
83 inet_ntop(addr->sa_family, addrdata, ipstr, sizeof (ipstr));
85 query->callback(query->extradata, ipstr, port);
87 for (; hosts; hosts = g_slist_delete_link(hosts, hosts)) {
88 // Free the addrlen, no data in this link
89 hosts = g_slist_delete_link(hosts, hosts);
90 // Free the address
91 g_free(hosts->data);
94 g_free(query);
97 struct sipe_dns_query *sipe_backend_dns_query_a(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
98 const gchar *hostname,
99 int port,
100 sipe_dns_resolved_cb callback,
101 gpointer data)
103 struct sipe_dns_query *query = g_new(struct sipe_dns_query, 1);
104 #if PURPLE_VERSION_CHECK(3,0,0)
105 struct sipe_backend_private *purple_private = sipe_public->backend_private;
106 #endif
108 query->type = A;
109 query->callback = callback;
110 query->extradata = data;
111 query->purple_query_data = purple_dnsquery_a(
112 #if PURPLE_VERSION_CHECK(3,0,0)
113 purple_private->account,
114 #endif
115 hostname,
116 port,
117 (PurpleDnsQueryConnectFunction) dns_a_response,
118 query);
120 return query;
124 static void dns_srv_response(PurpleSrvResponse *resp,
125 int results,
126 struct sipe_dns_query *query)
128 if (results)
129 query->callback(query->extradata, resp->hostname, resp->port);
130 else
131 query->callback(query->extradata, NULL, 0);
133 g_free(query);
134 g_free(resp);
137 struct sipe_dns_query *sipe_backend_dns_query_srv(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
138 const gchar *protocol,
139 const gchar *transport,
140 const gchar *domain,
141 sipe_dns_resolved_cb callback,
142 gpointer data)
144 struct sipe_dns_query *query = g_new(struct sipe_dns_query, 1);
145 #if PURPLE_VERSION_CHECK(3,0,0)
146 struct sipe_backend_private *purple_private = sipe_public->backend_private;
147 #endif
149 query->type = SRV;
150 query->callback = callback;
151 query->extradata = data;
152 query->purple_query_data = purple_srv_resolve(
153 #if PURPLE_VERSION_CHECK(3,0,0)
154 purple_private->account,
155 #endif
156 protocol,
157 transport,
158 domain,
159 (PurpleSrvCallback) dns_srv_response,
160 query);
162 return query;
165 void sipe_backend_dns_query_cancel(struct sipe_dns_query *query)
167 switch (query->type) {
168 case A:
169 purple_dnsquery_destroy(query->purple_query_data);
170 break;
171 case SRV:
172 #if PURPLE_VERSION_CHECK(2,8,0) || PURPLE_VERSION_CHECK(3,0,0)
173 purple_srv_txt_query_destroy(query->purple_query_data);
174 #else
175 purple_srv_cancel(query->purple_query_data);
176 #endif
177 break;
180 g_free(query);
184 Local Variables:
185 mode: c
186 c-file-style: "bsd"
187 indent-tabs-mode: t
188 tab-width: 8
189 End: