2 * @file telepathy-dnsquery.c
6 * Copyright (C) 2012-2017 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");
58 query
->callback(query
->extradata
, NULL
, 0);
60 g_object_unref(query
->cancel
);
64 struct sipe_dns_query
*sipe_backend_dns_query_srv(SIPE_UNUSED_PARAMETER
struct sipe_core_public
*sipe_public
,
65 const gchar
*protocol
,
66 const gchar
*transport
,
68 sipe_dns_resolved_cb callback
,
71 struct sipe_dns_query
*query
= g_new0(struct sipe_dns_query
, 1);
72 GResolver
*resolver
= g_resolver_get_default();
74 SIPE_DEBUG_INFO("sipe_backend_dns_query_srv: %s/%s/%s",
75 protocol
, transport
, domain
);
77 query
->callback
= callback
;
78 query
->extradata
= data
;
79 query
->cancel
= g_cancellable_new();
80 g_resolver_lookup_service_async(resolver
,
81 protocol
, transport
, domain
,
86 g_object_unref(resolver
);
90 static void dns_a_response(GObject
*resolver
,
95 GList
*addresses
= g_resolver_lookup_by_name_finish(G_RESOLVER(resolver
),
98 struct sipe_dns_query
*query
= data
;
101 GInetAddress
*address
= addresses
->data
;
102 gchar
*ipstr
= g_inet_address_to_string(address
);
103 query
->callback(query
->extradata
, ipstr
, query
->port
);
105 g_resolver_free_addresses(addresses
);
107 SIPE_DEBUG_INFO("dns_a_response: failed: %s",
108 error
? error
->message
: "UNKNOWN");
112 query
->callback(query
->extradata
, NULL
, 0);
114 g_object_unref(query
->cancel
);
118 struct sipe_dns_query
*sipe_backend_dns_query_a(SIPE_UNUSED_PARAMETER
struct sipe_core_public
*sipe_public
,
119 const gchar
*hostname
,
121 sipe_dns_resolved_cb callback
,
124 struct sipe_dns_query
*query
= g_new0(struct sipe_dns_query
, 1);
125 GResolver
*resolver
= g_resolver_get_default();
127 SIPE_DEBUG_INFO("sipe_backend_dns_query_a: %s", hostname
);
129 query
->callback
= callback
;
130 query
->extradata
= data
;
132 query
->cancel
= g_cancellable_new();
133 g_resolver_lookup_by_name_async(resolver
,
139 g_object_unref(resolver
);
143 void sipe_backend_dns_query_cancel(struct sipe_dns_query
*query
)
145 /* callback is invalid now, do no longer call! */
146 query
->callback
= NULL
;
147 g_cancellable_cancel(query
->cancel
);