int ProfileMain() { srand( (unsigned int) time( NULL ) ); GamePacketFactory packetFactory; GameMessageFactory messageFactory; ServerData * serverData = new ServerData(); serverData->address = Address( "::1", ServerPort ); ClientData * clientData = new ClientData[MaxClients]; LocalMatcher matcher; for ( int i = 0; i < MaxClients; ++i ) { clientData[i].address = Address( "::1", ClientPort + i ); clientData[i].clientId = 1 + i; if ( !matcher.RequestMatch( clientData[i].clientId, clientData[i].connectTokenData, clientData[i].connectTokenNonce, clientData[i].clientToServerKey, clientData[i].serverToClientKey, clientData[i].numServerAddresses, clientData[i].serverAddresses ) ) { printf( "error: client %d request match failed\n", i ); delete serverData; delete [] clientData; return 1; } } serverData->transport = new SocketTransport( GetDefaultAllocator(), packetFactory, serverData->address, ProtocolId ); for ( int i = 0; i < MaxClients; ++i ) clientData[i].transport = new SocketTransport( GetDefaultAllocator(), packetFactory, clientData[i].address, ProtocolId ); serverData->server = new GameServer( GetDefaultAllocator(), *serverData->transport, messageFactory ); for ( int i = 0; i < MaxClients; ++i ) clientData[i].client = new GameClient( GetDefaultAllocator(), *clientData[i].transport, messageFactory ); serverData->server->SetServerAddress( serverData->address ); serverData->server->Start(); for ( int i = 0; i < MaxClients; ++i ) { clientData[i].client->Connect( serverData->address, clientData[i].connectTokenData, clientData[i].connectTokenNonce, clientData[i].clientToServerKey, clientData[i].serverToClientKey ); } signal( SIGINT, interrupt_handler ); double time = 0.0; while ( !quit ) { serverData->server->SendPackets(); for ( int i = 0; i < MaxClients; ++i ) clientData[i].client->SendPackets(); serverData->transport->WritePackets(); for ( int i = 0; i < MaxClients; ++i ) clientData[i].transport->WritePackets(); serverData->transport->ReadPackets(); for ( int i = 0; i < MaxClients; ++i ) clientData[i].transport->ReadPackets(); serverData->server->ReceivePackets(); for ( int i = 0; i < MaxClients; ++i ) clientData[i].client->ReceivePackets(); for ( int i = 0; i < MaxClients; ++i ) { if ( clientData[i].client->ConnectionFailed() ) { printf( "\nerror: client %d connect failed!", i ); quit = true; break; } } time += 0.1; for ( int i = 0; i < MaxClients; ++i ) { const int messagesToSend = random_int( 0, 64 ); for ( int j = 0; j < messagesToSend; ++j ) { if ( !clientData[i].client->CanSendMessage() ) break; Message * message = CreateRandomMessage( messageFactory, clientData[i].numMessagesSent ); if ( message ) { clientData[i].client->SendMessage( message ); clientData[i].numMessagesSent++; } } } for ( int i = 0; i < MaxClients; ++i ) { const int messagesToSend = random_int( 0, 64 ); for ( int j = 0; j < messagesToSend; ++j ) { if ( !serverData->server->CanSendMessage( i ) ) break; Message * message = CreateRandomMessage( messageFactory, serverData->numMessagesSent[i] ); if ( message ) { serverData->server->SendMessage( i, message ); serverData->numMessagesSent[i]++; } } } for ( int i = 0; i < MaxClients; ++i ) { while ( true ) { Message * message = serverData->server->ReceiveMessage( i ); if ( !message ) break; serverData->numMessagesReceived[i]++; // printf( "server received message %d from client %d\n", message->GetId(), i ); messageFactory.Release( message ); } } for ( int i = 0; i < MaxClients; ++i ) { while ( true ) { Message * message = clientData[i].client->ReceiveMessage(); if ( !message ) break; clientData[i].numMessagesReceived++; // printf( "client received message %d from client %d\n", message->GetId(), i ); messageFactory.Release( message ); } } serverData->server->CheckForTimeOut(); for ( int i = 0; i < MaxClients; ++i ) clientData[i].client->CheckForTimeOut(); serverData->server->AdvanceTime( time ); for ( int i = 0; i < MaxClients; ++i ) clientData[i].client->AdvanceTime( time ); serverData->transport->AdvanceTime( time ); for ( int i = 0; i < MaxClients; ++i ) clientData[i].transport->AdvanceTime( time ); platform_sleep( 0.1 ); } if ( quit ) { printf( "\n\nstopped\n" ); } delete [] clientData; delete serverData; return 0; }
ClientConf *DoAuthenticateServer(int net_fd, struct sockaddr_in *from) { ClientConf *Client = NULL; int dcr_size = 0, cr_size = 0, i = 0; char ConnectionID[MED_BUF]; unsigned char cr_buffer[BUFSIZ], dcr_buffer[BUFSIZ], orig_buffer[BUFSIZ], signature[MED_BUF]; unsigned int siglen = 0; memset(dcr_buffer, 0, sizeof(dcr_buffer)); memset(orig_buffer, 0, sizeof(orig_buffer)); /* Get the connection ID */ if ((i = ReadN(net_fd, (struct sockaddr *)from, (unsigned char *)ConnectionID, MED_BUF)) <= 0) { fprintf(stderr, "DoAuthenticateServer : ReadN\n"); return NULL; } ConnectionID[i] = '\0'; /* Load the client setup */ if ((Client = ReadClientConf(SrvSetup.cfgfile, ConnectionID)) == NULL) { fprintf(stderr, "DoAuthenticateServer : ReadClientConf\n"); return NULL; } if (LoadPublicKeyFromFile(Client->fpubkey, &(Client->pub), CLIENT_LOAD_SERVER_PUBLIC_KEYFILE_ERR)) { fprintf(stderr, "DoAuthenticateServer : LoadPublicKeyFromFile\n"); return NULL; } /* Create a random message, sign it, encrypt it and send it with its signature */ if (CreateRandomMessage(orig_buffer, MIN_BUF) < 0) { fprintf(stderr, "DoAuthenticateServer : CreateRandomMessage\n"); return NULL; } if (SignMessage(SrvSetup.priv, orig_buffer, MIN_BUF, signature, &siglen) < 0) { fprintf(stderr, "DoAuthenticateServer : SignMesage\n"); return NULL; } if (EncryptMessageWithPublicKey(Client->pub, orig_buffer, MIN_BUF, cr_buffer, &cr_size) < 0) { fprintf(stderr, "DoAuthenticateServer : EncryptMessageWithPublicKey\n"); return NULL; } if (WriteH(net_fd, (struct sockaddr *)from, cr_buffer, cr_size) < 0) { fprintf(stderr, "DoAuthenticateServer : WriteH\n"); return NULL; } if (WriteH(net_fd, (struct sockaddr *)from, signature, siglen) <= 0) { fprintf(stderr, "DoAuthenticateServer : WriteH\n"); return NULL; } /* Read the answer and decrypt it */ if ((cr_size = ReadN(net_fd, (struct sockaddr *)from, cr_buffer, BUFSIZ)) < 0) { fprintf(stderr, "DoAuthenticateServer : ReadN\n"); return NULL; } else if (cr_size == 0) { fprintf(stderr, "Authentication failed. Access denied!\n"); fprintf(stderr, "Disconnecting client\n"); return NULL; } if (DecryptMessageWithPrivateKey(SrvSetup.priv, cr_buffer, cr_size, dcr_buffer, &dcr_size)) { fprintf(stderr, "DoAuthenticateServer : DecryptMessageWithPrivateKey\n"); return NULL; } /* Check if the message is ok */ if (memcmp(orig_buffer, dcr_buffer, MIN_BUF)) { fprintf(stderr, "Access denied!\n"); return NULL; } /* Save the origin information in the client's structure */ memcpy(&(Client->ns.data), from, sizeof(struct sockaddr_in)); inet_aton(Client->ns.ip, &Client->ns.data.sin_addr); /* Create a key, arrange a network setup for the client and send it over */ if (CreateRandomKey(Client->PrivKey, KEYSIZE)) { fprintf(stderr, "DoAuthenticateServer : CreateRandomKey\n"); return NULL; } /*sprintf((char *)dcr_buffer, "key=%s,ip=%s,netmask=%s,broadcast=%s,mtu=%s", Client->PrivKey, Client->ns.ip, Client->ns.msk, Client->ns.brd, Client->ns.mtu);*/ memset(dcr_buffer, '\0', sizeof(dcr_buffer)); memcpy(dcr_buffer, "key=", 4); memcpy(dcr_buffer + 4, Client->PrivKey, KEYSIZE); sprintf((char *)dcr_buffer + 4 + KEYSIZE, ",ip=%s,netmask=%s,broadcast=%s", Client->ns.ip, Client->ns.msk, Client->ns.brd); dcr_size = 4 + KEYSIZE + strlen(((char *)dcr_buffer) + 4 + KEYSIZE); if (EncryptMessageWithPublicKey(Client->pub, dcr_buffer, dcr_size, cr_buffer, &cr_size) < 0) { fprintf(stderr, "DoAuthenticateServer : EncryptMessageWithPublicKey\n"); return NULL; } if (WriteH(net_fd, (struct sockaddr *)from, cr_buffer, cr_size) < 0) { fprintf(stderr, "DoAuthenticateServer : WriteH\n"); return NULL; } CryptoInit(&(Client->ctx), Client->PrivKey, &(Client->IV)); return Client; }