Open an explorer.exe window at the location of the file when clicking
[pidgin-git.git] / libpurple / dnssrv.c
blob04aff7ecff2e8f94a78f5e82916d41e7c0e66582
1 /**
2 * @file dnssrv.c
3 */
5 /* purple
7 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #define _PURPLE_DNSSRV_C_
25 #include "internal.h"
26 #include "util.h"
28 #ifndef _WIN32
29 #include <arpa/nameser.h>
30 #include <resolv.h>
31 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
32 #include <arpa/nameser_compat.h>
33 #endif
34 #else /* WIN32 */
35 #include <windns.h>
36 /* Missing from the mingw headers */
37 #ifndef DNS_TYPE_SRV
38 # define DNS_TYPE_SRV PurpleDnsTypeSrv
39 #endif
40 #ifndef DNS_TYPE_TXT
41 # define DNS_TYPE_TXT PurpleDnsTypeTxt
42 #endif
43 #endif
45 #ifndef T_SRV
46 #define T_SRV PurpleDnsTypeSrv
47 #endif
48 #ifndef T_TXT
49 #define T_TXT PurpleDnsTypeTxt
50 #endif
52 #include "debug.h"
53 #include "dnssrv.h"
54 #include "eventloop.h"
55 #include "network.h"
57 static PurpleSrvTxtQueryUiOps *srv_txt_query_ui_ops = NULL;
59 #ifndef _WIN32
60 typedef union {
61 HEADER hdr;
62 u_char buf[1024];
63 } queryans;
64 #else
65 static DNS_STATUS (WINAPI *MyDnsQuery_UTF8) (
66 PCSTR lpstrName, WORD wType, DWORD fOptions,
67 PIP4_ARRAY aipServers, PDNS_RECORD* ppQueryResultsSet,
68 PVOID* pReserved) = NULL;
69 static void (WINAPI *MyDnsRecordListFree) (PDNS_RECORD pRecordList,
70 DNS_FREE_TYPE FreeType) = NULL;
71 #endif
73 struct _PurpleSrvTxtQueryData {
74 union {
75 PurpleSrvCallback srv;
76 PurpleTxtCallback txt;
77 } cb;
79 gpointer extradata;
80 guint handle;
81 int type;
82 char *query;
83 #ifdef _WIN32
84 GThread *resolver;
85 char *error_message;
86 GList *results;
87 #else
88 int fd_in, fd_out;
89 pid_t pid;
90 #endif
93 typedef struct _PurpleSrvInternalQuery {
94 int type;
95 char query[256];
96 } PurpleSrvInternalQuery;
98 typedef struct _PurpleSrvResponseContainer {
99 PurpleSrvResponse *response;
100 int sum;
101 } PurpleSrvResponseContainer;
103 static gboolean purple_srv_txt_query_ui_resolve(PurpleSrvTxtQueryData *query_data);
106 * Sort by priority, then by weight. Strictly numerically--no
107 * randomness. Technically we only need to sort by pref and then
108 * make sure any records with weight 0 are at the beginning of
109 * their group, but it's just as easy to sort by weight.
111 static gint
112 responsecompare(gconstpointer ar, gconstpointer br)
114 PurpleSrvResponse *a = (PurpleSrvResponse*)ar;
115 PurpleSrvResponse *b = (PurpleSrvResponse*)br;
117 if(a->pref == b->pref) {
118 if(a->weight == b->weight)
119 return 0;
120 if(a->weight < b->weight)
121 return -1;
122 return 1;
124 if(a->pref < b->pref)
125 return -1;
126 return 1;
130 * Iterate over a list of PurpleSrvResponseContainer making the sum
131 * the running total of the sums. Select a random integer in the range
132 * (1, sum+1), then find the first element greater than or equal to the
133 * number selected. From RFC 2782.
135 * @param list The list of PurpleSrvResponseContainer. This function
136 * removes a node from this list and returns the new list.
137 * @param container_ptr The PurpleSrvResponseContainer that was chosen
138 * will be returned here.
140 static GList *
141 select_random_response(GList *list, PurpleSrvResponseContainer **container_ptr)
143 GList *cur;
144 size_t runningtotal;
145 int r;
147 runningtotal = 0;
148 cur = list;
150 while (cur) {
151 PurpleSrvResponseContainer *container = cur->data;
152 runningtotal += container->response->weight;
153 container->sum = runningtotal;
154 cur = cur->next;
158 * If the running total is greater than 0, pick a number between
159 * 1 and the runningtotal inclusive. (This is not precisely what
160 * the RFC algorithm describes, but we wish to deal with integers
161 * and avoid floats. This is functionally equivalent.)
162 * If running total is 0, then choose r = 0.
164 r = runningtotal ? g_random_int_range(1, runningtotal + 1) : 0;
165 cur = list;
166 while (r > ((PurpleSrvResponseContainer *)cur->data)->sum) {
167 cur = cur->next;
170 /* Set the return parameter and remove cur from the list */
171 *container_ptr = cur->data;
172 return g_list_delete_link(list, cur);
176 * Reorder a GList of PurpleSrvResponses that have the same priority
177 * (aka "pref").
179 static void
180 srv_reorder(GList *list, int num)
182 int i;
183 GList *cur, *container_list = NULL;
184 PurpleSrvResponseContainer *container;
186 if (num < 2)
187 /* Nothing to sort */
188 return;
190 /* First build a list of container structs */
191 for (i = 0, cur = list; i < num; i++, cur = cur->next) {
192 container = g_new(PurpleSrvResponseContainer, 1);
193 container->response = cur->data;
194 container_list = g_list_prepend(container_list, container);
196 container_list = g_list_reverse(container_list);
199 * Re-order the list that was passed in as a parameter. We leave
200 * the list nodes in place, but replace their data pointers.
202 cur = list;
203 while (container_list) {
204 container_list = select_random_response(container_list, &container);
205 cur->data = container->response;
206 g_free(container);
207 cur = cur->next;
212 * Sorts a GList of PurpleSrvResponses according to the
213 * algorithm described in RFC 2782.
215 * @param response GList of PurpleSrvResponse's
216 * @param The original list, resorted
218 static GList *
219 purple_srv_sort(GList *list)
221 int pref, count;
222 GList *cur, *start;
224 if (!list || !list->next) {
225 /* Nothing to sort */
226 return list;
229 list = g_list_sort(list, responsecompare);
231 start = cur = list;
232 count = 1;
233 while (cur) {
234 PurpleSrvResponse *next_response;
235 pref = ((PurpleSrvResponse *)cur->data)->pref;
236 next_response = cur->next ? cur->next->data : NULL;
237 if (!next_response || next_response->pref != pref) {
239 * The 'count' records starting at 'start' all have the same
240 * priority. Sort them by weight.
242 srv_reorder(start, count);
243 start = cur->next;
244 count = 0;
246 count++;
247 cur = cur->next;
250 return list;
253 static PurpleSrvTxtQueryData *
254 query_data_new(int type, gchar *query, gpointer extradata)
256 PurpleSrvTxtQueryData *query_data = g_new0(PurpleSrvTxtQueryData, 1);
257 query_data->type = type;
258 query_data->extradata = extradata;
259 query_data->query = query;
260 #ifndef _WIN32
261 query_data->fd_in = -1;
262 query_data->fd_out = -1;
263 #endif
264 return query_data;
267 void
268 purple_srv_txt_query_destroy(PurpleSrvTxtQueryData *query_data)
270 PurpleSrvTxtQueryUiOps *ops = purple_srv_txt_query_get_ui_ops();
272 if (ops && ops->destroy)
273 ops->destroy(query_data);
275 if (query_data->handle > 0)
276 purple_input_remove(query_data->handle);
277 #ifdef _WIN32
278 if (query_data->resolver != NULL)
281 * It's not really possible to kill a thread. So instead we
282 * just set the callback to NULL and let the DNS lookup
283 * finish.
285 query_data->cb.srv = NULL;
286 return;
288 g_free(query_data->error_message);
289 #else
290 if (query_data->fd_out != -1)
291 close(query_data->fd_out);
292 if (query_data->fd_in != -1)
293 close(query_data->fd_in);
294 #endif
295 g_free(query_data->query);
296 g_free(query_data);
299 #ifdef USE_IDN
300 static gboolean
301 dns_str_is_ascii(const char *name)
303 guchar *c;
304 for (c = (guchar *)name; c && *c; ++c) {
305 if (*c > 0x7f)
306 return FALSE;
309 return TRUE;
311 #endif
313 #ifndef _WIN32
314 static void
315 write_to_parent(int in, int out, gconstpointer data, gsize size)
317 const guchar *buf = data;
318 gssize w;
320 do {
321 w = write(out, buf, size);
322 if (w > 0) {
323 buf += w;
324 size -= w;
325 } else if (w < 0 && errno == EINTR) {
326 /* Let's try some more; */
327 w = 1;
329 } while (size > 0 && w > 0);
331 if (size != 0) {
332 /* An error occurred */
333 close(out);
334 close(in);
335 _exit(0);
339 /* Read size bytes to data. Dies if an error occurs. */
340 static void
341 read_from_parent(int in, int out, gpointer data, gsize size)
343 guchar *buf = data;
344 gssize r;
346 do {
347 r = read(in, data, size);
348 if (r > 0) {
349 buf += r;
350 size -= r;
351 } else if (r < 0 && errno == EINTR) {
352 /* Let's try some more; */
353 r = 1;
355 } while (size > 0 && r > 0);
357 if (size != 0) {
358 /* An error occurred */
359 close(out);
360 close(in);
361 _exit(0);
366 G_GNUC_NORETURN static void
367 resolve(int in, int out)
369 GList *ret = NULL;
370 PurpleSrvResponse *srvres;
371 PurpleTxtResponse *txtres;
372 queryans answer;
373 int size, qdcount, ancount;
374 guchar *end, *cp;
375 gchar name[256];
376 guint16 type, dlen, pref, weight, port;
377 PurpleSrvInternalQuery query;
379 #ifdef HAVE_SIGNAL_H
380 purple_restore_default_signal_handlers();
381 #endif
383 read_from_parent(in, out, &query, sizeof(query));
385 size = res_query( query.query, C_IN, query.type, (u_char*)&answer, sizeof( answer));
386 if (size == -1) {
387 write_to_parent(in, out, &(query.type), sizeof(query.type));
388 write_to_parent(in, out, &size, sizeof(size));
389 close(out);
390 close(in);
391 _exit(0);
394 qdcount = ntohs(answer.hdr.qdcount);
395 ancount = ntohs(answer.hdr.ancount);
396 cp = (guchar*)&answer + sizeof(HEADER);
397 end = (guchar*)&answer + size;
399 /* skip over unwanted stuff */
400 while (qdcount-- > 0 && cp < end) {
401 size = dn_expand( (unsigned char*)&answer, end, cp, name, 256);
402 if(size < 0) goto end;
403 cp += size + QFIXEDSZ;
406 while (ancount-- > 0 && cp < end) {
407 size = dn_expand((unsigned char*)&answer, end, cp, name, 256);
408 if(size < 0)
409 goto end;
410 cp += size;
411 GETSHORT(type,cp);
413 /* skip ttl and class since we already know it */
414 cp += 6;
416 GETSHORT(dlen,cp);
417 if (type == T_SRV) {
418 GETSHORT(pref,cp);
420 GETSHORT(weight,cp);
422 GETSHORT(port,cp);
424 size = dn_expand( (unsigned char*)&answer, end, cp, name, 256);
425 if(size < 0 )
426 goto end;
428 cp += size;
430 srvres = g_new0(PurpleSrvResponse, 1);
431 if (strlen(name) > sizeof(srvres->hostname) - 1) {
432 purple_debug_error("dnssrv", "hostname is longer than available buffer ('%s', %zd bytes)!",
433 name, strlen(name));
435 g_strlcpy(srvres->hostname, name, sizeof(srvres->hostname));
436 srvres->pref = pref;
437 srvres->port = port;
438 srvres->weight = weight;
440 ret = g_list_prepend(ret, srvres);
441 } else if (type == T_TXT) {
442 txtres = g_new0(PurpleTxtResponse, 1);
443 txtres->content = g_strndup((gchar*)(++cp), dlen-1);
444 ret = g_list_append(ret, txtres);
445 cp += dlen - 1;
446 } else {
447 cp += dlen;
451 end:
452 size = g_list_length(ret);
454 if (query.type == T_SRV)
455 ret = purple_srv_sort(ret);
457 write_to_parent(in, out, &(query.type), sizeof(query.type));
458 write_to_parent(in, out, &size, sizeof(size));
459 while (ret != NULL)
461 if (query.type == T_SRV)
462 write_to_parent(in, out, ret->data, sizeof(PurpleSrvResponse));
463 if (query.type == T_TXT) {
464 PurpleTxtResponse *response = ret->data;
465 gsize l = strlen(response->content) + 1 /* null byte */;
466 write_to_parent(in, out, &l, sizeof(l));
467 write_to_parent(in, out, response->content, l);
470 g_free(ret->data);
471 ret = g_list_remove(ret, ret->data);
474 close(out);
475 close(in);
477 _exit(0);
480 static void
481 resolved(gpointer data, gint source, PurpleInputCondition cond)
483 int size;
484 int type;
485 PurpleSrvTxtQueryData *query_data = (PurpleSrvTxtQueryData*)data;
486 int i;
487 int status;
489 if (read(source, &type, sizeof(type)) == sizeof(type)) {
490 if (read(source, &size, sizeof(size)) == sizeof(size)) {
491 if (size == -1 || size == 0) {
492 if (size == -1) {
493 purple_debug_warning("dnssrv", "res_query returned an error\n");
494 /* Re-read resolv.conf and friends in case DNS servers have changed */
495 res_init();
496 } else
497 purple_debug_info("dnssrv", "Found 0 entries, errno is %i\n", errno);
499 if (type == T_SRV) {
500 PurpleSrvCallback cb = query_data->cb.srv;
501 cb(NULL, 0, query_data->extradata);
502 } else if (type == T_TXT) {
503 PurpleTxtCallback cb = query_data->cb.txt;
504 cb(NULL, query_data->extradata);
505 } else {
506 purple_debug_error("dnssrv", "type unknown of DNS result entry; errno is %i\n", errno);
509 } else if (size) {
510 if (type == T_SRV) {
511 PurpleSrvResponse *res;
512 PurpleSrvResponse *tmp;
513 PurpleSrvCallback cb = query_data->cb.srv;
514 ssize_t red;
515 purple_debug_info("dnssrv","found %d SRV entries\n", size);
516 tmp = res = g_new0(PurpleSrvResponse, size);
517 for (i = 0; i < size; i++) {
518 red = read(source, tmp++, sizeof(PurpleSrvResponse));
519 if (red != sizeof(PurpleSrvResponse)) {
520 purple_debug_error("dnssrv","unable to read srv "
521 "response: %s\n", g_strerror(errno));
522 size = 0;
523 g_free(res);
524 res = NULL;
528 cb(res, size, query_data->extradata);
529 } else if (type == T_TXT) {
530 GList *responses = NULL;
531 PurpleTxtResponse *res;
532 PurpleTxtCallback cb = query_data->cb.txt;
533 ssize_t red;
534 purple_debug_info("dnssrv","found %d TXT entries\n", size);
535 for (i = 0; i < size; i++) {
536 gsize len;
538 red = read(source, &len, sizeof(len));
539 if (red != sizeof(len)) {
540 purple_debug_error("dnssrv","unable to read txt "
541 "response length: %s\n", g_strerror(errno));
542 size = 0;
543 g_list_foreach(responses, (GFunc)purple_txt_response_destroy, NULL);
544 g_list_free(responses);
545 responses = NULL;
546 break;
549 res = g_new0(PurpleTxtResponse, 1);
550 res->content = g_new0(gchar, len);
552 red = read(source, res->content, len);
553 if (red != len) {
554 purple_debug_error("dnssrv","unable to read txt "
555 "response: %s\n", g_strerror(errno));
556 size = 0;
557 purple_txt_response_destroy(res);
558 g_list_foreach(responses, (GFunc)purple_txt_response_destroy, NULL);
559 g_list_free(responses);
560 responses = NULL;
561 break;
563 responses = g_list_prepend(responses, res);
566 responses = g_list_reverse(responses);
567 cb(responses, query_data->extradata);
568 } else {
569 purple_debug_error("dnssrv", "type unknown of DNS result entry; errno is %i\n", errno);
575 waitpid(query_data->pid, &status, 0);
576 purple_srv_txt_query_destroy(query_data);
579 #else /* _WIN32 */
581 /** The Jabber Server code was inspiration for parts of this. */
583 static gboolean
584 res_main_thread_cb(gpointer data)
586 PurpleSrvResponse *srvres = NULL;
587 PurpleSrvTxtQueryData *query_data = data;
588 if(query_data->error_message != NULL) {
589 purple_debug_error("dnssrv", "%s", query_data->error_message);
590 if (query_data->type == DNS_TYPE_SRV) {
591 if (query_data->cb.srv)
592 query_data->cb.srv(srvres, 0, query_data->extradata);
593 } else if (query_data->type == DNS_TYPE_TXT) {
594 if (query_data->cb.txt)
595 query_data->cb.txt(NULL, query_data->extradata);
597 } else {
598 if (query_data->type == DNS_TYPE_SRV) {
599 PurpleSrvResponse *srvres_tmp = NULL;
600 GList *lst = query_data->results;
601 int size = g_list_length(lst);
603 if(query_data->cb.srv && size > 0)
604 srvres_tmp = srvres = g_new0(PurpleSrvResponse, size);
605 while (lst) {
606 PurpleSrvResponse *lstdata = lst->data;
607 lst = g_list_delete_link(lst, lst);
609 if(query_data->cb.srv)
610 memcpy(srvres_tmp++, lstdata, sizeof(PurpleSrvResponse));
611 g_free(lstdata);
614 query_data->results = NULL;
616 purple_debug_info("dnssrv", "found %d SRV entries\n", size);
618 if(query_data->cb.srv) query_data->cb.srv(srvres, size, query_data->extradata);
619 } else if (query_data->type == DNS_TYPE_TXT) {
620 GList *lst = query_data->results;
622 purple_debug_info("dnssrv", "found %d TXT entries\n", g_list_length(lst));
624 if (query_data->cb.txt) {
625 query_data->results = NULL;
626 query_data->cb.txt(lst, query_data->extradata);
628 } else {
629 purple_debug_error("dnssrv", "unknown query type");
633 query_data->resolver = NULL;
634 query_data->handle = 0;
636 purple_srv_txt_query_destroy(query_data);
638 return FALSE;
641 static gpointer
642 res_thread(gpointer data)
644 PDNS_RECORD dr = NULL;
645 int type;
646 DNS_STATUS ds;
647 PurpleSrvTxtQueryData *query_data = data;
648 type = query_data->type;
649 ds = MyDnsQuery_UTF8(query_data->query, type, DNS_QUERY_STANDARD, NULL, &dr, NULL);
650 if (ds != ERROR_SUCCESS) {
651 gchar *msg = g_win32_error_message(ds);
652 if (type == DNS_TYPE_SRV) {
653 query_data->error_message = g_strdup_printf("Couldn't look up SRV record. %s (%lu).\n", msg, ds);
654 } else if (type == DNS_TYPE_TXT) {
655 query_data->error_message = g_strdup_printf("Couldn't look up TXT record. %s (%lu).\n", msg, ds);
657 g_free(msg);
658 } else {
659 if (type == DNS_TYPE_SRV) {
660 PDNS_RECORD dr_tmp;
661 GList *lst = NULL;
662 DNS_SRV_DATA *srv_data;
663 PurpleSrvResponse *srvres;
665 for (dr_tmp = dr; dr_tmp != NULL; dr_tmp = dr_tmp->pNext) {
666 /* Discard any incorrect entries. I'm not sure if this is necessary */
667 if (dr_tmp->wType != type || strcmp(dr_tmp->pName, query_data->query) != 0) {
668 continue;
671 srv_data = &dr_tmp->Data.SRV;
672 srvres = g_new0(PurpleSrvResponse, 1);
673 strncpy(srvres->hostname, srv_data->pNameTarget, 255);
674 srvres->hostname[255] = '\0';
675 srvres->pref = srv_data->wPriority;
676 srvres->port = srv_data->wPort;
677 srvres->weight = srv_data->wWeight;
679 lst = g_list_prepend(lst, srvres);
682 MyDnsRecordListFree(dr, DnsFreeRecordList);
683 query_data->results = purple_srv_sort(lst);
684 } else if (type == DNS_TYPE_TXT) {
685 PDNS_RECORD dr_tmp;
686 GList *lst = NULL;
687 DNS_TXT_DATA *txt_data;
688 PurpleTxtResponse *txtres;
690 for (dr_tmp = dr; dr_tmp != NULL; dr_tmp = dr_tmp->pNext) {
691 GString *s;
692 int i;
694 /* Discard any incorrect entries. I'm not sure if this is necessary */
695 if (dr_tmp->wType != type || strcmp(dr_tmp->pName, query_data->query) != 0) {
696 continue;
699 txt_data = &dr_tmp->Data.TXT;
700 txtres = g_new0(PurpleTxtResponse, 1);
702 s = g_string_new("");
703 for (i = 0; i < txt_data->dwStringCount; ++i)
704 s = g_string_append(s, txt_data->pStringArray[i]);
705 txtres->content = g_string_free(s, FALSE);
707 lst = g_list_append(lst, txtres);
710 MyDnsRecordListFree(dr, DnsFreeRecordList);
711 query_data->results = lst;
712 } else {
717 /* back to main thread */
718 /* Note: this should *not* be attached to query_data->handle - it will cause leakage */
719 purple_timeout_add(0, res_main_thread_cb, query_data);
721 g_thread_exit(NULL);
722 return NULL;
725 #endif
727 PurpleSrvTxtQueryData *
728 purple_srv_resolve(const char *protocol, const char *transport,
729 const char *domain, PurpleSrvCallback cb, gpointer extradata)
731 return purple_srv_resolve_account(NULL, protocol, transport, domain,
732 cb, extradata);
735 PurpleSrvTxtQueryData *
736 purple_srv_resolve_account(PurpleAccount *account, const char *protocol,
737 const char *transport, const char *domain, PurpleSrvCallback cb,
738 gpointer extradata)
740 char *query;
741 char *hostname;
742 PurpleSrvTxtQueryData *query_data;
743 PurpleProxyType proxy_type;
744 #ifndef _WIN32
745 PurpleSrvInternalQuery internal_query;
746 int in[2], out[2];
747 int pid;
748 #else
749 GError* err = NULL;
750 static gboolean initialized = FALSE;
751 #endif
753 if (!protocol || !*protocol || !transport || !*transport || !domain || !*domain) {
754 purple_debug_error("dnssrv", "Wrong arguments\n");
755 cb(NULL, 0, extradata);
756 g_return_val_if_reached(NULL);
759 proxy_type = purple_proxy_info_get_type(
760 purple_proxy_get_setup(account));
761 if (proxy_type == PURPLE_PROXY_TOR) {
762 purple_debug_info("dnssrv", "Aborting SRV lookup in Tor Proxy mode.");
763 cb(NULL, 0, extradata);
764 return NULL;
767 #ifdef USE_IDN
768 if (!dns_str_is_ascii(domain)) {
769 int ret = purple_network_convert_idn_to_ascii(domain, &hostname);
770 if (ret != 0) {
771 purple_debug_error("dnssrv", "IDNA ToASCII failed\n");
772 cb(NULL, 0, extradata);
773 return NULL;
775 } else /* Fallthru is intentional */
776 #endif
777 hostname = g_strdup(domain);
779 query = g_strdup_printf("_%s._%s.%s", protocol, transport, hostname);
780 purple_debug_info("dnssrv","querying SRV record for %s: %s\n", domain,
781 query);
782 g_free(hostname);
784 query_data = query_data_new(PurpleDnsTypeSrv, query, extradata);
785 query_data->cb.srv = cb;
787 if (purple_srv_txt_query_ui_resolve(query_data))
789 return query_data;
792 #ifndef _WIN32
793 if(pipe(in) || pipe(out)) {
794 purple_debug_error("dnssrv", "Could not create pipe\n");
795 g_free(query);
796 g_free(query_data);
797 cb(NULL, 0, extradata);
798 return NULL;
801 pid = fork();
802 if (pid == -1) {
803 purple_debug_error("dnssrv", "Could not create process!\n");
804 g_free(query);
805 g_free(query_data);
806 cb(NULL, 0, extradata);
807 return NULL;
810 /* Child */
811 if (pid == 0)
813 g_free(query);
814 g_free(query_data);
816 close(out[0]);
817 close(in[1]);
818 resolve(in[0], out[1]);
819 /* resolve() does not return */
822 close(out[1]);
823 close(in[0]);
825 internal_query.type = T_SRV;
826 strncpy(internal_query.query, query, 255);
827 internal_query.query[255] = '\0';
829 if (write(in[1], &internal_query, sizeof(internal_query)) < 0)
830 purple_debug_error("dnssrv", "Could not write to SRV resolver\n");
832 query_data->pid = pid;
833 query_data->fd_out = out[0];
834 query_data->fd_in = in[1];
835 query_data->handle = purple_input_add(out[0], PURPLE_INPUT_READ, resolved, query_data);
837 return query_data;
838 #else
839 if (!initialized) {
840 MyDnsQuery_UTF8 = (void*) wpurple_find_and_loadproc("dnsapi.dll", "DnsQuery_UTF8");
841 MyDnsRecordListFree = (void*) wpurple_find_and_loadproc(
842 "dnsapi.dll", "DnsRecordListFree");
843 initialized = TRUE;
846 if (!MyDnsQuery_UTF8 || !MyDnsRecordListFree)
847 query_data->error_message = g_strdup("System missing DNS API (Requires W2K+)\n");
848 else {
849 query_data->resolver = g_thread_create(res_thread, query_data, FALSE, &err);
850 if (query_data->resolver == NULL) {
851 query_data->error_message = g_strdup_printf("SRV thread create failure: %s\n", (err && err->message) ? err->message : "");
852 g_error_free(err);
856 /* The query isn't going to happen, so finish the SRV lookup now.
857 * Asynchronously call the callback since stuff may not expect
858 * the callback to be called before this returns */
859 if (query_data->error_message != NULL)
860 query_data->handle = purple_timeout_add(0, res_main_thread_cb, query_data);
862 return query_data;
863 #endif
866 PurpleSrvTxtQueryData *purple_txt_resolve(const char *owner,
867 const char *domain, PurpleTxtCallback cb, gpointer extradata)
869 return purple_txt_resolve_account(NULL, owner, domain, cb, extradata);
872 PurpleSrvTxtQueryData *purple_txt_resolve_account(PurpleAccount *account,
873 const char *owner, const char *domain, PurpleTxtCallback cb,
874 gpointer extradata)
876 char *query;
877 char *hostname;
878 PurpleSrvTxtQueryData *query_data;
879 PurpleProxyType proxy_type;
880 #ifndef _WIN32
881 PurpleSrvInternalQuery internal_query;
882 int in[2], out[2];
883 int pid;
884 #else
885 GError* err = NULL;
886 static gboolean initialized = FALSE;
887 #endif
889 proxy_type = purple_proxy_info_get_type(
890 purple_proxy_get_setup(account));
891 if (proxy_type == PURPLE_PROXY_TOR) {
892 purple_debug_info("dnssrv", "Aborting TXT lookup in Tor Proxy mode.");
893 cb(NULL, extradata);
894 return NULL;
897 #ifdef USE_IDN
898 if (!dns_str_is_ascii(domain)) {
899 int ret = purple_network_convert_idn_to_ascii(domain, &hostname);
900 if (ret != 0) {
901 purple_debug_error("dnssrv", "IDNA ToASCII failed\n");
902 cb(NULL, extradata);
903 return NULL;
905 } else /* fallthru is intentional */
906 #endif
907 hostname = g_strdup(domain);
909 query = g_strdup_printf("%s.%s", owner, hostname);
910 purple_debug_info("dnssrv","querying TXT record for %s: %s\n", domain,
911 query);
912 g_free(hostname);
914 query_data = query_data_new(PurpleDnsTypeTxt, query, extradata);
915 query_data->cb.txt = cb;
917 if (purple_srv_txt_query_ui_resolve(query_data)) {
918 /* query intentionally not freed
920 return query_data;
923 #ifndef _WIN32
924 if(pipe(in) || pipe(out)) {
925 purple_debug_error("dnssrv", "Could not create pipe\n");
926 g_free(query);
927 g_free(query_data);
928 cb(NULL, extradata);
929 return NULL;
932 pid = fork();
933 if (pid == -1) {
934 purple_debug_error("dnssrv", "Could not create process!\n");
935 g_free(query);
936 g_free(query_data);
937 cb(NULL, extradata);
938 return NULL;
941 /* Child */
942 if (pid == 0)
944 g_free(query);
945 g_free(query_data);
947 close(out[0]);
948 close(in[1]);
949 resolve(in[0], out[1]);
950 /* resolve() does not return */
953 close(out[1]);
954 close(in[0]);
956 internal_query.type = T_TXT;
957 strncpy(internal_query.query, query, 255);
958 internal_query.query[255] = '\0';
960 if (write(in[1], &internal_query, sizeof(internal_query)) < 0)
961 purple_debug_error("dnssrv", "Could not write to TXT resolver\n");
963 query_data->pid = pid;
964 query_data->fd_out = out[0];
965 query_data->fd_in = in[1];
966 query_data->handle = purple_input_add(out[0], PURPLE_INPUT_READ, resolved, query_data);
968 return query_data;
969 #else
970 if (!initialized) {
971 MyDnsQuery_UTF8 = (void*) wpurple_find_and_loadproc("dnsapi.dll", "DnsQuery_UTF8");
972 MyDnsRecordListFree = (void*) wpurple_find_and_loadproc(
973 "dnsapi.dll", "DnsRecordListFree");
974 initialized = TRUE;
977 if (!MyDnsQuery_UTF8 || !MyDnsRecordListFree)
978 query_data->error_message = g_strdup("System missing DNS API (Requires W2K+)\n");
979 else {
980 query_data->resolver = g_thread_create(res_thread, query_data, FALSE, &err);
981 if (query_data->resolver == NULL) {
982 query_data->error_message = g_strdup_printf("TXT thread create failure: %s\n", (err && err->message) ? err->message : "");
983 g_error_free(err);
987 /* The query isn't going to happen, so finish the TXT lookup now.
988 * Asynchronously call the callback since stuff may not expect
989 * the callback to be called before this returns */
990 if (query_data->error_message != NULL)
991 query_data->handle = purple_timeout_add(0, res_main_thread_cb, query_data);
993 return query_data;
994 #endif
997 void
998 purple_txt_cancel(PurpleSrvTxtQueryData *query_data)
1000 purple_srv_txt_query_destroy(query_data);
1003 void
1004 purple_srv_cancel(PurpleSrvTxtQueryData *query_data)
1006 purple_srv_txt_query_destroy(query_data);
1009 const gchar *
1010 purple_txt_response_get_content(PurpleTxtResponse *resp)
1012 g_return_val_if_fail(resp != NULL, NULL);
1014 return resp->content;
1017 void purple_txt_response_destroy(PurpleTxtResponse *resp)
1019 g_return_if_fail(resp != NULL);
1021 g_free(resp->content);
1022 g_free(resp);
1026 * Only used as the callback for the ui ops.
1028 static void
1029 purple_srv_query_resolved(PurpleSrvTxtQueryData *query_data, GList *records)
1031 GList *l;
1032 PurpleSrvResponse *records_array;
1033 int i = 0, length;
1035 g_return_if_fail(records != NULL);
1037 if (query_data->cb.srv == NULL) {
1038 purple_srv_txt_query_destroy(query_data);
1040 while (records) {
1041 g_free(records->data);
1042 records = g_list_delete_link(records, records);
1044 return;
1047 records = purple_srv_sort(records);
1048 length = g_list_length(records);
1050 purple_debug_info("dnssrv", "SRV records resolved for %s, count: %d\n",
1051 query_data->query, length);
1053 records_array = g_new(PurpleSrvResponse, length);
1054 for (l = records; l; l = l->next, i++) {
1055 records_array[i] = *(PurpleSrvResponse *)l->data;
1058 query_data->cb.srv(records_array, length, query_data->extradata);
1060 purple_srv_txt_query_destroy(query_data);
1062 while (records) {
1063 g_free(records->data);
1064 records = g_list_delete_link(records, records);
1069 * Only used as the callback for the ui ops.
1071 static void
1072 purple_txt_query_resolved(PurpleSrvTxtQueryData *query_data, GList *entries)
1074 g_return_if_fail(entries != NULL);
1076 purple_debug_info("dnssrv", "TXT entries resolved for %s, count: %d\n", query_data->query, g_list_length(entries));
1078 /* the callback should g_free the entries.
1080 if (query_data->cb.txt != NULL)
1081 query_data->cb.txt(entries, query_data->extradata);
1082 else {
1083 while (entries) {
1084 g_free(entries->data);
1085 entries = g_list_delete_link(entries, entries);
1089 purple_srv_txt_query_destroy(query_data);
1092 static void
1093 purple_srv_query_failed(PurpleSrvTxtQueryData *query_data, const gchar *error_message)
1095 purple_debug_error("dnssrv", "%s\n", error_message);
1097 if (query_data->cb.srv != NULL)
1098 query_data->cb.srv(NULL, 0, query_data->extradata);
1100 purple_srv_txt_query_destroy(query_data);
1103 static gboolean
1104 purple_srv_txt_query_ui_resolve(PurpleSrvTxtQueryData *query_data)
1106 PurpleSrvTxtQueryUiOps *ops = purple_srv_txt_query_get_ui_ops();
1108 if (ops && ops->resolve)
1109 return ops->resolve(query_data, (query_data->type == T_SRV ? purple_srv_query_resolved : purple_txt_query_resolved), purple_srv_query_failed);
1111 return FALSE;
1114 void
1115 purple_srv_txt_query_set_ui_ops(PurpleSrvTxtQueryUiOps *ops)
1117 srv_txt_query_ui_ops = ops;
1120 PurpleSrvTxtQueryUiOps *
1121 purple_srv_txt_query_get_ui_ops(void)
1123 /* It is perfectly acceptable for srv_txt_query_ui_ops to be NULL; this just
1124 * means that the default platform-specific implementation will be used.
1126 return srv_txt_query_ui_ops;
1129 char *
1130 purple_srv_txt_query_get_query(PurpleSrvTxtQueryData *query_data)
1132 g_return_val_if_fail(query_data != NULL, NULL);
1134 return query_data->query;
1139 purple_srv_txt_query_get_type(PurpleSrvTxtQueryData *query_data)
1141 g_return_val_if_fail(query_data != NULL, 0);
1143 return query_data->type;