Oops, I forgot this. This is done internally in libgadu, and not doing it
[pidgin-git.git] / libpurple / dnssrv.c
blob1e011c0fc48951aeefb5a0067ced67ae13fb2df4
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
24 #include "internal.h"
25 #include "util.h"
27 #ifndef _WIN32
28 #include <arpa/nameser.h>
29 #include <resolv.h>
30 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
31 #include <arpa/nameser_compat.h>
32 #endif
33 #ifndef T_SRV
34 #define T_SRV 33
35 #endif
36 #ifndef T_TXT
37 #define T_TXT 16
38 #endif
39 #else /* WIN32 */
40 #include <windns.h>
41 /* Missing from the mingw headers */
42 #ifndef DNS_TYPE_SRV
43 # define DNS_TYPE_SRV 33
44 #endif
45 #ifndef DNS_TYPE_TXT
46 # define DNS_TYPE_TXT 16
47 #endif
48 #endif
50 #include "debug.h"
51 #include "dnssrv.h"
52 #include "eventloop.h"
53 #include "network.h"
55 #ifndef _WIN32
56 typedef union {
57 HEADER hdr;
58 u_char buf[1024];
59 } queryans;
60 #else
61 static DNS_STATUS (WINAPI *MyDnsQuery_UTF8) (
62 PCSTR lpstrName, WORD wType, DWORD fOptions,
63 PIP4_ARRAY aipServers, PDNS_RECORD* ppQueryResultsSet,
64 PVOID* pReserved) = NULL;
65 static void (WINAPI *MyDnsRecordListFree) (PDNS_RECORD pRecordList,
66 DNS_FREE_TYPE FreeType) = NULL;
67 #endif
69 struct _PurpleTxtResponse {
70 char *content;
73 struct _PurpleSrvQueryData {
74 union {
75 PurpleSrvCallback srv;
76 PurpleTxtCallback txt;
77 } cb;
79 gpointer extradata;
80 guint handle;
81 int type;
82 #ifdef _WIN32
83 GThread *resolver;
84 char *query;
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;
104 * Sort by priority, then by weight. Strictly numerically--no
105 * randomness. Technically we only need to sort by pref and then
106 * make sure any records with weight 0 are at the beginning of
107 * their group, but it's just as easy to sort by weight.
109 static gint
110 responsecompare(gconstpointer ar, gconstpointer br)
112 PurpleSrvResponse *a = (PurpleSrvResponse*)ar;
113 PurpleSrvResponse *b = (PurpleSrvResponse*)br;
115 if(a->pref == b->pref) {
116 if(a->weight == b->weight)
117 return 0;
118 if(a->weight < b->weight)
119 return -1;
120 return 1;
122 if(a->pref < b->pref)
123 return -1;
124 return 1;
128 * Iterate over a list of PurpleSrvResponseContainer making the sum
129 * the running total of the sums. Select a random integer in the range
130 * (1, sum+1), then find the first element greater than or equal to the
131 * number selected. From RFC 2782.
133 * @param list The list of PurpleSrvResponseContainer. This function
134 * removes a node from this list and returns the new list.
135 * @param container_ptr The PurpleSrvResponseContainer that was chosen
136 * will be returned here.
138 static GList *
139 select_random_response(GList *list, PurpleSrvResponseContainer **container_ptr)
141 GList *cur;
142 size_t runningtotal;
143 int r;
145 runningtotal = 0;
146 cur = list;
148 while (cur) {
149 PurpleSrvResponseContainer *container = cur->data;
150 runningtotal += container->response->weight;
151 container->sum = runningtotal;
152 cur = cur->next;
156 * If the running total is greater than 0, pick a number between
157 * 1 and the runningtotal inclusive. (This is not precisely what
158 * the RFC algorithm describes, but we wish to deal with integers
159 * and avoid floats. This is functionally equivalent.)
160 * If running total is 0, then choose r = 0.
162 r = runningtotal ? g_random_int_range(1, runningtotal + 1) : 0;
163 cur = list;
164 while (r > ((PurpleSrvResponseContainer *)cur->data)->sum) {
165 cur = cur->next;
168 /* Set the return parameter and remove cur from the list */
169 *container_ptr = cur->data;
170 return g_list_delete_link(list, cur);
174 * Reorder a GList of PurpleSrvResponses that have the same priority
175 * (aka "pref").
177 static void
178 srv_reorder(GList *list, int num)
180 int i;
181 GList *cur, *container_list = NULL;
182 PurpleSrvResponseContainer *container;
184 if (num < 2)
185 /* Nothing to sort */
186 return;
188 /* First build a list of container structs */
189 for (i = 0, cur = list; i < num; i++, cur = cur->next) {
190 container = g_new(PurpleSrvResponseContainer, 1);
191 container->response = cur->data;
192 container_list = g_list_prepend(container_list, container);
194 container_list = g_list_reverse(container_list);
197 * Re-order the list that was passed in as a parameter. We leave
198 * the list nodes in place, but replace their data pointers.
200 cur = list;
201 while (container_list) {
202 container_list = select_random_response(container_list, &container);
203 cur->data = container->response;
204 g_free(container);
205 cur = cur->next;
210 * Sorts a GList of PurpleSrvResponses according to the
211 * algorithm described in RFC 2782.
213 * @param response GList of PurpleSrvResponse's
214 * @param The original list, resorted
216 static GList *
217 purple_srv_sort(GList *list)
219 int pref, count;
220 GList *cur, *start;
222 if (!list || !list->next) {
223 /* Nothing to sort */
224 return list;
227 list = g_list_sort(list, responsecompare);
229 start = cur = list;
230 count = 1;
231 while (cur) {
232 PurpleSrvResponse *next_response;
233 pref = ((PurpleSrvResponse *)cur->data)->pref;
234 next_response = cur->next ? cur->next->data : NULL;
235 if (!next_response || next_response->pref != pref) {
237 * The 'count' records starting at 'start' all have the same
238 * priority. Sort them by weight.
240 srv_reorder(start, count);
241 start = cur->next;
242 count = 0;
244 count++;
245 cur = cur->next;
248 return list;
251 #ifdef USE_IDN
252 static gboolean
253 dns_str_is_ascii(const char *name)
255 guchar *c;
256 for (c = (guchar *)name; c && *c; ++c) {
257 if (*c > 0x7f)
258 return FALSE;
261 return TRUE;
263 #endif
265 #ifndef _WIN32
266 static void
267 write_to_parent(int in, int out, gconstpointer data, gsize size)
269 const guchar *buf = data;
270 gssize w;
272 do {
273 w = write(out, buf, size);
274 if (w > 0) {
275 buf += w;
276 size -= w;
277 } else if (w < 0 && errno == EINTR) {
278 /* Let's try some more; */
279 w = 1;
281 } while (size > 0 && w > 0);
283 if (size != 0) {
284 /* An error occurred */
285 close(out);
286 close(in);
287 _exit(0);
291 /* Read size bytes to data. Dies if an error occurs. */
292 static void
293 read_from_parent(int in, int out, gpointer data, gsize size)
295 guchar *buf = data;
296 gssize r;
298 do {
299 r = read(in, data, size);
300 if (r > 0) {
301 buf += r;
302 size -= r;
303 } else if (r < 0 && errno == EINTR) {
304 /* Let's try some more; */
305 r = 1;
307 } while (size > 0 && r > 0);
309 if (size != 0) {
310 /* An error occurred */
311 close(out);
312 close(in);
313 _exit(0);
318 G_GNUC_NORETURN static void
319 resolve(int in, int out)
321 GList *ret = NULL;
322 PurpleSrvResponse *srvres;
323 PurpleTxtResponse *txtres;
324 queryans answer;
325 int size, qdcount, ancount;
326 guchar *end, *cp;
327 gchar name[256];
328 guint16 type, dlen, pref, weight, port;
329 PurpleSrvInternalQuery query;
331 #ifdef HAVE_SIGNAL_H
332 purple_restore_default_signal_handlers();
333 #endif
335 read_from_parent(in, out, &query, sizeof(query));
337 size = res_query( query.query, C_IN, query.type, (u_char*)&answer, sizeof( answer));
338 if (size == -1) {
339 write_to_parent(in, out, &(query.type), sizeof(query.type));
340 write_to_parent(in, out, &size, sizeof(size));
341 close(out);
342 close(in);
343 _exit(0);
346 qdcount = ntohs(answer.hdr.qdcount);
347 ancount = ntohs(answer.hdr.ancount);
348 cp = (guchar*)&answer + sizeof(HEADER);
349 end = (guchar*)&answer + size;
351 /* skip over unwanted stuff */
352 while (qdcount-- > 0 && cp < end) {
353 size = dn_expand( (unsigned char*)&answer, end, cp, name, 256);
354 if(size < 0) goto end;
355 cp += size + QFIXEDSZ;
358 while (ancount-- > 0 && cp < end) {
359 size = dn_expand((unsigned char*)&answer, end, cp, name, 256);
360 if(size < 0)
361 goto end;
362 cp += size;
363 GETSHORT(type,cp);
365 /* skip ttl and class since we already know it */
366 cp += 6;
368 GETSHORT(dlen,cp);
369 if (type == T_SRV) {
370 GETSHORT(pref,cp);
372 GETSHORT(weight,cp);
374 GETSHORT(port,cp);
376 size = dn_expand( (unsigned char*)&answer, end, cp, name, 256);
377 if(size < 0 )
378 goto end;
380 cp += size;
382 srvres = g_new0(PurpleSrvResponse, 1);
383 strcpy(srvres->hostname, name);
384 srvres->pref = pref;
385 srvres->port = port;
386 srvres->weight = weight;
388 ret = g_list_prepend(ret, srvres);
389 } else if (type == T_TXT) {
390 txtres = g_new0(PurpleTxtResponse, 1);
391 txtres->content = g_strndup((gchar*)(++cp), dlen-1);
392 ret = g_list_append(ret, txtres);
393 cp += dlen - 1;
394 } else {
395 cp += dlen;
399 end:
400 size = g_list_length(ret);
402 if (query.type == T_SRV)
403 ret = purple_srv_sort(ret);
405 write_to_parent(in, out, &(query.type), sizeof(query.type));
406 write_to_parent(in, out, &size, sizeof(size));
407 while (ret != NULL)
409 if (query.type == T_SRV)
410 write_to_parent(in, out, ret->data, sizeof(PurpleSrvResponse));
411 if (query.type == T_TXT) {
412 PurpleTxtResponse *response = ret->data;
413 gsize l = strlen(response->content) + 1 /* null byte */;
414 write_to_parent(in, out, &l, sizeof(l));
415 write_to_parent(in, out, response->content, l);
418 g_free(ret->data);
419 ret = g_list_remove(ret, ret->data);
422 close(out);
423 close(in);
425 _exit(0);
428 static void
429 resolved(gpointer data, gint source, PurpleInputCondition cond)
431 int size;
432 int type;
433 PurpleSrvQueryData *query_data = (PurpleSrvQueryData*)data;
434 int i;
435 int status;
437 if (read(source, &type, sizeof(type)) == sizeof(type)) {
438 if (read(source, &size, sizeof(size)) == sizeof(size)) {
439 if (size == -1 || size == 0) {
440 if (size == -1) {
441 purple_debug_warning("dnssrv", "res_query returned an error\n");
442 /* Re-read resolv.conf and friends in case DNS servers have changed */
443 res_init();
444 } else
445 purple_debug_info("dnssrv", "Found 0 entries, errno is %i\n", errno);
447 if (type == T_SRV) {
448 PurpleSrvCallback cb = query_data->cb.srv;
449 cb(NULL, 0, query_data->extradata);
450 } else if (type == T_TXT) {
451 PurpleTxtCallback cb = query_data->cb.txt;
452 cb(NULL, query_data->extradata);
453 } else {
454 purple_debug_error("dnssrv", "type unknown of DNS result entry; errno is %i\n", errno);
457 } else if (size) {
458 if (type == T_SRV) {
459 PurpleSrvResponse *res;
460 PurpleSrvResponse *tmp;
461 PurpleSrvCallback cb = query_data->cb.srv;
462 ssize_t red;
463 purple_debug_info("dnssrv","found %d SRV entries\n", size);
464 tmp = res = g_new0(PurpleSrvResponse, size);
465 for (i = 0; i < size; i++) {
466 red = read(source, tmp++, sizeof(PurpleSrvResponse));
467 if (red != sizeof(PurpleSrvResponse)) {
468 purple_debug_error("dnssrv","unable to read srv "
469 "response: %s\n", g_strerror(errno));
470 size = 0;
471 g_free(res);
472 res = NULL;
476 cb(res, size, query_data->extradata);
477 } else if (type == T_TXT) {
478 GList *responses = NULL;
479 PurpleTxtResponse *res;
480 PurpleTxtCallback cb = query_data->cb.txt;
481 ssize_t red;
482 purple_debug_info("dnssrv","found %d TXT entries\n", size);
483 for (i = 0; i < size; i++) {
484 gsize len;
486 red = read(source, &len, sizeof(len));
487 if (red != sizeof(len)) {
488 purple_debug_error("dnssrv","unable to read txt "
489 "response length: %s\n", g_strerror(errno));
490 size = 0;
491 g_list_foreach(responses, (GFunc)purple_txt_response_destroy, NULL);
492 g_list_free(responses);
493 responses = NULL;
494 break;
497 res = g_new0(PurpleTxtResponse, 1);
498 res->content = g_new0(gchar, len);
500 red = read(source, res->content, len);
501 if (red != len) {
502 purple_debug_error("dnssrv","unable to read txt "
503 "response: %s\n", g_strerror(errno));
504 size = 0;
505 purple_txt_response_destroy(res);
506 g_list_foreach(responses, (GFunc)purple_txt_response_destroy, NULL);
507 g_list_free(responses);
508 responses = NULL;
509 break;
511 responses = g_list_prepend(responses, res);
514 responses = g_list_reverse(responses);
515 cb(responses, query_data->extradata);
516 } else {
517 purple_debug_error("dnssrv", "type unknown of DNS result entry; errno is %i\n", errno);
523 waitpid(query_data->pid, &status, 0);
524 purple_srv_cancel(query_data);
527 #else /* _WIN32 */
529 /** The Jabber Server code was inspiration for parts of this. */
531 static gboolean
532 res_main_thread_cb(gpointer data)
534 PurpleSrvResponse *srvres = NULL;
535 PurpleSrvQueryData *query_data = data;
536 if(query_data->error_message != NULL) {
537 purple_debug_error("dnssrv", query_data->error_message);
538 if (query_data->type == DNS_TYPE_SRV) {
539 if (query_data->cb.srv)
540 query_data->cb.srv(srvres, 0, query_data->extradata);
541 } else if (query_data->type == DNS_TYPE_TXT) {
542 if (query_data->cb.txt)
543 query_data->cb.txt(NULL, query_data->extradata);
545 } else {
546 if (query_data->type == DNS_TYPE_SRV) {
547 PurpleSrvResponse *srvres_tmp = NULL;
548 GList *lst = query_data->results;
549 int size = g_list_length(lst);
551 if(query_data->cb.srv && size > 0)
552 srvres_tmp = srvres = g_new0(PurpleSrvResponse, size);
553 while (lst) {
554 PurpleSrvResponse *lstdata = lst->data;
555 lst = g_list_delete_link(lst, lst);
557 if(query_data->cb.srv)
558 memcpy(srvres_tmp++, lstdata, sizeof(PurpleSrvResponse));
559 g_free(lstdata);
562 query_data->results = NULL;
564 purple_debug_info("dnssrv", "found %d SRV entries\n", size);
566 if(query_data->cb.srv) query_data->cb.srv(srvres, size, query_data->extradata);
567 } else if (query_data->type == DNS_TYPE_TXT) {
568 GList *lst = query_data->results;
570 purple_debug_info("dnssrv", "found %d TXT entries\n", g_list_length(lst));
572 if (query_data->cb.txt) {
573 query_data->results = NULL;
574 query_data->cb.txt(lst, query_data->extradata);
576 } else {
577 purple_debug_error("dnssrv", "unknown query type");
581 query_data->resolver = NULL;
582 query_data->handle = 0;
584 purple_srv_cancel(query_data);
586 return FALSE;
589 static gpointer
590 res_thread(gpointer data)
592 PDNS_RECORD dr = NULL;
593 int type;
594 DNS_STATUS ds;
595 PurpleSrvQueryData *query_data = data;
596 type = query_data->type;
597 ds = MyDnsQuery_UTF8(query_data->query, type, DNS_QUERY_STANDARD, NULL, &dr, NULL);
598 if (ds != ERROR_SUCCESS) {
599 gchar *msg = g_win32_error_message(ds);
600 if (type == DNS_TYPE_SRV) {
601 query_data->error_message = g_strdup_printf("Couldn't look up SRV record. %s (%lu).\n", msg, ds);
602 } else if (type == DNS_TYPE_TXT) {
603 query_data->error_message = g_strdup_printf("Couldn't look up TXT record. %s (%lu).\n", msg, ds);
605 g_free(msg);
606 } else {
607 if (type == DNS_TYPE_SRV) {
608 PDNS_RECORD dr_tmp;
609 GList *lst = NULL;
610 DNS_SRV_DATA *srv_data;
611 PurpleSrvResponse *srvres;
613 for (dr_tmp = dr; dr_tmp != NULL; dr_tmp = dr_tmp->pNext) {
614 /* Discard any incorrect entries. I'm not sure if this is necessary */
615 if (dr_tmp->wType != type || strcmp(dr_tmp->pName, query_data->query) != 0) {
616 continue;
619 srv_data = &dr_tmp->Data.SRV;
620 srvres = g_new0(PurpleSrvResponse, 1);
621 strncpy(srvres->hostname, srv_data->pNameTarget, 255);
622 srvres->hostname[255] = '\0';
623 srvres->pref = srv_data->wPriority;
624 srvres->port = srv_data->wPort;
625 srvres->weight = srv_data->wWeight;
627 lst = g_list_prepend(lst, srvres);
630 MyDnsRecordListFree(dr, DnsFreeRecordList);
631 query_data->results = purple_srv_sort(lst);
632 } else if (type == DNS_TYPE_TXT) {
633 PDNS_RECORD dr_tmp;
634 GList *lst = NULL;
635 DNS_TXT_DATA *txt_data;
636 PurpleTxtResponse *txtres;
638 for (dr_tmp = dr; dr_tmp != NULL; dr_tmp = dr_tmp->pNext) {
639 GString *s;
640 int i;
642 /* Discard any incorrect entries. I'm not sure if this is necessary */
643 if (dr_tmp->wType != type || strcmp(dr_tmp->pName, query_data->query) != 0) {
644 continue;
647 txt_data = &dr_tmp->Data.TXT;
648 txtres = g_new0(PurpleTxtResponse, 1);
650 s = g_string_new("");
651 for (i = 0; i < txt_data->dwStringCount; ++i)
652 s = g_string_append(s, txt_data->pStringArray[i]);
653 txtres->content = g_string_free(s, FALSE);
655 lst = g_list_append(lst, txtres);
658 MyDnsRecordListFree(dr, DnsFreeRecordList);
659 query_data->results = lst;
660 } else {
665 /* back to main thread */
666 /* Note: this should *not* be attached to query_data->handle - it will cause leakage */
667 purple_timeout_add(0, res_main_thread_cb, query_data);
669 g_thread_exit(NULL);
670 return NULL;
673 #endif
675 PurpleSrvQueryData *
676 purple_srv_resolve(const char *protocol, const char *transport, const char *domain, PurpleSrvCallback cb, gpointer extradata)
678 char *query;
679 char *hostname;
680 PurpleSrvQueryData *query_data;
681 #ifndef _WIN32
682 PurpleSrvInternalQuery internal_query;
683 int in[2], out[2];
684 int pid;
685 #else
686 GError* err = NULL;
687 static gboolean initialized = FALSE;
688 #endif
690 if (!protocol || !*protocol || !transport || !*transport || !domain || !*domain) {
691 purple_debug_error("dnssrv", "Wrong arguments\n");
692 cb(NULL, 0, extradata);
693 g_return_val_if_reached(NULL);
696 #ifdef USE_IDN
697 if (!dns_str_is_ascii(domain)) {
698 int ret = purple_network_convert_idn_to_ascii(domain, &hostname);
699 if (ret != 0) {
700 purple_debug_error("dnssrv", "IDNA ToASCII failed\n");
701 cb(NULL, 0, extradata);
702 return NULL;
704 } else /* Fallthru is intentional */
705 #endif
706 hostname = g_strdup(domain);
708 query = g_strdup_printf("_%s._%s.%s", protocol, transport, hostname);
709 purple_debug_info("dnssrv","querying SRV record for %s: %s\n", domain,
710 query);
711 g_free(hostname);
713 #ifndef _WIN32
714 if(pipe(in) || pipe(out)) {
715 purple_debug_error("dnssrv", "Could not create pipe\n");
716 g_free(query);
717 cb(NULL, 0, extradata);
718 return NULL;
721 pid = fork();
722 if (pid == -1) {
723 purple_debug_error("dnssrv", "Could not create process!\n");
724 cb(NULL, 0, extradata);
725 g_free(query);
726 return NULL;
729 /* Child */
730 if (pid == 0)
732 g_free(query);
734 close(out[0]);
735 close(in[1]);
736 resolve(in[0], out[1]);
737 /* resolve() does not return */
740 close(out[1]);
741 close(in[0]);
743 internal_query.type = T_SRV;
744 strncpy(internal_query.query, query, 255);
745 internal_query.query[255] = '\0';
747 if (write(in[1], &internal_query, sizeof(internal_query)) < 0)
748 purple_debug_error("dnssrv", "Could not write to SRV resolver\n");
750 query_data = g_new0(PurpleSrvQueryData, 1);
751 query_data->type = T_SRV;
752 query_data->cb.srv = cb;
753 query_data->extradata = extradata;
754 query_data->pid = pid;
755 query_data->fd_out = out[0];
756 query_data->fd_in = in[1];
757 query_data->handle = purple_input_add(out[0], PURPLE_INPUT_READ, resolved, query_data);
759 g_free(query);
761 return query_data;
762 #else
763 if (!initialized) {
764 MyDnsQuery_UTF8 = (void*) wpurple_find_and_loadproc("dnsapi.dll", "DnsQuery_UTF8");
765 MyDnsRecordListFree = (void*) wpurple_find_and_loadproc(
766 "dnsapi.dll", "DnsRecordListFree");
767 initialized = TRUE;
770 query_data = g_new0(PurpleSrvQueryData, 1);
771 query_data->type = DNS_TYPE_SRV;
772 query_data->cb.srv = cb;
773 query_data->query = query;
774 query_data->extradata = extradata;
776 if (!MyDnsQuery_UTF8 || !MyDnsRecordListFree)
777 query_data->error_message = g_strdup("System missing DNS API (Requires W2K+)\n");
778 else {
779 query_data->resolver = g_thread_create(res_thread, query_data, FALSE, &err);
780 if (query_data->resolver == NULL) {
781 query_data->error_message = g_strdup_printf("SRV thread create failure: %s\n", (err && err->message) ? err->message : "");
782 g_error_free(err);
786 /* The query isn't going to happen, so finish the SRV lookup now.
787 * Asynchronously call the callback since stuff may not expect
788 * the callback to be called before this returns */
789 if (query_data->error_message != NULL)
790 query_data->handle = purple_timeout_add(0, res_main_thread_cb, query_data);
792 return query_data;
793 #endif
796 PurpleSrvQueryData *purple_txt_resolve(const char *owner, const char *domain, PurpleTxtCallback cb, gpointer extradata)
798 char *query;
799 char *hostname;
800 PurpleSrvQueryData *query_data;
801 #ifndef _WIN32
802 PurpleSrvInternalQuery internal_query;
803 int in[2], out[2];
804 int pid;
805 #else
806 GError* err = NULL;
807 static gboolean initialized = FALSE;
808 #endif
810 #ifdef USE_IDN
811 if (!dns_str_is_ascii(domain)) {
812 int ret = purple_network_convert_idn_to_ascii(domain, &hostname);
813 if (ret != 0) {
814 purple_debug_error("dnssrv", "IDNA ToASCII failed\n");
815 cb(NULL, extradata);
816 return NULL;
818 } else /* fallthru is intentional */
819 #endif
820 hostname = g_strdup(domain);
822 query = g_strdup_printf("%s.%s", owner, hostname);
823 purple_debug_info("dnssrv","querying TXT record for %s: %s\n", domain,
824 query);
825 g_free(hostname);
827 #ifndef _WIN32
828 if(pipe(in) || pipe(out)) {
829 purple_debug_error("dnssrv", "Could not create pipe\n");
830 g_free(query);
831 cb(NULL, extradata);
832 return NULL;
835 pid = fork();
836 if (pid == -1) {
837 purple_debug_error("dnssrv", "Could not create process!\n");
838 cb(NULL, extradata);
839 g_free(query);
840 return NULL;
843 /* Child */
844 if (pid == 0)
846 g_free(query);
848 close(out[0]);
849 close(in[1]);
850 resolve(in[0], out[1]);
851 /* resolve() does not return */
854 close(out[1]);
855 close(in[0]);
857 internal_query.type = T_TXT;
858 strncpy(internal_query.query, query, 255);
859 internal_query.query[255] = '\0';
861 if (write(in[1], &internal_query, sizeof(internal_query)) < 0)
862 purple_debug_error("dnssrv", "Could not write to TXT resolver\n");
864 query_data = g_new0(PurpleSrvQueryData, 1);
865 query_data->type = T_TXT;
866 query_data->cb.txt = cb;
867 query_data->extradata = extradata;
868 query_data->pid = pid;
869 query_data->fd_out = out[0];
870 query_data->fd_in = in[1];
871 query_data->handle = purple_input_add(out[0], PURPLE_INPUT_READ, resolved, query_data);
873 g_free(query);
875 return query_data;
876 #else
877 if (!initialized) {
878 MyDnsQuery_UTF8 = (void*) wpurple_find_and_loadproc("dnsapi.dll", "DnsQuery_UTF8");
879 MyDnsRecordListFree = (void*) wpurple_find_and_loadproc(
880 "dnsapi.dll", "DnsRecordListFree");
881 initialized = TRUE;
884 query_data = g_new0(PurpleSrvQueryData, 1);
885 query_data->type = DNS_TYPE_TXT;
886 query_data->cb.txt = cb;
887 query_data->query = query;
888 query_data->extradata = extradata;
890 if (!MyDnsQuery_UTF8 || !MyDnsRecordListFree)
891 query_data->error_message = g_strdup("System missing DNS API (Requires W2K+)\n");
892 else {
893 query_data->resolver = g_thread_create(res_thread, query_data, FALSE, &err);
894 if (query_data->resolver == NULL) {
895 query_data->error_message = g_strdup_printf("TXT thread create failure: %s\n", (err && err->message) ? err->message : "");
896 g_error_free(err);
900 /* The query isn't going to happen, so finish the TXT lookup now.
901 * Asynchronously call the callback since stuff may not expect
902 * the callback to be called before this returns */
903 if (query_data->error_message != NULL)
904 query_data->handle = purple_timeout_add(0, res_main_thread_cb, query_data);
906 return query_data;
907 #endif
910 void
911 purple_srv_cancel(PurpleSrvQueryData *query_data)
913 if (query_data->handle > 0)
914 purple_input_remove(query_data->handle);
915 #ifdef _WIN32
916 if (query_data->resolver != NULL)
919 * It's not really possible to kill a thread. So instead we
920 * just set the callback to NULL and let the DNS lookup
921 * finish.
923 query_data->cb.srv = NULL;
924 return;
926 g_free(query_data->query);
927 g_free(query_data->error_message);
928 #else
929 close(query_data->fd_out);
930 close(query_data->fd_in);
931 #endif
932 g_free(query_data);
935 void
936 purple_txt_cancel(PurpleSrvQueryData *query_data)
938 purple_srv_cancel(query_data);
941 const gchar *
942 purple_txt_response_get_content(PurpleTxtResponse *resp)
944 g_return_val_if_fail(resp != NULL, NULL);
946 return resp->content;
949 void purple_txt_response_destroy(PurpleTxtResponse *resp)
951 g_return_if_fail(resp != NULL);
953 g_free(resp->content);
954 g_free(resp);