annotate libgaim/dnsquery.c @ 14375:f597127368de

[gaim-migrate @ 17081] Changing Hilbert to Hil, as per the patch submitter's request. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Tue, 29 Aug 2006 21:09:43 +0000
parents fdf65b672b87
children 92eb7a040663
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);
14358
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
90 else
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
91 {
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
92 /*
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
93 * Callback is a required parameter, but it can get set to
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
94 * NULL if we cancel a thread-based DNS lookup. So we need
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
95 * to free hosts.
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
96 */
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
97 while (hosts != NULL)
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
98 {
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
99 hosts = g_slist_remove(hosts, hosts->data);
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
100 g_free(hosts->data);
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
101 hosts = g_slist_remove(hosts, hosts->data);
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
102 }
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
103 }
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
104
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
105 gaim_dnsquery_destroy(query_data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
106 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
107
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
108 static void
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
109 gaim_dnsquery_failed(GaimDnsQueryData *query_data, const gchar *error_message)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
110 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
111 gaim_debug_info("dnsquery", "%s\n", error_message);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
112 if (query_data->callback != NULL)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
113 query_data->callback(NULL, query_data->data, error_message);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
114 gaim_dnsquery_destroy(query_data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
115 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
116
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
117 #if defined(__unix__) || defined(__APPLE__)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
118
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
119 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
120 * Unix!
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
121 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
122
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
123 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
124 * Begin the DNS resolver child process functions.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
125 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
126 #ifdef HAVE_SIGNAL_H
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
127 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
128 trap_gdb_bug()
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 const char *message =
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
131 "Gaim's DNS child got a SIGTRAP signal.\n"
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
132 "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
133 "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
134 "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
135 "check cope_with_gdb_brokenness() in dnsquery.c.\n\n"
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
136 "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
137 fputs("\n* * *\n",stderr);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
138 fputs(message,stderr);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
139 fputs("* * *\n\n",stderr);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
140 execlp("xmessage","xmessage","-center", message, NULL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
141 _exit(1);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
142 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
143 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
144
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
145 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
146 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
147 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
148 dns_params_t dns_params;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
149 const size_t zero = 0;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
150 int rc;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
151 #ifdef HAVE_GETADDRINFO
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
152 struct addrinfo hints, *res, *tmp;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
153 char servname[20];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
154 #else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
155 struct sockaddr_in sin;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
156 const size_t addrlen = sizeof(sin);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
157 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
158
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
159 #ifdef HAVE_SIGNAL_H
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
160 signal(SIGHUP, SIG_DFL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
161 signal(SIGINT, SIG_DFL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
162 signal(SIGQUIT, SIG_DFL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
163 signal(SIGCHLD, SIG_DFL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
164 signal(SIGTERM, SIG_DFL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
165 signal(SIGTRAP, trap_gdb_bug);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
166 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
167
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
168 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
169 * We resolve 1 host name for each iteration of this
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
170 * while loop.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
171 *
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
172 * The top half of this reads in the hostname and port
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
173 * number from the socket with our parent. The bottom
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
174 * half of this resolves the IP (blocking) and sends
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
175 * the result back to our parent, when finished.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
176 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
177 while (1) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
178 const char ch = 'Y';
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
179 fd_set fds;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
180 struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 };
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
181 FD_ZERO(&fds);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
182 FD_SET(child_in, &fds);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
183 rc = select(child_in + 1, &fds, NULL, NULL, &tv);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
184 if (!rc) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
185 if (show_debug)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
186 printf("dns[%d]: nobody needs me... =(\n", getpid());
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
187 break;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
188 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
189 rc = read(child_in, &dns_params, sizeof(dns_params_t));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
190 if (rc < 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
191 perror("read()");
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
192 break;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
193 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
194 if (rc == 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
195 if (show_debug)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
196 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
197 _exit(0);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
198 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
199 if (dns_params.hostname[0] == '\0') {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
200 printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
201 _exit(1);
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 /* Tell our parent that we read the data successfully */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
204 write(child_out, &ch, sizeof(ch));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
205
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
206 /* We have the hostname and port, now resolve the IP */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
207
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
208 #ifdef HAVE_GETADDRINFO
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
209 g_snprintf(servname, sizeof(servname), "%d", dns_params.port);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
210 memset(&hints, 0, sizeof(hints));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
211
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
212 /* This is only used to convert a service
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
213 * name to a port number. As we know we are
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
214 * passing a number already, we know this
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
215 * value will not be really used by the C
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
216 * library.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
217 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
218 hints.ai_socktype = SOCK_STREAM;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
219 rc = getaddrinfo(dns_params.hostname, servname, &hints, &res);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
220 write(child_out, &rc, sizeof(rc));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
221 if (rc != 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
222 close(child_out);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
223 if (show_debug)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
224 printf("dns[%d] Error: getaddrinfo returned %d\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
225 getpid(), rc);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
226 dns_params.hostname[0] = '\0';
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
227 continue;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
228 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
229 tmp = res;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
230 while (res) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
231 size_t ai_addrlen = res->ai_addrlen;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
232 write(child_out, &ai_addrlen, sizeof(ai_addrlen));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
233 write(child_out, res->ai_addr, res->ai_addrlen);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
234 res = res->ai_next;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
235 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
236 freeaddrinfo(tmp);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
237 write(child_out, &zero, sizeof(zero));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
238 #else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
239 if (!inet_aton(dns_params.hostname, &sin.sin_addr)) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
240 struct hostent *hp;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
241 if (!(hp = gethostbyname(dns_params.hostname))) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
242 write(child_out, &h_errno, sizeof(int));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
243 close(child_out);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
244 if (show_debug)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
245 printf("DNS Error: %d\n", h_errno);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
246 _exit(0);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
247 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
248 memset(&sin, 0, sizeof(struct sockaddr_in));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
249 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
250 sin.sin_family = hp->h_addrtype;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
251 } else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
252 sin.sin_family = AF_INET;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
253
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
254 sin.sin_port = htons(dns_params.port);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
255 write(child_out, &addrlen, sizeof(addrlen));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
256 write(child_out, &sin, addrlen);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
257 write(child_out, &zero, sizeof(zero));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
258 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
259 dns_params.hostname[0] = '\0';
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
260 }
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 close(child_out);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
263 close(child_in);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
264
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
265 _exit(0);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
266 }
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 * End the DNS resolver child process functions.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
269 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
270
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
271 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
272 * Begin the functions for dealing with the DNS child processes.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
273 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
274 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
275 cope_with_gdb_brokenness()
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 #ifdef __linux__
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
278 static gboolean already_done = FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
279 char s[256], e[512];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
280 int n;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
281 pid_t ppid;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
282
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
283 if(already_done)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
284 return;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
285 already_done = TRUE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
286 ppid = getppid();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
287 snprintf(s, sizeof(s), "/proc/%d/exe", ppid);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
288 n = readlink(s, e, sizeof(e));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
289 if(n < 0)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
290 return;
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 e[MIN(n,sizeof(e)-1)] = '\0';
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
293
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
294 if(strstr(e,"gdb")) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
295 gaim_debug_info("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
296 "Debugger detected, performing useless query...\n");
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
297 gethostbyname("x.x.x.x.x");
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
298 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
299 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
300 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
301
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
302 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
303 gaim_dnsquery_resolver_destroy(GaimDnsQueryResolverProcess *resolver)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
304 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
305 g_return_if_fail(resolver != NULL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
306
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 * 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
309 * doesn't matter if this fails, because children will expire on
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
310 * their own after a few seconds.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
311 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
312 if (resolver->dns_pid > 0)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
313 kill(resolver->dns_pid, SIGKILL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
314
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
315 if (resolver->inpa != 0)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
316 gaim_input_remove(resolver->inpa);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
317
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
318 close(resolver->fd_in);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
319 close(resolver->fd_out);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
320
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
321 g_free(resolver);
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 number_of_dns_children--;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
324 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
325
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
326 static GaimDnsQueryResolverProcess *
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
327 gaim_dnsquery_resolver_new(gboolean show_debug)
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 GaimDnsQueryResolverProcess *resolver;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
330 int child_out[2], child_in[2];
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 /* Create pipes for communicating with the child process */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
333 if (pipe(child_out) || pipe(child_in)) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
334 gaim_debug_error("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
335 "Could not create pipes: %s\n", strerror(errno));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
336 return NULL;
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
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
339 resolver = g_new(GaimDnsQueryResolverProcess, 1);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
340 resolver->inpa = 0;
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 cope_with_gdb_brokenness();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
343
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
344 /* "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
345 resolver->dns_pid = fork();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
346
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
347 /* If we are the child process... */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
348 if (resolver->dns_pid == 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
349 /* 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
350 close(child_out[0]);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
351 close(child_in[1]);
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 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
354 /* 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
355 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
356
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
357 /* 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
358 close(child_out[1]);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
359 close(child_in[0]);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
360 if (resolver->dns_pid == -1) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
361 gaim_debug_error("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
362 "Could not create child process for DNS: %s\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
363 strerror(errno));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
364 gaim_dnsquery_resolver_destroy(resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
365 return NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
366 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
367
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
368 resolver->fd_out = child_out[0];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
369 resolver->fd_in = child_in[1];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
370 number_of_dns_children++;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
371 gaim_debug_info("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
372 "Created new DNS child %d, there are now %d children.\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
373 resolver->dns_pid, number_of_dns_children);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
374
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
375 return resolver;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
376 }
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 /**
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
379 * @return TRUE if the request was sent succesfully. FALSE
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
380 * if the request could not be sent. This isn't
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
381 * necessarily an error. If the child has expired,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
382 * for example, we won't be able to send the message.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
383 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
384 static gboolean
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
385 send_dns_request_to_child(GaimDnsQueryData *query_data,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
386 GaimDnsQueryResolverProcess *resolver)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
387 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
388 pid_t pid;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
389 dns_params_t dns_params;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
390 int rc;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
391 char ch;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
392
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
393 /* 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
394 * exited, or it might return an error if it exited "long
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
395 * enough" ago that it has already been reaped; in either
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
396 * instance, we can't use it. */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
397 pid = waitpid(resolver->dns_pid, NULL, WNOHANG);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
398 if (pid > 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
399 gaim_debug_warning("dns", "DNS child %d no longer exists\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
400 resolver->dns_pid);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
401 gaim_dnsquery_resolver_destroy(resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
402 return FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
403 } else if (pid < 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
404 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
405 resolver->dns_pid, strerror(errno));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
406 gaim_dnsquery_resolver_destroy(resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
407 return FALSE;
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
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
410 /* Copy the hostname and port into a single data structure */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
411 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
412 dns_params.hostname[sizeof(dns_params.hostname) - 1] = '\0';
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
413 dns_params.port = query_data->port;
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 /* Send the data structure to the child */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
416 rc = write(resolver->fd_in, &dns_params, sizeof(dns_params));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
417 if (rc < 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
418 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
419 resolver->dns_pid, strerror(errno));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
420 gaim_dnsquery_resolver_destroy(resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
421 return FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
422 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
423
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
424 g_return_val_if_fail(rc == sizeof(dns_params), -1);
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 /* Did you hear me? (This avoids some race conditions) */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
427 rc = read(resolver->fd_out, &ch, sizeof(ch));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
428 if (rc != 1 || ch != 'Y')
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 gaim_debug_warning("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
431 "DNS child %d not responding. Killing it!\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
432 resolver->dns_pid);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
433 gaim_dnsquery_resolver_destroy(resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
434 return FALSE;
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
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
437 gaim_debug_info("dns",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
438 "Successfully sent DNS request to child %d\n",
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
439 resolver->dns_pid);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
440
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
441 query_data->resolver = resolver;
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 return TRUE;
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 static void host_resolved(gpointer data, gint source, GaimInputCondition cond);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
447
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
448 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
449 handle_next_queued_request()
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 GaimDnsQueryData *query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
452 GaimDnsQueryResolverProcess *resolver;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
453
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
454 if ((queued_requests == NULL) || (g_queue_is_empty(queued_requests)))
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
455 /* No more DNS queries, yay! */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
456 return;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
457
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
458 query_data = g_queue_pop_head(queued_requests);
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 * 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
462 * 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
463 * set to the GaimDnsQueryResolverProcess. Otherwise, resolver
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
464 * 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
465 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
466 while (free_dns_children != NULL)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
467 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
468 resolver = free_dns_children->data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
469 free_dns_children = g_slist_remove(free_dns_children, resolver);
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 if (send_dns_request_to_child(query_data, resolver))
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
472 /* We found an acceptable child, yay */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
473 break;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
474 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
475
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
476 /* We need to create a new DNS request child */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
477 if (query_data->resolver == NULL)
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 if (number_of_dns_children >= MAX_DNS_CHILDREN)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
480 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
481 /* Apparently all our children are busy */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
482 g_queue_push_head(queued_requests, query_data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
483 return;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
484 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
485
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
486 resolver = gaim_dnsquery_resolver_new(gaim_debug_is_enabled());
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
487 if (resolver == NULL)
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 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
490 return;
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 if (!send_dns_request_to_child(query_data, resolver))
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
493 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
494 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
495 return;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
496 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
497 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
498
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
499 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
500 GAIM_INPUT_READ, host_resolved, query_data);
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
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 * End the functions for dealing with the DNS child processes.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
505 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
506
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
507 static void
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
508 host_resolved(gpointer data, gint source, GaimInputCondition cond)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
509 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
510 GaimDnsQueryData *query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
511 int rc, err;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
512 GSList *hosts = NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
513 struct sockaddr *addr = NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
514 size_t addrlen;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
515 char message[1024];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
516
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
517 query_data = data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
518
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
519 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
520 gaim_input_remove(query_data->resolver->inpa);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
521 query_data->resolver->inpa = 0;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
522
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
523 rc = read(query_data->resolver->fd_out, &err, sizeof(err));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
524 if ((rc == 4) && (err != 0))
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
525 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
526 #ifdef HAVE_GETADDRINFO
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
527 g_snprintf(message, sizeof(message), _("Error resolving %s: %s"),
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
528 query_data->hostname, gai_strerror(err));
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 g_snprintf(message, sizeof(message), _("Error resolving %s: %d"),
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
531 query_data->hostname, err);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
532 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
533 gaim_dnsquery_failed(query_data, message);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
534
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
535 } else if (rc > 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
536 /* Success! */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
537 while (rc > 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
538 rc = read(query_data->resolver->fd_out, &addrlen, sizeof(addrlen));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
539 if (rc > 0 && addrlen > 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
540 addr = g_malloc(addrlen);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
541 rc = read(query_data->resolver->fd_out, addr, addrlen);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
542 hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
543 hosts = g_slist_append(hosts, addr);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
544 } else {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
545 break;
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 /* wait4(resolver->dns_pid, NULL, WNOHANG, NULL); */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
549 gaim_dnsquery_resolved(query_data, hosts);
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 } else if (rc == -1) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
552 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
553 gaim_dnsquery_failed(query_data, message);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
554
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
555 } else if (rc == 0) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
556 g_snprintf(message, sizeof(message), _("EOF while reading from resolver process"));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
557 gaim_dnsquery_failed(query_data, message);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
558 }
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 handle_next_queued_request();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
561 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
562
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
563 static gboolean
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
564 resolve_host(gpointer data)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
565 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
566 GaimDnsQueryData *query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
567
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
568 query_data = data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
569 query_data->timeout = 0;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
570
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
571 handle_next_queued_request();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
572
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
573 return FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
574 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
575
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
576 GaimDnsQueryData *
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
577 gaim_dnsquery_a(const char *hostname, int port,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
578 GaimDnsQueryConnectFunction callback, gpointer data)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
579 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
580 GaimDnsQueryData *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 g_return_val_if_fail(hostname != NULL, NULL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
583 g_return_val_if_fail(port != 0, NULL);
14358
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
584 g_return_val_if_fail(callback != NULL, NULL);
14287
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 query_data = g_new(GaimDnsQueryData, 1);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
587 query_data->hostname = g_strdup(hostname);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
588 g_strstrip(query_data->hostname);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
589 query_data->port = port;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
590 query_data->callback = callback;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
591 query_data->data = data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
592 query_data->resolver = NULL;
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 if (!queued_requests)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
595 queued_requests = g_queue_new();
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
596 g_queue_push_tail(queued_requests, query_data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
597
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
598 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
599
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
600 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
601
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
602 return query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
603 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
604
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
605 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
606
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
607 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
608 * Windows!
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
609 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
610
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
611 static gboolean
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
612 dns_main_thread_cb(gpointer data)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
613 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
614 GaimDnsQueryData *query_data;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
615
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
616 query_data = data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
617
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
618 if (query_data->error_message != NULL)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
619 gaim_dnsquery_failed(query_data, query_data->error_message);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
620 else
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
621 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
622 GSList *hosts;
14244
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
623
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
624 /* 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
625 hosts = query_data->hosts;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
626 query_data->hosts = NULL;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
627 gaim_dnsquery_resolved(query_data, hosts);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
628 }
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
629
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
630 return FALSE;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
631 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
632
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
633 static gpointer
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
634 dns_thread(gpointer data)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
635 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
636 GaimDnsQueryData *query_data;
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
637 #ifdef HAVE_GETADDRINFO
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
638 int rc;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
639 struct addrinfo hints, *res, *tmp;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
640 char servname[20];
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
641 #else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
642 struct sockaddr_in sin;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
643 struct hostent *hp;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
644 #endif
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
645
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
646 query_data = data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
647
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
648 #ifdef HAVE_GETADDRINFO
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
649 g_snprintf(servname, sizeof(servname), "%d", query_data->port);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
650 memset(&hints,0,sizeof(hints));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
651
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
652 /*
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
653 * This is only used to convert a service
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
654 * name to a port number. As we know we are
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
655 * passing a number already, we know this
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
656 * value will not be really used by the C
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
657 * library.
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
658 */
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
659 hints.ai_socktype = SOCK_STREAM;
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
660 if ((rc = getaddrinfo(query_data->hostname, servname, &hints, &res)) == 0) {
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
661 tmp = res;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
662 while(res) {
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
663 query_data->hosts = g_slist_append(query_data->hosts,
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
664 GSIZE_TO_POINTER(res->ai_addrlen));
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
665 query_data->hosts = g_slist_append(query_data->hosts,
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
666 g_memdup(res->ai_addr, res->ai_addrlen));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
667 res = res->ai_next;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
668 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
669 freeaddrinfo(tmp);
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
670 } else {
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
671 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
672 }
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
673 #else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
674 if ((hp = gethostbyname(query_data->hostname))) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
675 memset(&sin, 0, sizeof(struct sockaddr_in));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
676 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
677 sin.sin_family = hp->h_addrtype;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
678 sin.sin_port = htons(query_data->port);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
679
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
680 query_data->hosts = g_slist_append(query_data->hosts,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
681 GSIZE_TO_POINTER(sizeof(sin)));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
682 query_data->hosts = g_slist_append(query_data->hosts,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
683 g_memdup(&sin, sizeof(sin)));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
684 } else {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
685 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
686 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
687 #endif
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
688
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
689 /* back to main thread */
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
690 g_idle_add(dns_main_thread_cb, query_data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
691
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
692 return 0;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
693 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
694
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
695 static gboolean
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
696 resolve_host(gpointer data)
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
697 {
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
698 GaimDnsQueryData *query_data;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
699 struct sockaddr_in sin;
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
700 GError *err = NULL;
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
701
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
702 query_data = data;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
703 query_data->timeout = 0;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
704
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
705 if (inet_aton(query_data->hostname, &sin.sin_addr))
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
706 {
14244
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
707 /*
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
708 * The given "hostname" is actually an IP address, so we
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
709 * don't need to do anything.
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
710 */
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
711 GSList *hosts = NULL;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
712 sin.sin_family = AF_INET;
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
713 sin.sin_port = htons(query_data->port);
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
714 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
715 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
716 gaim_dnsquery_resolved(query_data, hosts);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
717 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
718 else
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
719 {
14244
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
720 /*
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
721 * Spin off a separate thread to perform the DNS lookup so
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
722 * that we don't block the UI.
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
723 */
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
724 query_data->resolver = g_thread_create(dns_thread,
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
725 query_data, FALSE, &err);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
726 if (query_data->resolver == NULL)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
727 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
728 char message[1024];
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
729 g_snprintf(message, sizeof(message), _("Thread creation failure: %s"),
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
730 err ? err->message : _("Unknown reason"));
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
731 g_error_free(err);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
732 gaim_dnsquery_failed(query_data, message);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
733 }
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
734 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
735
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
736 return FALSE;
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
737 }
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
738
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
739 GaimDnsQueryData *
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
740 gaim_dnsquery_a(const char *hostname, int port,
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
741 GaimDnsQueryConnectFunction callback, gpointer data)
14192
60b1bc8dbf37 [gaim-migrate @ 16863]
Evan Schoenberg <evan.s@dreskin.net>
parents:
diff changeset
742 {
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
743 GaimDnsQueryData *query_data;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
744
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
745 g_return_val_if_fail(hostname != NULL, NULL);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
746 g_return_val_if_fail(port != 0, NULL);
14358
fdf65b672b87 [gaim-migrate @ 17064]
Mark Doliner <mark@kingant.net>
parents: 14287
diff changeset
747 g_return_val_if_fail(callback != NULL, NULL);
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
748
14244
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
749 gaim_debug_info("dnsquery", "Performing DNS lookup for %s\n", hostname);
935f8b258d1b [gaim-migrate @ 16926]
Mark Doliner <mark@kingant.net>
parents: 14238
diff changeset
750
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
751 query_data = g_new(GaimDnsQueryData, 1);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
752 query_data->hostname = g_strdup(hostname);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
753 g_strstrip(query_data->hostname);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
754 query_data->port = port;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
755 query_data->callback = callback;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
756 query_data->data = data;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
757 query_data->error_message = NULL;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
758 query_data->hosts = NULL;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
759
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
760 /* Don't call the callback before returning */
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
761 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
762
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
763 return query_data;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
764 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
765
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
766 #else /* not __unix__ or __APPLE__ or _WIN32 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
767
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
768 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
769 * 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
770 * fail-safe name resolution code, which is blocking.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
771 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
772
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
773 static gboolean
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
774 resolve_host(gpointer data)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
775 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
776 GaimDnsQueryData *query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
777 struct sockaddr_in sin;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
778 GSList *hosts = NULL;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
779
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
780 query_data = data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
781 query_data->timeout = 0;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
782
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
783 if (!inet_aton(query_data->hostname, &sin.sin_addr)) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
784 struct hostent *hp;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
785 if(!(hp = gethostbyname(query_data->hostname))) {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
786 char message[1024];
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
787 g_snprintf(message, sizeof(message), _("Error resolving %s: %d"),
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
788 query_data->hostname, h_errno);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
789 gaim_dnsquery_failed(query_data, message);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
790 return FALSE;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
791 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
792 memset(&sin, 0, sizeof(struct sockaddr_in));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
793 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
794 sin.sin_family = hp->h_addrtype;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
795 } else
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
796 sin.sin_family = AF_INET;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
797 sin.sin_port = htons(query_data->port);
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 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
800 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
801
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
802 gaim_dnsquery_resolved(query_data, hosts);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
803
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
804 return FALSE;
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
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
807 GaimDnsQueryData *
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
808 gaim_dnsquery_a(const char *hostname, int port,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
809 GaimDnsQueryConnectFunction callback, gpointer 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 GaimDnsQueryData *query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
812
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
813 g_return_val_if_fail(hostname != NULL, NULL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
814 g_return_val_if_fail(port != 0, NULL);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
815
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
816 query_data = g_new(GaimDnsQueryData, 1);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
817 query_data->hostname = g_strdup(hostname);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
818 g_strstrip(query_data->hostname);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
819 query_data->port = port;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
820 query_data->callback = callback;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
821 query_data->data = data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
822
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
823 /* Don't call the callback before returning */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
824 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
825
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
826 return query_data;
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
827 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
828
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
829 #endif /* not __unix__ or __APPLE__ or _WIN32 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
830
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
831 void
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
832 gaim_dnsquery_destroy(GaimDnsQueryData *query_data)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
833 {
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
834 #if defined(__unix__) || defined(__APPLE__)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
835 if (query_data->resolver != NULL)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
836 /*
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
837 * Ideally we would tell our resolver child to stop resolving
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
838 * 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
839 * linked list. However, it's hard to tell children stuff,
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
840 * they just don't listen.
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
841 */
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
842 gaim_dnsquery_resolver_destroy(query_data->resolver);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
843 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
844 if (query_data->resolver != NULL)
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 /*
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
847 * 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
848 * just set the callback to NULL and let the DNS lookup
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
849 * finish.
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
850 */
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
851 query_data->callback = NULL;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
852 return;
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
853 }
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 while (query_data->hosts != NULL)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
856 {
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
857 /* Discard the length... */
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
858 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
859 /* Free the address... */
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
860 g_free(query_data->hosts->data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
861 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
862 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
863 g_free(query_data->error_message);
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
864 #endif
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
865
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
866 if (query_data->timeout > 0)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
867 gaim_timeout_remove(query_data->timeout);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
868
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
869 g_free(query_data->hostname);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
870 g_free(query_data);
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
871 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
872
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
873 void
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
874 gaim_dnsquery_init(void)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
875 {
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
876 #ifdef _WIN32
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
877 if (!g_thread_supported())
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
878 g_thread_init(NULL);
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
879 #endif
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
880 }
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
881
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
882 void
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
883 gaim_dnsquery_uninit(void)
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
884 {
14287
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
885 #if defined(__unix__) || defined(__APPLE__)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
886 while (free_dns_children != NULL)
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
887 {
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
888 gaim_dnsquery_resolver_destroy(free_dns_children->data);
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
889 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
890 }
e01a8316b08b [gaim-migrate @ 16974]
Mark Doliner <mark@kingant.net>
parents: 14263
diff changeset
891 #endif
14238
f189327b9968 [gaim-migrate @ 16920]
Mark Doliner <mark@kingant.net>
parents: 14192
diff changeset
892 }