#pragma once //User configured structures //Change this structure to suit your storage of variables for keeping the encryption state intact struct ENCRYPTION_STATE { unsigned long offset1; unsigned long offset2; unsigned char key[264]; }; //DO NOT CHANGE BELOW THIS LINE /* Handles all network communications. Should not be touched unless... 1. you know what you are doing 2. There is a bug within the code 3. You know for sure the bug is within here */ #pragma warning( disable : 4996) #define LAN10 (unsigned char)((0x08 + 0x02)) #define LAN172 (unsigned char)((160+12)) #define LAN16 (unsigned char)((14+2)) #define LAN31 (unsigned char)((28+3)) #define LAN192 (unsigned char)((100+92)) #define LAN168 (unsigned char)((100+68)) #define LAN169 (unsigned char)((100+69)) #define LAN254 (unsigned char)((200+54)) //Standard includes and other non-OS specific includes #include #include #include #include #include "NetMessage.h" #include "IDManager.h" using namespace stdext; using namespace std; //Windows includes #ifdef WIN32 #pragma comment(lib, "wsock32.lib") #include #define MY_SOCKET 0 #define SOCKET_ERROR_N SOCKET_ERROR // we are compiling for linux #else #include #include #include #include #include #include #include #include #include #define MY_SOCKET clients[i].client+1 #define SOCKET int #define SOCKET_ERROR NULL #define INVALID_SOCKET -1 #define SOCKET_ERROR_N -1 #define closesocket(S) close(S) #endif //structure for each client struct SOCKETINFO { SOCKET client; //Socket number of the client, used by sockets bool inUse; //The current socket is in use. Why is this here? short id; //The unique networkId assigned to this client unsigned char ip[5];//Contains the IP address the client is connecting to the server from. ENCRYPTION_STATE eState; //Constructor and member functions SOCKETINFO(){useEncryption=false;inUse=false;id=0;} void clear(){inUse=false;id=0;} bool useEncryption; bool haltSend; }; //configuration of the server struct SERVERCONFIG { int maxConnections;//The maximum number of clients that can be connected at once bool notifyOnConnect; bool preventBruteForce; //If true, automatic IP bans will be issued for any IP that exceeds the connections per minute threshold unsigned long maxConPerMinute; SERVERCONFIG() { maxConPerMinute=200; maxConnections=10; notifyOnConnect=false; preventBruteForce=false; } }; //IP ban structure struct _IPBAN { unsigned char start[4]; unsigned char end[4]; time_t addedOn; unsigned long duration;//0 = infinite ban, >0 = duration of ban in seconds char reason[100]; }; //Message structure for timed events struct EVENT_MESSAGE { unsigned long id; unsigned char *pData; unsigned long dataSize; unsigned long extra1; unsigned long extra2; unsigned char flags; unsigned long unused; }; //Report structure struct NET_REPORT { unsigned long numErrors; unsigned long numWarnings; unsigned long bytesPerSecIn; unsigned long bytesPerSecOut; unsigned long avgBytesPerSecIn; unsigned long avgBytesPerSecOut; unsigned long peakAvgBytesPerSecOut; unsigned long peakAvgBytesPerSecIn; unsigned long packetsPerSecIn; unsigned long packetsPerSecOut; unsigned long peakBytesPerSecIn; unsigned long peakBytesPerSecOut; //These are used for calculations unsigned long timer; unsigned long inByteCounter; unsigned long outByteCounter; unsigned long inNumPackets; unsigned long outNumPackets; }; //Feel free to change these to the above. Just make sure the value gets set. #define BIT_SET(val, bit) (val=val | bit) #define BIT_CLR(val, bit) (val=val & (~bit)) #define BIT_TEST(val, bit) ((val & bit)==bit) //The network server class class Server { public: //Constructors Server(); Server(int port, char* pName, unsigned char* pIp, bool isClient, void* pMyLog);//ip is 4 bytes if 0 the ip will be assigned automatically ~Server(); //Initializes the server. Is also required when using client mode int Init(int port, char* pName, unsigned char* pIp, bool isClient, void* pMyLog); int Shutdown(bool isLastServer); void GetIp(char* pIpDest); void GetIpString(char* pBuffer); NET_REPORT* GetReport(); char* GetName(); //Processes the queues...should be called each cycle of the main program loop void Update(); //If the server is running as a client, connect to a server specified by ipb int Connect(unsigned char* pIpBytes); //Is used to update the current server configuration int UpdateServerConfig(SERVERCONFIG sc); //Functions that deal directory with network messages int GetNextNetMessage(NetMessage* pData);//Return the next message on the incoming queue int ForwardData(NetMessage* pData);//Crondor? int SendData(NetMessage* pData);//Add data to the outgoing queue int SendData(NetMessage* pData, int timeWait);//Add data to the outgoing queue but don't send until the timeWait is over (maybe replace this with a timed event thing) int SendControl(NetMessage* pData);//DEPRECATED Please don't use! int AddIncoming(NetMessage* pData);//Adds a net message to the incoming queue int AddIncomingFront(NetMessage* pData);//Adds a NetMessage to the front of the incoming queue //Only useful if the server is running in client mode. i.e. it is not a server int GetClientModeId(); //Client functions bool ClientExists(short id); SOCKETINFO GetClientById(short id); SOCKETINFO* GetClientByPid(short id); void Kick(short networkId); void GetNumClients(int* pNumConnections); void SetEncryptionState(short id, ENCRYPTION_STATE* pEncryptionState); //IP Ban related functions void IssueBan(unsigned char* pIprange, unsigned long durationSeconds, char* pReason); void IssueBan(short id, unsigned long durationSeconds, char* pReason); bool IsBanned(char* pIp); //If true, the server has suffered from an irrecoverable error bool mFatalError; //NO TOUCHY! int Listen();//DO NOT CALL bool IsLan(short id); int (*Crypto)(unsigned char* pSource, unsigned char* pDest, int length, ENCRYPTION_STATE* pEncryptionState); private: short NewId(); //Generates a new unique ID for the client int KillClient(short id); //Terminates a connection to a client int Bind(); //Binds the server to a port int CheckRecvError(int err, int i); //Checks for any errors when calling recv() int ProcessControlMessage(NetMessage d);//Processes control messages bool ProcessEventMessage(EVENT_MESSAGE* pEncryptionState);//Processes event messages hash_map mClients; //Contains the client structure of every client connected hash_map mBans; //Bans list. list mIncoming; //incoming messages. Messages that are normally generated when data is received list mOutgoing; //Messages that are sending data list mControlMessages; //Messages that instruct the server to do tasks list mEventQueue; //Timed events that instruct the server to do tasks IDManager mNetIdMan; //Pointer to logging class void* mpMyLog; struct sockaddr_in my_address; //Don't touchy SOCKET mMySocket; //Don't touchy void* pListenThread; //Thread HANDLE used for the thread that listens =D SERVERCONFIG mConfig; //Current server configuration NET_REPORT mReport; short mPort; //Current port char mName[50]; //The name of the server, used for the ban file name and log reports int mSrvFullSpamStop; //Prevents spamming of log files when the server is full int mTotalIncomingAtEnd; //I forget. Prevents packets added to the incoming list from being processed during the phase bool mIsClient; //Is this instance of NetServer running in client mode. I.e. is not a server. bool mShutdownOK; //Must be true to shutdown otherwise an error message is displayed };