diff --git a/llarp/address/ip_packet.cpp b/llarp/address/ip_packet.cpp index 3ac0b77f94..d1d19ba815 100644 --- a/llarp/address/ip_packet.cpp +++ b/llarp/address/ip_packet.cpp @@ -57,6 +57,9 @@ namespace llarp return ret; } + static const auto v4_header_version = oxenc::host_to_big(uint8_t{4}); + static const auto udp_header_proto = oxenc::host_to_big(uint8_t{17}); + void IPPacket::_init_internals() { _header = reinterpret_cast(data()); @@ -65,12 +68,8 @@ namespace llarp if (_buf.empty()) return; - // log::trace(logcat, "ippkt header: {}", buffer_printer{_buf}); - // log::trace(logcat, "ippkt protocol: {}", _header->protocol); - // log::trace(logcat, "ippkt version: {}", _header->version); - - _is_v4 = _header->version == oxenc::host_to_big(uint8_t{4}); - _is_udp = _header->protocol == uint8_t{17}; + _is_v4 = _header->version == v4_header_version; + _is_udp = _header->protocol == udp_header_proto; uint16_t src_port = (_is_udp) ? *reinterpret_cast(data() + (static_cast(_header->header_len) * 4)) : 0; @@ -126,8 +125,8 @@ namespace llarp std::basic_string_view head_u16s{reinterpret_cast(_header), sizeof(ip_header)}; // set new IP addresses - _header->src = src.addr; - _header->dest = dst.addr; + _header->src = oxenc::host_to_big(src.addr); + _header->dest = oxenc::host_to_big(dst.addr); switch (_header->protocol) { @@ -290,7 +289,7 @@ namespace llarp oxenc::write_host_as_big(1500, itr); itr += 2; - // copy ip header and first 8 bytes of datagram for icmp rject + // copy ip header and first 8 bytes of datagram for icmp reject std::memcpy(itr, _buf.data(), ip_hdr_sz + ICMP_HEADER_SIZE); itr += ip_hdr_sz + ICMP_HEADER_SIZE; diff --git a/llarp/address/ip_range.cpp b/llarp/address/ip_range.cpp index c47069af65..5ae5d47e53 100644 --- a/llarp/address/ip_range.cpp +++ b/llarp/address/ip_range.cpp @@ -91,6 +91,7 @@ namespace llarp for (const auto& e : excluding) if (e == range) return false; + log::debug(logcat, "{}", std::get(range).base); return true; }; @@ -109,7 +110,7 @@ namespace llarp { for (size_t n = 0; n < num_ipv6_private; ++n) { - if (auto v6 = ipv6(0xfd2e, 0x6c6f, 0x6b69, n) / 64; filter(v6)) + if (auto v6 = ipv6(0xfd2e, 0x6c6f, 0x6b69, n, 0x0000, 0x0000, 0x0000, 0x0001) / 64; filter(v6)) return v6; } } diff --git a/llarp/address/utils.hpp b/llarp/address/utils.hpp index 227dd58ccc..df5c2f995a 100644 --- a/llarp/address/utils.hpp +++ b/llarp/address/utils.hpp @@ -61,27 +61,29 @@ namespace llarp return ret; }; + inline constexpr auto DIGITS = "0123456789"sv; + inline constexpr auto PDIGITS = "0123456789."sv; + inline constexpr auto ALDIGITS = "0123456789abcdef:."sv; + inline std::pair parse_addr(std::string_view addr, std::optional default_port) { std::pair result; auto &[host, port] = result; - if (auto p = addr.find_last_not_of("0123456789"); + if (auto p = addr.find_last_not_of(DIGITS); p != std::string_view::npos && p + 2 <= addr.size() && addr[p] == ':') { if (!parse_int(addr.substr(p + 1), port)) throw std::invalid_argument{"Invalid address: could not parse port"}; addr.remove_suffix(addr.size() - p); } - else if (default_port) + else if (default_port.has_value()) // use ::has_value() in case default_port is set but is == 0 { - // log::critical(utilcat, "Setting default port for addr parse!"); port = *default_port; } else - { - throw std::invalid_argument{"Invalid address: no port was specified and there is no default"}; - } + throw std::invalid_argument{ + "Invalid address: argument contains no port and no default was specified (input:{})"_format(addr)}; bool had_sq_brackets = false; @@ -92,20 +94,14 @@ namespace llarp had_sq_brackets = true; } - if (auto p = addr.find_first_not_of("0123456789."); p != std::string_view::npos) + if (auto p = addr.find_first_not_of(PDIGITS); p != std::string_view::npos) { - if (auto q = addr.find_first_not_of("0123456789abcdef:."); q != std::string_view::npos) + if (auto q = addr.find_first_not_of(ALDIGITS); q != std::string_view::npos) throw std::invalid_argument{"Invalid address: does not look like IPv4 or IPv6!"}; if (!had_sq_brackets) throw std::invalid_argument{"Invalid address: IPv6 addresses require [...] square brackets"}; } - // if (addr.empty()) - // { - // log::critical(utilcat, "addr is empty, tough titties buddy"); // TESTNET: remove this log please - // // addr = "::"; - // } - host = addr; return result; } @@ -117,10 +113,10 @@ namespace llarp std::array ret{}; for (size_t n = 16; n < 32; ++n) - ret[n - 16] = ipv4(172, n, 0, 0) / 16; + ret[n - 16] = ipv4(172, n, 0, 1) / 16; for (size_t n = 0; n < 256; ++n) - ret[n + 16] = ipv4(10, n, 0, 0) / 16; + ret[n + 16] = ipv4(10, n, 0, 1) / 16; return ret; } diff --git a/llarp/config/config.cpp b/llarp/config/config.cpp index a27dea7f26..ad5e45b33a 100644 --- a/llarp/config/config.cpp +++ b/llarp/config/config.cpp @@ -1230,15 +1230,12 @@ namespace llarp if (auto pos = arg_v.find(':'); pos != arg_v.npos) { // host = arg_v.substr(0, pos); - log::critical(logcat, "Parsing input: {}", arg); std::tie(host, p) = detail::parse_addr(arg_v, DEFAULT_LISTEN_PORT); - log::critical(logcat, "Parsed input = {}:{}", host, p); } if (host.empty()) { - log::critical( - logcat, "Host value empty, port:{}{}", p, p == DEFAULT_LISTEN_PORT ? "(DEFAULT PORT)" : ""); + log::debug(logcat, "Host value empty, port:{}{}", p, p == DEFAULT_LISTEN_PORT ? "(DEFAULT PORT)" : ""); given_port_only = p != DEFAULT_LISTEN_PORT; maybe = net_ptr->get_best_public_address(true, p); } @@ -1248,7 +1245,7 @@ namespace llarp if (maybe and maybe->is_loopback()) throw std::invalid_argument{"{} is a loopback address"_format(arg)}; - log::critical(logcat, "parsed address: {}", *maybe); + log::trace(logcat, "parsed address: {}", *maybe); return maybe; }; diff --git a/llarp/handlers/session.cpp b/llarp/handlers/session.cpp index b92f2b6456..c3383527de 100644 --- a/llarp/handlers/session.cpp +++ b/llarp/handlers/session.cpp @@ -159,10 +159,10 @@ namespace llarp::handlers // { // testnet_trigger = true; - // _router.loop()->call_later(10s, [this]() { + // _router.loop()->call_later(5s, [this]() { // try // { - // RouterID cpk{oxenc::from_base32z("6e9wdnd4cj3j3rgc9ze8ctxqj4z976tmu8osbzwgabruabb4u1ky")}; + // RouterID cpk{oxenc::from_base32z("mprqiu67f4gr8hb4zx8kuuqmxanmct4b6fp1nkeeruhxx9tqwc7y")}; // log::info(logcat, "Beginning session init to client: {}", cpk.to_network_address(false)); // _initiate_session( // NetworkAddress::from_pubkey(cpk, true), [](ip_v) { log::critical(logcat, "FUCK YEAH"); diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index bc8ec16dcc..93b4d4c3fa 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -960,13 +960,14 @@ namespace llarp::handlers } } + // handles an outbound packet going OUT to the network void TunEndpoint::handle_outbound_packet(IPPacket pkt) { ip_v src, dest; auto pkt_is_ipv4 = pkt.is_ipv4(); - log::trace(logcat, "outbound packet is ipv{}", pkt_is_ipv4 ? "4" : "6"); + log::debug(logcat, "outbound packet: {}", pkt.info_line()); if (pkt_is_ipv4) { @@ -979,6 +980,8 @@ namespace llarp::handlers dest = pkt.dest_ipv6(); } + log::debug(logcat, "src:{}, dest:{}", src, dest); + if constexpr (llarp::platform::is_apple) { if (ip_equals_address(dest, _local_addr, pkt_is_ipv4)) @@ -1005,45 +1008,30 @@ namespace llarp::handlers session->send_path_data_message(std::move(pkt).steal_payload()); } else - log::warning(logcat, "Could not find session (remote: {}) for outbound packet!", remote); + log::info(logcat, "Could not find session (remote: {}) for outbound packet!", remote); } else log::debug(logcat, "Could not find remote for route {}", pkt.info_line()); } - bool TunEndpoint::obtain_src_for_remote(const NetworkAddress& remote, ip_v& src, bool use_ipv4) + std::optional TunEndpoint::obtain_src_for_remote(const NetworkAddress& remote, bool use_ipv4) { - // we are receiving traffic from a session to a local exit node if (auto maybe_src = _local_ip_mapping.get_local_from_remote(remote)) { if (std::holds_alternative(*maybe_src)) { if (use_ipv4) - src = *maybe_src; - else - { - auto quicaddr = oxen::quic::Address{std::get(*maybe_src)}; - src = quicaddr.to_ipv6(); - } - } - else - { - if (use_ipv4) - { - auto quicaddr = oxen::quic::Address{std::get(*maybe_src)}; - src = quicaddr.to_ipv4(); - } - else - src = *maybe_src; + return *maybe_src; + return oxen::quic::Address{std::get(*maybe_src)}.to_ipv6(); } - } - else - { - log::critical(logcat, "Unable to find local IP for inbound packet from remote: {}", remote); - return false; + + if (use_ipv4) + return oxen::quic::Address{std::get(*maybe_src)}.to_ipv4(); + return *maybe_src; } - return true; + log::warning(logcat, "Unable to find src IP for inbound packet from remote: {}", remote); + return std::nullopt; } void TunEndpoint::send_packet_to_net_if(IPPacket&& pkt) @@ -1058,9 +1046,11 @@ namespace llarp::handlers else pkt.update_ipv6_address(std::get(src), std::get(dest)); + log::debug(logcat, "Rewritten packet: {}: {}", pkt.info_line(), buffer_printer{pkt.uview()}); send_packet_to_net_if(std::move(pkt)); } + // handles an inbound packet coming IN from the network bool TunEndpoint::handle_inbound_packet( IPPacket pkt, NetworkAddress remote, bool is_exit_session, bool is_outbound_session) { @@ -1070,6 +1060,7 @@ namespace llarp::handlers if (is_exit_session and is_outbound_session) { + log::debug(logcat, "inbound exit session pkt: {}", pkt.info_line()); // we are receiving traffic from a session to a remote exit node if (pkt_is_ipv4) { @@ -1088,13 +1079,12 @@ namespace llarp::handlers if (not maybe_remote) { - log::critical( - logcat, "Could not find mapping of local IP (ip:{}) for session to remote: {}", src, remote); + log::info(logcat, "Could not find mapping of local IP (ip:{}) for session to remote: {}", src, remote); return false; } if (*maybe_remote != remote) { - log::critical( + log::info( logcat, "Internal mapping of local IP (ip:{}, remote:{}) did not match inbound packet from remote: {}", src, @@ -1107,6 +1097,7 @@ namespace llarp::handlers { if (is_exit_session and not is_outbound_session) { + log::debug(logcat, "inbound exit session pkt: {}", pkt.info_line()); // we are receiving traffic from a session to a local exit node if (not is_allowing_traffic(pkt)) return false; @@ -1118,6 +1109,7 @@ namespace llarp::handlers } else { + log::debug(logcat, "inbound service session pkt: {}", pkt.info_line()); // we are receiving hidden service traffic if (pkt_is_ipv4) dest = _local_addr.to_ipv4(); @@ -1125,10 +1117,14 @@ namespace llarp::handlers dest = _local_ipv6.to_ipv6(); } - if (not obtain_src_for_remote(remote, src, pkt_is_ipv4)) + if (auto maybe_src = obtain_src_for_remote(remote, pkt_is_ipv4)) + src = std::move(*maybe_src); + else return false; } + log::debug(logcat, "src:{}, dest:{}", src, dest); + rewrite_and_send_packet(std::move(pkt), src, dest); return true; diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index 36e24d2dc1..a0952ee1fd 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -108,12 +108,12 @@ namespace llarp::handlers void setup_dns(); // INPROGRESS: new API - // Handles an outbound packet going out INTO the network + // Handles an outbound packet going OUT to the network void handle_outbound_packet(IPPacket pkt); void rewrite_and_send_packet(IPPacket&& pkt, ip_v src, ip_v dest); - // Handle an inbound packet coming in FROM the network + // Handles an inbound packet coming IN from the network bool handle_inbound_packet(IPPacket pkt, NetworkAddress remote, bool is_exit_session, bool is_outbound_session); // Upon session creation, SessionHandler will instruct TunEndpoint to requisition a private IP through which to @@ -162,7 +162,7 @@ namespace llarp::handlers private: std::optional get_next_local_ip(); - bool obtain_src_for_remote(const NetworkAddress& remote, ip_v& src, bool use_ipv4); + std::optional obtain_src_for_remote(const NetworkAddress& remote, bool use_ipv4); void send_packet_to_net_if(IPPacket&& pkt); }; diff --git a/llarp/link/link_manager.cpp b/llarp/link/link_manager.cpp index 64cd66d397..a79f86e965 100644 --- a/llarp/link/link_manager.cpp +++ b/llarp/link/link_manager.cpp @@ -1527,8 +1527,6 @@ namespace llarp return; } - log::info(logcat, "Received path data for local client: {}", buffer_printer{payload}); - for (auto& hop : path->hops) { nonce = crypto::onion( @@ -1537,17 +1535,16 @@ namespace llarp hop.kx.shared_secret, nonce, hop.kx.xor_nonce); - - log::debug(logcat, "xchacha20 -> {}", buffer_printer{payload}); } + log::info(logcat, "Received path data for local client: {}", buffer_printer{payload}); + NetworkAddress sender; bstring data; try { - oxenc::bt_dict_consumer btdc{payload}; - std::tie(sender, data) = PATH::DATA::deserialize(btdc); + std::tie(sender, data) = PATH::DATA::deserialize(oxenc::bt_dict_consumer{payload}); if (auto session = _router.session_endpoint()->get_session(sender)) { @@ -1560,13 +1557,11 @@ namespace llarp } catch (const std::exception& e) { - log::warning(logcat, "Exception: {}: {}", e.what(), buffer_printer{data}); + log::warning(logcat, "Exception: {}: {}", e.what(), buffer_printer{payload}); } return; } - log::debug(logcat, "Received path data for local relay: {}", buffer_printer{payload}); - auto hop = _router.path_context()->get_transit_hop(hop_id); if (not hop) @@ -1587,12 +1582,16 @@ namespace llarp std::optional> next_ids = std::nullopt; std::string next_payload; + log::debug( + logcat, + "We are {} hop for path data: {}: {}", + hop->terminal_hop ? "terminal" : "intermediate", + hop->to_string(), + buffer_printer{payload}); + // if terminal hop, pass to the correct path expecting to receive this message if (hop->terminal_hop) { - log::debug( - logcat, "We are terminal hop for path data: {}: {}", hop->to_string(), buffer_printer{payload}); - HopID ihid; std::string intermediate; @@ -1618,7 +1617,7 @@ namespace llarp return; } - log::debug(logcat, "Bridging path data message on hop: {}", next_hop->to_string()); + log::debug(logcat, "Bridging path data message to hop: {}", next_hop->to_string()); next_ids = next_hop->next_id(ihid); diff --git a/llarp/messages/path.hpp b/llarp/messages/path.hpp index 69d5bac675..ddda730de8 100644 --- a/llarp/messages/path.hpp +++ b/llarp/messages/path.hpp @@ -287,7 +287,7 @@ namespace llarp return std::move(btdp).str(); } - inline static std::tuple deserialize(oxenc::bt_dict_consumer& btdc) + inline static std::tuple deserialize(oxenc::bt_dict_consumer&& btdc) { RouterID remote; bstring payload; diff --git a/llarp/net/net.hpp b/llarp/net/net.hpp index dea007acb7..4a6bdee6dc 100644 --- a/llarp/net/net.hpp +++ b/llarp/net/net.hpp @@ -42,12 +42,9 @@ namespace llarp namespace net { - /// info about a network interface lokinet does not own struct InterfaceInfo { - private: - public: // TODO: is this needed? /// a gateway we can use if it exists std::optional _gateway; diff --git a/llarp/net/posix.cpp b/llarp/net/posix.cpp index 0e341e1931..9bbcc65e19 100644 --- a/llarp/net/posix.cpp +++ b/llarp/net/posix.cpp @@ -92,7 +92,7 @@ namespace llarp::net oxen::quic::Address addr{i->ifa_addr}; auto nma = reinterpret_cast(i->ifa_netmask)->sin_addr.s_addr; auto m = std::popcount(nma); - log::trace( + log::debug( logcat, "Adding {} {} (mask={}) to current ranges", addr.is_ipv4() ? "ipv4" : "ipv6", addr, m); current_ranges.emplace_back(std::move(addr), std::move(m)); } diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 436c3b16d9..00bcfa257c 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -551,12 +551,12 @@ namespace llarp // TODO: load strict-connects as bootstraps as well - log::info(logcat, "Local client configured to strictly use {} edge relays", n_edges); + log::debug(logcat, "Local client configured to strictly use {} edge relays", n_edges); if (min_client_outbounds > n_edges) { min_client_outbounds = n_edges; - log::info( + log::debug( logcat, "Local client holds only {} strict-connect edge relays; adjusting minimum router connections " "commensurately", @@ -564,7 +564,7 @@ namespace llarp } } else - log::info( + log::debug( logcat, "Local client configured to maintain {} router connections at minimum", min_client_outbounds); if (not min_client_outbounds) diff --git a/llarp/session/session.cpp b/llarp/session/session.cpp index 02d6b1864d..91ab487dee 100644 --- a/llarp/session/session.cpp +++ b/llarp/session/session.cpp @@ -29,8 +29,7 @@ namespace llarp::session _tag{std::move(_t)}, _remote{std::move(remote)}, _use_tun{use_tun}, - _is_outbound{is_outbound}, - _is_exit_session{kx_data.has_value()} + _is_outbound{is_outbound} { if (kx_data.has_value()) session_keys = std::move(*kx_data); @@ -48,7 +47,6 @@ namespace llarp::session bool BaseSession::send_path_data_message(std::string data) { - // session_keys.encrypt(to_uspan(data)); auto inner_payload = PATH::DATA::serialize(std::move(data), _r.local_rid()); auto intermediate_payload = PATH::DATA::serialize_intermediate(std::move(inner_payload), remote_intro.pivot_txid); diff --git a/llarp/vpn/linux.hpp b/llarp/vpn/linux.hpp index ca209bdddd..1feb8904ae 100644 --- a/llarp/vpn/linux.hpp +++ b/llarp/vpn/linux.hpp @@ -84,7 +84,7 @@ namespace llarp::vpn control.ioctl(SIOCSIFADDR, &ifr); auto subnet_mask = (ipv4_subnet / range.mask()).base; - log::trace(logcat, "IP Range:{}, subnet mask: {}", range, subnet_mask); + log::debug(logcat, "IP Range:{}, subnet mask: {}", range, subnet_mask); ((sockaddr_in*)&ifr.ifr_netmask)->sin_addr.s_addr = oxenc::load_host_to_big(&subnet_mask.addr); diff --git a/llarp/vpn/packet_router.cpp b/llarp/vpn/packet_router.cpp index 4c99e6e08b..b34ba55f13 100644 --- a/llarp/vpn/packet_router.cpp +++ b/llarp/vpn/packet_router.cpp @@ -20,7 +20,7 @@ namespace llarp::vpn void handle_ip_packet(IPPacket pkt) override { - log::debug(logcat, "inbound pkt: ", pkt.info_line()); + log::debug(logcat, "udp pkt: ", pkt.info_line()); auto dstport = pkt.dest_port(); if (not dstport) @@ -45,7 +45,7 @@ namespace llarp::vpn void handle_ip_packet(IPPacket pkt) override { - log::debug(logcat, "inbound pkt: {}", pkt.info_line()); + log::debug(logcat, "l4 pkt: {}", pkt.info_line()); // TOFIX: // _base_handler(IPPacket::from_udp(std::move(pkt))); } @@ -55,7 +55,7 @@ namespace llarp::vpn void PacketRouter::handle_ip_packet(IPPacket pkt) { - log::debug(logcat, "inbound pkt: {}", pkt.info_line()); + log::debug(logcat, "ip pkt: {}", pkt.info_line()); auto dest_port = pkt.dest_port(); if (not dest_port)