Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
8.4 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // net.h -- Half-Life's interface to the networking layer
  9. // For banning IP addresses (or allowing private games)
  10. #ifndef NET_H
  11. #define NET_H
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15. #include "common.h"
  16. #include "bitbuf.h"
  17. #include "netadr.h"
  18. #include "inetchannel.h"
  19. #include "networksystem/inetworksystem.h"
  20. class ISteamDatagramTransportClient;
  21. // Flow control bytes per second limits:
  22. // 16,000 bytes per second = 128kbps
  23. // default rate: 192kbytes per second = 1.5mbps
  24. // 0.75Mbytes per second = 6mbps
  25. #define MIN_RATE 16000
  26. #define DEFAULT_RATE 196608
  27. #define MAX_RATE 786432
  28. #ifdef _GAMECONSOLE
  29. #define SIGNON_TIME_OUT 75.0f // signon disconnect timeout
  30. #else
  31. #define SIGNON_TIME_OUT 300.0f // signon disconnect timeout
  32. #endif
  33. #define SIGNON_TIME_OUT_360 75.0f
  34. #define FRAGMENT_BITS 8
  35. #define FRAGMENT_SIZE (1<<FRAGMENT_BITS)
  36. #define MAX_FILE_SIZE_BITS 26
  37. #define MAX_FILE_SIZE ((1<<MAX_FILE_SIZE_BITS)-1) // maximum transferable size is 64MB
  38. // 0 == regular, 1 == file stream
  39. #define MAX_STREAMS 2
  40. #define FRAG_NORMAL_STREAM 0
  41. #define FRAG_FILE_STREAM 1
  42. #define TCP_CONNECT_TIMEOUT 4.0f
  43. #define PORT_ANY -1
  44. #define PORT_TRY_MAX 10 // the number of different ports to try to find an unused one
  45. #define PORT_TRY_MAX_FORKED 150 // the number to try when we are running as a forked child process (for the server farm)
  46. #define TCP_MAX_ACCEPTS 8
  47. #define LOOPBACK_SOCKETS 2
  48. #define STREAM_CMD_NONE 0 // waiting for next blob
  49. #define STREAM_CMD_AUTH 1 // first command, send back challengenr
  50. #define STREAM_CMD_DATA 2 // receiving a data blob
  51. #define STREAM_CMD_FILE 3 // receiving a file blob
  52. #define STREAM_CMD_ACKN 4 // acknowledged a recveived blob
  53. // NETWORKING INFO
  54. class INetChannel;
  55. enum ESocketIndex_t
  56. {
  57. NS_INVALID = -1,
  58. NS_CLIENT = 0, // client socket
  59. NS_SERVER, // server socket
  60. #ifdef _X360
  61. NS_X360_SYSTEMLINK,
  62. NS_X360_LOBBY,
  63. NS_X360_TEAMLINK,
  64. #endif
  65. NS_HLTV,
  66. NS_HLTV1, // Note: NS_HLTV1 must follow NS_HLTV, NS_HLTV2 must follow NS_HLTV1, etc.
  67. #if defined( REPLAY_ENABLED )
  68. NS_REPLAY,
  69. #endif
  70. MAX_SOCKETS
  71. };
  72. extern netadr_t net_local_adr;
  73. extern double net_time;
  74. class INetChannelHandler;
  75. class IConnectionlessPacketHandler;
  76. class CMsgSteamDatagramGameServerAuthTicket;
  77. class ISteamDatagramTransportClient;
  78. // Start up networking
  79. void NET_Init( bool bDedicated );
  80. // initialize queued packet sender. must do after fork(), not before
  81. void NET_InitPostFork( void );
  82. // Shut down networking
  83. void NET_Shutdown (void);
  84. // Read any incoming packets, dispatch to known netchannels and call handler for connectionless packets
  85. void NET_ProcessSocket( int sock, IConnectionlessPacketHandler * handler );
  86. // Set a port to listen mode
  87. void NET_ListenSocket( int sock, bool listen );
  88. // Send connectionsless string over the wire
  89. void NET_OutOfBandPrintf(int sock, const ns_address &adr, PRINTF_FORMAT_STRING const char *format, ...) FMTFUNCTION( 3, 4 );
  90. void NET_OutOfBandDelayedPrintf(int sock, const ns_address &adr, uint32 unMillisecondsDelay, PRINTF_FORMAT_STRING const char *format, ...) FMTFUNCTION( 4, 5 );
  91. // Send a raw packet, connectionless must be provided (chan can be NULL)
  92. int NET_SendPacket ( INetChannel *chan, int sock, const ns_address &to, const unsigned char *data, int length, bf_write *pVoicePayload = NULL, bool bUseCompression = false, uint32 unMillisecondsDelay = 0u );
  93. // Called periodically to maybe send any queued packets (up to 4 per frame)
  94. void NET_SendQueuedPackets();
  95. // Start set current network configuration
  96. void NET_SetMultiplayer(bool multiplayer);
  97. // Set net_time
  98. void NET_SetTime( double realtime );
  99. // RunFrame must be called each system frame before reading/sending on any socket
  100. void NET_RunFrame( double realtime );
  101. // Check configuration state
  102. bool NET_IsMultiplayer( void );
  103. bool NET_IsDedicated( void );
  104. #ifdef SERVER_XLSP
  105. bool NET_IsDedicatedForXbox( void );
  106. #else
  107. FORCEINLINE bool NET_IsDedicatedForXbox( void )
  108. {
  109. return false;
  110. }
  111. #endif
  112. // Writes a error file with bad packet content
  113. void NET_LogBadPacket(netpacket_t * packet);
  114. // bForceNew (used for bots) tells it not to share INetChannels (bots will crash when disconnecting if they
  115. // share an INetChannel).
  116. INetChannel *NET_CreateNetChannel( int socketnumber, const ns_address *adr, const char * name, INetChannelHandler * handler, const byte *pbEncryptionKey, bool bForceNew );
  117. void NET_RemoveNetChannel(INetChannel *netchan, bool bDeleteNetChan);
  118. void NET_PrintChannelStatus( INetChannel * chan );
  119. void NET_WriteStringCmd( const char * cmd, bf_write *buf );
  120. // Address conversion
  121. bool NET_StringToAdr ( const char *s, netadr_t *a);
  122. // Convert from host to network byte ordering
  123. unsigned short NET_HostToNetShort( unsigned short us_in );
  124. // and vice versa
  125. unsigned short NET_NetToHostShort( unsigned short us_in );
  126. // Find out what port is mapped to a local socket
  127. unsigned short NET_GetUDPPort(int socket);
  128. // add/remove extra sockets for testing
  129. int NET_AddExtraSocket( int port );
  130. void NET_RemoveAllExtraSockets();
  131. const char *NET_ErrorString (int code); // translate a socket error into a friendly string
  132. // Returns true if compression succeeded, false otherwise
  133. bool NET_BufferToBufferCompress( char *dest, unsigned int *destLen, char *source, unsigned int sourceLen );
  134. bool NET_BufferToBufferDecompress( char *dest, unsigned int *destLen, char *source, unsigned int sourceLen );
  135. netadr_t NET_InitiateSteamConnection(int sock, uint64 uSteamID, PRINTF_FORMAT_STRING const char *format, ...) FMTFUNCTION( 3, 4 );
  136. void NET_TerminateConnection(int sock, const ns_address &peer );
  137. void NET_TerminateSteamConnection(int sock, uint64 uSteamID );
  138. void NET_SleepUntilMessages( int nMilliseconds );
  139. // If net_public_adr convar is set then returns that, otherwise, checks with steam if we are a dedicated server (eventually will work for the client) and returns that
  140. // Returns false if not able to deduce address
  141. bool NET_GetPublicAdr( netadr_t &adr );
  142. /// Start listening for Steam datagram, if the convar tells us to
  143. void NET_SteamDatagramServerListen();
  144. /// Called when we receive a ticket to play on a particular gameserver
  145. #ifndef DEDICATED
  146. /// Make sure we are setup to talk to this gameserver
  147. bool NET_InitSteamDatagramProxiedGameserverConnection( const ns_address &adr );
  148. #endif
  149. //============================================================================
  150. //
  151. // Encrypted network channel communication support
  152. //
  153. #define NET_CRYPT_KEY_LENGTH 16
  154. bool NET_CryptVerifyServerCertificateAndAllocateSessionKey( bool bOfficial, const ns_address &from,
  155. const byte *pchKeyPub, int numKeyPub,
  156. const byte *pchKeySgn, int numKeySgn,
  157. byte **pbAllocatedKey, int *pnAllocatedCryptoBlockSize );
  158. bool NET_CryptVerifyClientSessionKey( bool bOfficial,
  159. const byte *pchKeyPri, int numKeyPri,
  160. const byte *pbEncryptedKey, int numEncryptedBytes,
  161. byte *pbPlainKey, int numPlainKeyBytes );
  162. enum ENetworkCertificate_t
  163. {
  164. k_ENetworkCertificate_PublicKey,
  165. k_ENetworkCertificate_PrivateKey,
  166. k_ENetworkCertificate_Signature,
  167. k_ENetworkCertificate_Max
  168. };
  169. bool NET_CryptGetNetworkCertificate( ENetworkCertificate_t eType, const byte **pbData, int *pnumBytes );
  170. //============================================================================
  171. // Message data
  172. typedef struct
  173. {
  174. // Size of message sent/received
  175. int size;
  176. // Time that message was sent/received
  177. float time;
  178. } flowstats_t;
  179. struct sockaddr;
  180. class ISteamSocketMgr
  181. {
  182. public:
  183. enum ESteamCnxType
  184. {
  185. ESCT_NEVER = 0,
  186. ESCT_ASBACKUP,
  187. ESCT_ALWAYS,
  188. ESCT_MAXTYPE,
  189. };
  190. enum
  191. {
  192. STEAM_CNX_PORT = 1,
  193. };
  194. virtual void Init() = 0;
  195. virtual void Shutdown() = 0;
  196. virtual ESteamCnxType GetCnxType() = 0;
  197. virtual void OpenSocket( int s, int nModule, int nSetPort, int nDefaultPort, const char *pName, int nProtocol, bool bTryAny ) = 0;
  198. virtual void CloseSocket( int s, int nModule ) = 0;
  199. virtual int sendto( int s, const char * buf, int len, int flags, const ns_address &to ) = 0;
  200. virtual int recvfrom( int s, char * buf, int len, int flags, ns_address * from ) = 0;
  201. virtual uint64 GetSteamIDForRemote( const ns_address &remote ) = 0;
  202. };
  203. extern ISteamSocketMgr *g_pSteamSocketMgr;
  204. // Some hackery to avoid using va() in constructor since we cache off the pointer to the string in the ConVar!!!
  205. #define NET_STRINGIZE( x ) #x
  206. #define NET_MAKESTRING( macro, val ) macro(val)
  207. #define NETSTRING( val ) NET_MAKESTRING( NET_STRINGIZE, val )
  208. #endif // !NET_H