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-09 15:46:20 +00:00
|
|
|
#ifdef sizeof
|
|
|
|
#undef sizeof
|
|
|
|
#endif
|
2018-11-19 08:36:39 +00:00
|
|
|
|
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-19 07:55:19 +00:00
|
|
|
if(_shouldClose)
|
|
|
|
return -1;
|
2018-10-30 12:30:21 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
ssize_t amount = uread(fd.socket, (char*)buf, sz);
|
2018-11-16 05:58:50 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
if(amount > 0)
|
2018-10-29 23:56:05 +00:00
|
|
|
{
|
2018-11-02 20:43:53 +00:00
|
|
|
if(tcp.read)
|
2018-11-19 07:55:19 +00:00
|
|
|
tcp.read(&tcp, buf, amount);
|
2018-10-29 23:56:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// error
|
|
|
|
_shouldClose = true;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-02 20:43:53 +00:00
|
|
|
void
|
|
|
|
tcp_conn::flush_write()
|
|
|
|
{
|
|
|
|
connected();
|
|
|
|
ev_io::flush_write();
|
|
|
|
}
|
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
ssize_t
|
|
|
|
tcp_conn::do_write(void* buf, size_t sz)
|
|
|
|
{
|
|
|
|
if(_shouldClose)
|
|
|
|
return -1;
|
|
|
|
return uwrite(fd.socket, (char*)buf, sz);
|
|
|
|
}
|
|
|
|
|
2018-11-02 20:43:53 +00:00
|
|
|
void
|
|
|
|
tcp_conn::connect()
|
|
|
|
{
|
|
|
|
socklen_t slen = sizeof(sockaddr_in);
|
|
|
|
if(_addr.ss_family == AF_UNIX)
|
2018-11-09 15:46:20 +00:00
|
|
|
slen = 115;
|
2018-11-02 20:43:53 +00:00
|
|
|
else if(_addr.ss_family == AF_INET6)
|
|
|
|
slen = sizeof(sockaddr_in6);
|
2018-11-19 07:55:19 +00:00
|
|
|
int result = ::connect(fd.socket, (const sockaddr*)&_addr, slen);
|
2018-11-02 20:43:53 +00:00
|
|
|
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)
|
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
int new_fd = ::accept(fd.socket, nullptr, nullptr);
|
|
|
|
if(new_fd == -1)
|
2018-10-29 23:56:05 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
llarp::LogError("failed to accept on ", fd.socket, ":", strerror(errno));
|
2018-10-29 23:56:05 +00:00
|
|
|
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-11-19 07:55:19 +00:00
|
|
|
udp_listener(int 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-11-19 07:55:19 +00:00
|
|
|
int
|
2018-07-30 04:38:14 +00:00
|
|
|
read(void* buf, size_t sz)
|
|
|
|
{
|
|
|
|
sockaddr_in6 src;
|
2018-11-19 07:55:19 +00:00
|
|
|
socklen_t slen = sizeof(sockaddr_in6);
|
|
|
|
sockaddr* addr = (sockaddr*)&src;
|
|
|
|
ssize_t ret = ::recvfrom(fd.socket, (char*)buf, sz, 0, addr, &slen);
|
|
|
|
if(ret < 0)
|
2018-07-30 04:38:14 +00:00
|
|
|
return -1;
|
2018-11-19 07:55:19 +00:00
|
|
|
if(static_cast< size_t >(ret) > sz)
|
|
|
|
return -1;
|
|
|
|
udp->recvfrom(udp, addr, buf, ret);
|
2018-07-30 04:38:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
int
|
2018-07-30 04:38:14 +00:00
|
|
|
sendto(const sockaddr* to, const void* data, size_t sz)
|
|
|
|
{
|
|
|
|
socklen_t slen;
|
|
|
|
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;
|
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
ssize_t sent = ::sendto(fd.socket, (char*)data, sz, 0, to, slen);
|
|
|
|
if(sent == -1)
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
llarp::LogWarn(strerror(errno));
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
return sent;
|
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-29 23:56:05 +00:00
|
|
|
tun(llarp_tun_io* tio, llarp_ev_loop* l)
|
|
|
|
: ev_io(INVALID_HANDLE_VALUE,
|
2018-11-16 05:58:50 +00:00
|
|
|
new LossyWriteQueue_t("win32_tun_write_queue", l, l))
|
2018-09-29 08:16:54 +00:00
|
|
|
, t(tio)
|
2018-11-19 07:55:19 +00:00
|
|
|
, tunif(tuntap_init())
|
|
|
|
{
|
|
|
|
this->is_tun = true;
|
|
|
|
};
|
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-11-19 08:36:39 +00:00
|
|
|
return tuntap_write(tunif, data, sz);
|
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()
|
|
|
|
{
|
|
|
|
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-11-19 07:55:19 +00:00
|
|
|
fd.tun = tunif->tun_fd;
|
|
|
|
if(fd.tun == 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
|
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
upoll_t* upollfd;
|
|
|
|
llarp_win32_loop() : upollfd(nullptr)
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-02 20:43:53 +00:00
|
|
|
bool
|
|
|
|
tcp_connect(struct llarp_tcp_connecter* tcp, const sockaddr* remoteaddr)
|
|
|
|
{
|
|
|
|
// create socket
|
2018-11-19 07:55:19 +00:00
|
|
|
int fd = usocket(remoteaddr->sa_family, SOCK_STREAM, 0);
|
|
|
|
if(fd == -1)
|
2018-11-02 20:43:53 +00:00
|
|
|
return false;
|
|
|
|
llarp::tcp_conn* conn = new llarp::tcp_conn(this, fd, remoteaddr, tcp);
|
|
|
|
add_ev(conn, true);
|
|
|
|
conn->connect();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-29 23:56:05 +00:00
|
|
|
llarp::ev_io*
|
|
|
|
bind_tcp(llarp_tcp_acceptor* tcp, const sockaddr* bindaddr)
|
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
int fd = usocket(bindaddr->sa_family, SOCK_STREAM, 0);
|
|
|
|
if(fd == -1)
|
2018-10-29 23:56:05 +00:00
|
|
|
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
|
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
if(::bind(fd, bindaddr, sz) == -1)
|
2018-10-29 23:56:05 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
uclose(fd);
|
2018-10-29 23:56:05 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
if(ulisten(fd, 5) == -1)
|
2018-10-29 23:56:05 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
uclose(fd);
|
2018-10-29 23:56:05 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
return new llarp::tcp_serv(this, fd, tcp);
|
|
|
|
}
|
2018-10-30 12:30:21 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
virtual bool
|
|
|
|
udp_listen(llarp_udp_io* l, const sockaddr* src)
|
|
|
|
{
|
|
|
|
auto ev = create_udp(l, src);
|
|
|
|
if(ev)
|
|
|
|
l->fd = ev->fd.socket;
|
|
|
|
return ev && add_ev(ev, false);
|
2018-10-29 23:56:05 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
~llarp_win32_loop()
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
if(upollfd)
|
|
|
|
upoll_destroy(upollfd);
|
|
|
|
}
|
2018-07-30 04:38:14 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
bool
|
|
|
|
running() const
|
|
|
|
{
|
|
|
|
return upollfd != nullptr;
|
|
|
|
}
|
2018-08-11 09:48:24 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
bool
|
|
|
|
init()
|
|
|
|
{
|
|
|
|
if(!upollfd)
|
|
|
|
upollfd = upoll_create(1);
|
|
|
|
return false;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tick(int ms)
|
|
|
|
{
|
2018-11-19 08:36:39 +00:00
|
|
|
upoll_event_t events[1024];
|
2018-11-19 07:55:19 +00:00
|
|
|
int result;
|
|
|
|
result = upoll_wait(upollfd, events, 1024, ms);
|
|
|
|
if(result > 0)
|
2018-11-16 05:58:50 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
int idx = 0;
|
|
|
|
while(idx < result)
|
2018-11-09 14:48:43 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
llarp::ev_io* ev = static_cast< llarp::ev_io* >(events[idx].data.ptr);
|
|
|
|
if(ev)
|
2018-11-09 14:48:43 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
if(events[idx].events & UPOLLERR)
|
|
|
|
{
|
|
|
|
ev->error();
|
|
|
|
}
|
|
|
|
else
|
2018-11-18 02:22:52 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
if(events[idx].events & UPOLLIN)
|
|
|
|
{
|
|
|
|
ev->read(readbuf, sizeof(readbuf));
|
|
|
|
}
|
|
|
|
if(events[idx].events & UPOLLOUT)
|
|
|
|
{
|
|
|
|
ev->flush_write();
|
|
|
|
}
|
2018-11-18 02:22:52 +00:00
|
|
|
}
|
2018-11-09 14:48:43 +00:00
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
++idx;
|
2018-11-09 14:48:43 +00:00
|
|
|
}
|
2018-10-04 11:20:08 +00:00
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
if(result != -1)
|
|
|
|
tick_listeners();
|
|
|
|
return result;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
run()
|
|
|
|
{
|
2018-11-19 08:36:39 +00:00
|
|
|
upoll_event_t events[1024];
|
2018-11-19 07:55:19 +00:00
|
|
|
int result;
|
|
|
|
do
|
2018-10-05 07:57:48 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
result = upoll_wait(upollfd, events, 1024, EV_TICK_INTERVAL);
|
|
|
|
if(result > 0)
|
2018-08-03 06:03:34 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
int idx = 0;
|
|
|
|
while(idx < result)
|
|
|
|
{
|
|
|
|
llarp::ev_io* ev = static_cast< llarp::ev_io* >(events[idx].data.ptr);
|
|
|
|
if(ev)
|
|
|
|
{
|
|
|
|
if(events[idx].events & UPOLLERR)
|
|
|
|
{
|
|
|
|
ev->error();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(events[idx].events & UPOLLIN)
|
|
|
|
{
|
|
|
|
ev->read(readbuf, sizeof(readbuf));
|
|
|
|
}
|
|
|
|
if(events[idx].events & UPOLLOUT)
|
|
|
|
{
|
|
|
|
ev->flush_write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
}
|
2018-08-03 06:03:34 +00:00
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
if(result != -1)
|
|
|
|
tick_listeners();
|
|
|
|
} while(upollfd);
|
2018-08-03 06:03:34 +00:00
|
|
|
return result;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
int
|
2018-07-30 04:38:14 +00:00
|
|
|
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:
|
2018-11-19 07:55:19 +00:00
|
|
|
return -1;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
int fd = usocket(addr->sa_family, SOCK_DGRAM, 0);
|
|
|
|
if(fd == -1)
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
perror("usocket()");
|
|
|
|
return -1;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(addr->sa_family == AF_INET6)
|
|
|
|
{
|
|
|
|
// enable dual stack explicitly
|
|
|
|
int dual = 1;
|
2018-11-19 07:55:19 +00:00
|
|
|
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&dual, sizeof(dual))
|
2018-08-03 06:03:34 +00:00
|
|
|
== -1)
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
|
|
|
// failed
|
|
|
|
perror("setsockopt()");
|
2018-11-19 07:55:19 +00:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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()");
|
2018-11-19 07:55:19 +00:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
close_ev(llarp::ev_io* ev)
|
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
return upoll_ctl(upollfd, UPOLL_CTL_DEL, ev->fd.socket, nullptr) != -1;
|
2018-08-15 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2018-11-19 07:55:19 +00:00
|
|
|
{
|
2018-09-29 08:16:54 +00:00
|
|
|
return t;
|
2018-11-19 07:55:19 +00:00
|
|
|
}
|
2018-09-29 08:16:54 +00:00
|
|
|
delete t;
|
2018-08-15 16:19:22 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
llarp::ev_io*
|
|
|
|
create_udp(llarp_udp_io* l, const sockaddr* src)
|
2018-08-15 16:19:22 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
int fd = udp_bind(src);
|
|
|
|
if(fd == -1)
|
|
|
|
return nullptr;
|
|
|
|
llarp::ev_io* listener = new llarp::udp_listener(fd, l);
|
|
|
|
l->impl = listener;
|
|
|
|
return listener;
|
|
|
|
}
|
2018-10-29 23:56:05 +00:00
|
|
|
|
2018-11-19 07:55:19 +00:00
|
|
|
bool
|
|
|
|
add_ev(llarp::ev_io* e, bool write)
|
|
|
|
{
|
2018-11-19 08:36:39 +00:00
|
|
|
upoll_event_t ev;
|
2018-11-19 07:55:19 +00:00
|
|
|
ev.data.ptr = e;
|
|
|
|
ev.events = UPOLLIN | UPOLLERR;
|
2018-10-30 10:19:34 +00:00
|
|
|
if(write)
|
2018-11-19 07:55:19 +00:00
|
|
|
ev.events |= UPOLLOUT;
|
|
|
|
if(upoll_ctl(upollfd, UPOLL_CTL_ADD, e->fd.socket, &ev) == -1)
|
2018-10-29 23:56:05 +00:00
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
delete e;
|
|
|
|
return false;
|
2018-10-29 23:56:05 +00:00
|
|
|
}
|
2018-11-19 07:55:19 +00:00
|
|
|
handlers.emplace_back(e);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
stop()
|
|
|
|
{
|
2018-11-19 07:55:19 +00:00
|
|
|
if(upollfd)
|
|
|
|
upoll_destroy(upollfd);
|
|
|
|
upollfd = nullptr;
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-09 15:46:20 +00:00
|
|
|
#endif
|