From 436025e0ca481aac1b953c935a48d3cdf5f395c1 Mon Sep 17 00:00:00 2001 From: despair Date: Tue, 20 Nov 2018 02:18:29 -0600 Subject: [PATCH] fix utp flags leaking into the actual socket transmission clang-format --- llarp/ev.cpp | 12 +++++----- llarp/ev.hpp | 2 +- llarp/handlers/exit.cpp | 4 ++-- llarp/link/utp.cpp | 53 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 60 insertions(+), 11 deletions(-) diff --git a/llarp/ev.cpp b/llarp/ev.cpp index d046a9d60..d80fbe1ba 100644 --- a/llarp/ev.cpp +++ b/llarp/ev.cpp @@ -124,13 +124,13 @@ llarp_ev_udp_sendto(struct llarp_udp_io *udp, const sockaddr *to, errno = 0; } #else - char ebuf[1024]; - int err = WSAGetLastError(); - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL, ebuf, - 1024, nullptr); - llarp::LogWarn("sendto failed: ", buf, ":", err); + char ebuf[1024]; + int err = WSAGetLastError(); + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL, ebuf, + 1024, nullptr); + llarp::LogWarn("sendto failed: ", buf); WSASetLastError(0); - } + } #endif return ret; } diff --git a/llarp/ev.hpp b/llarp/ev.hpp index 98a44140b..a612841fe 100644 --- a/llarp/ev.hpp +++ b/llarp/ev.hpp @@ -644,7 +644,7 @@ namespace llarp struct llarp_ev_loop { byte_t readbuf[EV_READ_BUF_SZ] = {0}; - llarp_time_t _now = 0; + llarp_time_t _now = 0; virtual bool init() = 0; virtual int diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index f7bad1fdf..b0c3fa0d7 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -261,7 +261,7 @@ namespace llarp std::string nmask_str = v.substr(1 + pos); std::string host_str = v.substr(0, pos); // string, or just a plain char array? - strncpy(m_Tun.ifaddr, host_str.c_str(), sizeof(m_Tun.ifaddr)-1); + strncpy(m_Tun.ifaddr, host_str.c_str(), sizeof(m_Tun.ifaddr) - 1); m_Tun.netmask = std::atoi(nmask_str.c_str()); llarp::Addr ifaddr(host_str); @@ -273,7 +273,7 @@ namespace llarp } if(k == "ifname") { - strncpy(m_Tun.ifname, v.c_str(), sizeof(m_Tun.ifname)-1); + strncpy(m_Tun.ifname, v.c_str(), sizeof(m_Tun.ifname) - 1); llarp::LogInfo(Name(), " set ifname to ", m_Tun.ifname); } if(k == "exit-whitelist") diff --git a/llarp/link/utp.cpp b/llarp/link/utp.cpp index ff4e4438c..1b938c2ad 100644 --- a/llarp/link/utp.cpp +++ b/llarp/link/utp.cpp @@ -20,6 +20,10 @@ #include #endif +#ifndef IP_DONTFRAGMENT +#define IP_DONTFRAGMENT IP_DONTFRAG +#endif + namespace llarp { namespace utp @@ -330,13 +334,59 @@ namespace llarp static_cast< LinkLayer* >(utp_context_get_userdata(arg->context)); llarp::LogDebug("utp_sendto ", Addr(*arg->address), " ", arg->len, " bytes"); + // For whatever reason, the UTP_UDP_DONTFRAG flag is set + // on the socket itself....which isn't correct and causes + // winsock (at minimum) to reeee + // here, we check its value, then set fragmentation the _right_ + // way. Naturally, Linux has its own special procedure. + // Of course, the flag itself is cleared. -rick #ifndef _WIN32 +#ifndef __linux__ + if(arg->flags == 2) + { + int val = 1; + setsockopt(l->m_udp.fd, IPPROTO_IP, IP_DONTFRAGMENT, &val, + sizeof(val)); + } + else + { + int val = 0; + setsockopt(l->m_udp.fd, IPPROTO_IP, IP_DONTFRAGMENT, &val, + sizeof(val)); + } +#else + if(arg->flags == 2) + { + int val = IP_PMTUDISC_DO; + setsockopt(l->m_udp.fd, IPPROTO_IP, IP_MTU_DISCOVER, &val, + sizeof(val)); + } + else + { + int val = IP_PMTUDISC_DONT; + setsockopt(l->m_udp.fd, IPPROTO_IP, IP_MTU_DISCOVER, &val, + sizeof(val)); + } +#endif + arg->flags = 0; if(::sendto(l->m_udp.fd, (char*)arg->buf, arg->len, arg->flags, arg->address, arg->address_len) == -1 && errno) #else - WSASetLastError(0); + if(arg->flags == 2) + { + char val = 1; + setsockopt(l->m_udp.fd, IPPROTO_IP, IP_DONTFRAGMENT, &val, + sizeof(val)); + } + else + { + char val = 0; + setsockopt(l->m_udp.fd, IPPROTO_IP, IP_DONTFRAGMENT, &val, + sizeof(val)); + } + arg->flags = 0; if(::sendto(l->m_udp.fd, (char*)arg->buf, arg->len, arg->flags, arg->address, arg->address_len) == -1) @@ -347,7 +397,6 @@ namespace llarp FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL, buf, 1024, nullptr); llarp::LogError("sendto failed: ", buf); - llarp::LogInfo("flags: ", arg->flags); } return 0; }