eventcore.drivers.posix.dns 10/69(14%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
459
460
479
489
490
500
510
520
538
548
558
568
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1439
1449
1459
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
module eventcore.drivers.posix.dns; @safe: import eventcore.driver; import eventcore.drivers.posix.driver; import eventcore.internal.utils : ChoppedVector, print; import std.socket : Address, AddressFamily, InternetAddress, Internet6Address, UnknownAddress; version (Posix) { import std.socket : UnixAddress; import core.sys.posix.netdb : AI_ADDRCONFIG, AI_V4MAPPED, addrinfo, freeaddrinfo, getaddrinfo; import core.sys.posix.netinet.in_; import core.sys.posix.netinet.tcp; import core.sys.posix.sys.un; import core.stdc.errno : errno, EAGAIN, EINPROGRESS; import core.sys.posix.fcntl; } version (Windows) { import core.sys.windows.windows; import core.sys.windows.winsock2; alias sockaddr_storage = SOCKADDR_STORAGE; alias EAGAIN = WSAEWOULDBLOCK; } /// getaddrinfo+thread based lookup - does not support true cancellation version (Posix) final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriverSignals) : EventDriverDNS { import std.parallelism : task, taskPool; import std.string : toStringz; private { static struct Lookup { DNSLookupCallback callback; addrinfo* result; int retcode; string name; } ChoppedVector!Lookup m_lookups; Events m_events; EventID m_event = EventID.invalid; size_t m_maxHandle; } this(Events events, Signals signals) { m_events = events; setupEvent(); } void dispose() { if (m_event != EventID.invalid) { m_events.cancelWait(m_event, &onDNSSignal); m_events.releaseRef(m_event); m_event = EventID.invalid; } } override DNSLookupID lookupHost(string name, DNSLookupCallback on_lookup_finished) { debug (EventCoreLogDNS) print("lookup %s", name); auto handle = getFreeHandle(); if (handle > m_maxHandle) m_maxHandle = handle; setupEvent(); assert(!m_lookups[handle].result); Lookup* l = () @trusted { return &m_lookups[handle]; } (); l.name = name; l.callback = on_lookup_finished; auto events = () @trusted { return cast(shared)m_events; } (); auto t = task!taskFun(l, AddressFamily.UNSPEC, events, m_event); try t.executeInNewThread();//taskPool.put(t); catch (Exception e) return DNSLookupID.invalid; debug (EventCoreLogDNS) print("lookup handle: %s", handle); m_events.loop.m_waiterCount++; return handle; } /// public static void taskFun(Lookup* lookup, int af, shared(Events) events, EventID event) { debug (EventCoreLogDNS) print("lookup %s start", lookup.name); addrinfo hints; hints.ai_flags = AI_ADDRCONFIG; version (linx) hints.ai_flags |= AI_V4MAPPED; hints.ai_family = af; () @trusted { lookup.retcode = getaddrinfo(lookup.name.toStringz(), null, af == AddressFamily.UNSPEC ? null : &hints, &lookup.result); } (); if (lookup.retcode == -1) version (CRuntime_Glibc) version (linux) __res_init(); events.trigger(event, true); debug (EventCoreLogDNS) print("lookup %s finished", lookup.name); } override void cancelLookup(DNSLookupID handle) { m_lookups[handle].callback = null; m_events.loop.m_waiterCount--; } private void onDNSSignal(EventID event) @trusted nothrow { debug (EventCoreLogDNS) print("DNS event triggered"); m_events.wait(m_event, &onDNSSignal); size_t lastmax; foreach (i, ref l; m_lookups) { if (i > m_maxHandle) break; if (l.callback) { if (l.result || l.retcode) { debug (EventCoreLogDNS) print("found finished lookup %s for %s", i, l.name); auto cb = l.callback; auto ai = l.result; DNSStatus status; switch (l.retcode) { default: status = DNSStatus.error; break; case 0: status = DNSStatus.ok; break; } l.callback = null; l.result = null; l.retcode = 0; if (i == m_maxHandle) m_maxHandle = lastmax; m_events.loop.m_waiterCount--; passToDNSCallback(cast(DNSLookupID)cast(int)i, cb, status, ai); } else lastmax = i; } } debug (EventCoreLogDNS) print("Max active DNS handle: %s", m_maxHandle); } private DNSLookupID getFreeHandle() @safe nothrow { assert(m_lookups.length <= int.max); foreach (i, ref l; m_lookups) if (!l.callback) return cast(DNSLookupID)cast(int)i; return cast(DNSLookupID)cast(int)m_lookups.length; } private void setupEvent() { if (m_event == EventID.invalid) { m_event = m_events.createInternal(); m_events.wait(m_event, &onDNSSignal); } } } /// getaddrinfo_a based asynchronous lookups final class EventDriverDNS_GAIA(Events : EventDriverEvents, Signals : EventDriverSignals) : EventDriverDNS { import core.sys.posix.signal : SIGEV_SIGNAL, SIGRTMIN, sigevent; private { static struct Lookup { gaicb ctx; DNSLookupCallback callback; } ChoppedVector!Lookup m_lookups; Signals m_signals; int m_dnsSignal; SignalListenID m_sighandle; } @safe nothrow: this(Events events, Signals signals) { m_signals = signals; m_dnsSignal = () @trusted { return SIGRTMIN; } (); m_sighandle = signals.listenInternal(m_dnsSignal, &onDNSSignal); } void dispose() { m_signals.releaseRef(m_sighandle); } override DNSLookupID lookupHost(string name, DNSLookupCallback on_lookup_finished) { import std.string : toStringz; auto handle = getFreeHandle(); sigevent evt; evt.sigev_notify = SIGEV_SIGNAL; evt.sigev_signo = m_dnsSignal; gaicb* res = &m_lookups[handle].ctx; res.ar_name = name.toStringz(); auto ret = () @trusted { return getaddrinfo_a(GAI_NOWAIT, &res, 1, &evt); } (); if (ret != 0) { version (CRuntime_Glibc) version (linux) __res_init(); return DNSLookupID.invalid; } m_lookups[handle].callback = on_lookup_finished; m_events.loop.m_waiterCount++; return handle; } override void cancelLookup(DNSLookupID handle) { gai_cancel(&m_lookups[handle].ctx); m_lookups[handle].callback = null; m_events.loop.m_waiterCount--; } private void onDNSSignal(SignalListenID, SignalStatus status, int signal) @safe nothrow { assert(status == SignalStatus.ok); foreach (i, ref l; m_lookups) { if (!l.callback) continue; auto err = gai_error(&l.ctx); if (err == EAI_INPROGRESS) continue; DNSStatus status; switch (err) { default: status = DNSStatus.error; break; case 0: status = DNSStatus.ok; break; } auto cb = l.callback; auto ai = l.ctx.ar_result; l.callback = null; l.ctx.ar_result = null; m_events.loop.m_waiterCount--; passToDNSCallback(cast(DNSLookupID)cast(int)i, cb, status, ai); } } private DNSLookupID getFreeHandle() { foreach (i, ref l; m_lookups) if (!l.callback) return cast(DNSLookupID)cast(int)i; return cast(DNSLookupID)cast(int)m_lookups.length; } } version (linux) extern(C) { import core.sys.posix.signal : sigevent; struct gaicb { const(char)* ar_name; const(char)* ar_service; const(addrinfo)* ar_request; addrinfo* ar_result; } enum GAI_NOWAIT = 1; enum EAI_INPROGRESS = -100; int getaddrinfo_a(int mode, gaicb** list, int nitems, sigevent *sevp); int gai_error(gaicb *req); int gai_cancel(gaicb *req); int __res_init(); } /// ghbn based lookup - does not support cancellation and blocks the thread! final class EventDriverDNS_GHBN(Events : EventDriverEvents, Signals : EventDriverSignals) : EventDriverDNS { import std.parallelism : task, taskPool; import std.string : toStringz; private { static struct Lookup { DNSLookupCallback callback; bool success; int retcode; string name; } size_t m_maxHandle; } this(Events events, Signals signals) { } void dispose() { } override DNSLookupID lookupHost(string name, DNSLookupCallback on_lookup_finished) { import std.string : toStringz; auto handle = DNSLookupID(m_maxHandle++); auto he = () @trusted { return gethostbyname(name.toStringz); } (); if (he is null) { on_lookup_finished(handle, DNSStatus.error, null); return handle; } switch (he.h_addrtype) { default: assert(false, "Invalid address family returned from host lookup."); case AF_INET: { sockaddr_in sa; sa.sin_family = AF_INET; sa.sin_addr = () @trusted { return *cast(in_addr*)he.h_addr_list[0]; } (); scope addr = new RefAddress(() @trusted { return cast(sockaddr*)&sa; } (), sa.sizeof); RefAddress[1] aa; aa[0] = addr; on_lookup_finished(handle, DNSStatus.ok, aa); } break; case AF_INET6: { sockaddr_in6 sa; sa.sin6_family = AF_INET6; sa.sin6_addr = () @trusted { return *cast(in6_addr*)he.h_addr_list[0]; } (); scope addr = new RefAddress(() @trusted { return cast(sockaddr*)&sa; } (), sa.sizeof); RefAddress[1] aa; aa[0] = addr; on_lookup_finished(handle, DNSStatus.ok, aa); } break; } return handle; } override void cancelLookup(DNSLookupID) {} } package struct DNSSlot { alias Handle = DNSLookupID; DNSLookupCallback callback; } private void passToDNSCallback()(DNSLookupID id, scope DNSLookupCallback cb, DNSStatus status, addrinfo* ai_orig) @trusted nothrow { import std.typecons : scoped; try { typeof(scoped!RefAddress())[16] addrs_prealloc = [ scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress(), scoped!RefAddress() ]; //Address[16] addrs; RefAddress[16] addrs; auto ai = ai_orig; size_t addr_count = 0; while (ai !is null && addr_count < addrs.length) { RefAddress ua = addrs_prealloc[addr_count]; // FIXME: avoid heap allocation ua.set(ai.ai_addr, ai.ai_addrlen); addrs[addr_count] = ua; addr_count++; ai = ai.ai_next; } cb(id, status, addrs[0 .. addr_count]); freeaddrinfo(ai_orig); } catch (Exception e) assert(false, e.msg); }