2018-05-29 12:14:50 +00:00
|
|
|
#ifndef EV_KQUEUE_HPP
|
|
|
|
#define EV_KQUEUE_HPP
|
|
|
|
#include <llarp/buffer.h>
|
|
|
|
#include <llarp/net.h>
|
|
|
|
|
2018-08-03 00:34:47 +00:00
|
|
|
#if __FreeBSD__ || __OpenBSD__ || __NetBSD__ || (__APPLE__ && __MACH__)
|
2018-06-01 22:39:30 +00:00
|
|
|
// kqueue / kevent
|
2018-06-17 15:26:00 +00:00
|
|
|
#include <sys/event.h>
|
|
|
|
#include <sys/time.h>
|
2018-06-01 22:39:30 +00:00
|
|
|
#endif
|
2018-05-29 12:14:50 +00:00
|
|
|
|
2018-06-01 22:39:30 +00:00
|
|
|
// MacOS needs this
|
|
|
|
#ifndef SOCK_NONBLOCK
|
2018-06-17 15:26:00 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#define SOCK_NONBLOCK O_NONBLOCK
|
2018-06-01 22:39:30 +00:00
|
|
|
#endif
|
2018-05-29 12:14:50 +00:00
|
|
|
|
|
|
|
// original upstream
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <cstdio>
|
2018-07-27 04:30:32 +00:00
|
|
|
#include <llarp/net.hpp>
|
2018-05-29 12:14:50 +00:00
|
|
|
#include "ev.hpp"
|
|
|
|
#include "logger.hpp"
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
struct udp_listener : public ev_io
|
|
|
|
{
|
|
|
|
llarp_udp_io* udp;
|
|
|
|
|
|
|
|
udp_listener(int fd, llarp_udp_io* u) : ev_io(fd), udp(u){};
|
|
|
|
|
|
|
|
~udp_listener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int
|
|
|
|
read(void* buf, size_t sz)
|
|
|
|
{
|
|
|
|
sockaddr_in6 src;
|
2018-08-13 08:47:24 +00:00
|
|
|
socklen_t slen = sizeof(sockaddr_in6);
|
2018-05-29 12:14:50 +00:00
|
|
|
sockaddr* addr = (sockaddr*)&src;
|
2018-06-18 22:05:02 +00:00
|
|
|
ssize_t ret = ::recvfrom(fd, buf, sz, 0, addr, &slen);
|
2018-05-29 12:14:50 +00:00
|
|
|
if(ret == -1)
|
|
|
|
return -1;
|
|
|
|
udp->recvfrom(udp, addr, buf, ret);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int
|
|
|
|
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-08-01 03:32:13 +00:00
|
|
|
if(!fd)
|
|
|
|
{
|
|
|
|
printf("kqueue sendto fd empty\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2018-06-04 13:08:35 +00:00
|
|
|
ssize_t sent = ::sendto(fd, data, sz, 0, to, slen);
|
2018-05-29 12:14:50 +00:00
|
|
|
if(sent == -1)
|
2018-06-01 22:39:30 +00:00
|
|
|
perror("kqueue sendto()");
|
2018-05-29 12:14:50 +00:00
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}; // namespace llarp
|
|
|
|
|
|
|
|
struct llarp_kqueue_loop : public llarp_ev_loop
|
|
|
|
{
|
|
|
|
int kqueuefd;
|
2018-06-06 12:46:26 +00:00
|
|
|
struct kevent change; /* event we want to monitor */
|
2018-05-29 12:14:50 +00:00
|
|
|
|
|
|
|
llarp_kqueue_loop() : kqueuefd(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~llarp_kqueue_loop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-15 15:46:39 +00:00
|
|
|
llarp::ev_io*
|
|
|
|
create_tun(llarp_tun_io* tun)
|
|
|
|
{
|
|
|
|
// TODO: implement me
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-29 12:14:50 +00:00
|
|
|
bool
|
|
|
|
init()
|
|
|
|
{
|
2018-06-06 12:46:26 +00:00
|
|
|
if(kqueuefd == -1)
|
|
|
|
{
|
2018-05-29 12:14:50 +00:00
|
|
|
kqueuefd = kqueue();
|
|
|
|
}
|
|
|
|
return kqueuefd != -1;
|
|
|
|
}
|
|
|
|
|
2018-08-10 03:51:38 +00:00
|
|
|
bool
|
|
|
|
running() const
|
|
|
|
{
|
|
|
|
return kqueuefd != -1;
|
|
|
|
}
|
|
|
|
|
2018-06-06 12:46:26 +00:00
|
|
|
int
|
2018-06-06 21:23:57 +00:00
|
|
|
tick(int ms)
|
2018-06-06 12:46:26 +00:00
|
|
|
{
|
|
|
|
struct kevent events[1024];
|
|
|
|
int result;
|
|
|
|
byte_t readbuf[2048];
|
2018-08-03 01:24:53 +00:00
|
|
|
timespec t;
|
|
|
|
t.tv_sec = 0;
|
2018-08-03 05:32:07 +00:00
|
|
|
t.tv_nsec = ms * 1000UL;
|
2018-08-03 01:24:53 +00:00
|
|
|
result = kevent(kqueuefd, nullptr, 0, events, 1024, &t);
|
2018-06-06 12:46:26 +00:00
|
|
|
// result: 0 is a timeout
|
|
|
|
if(result > 0)
|
|
|
|
{
|
|
|
|
int idx = 0;
|
|
|
|
while(idx < result)
|
|
|
|
{
|
|
|
|
llarp::ev_io* ev = static_cast< llarp::ev_io* >(events[idx].udata);
|
2018-08-10 03:51:38 +00:00
|
|
|
ev->read(readbuf, sizeof(readbuf));
|
2018-06-06 12:46:26 +00:00
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 15:36:34 +00:00
|
|
|
tick_listeners();
|
2018-06-06 12:46:26 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-29 12:14:50 +00:00
|
|
|
int
|
|
|
|
run()
|
|
|
|
{
|
2018-08-03 03:56:45 +00:00
|
|
|
timespec t;
|
|
|
|
t.tv_sec = 0;
|
2018-08-10 03:51:38 +00:00
|
|
|
t.tv_nsec = 1000UL * EV_TICK_INTERVAL;
|
2018-05-29 12:14:50 +00:00
|
|
|
struct kevent events[1024];
|
|
|
|
int result;
|
|
|
|
byte_t readbuf[2048];
|
|
|
|
do
|
|
|
|
{
|
2018-08-03 03:56:45 +00:00
|
|
|
result = kevent(kqueuefd, nullptr, 0, events, 1024, &t);
|
2018-05-29 12:14:50 +00:00
|
|
|
// result: 0 is a timeout
|
2018-06-06 12:46:26 +00:00
|
|
|
if(result > 0)
|
2018-05-29 12:14:50 +00:00
|
|
|
{
|
|
|
|
int idx = 0;
|
|
|
|
while(idx < result)
|
|
|
|
{
|
|
|
|
llarp::ev_io* ev = static_cast< llarp::ev_io* >(events[idx].udata);
|
2018-08-01 03:32:13 +00:00
|
|
|
if(ev && ev->fd)
|
2018-05-29 12:14:50 +00:00
|
|
|
{
|
2018-08-01 03:32:13 +00:00
|
|
|
// printf("reading_ev [%x] fd[%d]\n", ev, ev->fd);
|
2018-08-10 03:51:38 +00:00
|
|
|
ev->read(readbuf, sizeof(readbuf));
|
2018-08-01 03:32:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogWarn("kqueue event ", idx, " udata wasnt an ev_io");
|
2018-05-29 12:14:50 +00:00
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 15:36:34 +00:00
|
|
|
tick_listeners();
|
2018-05-29 12:14:50 +00:00
|
|
|
} while(result != -1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
udp_bind(const sockaddr* addr)
|
|
|
|
{
|
|
|
|
socklen_t slen;
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("kqueue bind affam", addr->sa_family);
|
2018-05-29 12:14:50 +00:00
|
|
|
switch(addr->sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
slen = sizeof(struct sockaddr_in);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
slen = sizeof(struct sockaddr_in6);
|
|
|
|
break;
|
|
|
|
#ifdef AF_LINK
|
|
|
|
#endif
|
|
|
|
#ifdef AF_PACKET
|
|
|
|
case AF_PACKET:
|
|
|
|
slen = sizeof(struct sockaddr_ll);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("unsupported address family");
|
2018-05-29 12:14:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int fd = socket(addr->sa_family, SOCK_DGRAM, 0);
|
|
|
|
if(fd == -1)
|
|
|
|
{
|
|
|
|
perror("socket()");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(addr->sa_family == AF_INET6)
|
|
|
|
{
|
|
|
|
// enable dual stack explicitly
|
|
|
|
int dual = 1;
|
|
|
|
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &dual, sizeof(dual)) == -1)
|
|
|
|
{
|
|
|
|
// failed
|
|
|
|
perror("setsockopt()");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llarp::Addr a(*addr);
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("bind to ", a);
|
2018-06-04 13:08:35 +00:00
|
|
|
// FreeBSD handbook said to do this
|
2018-06-17 15:26:00 +00:00
|
|
|
if(addr->sa_family == AF_INET && INADDR_ANY)
|
2018-06-04 13:08:35 +00:00
|
|
|
a._addr4.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
2018-05-29 12:14:50 +00:00
|
|
|
if(bind(fd, addr, slen) == -1)
|
|
|
|
{
|
|
|
|
perror("bind()");
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
close_ev(llarp::ev_io* ev)
|
|
|
|
{
|
2018-07-30 04:38:14 +00:00
|
|
|
EV_SET(&change, ev->fd, EVFILT_READ, EV_DELETE, 0, 0, nullptr);
|
|
|
|
return kevent(kqueuefd, &change, 1, nullptr, 0, nullptr) == -1;
|
2018-05-29 12:14:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 15:45:07 +00:00
|
|
|
llarp::ev_io*
|
|
|
|
create_udp(llarp_udp_io* l, const sockaddr* src)
|
2018-05-29 12:14:50 +00:00
|
|
|
{
|
|
|
|
int fd = udp_bind(src);
|
|
|
|
if(fd == -1)
|
2018-08-15 15:45:07 +00:00
|
|
|
return nullptr;
|
2018-05-29 12:14:50 +00:00
|
|
|
llarp::udp_listener* listener = new llarp::udp_listener(fd, l);
|
2018-08-15 15:45:07 +00:00
|
|
|
udp_listeners.push_back(l);
|
|
|
|
l->impl = listener;
|
|
|
|
return listener;
|
|
|
|
}
|
2018-05-29 12:14:50 +00:00
|
|
|
|
2018-08-15 15:45:07 +00:00
|
|
|
bool
|
2018-08-16 15:31:32 +00:00
|
|
|
add_ev(llarp::ev_io* ev, bool write)
|
2018-08-15 15:45:07 +00:00
|
|
|
{
|
2018-08-16 15:31:32 +00:00
|
|
|
if(write)
|
|
|
|
EV_SET(&change, ev->fd, EVFILT_READ | EVFILT_WRITE, EV_ADD, 0, 0, ev);
|
|
|
|
else
|
|
|
|
EV_SET(&change, ev->fd, EVFILT_READ, EV_ADD, 0, 0, ev);
|
2018-07-30 04:38:14 +00:00
|
|
|
if(kevent(kqueuefd, &change, 1, nullptr, 0, nullptr) == -1)
|
2018-06-06 12:46:26 +00:00
|
|
|
{
|
2018-08-15 15:45:07 +00:00
|
|
|
delete ev;
|
2018-05-29 12:14:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
udp_close(llarp_udp_io* l)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
auto listener = static_cast< llarp::udp_listener* >(l->impl);
|
|
|
|
if(listener)
|
|
|
|
{
|
2018-08-01 03:32:13 +00:00
|
|
|
// printf("Calling close_ev for [%x] fd[%d]\n", listener, listener->fd);
|
|
|
|
ret = close_ev(listener);
|
2018-05-29 12:14:50 +00:00
|
|
|
l->impl = nullptr;
|
2018-08-01 03:32:13 +00:00
|
|
|
delete listener;
|
2018-06-27 13:14:07 +00:00
|
|
|
udp_listeners.remove(l);
|
2018-05-29 12:14:50 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
stop()
|
|
|
|
{
|
|
|
|
if(kqueuefd != -1)
|
|
|
|
::close(kqueuefd);
|
|
|
|
|
|
|
|
kqueuefd = -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|