2 * @file purple-dnsquery.c
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
24 /* wrappers for write() & friends for socket handling */
25 #include "win32/win32dep.h"
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
30 #include <sys/socket.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
{
50 sipe_dns_resolved_cb callback
;
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
;
64 if (error_message
|| !g_slist_next(hosts
)) {
65 query
->callback(query
->extradata
, NULL
, 0);
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
;
77 /* OS provides addr so it must be properly aligned */
78 struct sockaddr_in
*sin
= (void *) addr
;
79 addrdata
= &sin
->sin_addr
;
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
);
97 struct sipe_dns_query
*sipe_backend_dns_query_a(SIPE_UNUSED_PARAMETER
struct sipe_core_public
*sipe_public
,
98 const gchar
*hostname
,
100 sipe_dns_resolved_cb callback
,
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
;
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
,
117 (PurpleDnsQueryConnectFunction
) dns_a_response
,
124 static void dns_srv_response(PurpleSrvResponse
*resp
,
126 struct sipe_dns_query
*query
)
129 query
->callback(query
->extradata
, resp
->hostname
, resp
->port
);
131 query
->callback(query
->extradata
, NULL
, 0);
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
,
141 sipe_dns_resolved_cb callback
,
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
;
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
,
159 (PurpleSrvCallback
) dns_srv_response
,
165 void sipe_backend_dns_query_cancel(struct sipe_dns_query
*query
)
167 switch (query
->type
) {
169 purple_dnsquery_destroy(query
->purple_query_data
);
172 #if PURPLE_VERSION_CHECK(2,8,0) || PURPLE_VERSION_CHECK(3,0,0)
173 purple_srv_txt_query_destroy(query
->purple_query_data
);
175 purple_srv_cancel(query
->purple_query_data
);