Merge pull request #14 from despair86/master

implement tuntap on BSD
pull/16/head
Jeff 6 years ago committed by GitHub
commit 2ab26e96c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,6 +2,9 @@
#include "mod3.h"
#include "swap.h"
#include "r3.h"
#ifdef _MSC_VER
#include <assert.h>
#endif
/* caller must ensure that x-y does not overflow */
static int smaller_mask(int x,int y)

@ -1,6 +1,9 @@
#include "params.h"
#include "swap.h"
#include "rq.h"
#ifdef _MSC_VER
#include <assert.h>
#endif
/* caller must ensure that x-y does not overflow */
static int smaller_mask(int x,int y)

@ -48,7 +48,7 @@ namespace llarp
void
HandleDataMessage(service::ProtocolMessage* msg);
#ifndef _WIN32
#ifndef _MINGW32_NO_THREADS
/// overrides Endpount
bool
IsolationFailed()
@ -101,7 +101,7 @@ namespace llarp
FlushSend();
private:
#ifndef _WIN32
#ifndef _MINGW32_NO_THREADS
/// handles setup, given value true on success and false on failure to set
/// up interface
std::promise< bool > m_TunSetupResult;

@ -36,8 +36,8 @@ typedef struct ip_hdr
#define iphdr IPV4_HDR
#define saddr ip_srcaddr
#define daddr ip_destaddr
#define ihl ip_header_len
#define check ip_checksum
#define ihl ip_header_len
#endif
#include <memory>
#if !defined(__linux__) && !defined(_WIN32)

@ -2,6 +2,7 @@
#define LLARP_THREADING_HPP
#include <mutex>
#if defined(__MINGW32__) && !defined(_GLIBCXX_HAS_GTHREADS)
#define _MINGW32_NO_THREADS
#include <llarp/win32/threads/mingw.condition_variable.h>
#include <llarp/win32/threads/mingw.mutex.h>
#include <llarp/win32/threads/mingw.thread.h>

@ -32,14 +32,19 @@
#endif
#if !defined Windows /* Unix :) */
#if !defined Linux
#include <netinet/in.h>
#endif
#if defined Linux
#include <linux/if.h>
#else
#include <net/if.h>
#endif
#include <netinet/if_ether.h>
#if defined Linux
#include <netinet/in.h>
#endif
#endif
#include <stdint.h>

@ -65,6 +65,7 @@ namespace llarp
return true;
#else
// writefile
return false;
#endif
});
/// reset errno

@ -73,6 +73,67 @@ namespace llarp
return sent;
}
};
struct tun : public ev_io
{
llarp_tun_io* t;
device* tunif;
tun(llarp_tun_io* tio)
: ev_io(-1)
, t(tio)
, tunif(tuntap_init())
{
};
int
sendto(const sockaddr* to, const void* data, size_t sz)
{
return -1;
}
void
flush_write()
{
if(t->before_write)
{
t->before_write(t);
ev_io::flush_write();
}
}
int
read(void* buf, size_t sz)
{
ssize_t ret = tuntap_read(tunif, buf, sz);
if(ret > 0 && t->recvpkt)
t->recvpkt(t, buf, ret);
return ret;
}
bool
setup()
{
llarp::LogDebug("set up tunif");
if(tuntap_start(tunif, TUNTAP_MODE_TUNNEL, 0) == -1)
return false;
llarp::LogDebug("set ifname to ", t->ifname);
if(tuntap_set_ifname(tunif, t->ifname) == -1)
return false;
if(tuntap_set_ip(tunif, t->ifaddr, t->netmask) == -1)
return false;
if(tuntap_up(tunif) == -1)
return false;
fd = tunif->tun_fd;
return fd != -1;
}
~tun()
{
}
};
}; // namespace llarp
struct llarp_kqueue_loop : public llarp_ev_loop
@ -91,7 +152,10 @@ struct llarp_kqueue_loop : public llarp_ev_loop
llarp::ev_io*
create_tun(llarp_tun_io* tun)
{
// TODO: implement me
llarp::tun* t = new llarp::tun(tun);
if(t->setup())
return t;
delete t;
return nullptr;
}

@ -153,6 +153,8 @@ struct llarp_win32_loop : public llarp_ev_loop
++idx;
} while(::GetQueuedCompletionStatus(iocpfd, &iolen, &ev_id, &qdata, ms));
// tick_listeners inlined since win32 does not
// implement ev_tun
for(auto& l : udp_listeners)
{
if(l->tick)

@ -1,3 +1,5 @@
// harmless on other platforms
#define __USE_MINGW_ANSI_STDIO 1
#include <llarp/handlers/tun.hpp>
#include "router.hpp"
@ -108,7 +110,7 @@ namespace llarp
// do network isolation first
if(!Endpoint::Start())
return false;
#ifdef _WIN32
#ifdef _MINGW32_NO_THREADS
return SetupNetworking();
#else
if(!NetworkIsIsolated())

@ -1,6 +1,7 @@
#include <llarp/endian.h>
#include <algorithm>
#include <llarp/ip.hpp>
#include "buffer.hpp"
#include "llarp/buffer.hpp"
#include "mem.hpp"
namespace llarp

@ -10,9 +10,9 @@
// these need to be in a specific order
#include <assert.h>
#include <iphlpapi.h>
#include <llarp/net.h>
#include <windows.h>
#include <iphlpapi.h>
#include "win32_intrnl.h"
const char *

@ -16,9 +16,9 @@
#endif
// these need to be in a specific order
#include <tdi.h>
#include <windows.h>
#include <winternl.h>
#include <tdi.h>
#include "win32_intrnl.h"
const PWCHAR TcpFileName = L"\\Device\\Tcp";
@ -528,11 +528,14 @@ SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
/* current win10 flights now have a new named-thread API, let's try to use
* that first! */
/* first, dlsym(2) the new call from system library */
hThread = NULL;
_SetThreadDescription = (p_SetThreadDescription)GetProcAddress(
GetModuleHandle("kernel32"), "SetThreadDescription");
if(_SetThreadDescription)
{
/* grab another reference to the thread */
hThread = OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, dwThreadID);
/* windows takes unicode, our input is utf-8 or plain ascii */
MultiByteToWideChar(CP_ACP, 0, szThreadName, -1, thr_name_w, 16);
if(hThread)
_SetThreadDescription(hThread, thr_name_w);
@ -558,5 +561,8 @@ SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
{
}
}
/* clean up */
if(hThread)
CloseHandle(hThread);
}
#endif

@ -33,13 +33,19 @@ tuntap_sys_set_ipv6(struct device *dev, t_tun_in6_addr *s6, uint32_t bits) {
return -1;
}
#ifndef SIOCSIFNAME
int
tuntap_sys_set_ifname(struct device *dev, const char *ifname, size_t len) {
(void)dev;
(void)ifname;
(void)len;
tuntap_log(TUNTAP_LOG_NOTICE,
"Your system does not support tuntap_set_ifname()");
return -1;
tuntap_sys_set_ifname(struct device *dev, const char *ifname, size_t len)
{
(void)dev;
(void)ifname;
(void)len;
tuntap_log(TUNTAP_LOG_NOTICE,
"Your system does not support tuntap_set_ifname()");
/* just leave it as tunX, there doesn't seem to be any
* practical manner of setting this param in NetBSD and its forks :-(
*/
return 0;
}
#endif

@ -283,3 +283,17 @@ tuntap_sys_set_descr(struct device *dev, const char *descr, size_t len) {
#endif
}
int
tuntap_sys_set_ifname(struct device *dev, const char *ifname, size_t len) {
struct ifreq ifr;
(void)strncpy(ifr.ifr_name, dev->if_name, IF_NAMESIZE);
(void)strncpy(ifr.ifr_newname, ifname, len);
if (ioctl(dev->ctrl_sock, SIOCSIFNAME, &ifr) == -1) {
perror(NULL);
tuntap_log(TUNTAP_LOG_ERR, "Can't set interface name");
return -1;
}
return 0;
}

@ -73,12 +73,18 @@ tuntap_sys_set_ipv6(struct device *dev, t_tun_in6_addr *s6, uint32_t imask)
}
int
tuntap_sys_set_ifname(struct device *dev, const char *ifname, size_t len)
{
(void)dev;
(void)ifname;
(void)len;
return -1;
tuntap_sys_set_ifname(struct device *dev, const char *ifname, size_t len) {
struct ifreq ifr;
(void)strncpy(ifr.ifr_name, dev->if_name, IF_NAMESIZE);
(void)strncpy(ifr.ifr_newname, ifname, len);
if (ioctl(dev->ctrl_sock, SIOCSIFNAME, &ifr) == -1) {
perror(NULL);
tuntap_log(TUNTAP_LOG_ERR, "Can't set interface name");
return -1;
}
return 0;
}
int

Loading…
Cancel
Save