diff --git a/llarp/ev_win32.hpp b/llarp/ev_win32.hpp index 2a6b068f1..f986a9b36 100644 --- a/llarp/ev_win32.hpp +++ b/llarp/ev_win32.hpp @@ -57,98 +57,13 @@ struct win32_tun_io device* tunif; byte_t readbuf[EV_READ_BUF_SZ] = {0}; - struct WriteBuffer - { - llarp_time_t timestamp = 0; - size_t bufsz; - byte_t buf[EV_WRITE_BUF_SZ]; - - WriteBuffer() = default; - - WriteBuffer(const byte_t* ptr, size_t sz) - { - if(sz <= sizeof(buf)) - { - bufsz = sz; - memcpy(buf, ptr, bufsz); - } - else - bufsz = 0; - } - - struct GetTime - { - llarp_time_t - operator()(const WriteBuffer& buf) const - { - return buf.timestamp; - } - }; - - struct GetNow - { - void* loop; - GetNow(void* l) : loop(l) - { - } - - llarp_time_t - operator()() const - { - return llarp::time_now_ms(); - } - }; - - struct PutTime - { - void* loop; - PutTime(void* l) : loop(l) - { - } - void - operator()(WriteBuffer& buf) - { - buf.timestamp = llarp::time_now_ms(); - } - }; - - struct Compare - { - bool - operator()(const WriteBuffer& left, const WriteBuffer& right) const - { - return left.timestamp < right.timestamp; - } - }; - }; - - using LossyWriteQueue_t = - llarp::util::CoDelQueue< WriteBuffer, WriteBuffer::GetTime, - WriteBuffer::PutTime, WriteBuffer::Compare, - WriteBuffer::GetNow, llarp::util::NullMutex, - llarp::util::NullLock, 5, 100, 128 >; - - std::unique_ptr< LossyWriteQueue_t > m_LossyWriteQueue; - - win32_tun_io(llarp_tun_io* tio) : t(tio), tunif(tuntap_init()) - { - // This is not your normal everyday event loop, this is _advanced_ event - // handling :> - m_LossyWriteQueue = std::make_unique< LossyWriteQueue_t >("win32_tun_queue", - nullptr, nullptr); - }; + win32_tun_io(llarp_tun_io* tio) : t(tio), tunif(tuntap_init()){}; bool queue_write(const byte_t* buf, size_t sz) { - if(m_LossyWriteQueue) - { - m_LossyWriteQueue->Emplace(buf, sz); - flush_write(); - return true; - } - else - return false; + do_write((void*)buf, sz); + return true; } bool @@ -210,7 +125,6 @@ struct win32_tun_io void do_write(void* data, size_t sz) { - llarp::LogInfo("writing some data"); asio_evt_pkt* pkt = new asio_evt_pkt; pkt->buf = data; pkt->sz = sz; @@ -226,11 +140,6 @@ struct win32_tun_io { if(t->before_write) t->before_write(t); - m_LossyWriteQueue->Process([&](WriteBuffer& buffer) { - do_write(buffer.buf, buffer.bufsz); - // we are NEVER going to block - // because Windows NT implements true async io - }); } void @@ -279,17 +188,21 @@ tun_ev_loop(void* unused) if(!pkt->write) { // llarp::LogInfo("read tun ", size, " bytes, pass to handler"); + // skip if our buffer remains empty + if(*(byte_t*)pkt->buf == '\0') + continue; if(ev->t->recvpkt) ev->t->recvpkt(ev->t, llarp::InitBuffer(pkt->buf, size)); - ev->flush_write(); ev->read(ev->readbuf, sizeof(ev->readbuf)); } else { - llarp::LogInfo("write ", size, " bytes to tunnel interface"); // ok let's queue another read! ev->read(ev->readbuf, sizeof(ev->readbuf)); } + if(ev->t->tick) + ev->t->tick(ev->t); + ev->flush_write(); delete pkt; // don't leak } llarp::LogInfo("exit TUN event loop thread from system managed thread pool"); diff --git a/llarp/path.cpp b/llarp/path.cpp index 4f1f8a820..bdd9a8897 100644 --- a/llarp/path.cpp +++ b/llarp/path.cpp @@ -586,6 +586,10 @@ namespace llarp { byte_t tmp[MAX_LINK_MSG_SIZE / 2]; auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); + // should help prevent bad paths with uninitialised members + // FIXME: Why would we get uninitialised IMessages? + if(msg->version != LLARP_PROTO_VERSION) + return false; if(!msg->BEncode(&buf)) { llarp::LogError("Bencode failed"); diff --git a/llarp/resource.h b/llarp/resource.h index 7097a903f..d90e55303 100644 --- a/llarp/resource.h +++ b/llarp/resource.h @@ -4,12 +4,12 @@ // // Next default values for new objects -// +// #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 101 -#define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1000 -#define _APS_NEXT_SYMED_VALUE 101 +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1000 +#define _APS_NEXT_SYMED_VALUE 101 #endif #endif