2 * @file telepathy-dnsquery.c
6 * Copyright (C) 2012 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
26 #include "sipe-backend.h"
27 #include "sipe-common.h"
29 struct sipe_dns_query
{
30 sipe_dns_resolved_cb callback
;
36 static void dns_srv_response(GObject
*resolver
,
41 GList
*targets
= g_resolver_lookup_service_finish(G_RESOLVER(resolver
),
44 struct sipe_dns_query
*query
= data
;
47 GSrvTarget
*target
= targets
->data
;
48 query
->callback(query
->extradata
,
49 g_srv_target_get_hostname(target
),
50 g_srv_target_get_port(target
));
51 g_resolver_free_targets(targets
);
53 SIPE_DEBUG_INFO("dns_srv_response: failed: %s",
54 error
? error
->message
: "UNKNOWN");
57 query
->callback(query
->extradata
, NULL
, 0);
59 g_object_unref(query
->cancel
);
63 struct sipe_dns_query
*sipe_backend_dns_query_srv(SIPE_UNUSED_PARAMETER
struct sipe_core_public
*sipe_public
,
64 const gchar
*protocol
,
65 const gchar
*transport
,
67 sipe_dns_resolved_cb callback
,
70 struct sipe_dns_query
*query
= g_new0(struct sipe_dns_query
, 1);
71 GResolver
*resolver
= g_resolver_get_default();
73 SIPE_DEBUG_INFO("sipe_backend_dns_query_srv: %s/%s/%s",
74 protocol
, transport
, domain
);
76 query
->callback
= callback
;
77 query
->extradata
= data
;
78 query
->cancel
= g_cancellable_new();
79 g_resolver_lookup_service_async(resolver
,
80 protocol
, transport
, domain
,
85 g_object_unref(resolver
);
89 static void dns_a_response(GObject
*resolver
,
94 GList
*addresses
= g_resolver_lookup_by_name_finish(G_RESOLVER(resolver
),
97 struct sipe_dns_query
*query
= data
;
100 GInetAddress
*address
= addresses
->data
;
101 gchar
*ipstr
= g_inet_address_to_string(address
);
102 query
->callback(query
->extradata
, ipstr
, query
->port
);
104 g_resolver_free_addresses(addresses
);
106 SIPE_DEBUG_INFO("dns_a_response: failed: %s",
107 error
? error
->message
: "UNKNOWN");
110 query
->callback(query
->extradata
, NULL
, 0);
112 g_object_unref(query
->cancel
);
116 struct sipe_dns_query
*sipe_backend_dns_query_a(SIPE_UNUSED_PARAMETER
struct sipe_core_public
*sipe_public
,
117 const gchar
*hostname
,
119 sipe_dns_resolved_cb callback
,
122 struct sipe_dns_query
*query
= g_new0(struct sipe_dns_query
, 1);
123 GResolver
*resolver
= g_resolver_get_default();
125 SIPE_DEBUG_INFO("sipe_backend_dns_query_a: %s", hostname
);
127 query
->callback
= callback
;
128 query
->extradata
= data
;
130 query
->cancel
= g_cancellable_new();
131 g_resolver_lookup_by_name_async(resolver
,
137 g_object_unref(resolver
);
141 void sipe_backend_dns_query_cancel(struct sipe_dns_query
*query
)
143 /* callback is invalid now, do no longer call! */
144 query
->callback
= NULL
;
145 g_cancellable_cancel(query
->cancel
);