| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | #ifndef ENGINE_SHARED_NETWORK_H |
| 4 | #define ENGINE_SHARED_NETWORK_H |
| 5 | |
| 6 | #include "ringbuffer.h" |
| 7 | #include "stun.h" |
| 8 | |
| 9 | #include <base/net.h> |
| 10 | #include <base/types.h> |
| 11 | |
| 12 | #include <array> |
| 13 | #include <optional> |
| 14 | #include <vector> |
| 15 | |
| 16 | class CHuffman; |
| 17 | class CNetBan; |
| 18 | class CPacker; |
| 19 | |
| 20 | /* |
| 21 | |
| 22 | CURRENT: |
| 23 | packet header: 3 bytes |
| 24 | unsigned char flags_ack; // 6bit flags, 2bit ack |
| 25 | 0.6: ORNCaaAA |
| 26 | 0.6.5: ORNCT-AA |
| 27 | 0.7: --NORCAA |
| 28 | |
| 29 | O = flag compression |
| 30 | R = flag resend |
| 31 | N = flag connless |
| 32 | C = flag control |
| 33 | T = flag token (0.6.5 only not supported by ddnet) |
| 34 | - = unused, should be zero |
| 35 | a = should be zero otherwise it messes up the ack number |
| 36 | A = bit of ack number |
| 37 | |
| 38 | |
| 39 | unsigned char ack; // 8 bit ack |
| 40 | unsigned char num_chunks; // 8 bit chunks |
| 41 | |
| 42 | (unsigned char padding[3]) // 24 bit extra in case it's a connection less packet |
| 43 | // this is to make sure that it's compatible with the |
| 44 | // old protocol |
| 45 | |
| 46 | chunk header: 2-3 bytes |
| 47 | unsigned char flags_size; // 2bit flags, 6 bit size |
| 48 | unsigned char size_seq; // 4bit size, 4bit seq |
| 49 | (unsigned char seq;) // 8bit seq, if vital flag is set |
| 50 | */ |
| 51 | |
| 52 | enum |
| 53 | { |
| 54 | NETSENDFLAG_VITAL = 1 << 0, |
| 55 | NETSENDFLAG_CONNLESS = 1 << 1, |
| 56 | NETSENDFLAG_FLUSH = 1 << 2, |
| 57 | NETSENDFLAG_EXTENDED = 1 << 3, |
| 58 | }; |
| 59 | |
| 60 | enum |
| 61 | { |
| 62 | NETSTATE_OFFLINE = 0, |
| 63 | NETSTATE_CONNECTING, |
| 64 | NETSTATE_ONLINE, |
| 65 | }; |
| 66 | |
| 67 | enum |
| 68 | { |
| 69 | NET_MAX_PACKETSIZE = 1400, |
| 70 | NET_MAX_CONNLESS_PAYLOAD = NET_MAX_PACKETSIZE - 6, |
| 71 | /** |
| 72 | * The maximum size of a chunk within a connection-oriented packet. |
| 73 | * |
| 74 | * This is 1023 because the size is packed into 10 bits in the chunk header. |
| 75 | */ |
| 76 | NET_MAX_CHUNK_SIZE = 1023, |
| 77 | = 3, |
| 78 | = 3, |
| 79 | = 4, |
| 80 | NET_MAX_CLIENTS = 128, |
| 81 | NET_MAX_CONSOLE_CLIENTS = 4, |
| 82 | NET_MAX_SEQUENCE = 1 << 10, |
| 83 | NET_MAX_PACKET_CHUNKS = 0xFF, |
| 84 | |
| 85 | NET_PACKETFLAG_UNUSED = 1 << 0, |
| 86 | NET_PACKETFLAG_TOKEN = 1 << 1, |
| 87 | NET_PACKETFLAG_CONTROL = 1 << 2, |
| 88 | NET_PACKETFLAG_CONNLESS = 1 << 3, |
| 89 | NET_PACKETFLAG_RESEND = 1 << 4, |
| 90 | NET_PACKETFLAG_COMPRESSION = 1 << 5, |
| 91 | // NOT SENT VIA THE NETWORK DIRECTLY: |
| 92 | NET_PACKETFLAG_EXTENDED = 1 << 6, |
| 93 | |
| 94 | NET_CHUNKFLAG_VITAL = 1, |
| 95 | NET_CHUNKFLAG_RESEND = 2, |
| 96 | |
| 97 | NET_CTRLMSG_KEEPALIVE = 0, |
| 98 | NET_CTRLMSG_CONNECT = 1, |
| 99 | NET_CTRLMSG_CONNECTACCEPT = 2, |
| 100 | NET_CTRLMSG_ACCEPT = 3, |
| 101 | NET_CTRLMSG_CLOSE = 4, |
| 102 | |
| 103 | NET_CONN_BUFFERSIZE = 1024 * 32, |
| 104 | |
| 105 | // Addresses tracked for `sv_connlimit`, evicted least recently used. The limit stops |
| 106 | // applying once addresses are evicted before they reach `sv_connlimit`. |
| 107 | NET_CONNLIMIT_IPS = 256, |
| 108 | |
| 109 | NET_TOKENCACHE_ADDRESSEXPIRY = 64, |
| 110 | NET_TOKENCACHE_PACKETEXPIRY = 5, |
| 111 | }; |
| 112 | enum |
| 113 | { |
| 114 | NET_TOKEN_MAX = 0xffffffff, |
| 115 | NET_TOKEN_NONE = NET_TOKEN_MAX, |
| 116 | NET_TOKEN_MASK = NET_TOKEN_MAX, |
| 117 | |
| 118 | NET_TOKENREQUEST_DATASIZE = 512, |
| 119 | }; |
| 120 | |
| 121 | typedef int SECURITY_TOKEN; |
| 122 | typedef unsigned int TOKEN; |
| 123 | |
| 124 | SECURITY_TOKEN ToSecurityToken(const unsigned char *pData); |
| 125 | void WriteSecurityToken(unsigned char *pData, SECURITY_TOKEN Token); |
| 126 | |
| 127 | extern const unsigned char SECURITY_TOKEN_MAGIC[4]; |
| 128 | |
| 129 | enum |
| 130 | { |
| 131 | NET_SECURITY_TOKEN_UNKNOWN = -1, |
| 132 | NET_SECURITY_TOKEN_UNSUPPORTED = 0, |
| 133 | }; |
| 134 | |
| 135 | typedef int (*NETFUNC_DELCLIENT)(int ClientId, const char *pReason, void *pUser); |
| 136 | typedef int (*NETFUNC_NEWCLIENT_CON)(int ClientId, void *pUser); |
| 137 | typedef int (*NETFUNC_NEWCLIENT)(int ClientId, void *pUser, bool Sixup); |
| 138 | typedef int (*NETFUNC_NEWCLIENT_NOAUTH)(int ClientId, void *pUser); |
| 139 | typedef int (*NETFUNC_CLIENTREJOIN)(int ClientId, void *pUser, bool Sixup, bool VanillaAuth); |
| 140 | |
| 141 | struct CNetChunk |
| 142 | { |
| 143 | // -1 means that it's a stateless packet |
| 144 | // 0 on the client means the server |
| 145 | int m_ClientId; |
| 146 | NETADDR m_Address; // only used when client_id == -1 |
| 147 | int m_Flags; |
| 148 | int m_DataSize; |
| 149 | const void *m_pData; |
| 150 | // only used if the flags contain NETSENDFLAG_EXTENDED and NETSENDFLAG_CONNLESS |
| 151 | unsigned char [NET_CONNLESS_EXTRA_SIZE]; |
| 152 | |
| 153 | void AssertSizeSanity() const; |
| 154 | }; |
| 155 | |
| 156 | class |
| 157 | { |
| 158 | public: |
| 159 | int ; |
| 160 | int ; |
| 161 | int ; |
| 162 | |
| 163 | unsigned char *(unsigned char *pData, int Split = 4) const; |
| 164 | unsigned char *(unsigned char *pData, int Split = 4); |
| 165 | }; |
| 166 | |
| 167 | class CNetChunkResend |
| 168 | { |
| 169 | public: |
| 170 | int m_Flags; |
| 171 | int m_DataSize; |
| 172 | unsigned char *m_pData; |
| 173 | |
| 174 | int m_Sequence; |
| 175 | int64_t m_LastSendTime; |
| 176 | int64_t m_FirstSendTime; |
| 177 | }; |
| 178 | |
| 179 | class CNetPacketConstruct |
| 180 | { |
| 181 | public: |
| 182 | int m_Flags; |
| 183 | int m_Ack; |
| 184 | int m_NumChunks; |
| 185 | int m_DataSize; |
| 186 | unsigned char m_aChunkData[NET_MAX_PACKETSIZE - NET_PACKETHEADERSIZE]; |
| 187 | unsigned char [NET_CONNLESS_EXTRA_SIZE]; |
| 188 | }; |
| 189 | |
| 190 | enum class CONNECTIVITY |
| 191 | { |
| 192 | UNKNOWN, |
| 193 | CHECKING, |
| 194 | UNREACHABLE, |
| 195 | REACHABLE, |
| 196 | ADDRESS_KNOWN, |
| 197 | }; |
| 198 | |
| 199 | class CStun |
| 200 | { |
| 201 | class CProtocol |
| 202 | { |
| 203 | int m_Index; |
| 204 | NETSOCKET m_Socket; |
| 205 | CStunData m_Stun; |
| 206 | bool m_HaveStunServer = false; |
| 207 | NETADDR m_StunServer; |
| 208 | bool m_HaveAddr = false; |
| 209 | NETADDR m_Addr; |
| 210 | int64_t m_LastResponse = -1; |
| 211 | int64_t m_NextTry = -1; |
| 212 | int m_NumUnsuccessfulTries = -1; |
| 213 | |
| 214 | public: |
| 215 | CProtocol(int Index, NETSOCKET Socket); |
| 216 | void FeedStunServer(NETADDR StunServer); |
| 217 | void Refresh(); |
| 218 | void Update(); |
| 219 | bool OnPacket(NETADDR Addr, unsigned char *pData, int DataSize); |
| 220 | CONNECTIVITY GetConnectivity(NETADDR *pGlobalAddr); |
| 221 | }; |
| 222 | CProtocol m_aProtocols[2]; |
| 223 | |
| 224 | public: |
| 225 | CStun(NETSOCKET Socket); |
| 226 | void FeedStunServer(NETADDR StunServer); |
| 227 | void Refresh(); |
| 228 | void Update(); |
| 229 | bool OnPacket(NETADDR Addr, unsigned char *pData, int DataSize); |
| 230 | CONNECTIVITY GetConnectivity(int NetType, NETADDR *pGlobalAddr); |
| 231 | }; |
| 232 | |
| 233 | class CNetConnection |
| 234 | { |
| 235 | // TODO: is this needed because this needs to be aware of |
| 236 | // the ack sequencing number and is also responsible for updating |
| 237 | // that. this should be fixed. |
| 238 | friend class CPacketChunkUnpacker; |
| 239 | |
| 240 | public: |
| 241 | enum class EState |
| 242 | { |
| 243 | OFFLINE, |
| 244 | WANT_TOKEN, |
| 245 | CONNECT, |
| 246 | PENDING, |
| 247 | ONLINE, |
| 248 | ERROR, |
| 249 | }; |
| 250 | |
| 251 | SECURITY_TOKEN m_SecurityToken; |
| 252 | |
| 253 | private: |
| 254 | unsigned short m_Sequence; |
| 255 | unsigned short m_Ack; |
| 256 | unsigned short m_PeerAck; |
| 257 | EState m_State = EState::OFFLINE; |
| 258 | int m_RemoteClosed; |
| 259 | bool m_BlockCloseMsg; |
| 260 | bool m_UnknownSeq; |
| 261 | |
| 262 | CStaticRingBuffer<CNetChunkResend, NET_CONN_BUFFERSIZE> m_Buffer; |
| 263 | |
| 264 | int64_t m_LastUpdateTime; |
| 265 | int64_t m_LastRecvTime; |
| 266 | int64_t m_LastSendTime; |
| 267 | int64_t m_LastResendTime; |
| 268 | bool m_ResendRequested; |
| 269 | |
| 270 | char m_aErrorString[256]; |
| 271 | |
| 272 | CNetPacketConstruct m_Construct; |
| 273 | |
| 274 | NETADDR m_aConnectAddrs[16]; |
| 275 | int m_NumConnectAddrs; |
| 276 | NETADDR m_PeerAddr; |
| 277 | NETSOCKET m_Socket; |
| 278 | NETSTATS m_Stats; |
| 279 | |
| 280 | std::array<char, NETADDR_MAXSTRSIZE> m_aPeerAddrStr; |
| 281 | std::array<char, NETADDR_MAXSTRSIZE> m_aPeerAddrStrNoPort; |
| 282 | // client 0.7 |
| 283 | static TOKEN GenerateToken7(const NETADDR *pPeerAddr); |
| 284 | class CNetBase *m_pNetBase; |
| 285 | bool IsSixup() const { return m_Sixup; } |
| 286 | |
| 287 | // |
| 288 | bool IsPeerAddress(const NETADDR &Addr) const; |
| 289 | void SetPeerAddr(const NETADDR *pAddr); |
| 290 | void ClearPeerAddr(); |
| 291 | void ResetStats(); |
| 292 | void SetError(const char *pString); |
| 293 | void AckChunks(int Ack); |
| 294 | |
| 295 | int QueueChunkEx(int Flags, int DataSize, const void *pData, int Sequence); |
| 296 | void SendConnect(); |
| 297 | void SendControl(int ControlMsg, const void *, int ); |
| 298 | void SendControlWithToken7(int ControlMsg, SECURITY_TOKEN ResponseToken); |
| 299 | void ResendChunk(CNetChunkResend *pResend); |
| 300 | void Resend(); |
| 301 | void AnswerResendRequest(int64_t Now); |
| 302 | |
| 303 | public: |
| 304 | bool m_TimeoutProtected; |
| 305 | bool m_TimeoutSituation; |
| 306 | |
| 307 | void SetToken7(TOKEN Token); |
| 308 | |
| 309 | void Reset(); |
| 310 | void Init(NETSOCKET Socket, bool BlockCloseMsg); |
| 311 | int Connect(const NETADDR *pAddr, int NumAddrs); |
| 312 | int Connect7(const NETADDR *pAddr, int NumAddrs); |
| 313 | void Disconnect(const char *pReason); |
| 314 | |
| 315 | int Update(); |
| 316 | int Flush(); |
| 317 | |
| 318 | int Feed(CNetPacketConstruct *pPacket, NETADDR *pAddr, SECURITY_TOKEN SecurityToken = NET_SECURITY_TOKEN_UNSUPPORTED, SECURITY_TOKEN ResponseToken = NET_SECURITY_TOKEN_UNSUPPORTED); |
| 319 | int QueueChunk(int Flags, int DataSize, const void *pData); |
| 320 | |
| 321 | const char *ErrorString(); |
| 322 | void SignalResend(); |
| 323 | EState State() const { return m_State; } |
| 324 | const NETADDR *PeerAddress() const { return &m_PeerAddr; } |
| 325 | const std::array<char, NETADDR_MAXSTRSIZE> &PeerAddressString(bool IncludePort) const |
| 326 | { |
| 327 | return IncludePort ? m_aPeerAddrStr : m_aPeerAddrStrNoPort; |
| 328 | } |
| 329 | void ConnectAddresses(const NETADDR **ppAddrs, int *pNumAddrs) const |
| 330 | { |
| 331 | *ppAddrs = m_aConnectAddrs; |
| 332 | *pNumAddrs = m_NumConnectAddrs; |
| 333 | } |
| 334 | |
| 335 | void ResetErrorString() { m_aErrorString[0] = 0; } |
| 336 | const char *ErrorString() const { return m_aErrorString; } |
| 337 | |
| 338 | // Needed for GotProblems in NetClient |
| 339 | int64_t LastRecvTime() const { return m_LastRecvTime; } |
| 340 | int64_t ConnectTime() const { return m_LastUpdateTime; } |
| 341 | |
| 342 | int AckSequence() const { return m_Ack; } |
| 343 | int SeqSequence() const { return m_Sequence; } |
| 344 | int SecurityToken() const { return m_SecurityToken; } |
| 345 | CStaticRingBuffer<CNetChunkResend, NET_CONN_BUFFERSIZE> *ResendBuffer() { return &m_Buffer; } |
| 346 | |
| 347 | void ResumeConnection(const NETADDR *pAddr, int Sequence, int Ack, SECURITY_TOKEN SecurityToken, CStaticRingBuffer<CNetChunkResend, NET_CONN_BUFFERSIZE> *pResendBuffer, bool Sixup); |
| 348 | |
| 349 | // anti spoof |
| 350 | void DirectInit(const NETADDR &Addr, SECURITY_TOKEN SecurityToken, SECURITY_TOKEN Token, bool Sixup); |
| 351 | void SetUnknownSeq() { m_UnknownSeq = true; } |
| 352 | void SetSequence(int Sequence) { m_Sequence = Sequence; } |
| 353 | |
| 354 | bool m_Sixup; |
| 355 | SECURITY_TOKEN m_Token; |
| 356 | }; |
| 357 | |
| 358 | class CConsoleNetConnection |
| 359 | { |
| 360 | public: |
| 361 | enum class EState |
| 362 | { |
| 363 | OFFLINE, |
| 364 | ONLINE, |
| 365 | ERROR, |
| 366 | }; |
| 367 | |
| 368 | private: |
| 369 | EState m_State; |
| 370 | |
| 371 | NETADDR m_PeerAddr; |
| 372 | std::array<char, NETADDR_MAXSTRSIZE> m_aPeerAddrStr; |
| 373 | NETSOCKET m_Socket; |
| 374 | |
| 375 | char m_aBuffer[NET_MAX_PACKETSIZE]; |
| 376 | int m_BufferOffset; |
| 377 | |
| 378 | char m_aErrorString[256]; |
| 379 | |
| 380 | bool m_LineEndingDetected; |
| 381 | char m_aLineEnding[3]; |
| 382 | |
| 383 | void SetPeerAddr(const NETADDR *pAddr); |
| 384 | void ClearPeerAddr(); |
| 385 | |
| 386 | public: |
| 387 | int Init(NETSOCKET Socket, const NETADDR *pAddr); |
| 388 | void Disconnect(const char *pReason); |
| 389 | |
| 390 | EState State() const { return m_State; } |
| 391 | const NETADDR *PeerAddress() const { return &m_PeerAddr; } |
| 392 | const std::array<char, NETADDR_MAXSTRSIZE> &PeerAddressString() const { return m_aPeerAddrStr; } |
| 393 | const char *ErrorString() const { return m_aErrorString; } |
| 394 | |
| 395 | void Reset(); |
| 396 | int Update(); |
| 397 | int Send(const char *pLine); |
| 398 | int Recv(char *pLine, int MaxLength); |
| 399 | }; |
| 400 | |
| 401 | /** |
| 402 | * Accepts a non-control packet containing one or more chunks and unpacks each chunk individually. |
| 403 | * After a packet has been fed into the unpacker by calling @link FeedPacket @endlink, all chunks have |
| 404 | * to be unpacked by calling @link UnpackNextChunk @endlink until the function returns `false`, before |
| 405 | * the unpacker can be fed another packet. |
| 406 | */ |
| 407 | class CPacketChunkUnpacker |
| 408 | { |
| 409 | public: |
| 410 | void FeedPacket(const NETADDR &Addr, const CNetPacketConstruct &Packet, CNetConnection *pConnection, int ClientId); |
| 411 | bool UnpackNextChunk(CNetChunk *pChunk); |
| 412 | void Reset(); |
| 413 | |
| 414 | private: |
| 415 | bool m_Valid = false; |
| 416 | NETADDR m_Addr; |
| 417 | CNetConnection *m_pConnection; |
| 418 | int m_CurrentChunk; |
| 419 | // offset of the next chunk header in m_Data.m_aChunkData |
| 420 | int m_CurrentOffset; |
| 421 | int m_ClientId; |
| 422 | CNetPacketConstruct m_Data; |
| 423 | }; |
| 424 | |
| 425 | // server side |
| 426 | class CNetServer |
| 427 | { |
| 428 | struct CSlot |
| 429 | { |
| 430 | public: |
| 431 | CNetConnection m_Connection; |
| 432 | }; |
| 433 | |
| 434 | struct CSpamConn |
| 435 | { |
| 436 | NETADDR m_Addr; |
| 437 | // start of the timespan the connections are counted in |
| 438 | int64_t m_Time; |
| 439 | // last connection, only used to pick the entry to evict |
| 440 | int64_t m_LastSeen; |
| 441 | int m_Conns; |
| 442 | }; |
| 443 | |
| 444 | NETADDR m_Address; |
| 445 | NETSOCKET m_Socket; |
| 446 | CNetBan *m_pNetBan; |
| 447 | CSlot m_aSlots[NET_MAX_CLIENTS]; |
| 448 | int m_MaxClients = NET_MAX_CLIENTS; |
| 449 | int m_MaxClientsPerIp; |
| 450 | |
| 451 | bool m_FlushBatch = false; |
| 452 | bool m_aFlushPending[NET_MAX_CLIENTS] = {}; |
| 453 | |
| 454 | NETFUNC_NEWCLIENT m_pfnNewClient; |
| 455 | NETFUNC_NEWCLIENT_NOAUTH m_pfnNewClientNoAuth; |
| 456 | NETFUNC_DELCLIENT m_pfnDelClient; |
| 457 | NETFUNC_CLIENTREJOIN m_pfnClientRejoin; |
| 458 | void *m_pUser; |
| 459 | |
| 460 | unsigned char m_aSecurityTokenSeed[16]; |
| 461 | |
| 462 | // vanilla connect flood detection |
| 463 | int64_t m_VConnFirst; |
| 464 | int m_VConnNum; |
| 465 | |
| 466 | // budgets for work unauthenticated peers can request, reset in Update(): |
| 467 | // `m_NumRecvPackets` per Recv() batch, the others per second |
| 468 | int m_NumRecvPackets = 0; |
| 469 | int64_t m_BudgetStart = 0; |
| 470 | int m_NumPreConnDecompress = 0; |
| 471 | int m_NumBanReplies = 0; |
| 472 | |
| 473 | CSpamConn m_aSpamConns[NET_CONNLIMIT_IPS] = {}; |
| 474 | |
| 475 | CPacketChunkUnpacker m_PacketChunkUnpacker; |
| 476 | CNetPacketConstruct m_RecvBuffer; |
| 477 | |
| 478 | void OnTokenCtrlMsg(NETADDR &Addr, int ControlMsg, const CNetPacketConstruct &Packet, int Slot); |
| 479 | int OnSixupCtrlMsg(NETADDR &Addr, CNetChunk *pChunk, int ControlMsg, const CNetPacketConstruct &Packet, SECURITY_TOKEN &ResponseToken, SECURITY_TOKEN Token, int Slot); |
| 480 | void OnPreConnMsg(NETADDR &Addr, CNetPacketConstruct &Packet, int Slot); |
| 481 | bool ClientExists(const NETADDR &Addr) { return GetClientSlot(Addr) != -1; } |
| 482 | int GetClientSlot(const NETADDR &Addr); |
| 483 | void SendControl(NETADDR &Addr, int ControlMsg, const void *, int , SECURITY_TOKEN SecurityToken); |
| 484 | |
| 485 | int TryAcceptClient(NETADDR &Addr, SECURITY_TOKEN SecurityToken, int Slot, bool VanillaAuth = false, bool Sixup = false, SECURITY_TOKEN Token = 0); |
| 486 | int NumClientsWithAddr(NETADDR Addr); |
| 487 | bool HasTimedOutClientWithAddr(NETADDR Addr); |
| 488 | bool Connlimit(NETADDR Addr); |
| 489 | void SendMsgs(NETADDR &Addr, const CPacker **ppMsgs, int Num); |
| 490 | |
| 491 | public: |
| 492 | int SetCallbacks(NETFUNC_NEWCLIENT pfnNewClient, NETFUNC_DELCLIENT pfnDelClient, void *pUser); |
| 493 | int SetCallbacks(NETFUNC_NEWCLIENT pfnNewClient, NETFUNC_NEWCLIENT_NOAUTH pfnNewClientNoAuth, NETFUNC_CLIENTREJOIN pfnClientRejoin, NETFUNC_DELCLIENT pfnDelClient, void *pUser); |
| 494 | |
| 495 | // |
| 496 | bool Open(NETADDR BindAddr, CNetBan *pNetBan, int MaxClients, int MaxClientsPerIp); |
| 497 | void Close(); |
| 498 | |
| 499 | // |
| 500 | int Recv(CNetChunk *pChunk, SECURITY_TOKEN *pResponseToken); |
| 501 | int Send(CNetChunk *pChunk); |
| 502 | void Update(); |
| 503 | |
| 504 | // While a flush batch is open, sends requesting MSGFLAG_FLUSH only queue |
| 505 | // their chunk and mark the connection; EndFlushBatch() then flushes each |
| 506 | // marked connection once. Used to coalesce the flushes triggered while |
| 507 | // draining a burst of incoming packets into one packet per recipient. |
| 508 | void BeginFlushBatch() { m_FlushBatch = true; } |
| 509 | void EndFlushBatch(); |
| 510 | |
| 511 | // |
| 512 | void Drop(int ClientId, const char *pReason); |
| 513 | |
| 514 | // status requests |
| 515 | const NETADDR *ClientAddr(int ClientId) const { return m_aSlots[ClientId].m_Connection.PeerAddress(); } |
| 516 | const std::array<char, NETADDR_MAXSTRSIZE> &ClientAddrString(int ClientId, bool IncludePort) const { return m_aSlots[ClientId].m_Connection.PeerAddressString(IncludePort); } |
| 517 | bool HasSecurityToken(int ClientId) const { return m_aSlots[ClientId].m_Connection.SecurityToken() != NET_SECURITY_TOKEN_UNSUPPORTED; } |
| 518 | NETADDR Address() const { return m_Address; } |
| 519 | NETSOCKET Socket() const { return m_Socket; } |
| 520 | CNetBan *NetBan() const { return m_pNetBan; } |
| 521 | int NetType() const { return net_socket_type(sock: m_Socket); } |
| 522 | int MaxClients() const { return m_MaxClients; } |
| 523 | |
| 524 | void SendTokenSixup(NETADDR &Addr, SECURITY_TOKEN Token); |
| 525 | |
| 526 | // |
| 527 | void SetMaxClientsPerIp(int Max); |
| 528 | bool HasErrored(int ClientId); |
| 529 | void ResumeOldConnection(int ClientId, int OrigId); |
| 530 | void IgnoreTimeouts(int ClientId); |
| 531 | |
| 532 | void ResetErrorString(int ClientId); |
| 533 | const char *ErrorString(int ClientId); |
| 534 | |
| 535 | // anti spoof |
| 536 | SECURITY_TOKEN GetGlobalToken(); |
| 537 | SECURITY_TOKEN GetToken(const NETADDR &Addr); |
| 538 | SECURITY_TOKEN GetVanillaToken(const NETADDR &Addr); |
| 539 | }; |
| 540 | |
| 541 | class CNetConsole |
| 542 | { |
| 543 | struct CSlot |
| 544 | { |
| 545 | CConsoleNetConnection m_Connection; |
| 546 | }; |
| 547 | |
| 548 | NETSOCKET m_Socket; |
| 549 | CNetBan *m_pNetBan; |
| 550 | CSlot m_aSlots[NET_MAX_CONSOLE_CLIENTS]; |
| 551 | |
| 552 | NETFUNC_NEWCLIENT_CON m_pfnNewClient; |
| 553 | NETFUNC_DELCLIENT m_pfnDelClient; |
| 554 | void *m_pUser; |
| 555 | |
| 556 | public: |
| 557 | void SetCallbacks(NETFUNC_NEWCLIENT_CON pfnNewClient, NETFUNC_DELCLIENT pfnDelClient, void *pUser); |
| 558 | |
| 559 | // |
| 560 | bool Open(NETADDR BindAddr, CNetBan *pNetBan); |
| 561 | void Close(); |
| 562 | |
| 563 | // |
| 564 | int Recv(char *pLine, int MaxLength, int *pClientId = nullptr); |
| 565 | int Send(int ClientId, const char *pLine); |
| 566 | void Update(); |
| 567 | |
| 568 | // |
| 569 | int AcceptClient(NETSOCKET Socket, const NETADDR *pAddr); |
| 570 | void Drop(int ClientId, const char *pReason); |
| 571 | |
| 572 | // status requests |
| 573 | const NETADDR *ClientAddr(int ClientId) const { return m_aSlots[ClientId].m_Connection.PeerAddress(); } |
| 574 | const std::array<char, NETADDR_MAXSTRSIZE> &ClientAddrString(int ClientId) const { return m_aSlots[ClientId].m_Connection.PeerAddressString(); } |
| 575 | CNetBan *NetBan() const { return m_pNetBan; } |
| 576 | }; |
| 577 | |
| 578 | class CNetTokenCache |
| 579 | { |
| 580 | public: |
| 581 | void Init(NETSOCKET Socket); |
| 582 | void SendPacketConnless(CNetChunk *pChunk); |
| 583 | void FetchToken(NETADDR *pAddr); |
| 584 | void AddToken(const NETADDR *pAddr, TOKEN Token); |
| 585 | TOKEN GetToken(const NETADDR *pAddr); |
| 586 | TOKEN GenerateToken(); |
| 587 | void Update(); |
| 588 | |
| 589 | private: |
| 590 | class CConnlessPacketInfo |
| 591 | { |
| 592 | public: |
| 593 | NETADDR m_Addr; |
| 594 | int m_DataSize; |
| 595 | unsigned char m_aData[NET_MAX_CONNLESS_PAYLOAD]; |
| 596 | int64_t m_Expiry; |
| 597 | }; |
| 598 | |
| 599 | class CAddressInfo |
| 600 | { |
| 601 | public: |
| 602 | NETADDR m_Addr; |
| 603 | TOKEN m_Token; |
| 604 | int64_t m_Expiry; |
| 605 | }; |
| 606 | |
| 607 | NETSOCKET m_Socket; |
| 608 | std::vector<CAddressInfo> m_TokenCache; |
| 609 | std::vector<CConnlessPacketInfo> m_ConnlessPackets; |
| 610 | }; |
| 611 | |
| 612 | // client side |
| 613 | class CNetClient |
| 614 | { |
| 615 | CNetConnection m_Connection; |
| 616 | CPacketChunkUnpacker m_PacketChunkUnpacker; |
| 617 | CNetPacketConstruct m_RecvBuffer; |
| 618 | CNetTokenCache m_TokenCache; |
| 619 | |
| 620 | CStun *m_pStun = nullptr; |
| 621 | |
| 622 | public: |
| 623 | NETSOCKET m_Socket = nullptr; |
| 624 | // openness |
| 625 | bool Open(NETADDR BindAddr); |
| 626 | void Close(); |
| 627 | |
| 628 | // connection state |
| 629 | void Disconnect(const char *pReason); |
| 630 | void Connect(const NETADDR *pAddr, int NumAddrs); |
| 631 | void Connect7(const NETADDR *pAddr, int NumAddrs); |
| 632 | |
| 633 | // communication |
| 634 | int Recv(CNetChunk *pChunk, SECURITY_TOKEN *pResponseToken, bool Sixup); |
| 635 | int Send(CNetChunk *pChunk); |
| 636 | |
| 637 | // pumping |
| 638 | void Update(); |
| 639 | int Flush(); |
| 640 | |
| 641 | void ResetErrorString(); |
| 642 | |
| 643 | // error and state |
| 644 | int NetType() const { return net_socket_type(sock: m_Socket); } |
| 645 | bool SocketIsBroken() const { return m_Socket != nullptr && net_udp_is_broken(sock: m_Socket); } |
| 646 | int State(); |
| 647 | const NETADDR *ServerAddress() const { return m_Connection.PeerAddress(); } |
| 648 | void ConnectAddresses(const NETADDR **ppAddrs, int *pNumAddrs) const { m_Connection.ConnectAddresses(ppAddrs, pNumAddrs); } |
| 649 | bool GotProblems(int64_t MaxLatency) const; |
| 650 | const char *ErrorString() const; |
| 651 | |
| 652 | // stun |
| 653 | void FeedStunServer(NETADDR StunServer); |
| 654 | void RefreshStun(); |
| 655 | CONNECTIVITY GetConnectivity(int NetType, NETADDR *pGlobalAddr); |
| 656 | }; |
| 657 | |
| 658 | // TODO: both, fix these. This feels like a junk class for stuff that doesn't fit anywhere |
| 659 | class CNetBase |
| 660 | { |
| 661 | static IOHANDLE ms_DataLogSent; |
| 662 | static IOHANDLE ms_DataLogRecv; |
| 663 | static CHuffman ms_Huffman; |
| 664 | |
| 665 | public: |
| 666 | static void OpenLog(IOHANDLE DataLogSent, IOHANDLE DataLogRecv); |
| 667 | static void CloseLog(); |
| 668 | static void Init(); |
| 669 | static int Compress(const void *pData, int DataSize, void *pOutput, int OutputSize); |
| 670 | static int Decompress(const void *pData, int DataSize, void *pOutput, int OutputSize); |
| 671 | |
| 672 | static bool IsValidConnectionOrientedPacket(const CNetPacketConstruct *pPacket); |
| 673 | |
| 674 | static void SendControlMsg(NETSOCKET Socket, NETADDR *pAddr, int Ack, int ControlMsg, const void *, int , SECURITY_TOKEN SecurityToken, bool Sixup = false); |
| 675 | static void SendControlMsgWithToken7(NETSOCKET Socket, NETADDR *pAddr, TOKEN Token, int Ack, int ControlMsg, TOKEN MyToken, bool Extended); |
| 676 | static void SendPacketConnless(NETSOCKET Socket, NETADDR *pAddr, const void *pData, int DataSize, bool Extended, unsigned char [NET_CONNLESS_EXTRA_SIZE]); |
| 677 | static void SendPacketConnlessWithToken7(NETSOCKET Socket, NETADDR *pAddr, const void *pData, int DataSize, SECURITY_TOKEN Token, SECURITY_TOKEN ResponseToken); |
| 678 | static void SendPacket(NETSOCKET Socket, NETADDR *pAddr, CNetPacketConstruct *pPacket, SECURITY_TOKEN SecurityToken, bool Sixup = false); |
| 679 | |
| 680 | static std::optional<int> UnpackPacketFlags(unsigned char *pBuffer, int Size); |
| 681 | // `AllowDecompression` false rejects compressed packets instead of decompressing them, |
| 682 | // decompression being the most expensive part of receiving a packet. `pDecompressed` is |
| 683 | // set when decompression was attempted, successfully or not. |
| 684 | static int UnpackPacket(unsigned char *pBuffer, int Size, CNetPacketConstruct *pPacket, bool &Sixup, bool AllowDecompression, SECURITY_TOKEN *pSecurityToken = nullptr, SECURITY_TOKEN *pResponseToken = nullptr, bool *pDecompressed = nullptr); |
| 685 | |
| 686 | // The backroom is ack-NET_MAX_SEQUENCE/2. Used for knowing if we acked a packet or not |
| 687 | static bool IsSeqInBackroom(int Seq, int Ack); |
| 688 | }; |
| 689 | |
| 690 | #endif |
| 691 | |