author | Mikael Hallendal <micke@imendio.com> |
Sun, 26 Oct 2008 01:56:11 +0200 | |
changeset 550 | 104d37c1b24a |
parent 549 | 99a387fdb1bb |
child 559 | 6602623168cd |
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) { |
|
259 |
saved_errno = errno; |
|
260 |
closedir(d); |
|
261 |
errno = saved_errno; |
|
262 |
||
263 |
return -1; |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
closedir(d); |
|
268 |
return 0; |
|
269 |
} |
|
270 |
||
271 |
#endif |
|
272 |
||
273 |
if (getrlimit(RLIMIT_NOFILE, &rl) < 0) |
|
274 |
return -1; |
|
275 |
||
276 |
for (fd = 0; fd < (int) rl.rlim_max; fd++) { |
|
277 |
int i; |
|
278 |
||
279 |
if (fd <= 3) |
|
280 |
continue; |
|
281 |
||
282 |
for (i = 0; except_fds[i] >= 0; i++) |
|
283 |
if (except_fds[i] == fd) |
|
284 |
continue; |
|
285 |
||
286 |
if (close(fd) < 0 && errno != EBADF) |
|
287 |
return -1; |
|
288 |
} |
|
289 |
||
290 |
return 0; |
|
291 |
} |
|
292 |
||
293 |
static int reset_sigsv(const int except[]) { |
|
294 |
int sig; |
|
295 |
assert(except); |
|
296 |
||
550
104d37c1b24a
Make the new libasyncns compile on mac os x
Mikael Hallendal <micke@imendio.com>
parents:
549
diff
changeset
|
297 |
for (sig = 1; sig < NSIG; sig++) { |
549 | 298 |
int reset = 1; |
299 |
||
300 |
switch (sig) { |
|
301 |
case SIGKILL: |
|
302 |
case SIGSTOP: |
|
303 |
reset = 0; |
|
304 |
break; |
|
305 |
||
306 |
default: { |
|
307 |
int i; |
|
308 |
||
309 |
for (i = 0; except[i] > 0; i++) { |
|
310 |
if (sig == except[i]) { |
|
311 |
reset = 0; |
|
312 |
break; |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
||
318 |
if (reset) { |
|
319 |
struct sigaction sa; |
|
320 |
||
321 |
memset(&sa, 0, sizeof(sa)); |
|
322 |
sa.sa_handler = SIG_DFL; |
|
323 |
||
324 |
/* On Linux the first two RT signals are reserved by |
|
325 |
* glibc, and sigaction() will return EINVAL for them. */ |
|
326 |
if ((sigaction(sig, &sa, NULL) < 0)) |
|
327 |
if (errno != EINVAL) |
|
328 |
return -1; |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
return 0; |
|
333 |
} |
|
334 |
||
335 |
static int ignore_sigsv(const int ignore[]) { |
|
336 |
int i; |
|
337 |
assert(ignore); |
|
338 |
||
339 |
for (i = 0; ignore[i] > 0; i++) { |
|
340 |
struct sigaction sa; |
|
341 |
||
342 |
memset(&sa, 0, sizeof(sa)); |
|
343 |
sa.sa_handler = SIG_IGN; |
|
344 |
||
345 |
if ((sigaction(ignore[i], &sa, NULL) < 0)) |
|
346 |
return -1; |
|
347 |
} |
|
348 |
||
349 |
return 0; |
|
350 |
} |
|
351 |
||
352 |
#endif |
|
353 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
354 |
static int fd_nonblock(int fd) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
355 |
int i; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
356 |
assert(fd >= 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
357 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
358 |
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
|
359 |
return -1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
360 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
361 |
if (i & O_NONBLOCK) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
362 |
return 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
363 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
364 |
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
|
365 |
} |
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 |
static int fd_cloexec(int fd) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
368 |
int v; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
369 |
assert(fd >= 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
370 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
371 |
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
|
372 |
return -1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
373 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
374 |
if (v & FD_CLOEXEC) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
375 |
return 0; |
549 | 376 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
377 |
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
|
378 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
379 |
|
549 | 380 |
static int send_died(int out_fd) { |
381 |
rheader_t rh; |
|
382 |
assert(out_fd > 0); |
|
383 |
||
384 |
memset(&rh, 0, sizeof(rh)); |
|
385 |
rh.type = RESPONSE_DIED; |
|
386 |
rh.id = 0; |
|
387 |
rh.length = sizeof(rh); |
|
388 |
||
389 |
return send(out_fd, &rh, rh.length, 0); |
|
390 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
391 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
392 |
static void *serialize_addrinfo(void *p, const struct addrinfo *ai, size_t *length, size_t maxlength) { |
549 | 393 |
addrinfo_serialization_t s; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
394 |
size_t cnl, l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
395 |
assert(p); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
396 |
assert(ai); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
397 |
assert(length); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
398 |
assert(*length <= maxlength); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
399 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
400 |
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
|
401 |
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
|
402 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
403 |
if (*length + l > maxlength) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
404 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
405 |
|
549 | 406 |
s.ai_flags = ai->ai_flags; |
407 |
s.ai_family = ai->ai_family; |
|
408 |
s.ai_socktype = ai->ai_socktype; |
|
409 |
s.ai_protocol = ai->ai_protocol; |
|
410 |
s.ai_addrlen = ai->ai_addrlen; |
|
411 |
s.canonname_len = cnl; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
412 |
|
549 | 413 |
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
|
414 |
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
|
415 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
416 |
if (ai->ai_canonname) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
417 |
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
|
418 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
419 |
*length += l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
420 |
return (uint8_t*) p + l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
421 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
422 |
|
549 | 423 |
static int send_addrinfo_reply(int out_fd, unsigned id, int ret, struct addrinfo *ai, int _errno, int _h_errno) { |
424 |
addrinfo_response_t data[BUFSIZE/sizeof(addrinfo_response_t) + 1]; |
|
425 |
addrinfo_response_t *resp = data; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
426 |
assert(out_fd >= 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
427 |
|
549 | 428 |
memset(data, 0, sizeof(data)); |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
429 |
resp->header.type = RESPONSE_ADDRINFO; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
430 |
resp->header.id = id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
431 |
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
|
432 |
resp->ret = ret; |
549 | 433 |
resp->_errno = _errno; |
434 |
resp->_h_errno = _h_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
435 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
436 |
if (ret == 0 && ai) { |
549 | 437 |
void *p = data + 1; |
438 |
struct addrinfo *k; |
|
439 |
||
440 |
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
|
441 |
|
549 | 442 |
if (!(p = serialize_addrinfo(p, k, &resp->header.length, (char*) data + BUFSIZE - (char*) p))) { |
443 |
resp->ret = EAI_MEMORY; |
|
444 |
break; |
|
445 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
446 |
} |
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 |
if (ai) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
450 |
freeaddrinfo(ai); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
451 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
452 |
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
|
453 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
454 |
|
549 | 455 |
static int send_nameinfo_reply(int out_fd, unsigned id, int ret, const char *host, const char *serv, int _errno, int _h_errno) { |
456 |
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
|
457 |
size_t hl, sl; |
549 | 458 |
nameinfo_response_t *resp = data; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
459 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
460 |
assert(out_fd >= 0); |
549 | 461 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
462 |
sl = serv ? strlen(serv)+1 : 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
463 |
hl = host ? strlen(host)+1 : 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
464 |
|
549 | 465 |
memset(data, 0, sizeof(data)); |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
466 |
resp->header.type = RESPONSE_NAMEINFO; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
467 |
resp->header.id = id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
468 |
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
|
469 |
resp->ret = ret; |
549 | 470 |
resp->_errno = _errno; |
471 |
resp->_h_errno = _h_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
472 |
resp->hostlen = hl; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
473 |
resp->servlen = sl; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
474 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
475 |
assert(sizeof(data) >= resp->header.length); |
549 | 476 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
477 |
if (host) |
549 | 478 |
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
|
479 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
480 |
if (serv) |
549 | 481 |
memcpy((uint8_t *)data + sizeof(nameinfo_response_t) + hl, serv, sl); |
482 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
483 |
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
|
484 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
485 |
|
549 | 486 |
static int send_res_reply(int out_fd, unsigned id, const unsigned char *answer, int ret, int _errno, int _h_errno) { |
487 |
res_response_t data[BUFSIZE/sizeof(res_response_t) + 1]; |
|
488 |
res_response_t *resp = data; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
489 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
490 |
assert(out_fd >= 0); |
549 | 491 |
|
492 |
memset(data, 0, sizeof(data)); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
493 |
resp->header.type = RESPONSE_RES; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
494 |
resp->header.id = id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
495 |
resp->header.length = sizeof(res_response_t) + (ret < 0 ? 0 : ret); |
549 | 496 |
resp->ret = ret; |
497 |
resp->_errno = _errno; |
|
498 |
resp->_h_errno = _h_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
499 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
500 |
assert(sizeof(data) >= resp->header.length); |
549 | 501 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
502 |
if (ret > 0) |
549 | 503 |
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
|
504 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
505 |
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
|
506 |
} |
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 |
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
|
509 |
assert(out_fd >= 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
510 |
assert(req); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
511 |
assert(length >= sizeof(rheader_t)); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
512 |
assert(length == req->length); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
513 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
514 |
switch (req->type) { |
549 | 515 |
case REQUEST_ADDRINFO: { |
516 |
struct addrinfo ai, *result = NULL; |
|
517 |
const addrinfo_request_t *ai_req = (const addrinfo_request_t*) req; |
|
518 |
const char *node, *service; |
|
519 |
int ret; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
520 |
|
549 | 521 |
assert(length >= sizeof(addrinfo_request_t)); |
522 |
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
|
523 |
|
549 | 524 |
memset(&ai, 0, sizeof(ai)); |
525 |
ai.ai_flags = ai_req->ai_flags; |
|
526 |
ai.ai_family = ai_req->ai_family; |
|
527 |
ai.ai_socktype = ai_req->ai_socktype; |
|
528 |
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
|
529 |
|
549 | 530 |
node = ai_req->node_len ? (const char*) req + sizeof(addrinfo_request_t) : NULL; |
531 |
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
|
532 |
|
549 | 533 |
ret = getaddrinfo(node, service, |
534 |
ai_req->hints_is_null ? NULL : &ai, |
|
535 |
&result); |
|
536 |
||
537 |
/* send_addrinfo_reply() frees result */ |
|
538 |
return send_addrinfo_reply(out_fd, req->id, ret, result, errno, h_errno); |
|
539 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
540 |
|
549 | 541 |
case REQUEST_NAMEINFO: { |
542 |
int ret; |
|
543 |
const nameinfo_request_t *ni_req = (const nameinfo_request_t*) req; |
|
544 |
char hostbuf[NI_MAXHOST], servbuf[NI_MAXSERV]; |
|
545 |
struct sockaddr_storage sa; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
546 |
|
549 | 547 |
assert(length >= sizeof(nameinfo_request_t)); |
548 |
assert(length == sizeof(nameinfo_request_t) + ni_req->sockaddr_len); |
|
549 |
||
550 |
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
|
551 |
|
549 | 552 |
ret = getnameinfo((struct sockaddr *)&sa, ni_req->sockaddr_len, |
553 |
ni_req->gethost ? hostbuf : NULL, ni_req->gethost ? sizeof(hostbuf) : 0, |
|
554 |
ni_req->getserv ? servbuf : NULL, ni_req->getserv ? sizeof(servbuf) : 0, |
|
555 |
ni_req->flags); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
556 |
|
549 | 557 |
return send_nameinfo_reply(out_fd, req->id, ret, |
558 |
ret == 0 && ni_req->gethost ? hostbuf : NULL, |
|
559 |
ret == 0 && ni_req->getserv ? servbuf : NULL, |
|
560 |
errno, h_errno); |
|
561 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
562 |
|
549 | 563 |
case REQUEST_RES_QUERY: |
564 |
case REQUEST_RES_SEARCH: { |
|
565 |
int ret; |
|
566 |
HEADER answer[BUFSIZE/sizeof(HEADER) + 1]; |
|
567 |
const res_request_t *res_req = (const res_request_t *)req; |
|
568 |
const char *dname; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
569 |
|
549 | 570 |
assert(length >= sizeof(res_request_t)); |
571 |
assert(length == sizeof(res_request_t) + res_req->dname_len); |
|
572 |
||
573 |
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
|
574 |
|
549 | 575 |
if (req->type == REQUEST_RES_QUERY) |
576 |
ret = res_query(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE); |
|
577 |
else |
|
578 |
ret = res_search(dname, res_req->class, res_req->type, (unsigned char *) answer, BUFSIZE); |
|
579 |
||
580 |
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
|
581 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
582 |
|
549 | 583 |
case REQUEST_TERMINATE: |
584 |
/* Quit */ |
|
585 |
return -1; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
586 |
|
549 | 587 |
default: |
588 |
; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
589 |
} |
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 |
return 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
592 |
} |
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 |
#ifndef HAVE_PTHREAD |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
595 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
596 |
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
|
597 |
int have_death_sig = 0; |
549 | 598 |
int good_fds[3]; |
599 |
int ret = 1; |
|
600 |
||
601 |
const int ignore_sigs[] = { |
|
602 |
SIGINT, |
|
603 |
SIGHUP, |
|
604 |
SIGPIPE, |
|
605 |
SIGUSR1, |
|
606 |
SIGUSR2, |
|
607 |
-1 |
|
608 |
}; |
|
609 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
610 |
assert(in_fd > 2); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
611 |
assert(out_fd > 2); |
549 | 612 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
613 |
close(0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
614 |
close(1); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
615 |
close(2); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
616 |
|
549 | 617 |
if (open("/dev/null", O_RDONLY) != 0) |
618 |
goto fail; |
|
619 |
||
620 |
if (open("/dev/null", O_WRONLY) != 1) |
|
621 |
goto fail; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
622 |
|
549 | 623 |
if (open("/dev/null", O_WRONLY) != 2) |
624 |
goto fail; |
|
625 |
||
626 |
if (chdir("/") < 0) |
|
627 |
goto fail; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
628 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
629 |
if (geteuid() == 0) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
630 |
struct passwd *pw; |
549 | 631 |
int r; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
632 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
633 |
if ((pw = getpwnam("nobody"))) { |
549 | 634 |
#ifdef HAVE_SETRESUID |
635 |
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
|
636 |
#elif HAVE_SETREUID |
549 | 637 |
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
|
638 |
#else |
549 | 639 |
if ((r = setuid(pw->pw_uid)) >= 0) |
640 |
r = seteuid(pw->pw_uid); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
641 |
#endif |
549 | 642 |
if (r < 0) |
643 |
goto fail; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
644 |
} |
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 |
|
549 | 647 |
if (reset_sigsv(ignore_sigs) < 0) |
648 |
goto fail; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
649 |
|
549 | 650 |
if (ignore_sigsv(ignore_sigs) < 0) |
651 |
goto fail; |
|
652 |
||
653 |
good_fds[0] = in_fd; good_fds[1] = out_fd; good_fds[2] = -1; |
|
654 |
if (close_allv(good_fds) < 0) |
|
655 |
goto fail; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
656 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
657 |
#ifdef PR_SET_PDEATHSIG |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
658 |
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
|
659 |
have_death_sig = 1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
660 |
#endif |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
661 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
662 |
if (!have_death_sig) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
663 |
fd_nonblock(in_fd); |
549 | 664 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
665 |
while (getppid() > 1) { /* if the parent PID is 1 our parent process died. */ |
549 | 666 |
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
|
667 |
ssize_t length; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
668 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
669 |
if (!have_death_sig) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
670 |
fd_set fds; |
549 | 671 |
struct timeval tv = { 0, 500000 }; |
672 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
673 |
FD_ZERO(&fds); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
674 |
FD_SET(in_fd, &fds); |
549 | 675 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
676 |
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
|
677 |
break; |
549 | 678 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
679 |
if (getppid() == 1) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
680 |
break; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
681 |
} |
549 | 682 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
683 |
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
|
684 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
685 |
if (length < 0 && errno == EAGAIN) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
686 |
continue; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
687 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
688 |
break; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
689 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
690 |
|
549 | 691 |
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
|
692 |
break; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
693 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
694 |
|
549 | 695 |
ret = 0; |
696 |
||
697 |
fail: |
|
698 |
||
699 |
send_died(out_fd); |
|
700 |
||
701 |
return ret; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
702 |
} |
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 |
#else |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
705 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
706 |
static void* thread_worker(void *p) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
707 |
sigset_t fullset; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
708 |
int *fds = p; |
549 | 709 |
int in_fd, out_fd; |
710 |
||
711 |
in_fd = fds[REQUEST_RECV_FD]; |
|
712 |
out_fd = fds[RESPONSE_SEND_FD]; |
|
713 |
free(p); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
714 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
715 |
/* No signals in this thread please */ |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
716 |
sigfillset(&fullset); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
717 |
pthread_sigmask(SIG_BLOCK, &fullset, NULL); |
549 | 718 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
719 |
for (;;) { |
549 | 720 |
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
|
721 |
ssize_t length; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
722 |
|
549 | 723 |
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
|
724 |
break; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
725 |
|
549 | 726 |
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
|
727 |
break; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
728 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
729 |
|
549 | 730 |
send_died(out_fd); |
731 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
732 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
733 |
} |
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 |
#endif |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
736 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
737 |
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
|
738 |
asyncns_t *asyncns = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
739 |
int i; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
740 |
assert(n_proc >= 1); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
741 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
742 |
if (n_proc > MAX_WORKERS) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
743 |
n_proc = MAX_WORKERS; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
744 |
|
549 | 745 |
if (!(asyncns = malloc(sizeof(asyncns_t)))) { |
746 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
747 |
goto fail; |
549 | 748 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
749 |
|
549 | 750 |
asyncns->dead = 0; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
751 |
asyncns->valid_workers = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
752 |
|
549 | 753 |
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
|
754 |
asyncns->fds[i] = -1; |
549 | 755 |
|
756 |
memset(asyncns->queries, 0, sizeof(asyncns->queries)); |
|
757 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
758 |
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
|
759 |
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
|
760 |
goto fail; |
549 | 761 |
|
762 |
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
|
763 |
fd_cloexec(asyncns->fds[i]); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
764 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
765 |
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
|
766 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
767 |
#ifndef HAVE_PTHREAD |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
768 |
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
|
769 |
goto fail; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
770 |
else if (asyncns->workers[asyncns->valid_workers] == 0) { |
549 | 771 |
int ret; |
772 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
773 |
close(asyncns->fds[REQUEST_SEND_FD]); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
774 |
close(asyncns->fds[RESPONSE_RECV_FD]); |
549 | 775 |
ret = process_worker(asyncns->fds[REQUEST_RECV_FD], asyncns->fds[RESPONSE_SEND_FD]); |
776 |
close(asyncns->fds[REQUEST_RECV_FD]); |
|
777 |
close(asyncns->fds[RESPONSE_SEND_FD]); |
|
778 |
_exit(ret); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
779 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
780 |
#else |
549 | 781 |
|
782 |
int *fds, r; |
|
783 |
||
784 |
/* We need to copy this array because otherwise we might have |
|
785 |
* a small chance of a race where the thread accesses fds when |
|
786 |
* *asyncns is already dead */ |
|
787 |
||
788 |
if (!(fds = malloc(sizeof(asyncns->fds)))) { |
|
789 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
790 |
goto fail; |
549 | 791 |
} |
792 |
||
793 |
memcpy(fds, asyncns->fds, sizeof(asyncns->fds)); |
|
794 |
||
795 |
if ((r = pthread_create(&asyncns->workers[asyncns->valid_workers], NULL, thread_worker, fds)) != 0) { |
|
796 |
free(fds); |
|
797 |
errno = r; |
|
798 |
goto fail; |
|
799 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
800 |
#endif |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
801 |
} |
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 |
#ifndef HAVE_PTHREAD |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
804 |
close(asyncns->fds[REQUEST_RECV_FD]); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
805 |
close(asyncns->fds[RESPONSE_SEND_FD]); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
806 |
asyncns->fds[REQUEST_RECV_FD] = asyncns->fds[RESPONSE_SEND_FD] = -1; |
549 | 807 |
#endif |
808 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
809 |
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
|
810 |
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
|
811 |
asyncns->n_queries = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
812 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
813 |
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
|
814 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
815 |
return asyncns; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
816 |
|
549 | 817 |
fail: |
818 |
if (asyncns) |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
819 |
asyncns_free(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
820 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
821 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
822 |
} |
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 |
void asyncns_free(asyncns_t *asyncns) { |
549 | 825 |
int i; |
826 |
int saved_errno = errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
827 |
unsigned p; |
549 | 828 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
829 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
830 |
|
549 | 831 |
if (asyncns->fds[REQUEST_SEND_FD] >= 0) { |
832 |
rheader_t req; |
|
833 |
||
834 |
memset(&req, 0, sizeof(req)); |
|
835 |
req.type = REQUEST_TERMINATE; |
|
836 |
req.length = sizeof(req); |
|
837 |
req.id = 0; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
838 |
|
549 | 839 |
/* Send one termiantion packet for each worker */ |
840 |
for (p = 0; p < asyncns->valid_workers; p++) |
|
841 |
send(asyncns->fds[REQUEST_SEND_FD], &req, req.length, 0); |
|
842 |
} |
|
843 |
||
844 |
/* Close all communication channels */ |
|
845 |
for (i = 0; i < MESSAGE_FD_MAX; i++) |
|
846 |
if (asyncns->fds[i] >= 0) |
|
847 |
close(asyncns->fds[i]); |
|
848 |
||
849 |
/* Now terminate them forcibly */ |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
850 |
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
|
851 |
#ifndef HAVE_PTHREAD |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
852 |
kill(asyncns->workers[p], SIGTERM); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
853 |
waitpid(asyncns->workers[p], NULL, 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
854 |
#else |
549 | 855 |
pthread_detach(asyncns->workers[p]); |
856 |
||
857 |
/* We don't join the thread here because there is no clean way |
|
858 |
to cancel a running lookup if one should be active. So it |
|
859 |
might take a while until the lookup thread actually |
|
860 |
terminates. But we don't really care, because it won't leak |
|
861 |
resources. */ |
|
862 |
#endif |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
863 |
} |
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 |
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
|
866 |
if (asyncns->queries[p]) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
867 |
asyncns_cancel(asyncns, asyncns->queries[p]); |
549 | 868 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
869 |
free(asyncns); |
549 | 870 |
|
871 |
errno = saved_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
872 |
} |
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 |
int asyncns_fd(asyncns_t *asyncns) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
875 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
876 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
877 |
return asyncns->fds[RESPONSE_RECV_FD]; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
878 |
} |
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 |
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
|
881 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
882 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
883 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
884 |
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
|
885 |
if (q->id == id) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
886 |
return q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
887 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
888 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
889 |
} |
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 |
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
|
892 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
893 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
894 |
assert(!q->done); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
895 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
896 |
q->done = 1; |
549 | 897 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
898 |
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
|
899 |
asyncns->done_tail->done_next = q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
900 |
else |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
901 |
asyncns->done_head = q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
902 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
903 |
asyncns->done_tail = q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
904 |
q->done_next = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
905 |
} |
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 |
static void *unserialize_addrinfo(void *p, struct addrinfo **ret_ai, size_t *length) { |
549 | 908 |
addrinfo_serialization_t s; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
909 |
size_t l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
910 |
struct addrinfo *ai; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
911 |
assert(p); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
912 |
assert(ret_ai); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
913 |
assert(length); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
914 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
915 |
if (*length < sizeof(addrinfo_serialization_t)) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
916 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
917 |
|
549 | 918 |
memcpy(&s, p, sizeof(s)); |
919 |
||
920 |
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
|
921 |
if (*length < l) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
922 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
923 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
924 |
if (!(ai = malloc(sizeof(struct addrinfo)))) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
925 |
goto fail; |
549 | 926 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
927 |
ai->ai_addr = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
928 |
ai->ai_canonname = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
929 |
ai->ai_next = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
930 |
|
549 | 931 |
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
|
932 |
goto fail; |
549 | 933 |
|
934 |
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
|
935 |
goto fail; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
936 |
|
549 | 937 |
ai->ai_flags = s.ai_flags; |
938 |
ai->ai_family = s.ai_family; |
|
939 |
ai->ai_socktype = s.ai_socktype; |
|
940 |
ai->ai_protocol = s.ai_protocol; |
|
941 |
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
|
942 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
943 |
if (ai->ai_addr) |
549 | 944 |
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
|
945 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
946 |
if (ai->ai_canonname) |
549 | 947 |
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
|
948 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
949 |
*length -= l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
950 |
*ret_ai = ai; |
549 | 951 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
952 |
return (uint8_t*) p + l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
953 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
954 |
|
549 | 955 |
fail: |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
956 |
if (ai) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
957 |
asyncns_freeaddrinfo(ai); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
958 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
959 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
960 |
} |
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 |
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
|
963 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
964 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
965 |
assert(resp); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
966 |
assert(length >= sizeof(rheader_t)); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
967 |
assert(length == resp->length); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
968 |
|
549 | 969 |
if (resp->type == RESPONSE_DIED) { |
970 |
asyncns->dead = 1; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
971 |
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
|
972 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
973 |
|
549 | 974 |
if (!(q = lookup_query(asyncns, resp->id))) |
975 |
return 0; |
|
976 |
||
977 |
switch (resp->type) { |
|
978 |
case RESPONSE_ADDRINFO: { |
|
979 |
const addrinfo_response_t *ai_resp = (addrinfo_response_t*) resp; |
|
980 |
void *p; |
|
981 |
size_t l; |
|
982 |
struct addrinfo *prev = NULL; |
|
983 |
||
984 |
assert(length >= sizeof(addrinfo_response_t)); |
|
985 |
assert(q->type == REQUEST_ADDRINFO); |
|
986 |
||
987 |
q->ret = ai_resp->ret; |
|
988 |
q->_errno = ai_resp->_errno; |
|
989 |
q->_h_errno = ai_resp->_h_errno; |
|
990 |
l = length - sizeof(addrinfo_response_t); |
|
991 |
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
|
992 |
|
549 | 993 |
while (l > 0 && p) { |
994 |
struct addrinfo *ai = NULL; |
|
995 |
p = unserialize_addrinfo(p, &ai, &l); |
|
996 |
||
997 |
if (!p || !ai) { |
|
998 |
q->ret = EAI_MEMORY; |
|
999 |
break; |
|
1000 |
} |
|
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
|
1001 |
|
549 | 1002 |
if (prev) |
1003 |
prev->ai_next = ai; |
|
1004 |
else |
|
1005 |
q->addrinfo = ai; |
|
1006 |
||
1007 |
prev = ai; |
|
1008 |
} |
|
1009 |
||
1010 |
complete_query(asyncns, q); |
|
1011 |
break; |
|
1012 |
} |
|
1013 |
||
1014 |
case RESPONSE_NAMEINFO: { |
|
1015 |
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
|
1016 |
|
549 | 1017 |
assert(length >= sizeof(nameinfo_response_t)); |
1018 |
assert(q->type == REQUEST_NAMEINFO); |
|
1019 |
||
1020 |
q->ret = ni_resp->ret; |
|
1021 |
q->_errno = ni_resp->_errno; |
|
1022 |
q->_h_errno = ni_resp->_h_errno; |
|
1023 |
||
1024 |
if (ni_resp->hostlen) |
|
1025 |
if (!(q->host = strndup((const char*) ni_resp + sizeof(nameinfo_response_t), ni_resp->hostlen-1))) |
|
1026 |
q->ret = EAI_MEMORY; |
|
1027 |
||
1028 |
if (ni_resp->servlen) |
|
1029 |
if (!(q->serv = strndup((const char*) ni_resp + sizeof(nameinfo_response_t) + ni_resp->hostlen, ni_resp->servlen-1))) |
|
1030 |
q->ret = EAI_MEMORY; |
|
1031 |
||
1032 |
complete_query(asyncns, q); |
|
1033 |
break; |
|
1034 |
} |
|
1035 |
||
1036 |
case RESPONSE_RES: { |
|
1037 |
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
|
1038 |
|
549 | 1039 |
assert(length >= sizeof(res_response_t)); |
1040 |
assert(q->type == REQUEST_RES_QUERY || q->type == REQUEST_RES_SEARCH); |
|
1041 |
||
1042 |
q->ret = res_resp->ret; |
|
1043 |
q->_errno = res_resp->_errno; |
|
1044 |
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
|
1045 |
|
549 | 1046 |
if (res_resp->ret >= 0) { |
1047 |
if (!(q->serv = malloc(res_resp->ret))) { |
|
1048 |
q->ret = -1; |
|
1049 |
q->_errno = ENOMEM; |
|
1050 |
} else |
|
1051 |
memcpy(q->serv, (char *)resp + sizeof(res_response_t), res_resp->ret); |
|
1052 |
} |
|
1053 |
||
1054 |
complete_query(asyncns, q); |
|
1055 |
break; |
|
1056 |
} |
|
1057 |
||
1058 |
default: |
|
1059 |
; |
|
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
|
1060 |
} |
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 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1062 |
return 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1063 |
} |
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 |
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
|
1066 |
int handled = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1067 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1068 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1069 |
for (;;) { |
549 | 1070 |
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
|
1071 |
ssize_t l; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1072 |
|
549 | 1073 |
if (asyncns->dead) { |
1074 |
errno = ECHILD; |
|
1075 |
return -1; |
|
1076 |
} |
|
1077 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1078 |
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
|
1079 |
fd_set fds; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1080 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1081 |
if (errno != EAGAIN) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1082 |
return -1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1083 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1084 |
if (!block || handled) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1085 |
return 0; |
549 | 1086 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1087 |
FD_ZERO(&fds); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1088 |
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
|
1089 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1090 |
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
|
1091 |
return -1; |
549 | 1092 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1093 |
continue; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1094 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1095 |
|
549 | 1096 |
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
|
1097 |
return -1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1098 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1099 |
handled = 1; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1100 |
} |
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 |
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
|
1104 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1105 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1106 |
|
549 | 1107 |
if (asyncns->n_queries >= MAX_QUERIES) { |
1108 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1109 |
return NULL; |
549 | 1110 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1111 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1112 |
while (asyncns->queries[asyncns->current_index]) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1113 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1114 |
asyncns->current_index++; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1115 |
asyncns->current_id++; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1116 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1117 |
while (asyncns->current_index >= MAX_QUERIES) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1118 |
asyncns->current_index -= MAX_QUERIES; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1119 |
} |
549 | 1120 |
|
1121 |
if (!(q = asyncns->queries[asyncns->current_index] = malloc(sizeof(asyncns_query_t)))) { |
|
1122 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1123 |
return NULL; |
549 | 1124 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1125 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1126 |
asyncns->n_queries++; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1127 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1128 |
q->asyncns = asyncns; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1129 |
q->done = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1130 |
q->id = asyncns->current_id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1131 |
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
|
1132 |
q->ret = 0; |
549 | 1133 |
q->_errno = 0; |
1134 |
q->_h_errno = 0; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1135 |
q->addrinfo = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1136 |
q->userdata = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1137 |
q->host = q->serv = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1138 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1139 |
return q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1140 |
} |
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 |
asyncns_query_t* asyncns_getaddrinfo(asyncns_t *asyncns, const char *node, const char *service, const struct addrinfo *hints) { |
549 | 1143 |
addrinfo_request_t data[BUFSIZE/sizeof(addrinfo_request_t) + 1]; |
1144 |
addrinfo_request_t *req = data; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1145 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1146 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1147 |
assert(node || service); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1148 |
|
549 | 1149 |
if (asyncns->dead) { |
1150 |
errno = ECHILD; |
|
1151 |
return NULL; |
|
1152 |
} |
|
1153 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1154 |
if (!(q = alloc_query(asyncns))) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1155 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1156 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1157 |
memset(req, 0, sizeof(addrinfo_request_t)); |
549 | 1158 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1159 |
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
|
1160 |
req->service_len = service ? strlen(service)+1 : 0; |
549 | 1161 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1162 |
req->header.id = q->id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1163 |
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
|
1164 |
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
|
1165 |
|
549 | 1166 |
if (req->header.length > BUFSIZE) { |
1167 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1168 |
goto fail; |
549 | 1169 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1170 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1171 |
if (!(req->hints_is_null = !hints)) { |
549 | 1172 |
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
|
1173 |
req->ai_family = hints->ai_family; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1174 |
req->ai_socktype = hints->ai_socktype; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1175 |
req->ai_protocol = hints->ai_protocol; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1176 |
} |
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 |
if (node) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1179 |
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
|
1180 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1181 |
if (service) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1182 |
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
|
1183 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1184 |
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
|
1185 |
goto fail; |
549 | 1186 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1187 |
return q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1188 |
|
549 | 1189 |
fail: |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1190 |
if (q) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1191 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1192 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1193 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1194 |
} |
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 |
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
|
1197 |
int ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1198 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1199 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1200 |
assert(q->asyncns == asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1201 |
assert(q->type == REQUEST_ADDRINFO); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1202 |
|
549 | 1203 |
if (asyncns->dead) { |
1204 |
errno = ECHILD; |
|
1205 |
return EAI_SYSTEM; |
|
1206 |
} |
|
1207 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1208 |
if (!q->done) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1209 |
return EAI_AGAIN; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1210 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1211 |
*ret_res = q->addrinfo; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1212 |
q->addrinfo = NULL; |
549 | 1213 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1214 |
ret = q->ret; |
549 | 1215 |
|
1216 |
if (ret == EAI_SYSTEM) |
|
1217 |
errno = q->_errno; |
|
1218 |
||
1219 |
if (ret != 0) |
|
1220 |
h_errno = q->_h_errno; |
|
1221 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1222 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1223 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1224 |
return ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1225 |
} |
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 |
asyncns_query_t* asyncns_getnameinfo(asyncns_t *asyncns, const struct sockaddr *sa, socklen_t salen, int flags, int gethost, int getserv) { |
549 | 1228 |
nameinfo_request_t data[BUFSIZE/sizeof(nameinfo_request_t) + 1]; |
1229 |
nameinfo_request_t *req = data; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1230 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1231 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1232 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1233 |
assert(sa); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1234 |
assert(salen > 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1235 |
|
549 | 1236 |
if (asyncns->dead) { |
1237 |
errno = ECHILD; |
|
1238 |
return NULL; |
|
1239 |
} |
|
1240 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1241 |
if (!(q = alloc_query(asyncns))) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1242 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1243 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1244 |
memset(req, 0, sizeof(nameinfo_request_t)); |
549 | 1245 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1246 |
req->header.id = q->id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1247 |
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
|
1248 |
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
|
1249 |
|
549 | 1250 |
if (req->header.length > BUFSIZE) { |
1251 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1252 |
goto fail; |
549 | 1253 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1254 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1255 |
req->flags = flags; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1256 |
req->sockaddr_len = salen; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1257 |
req->gethost = gethost; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1258 |
req->getserv = getserv; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1259 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1260 |
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
|
1261 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1262 |
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
|
1263 |
goto fail; |
549 | 1264 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1265 |
return q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1266 |
|
549 | 1267 |
fail: |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1268 |
if (q) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1269 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1270 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1271 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1272 |
} |
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 |
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
|
1275 |
int ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1276 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1277 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1278 |
assert(q->asyncns == asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1279 |
assert(q->type == REQUEST_NAMEINFO); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1280 |
assert(!ret_host || hostlen); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1281 |
assert(!ret_serv || servlen); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1282 |
|
549 | 1283 |
if (asyncns->dead) { |
1284 |
errno = ECHILD; |
|
1285 |
return EAI_SYSTEM; |
|
1286 |
} |
|
1287 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1288 |
if (!q->done) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1289 |
return EAI_AGAIN; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1290 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1291 |
if (ret_host && q->host) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1292 |
strncpy(ret_host, q->host, hostlen); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1293 |
ret_host[hostlen-1] = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1294 |
} |
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 |
if (ret_serv && q->serv) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1297 |
strncpy(ret_serv, q->serv, servlen); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1298 |
ret_serv[servlen-1] = 0; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1299 |
} |
549 | 1300 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1301 |
ret = q->ret; |
549 | 1302 |
|
1303 |
if (ret == EAI_SYSTEM) |
|
1304 |
errno = q->_errno; |
|
1305 |
||
1306 |
if (ret != 0) |
|
1307 |
h_errno = q->_h_errno; |
|
1308 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1309 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1310 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1311 |
return ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1312 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1313 |
|
549 | 1314 |
static asyncns_query_t * asyncns_res(asyncns_t *asyncns, query_type_t qtype, const char *dname, int class, int type) { |
1315 |
res_request_t data[BUFSIZE/sizeof(res_request_t) + 1]; |
|
1316 |
res_request_t *req = data; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1317 |
asyncns_query_t *q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1318 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1319 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1320 |
assert(dname); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1321 |
|
549 | 1322 |
if (asyncns->dead) { |
1323 |
errno = ECHILD; |
|
1324 |
return NULL; |
|
1325 |
} |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1326 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1327 |
if (!(q = alloc_query(asyncns))) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1328 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1329 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1330 |
memset(req, 0, sizeof(res_request_t)); |
549 | 1331 |
|
1332 |
req->dname_len = strlen(dname) + 1; |
|
1333 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1334 |
req->header.id = q->id; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1335 |
req->header.type = q->type = qtype; |
549 | 1336 |
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
|
1337 |
|
549 | 1338 |
if (req->header.length > BUFSIZE) { |
1339 |
errno = ENOMEM; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1340 |
goto fail; |
549 | 1341 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1342 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1343 |
req->class = class; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1344 |
req->type = type; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1345 |
|
549 | 1346 |
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
|
1347 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1348 |
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
|
1349 |
goto fail; |
300
ce78172edafd
Fixed off-by-one error in SRV lookups
Senko Rasic <senko.rasic@collabora.co.uk>
parents:
271
diff
changeset
|
1350 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1351 |
return q; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1352 |
|
549 | 1353 |
fail: |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1354 |
if (q) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1355 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1356 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1357 |
return NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1358 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1359 |
|
549 | 1360 |
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
|
1361 |
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
|
1362 |
} |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1363 |
|
549 | 1364 |
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
|
1365 |
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
|
1366 |
} |
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 |
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
|
1369 |
int ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1370 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1371 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1372 |
assert(q->asyncns == asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1373 |
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
|
1374 |
assert(answer); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1375 |
|
549 | 1376 |
if (asyncns->dead) { |
1377 |
errno = ECHILD; |
|
1378 |
return -ECHILD; |
|
1379 |
} |
|
1380 |
||
1381 |
if (!q->done) { |
|
1382 |
errno = EAGAIN; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1383 |
return -EAGAIN; |
549 | 1384 |
} |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1385 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1386 |
*answer = (unsigned char *)q->serv; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1387 |
q->serv = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1388 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1389 |
ret = q->ret; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1390 |
|
549 | 1391 |
if (ret < 0) { |
1392 |
errno = q->_errno; |
|
1393 |
h_errno = q->_h_errno; |
|
1394 |
} |
|
1395 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1396 |
asyncns_cancel(asyncns, q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1397 |
|
549 | 1398 |
return ret < 0 ? -errno : ret; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1399 |
} |
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 |
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
|
1402 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1403 |
return asyncns->done_head; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1404 |
} |
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 |
int asyncns_getnqueries(asyncns_t *asyncns) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1407 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1408 |
return asyncns->n_queries; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1409 |
} |
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 |
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
|
1412 |
int i; |
549 | 1413 |
int saved_errno = errno; |
1414 |
||
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1415 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1416 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1417 |
assert(q->asyncns == asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1418 |
assert(asyncns->n_queries > 0); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1419 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1420 |
if (q->done) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1421 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1422 |
if (q->done_prev) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1423 |
q->done_prev->done_next = q->done_next; |
549 | 1424 |
else |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1425 |
asyncns->done_head = q->done_next; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1426 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1427 |
if (q->done_next) |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1428 |
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
|
1429 |
else |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1430 |
asyncns->done_tail = q->done_prev; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1431 |
} |
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 |
i = q->id % MAX_QUERIES; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1434 |
assert(asyncns->queries[i] == q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1435 |
asyncns->queries[i] = NULL; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1436 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1437 |
asyncns_freeaddrinfo(q->addrinfo); |
549 | 1438 |
free(q->host); |
1439 |
free(q->serv); |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1440 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1441 |
asyncns->n_queries--; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1442 |
free(q); |
549 | 1443 |
|
1444 |
errno = saved_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1445 |
} |
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 |
void asyncns_freeaddrinfo(struct addrinfo *ai) { |
549 | 1448 |
int saved_errno = errno; |
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1449 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1450 |
while (ai) { |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1451 |
struct addrinfo *next = ai->ai_next; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1452 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1453 |
free(ai->ai_addr); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1454 |
free(ai->ai_canonname); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1455 |
free(ai); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1456 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1457 |
ai = next; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1458 |
} |
549 | 1459 |
|
1460 |
errno = saved_errno; |
|
1461 |
} |
|
1462 |
||
1463 |
void asyncns_freeanswer(unsigned char *answer) { |
|
1464 |
int saved_errno = errno; |
|
1465 |
||
1466 |
if (!answer) |
|
1467 |
return; |
|
1468 |
||
1469 |
/* Please note that this function is new in libasyncns 0.4. In |
|
1470 |
* older versions you were supposed to free the answer directly |
|
1471 |
* with free(). Hence, if this function is changed to do more than |
|
1472 |
* just a simple free() this must be considered ABI/API breakage! */ |
|
1473 |
||
1474 |
free(answer); |
|
1475 |
||
1476 |
errno = saved_errno; |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1477 |
} |
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 |
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
|
1480 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1481 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1482 |
assert(q->asyncns == asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1483 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1484 |
return q->done; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1485 |
} |
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 |
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
|
1488 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1489 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1490 |
assert(q->asyncns = asyncns); |
549 | 1491 |
|
271
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1492 |
q->userdata = userdata; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1493 |
} |
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 |
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
|
1496 |
assert(q); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1497 |
assert(asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1498 |
assert(q->asyncns = asyncns); |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1499 |
|
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1500 |
return q->userdata; |
52ea4e0b897a
Perform name resolution asynchronously. Patch by Senko Rašić.
Dafydd Harries <daf@rhydd.org>
parents:
diff
changeset
|
1501 |
} |