Port server to Linux and add Docker Compose deployment
Rebuilds (not Wine) the login/char/world servers natively for Linux via CMake, and ships a single Docker image plus docker-compose stack (MariaDB + 3 servers) with all settings configurable via .env. Linux portability: - Src/Common/compat.h shims MSVC-only CRT calls (fopen_s, _strdup, strncpy_s, sprintf_s, SYSTEMTIME/GetLocalTime, WSAGetLastError, __cdecl) and adds a case-insensitive/backslash-tolerant path resolver for the Windows-authored STB/AIP data file paths. - Fixed case-sensitive header includes and filenames (charserver.h vs CharServer.h, worldserver.h vs WorldServer.h, ../common vs ../Common). - rose_inet_addr() resolves hostnames (not just literal IPs) for the server-to-server links, so docker-compose service names work. Fixed three real bugs that only crashed on 64-bit Linux (silently tolerated on 32-bit Windows): - CIntStb::LoadSTB had an inverted fopen success/failure check, so STB game data (zones, items, etc.) was never actually loaded. - GenerateCryptTables allocated its pointer table with sizeof(unsigned) instead of sizeof(unsigned*) -- a 1x vs 2x-size heap overflow on 64-bit. - Crypto bit-twiddling relies throughout on signed-overflow-as-wraparound, which is UB in C++; GCC's -O3 exploited it and miscompiled GenerateCryptTables (Release-only crash). Fixed via -fwrapv. Plus smaller fixes: off-by-one array bounds (inventory/storage/sell data), SO_REUSEADDR on server sockets (containers restarting in the same netns hit TIME_WAIT otherwise), and a va_list reused across multiple vprintf/vfprintf calls in Log() that garbled every formatted log line on the Linux ABI. Docker: - CMakeLists.txt builds all three servers + shared Common code. - Dockerfile: multi-stage build, one runtime image for all three servers, server role selected via `command: [login|char|world]`. - docker-compose.yml: MariaDB (auto-imports database sql files/ on first run) + login/char/world (single channel), config rendered from docker/conf/*.conf.tmpl via envsubst at container start. - .env.example documents every setting (ports, server names, rates, inter-server passwords, PUBLIC_HOST for internet-facing deployments). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a320bbe470
commit
764fcb3b3f
37 files changed
+811
-72
No files matched your search
@@ -45,7 +45,6 @@ CServerSocket::~CServerSocket( )
|
||||
bool CServerSocket::StartServer( )
|
||||
{
|
||||
//struct sockaddr_in ain;
|
||||
int test;
|
||||
sock = socket( AF_INET, SOCK_STREAM, 0 );
|
||||
if (sock == INVALID_SOCKET)
|
||||
{
|
||||
@@ -59,16 +58,16 @@ bool CServerSocket::StartServer( )
|
||||
Log(MSG_ERROR, "setsockopt:SO_KEEPALIVE" );
|
||||
}
|
||||
setsockopt(sock, IPPROTO_TCP,TCP_NODELAY,(const char*)&optval,sizeof(optval));
|
||||
// Lets a restarted server (docker restart, crash-loop) rebind immediately
|
||||
// instead of failing on a TIME_WAIT socket from its own previous run.
|
||||
setsockopt(sock, SOL_SOCKET,SO_REUSEADDR,(const char*)&optval,sizeof(optval));
|
||||
ain.sin_family = AF_INET;
|
||||
ain.sin_addr.s_addr = INADDR_ANY;
|
||||
ain.sin_port = htons( port );
|
||||
memset(&(ain.sin_zero), '\0', 8);
|
||||
if ( bind( sock, (const sockaddr*)&ain, sizeof( struct sockaddr ) ) )
|
||||
{
|
||||
|
||||
Log( MSG_FATALERROR, "Could not bind socket" );
|
||||
std::cout <<"socket " << sock << std::endl;
|
||||
std::cin >> test;
|
||||
closesocket( sock );
|
||||
sock = INVALID_SOCKET;
|
||||
return false;
|
||||
@@ -94,6 +93,7 @@ bool CServerSocket::StartServer( )
|
||||
Log(MSG_ERROR, "setsockopt:SO_KEEPALIVE" );
|
||||
}
|
||||
setsockopt(sckISC, IPPROTO_TCP,TCP_NODELAY,(const char*)&optval,sizeof(optval));
|
||||
setsockopt(sckISC, SOL_SOCKET,SO_REUSEADDR,(const char*)&optval,sizeof(optval));
|
||||
sain.sin_family = AF_INET;
|
||||
sain.sin_addr.s_addr = INADDR_ANY;
|
||||
sain.sin_port = htons( Config.CharsPort);//29110 );
|
||||
@@ -125,7 +125,7 @@ bool CServerSocket::StartServer( )
|
||||
return false;
|
||||
}
|
||||
aina.sin_family = AF_INET;
|
||||
aina.sin_addr.s_addr = inet_addr(Config.CharIP);//( "127.0.0.1" );
|
||||
aina.sin_addr.s_addr = rose_inet_addr(Config.CharIP);//( "127.0.0.1" );
|
||||
aina.sin_port = htons( Config.CharPort);//29110 );
|
||||
if ( connect( sckISC, (const sockaddr*)&aina, sizeof( aina ) ) == SOCKET_ERROR ) {
|
||||
Log( MSG_ERROR, "Could not connect to ISC (WSK2 ERROR: %i)", WSAGetLastError( ) );
|
||||
@@ -146,6 +146,7 @@ bool CServerSocket::StartServer( )
|
||||
Log(MSG_ERROR, "setsockopt:SO_KEEPALIVE" );
|
||||
}
|
||||
setsockopt(sckISCII, IPPROTO_TCP,TCP_NODELAY,(const char*)&optval,sizeof(optval));
|
||||
setsockopt(sckISCII, SOL_SOCKET,SO_REUSEADDR,(const char*)&optval,sizeof(optval));
|
||||
sain.sin_family = AF_INET;
|
||||
sain.sin_addr.s_addr = INADDR_ANY;
|
||||
sain.sin_port = htons( Config.WorldsPort );//29210 );
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
Linux compatibility shims for MSVC-only CRT calls used throughout the
|
||||
original Windows codebase (fopen_s, _strdup, _stricmp, sprintf_s, ...),
|
||||
plus a path resolver: the shipped STB/AIP/quest data files embed
|
||||
Windows-style paths ("3DDATA\AI\FOO.AIP") with arbitrary case, which
|
||||
don't resolve on a case-sensitive Linux filesystem. rose_fopen() rewrites
|
||||
backslashes to '/' and, if that still doesn't exist, resolves each path
|
||||
component case-insensitively against the real directory contents before
|
||||
opening the file.
|
||||
*/
|
||||
#ifndef __ROSE_COMPAT_H__
|
||||
#define __ROSE_COMPAT_H__
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <strings.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <ctime>
|
||||
#include <cstdint>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#define __cdecl
|
||||
|
||||
typedef int errno_t;
|
||||
typedef int INT;
|
||||
typedef unsigned int UINT;
|
||||
typedef long LONG;
|
||||
typedef unsigned long ULONG;
|
||||
|
||||
inline FILE* rose_fopen(const char* path, const char* mode)
|
||||
{
|
||||
char norm[1024];
|
||||
size_t n = 0;
|
||||
for (; path[n] != 0 && n < sizeof(norm) - 1; ++n)
|
||||
norm[n] = (path[n] == '\\') ? '/' : path[n];
|
||||
norm[n] = 0;
|
||||
|
||||
struct stat st;
|
||||
if (stat(norm, &st) == 0)
|
||||
return fopen(norm, mode);
|
||||
|
||||
char resolved[1024];
|
||||
size_t rlen = 0;
|
||||
resolved[0] = 0;
|
||||
const char* p = norm;
|
||||
if (*p == '/') { resolved[0] = '/'; resolved[1] = 0; rlen = 1; ++p; }
|
||||
|
||||
while (*p)
|
||||
{
|
||||
const char* slash = strchr(p, '/');
|
||||
size_t len = slash ? (size_t)(slash - p) : strlen(p);
|
||||
char component[256];
|
||||
if (len >= sizeof(component)) len = sizeof(component) - 1;
|
||||
memcpy(component, p, len);
|
||||
component[len] = 0;
|
||||
|
||||
char candidate[1024];
|
||||
snprintf(candidate, sizeof(candidate), "%s%s", resolved, component);
|
||||
|
||||
if (stat(candidate, &st) != 0)
|
||||
{
|
||||
char dirbuf[1024];
|
||||
const char* dirpath = ".";
|
||||
if (rlen > 0)
|
||||
{
|
||||
size_t copylen = (rlen > 1 && resolved[rlen - 1] == '/') ? rlen - 1 : rlen;
|
||||
snprintf(dirbuf, sizeof(dirbuf), "%.*s", (int)copylen, resolved);
|
||||
dirpath = dirbuf[0] ? dirbuf : "/";
|
||||
}
|
||||
DIR* d = opendir(dirpath);
|
||||
if (d)
|
||||
{
|
||||
struct dirent* de;
|
||||
while ((de = readdir(d)) != NULL)
|
||||
{
|
||||
if (strcasecmp(de->d_name, component) == 0)
|
||||
{
|
||||
snprintf(candidate, sizeof(candidate), "%s/%s", dirpath, de->d_name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
}
|
||||
|
||||
rlen = snprintf(resolved, sizeof(resolved), "%s/", candidate);
|
||||
p = slash ? slash + 1 : p + len;
|
||||
}
|
||||
|
||||
if (rlen > 0 && resolved[rlen - 1] == '/')
|
||||
resolved[rlen - 1] = 0;
|
||||
|
||||
return fopen(resolved, mode);
|
||||
}
|
||||
|
||||
inline errno_t rose_fopen_s(FILE** fh, const char* path, const char* mode)
|
||||
{
|
||||
*fh = rose_fopen(path, mode);
|
||||
return (*fh != NULL) ? 0 : 1;
|
||||
}
|
||||
#define fopen_s(pfh, path, mode) rose_fopen_s(pfh, path, mode)
|
||||
|
||||
#define _strdup strdup
|
||||
#define _stricmp strcasecmp
|
||||
#define _strcmpi strcasecmp
|
||||
|
||||
#define sprintf_s(buf, ...) sprintf(buf, __VA_ARGS__)
|
||||
#define vsprintf_s(buf, fmt, ap) vsprintf(buf, fmt, ap)
|
||||
|
||||
inline errno_t rose_ctime_s(char* buf, size_t bufsz, const time_t* t)
|
||||
{
|
||||
return (ctime_r(t, buf) != NULL) ? 0 : 1;
|
||||
}
|
||||
#define ctime_s(buf, bufsz, t) rose_ctime_s(buf, bufsz, t)
|
||||
|
||||
inline errno_t rose_strncpy_s(char* dst, const char* src, size_t count)
|
||||
{
|
||||
strncpy(dst, src, count);
|
||||
dst[count] = 0;
|
||||
return 0;
|
||||
}
|
||||
#define strncpy_s(dst, src, count) rose_strncpy_s(dst, src, count)
|
||||
|
||||
// Resolves a literal IPv4 address ("10.0.0.5") *or* a hostname (docker
|
||||
// compose service name, e.g. "charserver") to a network-order IPv4 address,
|
||||
// so inter-server links (server2server config values) work with either.
|
||||
inline unsigned long rose_inet_addr(const char* host)
|
||||
{
|
||||
struct in_addr addr;
|
||||
if (inet_pton(AF_INET, host, &addr) == 1)
|
||||
return addr.s_addr;
|
||||
|
||||
struct addrinfo hints;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
struct addrinfo* res = NULL;
|
||||
if (getaddrinfo(host, NULL, &hints, &res) != 0 || res == NULL)
|
||||
return INADDR_NONE;
|
||||
|
||||
unsigned long result = ((struct sockaddr_in*)res->ai_addr)->sin_addr.s_addr;
|
||||
freeaddrinfo(res);
|
||||
return result;
|
||||
}
|
||||
|
||||
struct SYSTEMTIME
|
||||
{
|
||||
unsigned short wYear;
|
||||
unsigned short wMonth;
|
||||
unsigned short wDayOfWeek;
|
||||
unsigned short wDay;
|
||||
unsigned short wHour;
|
||||
unsigned short wMinute;
|
||||
unsigned short wSecond;
|
||||
unsigned short wMilliseconds;
|
||||
};
|
||||
|
||||
inline void GetLocalTime(SYSTEMTIME* out)
|
||||
{
|
||||
time_t rawtime = time(NULL);
|
||||
struct tm ltm;
|
||||
localtime_r(&rawtime, <m);
|
||||
out->wYear = (unsigned short)(ltm.tm_year + 1900);
|
||||
out->wMonth = (unsigned short)(ltm.tm_mon + 1);
|
||||
out->wDayOfWeek = (unsigned short)ltm.tm_wday;
|
||||
out->wDay = (unsigned short)ltm.tm_mday;
|
||||
out->wHour = (unsigned short)ltm.tm_hour;
|
||||
out->wMinute = (unsigned short)ltm.tm_min;
|
||||
out->wSecond = (unsigned short)ltm.tm_sec;
|
||||
out->wMilliseconds = 0;
|
||||
}
|
||||
|
||||
#endif // !_WIN32
|
||||
#endif // __ROSE_COMPAT_H__
|
||||
+20
-3
@@ -4,6 +4,8 @@
|
||||
#include <cstdio>
|
||||
#endif
|
||||
#include <cstdio>
|
||||
#include <cstdarg>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
#include "log.h"
|
||||
@@ -59,9 +61,14 @@ void Log( enum msg_type flag, const char *Format, ... )
|
||||
|
||||
switch (flag) {
|
||||
case MSG_NONE: // direct printf replacement
|
||||
{
|
||||
textcolor(WHITE);
|
||||
vprintf( Format, ap );
|
||||
va_list apcopy;
|
||||
va_copy(apcopy, ap);
|
||||
vprintf( Format, apcopy );
|
||||
va_end(apcopy);
|
||||
break;
|
||||
}
|
||||
case MSG_STATUS:
|
||||
textcolor(GREEN);
|
||||
printf("[STATUS]: ");
|
||||
@@ -107,10 +114,15 @@ void Log( enum msg_type flag, const char *Format, ... )
|
||||
printf("[GM ACTION]: ");
|
||||
break;
|
||||
case MSG_START:
|
||||
{
|
||||
textcolor(MAGENTA);
|
||||
vprintf( Format, ap );
|
||||
va_list apcopy;
|
||||
va_copy(apcopy, ap);
|
||||
vprintf( Format, apcopy );
|
||||
va_end(apcopy);
|
||||
printf( "\r\n" );
|
||||
break;
|
||||
}
|
||||
//case MSG_SDEBUG:
|
||||
// textcolor(MAGENTA);
|
||||
// printf("[SDEBUG]: ");
|
||||
@@ -128,7 +140,12 @@ void Log( enum msg_type flag, const char *Format, ... )
|
||||
if(flag!=MSG_QUERY)
|
||||
{
|
||||
printf( "%s- ", timestamp );
|
||||
vprintf( Format, ap );
|
||||
{
|
||||
va_list apcopy;
|
||||
va_copy(apcopy, ap);
|
||||
vprintf( Format, apcopy );
|
||||
va_end(apcopy);
|
||||
}
|
||||
printf( (flag==MSG_LOAD) ? "\r" : "\n" );
|
||||
}
|
||||
FILE *fh = NULL;
|
||||
|
||||
@@ -113,7 +113,7 @@ void GenerateCryptTables( CCryptTable* &CryptTables, unsigned ModValue )
|
||||
|
||||
// Allocate memory for structure
|
||||
CryptTables = (CCryptTable*)malloc( sizeof( CCryptTable ) );
|
||||
CryptTables->Tables = (unsigned **)calloc( 16, sizeof( unsigned ) );
|
||||
CryptTables->Tables = (unsigned **)calloc( 16, sizeof( unsigned* ) );
|
||||
|
||||
// Prepare values
|
||||
ModValue = (int(ModValue * 0x41C64E6D) + 0x3039);
|
||||
@@ -133,8 +133,12 @@ void GenerateCryptTables( CCryptTable* &CryptTables, unsigned ModValue )
|
||||
ControlStruct.CurTableMod = 0;
|
||||
for ( table=0; table<0x11; ++table )
|
||||
{
|
||||
ivalue = ctGenerateNextValue( CryptTables, &ControlStruct ) & 0x80000003;
|
||||
if ( ivalue<0 ) ivalue = ( ( ivalue - 1 ) | 0xFFFFFFFC ) + 1;
|
||||
// Signed-overflow-safe rewrite of the original ivalue-based bit trick:
|
||||
// GCC's -O3 exploited UB from (INT_MIN - 1) when the mask below set the
|
||||
// sign bit, corrupting unrelated registers. Same bit pattern, unsigned math.
|
||||
unsigned uvalue = ctGenerateNextValue( CryptTables, &ControlStruct ) & 0x80000003u;
|
||||
if ( (int)uvalue<0 ) uvalue = ( ( uvalue - 1 ) | 0xFFFFFFFCu ) + 1;
|
||||
ivalue = (int)uvalue;
|
||||
TableFuncList[ table ] = ( unsigned char )ivalue;
|
||||
}
|
||||
|
||||
@@ -196,7 +200,7 @@ void GenerateLoginServerCryptTables( CCryptTable* &CryptTables )
|
||||
|
||||
// Allocate memory for structure
|
||||
CryptTables = (CCryptTable*)malloc( sizeof( CCryptTable ) );
|
||||
CryptTables->Tables = (unsigned **)calloc( 16, sizeof( unsigned ) );
|
||||
CryptTables->Tables = (unsigned **)calloc( 16, sizeof( unsigned* ) );
|
||||
|
||||
// Set default startvalues
|
||||
for ( table=0; table<4; ++table )
|
||||
@@ -237,10 +241,11 @@ void GenerateLoginServerCryptTables( CCryptTable* &CryptTables )
|
||||
do
|
||||
{
|
||||
// Generate next value for table
|
||||
// Unsigned multiply: signed overflow here is UB and gets miscompiled at -O3.
|
||||
ControlStruct.CountTableMod[1];
|
||||
ivalue = ControlStruct.ValueTableMod[1];
|
||||
ivalue *= 0x3E39B193;
|
||||
value = (0x3039 - (unsigned)ivalue) & 0x7FFFFFFF;
|
||||
unsigned umulvalue = (unsigned)ControlStruct.ValueTableMod[1];
|
||||
umulvalue *= 0x3E39B193u;
|
||||
value = (0x3039 - umulvalue) & 0x7FFFFFFF;
|
||||
ControlStruct.ValueTableMod[1] = value;
|
||||
|
||||
// Don't save, if value already exist
|
||||
|
||||
+12
-1
@@ -31,15 +31,16 @@
|
||||
#endif
|
||||
#define FD_SETSIZE 1024
|
||||
#else
|
||||
#include <winsock2.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <cstdarg>
|
||||
#include <errno.h>
|
||||
#define WSAGetLastError() errno
|
||||
#define SOCKET int
|
||||
#define INVALID_SOCKET -1
|
||||
#define SOCKET_ERROR -1
|
||||
@@ -69,7 +70,9 @@
|
||||
#include "rosecrypt.hpp"
|
||||
#include "config.h"
|
||||
#include "database/database.h"
|
||||
#ifdef _WIN32
|
||||
#pragma comment(lib, "Ws2_32.lib")
|
||||
#endif
|
||||
// Just some defs for easier use
|
||||
typedef char SBYTE;
|
||||
typedef unsigned char BYTE;
|
||||
@@ -79,10 +82,18 @@ typedef long SDWORD;
|
||||
typedef unsigned long DWORD;
|
||||
typedef long long SQWORD;
|
||||
typedef unsigned long long QWORD;
|
||||
#ifdef _WIN32
|
||||
typedef unsigned __int8 byte;
|
||||
typedef unsigned __int16 word;
|
||||
typedef unsigned __int32 dword;
|
||||
typedef unsigned __int64 qword;
|
||||
#else
|
||||
typedef unsigned char byte;
|
||||
typedef unsigned short word;
|
||||
typedef unsigned int dword;
|
||||
typedef unsigned long long qword;
|
||||
typedef unsigned int UINT;
|
||||
#endif
|
||||
typedef char* strings;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
Reference in new issue
Block a user