author | Jayson Vantuyl <jvantuyl@engineyard.com> |
Wed, 25 Mar 2009 11:28:18 -0700 | |
changeset 596 | 9096d2549372 |
parent 559 | 6602623168cd |
child 604 | 1f5d313902dd |
permissions | -rw-r--r-- |
549 | 1 |
/*** |
2 |
This file is part of libasyncns. |
|
3 |
||
4 |
Copyright 2005-2008 Lennart Poettering |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
5 |
|
549 | 6 |
libasyncns is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU Lesser General Public License as |
|
8 |
published by the Free Software Foundation, either version 2.1 of the |
|
9 |
License, or (at your option) any later version. |
|
10 |
||
11 |
libasyncns is distributed in the hope that it will be useful, but |
|
12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
Lesser General Public License for more details. |
|
15 |
||
16 |
You should have received a copy of the GNU Lesser General Public |
|
17 |
License along with libasyncns. If not, see |
|
18 |
<http://www.gnu.org/licenses/>. |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
19 |
***/ |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
20 |
|
549 | 21 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
|
23 |
#endif |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
24 |
|
549 | 25 |
/* #undef HAVE_PTHREAD */ |
26 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
27 |
#include <assert.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
28 |
#include <fcntl.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
29 |
#include <signal.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
30 |
#include <unistd.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
31 |
#include <sys/select.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
32 |
#include <stdio.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
33 |
#include <string.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
34 |
#include <stdlib.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
35 |
#include <errno.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
36 |
#include <sys/wait.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
37 |
#include <sys/types.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
38 |
#include <pwd.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
39 |
#include <netinet/in.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
40 |
#include <arpa/nameser.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
41 |
#include <resolv.h> |
549 | 42 |
#include <dirent.h> |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
43 |
|
550
104d37c1b24a
Make the new libasyncns compile on mac os x
Mikael Hallendal <micke@imendio.com>
parents:
549
diff
changeset
|
44 |
/* Needed on Mac OS X where it is not included from arpa/nameser.h */ |
104d37c1b24a
Make the new libasyncns compile on mac os x
Mikael Hallendal <micke@imendio.com>
parents:
549
diff
changeset
|
45 |
#if HAVE_ARPA_NAMESER_COMPAT_H |
104d37c1b24a
Make the new libasyncns compile on mac os x
Mikael Hallendal <micke@imendio.com>
parents:
549
diff
changeset
|
46 |
#include <arpa/nameser_compat.h> |
104d37c1b24a
Make the new libasyncns compile on mac os x
Mikael Hallendal <micke@imendio.com>
parents:
549
diff
changeset
|
47 |
#endif |
104d37c1b24a
Make the new libasyncns compile on mac os x
Mikael Hallendal <micke@imendio.com>
parents:
549
diff
changeset
|
48 |
|
104d37c1b24a
Make the new libasyncns compile on mac os x
Mikael Hallendal <micke@imendio.com>
parents:
549
diff
changeset
|
49 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
50 |
#ifdef HAVE_SYS_PRCTL_H |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
51 |
#include <sys/prctl.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
52 |
#endif |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
53 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
54 |
#if HAVE_PTHREAD |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
55 |
#include <pthread.h> |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
56 |
#endif |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
57 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
58 |
#include "asyncns.h" |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
59 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
60 |
#define MAX_WORKERS 16 |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
61 |
#define MAX_QUERIES 256 |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
62 |
#define BUFSIZE (10240) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
63 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
64 |
typedef enum { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
65 |
REQUEST_ADDRINFO, |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
66 |
RESPONSE_ADDRINFO, |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
67 |
REQUEST_NAMEINFO, |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
68 |
RESPONSE_NAMEINFO, |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
69 |
REQUEST_RES_QUERY, |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
70 |
REQUEST_RES_SEARCH, |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
71 |
RESPONSE_RES, |
549 | 72 |
REQUEST_TERMINATE, |
73 |
RESPONSE_DIED |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
74 |
} query_type_t; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
75 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
76 |
enum { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
77 |
REQUEST_RECV_FD = 0, |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
78 |
REQUEST_SEND_FD = 1, |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
79 |
RESPONSE_RECV_FD = 2, |
549 | 80 |
RESPONSE_SEND_FD = 3, |
81 |
MESSAGE_FD_MAX = 4 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
82 |
}; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
83 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
84 |
struct asyncns { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
85 |
int fds[4]; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
86 |
|
549 | 87 |
#ifndef HAVE_PTHREAD |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
88 |
pid_t workers[MAX_WORKERS]; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
89 |
#else |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
90 |
pthread_t workers[MAX_WORKERS]; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
91 |
#endif |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
92 |
unsigned valid_workers; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
93 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
94 |
unsigned current_id, current_index; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
95 |
asyncns_query_t* queries[MAX_QUERIES]; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
96 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
97 |
asyncns_query_t *done_head, *done_tail; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
98 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
99 |
int n_queries; |
549 | 100 |
int dead; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
101 |
}; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
102 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
103 |
struct asyncns_query { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
104 |
asyncns_t *asyncns; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
105 |
int done; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
106 |
unsigned id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
107 |
query_type_t type; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
108 |
asyncns_query_t *done_next, *done_prev; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
109 |
int ret; |
549 | 110 |
int _errno; |
111 |
int _h_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
112 |
struct addrinfo *addrinfo; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
113 |
char *serv, *host; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
114 |
void *userdata; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
115 |
}; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
116 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
117 |
typedef struct rheader { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
118 |
query_type_t type; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
119 |
unsigned id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
120 |
size_t length; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
121 |
} rheader_t; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
122 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
123 |
typedef struct addrinfo_request { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
124 |
struct rheader header; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
125 |
int hints_is_null; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
126 |
int ai_flags; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
127 |
int ai_family; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
128 |
int ai_socktype; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
129 |
int ai_protocol; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
130 |
size_t node_len, service_len; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
131 |
} addrinfo_request_t; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
132 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
133 |
typedef struct addrinfo_response { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
134 |
struct rheader header; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
135 |
int ret; |
549 | 136 |
int _errno; |
137 |
int _h_errno; |
|
138 |
/* followed by addrinfo_serialization[] */ |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
139 |
} addrinfo_response_t; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
140 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
141 |
typedef struct addrinfo_serialization { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
142 |
int ai_flags; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
143 |
int ai_family; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
144 |
int ai_socktype; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
145 |
int ai_protocol; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
146 |
size_t ai_addrlen; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
147 |
size_t canonname_len; |
549 | 148 |
/* Followed by ai_addr amd ai_canonname with variable lengths */ |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
149 |
} addrinfo_serialization_t; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
150 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
151 |
typedef struct nameinfo_request { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
152 |
struct rheader header; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
153 |
int flags; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
154 |
socklen_t sockaddr_len; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
155 |
int gethost, getserv; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
156 |
} nameinfo_request_t; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
157 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
158 |
typedef struct nameinfo_response { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
159 |
struct rheader header; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
160 |
size_t hostlen, servlen; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
161 |
int ret; |
549 | 162 |
int _errno; |
163 |
int _h_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
164 |
} nameinfo_response_t; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
165 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
166 |
typedef struct res_query_request { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
167 |
struct rheader header; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
168 |
int class; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
169 |
int type; |
549 | 170 |
size_t dname_len; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
171 |
} res_request_t; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
172 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
173 |
typedef struct res_query_response { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
174 |
struct rheader header; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
175 |
int ret; |
549 | 176 |
int _errno; |
177 |
int _h_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
178 |
} res_response_t; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
179 |
|
549 | 180 |
#ifndef HAVE_STRNDUP |
181 |
||
182 |
static char *strndup(const char *s, size_t l) { |
|
183 |
size_t a; |
|
184 |
char *n; |
|
185 |
||
186 |
a = strlen(s); |
|
187 |
if (a > l) |
|
188 |
a = l; |
|
189 |
||
190 |
if (!(n = malloc(a+1))) |
|
191 |
return NULL; |
|
192 |
||
193 |
memcpy(n, s, a); |
|
194 |
n[a] = 0; |
|
195 |
||
196 |
return n; |
|
197 |
} |
|
198 |
||
199 |
#endif |
|
200 |
||
201 |
#ifndef HAVE_PTHREAD |
|
202 |
||
203 |
static int close_allv(const int except_fds[]) { |
|
204 |
struct rlimit rl; |
|
205 |
int fd; |
|
206 |
||
207 |
#ifdef __linux__ |
|
208 |
||
209 |
DIR *d; |
|
210 |
||
211 |
assert(except_fds); |
|
212 |
||
213 |
if ((d = opendir("/proc/self/fd"))) { |
|
214 |
||
215 |
struct dirent *de; |
|
216 |
||
217 |
while ((de = readdir(d))) { |
|
218 |
int found; |
|
219 |
long l; |
|
220 |
char *e = NULL; |
|
221 |
int i; |
|
222 |
||
223 |
if (de->d_name[0] == '.') |
|
224 |
continue; |
|
225 |
||
226 |
errno = 0; |
|
227 |
l = strtol(de->d_name, &e, 10); |
|
228 |
if (errno != 0 || !e || *e) { |
|
229 |
closedir(d); |
|
230 |
errno = EINVAL; |
|
231 |
return -1; |
|
232 |
} |
|
233 |
||
234 |
fd = (int) l; |
|
235 |
||
236 |
if ((long) fd != l) { |
|
237 |
closedir(d); |
|
238 |
errno = EINVAL; |
|
239 |
return -1; |
|
240 |
} |
|
241 |
||
242 |
if (fd < 3) |
|
243 |
continue; |
|
244 |
||
245 |
if (fd == dirfd(d)) |
|
246 |
continue; |
|
247 |
||
248 |
found = 0; |
|
249 |
for (i = 0; except_fds[i] >= 0; i++) |
|
250 |
if (except_fds[i] == fd) { |
|
251 |
found = 1; |
|
252 |
break; |
|
253 |
} |
|
254 |
||
255 |
if (found) |
|
256 |
continue; |
|
257 |
||
258 |
if (close(fd) < 0) { |
|
559
6602623168cd
fixed compile error in asyncns.c
Mikael Hallendal <micke@imendio.com>
parents:
550
diff
changeset
|
259 |
int saved_errno; |
549 | 260 |
saved_errno = errno; |
261 |
closedir(d); |
|
262 |
errno = saved_errno; |
|
263 |
||
264 |
return -1; |
|
265 |
} |
|
266 |
} |
|
267 |
||
268 |
closedir(d); |
|
269 |
return 0; |
|
270 |
} |
|
271 |
||
272 |
#endif |
|
273 |
||
274 |
if (getrlimit(RLIMIT_NOFILE, &rl) < 0) |
|
275 |
return -1; |
|
276 |
||
277 |
for (fd = 0; fd < (int) rl.rlim_max; fd++) { |
|
278 |
int i; |
|
279 |
||
280 |
if (fd <= 3) |
|
281 |
continue; |
|
282 |
||
283 |
for (i = 0; except_fds[i] >= 0; i++) |
|
284 |
if (except_fds[i] == fd) |
|
285 |
continue; |
|
286 |
||
287 |
if (close(fd) < 0 && errno != EBADF) |
|
288 |
return -1; |
|
289 |
} |
|
290 |
||
291 |
return 0; |
|
292 |
} |
|
293 |
||
294 |
static int reset_sigsv(const int except[]) { |
|
295 |
int sig; |
|
296 |
assert(except); |
|
297 |
||
550
104d37c1b24a
Make the new libasyncns compile on mac os x
Mikael Hallendal <micke@imendio.com>
parents:
549
diff
changeset
|
298 |
for (sig = 1; sig < NSIG; sig++) { |
549 | 299 |
int reset = 1; |
300 |
||
301 |
switch (sig) { |
|
302 |
case SIGKILL: |
|
303 |
case SIGSTOP: |
|
304 |
reset = 0; |
|
305 |
break; |
|
306 |
||
307 |
default: { |
|
308 |
int i; |
|
309 |
||
310 |
for (i = 0; except[i] > 0; i++) { |
|
311 |
if (sig == except[i]) { |
|
312 |
reset = 0; |
|
313 |
break; |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
} |
|
318 |
||
319 |
if (reset) { |
|
320 |
struct sigaction sa; |
|
321 |
||
322 |
memset(&sa, 0, sizeof(sa)); |
|
323 |
sa.sa_handler = SIG_DFL; |
|
324 |
||
325 |
/* On Linux the first two RT signals are reserved by |
|
326 |
* glibc, and sigaction() will return EINVAL for them. */ |
|
327 |
if ((sigaction(sig, &sa, NULL) < 0)) |
|
328 |
if (errno != EINVAL) |
|
329 |
return -1; |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
return 0; |
|
334 |
} |
|
335 |
||
336 |
static int ignore_sigsv(const int ignore[]) { |
|
337 |
int i; |
|
338 |
assert(ignore); |
|
339 |
||
340 |
for (i = 0; ignore[i] > 0; i++) { |
|
341 |
struct sigaction sa; |
|
342 |
||
343 |
memset(&sa, 0, sizeof(sa)); |
|
344 |
sa.sa_handler = SIG_IGN; |
|
345 |
||
346 |
if ((sigaction(ignore[i], &sa, NULL) < 0)) |
|
347 |
return -1; |
|
348 |
} |
|
349 |
||
350 |
return 0; |
|
351 |
} |
|
352 |
||
353 |
#endif |
|
354 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
355 |
static int fd_nonblock(int fd) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
356 |
int i; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
357 |
assert(fd >= 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
358 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
359 |
if ((i = fcntl(fd, F_GETFL, 0)) < 0) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
360 |
return -1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
361 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
362 |
if (i & O_NONBLOCK) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
363 |
return 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
364 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
365 |
return fcntl(fd, F_SETFL, i | O_NONBLOCK); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
366 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
367 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
368 |
static int fd_cloexec(int fd) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
369 |
int v; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
370 |
assert(fd >= 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
371 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
372 |
if ((v = fcntl(fd, F_GETFD, 0)) < 0) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
373 |
return -1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
374 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
375 |
if (v & FD_CLOEXEC) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
376 |
return 0; |
549 | 377 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
378 |
return fcntl(fd, F_SETFD, v | FD_CLOEXEC); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
379 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
380 |
|
549 | 381 |
static int send_died(int out_fd) { |
382 |
rheader_t rh; |
|
383 |
assert(out_fd > 0); |
|
384 |
||
385 |
memset(&rh, 0, sizeof(rh)); |
|
386 |
rh.type = RESPONSE_DIED; |
|
387 |
rh.id = 0; |
|
388 |
rh.length = sizeof(rh); |
|
389 |
||
390 |
return send(out_fd, &rh, rh.length, 0); |
|
391 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
392 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
393 |
static void *serialize_addrinfo(void *p, const struct addrinfo *ai, size_t *length, size_t maxlength) { |
549 | 394 |
addrinfo_serialization_t s; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
395 |
size_t cnl, l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
396 |
assert(p); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
397 |
assert(ai); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
398 |
assert(length); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
399 |
assert(*length <= maxlength); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
400 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
401 |
cnl = (ai->ai_canonname ? strlen(ai->ai_canonname)+1 : 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
402 |
l = sizeof(addrinfo_serialization_t) + ai->ai_addrlen + cnl; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
403 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
404 |
if (*length + l > maxlength) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
405 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
406 |
|
549 | 407 |
s.ai_flags = ai->ai_flags; |
408 |
s.ai_family = ai->ai_family; |
|
409 |
s.ai_socktype = ai->ai_socktype; |
|
410 |
s.ai_protocol = ai->ai_protocol; |
|
411 |
s.ai_addrlen = ai->ai_addrlen; |
|
412 |
s.canonname_len = cnl; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
413 |
|
549 | 414 |
memcpy((uint8_t*) p, &s, sizeof(addrinfo_serialization_t)); |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
415 |
memcpy((uint8_t*) p + sizeof(addrinfo_serialization_t), ai->ai_addr, ai->ai_addrlen); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
416 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
417 |
if (ai->ai_canonname) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
418 |
strcpy((char*) p + sizeof(addrinfo_serialization_t) + ai->ai_addrlen, ai->ai_canonname); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
419 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
420 |
*length += l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
421 |
return (uint8_t*) p + l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
422 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
423 |
|
549 | 424 |
static int send_addrinfo_reply(int out_fd, unsigned id, int ret, struct addrinfo *ai, int _errno, int _h_errno) { |
425 |
addrinfo_response_t data[BUFSIZE/sizeof(addrinfo_response_t) + 1]; |
|
426 |
addrinfo_response_t *resp = data; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
427 |
assert(out_fd >= 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
428 |
|
549 | 429 |
memset(data, 0, sizeof(data)); |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
430 |
resp->header.type = RESPONSE_ADDRINFO; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
431 |
resp->header.id = id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
432 |
resp->header.length = sizeof(addrinfo_response_t); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
433 |
resp->ret = ret; |
549 | 434 |
resp->_errno = _errno; |
435 |
resp->_h_errno = _h_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
436 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
437 |
if (ret == 0 && ai) { |
549 | 438 |
void *p = data + 1; |
439 |
struct addrinfo *k; |
|
440 |
||
441 |
for (k = ai; k; k = k->ai_next) { |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
442 |
|
549 | 443 |
if (!(p = serialize_addrinfo(p, k, &resp->header.length, (char*) data + BUFSIZE - (char*) p))) { |
444 |
resp->ret = EAI_MEMORY; |
|
445 |
break; |
|
446 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
447 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
448 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
449 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
450 |
if (ai) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
451 |
freeaddrinfo(ai); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
452 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
453 |
return send(out_fd, resp, resp->header.length, 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
454 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
455 |
|
549 | 456 |
static int send_nameinfo_reply(int out_fd, unsigned id, int ret, const char *host, const char *serv, int _errno, int _h_errno) { |
457 |
nameinfo_response_t data[BUFSIZE/sizeof(nameinfo_response_t) + 1]; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
458 |
size_t hl, sl; |
549 | 459 |
nameinfo_response_t *resp = data; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
460 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
461 |
assert(out_fd >= 0); |
549 | 462 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
463 |
sl = serv ? strlen(serv)+1 : 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
464 |
hl = host ? strlen(host)+1 : 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
465 |
|
549 | 466 |
memset(data, 0, sizeof(data)); |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
467 |
resp->header.type = RESPONSE_NAMEINFO; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
468 |
resp->header.id = id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
469 |
resp->header.length = sizeof(nameinfo_response_t) + hl + sl; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
470 |
resp->ret = ret; |
549 | 471 |
resp->_errno = _errno; |
472 |
resp->_h_errno = _h_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
473 |
resp->hostlen = hl; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
474 |
resp->servlen = sl; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
475 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
476 |
assert(sizeof(data) >= resp->header.length); |
549 | 477 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
478 |
if (host) |
549 | 479 |
memcpy((uint8_t *)data + sizeof(nameinfo_response_t), host, hl); |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
480 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
481 |
if (serv) |
549 | 482 |
memcpy((uint8_t *)data + sizeof(nameinfo_response_t) + hl, serv, sl); |
483 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
484 |
return send(out_fd, resp, resp->header.length, 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
485 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
486 |
|
549 | 487 |
static int send_res_reply(int out_fd, unsigned id, const unsigned char *answer, int ret, int _errno, int _h_errno) { |
488 |
res_response_t data[BUFSIZE/sizeof(res_response_t) + 1]; |
|
489 |
res_response_t *resp = data; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
490 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
491 |
assert(out_fd >= 0); |
549 | 492 |
|
493 |
memset(data, 0, sizeof(data)); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
494 |
resp->header.type = RESPONSE_RES; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
495 |
resp->header.id = id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
496 |
resp->header.length = sizeof(res_response_t) + (ret < 0 ? 0 : ret); |
549 | 497 |
resp->ret = ret; |
498 |
resp->_errno = _errno; |
|
499 |
resp->_h_errno = _h_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
500 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
501 |
assert(sizeof(data) >= resp->header.length); |
549 | 502 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
503 |
if (ret > 0) |
549 | 504 |
memcpy((uint8_t *)data + sizeof(res_response_t), answer, ret); |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
505 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
506 |
return send(out_fd, resp, resp->header.length, 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
507 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
508 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
509 |
static int handle_request(int out_fd, const rheader_t *req, size_t length) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
510 |
assert(out_fd >= 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
511 |
assert(req); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
512 |
assert(length >= sizeof(rheader_t)); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
513 |
assert(length == req->length); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
514 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
515 |
switch (req->type) { |
549 | 516 |
case REQUEST_ADDRINFO: { |
517 |
struct addrinfo ai, *result = NULL; |
|
518 |
const addrinfo_request_t *ai_req = (const addrinfo_request_t*) req; |
|
519 |
const char *node, *service; |
|
520 |
int ret; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
521 |
|
549 | 522 |
assert(length >= sizeof(addrinfo_request_t)); |
523 |
assert(length == sizeof(addrinfo_request_t) + ai_req->node_len + ai_req->service_len); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
524 |
|
549 | 525 |
memset(&ai, 0, sizeof(ai)); |
526 |
ai.ai_flags = ai_req->ai_flags; |
|
527 |
ai.ai_family = ai_req->ai_family; |
|
528 |
ai.ai_socktype = ai_req->ai_socktype; |
|
529 |
ai.ai_protocol = ai_req->ai_protocol; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
530 |
|
549 | 531 |
node = ai_req->node_len ? (const char*) req + sizeof(addrinfo_request_t) : NULL; |
532 |
service = ai_req->service_len ? (const char*) req + sizeof(addrinfo_request_t) + ai_req->node_len : NULL; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
533 |
|
549 | 534 |
ret = getaddrinfo(node, service, |
535 |
ai_req->hints_is_null ? NULL : &ai, |
|
536 |
&result); |
|
537 |
||
538 |
/* send_addrinfo_reply() frees result */ |
|
539 |
return send_addrinfo_reply(out_fd, req->id, ret, result, errno, h_errno); |
|
540 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
541 |
|
549 | 542 |
case REQUEST_NAMEINFO: { |
543 |
int ret; |
|
544 |
const nameinfo_request_t *ni_req = (const nameinfo_request_t*) req; |
|
545 |
char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV]; |
|
546 |
struct sockaddr_storage sa; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
547 |
|
549 | 548 |
assert(length >= sizeof(nameinfo_request_t)); |
549 |
assert(length == sizeof(nameinfo_request_t) + ni_req->sockaddr_len); |
|
550 |
||
551 |
memcpy(&sa, (const uint8_t *)req + sizeof(nameinfo_request_t), ni_req->sockaddr_len); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
552 |
|
549 | 553 |
ret = getnameinfo((struct sockaddr *)&sa, ni_req->sockaddr_len, |
554 |
ni_req->gethost ? hostbuf : NULL, ni_req->gethost ? sizeof(hostbuf) : 0, |
|
555 |
ni_req->getserv ? servbuf : NULL, ni_req->getserv ? sizeof(servbuf) : 0, |
|
556 |
ni_req->flags); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
557 |
|
549 | 558 |
return send_nameinfo_reply(out_fd, req->id, ret, |
559 |
ret == 0 && ni_req->gethost ? hostbuf : NULL, |
|
560 |
ret == 0 && ni_req->getserv ? servbuf : NULL, |
|
561 |
errno, h_errno); |
|
562 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
563 |
|
549 | 564 |
case REQUEST_RES_QUERY: |
565 |
case REQUEST_RES_SEARCH: { |
|
566 |
int ret; |
|
567 |
HEADER answer[BUFSIZE/sizeof(HEADER) + 1]; |
|
568 |
const res_request_t *res_req = (const res_request_t *)req; |
|
569 |
const char *dname; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
570 |
|
549 | 571 |
assert(length >= sizeof(res_request_t)); |
572 |
assert(length == sizeof(res_request_t) + res_req->dname_len); |
|
573 |
||
574 |
dname = (const char *) req + sizeof(res_request_t); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
575 |
|
549 | 576 |
if (req->type == REQUEST_RES_QUERY) |
577 |
ret = res_query(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE); |
|
578 |
else |
|
579 |
ret = res_search(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE); |
|
580 |
||
581 |
return send_res_reply(out_fd, req->id, (unsigned char *) answer, ret, errno, h_errno); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
582 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
583 |
|
549 | 584 |
case REQUEST_TERMINATE: |
585 |
/* Quit */ |
|
586 |
return -1; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
587 |
|
549 | 588 |
default: |
589 |
; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
590 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
591 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
592 |
return 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
593 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
594 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
595 |
#ifndef HAVE_PTHREAD |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
596 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
597 |
static int process_worker(int in_fd, int out_fd) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
598 |
int have_death_sig = 0; |
549 | 599 |
int good_fds[3]; |
600 |
int ret = 1; |
|
601 |
||
602 |
const int ignore_sigs[] = { |
|
603 |
SIGINT, |
|
604 |
SIGHUP, |
|
605 |
SIGPIPE, |
|
606 |
SIGUSR1, |
|
607 |
SIGUSR2, |
|
608 |
-1 |
|
609 |
}; |
|
610 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
611 |
assert(in_fd > 2); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
612 |
assert(out_fd > 2); |
549 | 613 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
614 |
close(0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
615 |
close(1); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
616 |
close(2); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
617 |
|
549 | 618 |
if (open("/dev/null", O_RDONLY) != 0) |
619 |
goto fail; |
|
620 |
||
621 |
if (open("/dev/null", O_WRONLY) != 1) |
|
622 |
goto fail; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
623 |
|
549 | 624 |
if (open("/dev/null", O_WRONLY) != 2) |
625 |
goto fail; |
|
626 |
||
627 |
if (chdir("/") < 0) |
|
628 |
goto fail; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
629 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
630 |
if (geteuid() == 0) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
631 |
struct passwd *pw; |
549 | 632 |
int r; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
633 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
634 |
if ((pw = getpwnam("nobody"))) { |
549 | 635 |
#ifdef HAVE_SETRESUID |
636 |
r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
637 |
#elif HAVE_SETREUID |
549 | 638 |
r = setreuid(pw->pw_uid, pw->pw_uid); |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
639 |
#else |
549 | 640 |
if ((r = setuid(pw->pw_uid)) >= 0) |
641 |
r = seteuid(pw->pw_uid); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
642 |
#endif |
549 | 643 |
if (r < 0) |
644 |
goto fail; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
645 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
646 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
647 |
|
549 | 648 |
if (reset_sigsv(ignore_sigs) < 0) |
649 |
goto fail; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
650 |
|
549 | 651 |
if (ignore_sigsv(ignore_sigs) < 0) |
652 |
goto fail; |
|
653 |
||
654 |
good_fds[0] = in_fd; good_fds[1] = out_fd; good_fds[2] = -1; |
|
655 |
if (close_allv(good_fds) < 0) |
|
656 |
goto fail; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
657 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
658 |
#ifdef PR_SET_PDEATHSIG |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
659 |
if (prctl(PR_SET_PDEATHSIG, SIGTERM) >= 0) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
660 |
have_death_sig = 1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
661 |
#endif |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
662 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
663 |
if (!have_death_sig) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
664 |
fd_nonblock(in_fd); |
549 | 665 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
666 |
while (getppid() > 1) { /* if the parent PID is 1 our parent process died. */ |
549 | 667 |
rheader_t buf[BUFSIZE/sizeof(rheader_t) + 1]; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
668 |
ssize_t length; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
669 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
670 |
if (!have_death_sig) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
671 |
fd_set fds; |
549 | 672 |
struct timeval tv = { 0, 500000 }; |
673 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
674 |
FD_ZERO(&fds); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
675 |
FD_SET(in_fd, &fds); |
549 | 676 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
677 |
if (select(in_fd+1, &fds, NULL, NULL, &tv) < 0) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
678 |
break; |
549 | 679 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
680 |
if (getppid() == 1) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
681 |
break; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
682 |
} |
549 | 683 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
684 |
if ((length = recv(in_fd, buf, sizeof(buf), 0)) <= 0) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
685 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
686 |
if (length < 0 && errno == EAGAIN) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
687 |
continue; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
688 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
689 |
break; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
690 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
691 |
|
549 | 692 |
if (handle_request(out_fd, buf, (size_t) length) < 0) |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
693 |
break; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
694 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
695 |
|
549 | 696 |
ret = 0; |
697 |
||
698 |
fail: |
|
699 |
||
700 |
send_died(out_fd); |
|
701 |
||
702 |
return ret; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
703 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
704 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
705 |
#else |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
706 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
707 |
static void* thread_worker(void *p) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
708 |
sigset_t fullset; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
709 |
int *fds = p; |
549 | 710 |
int in_fd, out_fd; |
711 |
||
712 |
in_fd = fds[REQUEST_RECV_FD]; |
|
713 |
out_fd = fds[RESPONSE_SEND_FD]; |
|
714 |
free(p); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
715 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
716 |
/* No signals in this thread please */ |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
717 |
sigfillset(&fullset); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
718 |
pthread_sigmask(SIG_BLOCK, &fullset, NULL); |
549 | 719 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
720 |
for (;;) { |
549 | 721 |
rheader_t buf[BUFSIZE/sizeof(rheader_t) + 1]; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
722 |
ssize_t length; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
723 |
|
549 | 724 |
if ((length = recv(in_fd, buf, sizeof(buf), 0)) <= 0) |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
725 |
break; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
726 |
|
549 | 727 |
if (handle_request(out_fd, buf, (size_t) length) < 0) |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
728 |
break; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
729 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
730 |
|
549 | 731 |
send_died(out_fd); |
732 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
733 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
734 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
735 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
736 |
#endif |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
737 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
738 |
asyncns_t* asyncns_new(unsigned n_proc) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
739 |
asyncns_t *asyncns = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
740 |
int i; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
741 |
assert(n_proc >= 1); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
742 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
743 |
if (n_proc > MAX_WORKERS) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
744 |
n_proc = MAX_WORKERS; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
745 |
|
549 | 746 |
if (!(asyncns = malloc(sizeof(asyncns_t)))) { |
747 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
748 |
goto fail; |
549 | 749 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
750 |
|
549 | 751 |
asyncns->dead = 0; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
752 |
asyncns->valid_workers = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
753 |
|
549 | 754 |
for (i = 0; i < MESSAGE_FD_MAX; i++) |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
755 |
asyncns->fds[i] = -1; |
549 | 756 |
|
757 |
memset(asyncns->queries, 0, sizeof(asyncns->queries)); |
|
758 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
759 |
if (socketpair(PF_UNIX, SOCK_DGRAM, 0, asyncns->fds) < 0 || |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
760 |
socketpair(PF_UNIX, SOCK_DGRAM, 0, asyncns->fds+2) < 0) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
761 |
goto fail; |
549 | 762 |
|
763 |
for (i = 0; i < MESSAGE_FD_MAX; i++) |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
764 |
fd_cloexec(asyncns->fds[i]); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
765 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
766 |
for (asyncns->valid_workers = 0; asyncns->valid_workers < n_proc; asyncns->valid_workers++) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
767 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
768 |
#ifndef HAVE_PTHREAD |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
769 |
if ((asyncns->workers[asyncns->valid_workers] = fork()) < 0) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
770 |
goto fail; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
771 |
else if (asyncns->workers[asyncns->valid_workers] == 0) { |
549 | 772 |
int ret; |
773 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
774 |
close(asyncns->fds[REQUEST_SEND_FD]); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
775 |
close(asyncns->fds[RESPONSE_RECV_FD]); |
549 | 776 |
ret = process_worker(asyncns->fds[REQUEST_RECV_FD], asyncns->fds[RESPONSE_SEND_FD]); |
777 |
close(asyncns->fds[REQUEST_RECV_FD]); |
|
778 |
close(asyncns->fds[RESPONSE_SEND_FD]); |
|
779 |
_exit(ret); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
780 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
781 |
#else |
549 | 782 |
|
783 |
int *fds, r; |
|
784 |
||
785 |
/* We need to copy this array because otherwise we might have |
|
786 |
* a small chance of a race where the thread accesses fds when |
|
787 |
* *asyncns is already dead */ |
|
788 |
||
789 |
if (!(fds = malloc(sizeof(asyncns->fds)))) { |
|
790 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
791 |
goto fail; |
549 | 792 |
} |
793 |
||
794 |
memcpy(fds, asyncns->fds, sizeof(asyncns->fds)); |
|
795 |
||
796 |
if ((r = pthread_create(&asyncns->workers[asyncns->valid_workers], NULL, thread_worker, fds)) != 0) { |
|
797 |
free(fds); |
|
798 |
errno = r; |
|
799 |
goto fail; |
|
800 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
801 |
#endif |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
802 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
803 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
804 |
#ifndef HAVE_PTHREAD |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
805 |
close(asyncns->fds[REQUEST_RECV_FD]); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
806 |
close(asyncns->fds[RESPONSE_SEND_FD]); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
807 |
asyncns->fds[REQUEST_RECV_FD] = asyncns->fds[RESPONSE_SEND_FD] = -1; |
549 | 808 |
#endif |
809 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
810 |
asyncns->current_index = asyncns->current_id = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
811 |
asyncns->done_head = asyncns->done_tail = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
812 |
asyncns->n_queries = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
813 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
814 |
fd_nonblock(asyncns->fds[RESPONSE_RECV_FD]); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
815 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
816 |
return asyncns; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
817 |
|
549 | 818 |
fail: |
819 |
if (asyncns) |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
820 |
asyncns_free(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
821 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
822 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
823 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
824 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
825 |
void asyncns_free(asyncns_t *asyncns) { |
549 | 826 |
int i; |
827 |
int saved_errno = errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
828 |
unsigned p; |
549 | 829 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
830 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
831 |
|
549 | 832 |
if (asyncns->fds[REQUEST_SEND_FD] >= 0) { |
833 |
rheader_t req; |
|
834 |
||
835 |
memset(&req, 0, sizeof(req)); |
|
836 |
req.type = REQUEST_TERMINATE; |
|
837 |
req.length = sizeof(req); |
|
838 |
req.id = 0; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
839 |
|
549 | 840 |
/* Send one termiantion packet for each worker */ |
841 |
for (p = 0; p < asyncns->valid_workers; p++) |
|
842 |
send(asyncns->fds[REQUEST_SEND_FD], &req, req.length, 0); |
|
843 |
} |
|
844 |
||
845 |
/* Close all communication channels */ |
|
846 |
for (i = 0; i < MESSAGE_FD_MAX; i++) |
|
847 |
if (asyncns->fds[i] >= 0) |
|
848 |
close(asyncns->fds[i]); |
|
849 |
||
850 |
/* Now terminate them forcibly */ |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
851 |
for (p = 0; p < asyncns->valid_workers; p++) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
852 |
#ifndef HAVE_PTHREAD |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
853 |
kill(asyncns->workers[p], SIGTERM); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
854 |
waitpid(asyncns->workers[p], NULL, 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
855 |
#else |
549 | 856 |
pthread_detach(asyncns->workers[p]); |
857 |
||
858 |
/* We don't join the thread here because there is no clean way |
|
859 |
to cancel a running lookup if one should be active. So it |
|
860 |
might take a while until the lookup thread actually |
|
861 |
terminates. But we don't really care, because it won't leak |
|
862 |
resources. */ |
|
863 |
#endif |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
864 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
865 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
866 |
for (p = 0; p < MAX_QUERIES; p++) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
867 |
if (asyncns->queries[p]) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
868 |
asyncns_cancel(asyncns, asyncns->queries[p]); |
549 | 869 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
870 |
free(asyncns); |
549 | 871 |
|
872 |
errno = saved_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
873 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
874 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
875 |
int asyncns_fd(asyncns_t *asyncns) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
876 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
877 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
878 |
return asyncns->fds[RESPONSE_RECV_FD]; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
879 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
880 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
881 |
static asyncns_query_t *lookup_query(asyncns_t *asyncns, unsigned id) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
882 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
883 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
884 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
885 |
if ((q = asyncns->queries[id % MAX_QUERIES])) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
886 |
if (q->id == id) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
887 |
return q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
888 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
889 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
890 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
891 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
892 |
static void complete_query(asyncns_t *asyncns, asyncns_query_t *q) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
893 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
894 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
895 |
assert(!q->done); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
896 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
897 |
q->done = 1; |
549 | 898 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
899 |
if ((q->done_prev = asyncns->done_tail)) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
900 |
asyncns->done_tail->done_next = q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
901 |
else |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
902 |
asyncns->done_head = q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
903 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
904 |
asyncns->done_tail = q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
905 |
q->done_next = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
906 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
907 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
908 |
static void *unserialize_addrinfo(void *p, struct addrinfo **ret_ai, size_t *length) { |
549 | 909 |
addrinfo_serialization_t s; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
910 |
size_t l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
911 |
struct addrinfo *ai; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
912 |
assert(p); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
913 |
assert(ret_ai); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
914 |
assert(length); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
915 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
916 |
if (*length < sizeof(addrinfo_serialization_t)) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
917 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
918 |
|
549 | 919 |
memcpy(&s, p, sizeof(s)); |
920 |
||
921 |
l = sizeof(addrinfo_serialization_t) + s.ai_addrlen + s.canonname_len; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
922 |
if (*length < l) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
923 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
924 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
925 |
if (!(ai = malloc(sizeof(struct addrinfo)))) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
926 |
goto fail; |
549 | 927 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
928 |
ai->ai_addr = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
929 |
ai->ai_canonname = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
930 |
ai->ai_next = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
931 |
|
549 | 932 |
if (s.ai_addrlen && !(ai->ai_addr = malloc(s.ai_addrlen))) |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
933 |
goto fail; |
549 | 934 |
|
935 |
if (s.canonname_len && !(ai->ai_canonname = malloc(s.canonname_len))) |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
936 |
goto fail; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
937 |
|
549 | 938 |
ai->ai_flags = s.ai_flags; |
939 |
ai->ai_family = s.ai_family; |
|
940 |
ai->ai_socktype = s.ai_socktype; |
|
941 |
ai->ai_protocol = s.ai_protocol; |
|
942 |
ai->ai_addrlen = s.ai_addrlen; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
943 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
944 |
if (ai->ai_addr) |
549 | 945 |
memcpy(ai->ai_addr, (uint8_t*) p + sizeof(addrinfo_serialization_t), s.ai_addrlen); |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
946 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
947 |
if (ai->ai_canonname) |
549 | 948 |
memcpy(ai->ai_canonname, (uint8_t*) p + sizeof(addrinfo_serialization_t) + s.ai_addrlen, s.canonname_len); |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
949 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
950 |
*length -= l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
951 |
*ret_ai = ai; |
549 | 952 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
953 |
return (uint8_t*) p + l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
954 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
955 |
|
549 | 956 |
fail: |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
957 |
if (ai) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
958 |
asyncns_freeaddrinfo(ai); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
959 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
960 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
961 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
962 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
963 |
static int handle_response(asyncns_t *asyncns, rheader_t *resp, size_t length) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
964 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
965 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
966 |
assert(resp); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
967 |
assert(length >= sizeof(rheader_t)); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
968 |
assert(length == resp->length); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
969 |
|
549 | 970 |
if (resp->type == RESPONSE_DIED) { |
971 |
asyncns->dead = 1; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
972 |
return 0; |
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
490
diff
changeset
|
973 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
974 |
|
549 | 975 |
if (!(q = lookup_query(asyncns, resp->id))) |
976 |
return 0; |
|
977 |
||
978 |
switch (resp->type) { |
|
979 |
case RESPONSE_ADDRINFO: { |
|
980 |
const addrinfo_response_t *ai_resp = (addrinfo_response_t*) resp; |
|
981 |
void *p; |
|
982 |
size_t l; |
|
983 |
struct addrinfo *prev = NULL; |
|
984 |
||
985 |
assert(length >= sizeof(addrinfo_response_t)); |
|
986 |
assert(q->type == REQUEST_ADDRINFO); |
|
987 |
||
988 |
q->ret = ai_resp->ret; |
|
989 |
q->_errno = ai_resp->_errno; |
|
990 |
q->_h_errno = ai_resp->_h_errno; |
|
991 |
l = length - sizeof(addrinfo_response_t); |
|
992 |
p = (uint8_t*) resp + sizeof(addrinfo_response_t); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
993 |
|
549 | 994 |
while (l > 0 && p) { |
995 |
struct addrinfo *ai = NULL; |
|
996 |
p = unserialize_addrinfo(p, &ai, &l); |
|
997 |
||
998 |
if (!p || !ai) { |
|
999 |
q->ret = EAI_MEMORY; |
|
1000 |
break; |
|
1001 |
} |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
490
diff
changeset
|
1002 |
|
549 | 1003 |
if (prev) |
1004 |
prev->ai_next = ai; |
|
1005 |
else |
|
1006 |
q->addrinfo = ai; |
|
1007 |
||
1008 |
prev = ai; |
|
1009 |
} |
|
1010 |
||
1011 |
complete_query(asyncns, q); |
|
1012 |
break; |
|
1013 |
} |
|
1014 |
||
1015 |
case RESPONSE_NAMEINFO: { |
|
1016 |
const nameinfo_response_t *ni_resp = (nameinfo_response_t*) resp; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1017 |
|
549 | 1018 |
assert(length >= sizeof(nameinfo_response_t)); |
1019 |
assert(q->type == REQUEST_NAMEINFO); |
|
1020 |
||
1021 |
q->ret = ni_resp->ret; |
|
1022 |
q->_errno = ni_resp->_errno; |
|
1023 |
q->_h_errno = ni_resp->_h_errno; |
|
1024 |
||
1025 |
if (ni_resp->hostlen) |
|
1026 |
if (!(q->host = strndup((const char*) ni_resp + sizeof(nameinfo_response_t), ni_resp->hostlen-1))) |
|
1027 |
q->ret = EAI_MEMORY; |
|
1028 |
||
1029 |
if (ni_resp->servlen) |
|
1030 |
if (!(q->serv = strndup((const char*) ni_resp + sizeof(nameinfo_response_t) + ni_resp->hostlen, ni_resp->servlen-1))) |
|
1031 |
q->ret = EAI_MEMORY; |
|
1032 |
||
1033 |
complete_query(asyncns, q); |
|
1034 |
break; |
|
1035 |
} |
|
1036 |
||
1037 |
case RESPONSE_RES: { |
|
1038 |
const res_response_t *res_resp = (res_response_t *)resp; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1039 |
|
549 | 1040 |
assert(length >= sizeof(res_response_t)); |
1041 |
assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH); |
|
1042 |
||
1043 |
q->ret = res_resp->ret; |
|
1044 |
q->_errno = res_resp->_errno; |
|
1045 |
q->_h_errno = res_resp->_h_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1046 |
|
549 | 1047 |
if (res_resp->ret >= 0) { |
1048 |
if (!(q->serv = malloc(res_resp->ret))) { |
|
1049 |
q->ret = -1; |
|
1050 |
q->_errno = ENOMEM; |
|
1051 |
} else |
|
1052 |
memcpy(q->serv, (char *)resp + sizeof(res_response_t), res_resp->ret); |
|
1053 |
} |
|
1054 |
||
1055 |
complete_query(asyncns, q); |
|
1056 |
break; |
|
1057 |
} |
|
1058 |
||
1059 |
default: |
|
1060 |
; |
|
518
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
490
diff
changeset
|
1061 |
} |
cdd6a0c5b439
Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents:
490
diff
changeset
|
1062 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1063 |
return 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1064 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1065 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1066 |
int asyncns_wait(asyncns_t *asyncns, int block) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1067 |
int handled = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1068 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1069 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1070 |
for (;;) { |
549 | 1071 |
rheader_t buf[BUFSIZE/sizeof(rheader_t) + 1]; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1072 |
ssize_t l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1073 |
|
549 | 1074 |
if (asyncns->dead) { |
1075 |
errno = ECHILD; |
|
1076 |
return -1; |
|
1077 |
} |
|
1078 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1079 |
if (((l = recv(asyncns->fds[RESPONSE_RECV_FD], buf, sizeof(buf), 0)) < 0)) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1080 |
fd_set fds; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1081 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1082 |
if (errno != EAGAIN) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1083 |
return -1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1084 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1085 |
if (!block || handled) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1086 |
return 0; |
549 | 1087 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1088 |
FD_ZERO(&fds); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1089 |
FD_SET(asyncns->fds[RESPONSE_RECV_FD], &fds); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1090 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1091 |
if (select(asyncns->fds[RESPONSE_RECV_FD]+1, &fds, NULL, NULL, NULL) < 0) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1092 |
return -1; |
549 | 1093 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1094 |
continue; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1095 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1096 |
|
549 | 1097 |
if (handle_response(asyncns, buf, (size_t) l) < 0) |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1098 |
return -1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1099 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1100 |
handled = 1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1101 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1102 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1103 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1104 |
static asyncns_query_t *alloc_query(asyncns_t *asyncns) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1105 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1106 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1107 |
|
549 | 1108 |
if (asyncns->n_queries >= MAX_QUERIES) { |
1109 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1110 |
return NULL; |
549 | 1111 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1112 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1113 |
while (asyncns->queries[asyncns->current_index]) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1114 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1115 |
asyncns->current_index++; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1116 |
asyncns->current_id++; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1117 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1118 |
while (asyncns->current_index >= MAX_QUERIES) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1119 |
asyncns->current_index -= MAX_QUERIES; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1120 |
} |
549 | 1121 |
|
1122 |
if (!(q = asyncns->queries[asyncns->current_index] = malloc(sizeof(asyncns_query_t)))) { |
|
1123 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1124 |
return NULL; |
549 | 1125 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1126 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1127 |
asyncns->n_queries++; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1128 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1129 |
q->asyncns = asyncns; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1130 |
q->done = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1131 |
q->id = asyncns->current_id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1132 |
q->done_next = q->done_prev = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1133 |
q->ret = 0; |
549 | 1134 |
q->_errno = 0; |
1135 |
q->_h_errno = 0; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1136 |
q->addrinfo = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1137 |
q->userdata = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1138 |
q->host = q->serv = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1139 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1140 |
return q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1141 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1142 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1143 |
asyncns_query_t* asyncns_getaddrinfo(asyncns_t *asyncns, const char *node, const char *service, const struct addrinfo *hints) { |
549 | 1144 |
addrinfo_request_t data[BUFSIZE/sizeof(addrinfo_request_t) + 1]; |
1145 |
addrinfo_request_t *req = data; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1146 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1147 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1148 |
assert(node || service); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1149 |
|
549 | 1150 |
if (asyncns->dead) { |
1151 |
errno = ECHILD; |
|
1152 |
return NULL; |
|
1153 |
} |
|
1154 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1155 |
if (!(q = alloc_query(asyncns))) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1156 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1157 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1158 |
memset(req, 0, sizeof(addrinfo_request_t)); |
549 | 1159 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1160 |
req->node_len = node ? strlen(node)+1 : 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1161 |
req->service_len = service ? strlen(service)+1 : 0; |
549 | 1162 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1163 |
req->header.id = q->id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1164 |
req->header.type = q->type = REQUEST_ADDRINFO; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1165 |
req->header.length = sizeof(addrinfo_request_t) + req->node_len + req->service_len; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1166 |
|
549 | 1167 |
if (req->header.length > BUFSIZE) { |
1168 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1169 |
goto fail; |
549 | 1170 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1171 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1172 |
if (!(req->hints_is_null = !hints)) { |
549 | 1173 |
req->ai_flags = hints->ai_flags; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1174 |
req->ai_family = hints->ai_family; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1175 |
req->ai_socktype = hints->ai_socktype; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1176 |
req->ai_protocol = hints->ai_protocol; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1177 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1178 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1179 |
if (node) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1180 |
strcpy((char*) req + sizeof(addrinfo_request_t), node); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1181 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1182 |
if (service) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1183 |
strcpy((char*) req + sizeof(addrinfo_request_t) + req->node_len, service); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1184 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1185 |
if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, 0) < 0) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1186 |
goto fail; |
549 | 1187 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1188 |
return q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1189 |
|
549 | 1190 |
fail: |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1191 |
if (q) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1192 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1193 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1194 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1195 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1196 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1197 |
int asyncns_getaddrinfo_done(asyncns_t *asyncns, asyncns_query_t* q, struct addrinfo **ret_res) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1198 |
int ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1199 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1200 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1201 |
assert(q->asyncns == asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1202 |
assert(q->type == REQUEST_ADDRINFO); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1203 |
|
549 | 1204 |
if (asyncns->dead) { |
1205 |
errno = ECHILD; |
|
1206 |
return EAI_SYSTEM; |
|
1207 |
} |
|
1208 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1209 |
if (!q->done) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1210 |
return EAI_AGAIN; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1211 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1212 |
*ret_res = q->addrinfo; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1213 |
q->addrinfo = NULL; |
549 | 1214 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1215 |
ret = q->ret; |
549 | 1216 |
|
1217 |
if (ret == EAI_SYSTEM) |
|
1218 |
errno = q->_errno; |
|
1219 |
||
1220 |
if (ret != 0) |
|
1221 |
h_errno = q->_h_errno; |
|
1222 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1223 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1224 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1225 |
return ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1226 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1227 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1228 |
asyncns_query_t* asyncns_getnameinfo(asyncns_t *asyncns, const struct sockaddr *sa, socklen_t salen, int flags, int gethost, int getserv) { |
549 | 1229 |
nameinfo_request_t data[BUFSIZE/sizeof(nameinfo_request_t) + 1]; |
1230 |
nameinfo_request_t *req = data; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1231 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1232 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1233 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1234 |
assert(sa); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1235 |
assert(salen > 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1236 |
|
549 | 1237 |
if (asyncns->dead) { |
1238 |
errno = ECHILD; |
|
1239 |
return NULL; |
|
1240 |
} |
|
1241 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1242 |
if (!(q = alloc_query(asyncns))) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1243 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1244 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1245 |
memset(req, 0, sizeof(nameinfo_request_t)); |
549 | 1246 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1247 |
req->header.id = q->id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1248 |
req->header.type = q->type = REQUEST_NAMEINFO; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1249 |
req->header.length = sizeof(nameinfo_request_t) + salen; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1250 |
|
549 | 1251 |
if (req->header.length > BUFSIZE) { |
1252 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1253 |
goto fail; |
549 | 1254 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1255 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1256 |
req->flags = flags; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1257 |
req->sockaddr_len = salen; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1258 |
req->gethost = gethost; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1259 |
req->getserv = getserv; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1260 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1261 |
memcpy((uint8_t*) req + sizeof(nameinfo_request_t), sa, salen); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1262 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1263 |
if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, 0) < 0) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1264 |
goto fail; |
549 | 1265 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1266 |
return q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1267 |
|
549 | 1268 |
fail: |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1269 |
if (q) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1270 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1271 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1272 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1273 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1274 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1275 |
int asyncns_getnameinfo_done(asyncns_t *asyncns, asyncns_query_t* q, char *ret_host, size_t hostlen, char *ret_serv, size_t servlen) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1276 |
int ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1277 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1278 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1279 |
assert(q->asyncns == asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1280 |
assert(q->type == REQUEST_NAMEINFO); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1281 |
assert(!ret_host || hostlen); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1282 |
assert(!ret_serv || servlen); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1283 |
|
549 | 1284 |
if (asyncns->dead) { |
1285 |
errno = ECHILD; |
|
1286 |
return EAI_SYSTEM; |
|
1287 |
} |
|
1288 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1289 |
if (!q->done) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1290 |
return EAI_AGAIN; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1291 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1292 |
if (ret_host && q->host) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1293 |
strncpy(ret_host, q->host, hostlen); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1294 |
ret_host[hostlen-1] = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1295 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1296 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1297 |
if (ret_serv && q->serv) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1298 |
strncpy(ret_serv, q->serv, servlen); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1299 |
ret_serv[servlen-1] = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1300 |
} |
549 | 1301 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1302 |
ret = q->ret; |
549 | 1303 |
|
1304 |
if (ret == EAI_SYSTEM) |
|
1305 |
errno = q->_errno; |
|
1306 |
||
1307 |
if (ret != 0) |
|
1308 |
h_errno = q->_h_errno; |
|
1309 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1310 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1311 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1312 |
return ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1313 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1314 |
|
549 | 1315 |
static asyncns_query_t * asyncns_res(asyncns_t *asyncns, query_type_t qtype, const char *dname, int class, int type) { |
1316 |
res_request_t data[BUFSIZE/sizeof(res_request_t) + 1]; |
|
1317 |
res_request_t *req = data; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1318 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1319 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1320 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1321 |
assert(dname); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1322 |
|
549 | 1323 |
if (asyncns->dead) { |
1324 |
errno = ECHILD; |
|
1325 |
return NULL; |
|
1326 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1327 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1328 |
if (!(q = alloc_query(asyncns))) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1329 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1330 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1331 |
memset(req, 0, sizeof(res_request_t)); |
549 | 1332 |
|
1333 |
req->dname_len = strlen(dname) + 1; |
|
1334 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1335 |
req->header.id = q->id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1336 |
req->header.type = q->type = qtype; |
549 | 1337 |
req->header.length = sizeof(res_request_t) + req->dname_len; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1338 |
|
549 | 1339 |
if (req->header.length > BUFSIZE) { |
1340 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1341 |
goto fail; |
549 | 1342 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1343 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1344 |
req->class = class; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1345 |
req->type = type; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1346 |
|
549 | 1347 |
strcpy((char*) req + sizeof(res_request_t), dname); |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1348 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1349 |
if (send(asyncns->fds[REQUEST_SEND_FD], req, req->header.length, 0) < 0) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1350 |
goto fail; |
300
ce78172edafd
Fixed off-by-one error in SRV lookups
Senko Rasic <senko.rasic@collabora.co.uk>
parents:
271
diff
changeset
|
1351 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1352 |
return q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1353 |
|
549 | 1354 |
fail: |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1355 |
if (q) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1356 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1357 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1358 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1359 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1360 |
|
549 | 1361 |
asyncns_query_t* asyncns_res_query(asyncns_t *asyncns, const char *dname, int class, int type) { |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1362 |
return asyncns_res(asyncns, REQUEST_RES_QUERY, dname, class, type); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1363 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1364 |
|
549 | 1365 |
asyncns_query_t* asyncns_res_search(asyncns_t *asyncns, const char *dname, int class, int type) { |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1366 |
return asyncns_res(asyncns, REQUEST_RES_SEARCH, dname, class, type); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1367 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1368 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1369 |
int asyncns_res_done(asyncns_t *asyncns, asyncns_query_t* q, unsigned char **answer) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1370 |
int ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1371 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1372 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1373 |
assert(q->asyncns == asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1374 |
assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1375 |
assert(answer); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1376 |
|
549 | 1377 |
if (asyncns->dead) { |
1378 |
errno = ECHILD; |
|
1379 |
return -ECHILD; |
|
1380 |
} |
|
1381 |
||
1382 |
if (!q->done) { |
|
1383 |
errno = EAGAIN; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1384 |
return -EAGAIN; |
549 | 1385 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1386 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1387 |
*answer = (unsigned char *)q->serv; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1388 |
q->serv = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1389 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1390 |
ret = q->ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1391 |
|
549 | 1392 |
if (ret < 0) { |
1393 |
errno = q->_errno; |
|
1394 |
h_errno = q->_h_errno; |
|
1395 |
} |
|
1396 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1397 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1398 |
|
549 | 1399 |
return ret < 0 ? -errno : ret; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1400 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1401 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1402 |
asyncns_query_t* asyncns_getnext(asyncns_t *asyncns) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1403 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1404 |
return asyncns->done_head; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1405 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1406 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1407 |
int asyncns_getnqueries(asyncns_t *asyncns) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1408 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1409 |
return asyncns->n_queries; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1410 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1411 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1412 |
void asyncns_cancel(asyncns_t *asyncns, asyncns_query_t* q) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1413 |
int i; |
549 | 1414 |
int saved_errno = errno; |
1415 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1416 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1417 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1418 |
assert(q->asyncns == asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1419 |
assert(asyncns->n_queries > 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1420 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1421 |
if (q->done) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1422 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1423 |
if (q->done_prev) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1424 |
q->done_prev->done_next = q->done_next; |
549 | 1425 |
else |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1426 |
asyncns->done_head = q->done_next; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1427 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1428 |
if (q->done_next) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1429 |
q->done_next->done_prev = q->done_prev; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1430 |
else |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1431 |
asyncns->done_tail = q->done_prev; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1432 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1433 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1434 |
i = q->id % MAX_QUERIES; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1435 |
assert(asyncns->queries[i] == q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1436 |
asyncns->queries[i] = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1437 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1438 |
asyncns_freeaddrinfo(q->addrinfo); |
549 | 1439 |
free(q->host); |
1440 |
free(q->serv); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1441 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1442 |
asyncns->n_queries--; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1443 |
free(q); |
549 | 1444 |
|
1445 |
errno = saved_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1446 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1447 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1448 |
void asyncns_freeaddrinfo(struct addrinfo *ai) { |
549 | 1449 |
int saved_errno = errno; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1450 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1451 |
while (ai) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1452 |
struct addrinfo *next = ai->ai_next; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1453 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1454 |
free(ai->ai_addr); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1455 |
free(ai->ai_canonname); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1456 |
free(ai); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1457 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1458 |
ai = next; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1459 |
} |
549 | 1460 |
|
1461 |
errno = saved_errno; |
|
1462 |
} |
|
1463 |
||
1464 |
void asyncns_freeanswer(unsigned char *answer) { |
|
1465 |
int saved_errno = errno; |
|
1466 |
||
1467 |
if (!answer) |
|
1468 |
return; |
|
1469 |
||
1470 |
/* Please note that this function is new in libasyncns 0.4. In |
|
1471 |
* older versions you were supposed to free the answer directly |
|
1472 |
* with free(). Hence, if this function is changed to do more than |
|
1473 |
* just a simple free() this must be considered ABI/API breakage! */ |
|
1474 |
||
1475 |
free(answer); |
|
1476 |
||
1477 |
errno = saved_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1478 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1479 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1480 |
int asyncns_isdone(asyncns_t *asyncns, asyncns_query_t*q) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1481 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1482 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1483 |
assert(q->asyncns == asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1484 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1485 |
return q->done; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1486 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1487 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1488 |
void asyncns_setuserdata(asyncns_t *asyncns, asyncns_query_t *q, void *userdata) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1489 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1490 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1491 |
assert(q->asyncns = asyncns); |
549 | 1492 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1493 |
q->userdata = userdata; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1494 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1495 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1496 |
void* asyncns_getuserdata(asyncns_t *asyncns, asyncns_query_t *q) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1497 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1498 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1499 |
assert(q->asyncns = asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1500 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1501 |
return q->userdata; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1502 |
} |