summaryrefslogtreecommitdiff
path: root/common/async-resolver.c
blob: c734bffca1d122ae294bcef7a66b3c515f752f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>

#include "async-resolver.h"
#include "server-mainloop.h"

/* -----------------------------------------------------------------------------
 * THREAD COMMUNICATION
 */

#define TSIGNAL_UNITITIALIZED  { -1 -1 }

static int
tsignal_init(int* sig)
{
    if(pipe(sig) == -1)
        return -1;
    fcntl(sig[0], F_SETFL, fcntl(sig[0], F_GETFL, 0) | O_NONBLOCK);
    return 0;
}

static int
tsignal_get_fd(int* sig)
{
    return sig[0];
}

static void
tsignal_wake(int* sig)
{
    write(sig[1], "1", 1);
}

static void
tsignal_clear(int* sig)
{
    char buf[16];
    while(read(sig[0], buf, sizeof(buf)) > 0);
}

static void
tsignal_wait(int* sig, struct timeval* tv)
{
    fd_set watch;
    FD_ZERO(&watch);
    FD_SET(sig[0], &watch);
    select(sig[0], &watch, NULL, NULL, tv);
}

static void
tsignal_uninit(int* sig)
{
    if(sig[1] != -1)
        close(sig[1]);
    sig[1] = -1;
    if(sig[0] != -1)
        close(sig[0]);
    sig[0] = -1;
}

/* -----------------------------------------------------------------------------
 * RESOLVER
 */

typedef struct _resolve_request
{
    char hostname[256];
    char servname[256];
    async_resolve_callback cb;
    void *arg;

    int gaierr;
    int errn;
    struct addrinfo *ai;

    struct _resolve_request *next;
}
resolve_request;

/* The queues */
static int res_quit = 0;
static resolve_request* res_requests = NULL;
static resolve_request* res_done = NULL;

/* Thread communication */
static pthread_t res_thread = 0;
static pthread_mutex_t res_mutex = PTHREAD_MUTEX_INITIALIZER;
static int res_request_signal[2] = TSIGNAL_UNITITIALIZED;
static int res_done_signal[2] = TSIGNAL_UNITITIALIZED;

static void*
resolver_thread(void* arg)
{
    resolve_request* req;
    resolve_request* r;
    struct timeval tv;

    while(!res_quit)
    {
        pthread_mutex_lock(&res_mutex);

            /* Dig out any requests */
            req = res_requests;
            if(req)
            {
                res_requests = req->next;
                req->next = NULL;
            }

        pthread_mutex_unlock(&res_mutex);

        /* No requests, wait for a request */
        if(!req)
        {
            tv.tv_sec = 0;
            tv.tv_usec = 500000;
            tsignal_wait(res_request_signal, &tv);
            tsignal_clear(res_request_signal);
            continue;
        }

        /* The actual resolve */
        req->gaierr = getaddrinfo(req->hostname, req->servname[0] ? req->servname : NULL,
                                 NULL, &req->ai);
        req->errn = errno;

        /* A timeout */
        if(!req->gaierr && !req->ai)
        {
            req->gaierr = EAI_SYSTEM;
            req->errn = ETIMEDOUT;
        }

        /* Append the result to done */
        pthread_mutex_lock(&res_mutex);

            if(!res_done)
            {
                res_done = req;
            }
            else
            {
                r = res_done;
                while(r->next)
                    r = r->next;
                r->next = req;
            }

        pthread_mutex_unlock(&res_mutex);

        /* Tell the main thread to check outbound */
        tsignal_wake(res_done_signal);
    }

    return NULL;
}

static void
resolver_done(int fd, int type, void* arg)
{
    resolve_request* req;
    resolve_request* r;

    tsignal_clear(res_done_signal);

    pthread_mutex_lock(&res_mutex);

        req = res_done;
        res_done = NULL;

    pthread_mutex_unlock(&res_mutex);

    while(req)
    {
        /* Send off the result */
        errno = req->errn;
        (req->cb)(req->gaierr, req->ai, req->arg);

        /* And free it all */
        r = req->next;
        if(req->ai)
            freeaddrinfo(req->ai);
        free(req);

        req = r;
    }
}

int
async_resolver_init()
{
    int r;

    /* The signal pipes */
    if(tsignal_init(res_request_signal) < 0)
        return -1;
    if(tsignal_init(res_done_signal) < 0)
        return -1;

    if(server_watch(tsignal_get_fd(res_done_signal), SERVER_READ, resolver_done, NULL) == -1)
        return -1;

    r = pthread_create(&res_thread, NULL, resolver_thread, NULL);
    if(r != 0)
    {
        res_thread = 0;
        return -1;
    }

    return 0;
}

void
async_resolver_queue(const char* hostname, const char* servname,
                     async_resolve_callback cb, void* arg)
{
    resolve_request* req;
    resolve_request* r;
    char* t;

    req = calloc(1, sizeof(resolve_request));
    if(!req)
    {
        /* All errors go to callback */
        (cb)(EAI_MEMORY, NULL, arg);
        return;
    }

    req->cb = cb;
    req->arg = arg;

    strncpy(req->hostname, hostname, sizeof(req->hostname));
    req->hostname[sizeof(req->hostname) - 1] = 0;

    /* A colon and we try to split */
    t = strchr(req->hostname, ':');
    if(t)
    {
        *t = 0;
        strncpy(req->servname, t + 1, sizeof(req->servname));
    }

    if(servname && !req->servname[0])
        strncpy(req->servname, servname, sizeof(req->servname));
    req->servname[sizeof(req->servname) - 1] = 0;

    /* Append the result to requests */
    pthread_mutex_lock(&res_mutex);

        if(!res_requests)
        {
            res_requests = req;
        }
        else
        {
            r = res_requests;
            while(r->next)
                r = r->next;
            r->next = req;
        }

    pthread_mutex_unlock(&res_mutex);

    tsignal_wake(res_request_signal);
}

void
async_resolver_uninit()
{
    resolve_request* req;

    /* No more responses from this point on */
    if(tsignal_get_fd(res_done_signal) != -1)
        server_unwatch(tsignal_get_fd(res_done_signal));

    pthread_mutex_lock(&res_mutex);

        while(res_requests)
        {
            req = res_requests->next;
            if(res_requests->ai)
                freeaddrinfo(res_requests->ai);
            free(res_requests);
            res_requests = req;
        }

        while(res_done)
        {
            req = res_done->next;
            if(res_done->ai)
                freeaddrinfo(res_done->ai);
            free(res_done);
            res_done = req;
        }

    pthread_mutex_unlock(&res_mutex);

    /* Wake up the resolver thread */
    res_quit = 1;
    tsignal_uninit(res_request_signal);

    /* Wait for it to finish */
    if(res_thread)
    {
        pthread_join(res_thread, NULL);
        res_thread = 0;
    }

    /* And close up the signals in the other direction */
    tsignal_uninit(res_done_signal);
}