summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/client.cpp49
-rw-r--r--server/client.hpp2
-rw-r--r--server/server.cpp20
-rw-r--r--server/server.hpp3
-rw-r--r--server/server.vcxproj3
-rw-r--r--server/server_windows.cpp2
6 files changed, 65 insertions, 14 deletions
diff --git a/server/client.cpp b/server/client.cpp
index 37f20a2..8039e65 100644
--- a/server/client.cpp
+++ b/server/client.cpp
@@ -2,7 +2,7 @@
std::vector< byte > server::c_client::receive_message( ) {
std::vector< uint8_t > ret;
- char buffer[ BUFFER_SIZE ];
+ char buffer[ BUFFER_SIZE ]{ };
int received = 0;
while( true ) {
@@ -27,28 +27,59 @@ std::vector< byte > server::c_client::receive_message( ) {
}
bool server::c_client::send_message( byte* msg, size_t length ) {
- auto new_buffer = ( uint8_t* )( malloc( length + 1 ) );
+ auto buffer = std::make_unique< uint8_t[ ] >( length + 1 );
auto key = util::random_number( 0, 255 ) & 0xff;
- new_buffer[ 0 ] = key;
- memcpy( new_buffer + 1,
+ buffer[ 0 ] = key;
+ memcpy( buffer.get( ) + 1,
msg,
length );
- for( size_t i = 1; i < length + 1; ++i ) {
- new_buffer[ i ] ^= key;
+ for( size_t i = 1; i <= length; ++i ) {
+ buffer[ i ] ^= key;
}
- int result = send( m_socket, ( char* )new_buffer, length + 1, 0 );
+ int result = send( m_socket, ( char* )buffer.get( ), length + 1, 0 );
if( result == -1 ) {
+#if WIN32
printf( "error sending message to %s: %d\n",
get_ip( ), WSAGetLastError( ) );
+#else
+ printf( "error sending message to %s\n",
+ get_ip( ) );
+#endif
+ return false;
+ }
+
+ return true;
+}
+
+bool server::c_client::send_message( const char* msg ) {
+ auto length = strlen( msg );
+ auto buffer = std::make_unique< uint8_t[ ] >( length + 1 );
+ auto key = util::random_number( 0, 255 ) & 0xff;
- free( new_buffer );
+ buffer[ 0 ] = key;
+ memcpy( buffer.get( ) + 1,
+ msg,
+ length );
+
+ for ( size_t i = 1; i <= length; ++i ) {
+ buffer[ i ] ^= key;
+ }
+
+ int result = send( m_socket, ( char* )buffer.get( ), length + 1, 0 );
+ if ( result == -1 ) {
+#if WIN32
+ printf( "error sending message to %s: %d\n",
+ get_ip( ), WSAGetLastError( ) );
+#else
+ printf( "error sending message to %s\n",
+ get_ip( ) );
+#endif
return false;
}
- free( new_buffer );
return true;
}
diff --git a/server/client.hpp b/server/client.hpp
index a3dcd78..40ba1a5 100644
--- a/server/client.hpp
+++ b/server/client.hpp
@@ -16,6 +16,7 @@
#include <cstdlib>
#include <string>
#include <fstream>
+#include <memory>
#include "util.hpp"
@@ -50,6 +51,7 @@ namespace server
std::vector< byte > receive_message( );
bool send_message( byte* msg, size_t length );
+ bool send_message( const char* );
//handles messages, hwid etc
void handle_buffer( byte* msg );
diff --git a/server/server.cpp b/server/server.cpp
index 805239c..2c46489 100644
--- a/server/server.cpp
+++ b/server/server.cpp
@@ -1,8 +1,10 @@
#include "server.hpp"
int server::c_server::init( ) {
- if( WSAStartup( MAKEWORD( 2, 2 ), &m_sock_data ) )// windows
+#if WIN32
+ if( WSAStartup( MAKEWORD( 2, 2 ), &m_sock_data ) ) // windows
return 1;
+#endif
m_socket = socket( AF_INET, SOCK_STREAM, 0 );
if( m_socket == INVALID_SOCKET )
@@ -21,21 +23,31 @@ void server::c_server::listen( ) {
::listen( m_socket, 5 );
sockaddr_in client_address{ };
- client_address.sin_port = htons( PORT_NUM );
+
+ // not needed, this is set in accept, values are input.
+ // client_address.sin_port = htons( PORT_NUM );
int len = sizeof( client_address );
SOCKET result = accept( m_socket, ( sockaddr* )( &client_address ), &len );
if( result == INVALID_SOCKET ) {
printf( "socket error accepting a connection\n" );
- return;
+ return;
}
printf( "incoming connection from: %s\n", inet_ntoa( client_address.sin_addr ) );
- m_clients.push_back( std::make_shared< c_client >( result, client_address.sin_addr ) );
+
+ // connection established.
+ std::lock_guard lock( m_mutex );
+ auto client = std::make_shared< c_client >( result, client_address.sin_addr );
+
+ client->send_message( "hello" );
+
+ m_clients.push_back( client );
}
void server::c_server::client_loop( ) {
if( m_clients.size( ) ) {
+ std::lock_guard lock( m_mutex );
for( auto it = m_clients.begin( ); it != m_clients.end( ); ++it ) {
if( !( *it )->handle( ) ) {
printf( "%s disconnected\n", ( *it )->get_ip( ) );
diff --git a/server/server.hpp b/server/server.hpp
index 8bd017e..1770a05 100644
--- a/server/server.hpp
+++ b/server/server.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <vector>
#include <memory>
+#include <mutex>
#include "client.hpp"
@@ -11,6 +12,8 @@
namespace server
{
class c_server {
+ std::mutex m_mutex;
+ // vectors are NOT thread safe.
std::vector< std::shared_ptr< c_client > > m_clients;
WSADATA m_sock_data{ };
diff --git a/server/server.vcxproj b/server/server.vcxproj
index e37c277..15d8173 100644
--- a/server/server.vcxproj
+++ b/server/server.vcxproj
@@ -31,7 +31,7 @@
<ProjectGuid>{F0038E32-6DE8-47B7-BC86-8A2274B24406}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>server</RootNamespace>
- <WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@@ -46,6 +46,7 @@
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
+ <SpectreMitigation>false</SpectreMitigation>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='pHit|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
diff --git a/server/server_windows.cpp b/server/server_windows.cpp
index a593d1b..bbe0b73 100644
--- a/server/server_windows.cpp
+++ b/server/server_windows.cpp
@@ -24,11 +24,13 @@ int main( ) {
int result = g_server.init( );
if( !result ) {
+ // thread unsafe.
listen_thread = std::thread( [ ]( ) { while( 1 ) { g_server.listen( ); } } );
listen_thread.detach( );
while( 1 ) {
g_server.client_loop( );
+ Sleep( 1 );
}
}
else