From: DeaDDooMER Date: Wed, 5 Apr 2017 19:10:11 +0000 (+0300) Subject: Больше проверок корректности пакетов на стороне сервера X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=20f7a88083b2bbaca64d860c6ecbf9059a70100a;p=netwar.git Больше проверок корректности пакетов на стороне сервера --- diff --git a/netwar.c b/netwar.c index b7c2222..e18438e 100644 --- a/netwar.c +++ b/netwar.c @@ -117,8 +117,8 @@ static void paintwindow() { SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0xFF, 0x00); static float start_r; - for(int i = 0; i < STAR_SIZE * 200; i++) - paintmodel(star, sizeof(star), 0, 0, M_PI / i + start_r, STAR_SIZE, cx, cy); + for(int i = 0; i < STAR_SIZE * 150; i++) + paintmodel(star, sizeof(star), 0, 0, start_r + i * M_PI, STAR_SIZE, cx, cy); start_r += 0.0001; diff --git a/server.c b/server.c index e60ac26..38c6790 100644 --- a/server.c +++ b/server.c @@ -84,24 +84,29 @@ static void sv_move_player(int id, ProtocolMessage m) { g_player_does(id, m.cl.does.code); } -static void sv_register_player(IPaddress address, ProtocolMessage m) { - if(m.cl.info.version != PROTOCOL_VERSION) - SendMessage(sock, address, sv_kill("Uncompatible version")); +static void sv_kill_address(IPaddress address, const char * msg) { + SDL_Log("sv_kill_address: %s", msg); + SendMessage(sock, address, sv_kill(msg)); +} + +static void sv_register_player(IPaddress address, ClInfo info) { + if(info.version != PROTOCOL_VERSION) { + sv_kill_address(address, "Uncompatible version"); + return; + } int i = 0; while((i < clientlimit) && (client[i].connected)) ++i; - int namesize = sizeof(m.cl.info.name); if(i >= clientlimit) { - SDL_Log("Too may clints, player %.*s disconnected", namesize, m.cl.info.name); - SendMessage(sock, address, sv_kill("Too many clients")); + sv_kill_address(address, "Too many clients"); return; } client[i].connected = true; client[i].address = address; - strncpy(client[i].name, (char*) m.cl.info.name, MAX_NAMESIZE); + strncpy(client[i].name, (char*) info.name, MAX_NAMESIZE); client[i].name[MAX_NAMESIZE - 1] = 0; SDL_Log("Player %s (%i) connected", client[i].name, i); @@ -111,17 +116,24 @@ static void sv_register_player(IPaddress address, ProtocolMessage m) { sv_sync_client(i, true); } +static void sv_recv_ipmsg(IPaddress address, ProtocolMessage m) { + int id = sv_find_client(address); + if(id < 0 && m.type != CL_INFO) + return; + + switch(m.type) { + case CL_INFO: sv_register_player(address, m.cl.info); break; + case CL_KILL: sv_kill_player(id, false, ""); break; + case CL_DOES: sv_move_player(id, m); break; + default: sv_kill_address(address, "Invalid message"); + } +} + static void sv_recv() { IPaddress address; ProtocolMessage m; - while(RecvMessage(sock, &address, &m)) { - switch(m.type) { - case CL_INFO: sv_register_player(address, m); break; - case CL_KILL: sv_kill_player(sv_find_client(address), false, ""); break; - case CL_DOES: sv_move_player(sv_find_client(address), m); break; - default: SDL_Log("invalid message %i", m.type); - } - } + while(RecvMessage(sock, &address, &m)) + sv_recv_ipmsg(address, m); } static void sv_send() {