Finally put together some skeletal TUN code for NT

up next: debugging the windows client code
stretch goal: prototype hosting a full masternode on Windows Server (still _highly_ experimental when it _does_ appear)
pull/20/head
despair86 6 years ago
parent 3b9ce8d41d
commit ca0d09142a

@ -91,7 +91,7 @@ namespace llarp
llarp_tun_io* t;
device* tunif;
tun(llarp_tun_io* tio)
: ev_io(-1)
: ev_io(INVALID_SOCKET)
, t(tio)
, tunif(tuntap_init())
@ -136,23 +136,23 @@ namespace llarp
llarp::LogWarn("failed to start interface");
return false;
}
if(tuntap_up(tunif) == -1)
if(tuntap_set_ip(tunif, t->ifaddr, t->ifaddr, t->netmask) == -1)
{
llarp::LogWarn("failed to put interface up: ", strerror(errno));
llarp::LogWarn("failed to set ip");
return false;
}
if(tuntap_set_ip(tunif, t->ifaddr, t->ifaddr, t->netmask) == -1)
if(tuntap_up(tunif) == -1)
{
llarp::LogWarn("failed to set ip");
llarp::LogWarn("failed to put interface up: ", strerror(errno));
return false;
}
fd = (SOCKET)tunif->tun_fd;
if(fd == -1)
return false;
// set non blocking
int on = 1;
return ioctlsocket(fd, FIONBIO, (u_long*)&on) != -1;
// we're already non-blocking
return true;
}
~tun()

@ -254,25 +254,7 @@ namespace llarp
{
llarp::LogInfo("Set Up networking for ", Name());
bool result = SetupTun();
#ifndef _WIN32
m_TunSetupResult.set_value(result);
#endif
if(!NetworkIsIsolated())
{
// need to check to see if we have more than one hidden service
// well we'll only use the primary
// FIXME: detect number of hidden services
llarp::LogWarn(
"Only utilizing first hidden service for .loki look ups");
// because we can't find to the tun interface because we don't want it
// accessible on lokinet we can only bind one to loopback, and we can't
// really utilize anything other than port 53 we can't bind to our
// public interface, don't want it exploitable maybe we could detect if
// you have a private interface
}
llarp::Addr dnsd_sockaddr(127, 0, 0, 1, DNS_PORT);
llarp::Addr dnsc_sockaddr(8, 8, 8, 8, 53);
llarp::LogInfo("TunDNS set up ", dnsd_sockaddr, " to ", dnsc_sockaddr);
m_TunSetupResult.set_value(result); // now that NT has tun, we don't need the CPP guard
if(!llarp_dnsd_init(&this->dnsd, EndpointLogic(), EndpointNetLoop(),
dnsd_sockaddr, dnsc_sockaddr))
{

@ -63,7 +63,7 @@ formated_error(LPWSTR pMessage, DWORD m, ...)
LPWSTR pBuffer = NULL;
va_list args = NULL;
va_start(args, pMessage);
va_start(args, m);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
pMessage, m, 0, (LPSTR)&pBuffer, 0, &args);
@ -189,8 +189,11 @@ tuntap_start(struct device *dev, int mode, int tun)
if(mode == TUNTAP_MODE_TUNNEL)
{
tuntap_log(TUNTAP_LOG_NOTICE, "Layer 3 tunneling is not implemented");
return -1;
deviceid = reg_query(NETWORK_ADAPTERS);
snprintf(buf, sizeof buf, "\\\\.\\Global\\%s.tap", deviceid);
tun_fd = CreateFile(buf, GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
}
else if(mode != TUNTAP_MODE_ETHERNET)
{
@ -198,14 +201,9 @@ tuntap_start(struct device *dev, int mode, int tun)
return -1;
}
deviceid = reg_query(NETWORK_ADAPTERS);
snprintf(buf, sizeof buf, "\\\\.\\Global\\%s.tap", deviceid);
tun_fd = CreateFile(buf, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
if(tun_fd == TUNFD_INVALID_VALUE)
{
int errcode = GetLastError();
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
@ -241,8 +239,8 @@ tuntap_get_hwaddr(struct device *dev)
char buf[128];
(void)_snprintf(buf, sizeof buf,
"MAC address: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x", hwaddr[0],
hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
"MAC address: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x", hwaddr[0],
hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
tuntap_log(TUNTAP_LOG_DEBUG, buf);
}
return (char *)hwaddr;
@ -274,8 +272,7 @@ tuntap_sys_set_updown(struct device *dev, ULONG flag)
{
char buf[32];
(void)_snprintf(buf, sizeof buf, "Status: %s",
flag ? "Up" : "Down");
(void)_snprintf(buf, sizeof buf, "Status: %s", flag ? "Up" : "Down");
tuntap_log(TUNTAP_LOG_DEBUG, buf);
return 0;
}
@ -330,26 +327,31 @@ tuntap_set_mtu(struct device *dev, int mtu)
int
tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s, uint32_t mask)
{
IPADDR psock[4];
DWORD len;
/* Address + Netmask */
psock[0] = s->S_un.S_addr;
psock[1] = mask;
/* DHCP server address (We don't want it) */
psock[2] = 0;
/* DHCP lease time */
psock[3] = 0;
if(DeviceIoControl(dev->tun_fd, TAP_IOCTL_CONFIG_DHCP_MASQ, &psock,
sizeof(psock), &psock, sizeof(psock), &len, NULL)
== 0)
IPADDR sock[3];
DWORD len, ret;
IPADDR ep[4];
sock[0] = s->S_un.S_addr;
sock[2] = mask;
sock[1] = sock[0] & sock[2];
ret = DeviceIoControl(dev->tun_fd, TAP_IOCTL_CONFIG_TUN, &sock, sizeof(sock),
&sock, sizeof(sock), &len, NULL);
ep[0] = s->S_un.S_addr;
ep[1] = mask;
ep[2] = (s->S_un.S_addr | ~mask) - (mask+1); /* For the 10.x.y.y subnet (in a class C config), _should_ be 10.x.255.254 i think */
ep[3] = 8400; /* one day */
ret = DeviceIoControl(dev->tun_fd, TAP_IOCTL_CONFIG_DHCP_MASQ, ep, sizeof(ep),
ep, sizeof(ep), &len, NULL);
if(!ret)
{
int errcode = GetLastError();
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
return 0;
}

Loading…
Cancel
Save