annotate libgaim/dnsquery.c @ 14340:2aaadbd919c4

[gaim-migrate @ 17037] Add QQ to the list of protocols in the About box. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Sat, 26 Aug 2006 06:26:25 +0000
parents e01a8316b08b
children fdf65b672b87
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 dnsquery.c DNS query API
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
3 * @ingroup core
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
4 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
5 * gaim
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
6 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
7 * Gaim is the legal property of its developers, whose names are too numerous
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
8 * to list here. Please refer to the COPYRIGHT file distributed with this
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
9 * source distribution.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
10 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
11 * 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
12 * 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
13 * the Free Software Foundation; either version 2 of the License, or
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
14 * (at your option) any later version.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
15 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
16 * 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
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
19 * GNU General Public License for more details.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
20 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
21 * 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
22 * along with this program; if not, write to the Free Software
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
23 * 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
24 *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
25 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
26
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
27 #include "internal.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
28 #include "debug.h"
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
29 #include "dnsquery.h"
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
30 #include "notify.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
31 #include "prefs.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
32 #include "util.h"
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
33
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
34 /**************************************************************************
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
35 * DNS query API
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
36 **************************************************************************/
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
37
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
38 typedef struct _GaimDnsQueryResolverProcess GaimDnsQueryResolverProcess;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
39
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
40 struct _GaimDnsQueryData {
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
41 char *hostname;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
42 int port;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
43 GaimDnsQueryConnectFunction callback;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
44 gpointer data;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
45 guint timeout;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
46
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
47 #if defined(__unix__) || defined(__APPLE__)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
48 GaimDnsQueryResolverProcess *resolver;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
49 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
50 GThread *resolver;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
51 GSList *hosts;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
52 gchar *error_message;
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
53 #endif
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
54 };
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
55
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
56 #if defined(__unix__) || defined(__APPLE__)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
57
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
58 #define MAX_DNS_CHILDREN 4
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
59
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
60 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
61 * This structure keeps a reference to a child resolver process.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
62 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
63 struct _GaimDnsQueryResolverProcess {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
64 guint inpa;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
65 int fd_in, fd_out;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
66 pid_t dns_pid;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
67 };
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
68
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
69 static GSList *free_dns_children = NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
70 static GQueue *queued_requests = NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
71
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
72 static int number_of_dns_children = 0;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
73
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
74 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
75 * This is a convenience struct used to pass data to
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
76 * the child resolver process.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
77 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
78 typedef struct {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
79 char hostname[512];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
80 int port;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
81 } dns_params_t;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
82 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
83
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
84 static void
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
85 gaim_dnsquery_resolved(GaimDnsQueryData *query_data, GSList *hosts)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
86 {
14244
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
87 gaim_debug_info("dnsquery", "IP resolved for %s\n", query_data->hostname);
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
88 if (query_data->callback != NULL)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
89 query_data->callback(hosts, query_data->data, NULL);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
90 gaim_dnsquery_destroy(query_data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
91 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
92
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
93 static void
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
94 gaim_dnsquery_failed(GaimDnsQueryData *query_data, const gchar *error_message)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
95 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
96 gaim_debug_info("dnsquery", "%s\n", error_message);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
97 if (query_data->callback != NULL)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
98 query_data->callback(NULL, query_data->data, error_message);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
99 gaim_dnsquery_destroy(query_data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
100 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
101
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
102 #if defined(__unix__) || defined(__APPLE__)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
103
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
104 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
105 * Unix!
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
106 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
107
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
108 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
109 * Begin the DNS resolver child process functions.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
110 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
111 #ifdef HAVE_SIGNAL_H
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
112 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
113 trap_gdb_bug()
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
114 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
115 const char *message =
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
116 "Gaim's DNS child got a SIGTRAP signal.\n"
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
117 "This can be caused by trying to run gaim inside gdb.\n"
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
118 "There is a known gdb bug which prevents this. Supposedly gaim\n"
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
119 "should have detected you were using gdb and used an ugly hack,\n"
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
120 "check cope_with_gdb_brokenness() in dnsquery.c.\n\n"
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
121 "For more info about this bug, see http://sources.redhat.com/ml/gdb/2001-07/msg00349.html\n";
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
122 fputs("\n* * *\n",stderr);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
123 fputs(message,stderr);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
124 fputs("* * *\n\n",stderr);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
125 execlp("xmessage","xmessage","-center", message, NULL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
126 _exit(1);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
127 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
128 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
129
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
130 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
131 gaim_dnsquery_resolver_run(int child_out, int child_in, gboolean show_debug)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
132 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
133 dns_params_t dns_params;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
134 const size_t zero = 0;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
135 int rc;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
136 #ifdef HAVE_GETADDRINFO
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
137 struct addrinfo hints, *res, *tmp;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
138 char servname[20];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
139 #else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
140 struct sockaddr_in sin;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
141 const size_t addrlen = sizeof(sin);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
142 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
143
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
144 #ifdef HAVE_SIGNAL_H
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
145 signal(SIGHUP, SIG_DFL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
146 signal(SIGINT, SIG_DFL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
147 signal(SIGQUIT, SIG_DFL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
148 signal(SIGCHLD, SIG_DFL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
149 signal(SIGTERM, SIG_DFL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
150 signal(SIGTRAP, trap_gdb_bug);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
151 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
152
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
153 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
154 * We resolve 1 host name for each iteration of this
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
155 * while loop.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
156 *
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
157 * The top half of this reads in the hostname and port
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
158 * number from the socket with our parent. The bottom
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
159 * half of this resolves the IP (blocking) and sends
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
160 * the result back to our parent, when finished.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
161 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
162 while (1) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
163 const char ch = 'Y';
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
164 fd_set fds;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
165 struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 };
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
166 FD_ZERO(&fds);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
167 FD_SET(child_in, &fds);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
168 rc = select(child_in + 1, &fds, NULL, NULL, &tv);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
169 if (!rc) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
170 if (show_debug)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
171 printf("dns[%d]: nobody needs me... =(\n", getpid());
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
172 break;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
173 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
174 rc = read(child_in, &dns_params, sizeof(dns_params_t));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
175 if (rc < 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
176 perror("read()");
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
177 break;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
178 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
179 if (rc == 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
180 if (show_debug)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
181 printf("dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid());
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
182 _exit(0);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
183 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
184 if (dns_params.hostname[0] == '\0') {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
185 printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
186 _exit(1);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
187 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
188 /* Tell our parent that we read the data successfully */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
189 write(child_out, &ch, sizeof(ch));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
190
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
191 /* We have the hostname and port, now resolve the IP */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
192
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
193 #ifdef HAVE_GETADDRINFO
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
194 g_snprintf(servname, sizeof(servname), "%d", dns_params.port);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
195 memset(&hints, 0, sizeof(hints));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
196
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
197 /* This is only used to convert a service
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
198 * name to a port number. As we know we are
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
199 * passing a number already, we know this
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
200 * value will not be really used by the C
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
201 * library.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
202 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
203 hints.ai_socktype = SOCK_STREAM;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
204 rc = getaddrinfo(dns_params.hostname, servname, &hints, &res);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
205 write(child_out, &rc, sizeof(rc));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
206 if (rc != 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
207 close(child_out);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
208 if (show_debug)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
209 printf("dns[%d] Error: getaddrinfo returned %d\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
210 getpid(), rc);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
211 dns_params.hostname[0] = '\0';
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
212 continue;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
213 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
214 tmp = res;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
215 while (res) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
216 size_t ai_addrlen = res->ai_addrlen;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
217 write(child_out, &ai_addrlen, sizeof(ai_addrlen));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
218 write(child_out, res->ai_addr, res->ai_addrlen);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
219 res = res->ai_next;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
220 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
221 freeaddrinfo(tmp);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
222 write(child_out, &zero, sizeof(zero));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
223 #else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
224 if (!inet_aton(dns_params.hostname, &sin.sin_addr)) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
225 struct hostent *hp;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
226 if (!(hp = gethostbyname(dns_params.hostname))) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
227 write(child_out, &h_errno, sizeof(int));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
228 close(child_out);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
229 if (show_debug)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
230 printf("DNS Error: %d\n", h_errno);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
231 _exit(0);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
232 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
233 memset(&sin, 0, sizeof(struct sockaddr_in));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
234 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
235 sin.sin_family = hp->h_addrtype;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
236 } else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
237 sin.sin_family = AF_INET;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
238
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
239 sin.sin_port = htons(dns_params.port);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
240 write(child_out, &addrlen, sizeof(addrlen));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
241 write(child_out, &sin, addrlen);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
242 write(child_out, &zero, sizeof(zero));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
243 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
244 dns_params.hostname[0] = '\0';
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
245 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
246
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
247 close(child_out);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
248 close(child_in);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
249
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
250 _exit(0);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
251 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
252 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
253 * End the DNS resolver child process functions.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
254 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
255
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
256 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
257 * Begin the functions for dealing with the DNS child processes.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
258 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
259 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
260 cope_with_gdb_brokenness()
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
261 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
262 #ifdef __linux__
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
263 static gboolean already_done = FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
264 char s[256], e[512];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
265 int n;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
266 pid_t ppid;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
267
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
268 if(already_done)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
269 return;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
270 already_done = TRUE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
271 ppid = getppid();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
272 snprintf(s, sizeof(s), "/proc/%d/exe", ppid);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
273 n = readlink(s, e, sizeof(e));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
274 if(n < 0)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
275 return;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
276
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
277 e[MIN(n,sizeof(e)-1)] = '\0';
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
278
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
279 if(strstr(e,"gdb")) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
280 gaim_debug_info("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
281 "Debugger detected, performing useless query...\n");
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
282 gethostbyname("x.x.x.x.x");
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
283 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
284 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
285 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
286
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
287 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
288 gaim_dnsquery_resolver_destroy(GaimDnsQueryResolverProcess *resolver)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
289 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
290 g_return_if_fail(resolver != NULL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
291
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
292 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
293 * We might as well attempt to kill our child process. It really
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
294 * doesn't matter if this fails, because children will expire on
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
295 * their own after a few seconds.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
296 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
297 if (resolver->dns_pid > 0)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
298 kill(resolver->dns_pid, SIGKILL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
299
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
300 if (resolver->inpa != 0)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
301 gaim_input_remove(resolver->inpa);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
302
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
303 close(resolver->fd_in);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
304 close(resolver->fd_out);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
305
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
306 g_free(resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
307
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
308 number_of_dns_children--;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
309 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
310
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
311 static GaimDnsQueryResolverProcess *
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
312 gaim_dnsquery_resolver_new(gboolean show_debug)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
313 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
314 GaimDnsQueryResolverProcess *resolver;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
315 int child_out[2], child_in[2];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
316
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
317 /* Create pipes for communicating with the child process */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
318 if (pipe(child_out) || pipe(child_in)) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
319 gaim_debug_error("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
320 "Could not create pipes: %s\n", strerror(errno));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
321 return NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
322 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
323
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
324 resolver = g_new(GaimDnsQueryResolverProcess, 1);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
325 resolver->inpa = 0;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
326
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
327 cope_with_gdb_brokenness();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
328
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
329 /* "Go fork and multiply." --Tommy Caldwell (Emily's dad, not the climber) */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
330 resolver->dns_pid = fork();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
331
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
332 /* If we are the child process... */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
333 if (resolver->dns_pid == 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
334 /* We should not access the parent's side of the pipes, so close them */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
335 close(child_out[0]);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
336 close(child_in[1]);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
337
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
338 gaim_dnsquery_resolver_run(child_out[1], child_in[0], show_debug);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
339 /* The thread calls _exit() rather than returning, so we never get here */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
340 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
341
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
342 /* We should not access the child's side of the pipes, so close them */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
343 close(child_out[1]);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
344 close(child_in[0]);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
345 if (resolver->dns_pid == -1) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
346 gaim_debug_error("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
347 "Could not create child process for DNS: %s\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
348 strerror(errno));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
349 gaim_dnsquery_resolver_destroy(resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
350 return NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
351 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
352
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
353 resolver->fd_out = child_out[0];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
354 resolver->fd_in = child_in[1];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
355 number_of_dns_children++;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
356 gaim_debug_info("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
357 "Created new DNS child %d, there are now %d children.\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
358 resolver->dns_pid, number_of_dns_children);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
359
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
360 return resolver;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
361 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
362
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
363 /**
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
364 * @return TRUE if the request was sent succesfully. FALSE
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
365 * if the request could not be sent. This isn't
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
366 * necessarily an error. If the child has expired,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
367 * for example, we won't be able to send the message.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
368 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
369 static gboolean
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
370 send_dns_request_to_child(GaimDnsQueryData *query_data,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
371 GaimDnsQueryResolverProcess *resolver)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
372 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
373 pid_t pid;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
374 dns_params_t dns_params;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
375 int rc;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
376 char ch;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
377
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
378 /* This waitpid might return the child's PID if it has recently
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
379 * exited, or it might return an error if it exited "long
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
380 * enough" ago that it has already been reaped; in either
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
381 * instance, we can't use it. */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
382 pid = waitpid(resolver->dns_pid, NULL, WNOHANG);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
383 if (pid > 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
384 gaim_debug_warning("dns", "DNS child %d no longer exists\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
385 resolver->dns_pid);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
386 gaim_dnsquery_resolver_destroy(resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
387 return FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
388 } else if (pid < 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
389 gaim_debug_warning("dns", "Wait for DNS child %d failed: %s\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
390 resolver->dns_pid, strerror(errno));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
391 gaim_dnsquery_resolver_destroy(resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
392 return FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
393 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
394
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
395 /* Copy the hostname and port into a single data structure */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
396 strncpy(dns_params.hostname, query_data->hostname, sizeof(dns_params.hostname) - 1);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
397 dns_params.hostname[sizeof(dns_params.hostname) - 1] = '\0';
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
398 dns_params.port = query_data->port;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
399
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
400 /* Send the data structure to the child */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
401 rc = write(resolver->fd_in, &dns_params, sizeof(dns_params));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
402 if (rc < 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
403 gaim_debug_error("dns", "Unable to write to DNS child %d: %d\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
404 resolver->dns_pid, strerror(errno));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
405 gaim_dnsquery_resolver_destroy(resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
406 return FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
407 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
408
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
409 g_return_val_if_fail(rc == sizeof(dns_params), -1);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
410
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
411 /* Did you hear me? (This avoids some race conditions) */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
412 rc = read(resolver->fd_out, &ch, sizeof(ch));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
413 if (rc != 1 || ch != 'Y')
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
414 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
415 gaim_debug_warning("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
416 "DNS child %d not responding. Killing it!\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
417 resolver->dns_pid);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
418 gaim_dnsquery_resolver_destroy(resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
419 return FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
420 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
421
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
422 gaim_debug_info("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
423 "Successfully sent DNS request to child %d\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
424 resolver->dns_pid);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
425
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
426 query_data->resolver = resolver;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
427
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
428 return TRUE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
429 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
430
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
431 static void host_resolved(gpointer data, gint source, GaimInputCondition cond);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
432
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
433 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
434 handle_next_queued_request()
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
435 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
436 GaimDnsQueryData *query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
437 GaimDnsQueryResolverProcess *resolver;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
438
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
439 if ((queued_requests == NULL) || (g_queue_is_empty(queued_requests)))
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
440 /* No more DNS queries, yay! */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
441 return;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
442
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
443 query_data = g_queue_pop_head(queued_requests);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
444
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
445 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
446 * If we have any children, attempt to have them perform the DNS
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
447 * query. If we're able to send the query then resolver will be
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
448 * set to the GaimDnsQueryResolverProcess. Otherwise, resolver
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
449 * will be NULL and we'll need to create a new DNS request child.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
450 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
451 while (free_dns_children != NULL)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
452 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
453 resolver = free_dns_children->data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
454 free_dns_children = g_slist_remove(free_dns_children, resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
455
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
456 if (send_dns_request_to_child(query_data, resolver))
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
457 /* We found an acceptable child, yay */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
458 break;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
459 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
460
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
461 /* We need to create a new DNS request child */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
462 if (query_data->resolver == NULL)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
463 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
464 if (number_of_dns_children >= MAX_DNS_CHILDREN)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
465 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
466 /* Apparently all our children are busy */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
467 g_queue_push_head(queued_requests, query_data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
468 return;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
469 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
470
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
471 resolver = gaim_dnsquery_resolver_new(gaim_debug_is_enabled());
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
472 if (resolver == NULL)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
473 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
474 gaim_dnsquery_failed(query_data, _("Unable to create new resolver process\n"));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
475 return;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
476 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
477 if (!send_dns_request_to_child(query_data, resolver))
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
478 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
479 gaim_dnsquery_failed(query_data, _("Unable to send request to resolver process\n"));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
480 return;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
481 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
482 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
483
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
484 query_data->resolver->inpa = gaim_input_add(query_data->resolver->fd_out,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
485 GAIM_INPUT_READ, host_resolved, query_data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
486 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
487
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
488 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
489 * End the functions for dealing with the DNS child processes.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
490 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
491
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
492 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
493 host_resolved(gpointer data, gint source, GaimInputCondition cond)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
494 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
495 GaimDnsQueryData *query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
496 int rc, err;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
497 GSList *hosts = NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
498 struct sockaddr *addr = NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
499 size_t addrlen;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
500 char message[1024];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
501
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
502 query_data = data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
503
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
504 gaim_debug_info("dns", "Got response for '%s'\n", query_data->hostname);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
505 gaim_input_remove(query_data->resolver->inpa);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
506 query_data->resolver->inpa = 0;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
507
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
508 rc = read(query_data->resolver->fd_out, &err, sizeof(err));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
509 if ((rc == 4) && (err != 0))
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
510 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
511 #ifdef HAVE_GETADDRINFO
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
512 g_snprintf(message, sizeof(message), _("Error resolving %s: %s"),
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
513 query_data->hostname, gai_strerror(err));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
514 #else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
515 g_snprintf(message, sizeof(message), _("Error resolving %s: %d"),
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
516 query_data->hostname, err);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
517 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
518 gaim_dnsquery_failed(query_data, message);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
519
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
520 } else if (rc > 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
521 /* Success! */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
522 while (rc > 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
523 rc = read(query_data->resolver->fd_out, &addrlen, sizeof(addrlen));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
524 if (rc > 0 && addrlen > 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
525 addr = g_malloc(addrlen);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
526 rc = read(query_data->resolver->fd_out, addr, addrlen);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
527 hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
528 hosts = g_slist_append(hosts, addr);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
529 } else {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
530 break;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
531 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
532 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
533 /* wait4(resolver->dns_pid, NULL, WNOHANG, NULL); */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
534 gaim_dnsquery_resolved(query_data, hosts);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
535
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
536 } else if (rc == -1) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
537 g_snprintf(message, sizeof(message), _("Error reading from resolver process: %s"), strerror(errno));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
538 gaim_dnsquery_failed(query_data, message);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
539
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
540 } else if (rc == 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
541 g_snprintf(message, sizeof(message), _("EOF while reading from resolver process"));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
542 gaim_dnsquery_failed(query_data, message);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
543 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
544
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
545 handle_next_queued_request();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
546 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
547
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
548 static gboolean
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
549 resolve_host(gpointer data)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
550 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
551 GaimDnsQueryData *query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
552
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
553 query_data = data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
554 query_data->timeout = 0;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
555
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
556 handle_next_queued_request();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
557
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
558 return FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
559 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
560
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
561 GaimDnsQueryData *
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
562 gaim_dnsquery_a(const char *hostname, int port,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
563 GaimDnsQueryConnectFunction callback, gpointer data)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
564 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
565 GaimDnsQueryData *query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
566
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
567 g_return_val_if_fail(hostname != NULL, NULL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
568 g_return_val_if_fail(port != 0, NULL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
569
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
570 query_data = g_new(GaimDnsQueryData, 1);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
571 query_data->hostname = g_strdup(hostname);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
572 g_strstrip(query_data->hostname);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
573 query_data->port = port;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
574 query_data->callback = callback;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
575 query_data->data = data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
576 query_data->resolver = NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
577
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
578 if (!queued_requests)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
579 queued_requests = g_queue_new();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
580 g_queue_push_tail(queued_requests, query_data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
581
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
582 gaim_debug_info("dns", "DNS query for '%s' queued\n", query_data->hostname);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
583
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
584 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
585
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
586 return query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
587 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
588
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
589 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
590
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
591 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
592 * Windows!
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
593 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
594
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
595 static gboolean
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
596 dns_main_thread_cb(gpointer data)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
597 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
598 GaimDnsQueryData *query_data;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
599
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
600 query_data = data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
601
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
602 if (query_data->error_message != NULL)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
603 gaim_dnsquery_failed(query_data, query_data->error_message);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
604 else
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
605 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
606 GSList *hosts;
14244
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
607
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
608 /* We don't want gaim_dns_query_resolved() to free(hosts) */
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
609 hosts = query_data->hosts;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
610 query_data->hosts = NULL;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
611 gaim_dnsquery_resolved(query_data, hosts);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
612 }
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
613
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
614 return FALSE;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
615 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
616
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
617 static gpointer
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
618 dns_thread(gpointer data)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
619 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
620 GaimDnsQueryData *query_data;
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
621 #ifdef HAVE_GETADDRINFO
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
622 int rc;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
623 struct addrinfo hints, *res, *tmp;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
624 char servname[20];
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
625 #else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
626 struct sockaddr_in sin;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
627 struct hostent *hp;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
628 #endif
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
629
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
630 query_data = data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
631
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
632 #ifdef HAVE_GETADDRINFO
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
633 g_snprintf(servname, sizeof(servname), "%d", query_data->port);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
634 memset(&hints,0,sizeof(hints));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
635
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
636 /*
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
637 * This is only used to convert a service
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
638 * name to a port number. As we know we are
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
639 * passing a number already, we know this
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
640 * value will not be really used by the C
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
641 * library.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
642 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
643 hints.ai_socktype = SOCK_STREAM;
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
644 if ((rc = getaddrinfo(query_data->hostname, servname, &hints, &res)) == 0) {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
645 tmp = res;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
646 while(res) {
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
647 query_data->hosts = g_slist_append(query_data->hosts,
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
648 GSIZE_TO_POINTER(res->ai_addrlen));
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
649 query_data->hosts = g_slist_append(query_data->hosts,
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
650 g_memdup(res->ai_addr, res->ai_addrlen));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
651 res = res->ai_next;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
652 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
653 freeaddrinfo(tmp);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
654 } else {
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
655 query_data->error_message = g_strdup_printf(_("Error resolving %s: %s"), query_data->hostname, gai_strerror(rc));
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
656 }
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
657 #else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
658 if ((hp = gethostbyname(query_data->hostname))) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
659 memset(&sin, 0, sizeof(struct sockaddr_in));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
660 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
661 sin.sin_family = hp->h_addrtype;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
662 sin.sin_port = htons(query_data->port);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
663
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
664 query_data->hosts = g_slist_append(query_data->hosts,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
665 GSIZE_TO_POINTER(sizeof(sin)));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
666 query_data->hosts = g_slist_append(query_data->hosts,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
667 g_memdup(&sin, sizeof(sin)));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
668 } else {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
669 query_data->error_message = g_strdup_printf(_("Error resolving %s: %d"), query_data->hostname, h_errno);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
670 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
671 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
672
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
673 /* back to main thread */
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
674 g_idle_add(dns_main_thread_cb, query_data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
675
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
676 return 0;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
677 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
678
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
679 static gboolean
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
680 resolve_host(gpointer data)
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
681 {
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
682 GaimDnsQueryData *query_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
683 struct sockaddr_in sin;
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
684 GError *err = NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
685
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
686 query_data = data;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
687 query_data->timeout = 0;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
688
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
689 if (inet_aton(query_data->hostname, &sin.sin_addr))
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
690 {
14244
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
691 /*
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
692 * The given "hostname" is actually an IP address, so we
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
693 * don't need to do anything.
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
694 */
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
695 GSList *hosts = NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
696 sin.sin_family = AF_INET;
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
697 sin.sin_port = htons(query_data->port);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
698 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
699 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
700 gaim_dnsquery_resolved(query_data, hosts);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
701 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
702 else
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
703 {
14244
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
704 /*
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
705 * Spin off a separate thread to perform the DNS lookup so
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
706 * that we don't block the UI.
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
707 */
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
708 query_data->resolver = g_thread_create(dns_thread,
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
709 query_data, FALSE, &err);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
710 if (query_data->resolver == NULL)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
711 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
712 char message[1024];
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
713 g_snprintf(message, sizeof(message), _("Thread creation failure: %s"),
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
714 err ? err->message : _("Unknown reason"));
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
715 g_error_free(err);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
716 gaim_dnsquery_failed(query_data, message);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
717 }
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
718 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
719
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
720 return FALSE;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
721 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
722
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
723 GaimDnsQueryData *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
724 gaim_dnsquery_a(const char *hostname, int port,
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
725 GaimDnsQueryConnectFunction callback, gpointer data)
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
726 {
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
727 GaimDnsQueryData *query_data;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
728
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
729 g_return_val_if_fail(hostname != NULL, NULL);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
730 g_return_val_if_fail(port != 0, NULL);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
731
14244
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
732 gaim_debug_info("dnsquery", "Performing DNS lookup for %s\n", hostname);
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
733
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
734 query_data = g_new(GaimDnsQueryData, 1);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
735 query_data->hostname = g_strdup(hostname);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
736 g_strstrip(query_data->hostname);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
737 query_data->port = port;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
738 query_data->callback = callback;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
739 query_data->data = data;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
740 query_data->error_message = NULL;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
741 query_data->hosts = NULL;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
742
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
743 /* Don't call the callback before returning */
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
744 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
745
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
746 return query_data;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
747 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
748
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
749 #else /* not __unix__ or __APPLE__ or _WIN32 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
750
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
751 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
752 * We weren't able to do anything fancier above, so use the
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
753 * fail-safe name resolution code, which is blocking.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
754 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
755
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
756 static gboolean
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
757 resolve_host(gpointer data)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
758 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
759 GaimDnsQueryData *query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
760 struct sockaddr_in sin;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
761 GSList *hosts = NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
762
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
763 query_data = data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
764 query_data->timeout = 0;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
765
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
766 if (!inet_aton(query_data->hostname, &sin.sin_addr)) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
767 struct hostent *hp;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
768 if(!(hp = gethostbyname(query_data->hostname))) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
769 char message[1024];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
770 g_snprintf(message, sizeof(message), _("Error resolving %s: %d"),
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
771 query_data->hostname, h_errno);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
772 gaim_dnsquery_failed(query_data, message);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
773 return FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
774 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
775 memset(&sin, 0, sizeof(struct sockaddr_in));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
776 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
777 sin.sin_family = hp->h_addrtype;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
778 } else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
779 sin.sin_family = AF_INET;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
780 sin.sin_port = htons(query_data->port);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
781
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
782 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
783 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
784
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
785 gaim_dnsquery_resolved(query_data, hosts);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
786
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
787 return FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
788 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
789
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
790 GaimDnsQueryData *
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
791 gaim_dnsquery_a(const char *hostname, int port,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
792 GaimDnsQueryConnectFunction callback, gpointer data)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
793 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
794 GaimDnsQueryData *query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
795
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
796 g_return_val_if_fail(hostname != NULL, NULL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
797 g_return_val_if_fail(port != 0, NULL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
798
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
799 query_data = g_new(GaimDnsQueryData, 1);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
800 query_data->hostname = g_strdup(hostname);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
801 g_strstrip(query_data->hostname);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
802 query_data->port = port;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
803 query_data->callback = callback;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
804 query_data->data = data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
805
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
806 /* Don't call the callback before returning */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
807 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
808
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
809 return query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
810 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
811
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
812 #endif /* not __unix__ or __APPLE__ or _WIN32 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
813
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
814 void
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
815 gaim_dnsquery_destroy(GaimDnsQueryData *query_data)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
816 {
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
817 #if defined(__unix__) || defined(__APPLE__)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
818 if (query_data->resolver != NULL)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
819 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
820 * Ideally we would tell our resolver child to stop resolving
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
821 * shit and then we would add it back to the free_dns_children
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
822 * linked list. However, it's hard to tell children stuff,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
823 * they just don't listen.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
824 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
825 gaim_dnsquery_resolver_destroy(query_data->resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
826 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
827 if (query_data->resolver != NULL)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
828 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
829 /*
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
830 * It's not really possible to kill a thread. So instead we
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
831 * just set the callback to NULL and let the DNS lookup
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
832 * finish.
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
833 */
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
834 query_data->callback = NULL;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
835 return;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
836 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
837
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
838 while (query_data->hosts != NULL)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
839 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
840 /* Discard the length... */
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
841 query_data->hosts = g_slist_remove(query_data->hosts, query_data->hosts->data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
842 /* Free the address... */
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
843 g_free(query_data->hosts->data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
844 query_data->hosts = g_slist_remove(query_data->hosts, query_data->hosts->data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
845 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
846 g_free(query_data->error_message);
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
847 #endif
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
848
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
849 if (query_data->timeout > 0)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
850 gaim_timeout_remove(query_data->timeout);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
851
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
852 g_free(query_data->hostname);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
853 g_free(query_data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
854 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
855
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
856 void
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
857 gaim_dnsquery_init(void)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
858 {
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
859 #ifdef _WIN32
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
860 if (!g_thread_supported())
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
861 g_thread_init(NULL);
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
862 #endif
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
863 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
864
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
865 void
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
866 gaim_dnsquery_uninit(void)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
867 {
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
868 #if defined(__unix__) || defined(__APPLE__)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
869 while (free_dns_children != NULL)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
870 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
871 gaim_dnsquery_resolver_destroy(free_dns_children->data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
872 free_dns_children = g_slist_remove(free_dns_children, free_dns_children->data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
873 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
874 #endif
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
875 }