fixed race condition in event loop....dns seems to work now :DDD

pull/36/head
despair 6 years ago
parent f381201d6b
commit 561c5228d3

@ -271,7 +271,7 @@ namespace abyss
const char* end = strstr(buf, "\r\n");
while(end)
{
string_view line(buf, *end);
string_view line(buf, end);
switch(m_State)
{
case eReadHTTPMethodLine:
@ -399,7 +399,7 @@ namespace abyss
auto itr = m_Conns.begin();
while(itr != m_Conns.end())
{
if((*itr)->ShouldClose(_now)
if((*itr)->ShouldClose(_now))
itr = m_Conns.erase(itr);
else
++itr;

@ -457,7 +457,7 @@ namespace llarp
// is overriden for each platform
struct llarp_ev_loop
{
byte_t readbuf[EV_READ_BUF_SZ];
byte_t readbuf[EV_READ_BUF_SZ] = {0};
llarp_time_t _now = 0;
virtual bool
init() = 0;

@ -15,8 +15,13 @@ namespace llarp
{
if(_shouldClose)
return -1;
// TODO: make async
ssize_t amount = ::recv(std::get< SOCKET >(fd), (char*)buf, sz, 0);
WSABUF r_buf = {sz, buf};
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);
if(amount > 0)
{
if(tcp->read)
@ -34,10 +39,16 @@ namespace llarp
ssize_t
tcp_conn::do_write(void* buf, size_t sz)
{
WSABUF s_buf = {sz, buf};
DWORD sent = 0;
if(_shouldClose)
return -1;
// TODO: make async
return ::send(std::get< SOCKET >(fd), (char*)buf, sz, 0);
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;
}
int
@ -109,7 +120,6 @@ namespace llarp
llarp::LogWarn("recv socket error ", s_errno);
return -1;
}
// get the _real_ payload size from tick()
udp->recvfrom(udp, addr, buf, sz);
return 0;
}
@ -283,8 +293,7 @@ struct llarp_win32_loop : public llarp_ev_loop
}
llarp::ev_io* serv = new llarp::tcp_serv(this, fd, tcp);
tcp->impl = serv;
// We're non-blocking now, but can't really make use of it
// until we cut over to WSA* functions
ioctlsocket(fd, FIONBIO, &on);
return serv;
}
@ -317,18 +326,14 @@ struct llarp_win32_loop : public llarp_ev_loop
BOOL result =
::GetQueuedCompletionStatus(iocpfd, &iolen, &ev_id, &qdata, ms);
if(result && qdata)
llarp::ev_io* ev = reinterpret_cast< llarp::ev_io* >(ev_id);
if(ev)
{
llarp::ev_io* ev = reinterpret_cast< llarp::ev_io* >(ev_id);
if(ev)
{
llarp::LogDebug("size: ", iolen, "\tev_id: ", ev_id,
"\tqdata: ", qdata);
if(ev->write)
ev->flush_write();
else
ev->read(readbuf, iolen);
}
llarp::LogDebug("size: ", iolen, "\tev_id: ", ev_id, "\tqdata: ", qdata);
if(ev->write)
ev->flush_write();
else
ev->read(readbuf, iolen);
++idx;
}

@ -68,7 +68,7 @@ TEST_F(DNSTest, TestDecodeDNSstring)
TEST_F(DNSTest, TestCodeDomain)
{
char buffer[16];
llarp::Zero(&buffer, 16);
llarp::Zero(buffer, 16);
char *write_buffer = buffer;
std::string url = "bob.com";
code_domain(write_buffer, url);

@ -176,7 +176,7 @@ tuntap_start(struct device *dev, int mode, int tun)
char buf[60];
/* put something in there to avoid problems (uninitialised field access) */
tun_fd = TUNFD_INVALID_VALUE;
tun_fd = TUNFD_INVALID_VALUE;
deviceid = NULL;
/* Don't re-initialise a previously started device */
@ -364,7 +364,7 @@ tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s, uint32_t mask)
ep[2] = (s->S_un.S_addr | ~mask)
- (mask + 1); /* For the 10.x.0.y subnet (in a class C config), _should_
be 10.x.0.254 i think */
ep[3] = 3153600; /* one year */
ep[3] = 3153600; /* one year */
ret = DeviceIoControl(dev->tun_fd, TAP_IOCTL_CONFIG_DHCP_MASQ, ep, sizeof(ep),
ep, sizeof(ep), &len, NULL);
@ -375,7 +375,7 @@ tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s, uint32_t mask)
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
/* set DNS address to 127.0.0.1 as lokinet-client runs its own DNS resolver
* inline */
dns.dhcp_opt = 6;
@ -412,42 +412,37 @@ tuntap_sys_set_ipv6(struct device *dev, t_tun_in6_addr *s, uint32_t mask)
int
tuntap_read(struct device *dev, void *buf, size_t size)
{
DWORD len;
if(ReadFile(dev->tun_fd, buf, (DWORD)size, &len, &dev->ovl[0]) == 0)
if(size)
{
ReadFile(dev->tun_fd, buf, (DWORD)size, NULL, &dev->ovl[0]);
int errcode = GetLastError();
if (errcode != 997)
{
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
else
return 0;
if(errcode != 997)
{
tuntap_log(TUNTAP_LOG_ERR,
(const char *)formated_error(L"%1%0", errcode));
return -1;
}
}
return 0;
}
int
tuntap_write(struct device *dev, void *buf, size_t size)
{
DWORD len;
if(WriteFile(dev->tun_fd, buf, (DWORD)size, &len, &dev->ovl[1]) == 0)
if(size)
{
int errcode = GetLastError();
if (errcode != 997)
{
tuntap_log(TUNTAP_LOG_ERR, (const char *)formated_error(L"%1%0", errcode));
return -1;
}
else
return 0;
}
WriteFile(dev->tun_fd, buf, (DWORD)size, NULL, &dev->ovl[1]);
int errcode = GetLastError();
if(errcode != 997)
{
tuntap_log(TUNTAP_LOG_ERR,
(const char *)formated_error(L"%1%0", errcode));
return -1;
}
}
return 0;
}
@ -466,7 +461,8 @@ tuntap_set_nonblocking(struct device *dev, int set)
(void)dev;
(void)set;
tuntap_log(TUNTAP_LOG_NOTICE,
"TUN/TAP devices on Windows are non-blocking by default using either overlapped I/O or IOCPs");
"TUN/TAP devices on Windows are non-blocking by default using "
"either overlapped I/O or IOCPs");
return -1;
}

Loading…
Cancel
Save