annotate libgaim/dnssrv.c @ 14849:7cf437793f16

[gaim-migrate @ 17618] "In 2.0.0b4 (as well as in trunk), my_MM.po claims itself to be encoded in iso-8859-1, but it is in fact encoded in utf-8. This is supported by the following: 1. Burmese is not encoded in iso-8859-1 2. If a suitable font is installed, opening the my_MM.po file as utf-8 actually causes Burmese characters to appear" --acli committer: Tailor Script <tailor@pidgin.im>
author Luke Schierer <lschiere@pidgin.im>
date Sun, 29 Oct 2006 14:51:20 +0000
parents ef05f400817f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
1 /**
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
2 * @file dnssrv.c
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
3 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
4 * gaim
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
5 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
6 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
7 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
11 * (at your option) any later version.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
12 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
16 * GNU General Public License for more details.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
17 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
19 * along with this program; if not, write to the Free Software
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
21 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
22
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
23 #include "internal.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
24
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
25 #ifndef _WIN32
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
26 #include <resolv.h>
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
27 #include <arpa/nameser.h>
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
28 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
29 #include <arpa/nameser_compat.h>
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
30 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
31 #ifndef T_SRV
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
32 #define T_SRV 33
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
33 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
34 #else
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
35 #include <windns.h>
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
36 /* Missing from the mingw headers */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
37 #ifndef DNS_TYPE_SRV
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
38 # define DNS_TYPE_SRV 33
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
39 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
40 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
41
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
42 #include "dnssrv.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
43 #include "eventloop.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
44 #include "debug.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
45
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
46 #ifndef _WIN32
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
47 typedef union {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
48 HEADER hdr;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
49 u_char buf[1024];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
50 } queryans;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
51 #else
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
52 static DNS_STATUS WINAPI (*MyDnsQuery_UTF8) (
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
53 PCSTR lpstrName, WORD wType, DWORD fOptions,
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
54 PIP4_ARRAY aipServers, PDNS_RECORD* ppQueryResultsSet,
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
55 PVOID* pReserved) = NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
56 static void WINAPI (*MyDnsRecordListFree) (PDNS_RECORD pRecordList,
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
57 DNS_FREE_TYPE FreeType) = NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
58 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
59
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
60 struct _GaimSrvQueryData {
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
61 GaimSrvCallback cb;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
62 gpointer extradata;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
63 guint handle;
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
64 #ifdef _WIN32
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
65 GThread *resolver;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
66 char *query;
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
67 char *error_message;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
68 GSList *results;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
69 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
70 };
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
71
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
72 static gint
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
73 responsecompare(gconstpointer ar, gconstpointer br)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
74 {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
75 GaimSrvResponse *a = (GaimSrvResponse*)ar;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
76 GaimSrvResponse *b = (GaimSrvResponse*)br;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
77
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
78 if(a->pref == b->pref) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
79 if(a->weight == b->weight)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
80 return 0;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
81 if(a->weight < b->weight)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
82 return -1;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
83 return 1;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
84 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
85 if(a->pref < b->pref)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
86 return -1;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
87 return 1;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
88 }
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
89
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
90 #ifndef _WIN32
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
91
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
92 static void
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
93 resolve(int in, int out)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
94 {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
95 GList *ret = NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
96 GaimSrvResponse *srvres;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
97 queryans answer;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
98 int size;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
99 int qdcount;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
100 int ancount;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
101 guchar *end;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
102 guchar *cp;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
103 gchar name[256];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
104 guint16 type, dlen, pref, weight, port;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
105 gchar query[256];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
106
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
107 if (read(in, query, 256) <= 0)
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
108 _exit(0);
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
109
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
110 size = res_query( query, C_IN, T_SRV, (u_char*)&answer, sizeof( answer));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
111
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
112 qdcount = ntohs(answer.hdr.qdcount);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
113 ancount = ntohs(answer.hdr.ancount);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
114
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
115 cp = (guchar*)&answer + sizeof(HEADER);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
116 end = (guchar*)&answer + size;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
117
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
118 /* skip over unwanted stuff */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
119 while (qdcount-- > 0 && cp < end) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
120 size = dn_expand( (unsigned char*)&answer, end, cp, name, 256);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
121 if(size < 0) goto end;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
122 cp += size + QFIXEDSZ;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
123 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
124
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
125 while (ancount-- > 0 && cp < end) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
126 size = dn_expand((unsigned char*)&answer, end, cp, name, 256);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
127 if(size < 0)
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
128 goto end;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
129
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
130 cp += size;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
131
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
132 GETSHORT(type,cp);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
133
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
134 /* skip ttl and class since we already know it */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
135 cp += 6;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
136
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
137 GETSHORT(dlen,cp);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
138
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
139 if (type == T_SRV) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
140 GETSHORT(pref,cp);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
141
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
142 GETSHORT(weight,cp);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
143
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
144 GETSHORT(port,cp);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
145
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
146 size = dn_expand( (unsigned char*)&answer, end, cp, name, 256);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
147 if(size < 0 )
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
148 goto end;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
149
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
150 cp += size;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
151
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
152 srvres = g_new0(GaimSrvResponse, 1);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
153 strcpy(srvres->hostname, name);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
154 srvres->pref = pref;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
155 srvres->port = port;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
156 srvres->weight = weight;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
157
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
158 ret = g_list_insert_sorted(ret, srvres, responsecompare);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
159 } else {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
160 cp += dlen;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
161 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
162 }
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
163
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
164 end:
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
165 size = g_list_length(ret);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
166 write(out, &size, sizeof(int));
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
167 while (ret != NULL)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
168 {
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
169 write(out, ret->data, sizeof(GaimSrvResponse));
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
170 g_free(ret->data);
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
171 ret = g_list_remove(ret, ret->data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
172 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
173
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
174 _exit(0);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
175 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
176
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
177 static void
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
178 resolved(gpointer data, gint source, GaimInputCondition cond)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
179 {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
180 int size;
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
181 GaimSrvQueryData *query_data = (GaimSrvQueryData*)data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
182 GaimSrvResponse *res;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
183 GaimSrvResponse *tmp;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
184 int i;
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
185 GaimSrvCallback cb = query_data->cb;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
186
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
187 read(source, &size, sizeof(int));
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
188 gaim_debug_info("dnssrv","found %d SRV entries\n", size);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
189 tmp = res = g_new0(GaimSrvResponse, size);
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
190 for (i = 0; i < size; i++) {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
191 read(source, tmp++, sizeof(GaimSrvResponse));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
192 }
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
193
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
194 cb(res, size, query_data->extradata);
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
195
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
196 gaim_srv_cancel(query_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
197 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
198
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
199 #else /* _WIN32 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
200
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
201 /** The Jabber Server code was inspiration for parts of this. */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
202
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
203 static gboolean
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
204 res_main_thread_cb(gpointer data)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
205 {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
206 GaimSrvResponse *srvres = NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
207 int size = 0;
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
208 GaimSrvQueryData *query_data = data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
209
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
210 if(query_data->error_message != NULL)
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
211 gaim_debug_error("dnssrv", query_data->error_message);
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
212 else {
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
213 GaimSrvResponse *srvres_tmp = NULL;
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
214 GSList *lst = query_data->results;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
215
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
216 size = g_slist_length(query_data->results);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
217
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
218 if(query_data->cb)
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
219 srvres_tmp = srvres = g_new0(GaimSrvResponse, size);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
220 while (lst) {
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
221 if(query_data->cb)
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
222 memcpy(srvres_tmp++, lst->data, sizeof(GaimSrvResponse));
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
223 g_free(lst->data);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
224 lst = g_slist_remove(lst, lst->data);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
225 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
226
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
227 query_data->results = NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
228 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
229
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
230 gaim_debug_info("dnssrv", "found %d SRV entries\n", size);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
231
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
232 if(query_data->cb)
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
233 query_data->cb(srvres, size, query_data->extradata);
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
234
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
235 query_data->resolver = NULL;
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
236 query_data->handle = 0;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
237
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
238 gaim_srv_cancel(query_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
239
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
240 return FALSE;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
241 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
242
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
243 static gpointer
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
244 res_thread(gpointer data)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
245 {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
246 PDNS_RECORD dr = NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
247 int type = DNS_TYPE_SRV;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
248 DNS_STATUS ds;
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
249 GaimSrvQueryData *query_data = data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
250
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
251 ds = MyDnsQuery_UTF8(query_data->query, type, DNS_QUERY_STANDARD, NULL, &dr, NULL);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
252 if (ds != ERROR_SUCCESS) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
253 gchar *msg = g_win32_error_message(ds);
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
254 query_data->error_message = g_strdup_printf("Couldn't look up SRV record. %s (%lu).\n", msg, ds);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
255 g_free(msg);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
256 } else {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
257 PDNS_RECORD dr_tmp;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
258 GSList *lst = NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
259 DNS_SRV_DATA *srv_data;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
260 GaimSrvResponse *srvres;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
261
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
262 for (dr_tmp = dr; dr_tmp != NULL; dr_tmp = dr_tmp->pNext) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
263 /* Discard any incorrect entries. I'm not sure if this is necessary */
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
264 if (dr_tmp->wType != type || strcmp(dr_tmp->pName, query_data->query) != 0) {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
265 continue;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
266 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
267
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
268 srv_data = &dr_tmp->Data.SRV;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
269 srvres = g_new0(GaimSrvResponse, 1);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
270 strncpy(srvres->hostname, srv_data->pNameTarget, 255);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
271 srvres->hostname[255] = '\0';
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
272 srvres->pref = srv_data->wPriority;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
273 srvres->port = srv_data->wPort;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
274 srvres->weight = srv_data->wWeight;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
275
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
276 lst = g_slist_insert_sorted(lst, srvres, responsecompare);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
277 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
278
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
279 MyDnsRecordListFree(dr, DnsFreeRecordList);
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
280 query_data->results = lst;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
281 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
282
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
283 /* back to main thread */
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
284 /* Note: this should *not* be attached to query_data->handle - it will cause leakage */
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
285 g_idle_add(res_main_thread_cb, query_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
286
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
287 g_thread_exit(NULL);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
288 return NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
289 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
290
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
291 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
292
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
293 GaimSrvQueryData *
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
294 gaim_srv_resolve(const char *protocol, const char *transport, const char *domain, GaimSrvCallback cb, gpointer extradata)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
295 {
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
296 char *query;
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
297 GaimSrvQueryData *query_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
298 #ifndef _WIN32
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
299 int in[2], out[2];
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
300 int pid;
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
301 #else
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
302 GError* err = NULL;
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
303 static gboolean initialized = FALSE;
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
304 #endif
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
305
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
306 query = g_strdup_printf("_%s._%s.%s", protocol, transport, domain);
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
307 gaim_debug_info("dnssrv","querying SRV record for %s\n", query);
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
308
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
309 #ifndef _WIN32
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
310 if(pipe(in) || pipe(out)) {
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
311 gaim_debug_error("dnssrv", "Could not create pipe\n");
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
312 g_free(query);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
313 cb(NULL, 0, extradata);
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
314 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
315 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
316
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
317 pid = fork();
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
318 if (pid == -1) {
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
319 gaim_debug_error("dnssrv", "Could not create process!\n");
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
320 cb(NULL, 0, extradata);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
321 g_free(query);
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
322 return NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
323 }
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
324
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
325 /* Child */
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
326 if (pid == 0)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
327 {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
328 close(out[0]);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
329 close(in[1]);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
330 resolve(in[0], out[1]);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
331 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
332
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
333 close(out[1]);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
334 close(in[0]);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
335
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
336 if (write(in[1], query, strlen(query)+1) < 0)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
337 gaim_debug_error("dnssrv", "Could not write to SRV resolver\n");
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
338
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
339 query_data = g_new0(GaimSrvQueryData, 1);
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
340 query_data->cb = cb;
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
341 query_data->extradata = extradata;
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
342 query_data->handle = gaim_input_add(out[0], GAIM_INPUT_READ, resolved, query_data);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
343
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
344 g_free(query);
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
345
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
346 return query_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
347 #else
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
348 if (!initialized) {
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
349 MyDnsQuery_UTF8 = (void*) wgaim_find_and_loadproc("dnsapi.dll", "DnsQuery_UTF8");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
350 MyDnsRecordListFree = (void*) wgaim_find_and_loadproc(
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
351 "dnsapi.dll", "DnsRecordListFree");
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
352 initialized = TRUE;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
353 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
354
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
355 query_data = g_new0(GaimSrvQueryData, 1);
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
356 query_data->cb = cb;
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
357 query_data->query = query;
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
358 query_data->extradata = extradata;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
359
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
360 if (!MyDnsQuery_UTF8 || !MyDnsRecordListFree) {
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
361 query_data->error_message = g_strdup_printf("System missing DNS API (Requires W2K+)\n");
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
362
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
363 /* Asynchronously call the callback since stuff may not expect
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
364 * the callback to be called before this returns */
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
365 query_data->handle = g_idle_add(res_main_thread_cb, query_data);
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
366
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
367 return query_data;
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
368 }
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
369
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
370 query_data->resolver = g_thread_create(res_thread, query_data, FALSE, &err);
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
371 if (query_data->resolver == NULL)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
372 {
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
373 query_data->error_message = g_strdup_printf("SRV thread create failure: %s\n", err ? err->message : "");
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
374 g_error_free(err);
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
375
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
376 /* Asynchronously call the callback since stuff may not expect
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
377 * the callback to be called before this returns */
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
378 query_data->handle = g_idle_add(res_main_thread_cb, query_data);
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
379
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
380 return query_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
381 }
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
382
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
383 return query_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
384 #endif
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
385 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
386
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
387 void
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
388 gaim_srv_cancel(GaimSrvQueryData *query_data)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
389 {
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
390 if (query_data->handle > 0)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
391 gaim_input_remove(query_data->handle);
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
392 #ifdef _WIN32
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
393 if (query_data->resolver != NULL)
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
394 {
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
395 /*
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
396 * It's not really possible to kill a thread. So instead we
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
397 * just set the callback to NULL and let the DNS lookup
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
398 * finish.
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
399 */
14312
ef05f400817f [gaim-migrate @ 17002]
Daniel Atallah <daniel.atallah@gmail.com>
parents: 14308
diff changeset
400 query_data->cb = NULL;
14308
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
401 return;
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
402 }
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
403 g_free(query_data->query);
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
404 g_free(query_data->error_message);
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
405 #endif
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
406 g_free(query_data);
9ad313800b19 [gaim-migrate @ 16998]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
407 }