diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 940570dda..38b611c22 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -925,7 +925,7 @@ namespace llarp { (void)ip; SendToServiceOrQueue( - service::Address{addr.as_array()}, pkt.ConstBuffer(), service::eProtocolExit); + service::Address{addr.as_array()}, pkt.ConstBuffer(), service::ProtocolType::Exit); } return; } @@ -960,7 +960,7 @@ namespace llarp { ctx->sendTimeout = 5s; } - self->SendToServiceOrQueue(addr, pkt.ConstBuffer(), service::eProtocolExit); + self->SendToServiceOrQueue(addr, pkt.ConstBuffer(), service::ProtocolType::Exit); }, 1s); } @@ -983,7 +983,7 @@ namespace llarp this, service::Address(itr->second.as_array()), std::placeholders::_1, - service::eProtocolExit); + service::ProtocolType::Exit); } else { @@ -1019,8 +1019,8 @@ namespace llarp service::ProtocolType t, uint64_t seqno) { - if (t != service::eProtocolTrafficV4 && t != service::eProtocolTrafficV6 - && t != service::eProtocolExit) + if (t != service::ProtocolType::TrafficV4 && t != service::ProtocolType::TrafficV6 + && t != service::ProtocolType::Exit) return false; AlignedBuffer<32> addr; bool snode = false; @@ -1036,7 +1036,7 @@ namespace llarp { // exit side from exit src = ObtainIPForAddr(addr, snode); - if (t == service::eProtocolExit) + if (t == service::ProtocolType::Exit) { if (pkt.IsV4()) dst = pkt.dst4to6(); @@ -1052,7 +1052,7 @@ namespace llarp dst = m_OurIP; } } - else if (t == service::eProtocolExit) + else if (t == service::ProtocolType::Exit) { // client side exit traffic from exit if (pkt.IsV4()) diff --git a/llarp/net/ip_packet.hpp b/llarp/net/ip_packet.hpp index b013b7f36..6375bbb3a 100644 --- a/llarp/net/ip_packet.hpp +++ b/llarp/net/ip_packet.hpp @@ -226,11 +226,11 @@ namespace llarp ServiceProtocol() const { if (IsV4()) - return service::eProtocolTrafficV4; + return service::ProtocolType::TrafficV4; if (IsV6()) - return service::eProtocolTrafficV6; + return service::ProtocolType::TrafficV6; - return service::eProtocolControl; + return service::ProtocolType::Control; } huint128_t diff --git a/llarp/rpc/endpoint_rpc.cpp b/llarp/rpc/endpoint_rpc.cpp index c546c8f2f..8f59576c6 100644 --- a/llarp/rpc/endpoint_rpc.cpp +++ b/llarp/rpc/endpoint_rpc.cpp @@ -65,7 +65,7 @@ namespace llarp::rpc return; } - if (msg->proto != llarp::service::eProtocolAuth) + if (msg->proto != llarp::service::ProtocolType::Auth) { // not an auth message, reject reply(service::AuthResult{service::AuthResultCode::eAuthRejected, "protocol error"}); diff --git a/llarp/service/endpoint.cpp b/llarp/service/endpoint.cpp index 6d958550e..22ebd496f 100644 --- a/llarp/service/endpoint.cpp +++ b/llarp/service/endpoint.cpp @@ -958,14 +958,14 @@ namespace llarp bool Endpoint::ProcessDataMessage(std::shared_ptr msg) { - if ((msg->proto == eProtocolExit + if ((msg->proto == ProtocolType::Exit && (m_state->m_ExitEnabled || m_ExitMap.ContainsValue(msg->sender.Addr()))) - || msg->proto == eProtocolTrafficV4 || msg->proto == eProtocolTrafficV6) + || msg->proto == ProtocolType::TrafficV4 || msg->proto == ProtocolType::TrafficV6) { m_InboundTrafficQueue.tryPushBack(std::move(msg)); return true; } - if (msg->proto == eProtocolControl) + if (msg->proto == ProtocolType::Control) { // TODO: implement me (?) // right now it's just random noise @@ -1014,7 +1014,7 @@ namespace llarp msg.PutBuffer(reason); f.N.Randomize(); f.C.Zero(); - msg.proto = eProtocolAuth; + msg.proto = ProtocolType::Auth; if (not GetReplyIntroFor(tag, msg.introReply)) { LogError("Failed to send auth reply: no reply intro"); @@ -1252,7 +1252,7 @@ namespace llarp return false; pkt.UpdateIPv4Address(src, dst); /// TODO: V6 - return HandleInboundPacket(tag, pkt.ConstBuffer(), eProtocolTrafficV4, 0); + return HandleInboundPacket(tag, pkt.ConstBuffer(), ProtocolType::TrafficV4, 0); }, Router(), numDesiredPaths, diff --git a/llarp/service/outbound_context.cpp b/llarp/service/outbound_context.cpp index 28370f042..9fddfefc8 100644 --- a/llarp/service/outbound_context.cpp +++ b/llarp/service/outbound_context.cpp @@ -327,7 +327,7 @@ namespace llarp Encrypted<64> tmp; tmp.Randomize(); llarp_buffer_t buf(tmp.data(), tmp.size()); - AsyncEncryptAndSendTo(buf, eProtocolControl); + AsyncEncryptAndSendTo(buf, ProtocolType::Control); } } } @@ -551,7 +551,7 @@ namespace llarp ProtocolMessage msg{}; if (frame.DecryptPayloadInto(sessionKey, msg)) { - if (msg.proto == eProtocolAuth and not msg.payload.empty()) + if (msg.proto == ProtocolType::Auth and not msg.payload.empty()) { result.reason = std::string{ reinterpret_cast(msg.payload.data()), msg.payload.size()}; @@ -574,7 +574,7 @@ namespace llarp authResultListener = nullptr; hook = [handler](std::shared_ptr msg) { AuthResult result{AuthResultCode::eAuthAccepted, "OK"}; - if (msg->proto == eProtocolAuth and not msg->payload.empty()) + if (msg->proto == ProtocolType::Auth and not msg->payload.empty()) { result.reason = std::string{ reinterpret_cast(msg->payload.data()), msg->payload.size()}; @@ -605,7 +605,7 @@ namespace llarp void OutboundContext::SendPacketToRemote(const llarp_buffer_t& buf) { - AsyncEncryptAndSendTo(buf, eProtocolExit); + AsyncEncryptAndSendTo(buf, ProtocolType::Exit); } } // namespace service diff --git a/llarp/service/protocol.hpp b/llarp/service/protocol.hpp index 51f126fac..24b56ec82 100644 --- a/llarp/service/protocol.hpp +++ b/llarp/service/protocol.hpp @@ -37,7 +37,7 @@ namespace llarp ProtocolMessage(const ConvoTag& tag); ProtocolMessage(); ~ProtocolMessage(); - ProtocolType proto = eProtocolTrafficV4; + ProtocolType proto = ProtocolType::TrafficV4; llarp_time_t queued = 0s; std::vector payload; Introduction introReply; diff --git a/llarp/service/protocol_type.hpp b/llarp/service/protocol_type.hpp index 695629a34..37521c520 100644 --- a/llarp/service/protocol_type.hpp +++ b/llarp/service/protocol_type.hpp @@ -4,11 +4,14 @@ namespace llarp::service { - using ProtocolType = uint64_t; - - constexpr ProtocolType eProtocolControl = 0UL; - constexpr ProtocolType eProtocolTrafficV4 = 1UL; - constexpr ProtocolType eProtocolTrafficV6 = 2UL; - constexpr ProtocolType eProtocolExit = 3UL; - constexpr ProtocolType eProtocolAuth = 4UL; + // Supported protocol types; the values are given explicitly because they are specifically used + // when sending over the wire. + enum class ProtocolType : uint64_t + { + Control = 0UL, + TrafficV4 = 1UL, + TrafficV6 = 2UL, + Exit = 3UL, + Auth = 4UL, + }; } // namespace llarp::service diff --git a/llarp/service/sendcontext.cpp b/llarp/service/sendcontext.cpp index 6d433b642..ede6b6ff9 100644 --- a/llarp/service/sendcontext.cpp +++ b/llarp/service/sendcontext.cpp @@ -114,7 +114,7 @@ namespace llarp { // send auth message const llarp_buffer_t authdata{maybe->token}; - AsyncGenIntro(authdata, eProtocolAuth); + AsyncGenIntro(authdata, ProtocolType::Auth); authResultListener = resultHandler; } else @@ -134,7 +134,7 @@ namespace llarp { // send auth message const llarp_buffer_t authdata(maybe->token); - AsyncGenIntro(authdata, eProtocolAuth); + AsyncGenIntro(authdata, ProtocolType::Auth); } else { diff --git a/llarp/util/bencode.hpp b/llarp/util/bencode.hpp index c7bebbcbc..48c977ee3 100644 --- a/llarp/util/bencode.hpp +++ b/llarp/util/bencode.hpp @@ -6,6 +6,7 @@ #include #include "mem.hpp" +#include #include #include #include @@ -41,7 +42,12 @@ namespace llarp bool BEncodeWriteDictInt(const char* k, const Int_t& i, llarp_buffer_t* buf) { - return bencode_write_bytestring(buf, k, 1) && bencode_write_uint64(buf, i); + if (!bencode_write_bytestring(buf, k, 1)) + return false; + if constexpr (std::is_enum_v) + return bencode_write_uint64(buf, static_cast>(i)); + else + return bencode_write_uint64(buf, i); } template @@ -92,7 +98,7 @@ namespace llarp return false; } - i = Int_t(read_i); + i = static_cast(read_i); read = true; } return true; diff --git a/pybind/llarp/context.cpp b/pybind/llarp/context.cpp index 6ebcf97d4..7fb369468 100644 --- a/pybind/llarp/context.cpp +++ b/pybind/llarp/context.cpp @@ -1,8 +1,9 @@ #include #include #include -#include +#include #include +#include "service/protocol_type.hpp" namespace llarp { void @@ -28,7 +29,8 @@ namespace llarp std::vector buf; buf.resize(pkt.size()); std::copy_n(pkt.c_str(), pkt.size(), buf.data()); - return ep and ep->SendToServiceOrQueue(to, std::move(buf), service::eProtocolControl); + return ep + and ep->SendToServiceOrQueue(to, std::move(buf), service::ProtocolType::Control); }) .def( "AddEndpoint",