-
Notifications
You must be signed in to change notification settings - Fork 34
/
zn_addrinfo.h
406 lines (344 loc) · 12.5 KB
/
zn_addrinfo.h
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#ifndef zn_addrinfo_h
#define zn_addrinfo_h
#include "znet.h"
ZN_NS_BEGIN
#define ZN_TCP 0x00
#define ZN_ACCEPT 0x01
#define ZN_UDP 0x02
#define ZN_IPV4 0x100
#define ZN_IPV6 0x200
typedef void zn_AddrInfoHandler(void *ud, unsigned err, unsigned count, zn_PeerInfo *infos);
ZN_API const char *zn_aierror (unsigned err);
ZN_API int zn_getaddrinfo (zn_State *S, const char *node, const char *service, int flags,
zn_AddrInfoHandler *h, void *ud);
ZN_API void zn_closeaddrinfo (zn_State *S);
ZN_NS_END
#endif /* zn_addrinfo_h */
#ifndef zn_list_h
#define zn_list_h
#define zn_rawset(l, r) (*(void**)&(l) = (void*)(r))
#define zn_reverse(T, h) do { \
T *ret_ = NULL, *next_; \
while (h) next_ = h->next, h->next = ret_, \
ret_ = h, h = next_; \
(h) = ret_; } while (0)
#define znL_entry(T) T *next; T *prev
#define znL_head(T) struct T##_hlist { znL_entry(T); }
#define znL_init(h) (zn_rawset((h)->prev, h), zn_rawset((h)->next, h))
#define znL_empty(h) ((void*)(h)->prev == (void*)&(h))
#define znL_insert(h,n) (zn_rawset((n)->next, h), \
(n)->prev = (h)->prev, \
(h)->prev->next = (n), \
(h)->prev = (n)) \
#define znL_remove(n) ((n)->prev->next = (n)->next, \
(n)->next->prev = (n)->prev) \
#define znL_apply(T, h, stmt) do { \
T *cur, *next_ = (h)->next; \
while (cur = next_, next_ = next_->next, cur != (T*)(h)) \
{ stmt; } } while (0)
#define znQ_entry(T) T *next
#define znQ_head(T) struct T##_qlist { T *first, **plast; }
#define znQ_init(h) ((h)->first = NULL, (h)->plast = &(h)->first)
#define znQ_first(h) ((h)->first)
#define znQ_empty(h) ((h)->first == NULL)
#define znQ_enqueue(h, n) ((n)->next = NULL, \
*(h)->plast = (n), \
(h)->plast = &(n)->next) \
#define znQ_dequeue(h, pn) do { \
if (((pn) = (h)->first) != NULL) { \
(h)->first = (h)->first->next; \
if ((h)->plast == &(pn)->next) \
(h)->plast = &(h)->first; } } while (0)
#define znQ_apply(T, h, stmt) do { \
T *cur = (h), *next_; \
while (cur) { next_ = cur->next; stmt; cur = next_; } } while (0)
#endif /* zn_list_h */
#if defined(ZN_IMPLEMENTATION) && !defined(zn_implemented)
#define zn_implemented
#include <stdlib.h>
ZN_NS_BEGIN
typedef struct znA_AddrRequest {
znQ_entry(struct znA_AddrRequest);
zn_AddrInfoHandler *h;
void *ud;
zn_State *S;
int flags;
int ret;
unsigned count;
zn_PeerInfo *peers;
char *node;
char *service;
} znA_AddrRequest;
static znA_AddrRequest *znA_current;
static znQ_head(znA_AddrRequest) znA_queue;
static void znA_makepeers(znA_AddrRequest *req, void *info);
static void znA_makehints(znA_AddrRequest *req, void *info);
static znA_AddrRequest *znA_makereq(zn_State *S, const char *node, const char *service, int flags, zn_AddrInfoHandler *h, void *ud) {
size_t nodelen = (node ? strlen(node) : 0);
size_t svrlen = (service ? strlen(service) : 0);
znA_AddrRequest *req = (znA_AddrRequest*)malloc(sizeof(znA_AddrRequest)
+ nodelen + svrlen + 2);
if (req == NULL) return NULL;
memset(req, 0, sizeof(*req));
req->ud = ud;
req->h = h;
req->S = S;
req->flags = flags & 0xFFFF;
req->node = (char*)(req + 1);
req->service = req->node + nodelen + 1;
req->node = node ? strcpy(req->node, node ) : NULL;
req->service = service ? strcpy(req->service, service) : NULL;
zn_retain(S);
return req;
}
static void znA_callback(void *ud, zn_State *S) {
znA_AddrRequest *req = (znA_AddrRequest*)ud;
if (req->count == 0 && req->ret == 0)
req->ret = ZN_ERROR;
if (req->h) req->h(req->ud, req->ret, req->count, req->peers);
free(req->peers);
free(req);
zn_release(S);
}
static void znA_clearreq(zn_State *S) {
znA_AddrRequest *req = znA_queue.first;
znQ_init(&znA_queue);
while (req != NULL) {
znA_AddrRequest *next = req->next;
if (req->S != S && S != NULL)
znQ_enqueue(&znA_queue, req);
else {
req->ret = ZN_ERROR;
zn_post(req->S, znA_callback, req);
}
req = next;
}
}
static znA_AddrRequest *znA_fetchreq(void) {
znA_AddrRequest *req = NULL;
if (znA_current)
zn_post(znA_current->S, znA_callback, znA_current);
znA_current = NULL;
znQ_dequeue(&znA_queue, req);
return znA_current = req;
}
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif /* WIN32_LEAN_AND_MEAN */
#include <Windows.h>
#include <WinSock2.h>
#include <process.h>
#define zn_AddrInfo ADDRINFOW
static LONG znA_initalize = 0;
static CRITICAL_SECTION znA_lock;
static HANDLE znA_event;
static HANDLE znA_thread;
static const char *znA_ntop(int af, const void *src, char *dst, socklen_t size) {
struct sockaddr_storage ss;
unsigned long s = size;
ZeroMemory(&ss, sizeof(ss));
ss.ss_family = af;
switch(af) {
case AF_INET:
((struct sockaddr_in *)&ss)->sin_addr = *(struct in_addr *)src;
break;
case AF_INET6:
((struct sockaddr_in6 *)&ss)->sin6_addr = *(struct in6_addr *)src;
break;
default:
return NULL;
}
return WSAAddressToString(
(struct sockaddr*)&ss, sizeof(ss), NULL, dst, &s) == 0 ? dst : NULL;
}
static WCHAR *znA_getwstring(const char *s) {
WCHAR *out;
int chars = MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0);
if (chars == 0 || (out = (WCHAR*)malloc(chars * sizeof(WCHAR))) == NULL)
return NULL;
MultiByteToWideChar(CP_UTF8, 0, s, -1, out, chars);
return out;
}
static int znA_processreq(znA_AddrRequest *req) {
zn_AddrInfo hints, *info;
WCHAR *node = NULL, *service = NULL;
if (req == NULL) return 0;
znA_makehints(req, &hints);
if (req->node) node = znA_getwstring(req->node);
if (req->service) service = znA_getwstring(req->service);
req->ret = GetAddrInfoW(node, service, &hints, &info);
free(node), free(service);
if (req->ret == 0 && info) {
znA_makepeers(req, info);
FreeAddrInfoW(info);
}
return 1;
}
static unsigned __stdcall znA_worker(void *param) {
(void)param;
for (;;) {
znA_AddrRequest *req;
if (WaitForSingleObject(znA_event, INFINITE) != WAIT_OBJECT_0)
return 1;
do {
EnterCriticalSection(&znA_lock);
req = znA_fetchreq();
LeaveCriticalSection(&znA_lock);
} while (znA_processreq(req));
}
return 0;
}
static int znA_init(zn_State *S, znA_AddrRequest *req) {
znQ_init(&znA_queue);
InitializeCriticalSection(&znA_lock);
znA_event = CreateEvent(NULL, FALSE, FALSE, NULL);
if (znA_event != NULL) {
znA_thread = (HANDLE)_beginthreadex(NULL, 0, znA_worker, NULL, 0, NULL);
if (znA_thread != NULL)
return 1;
}
if (znA_event != NULL) CloseHandle(znA_event);
DeleteCriticalSection(&znA_lock);
zn_release(S);
free(req);
return 0;
}
ZN_API int zn_getaddrinfo(zn_State *S, const char *node, const char *service, int flags, zn_AddrInfoHandler *h, void *ud) {
znA_AddrRequest *req = znA_makereq(S, node, service, flags, h, ud);
LONG ret;
if (req == NULL) return ZN_ERROR;
while ((ret = InterlockedCompareExchange(&znA_initalize, -1, 0)) < 0)
Sleep(1);
if (ret == 0) {
ret = znA_init(S, req);
InterlockedExchange(&znA_initalize, ret);
if (!ret) return ZN_ERROR;
}
EnterCriticalSection(&znA_lock);
znQ_enqueue(&znA_queue, req);
SetEvent(znA_event);
LeaveCriticalSection(&znA_lock);
return ZN_OK;
}
ZN_API void zn_closeaddrinfo(zn_State *S) {
LONG ret;
while ((ret = InterlockedCompareExchange(&znA_initalize, -1, 1)) < 0)
Sleep(1);
if (ret) {
EnterCriticalSection(&znA_lock);
znA_clearreq(S);
LeaveCriticalSection(&znA_lock);
if (S == NULL) {
_endthreadex((uintptr_t)znA_thread);
CloseHandle(znA_event);
CloseHandle(znA_thread);
DeleteCriticalSection(&znA_lock);
}
}
InterlockedExchange(&znA_initalize, 0);
}
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <pthread.h>
#define znA_ntop inet_ntop
#define zn_AddrInfo struct addrinfo
static int znA_initalize = 0;
static pthread_mutex_t znA_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t znA_event = PTHREAD_COND_INITIALIZER;
static pthread_t znA_thread;
static void *znA_worker(void *param) {
(void)param;
for (;;) {
zn_AddrInfo hints, *info;
znA_AddrRequest *req;
pthread_mutex_lock(&znA_lock);
while ((req = znA_fetchreq()) == NULL)
pthread_cond_wait(&znA_event, &znA_lock);
pthread_mutex_unlock(&znA_lock);
znA_makehints(req, &hints);
req->ret = getaddrinfo(req->node, req->service, &hints, &info);
if (req->ret == 0 && info) {
znA_makepeers(req, info);
freeaddrinfo(info);
}
}
return NULL;
}
ZN_API int zn_getaddrinfo(zn_State *S, const char *node, const char *service, int flags, zn_AddrInfoHandler *h, void *ud) {
znA_AddrRequest *req = znA_makereq(S, node, service, flags, h, ud);
if (req == NULL) return ZN_ERROR;
pthread_mutex_lock(&znA_lock);
if (!znA_initalize && pthread_create(&znA_thread, NULL, znA_worker, NULL) == 0) {
znQ_init(&znA_queue);
znA_initalize = 1;
}
znQ_enqueue(&znA_queue, req);
pthread_cond_signal(&znA_event);
pthread_mutex_unlock(&znA_lock);
return ZN_OK;
}
ZN_API void zn_closeaddrinfo(zn_State *S) {
pthread_mutex_lock(&znA_lock);
if (!znA_initalize) return;
znA_clearreq(S);
if (S == NULL) {
pthread_cancel(znA_thread);
pthread_join(znA_thread, NULL);
znA_initalize = 0;
}
pthread_mutex_unlock(&znA_lock);
}
#endif
ZN_API const char *zn_aierror(unsigned err) { return gai_strerror(err); }
static void znA_makepeers(znA_AddrRequest *req, void *info) {
zn_AddrInfo *p;
size_t count = 0;
for (p = (zn_AddrInfo*)info; p != NULL; p = p->ai_next)
++count;
req->peers = (zn_PeerInfo*)malloc(count * sizeof(zn_PeerInfo));
if (req->peers == NULL) return;
for (p = (zn_AddrInfo*)info; p != NULL; p = p->ai_next) {
zn_PeerInfo *peer = &req->peers[req->count];
int family = p->ai_family;
if (family == AF_INET) {
struct sockaddr_in *addr = (struct sockaddr_in*)p->ai_addr;
znA_ntop(family, &addr->sin_addr, peer->addr, ZN_MAX_ADDRLEN);
peer->port = ntohs(addr->sin_port);
++req->count;
}
else if (family == AF_INET6) {
struct sockaddr_in6 *addr = (struct sockaddr_in6*)p->ai_addr;
znA_ntop(family, &addr->sin6_addr, peer->addr, ZN_MAX_ADDRLEN);
peer->port = ntohs(addr->sin6_port);
++req->count;
}
}
}
static void znA_makehints(znA_AddrRequest *req, void *info) {
zn_AddrInfo *hints = (zn_AddrInfo*)info;
memset(hints, 0, sizeof(*hints));
hints->ai_family = req->flags & ZN_IPV6 ? AF_INET6 :
req->flags & ZN_IPV4 ? AF_INET : AF_UNSPEC;
switch (req->flags & 0xFF) {
default:
case ZN_ACCEPT:
hints->ai_flags = AI_PASSIVE;
/* FALLTHROUGH */
case ZN_TCP:
hints->ai_socktype = SOCK_STREAM;
hints->ai_protocol = IPPROTO_TCP;
break;
case ZN_UDP:
hints->ai_socktype = SOCK_DGRAM;
hints->ai_protocol = IPPROTO_UDP;
break;
}
}
ZN_NS_END
#endif /* ZN_IMPLEMENTATION */
/* win32cc: flags+='-s -O3 -mdll -DZN_IMPLEMENTATION -xc'
* win32cc: libs+='-lws2_32' output='zn_addrinfo.dll'
unixcc: flags+='-O3 -shared -fPIC -DZN_IMPLEMENTATION -xc' output='zn_addrinfo.so' */