Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "NetEnet.h" 21 : 22 : #include "ps/ConfigDB.h" 23 : #include <span> 24 : 25 : namespace PS 26 : { 27 : 28 : namespace Enet 29 : { 30 : 31 : // ENet protocol MTU 32 : // ENet by default uses 1400 as ENet packet fragment max size, adding ICMP and 33 : // IPv4 headers this may exceed the MTU for some VPN solutions [1], so provide 34 : // a lower default. 35 : // MTU negotiation server-side needs [2], which was merged after enet-1.3.17, 36 : // so the user configured value may be ignored on older versions. 37 : // [1] https://github.com/lsalzman/enet/issues/132 38 : // [2] https://github.com/lsalzman/enet/pull/222 39 : constexpr enet_uint32 HOST_DEFAULT_MTU = 1372; 40 : 41 0 : ENetHost* CreateHost(const ENetAddress* address, size_t peerCount, size_t channelLimit) 42 : { 43 : // TODO: Maybe allow user to set rate limits? 44 : 45 0 : ENetHost* host = enet_host_create(address, peerCount, channelLimit, 0, 0); 46 0 : if (!host) 47 : return nullptr; 48 : 49 : // Public ENet API doesn't offer a means to change MTU, so do it in a 50 : // way least likely to break with ENet updates. 51 0 : host->mtu = g_ConfigDB.Get("network.enetmtu", HOST_DEFAULT_MTU); 52 0 : for (ENetPeer& p : std::span{host->peers, host->peerCount}) 53 0 : enet_peer_reset(&p); 54 : 55 0 : return host; 56 : } 57 : 58 : } // namespace Enet 59 : 60 : } // namespace PS