2018-07-30 04:38:14 +00:00
|
|
|
#ifndef EV_WIN32_HPP
|
|
|
|
#define EV_WIN32_HPP
|
|
|
|
#include <llarp/buffer.h>
|
|
|
|
#include <llarp/net.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <cstdio>
|
2018-08-03 06:03:34 +00:00
|
|
|
#include <llarp/net.hpp>
|
2018-07-30 04:38:14 +00:00
|
|
|
#include "ev.hpp"
|
|
|
|
#include "logger.hpp"
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
struct udp_listener : public ev_io
|
|
|
|
{
|
|
|
|
llarp_udp_io* udp;
|
|
|
|
|
2018-08-03 06:03:34 +00:00
|
|
|
// we receive queued data in the OVERLAPPED data field,
|
|
|
|
// much like the pipefds in the UNIX kqueue and loonix
|
|
|
|
// epoll handles
|
|
|
|
// 0 is the read port, 1 is the write port
|
2018-07-30 04:38:14 +00:00
|
|
|
WSAOVERLAPPED portfds[2] = {0};
|
2018-08-03 06:03:34 +00:00
|
|
|
size_t iosz;
|
|
|
|
|
2018-08-20 10:52:47 +00:00
|
|
|
udp_listener(SOCKET fd, llarp_udp_io* u) : ev_io(fd), udp(u){};
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
~udp_listener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-03 06:03:34 +00:00
|
|
|
int
|
|
|
|
getData(void* buf, size_t sz, size_t ret)
|
|
|
|
{
|
|
|
|
iosz = ret;
|
|
|
|
return read(buf, sz);
|
|
|
|
}
|
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
virtual int
|
|
|
|
read(void* buf, size_t sz)
|
|
|
|
{
|
|
|
|
sockaddr_in6 src;
|
2018-08-03 06:03:34 +00:00
|
|
|
socklen_t slen = sizeof(src);
|
|
|
|
sockaddr* addr = (sockaddr*)&src;
|
|
|
|
unsigned long flags = 0;
|
|
|
|
WSABUF wbuf = {sz, static_cast< char* >(buf)};
|
2018-07-30 04:38:14 +00:00
|
|
|
// WSARecvFrom
|
2018-08-03 06:03:34 +00:00
|
|
|
int ret = ::WSARecvFrom(fd, &wbuf, 1, nullptr, &flags, addr, &slen,
|
|
|
|
&portfds[0], nullptr);
|
|
|
|
// 997 is the error code for queued ops
|
|
|
|
int s_errno = ::WSAGetLastError();
|
|
|
|
if(ret && s_errno != 997)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("recv socket error ", s_errno);
|
2018-07-30 04:38:14 +00:00
|
|
|
return -1;
|
2018-08-03 06:03:34 +00:00
|
|
|
}
|
2018-08-04 00:05:45 +00:00
|
|
|
// get the _real_ payload size from tick()
|
2018-08-03 06:03:34 +00:00
|
|
|
udp->recvfrom(udp, addr, buf, iosz);
|
2018-07-30 04:38:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int
|
|
|
|
sendto(const sockaddr* to, const void* data, size_t sz)
|
|
|
|
{
|
|
|
|
socklen_t slen;
|
|
|
|
WSABUF wbuf = {sz, (char*)data};
|
|
|
|
switch(to->sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
slen = sizeof(struct sockaddr_in);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
slen = sizeof(struct sockaddr_in6);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// WSASendTo
|
|
|
|
ssize_t sent =
|
2018-08-03 06:03:34 +00:00
|
|
|
::WSASendTo(fd, &wbuf, 1, nullptr, 0, to, slen, &portfds[1], nullptr);
|
|
|
|
int s_errno = ::WSAGetLastError();
|
|
|
|
if(sent && s_errno != 997)
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
2018-08-03 06:03:34 +00:00
|
|
|
llarp::LogWarn("send socket error ", s_errno);
|
|
|
|
return -1;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
2018-08-03 06:03:34 +00:00
|
|
|
return 0;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}; // namespace llarp
|
|
|
|
|
|
|
|
struct llarp_win32_loop : public llarp_ev_loop
|
|
|
|
{
|
|
|
|
HANDLE iocpfd;
|
|
|
|
|
|
|
|
llarp_win32_loop() : iocpfd(INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
WSADATA wsockd;
|
|
|
|
int err;
|
|
|
|
// So, what I was told last time was that we can defer
|
|
|
|
// loading winsock2 up to this point, as we reach this ctor
|
|
|
|
// early on during daemon startup.
|
|
|
|
err = ::WSAStartup(MAKEWORD(2, 2), &wsockd);
|
|
|
|
if(err)
|
|
|
|
perror("Failed to start Windows Sockets");
|
|
|
|
}
|
|
|
|
|
|
|
|
~llarp_win32_loop()
|
|
|
|
{
|
|
|
|
if(iocpfd != INVALID_HANDLE_VALUE)
|
|
|
|
::CloseHandle(iocpfd);
|
|
|
|
|
|
|
|
::WSACleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
init()
|
|
|
|
{
|
|
|
|
if(iocpfd == INVALID_HANDLE_VALUE)
|
|
|
|
iocpfd = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, nullptr, 0, 0);
|
|
|
|
|
2018-08-10 03:51:38 +00:00
|
|
|
if(iocpfd == INVALID_HANDLE_VALUE)
|
|
|
|
return false;
|
2018-08-11 09:48:24 +00:00
|
|
|
|
2018-08-10 03:51:38 +00:00
|
|
|
return true;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 06:03:34 +00:00
|
|
|
// it works! -despair86, 3-Aug-18 @0420
|
2018-07-30 04:38:14 +00:00
|
|
|
int
|
|
|
|
tick(int ms)
|
|
|
|
{
|
2018-08-03 06:03:34 +00:00
|
|
|
// The only field we really care about is
|
|
|
|
// the listener_id, as it contains the address
|
|
|
|
// of the udp_listener instance.
|
|
|
|
DWORD iolen = 0;
|
|
|
|
// ULONG_PTR is guaranteed to be the same size
|
|
|
|
// as an arch-specific pointer value
|
|
|
|
ULONG_PTR ev_id = 0;
|
|
|
|
WSAOVERLAPPED* qdata = nullptr;
|
|
|
|
int result = 0;
|
|
|
|
int idx = 0;
|
2018-08-08 17:43:46 +00:00
|
|
|
|
2018-08-03 06:03:34 +00:00
|
|
|
do
|
|
|
|
{
|
2018-08-04 00:05:45 +00:00
|
|
|
if(ev_id && qdata && iolen)
|
2018-08-03 06:03:34 +00:00
|
|
|
{
|
2018-08-04 00:05:45 +00:00
|
|
|
llarp::udp_listener* ev =
|
|
|
|
reinterpret_cast< llarp::udp_listener* >(ev_id);
|
|
|
|
if(ev && ev->fd)
|
2018-08-03 06:03:34 +00:00
|
|
|
{
|
2018-08-10 03:51:38 +00:00
|
|
|
ev->getData(readbuf, sizeof(readbuf), iolen);
|
2018-08-03 06:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
} while(::GetQueuedCompletionStatus(iocpfd, &iolen, &ev_id, &qdata, ms));
|
|
|
|
|
2018-08-26 12:51:22 +00:00
|
|
|
// tick_listeners inlined since win32 does not
|
|
|
|
// implement ev_tun
|
2018-08-03 06:03:34 +00:00
|
|
|
for(auto& l : udp_listeners)
|
|
|
|
{
|
|
|
|
if(l->tick)
|
|
|
|
l->tick(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!idx)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
result = idx;
|
|
|
|
|
|
|
|
return result;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 06:03:34 +00:00
|
|
|
// ok apparently this isn't being used yet...
|
2018-07-30 04:38:14 +00:00
|
|
|
int
|
|
|
|
run()
|
|
|
|
{
|
2018-08-03 06:03:34 +00:00
|
|
|
// The only field we really care about is
|
|
|
|
// the listener_id, as it contains the address
|
|
|
|
// of the udp_listener instance.
|
|
|
|
DWORD iolen = 0;
|
|
|
|
// ULONG_PTR is guaranteed to be the same size
|
|
|
|
// as an arch-specific pointer value
|
|
|
|
ULONG_PTR ev_id = 0;
|
|
|
|
WSAOVERLAPPED* qdata = nullptr;
|
|
|
|
int result = 0;
|
|
|
|
int idx = 0;
|
|
|
|
|
2018-08-04 00:05:45 +00:00
|
|
|
// unlike epoll and kqueue, we only need to run so long as the
|
|
|
|
// system call returns TRUE
|
2018-08-03 06:03:34 +00:00
|
|
|
do
|
|
|
|
{
|
2018-08-04 00:05:45 +00:00
|
|
|
if(ev_id && qdata && iolen)
|
2018-08-03 06:03:34 +00:00
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
llarp::udp_listener* ev =
|
|
|
|
reinterpret_cast< llarp::udp_listener* >(ev_id);
|
|
|
|
if(ev && ev->fd)
|
|
|
|
{
|
2018-08-10 03:51:38 +00:00
|
|
|
ev->getData(readbuf, sizeof(readbuf), iolen);
|
2018-08-08 17:43:46 +00:00
|
|
|
}
|
2018-08-03 06:03:34 +00:00
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
} while(::GetQueuedCompletionStatus(iocpfd, &iolen, &ev_id, &qdata, 10));
|
|
|
|
|
|
|
|
for(auto& l : udp_listeners)
|
|
|
|
{
|
|
|
|
if(l->tick)
|
|
|
|
l->tick(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!idx)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
result = idx;
|
|
|
|
|
|
|
|
return result;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SOCKET
|
|
|
|
udp_bind(const sockaddr* addr)
|
|
|
|
{
|
|
|
|
socklen_t slen;
|
|
|
|
switch(addr->sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
slen = sizeof(struct sockaddr_in);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
slen = sizeof(struct sockaddr_in6);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
SOCKET fd = ::WSASocket(addr->sa_family, SOCK_DGRAM, 0, nullptr, 0,
|
|
|
|
WSA_FLAG_OVERLAPPED);
|
|
|
|
if(fd == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
perror("WSASocket()");
|
2018-08-03 06:03:34 +00:00
|
|
|
return INVALID_SOCKET;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(addr->sa_family == AF_INET6)
|
|
|
|
{
|
|
|
|
// enable dual stack explicitly
|
|
|
|
int dual = 1;
|
2018-08-03 06:03:34 +00:00
|
|
|
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&dual,
|
|
|
|
sizeof(dual))
|
|
|
|
== -1)
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
|
|
|
// failed
|
|
|
|
perror("setsockopt()");
|
|
|
|
closesocket(fd);
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llarp::Addr a(*addr);
|
|
|
|
llarp::LogDebug("bind to ", a);
|
|
|
|
if(bind(fd, addr, slen) == -1)
|
|
|
|
{
|
|
|
|
perror("bind()");
|
|
|
|
closesocket(fd);
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
2018-08-03 06:03:34 +00:00
|
|
|
llarp::LogInfo("socket fd is ", fd);
|
2018-07-30 04:38:14 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
close_ev(llarp::ev_io* ev)
|
|
|
|
{
|
|
|
|
// On Windows, just close the socket to decrease the iocp refcount
|
2018-08-03 06:03:34 +00:00
|
|
|
// and stop any pending I/O
|
|
|
|
BOOL stopped = ::CancelIo(reinterpret_cast< HANDLE >(ev->fd));
|
|
|
|
return closesocket(ev->fd) == 0 && stopped == TRUE;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 16:19:22 +00:00
|
|
|
llarp::ev_io*
|
|
|
|
create_udp(llarp_udp_io* l, const sockaddr* src)
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
|
|
|
SOCKET fd = udp_bind(src);
|
2018-08-03 06:03:34 +00:00
|
|
|
llarp::LogDebug("new socket fd is ", fd);
|
2018-07-30 04:38:14 +00:00
|
|
|
if(fd == INVALID_SOCKET)
|
2018-08-15 16:21:51 +00:00
|
|
|
return nullptr;
|
2018-07-30 04:38:14 +00:00
|
|
|
llarp::udp_listener* listener = new llarp::udp_listener(fd, l);
|
2018-08-15 16:19:22 +00:00
|
|
|
l->impl = listener;
|
|
|
|
udp_listeners.push_back(l);
|
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp::ev_io*
|
|
|
|
create_tun(llarp_tun_io* tun)
|
|
|
|
{
|
|
|
|
// TODO implement me
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-08-18 03:19:00 +00:00
|
|
|
add_ev(llarp::ev_io* ev, bool write)
|
2018-08-15 16:19:22 +00:00
|
|
|
{
|
|
|
|
ev->listener_id = reinterpret_cast< ULONG_PTR >(ev);
|
|
|
|
if(!::CreateIoCompletionPort(reinterpret_cast< HANDLE >(ev->fd), iocpfd,
|
|
|
|
ev->listener_id, 0))
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
2018-08-15 16:19:22 +00:00
|
|
|
delete ev;
|
2018-07-30 04:38:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
udp_close(llarp_udp_io* l)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
llarp::udp_listener* listener =
|
|
|
|
static_cast< llarp::udp_listener* >(l->impl);
|
|
|
|
if(listener)
|
|
|
|
{
|
2018-08-03 06:03:34 +00:00
|
|
|
ret = close_ev(listener);
|
2018-07-30 04:38:14 +00:00
|
|
|
l->impl = nullptr;
|
|
|
|
delete listener;
|
|
|
|
udp_listeners.remove(l);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-10 03:51:38 +00:00
|
|
|
bool
|
|
|
|
running() const
|
|
|
|
{
|
2018-08-11 09:48:24 +00:00
|
|
|
return iocpfd != INVALID_HANDLE_VALUE;
|
2018-08-10 03:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
void
|
|
|
|
stop()
|
|
|
|
{
|
2018-08-11 09:48:24 +00:00
|
|
|
// still does nothing
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|