comparison libpurple/dnsquery.c @ 22146:3c814c64a507

merge of '4b5e932e01a84088124dee9791809545cdc9240b' and 'e4081b0fea9b2e91db9a532aa729cfc1c078f287'
author Sean Egan <seanegan@gmail.com>
date Thu, 17 Jan 2008 20:44:48 +0000
parents bfe1854bbf87
children 3225c99785b8
comparison
equal deleted inserted replaced
22083:2fb026d3f42b 22146:3c814c64a507
157 execlp("xmessage","xmessage","-center", message, NULL); 157 execlp("xmessage","xmessage","-center", message, NULL);
158 _exit(1); 158 _exit(1);
159 } 159 }
160 #endif 160 #endif
161 161
162 static void
163 write_to_parent(int fd, const void *buf, size_t count)
164 {
165 ssize_t written;
166
167 written = write(fd, buf, count);
168 if (written != count) {
169 if (written < 0)
170 fprintf(stderr, "dns[%d]: Error writing data to "
171 "parent: %s\n", getpid(), strerror(errno));
172 else
173 fprintf(stderr, "dns[%d]: Error: Tried to write %"
174 G_GSIZE_FORMAT " bytes to parent but instead "
175 "wrote %" G_GSIZE_FORMAT " bytes\n",
176 getpid(), count, written);
177 }
178 }
179
162 G_GNUC_NORETURN static void 180 G_GNUC_NORETURN static void
163 purple_dnsquery_resolver_run(int child_out, int child_in, gboolean show_debug) 181 purple_dnsquery_resolver_run(int child_out, int child_in, gboolean show_debug)
164 { 182 {
165 dns_params_t dns_params; 183 dns_params_t dns_params;
166 const size_t zero = 0; 184 const size_t zero = 0;
199 printf("dns[%d]: nobody needs me... =(\n", getpid()); 217 printf("dns[%d]: nobody needs me... =(\n", getpid());
200 break; 218 break;
201 } 219 }
202 rc = read(child_in, &dns_params, sizeof(dns_params_t)); 220 rc = read(child_in, &dns_params, sizeof(dns_params_t));
203 if (rc < 0) { 221 if (rc < 0) {
204 perror("read()"); 222 fprintf(stderr, "dns[%d]: Error: Could not read dns_params: "
223 "%s\n", getpid(), strerror(errno));
205 break; 224 break;
206 } 225 }
207 if (rc == 0) { 226 if (rc == 0) {
208 if (show_debug) 227 if (show_debug)
209 printf("dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid()); 228 printf("dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid());
210 _exit(0); 229 _exit(0);
211 } 230 }
212 if (dns_params.hostname[0] == '\0') { 231 if (dns_params.hostname[0] == '\0') {
213 printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port); 232 fprintf(stderr, "dns[%d]: Error: Parent requested resolution "
233 "of an empty hostname (port = %d)!!!\n", getpid(),
234 dns_params.port);
214 _exit(1); 235 _exit(1);
215 } 236 }
216 /* Tell our parent that we read the data successfully */ 237 /* Tell our parent that we read the data successfully */
217 write(child_out, &ch, sizeof(ch)); 238 write_to_parent(child_out, &ch, sizeof(ch));
218 239
219 /* We have the hostname and port, now resolve the IP */ 240 /* We have the hostname and port, now resolve the IP */
220 241
221 #ifdef HAVE_GETADDRINFO 242 #ifdef HAVE_GETADDRINFO
222 g_snprintf(servname, sizeof(servname), "%d", dns_params.port); 243 g_snprintf(servname, sizeof(servname), "%d", dns_params.port);
228 * value will not be really used by the C 249 * value will not be really used by the C
229 * library. 250 * library.
230 */ 251 */
231 hints.ai_socktype = SOCK_STREAM; 252 hints.ai_socktype = SOCK_STREAM;
232 rc = getaddrinfo(dns_params.hostname, servname, &hints, &res); 253 rc = getaddrinfo(dns_params.hostname, servname, &hints, &res);
233 write(child_out, &rc, sizeof(rc)); 254 write_to_parent(child_out, &rc, sizeof(rc));
234 if (rc != 0) { 255 if (rc != 0) {
235 close(child_out); 256 close(child_out);
236 if (show_debug) 257 if (show_debug)
237 printf("dns[%d] Error: getaddrinfo returned %d\n", 258 printf("dns[%d] Error: getaddrinfo returned %d\n",
238 getpid(), rc); 259 getpid(), rc);
240 continue; 261 continue;
241 } 262 }
242 tmp = res; 263 tmp = res;
243 while (res) { 264 while (res) {
244 size_t ai_addrlen = res->ai_addrlen; 265 size_t ai_addrlen = res->ai_addrlen;
245 write(child_out, &ai_addrlen, sizeof(ai_addrlen)); 266 write_to_parent(child_out, &ai_addrlen, sizeof(ai_addrlen));
246 write(child_out, res->ai_addr, res->ai_addrlen); 267 write_to_parent(child_out, res->ai_addr, res->ai_addrlen);
247 res = res->ai_next; 268 res = res->ai_next;
248 } 269 }
249 freeaddrinfo(tmp); 270 freeaddrinfo(tmp);
250 write(child_out, &zero, sizeof(zero)); 271 write_to_parent(child_out, &zero, sizeof(zero));
251 #else 272 #else
252 if (!inet_aton(dns_params.hostname, &sin.sin_addr)) { 273 if (!inet_aton(dns_params.hostname, &sin.sin_addr)) {
253 struct hostent *hp; 274 struct hostent *hp;
254 if (!(hp = gethostbyname(dns_params.hostname))) { 275 if (!(hp = gethostbyname(dns_params.hostname))) {
255 write(child_out, &h_errno, sizeof(int)); 276 write_to_parent(child_out, &h_errno, sizeof(int));
256 close(child_out); 277 close(child_out);
257 if (show_debug) 278 if (show_debug)
258 printf("DNS Error: %d\n", h_errno); 279 printf("DNS Error: %d\n", h_errno);
259 _exit(0); 280 _exit(0);
260 } 281 }
263 sin.sin_family = hp->h_addrtype; 284 sin.sin_family = hp->h_addrtype;
264 } else 285 } else
265 sin.sin_family = AF_INET; 286 sin.sin_family = AF_INET;
266 287
267 sin.sin_port = htons(dns_params.port); 288 sin.sin_port = htons(dns_params.port);
268 write(child_out, &zero, sizeof(zero)); 289 write_to_parent(child_out, &zero, sizeof(zero));
269 write(child_out, &addrlen, sizeof(addrlen)); 290 write_to_parent(child_out, &addrlen, sizeof(addrlen));
270 write(child_out, &sin, addrlen); 291 write_to_parent(child_out, &sin, addrlen);
271 write(child_out, &zero, sizeof(zero)); 292 write_to_parent(child_out, &zero, sizeof(zero));
272 #endif 293 #endif
273 dns_params.hostname[0] = '\0'; 294 dns_params.hostname[0] = '\0';
274 } 295 }
275 296
276 close(child_out); 297 close(child_out);