lync: parse servers out of response
[siplcs.git] / src / telepathy / telepathy-dnsquery.c
blob441e50c61c5f952be9bd77f4246074d121a71f67
1 /**
2 * @file telepathy-dnsquery.c
4 * pidgin-sipe
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
23 #include <glib.h>
24 #include <gio/gio.h>
26 #include "sipe-backend.h"
27 #include "sipe-common.h"
29 struct sipe_dns_query {
30 sipe_dns_resolved_cb callback;
31 gpointer extradata;
32 guint port;
33 GCancellable *cancel;
36 static void dns_srv_response(GObject *resolver,
37 GAsyncResult *result,
38 gpointer data)
40 GError *error = NULL;
41 GList *targets = g_resolver_lookup_service_finish(G_RESOLVER(resolver),
42 result,
43 &error);
44 struct sipe_dns_query *query = data;
46 if (targets) {
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);
52 } else {
53 SIPE_DEBUG_INFO("dns_srv_response: failed: %s",
54 error ? error->message : "UNKNOWN");
55 g_error_free(error);
56 if (query->callback)
57 query->callback(query->extradata, NULL, 0);
59 g_object_unref(query->cancel);
60 g_free(query);
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,
66 const gchar *domain,
67 sipe_dns_resolved_cb callback,
68 gpointer data)
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,
81 query->cancel,
82 dns_srv_response,
83 query);
85 g_object_unref(resolver);
86 return(query);
89 static void dns_a_response(GObject *resolver,
90 GAsyncResult *result,
91 gpointer data)
93 GError *error = NULL;
94 GList *addresses = g_resolver_lookup_by_name_finish(G_RESOLVER(resolver),
95 result,
96 &error);
97 struct sipe_dns_query *query = data;
99 if (addresses) {
100 GInetAddress *address = addresses->data;
101 gchar *ipstr = g_inet_address_to_string(address);
102 query->callback(query->extradata, ipstr, query->port);
103 g_free(ipstr);
104 g_resolver_free_addresses(addresses);
105 } else {
106 SIPE_DEBUG_INFO("dns_a_response: failed: %s",
107 error ? error->message : "UNKNOWN");
108 g_error_free(error);
109 if (query->callback)
110 query->callback(query->extradata, NULL, 0);
112 g_object_unref(query->cancel);
113 g_free(query);
116 struct sipe_dns_query *sipe_backend_dns_query_a(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
117 const gchar *hostname,
118 guint port,
119 sipe_dns_resolved_cb callback,
120 gpointer data)
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;
129 query->port = port;
130 query->cancel = g_cancellable_new();
131 g_resolver_lookup_by_name_async(resolver,
132 hostname,
133 query->cancel,
134 dns_a_response,
135 query);
137 g_object_unref(resolver);
138 return(query);
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);
149 Local Variables:
150 mode: c
151 c-file-style: "bsd"
152 indent-tabs-mode: t
153 tab-width: 8
154 End: