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"
|
|
|
|
|
2018-11-02 20:43:53 +00:00
|
|
|
// TODO: convert all socket errno calls to WSAGetLastError(3),
|
|
|
|
// don't think winsock sets regular errno to this day
|
2018-07-30 04:38:14 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2018-10-29 23:56:05 +00:00
|
|
|
int
|
|
|
|
tcp_conn::read(void* buf, size_t sz)
|
|
|
|
{
|
2018-11-09 12:56:04 +00:00
|
|
|
WSABUF r_buf = {(u_long)sz, (char*)buf};
|
2018-10-30 12:30:21 +00:00
|
|
|
DWORD amount = 0;
|
|
|
|
|
|
|
|
WSARecv(std::get< SOCKET >(fd), &r_buf, 1, nullptr, 0, &portfd[0], nullptr);
|
|
|
|
GetOverlappedResult((HANDLE)std::get< SOCKET >(fd), &portfd[0], &amount,
|
|
|
|
TRUE);
|
2018-10-29 23:56:05 +00:00
|
|
|
if(amount > 0)
|
|
|
|
{
|
2018-11-02 20:43:53 +00:00
|
|
|
if(tcp.read)
|
|
|
|
tcp.read(&tcp, buf, amount);
|
2018-10-29 23:56:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// error
|
|
|
|
_shouldClose = true;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
tcp_conn::do_write(void* buf, size_t sz)
|
|
|
|
{
|
2018-11-09 12:56:04 +00:00
|
|
|
WSABUF s_buf = {(u_long)sz, (char*)buf};
|
2018-10-30 12:30:21 +00:00
|
|
|
DWORD sent = 0;
|
|
|
|
|
2018-10-29 23:56:05 +00:00
|
|
|
if(_shouldClose)
|
|
|
|
return -1;
|
2018-10-30 12:30:21 +00:00
|
|
|
|
|
|
|
WSASend(std::get< SOCKET >(fd), &s_buf, 1, nullptr, 0, &portfd[1], nullptr);
|
|
|
|
GetOverlappedResult((HANDLE)std::get< SOCKET >(fd), &portfd[1], &sent,
|
|
|
|
TRUE);
|
|
|
|
return sent;
|
2018-10-29 23:56:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 20:43:53 +00:00
|
|
|
void
|
|
|
|
tcp_conn::flush_write()
|
|
|
|
{
|
|
|
|
connected();
|
|
|
|
ev_io::flush_write();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tcp_conn::connect()
|
|
|
|
{
|
|
|
|
socklen_t slen = sizeof(sockaddr_in);
|
|
|
|
if(_addr.ss_family == AF_UNIX)
|
|
|
|
slen = sizeof(sockaddr_un);
|
|
|
|
else if(_addr.ss_family == AF_INET6)
|
|
|
|
slen = sizeof(sockaddr_in6);
|
|
|
|
int result =
|
|
|
|
::connect(std::get< SOCKET >(fd), (const sockaddr*)&_addr, slen);
|
|
|
|
if(result == 0)
|
|
|
|
{
|
|
|
|
llarp::LogDebug("connected immedidately");
|
|
|
|
connected();
|
|
|
|
}
|
|
|
|
else if(errno == EINPROGRESS)
|
|
|
|
{
|
|
|
|
// in progress
|
|
|
|
llarp::LogDebug("connect in progress");
|
|
|
|
errno = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(_conn->error)
|
|
|
|
{
|
|
|
|
// wtf?
|
|
|
|
llarp::LogError("error connecting ", strerror(errno));
|
|
|
|
_conn->error(_conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 23:56:05 +00:00
|
|
|
int
|
|
|
|
tcp_serv::read(void*, size_t)
|
|
|
|
{
|
|
|
|
SOCKET new_fd = ::accept(std::get< SOCKET >(fd), nullptr, nullptr);
|
|
|
|
if(new_fd == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to accept on ", std::get< SOCKET >(fd), ":",
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// build handler
|
2018-11-02 20:43:53 +00:00
|
|
|
llarp::tcp_conn* connimpl = new tcp_conn(loop, new_fd);
|
2018-10-29 23:56:05 +00:00
|
|
|
if(loop->add_ev(connimpl, true))
|
|
|
|
{
|
|
|
|
// call callback
|
|
|
|
if(tcp->accepted)
|
2018-11-02 20:43:53 +00:00
|
|
|
tcp->accepted(tcp, &connimpl->tcp);
|
2018-10-29 23:56:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// cleanup error
|
|
|
|
delete connimpl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
struct udp_listener : public ev_io
|
|
|
|
{
|
|
|
|
llarp_udp_io* udp;
|
|
|
|
|
2018-10-25 22:16:03 +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-10-25 22:16:03 +00:00
|
|
|
bool
|
2018-10-23 00:56:53 +00:00
|
|
|
tick()
|
|
|
|
{
|
|
|
|
if(udp->tick)
|
|
|
|
udp->tick(udp);
|
2018-10-25 22:16:03 +00:00
|
|
|
return true;
|
2018-10-23 00:56:53 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2018-11-09 12:56:04 +00:00
|
|
|
WSABUF wbuf = {(u_long)sz, static_cast< char* >(buf)};
|
2018-07-30 04:38:14 +00:00
|
|
|
// WSARecvFrom
|
2018-10-05 05:06:54 +00:00
|
|
|
llarp::LogDebug("read ", sz, " bytes into socket");
|
2018-10-05 07:57:48 +00:00
|
|
|
int ret = ::WSARecvFrom(std::get< SOCKET >(fd), &wbuf, 1, nullptr, &flags,
|
2018-10-15 22:18:45 +00:00
|
|
|
addr, &slen, &portfd[0], nullptr);
|
2018-08-03 06:03:34 +00:00
|
|
|
// 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-10-15 18:06:07 +00:00
|
|
|
udp->recvfrom(udp, addr, buf, sz);
|
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;
|
2018-11-09 12:56:04 +00:00
|
|
|
WSABUF wbuf = {(u_long)sz, (char*)data};
|
2018-07-30 04:38:14 +00:00
|
|
|
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
|
2018-10-05 05:06:54 +00:00
|
|
|
llarp::LogDebug("write ", sz, " bytes into socket");
|
2018-10-04 11:20:08 +00:00
|
|
|
ssize_t sent = ::WSASendTo(std::get< SOCKET >(fd), &wbuf, 1, nullptr, 0,
|
2018-10-15 22:18:45 +00:00
|
|
|
to, slen, &portfd[1], nullptr);
|
2018-10-04 11:20:08 +00:00
|
|
|
int s_errno = ::WSAGetLastError();
|
2018-08-03 06:03:34 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|
2018-09-29 08:16:54 +00:00
|
|
|
|
2018-10-01 02:08:03 +00:00
|
|
|
struct tun : public ev_io
|
2018-09-29 08:16:54 +00:00
|
|
|
{
|
|
|
|
llarp_tun_io* t;
|
|
|
|
device* tunif;
|
2018-10-15 22:18:45 +00:00
|
|
|
OVERLAPPED* tun_async[2];
|
2018-10-29 23:56:05 +00:00
|
|
|
tun(llarp_tun_io* tio, llarp_ev_loop* l)
|
|
|
|
: ev_io(INVALID_HANDLE_VALUE,
|
|
|
|
new LossyWriteQueue_t("win32_tun_write", l))
|
2018-09-29 08:16:54 +00:00
|
|
|
, t(tio)
|
2018-10-29 23:56:05 +00:00
|
|
|
, tunif(tuntap_init()){};
|
2018-09-29 08:16:54 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
sendto(const sockaddr* to, const void* data, size_t sz)
|
|
|
|
{
|
2018-11-08 00:09:49 +00:00
|
|
|
(void)(to);
|
|
|
|
(void)(data);
|
|
|
|
(void)(sz);
|
2018-09-29 08:16:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
flush_write()
|
|
|
|
{
|
|
|
|
if(t->before_write)
|
|
|
|
{
|
|
|
|
t->before_write(t);
|
|
|
|
}
|
|
|
|
ev_io::flush_write();
|
|
|
|
}
|
|
|
|
|
2018-10-25 22:16:03 +00:00
|
|
|
bool
|
2018-10-23 00:56:53 +00:00
|
|
|
tick()
|
|
|
|
{
|
|
|
|
if(t->tick)
|
|
|
|
t->tick(t);
|
2018-10-25 22:16:03 +00:00
|
|
|
flush_write();
|
|
|
|
return true;
|
2018-10-23 00:56:53 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 23:28:01 +00:00
|
|
|
ssize_t
|
2018-10-04 13:32:33 +00:00
|
|
|
do_write(void* data, size_t sz)
|
|
|
|
{
|
2018-10-15 22:18:45 +00:00
|
|
|
return WriteFile(std::get< HANDLE >(fd), data, sz, nullptr, tun_async[1]);
|
2018-10-04 13:32:33 +00:00
|
|
|
}
|
|
|
|
|
2018-09-29 08:16:54 +00:00
|
|
|
int
|
|
|
|
read(void* buf, size_t sz)
|
|
|
|
{
|
|
|
|
ssize_t ret = tuntap_read(tunif, buf, sz);
|
2018-10-25 22:16:03 +00:00
|
|
|
if(ret > 0 && t->recvpkt)
|
2018-10-01 20:14:36 +00:00
|
|
|
// should have pktinfo
|
2018-10-25 22:16:03 +00:00
|
|
|
// I have no idea...
|
|
|
|
t->recvpkt(t, (byte_t*)buf, ret);
|
2018-09-29 08:16:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
setup()
|
|
|
|
{
|
|
|
|
llarp::LogDebug("set ifname to ", t->ifname);
|
2018-11-09 12:56:04 +00:00
|
|
|
strncpy(tunif->if_name, t->ifname, IFNAMSIZ);
|
2018-09-29 08:16:54 +00:00
|
|
|
|
|
|
|
if(tuntap_start(tunif, TUNTAP_MODE_TUNNEL, 0) == -1)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to start interface");
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-03 00:47:53 +00:00
|
|
|
if(tuntap_set_ip(tunif, t->ifaddr, t->ifaddr, t->netmask) == -1)
|
2018-09-29 08:16:54 +00:00
|
|
|
{
|
2018-10-03 00:47:53 +00:00
|
|
|
llarp::LogWarn("failed to set ip");
|
2018-09-29 08:16:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-03 00:47:53 +00:00
|
|
|
if(tuntap_up(tunif) == -1)
|
2018-09-29 08:16:54 +00:00
|
|
|
{
|
2018-10-03 00:47:53 +00:00
|
|
|
llarp::LogWarn("failed to put interface up: ", strerror(errno));
|
2018-09-29 08:16:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-03 00:47:53 +00:00
|
|
|
|
2018-10-23 00:56:53 +00:00
|
|
|
fd = tunif->tun_fd;
|
2018-10-15 22:18:45 +00:00
|
|
|
tun_async[0] = &tunif->ovl[0];
|
|
|
|
tun_async[1] = &tunif->ovl[1];
|
2018-10-04 11:20:08 +00:00
|
|
|
if(std::get< HANDLE >(fd) == INVALID_HANDLE_VALUE)
|
2018-09-29 08:16:54 +00:00
|
|
|
return false;
|
|
|
|
|
2018-10-03 00:47:53 +00:00
|
|
|
// we're already non-blocking
|
2018-10-04 11:20:08 +00:00
|
|
|
return true;
|
2018-09-29 08:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~tun()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2018-10-29 23:56:05 +00:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-02 20:43:53 +00:00
|
|
|
bool
|
|
|
|
tcp_connect(struct llarp_tcp_connecter* tcp, const sockaddr* remoteaddr)
|
|
|
|
{
|
|
|
|
// create socket
|
|
|
|
DWORD on = 1;
|
|
|
|
SOCKET fd = ::socket(remoteaddr->sa_family, SOCK_STREAM, 0);
|
|
|
|
if(fd == INVALID_SOCKET)
|
|
|
|
return false;
|
|
|
|
// set non blocking
|
|
|
|
if(ioctlsocket(fd, FIONBIO, &on) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
::closesocket(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llarp::tcp_conn* conn = new llarp::tcp_conn(this, fd, remoteaddr, tcp);
|
|
|
|
add_ev(conn, true);
|
|
|
|
conn->connect();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
~llarp_win32_loop()
|
|
|
|
{
|
|
|
|
if(iocpfd != INVALID_HANDLE_VALUE)
|
|
|
|
::CloseHandle(iocpfd);
|
2018-10-25 22:16:03 +00:00
|
|
|
iocpfd = INVALID_HANDLE_VALUE;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 23:56:05 +00:00
|
|
|
llarp::ev_io*
|
|
|
|
bind_tcp(llarp_tcp_acceptor* tcp, const sockaddr* bindaddr)
|
|
|
|
{
|
|
|
|
DWORD on = 1;
|
|
|
|
SOCKET fd = ::socket(bindaddr->sa_family, SOCK_STREAM, 0);
|
|
|
|
if(fd == INVALID_SOCKET)
|
|
|
|
return nullptr;
|
|
|
|
socklen_t sz = sizeof(sockaddr_in);
|
|
|
|
if(bindaddr->sa_family == AF_INET6)
|
|
|
|
{
|
|
|
|
sz = sizeof(sockaddr_in6);
|
|
|
|
}
|
|
|
|
// keep. inexplicably, windows now has unix domain sockets
|
|
|
|
// for now, use the ID numbers directly until this comes out of
|
|
|
|
// beta
|
|
|
|
else if(bindaddr->sa_family == AF_UNIX)
|
|
|
|
{
|
|
|
|
sz = 110; // current size in 10.0.17763, verify each time the beta PSDK
|
|
|
|
// is updated
|
|
|
|
}
|
|
|
|
if(::bind(fd, bindaddr, sz) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
::closesocket(fd);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if(::listen(fd, 5) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
::closesocket(fd);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
llarp::ev_io* serv = new llarp::tcp_serv(this, fd, tcp);
|
|
|
|
tcp->impl = serv;
|
2018-10-30 12:30:21 +00:00
|
|
|
|
2018-10-29 23:56:05 +00:00
|
|
|
ioctlsocket(fd, FIONBIO, &on);
|
|
|
|
return serv;
|
|
|
|
}
|
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
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-11-09 14:48:43 +00:00
|
|
|
OVERLAPPED_ENTRY events[1024];
|
|
|
|
ULONG numEvents = 0;
|
|
|
|
if(::GetQueuedCompletionStatusEx(iocpfd, events, 1024, &numEvents, ms,
|
|
|
|
false))
|
2018-08-03 06:03:34 +00:00
|
|
|
{
|
2018-11-09 14:48:43 +00:00
|
|
|
for(ULONG idx = 0; idx < numEvents; ++idx)
|
|
|
|
{
|
|
|
|
llarp::ev_io* ev =
|
|
|
|
reinterpret_cast< llarp::ev_io* >(events[idx].lpCompletionKey);
|
|
|
|
if(ev)
|
|
|
|
{
|
|
|
|
if(ev->write)
|
|
|
|
ev->flush_write();
|
|
|
|
auto amount =
|
|
|
|
std::min(EV_READ_BUF_SZ, events[idx].dwNumberOfBytesTransferred);
|
|
|
|
memcpy(readbuf, events[idx].lpOverlapped->Pointer, amount);
|
|
|
|
ev->read(readbuf, amount);
|
|
|
|
}
|
|
|
|
}
|
2018-10-04 11:20:08 +00:00
|
|
|
}
|
2018-11-09 14:48:43 +00:00
|
|
|
tick_listeners();
|
|
|
|
return 0;
|
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 idx = 0;
|
2018-10-23 00:56:53 +00:00
|
|
|
BOOL result =
|
2018-10-05 07:57:48 +00:00
|
|
|
::GetQueuedCompletionStatus(iocpfd, &iolen, &ev_id, &qdata, 10);
|
2018-08-03 06:03:34 +00:00
|
|
|
|
2018-10-23 00:56:53 +00:00
|
|
|
if(result && qdata)
|
2018-10-05 07:57:48 +00:00
|
|
|
{
|
|
|
|
llarp::udp_listener* ev = reinterpret_cast< llarp::udp_listener* >(ev_id);
|
2018-10-23 00:56:53 +00:00
|
|
|
if(ev)
|
2018-08-03 06:03:34 +00:00
|
|
|
{
|
2018-10-23 00:56:53 +00:00
|
|
|
llarp::LogDebug("size: ", iolen, "\tev_id: ", ev_id,
|
|
|
|
"\tqdata: ", qdata);
|
2018-10-16 15:27:31 +00:00
|
|
|
if(iolen <= sizeof(readbuf))
|
|
|
|
ev->read(readbuf, iolen);
|
2018-08-03 06:03:34 +00:00
|
|
|
}
|
2018-10-05 07:57:48 +00:00
|
|
|
++idx;
|
|
|
|
}
|
2018-08-03 06:03:34 +00:00
|
|
|
|
|
|
|
if(!idx)
|
|
|
|
return -1;
|
|
|
|
else
|
2018-10-04 11:20:08 +00:00
|
|
|
{
|
2018-08-03 06:03:34 +00:00
|
|
|
result = idx;
|
2018-10-23 00:56:53 +00:00
|
|
|
tick_listeners();
|
2018-10-04 11:20:08 +00:00
|
|
|
}
|
2018-08-03 06:03:34 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-10-05 07:57:48 +00:00
|
|
|
DWORD on = 1;
|
2018-10-05 05:06:54 +00:00
|
|
|
SOCKET fd = ::socket(addr->sa_family, SOCK_DGRAM, 0);
|
2018-07-30 04:38:14 +00:00
|
|
|
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);
|
2018-10-05 05:06:54 +00:00
|
|
|
llarp::LogDebug("bind to ", a);
|
2018-07-30 04:38:14 +00:00
|
|
|
if(bind(fd, addr, slen) == -1)
|
|
|
|
{
|
|
|
|
perror("bind()");
|
|
|
|
closesocket(fd);
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
2018-10-05 05:06:54 +00:00
|
|
|
llarp::LogDebug("socket fd is ", fd);
|
|
|
|
ioctlsocket(fd, FIONBIO, &on);
|
2018-07-30 04:38:14 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
close_ev(llarp::ev_io* ev)
|
|
|
|
{
|
2018-10-25 22:16:03 +00:00
|
|
|
// On Windows, just close the descriptor to decrease the iocp refcount
|
2018-08-03 06:03:34 +00:00
|
|
|
// and stop any pending I/O
|
2018-10-25 22:16:03 +00:00
|
|
|
BOOL stopped;
|
|
|
|
int close_fd;
|
2018-10-29 23:56:05 +00:00
|
|
|
|
|
|
|
if(std::holds_alternative< SOCKET >(ev->fd))
|
2018-10-25 22:16:03 +00:00
|
|
|
{
|
2018-10-29 23:56:05 +00:00
|
|
|
stopped =
|
|
|
|
::CancelIo(reinterpret_cast< HANDLE >(std::get< SOCKET >(ev->fd)));
|
|
|
|
close_fd = closesocket(std::get< SOCKET >(ev->fd));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stopped = ::CancelIo(std::get< HANDLE >(ev->fd));
|
|
|
|
close_fd = CloseHandle(std::get< HANDLE >(ev->fd));
|
|
|
|
if(close_fd)
|
|
|
|
close_fd = 0; // must be zero
|
|
|
|
else
|
|
|
|
close_fd = 1;
|
2018-10-25 22:16:03 +00:00
|
|
|
}
|
|
|
|
return close_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;
|
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp::ev_io*
|
|
|
|
create_tun(llarp_tun_io* tun)
|
|
|
|
{
|
2018-10-29 23:56:05 +00:00
|
|
|
llarp::tun* t = new llarp::tun(tun, this);
|
2018-09-29 08:16:54 +00:00
|
|
|
if(t->setup())
|
|
|
|
return t;
|
|
|
|
delete t;
|
2018-08-15 16:19:22 +00:00
|
|
|
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
|
|
|
{
|
2018-10-25 22:16:03 +00:00
|
|
|
ev->listener_id = reinterpret_cast< ULONG_PTR >(ev);
|
2018-10-05 05:06:54 +00:00
|
|
|
|
2018-10-29 23:56:05 +00:00
|
|
|
// if the write flag was set earlier,
|
|
|
|
// clear it on demand
|
|
|
|
if(ev->write && !write)
|
|
|
|
ev->write = false;
|
|
|
|
|
2018-10-30 10:19:34 +00:00
|
|
|
if(write)
|
|
|
|
ev->write = true;
|
|
|
|
|
|
|
|
// now write a blank packet containing nothing but the address of
|
|
|
|
// the event listener
|
2018-10-25 22:16:03 +00:00
|
|
|
if(ev->isTCP)
|
|
|
|
{
|
|
|
|
if(!::CreateIoCompletionPort((HANDLE)std::get< SOCKET >(ev->fd), iocpfd,
|
|
|
|
ev->listener_id, 0))
|
|
|
|
{
|
|
|
|
delete ev;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
2018-10-30 10:19:34 +00:00
|
|
|
goto start_loop;
|
2018-10-25 22:16:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 23:56:05 +00:00
|
|
|
if(std::holds_alternative< SOCKET >(ev->fd))
|
|
|
|
{
|
2018-10-30 10:19:34 +00:00
|
|
|
if(!::CreateIoCompletionPort((HANDLE)std::get< SOCKET >(ev->fd), iocpfd,
|
2018-10-29 23:56:05 +00:00
|
|
|
ev->listener_id, 0))
|
|
|
|
{
|
|
|
|
delete ev;
|
2018-10-04 11:20:08 +00:00
|
|
|
return false;
|
2018-10-29 23:56:05 +00:00
|
|
|
}
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
2018-10-29 23:56:05 +00:00
|
|
|
else
|
|
|
|
{
|
2018-10-30 10:19:34 +00:00
|
|
|
if(!::CreateIoCompletionPort(std::get< HANDLE >(ev->fd), iocpfd,
|
2018-10-29 23:56:05 +00:00
|
|
|
ev->listener_id, 0))
|
|
|
|
{
|
|
|
|
delete ev;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 10:19:34 +00:00
|
|
|
start_loop:
|
|
|
|
PostQueuedCompletionStatus(iocpfd, 0, ev->listener_id, nullptr);
|
2018-10-23 00:56:53 +00:00
|
|
|
handlers.emplace_back(ev);
|
2018-07-30 04:38:14 +00:00
|
|
|
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-10-25 22:16:03 +00:00
|
|
|
close_ev(listener);
|
|
|
|
// remove handler
|
|
|
|
auto itr = handlers.begin();
|
|
|
|
while(itr != handlers.end())
|
|
|
|
{
|
|
|
|
if(itr->get() == listener)
|
|
|
|
itr = handlers.erase(itr);
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
2018-07-30 04:38:14 +00:00
|
|
|
l->impl = nullptr;
|
2018-10-25 22:16:03 +00:00
|
|
|
ret = true;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
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-10-29 23:56:05 +00:00
|
|
|
bool
|
|
|
|
udp_listen(llarp_udp_io* l, const sockaddr* src)
|
|
|
|
{
|
|
|
|
auto ev = create_udp(l, src);
|
|
|
|
if(ev)
|
|
|
|
l->fd = std::get< SOCKET >(ev->fd);
|
|
|
|
return ev && add_ev(ev, false);
|
|
|
|
}
|
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
void
|
|
|
|
stop()
|
|
|
|
{
|
2018-10-25 22:16:03 +00:00
|
|
|
// Are we leaking any file descriptors?
|
|
|
|
// This was part of the reason I had this
|
|
|
|
// in the destructor.
|
2018-10-26 03:56:48 +00:00
|
|
|
/*if(iocpfd != INVALID_HANDLE_VALUE)
|
2018-10-25 22:16:03 +00:00
|
|
|
::CloseHandle(iocpfd);
|
2018-10-26 03:56:48 +00:00
|
|
|
iocpfd = INVALID_HANDLE_VALUE;*/
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|