forked from buytenh/dvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iv_getaddrinfo.c
162 lines (130 loc) · 3.69 KB
/
iv_getaddrinfo.c
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
/*
* ivykis, an event handling library
* Copyright (C) 2011 Lennert Buytenhek
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <iv.h>
#include <iv_tls.h>
#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "iv_getaddrinfo.h"
struct iv_getaddrinfo_thr_info {
int num_requests;
struct iv_work_pool pool;
};
static void iv_getaddrinfo_tls_init_thread(void *_tinfo)
{
struct iv_getaddrinfo_thr_info *tinfo = _tinfo;
tinfo->num_requests = 0;
IV_WORK_POOL_INIT(&tinfo->pool);
tinfo->pool.max_threads = 100;
tinfo->pool.cookie = NULL;
tinfo->pool.thread_start = NULL;
tinfo->pool.thread_stop = NULL;
}
static struct iv_tls_user iv_getaddrinfo_tls_user = {
.sizeof_state = sizeof(struct iv_getaddrinfo_thr_info),
.init_thread = iv_getaddrinfo_tls_init_thread,
};
static void iv_getaddrinfo_tls_init(void) __attribute__((constructor));
static void iv_getaddrinfo_tls_init(void)
{
iv_tls_user_register(&iv_getaddrinfo_tls_user);
}
struct iv_getaddrinfo_task {
struct iv_getaddrinfo *ig;
struct iv_work_item work;
char *node;
char *service;
int have_hints;
struct addrinfo hints;
int ret;
struct addrinfo *res;
};
static void iv_getaddrinfo_task_work(void *_igt)
{
struct iv_getaddrinfo_task *igt = _igt;
igt->ret = getaddrinfo(igt->node, igt->service,
igt->have_hints ? &igt->hints : NULL, &igt->res);
}
static void iv_getaddrinfo_task_complete(void *_igt)
{
struct iv_getaddrinfo_task *igt = _igt;
struct iv_getaddrinfo *ig;
struct iv_getaddrinfo_thr_info *tinfo;
ig = igt->ig;
if (ig != NULL)
ig->handler(ig->cookie, igt->ret, igt->res);
else
freeaddrinfo(igt->res);
free(igt->node);
free(igt->service);
free(igt);
tinfo = iv_tls_user_ptr(&iv_getaddrinfo_tls_user);
if (!--tinfo->num_requests)
iv_work_pool_put(&tinfo->pool);
}
int iv_getaddrinfo_submit(struct iv_getaddrinfo *ig)
{
struct iv_getaddrinfo_task *igt;
struct iv_getaddrinfo_thr_info *tinfo;
igt = calloc(1, sizeof(*igt));
if (igt == NULL)
return -1;
igt->ig = ig;
IV_WORK_ITEM_INIT(&igt->work);
igt->work.cookie = igt;
igt->work.work = iv_getaddrinfo_task_work;
igt->work.completion = iv_getaddrinfo_task_complete;
if (ig->node != NULL) {
igt->node = strdup(ig->node);
if (igt->node == NULL) {
free(igt);
return -1;
}
}
if (ig->service != NULL) {
igt->service = strdup(ig->service);
if (igt->service == NULL) {
free(igt->node);
free(igt);
return -1;
}
}
if (ig->hints != NULL) {
igt->hints.ai_family = ig->hints->ai_family;
igt->hints.ai_socktype = ig->hints->ai_socktype;
igt->hints.ai_protocol = ig->hints->ai_protocol;
igt->hints.ai_flags = ig->hints->ai_flags;
igt->have_hints = 1;
}
tinfo = iv_tls_user_ptr(&iv_getaddrinfo_tls_user);
if (!tinfo->num_requests++)
iv_work_pool_create(&tinfo->pool);
iv_work_pool_submit_work(&tinfo->pool, &igt->work);
ig->task = igt;
return 0;
}
void iv_getaddrinfo_cancel(struct iv_getaddrinfo *ig)
{
struct iv_getaddrinfo_task *t = ig->task;
ig->task = NULL;
t->ig = NULL;
}