2020-04-14 04:20:29 +00:00
|
|
|
// SONIC ROBO BLAST 2 KART
|
2020-03-01 10:36:56 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Copyright (C) 2020 by James R.
|
|
|
|
//
|
|
|
|
// This program is free software distributed under the
|
|
|
|
// terms of the GNU General Public License, version 2.
|
|
|
|
// See the 'LICENSE' file for more details.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// \brief HTTP based master server
|
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
/*
|
|
|
|
Documentation available here.
|
|
|
|
|
|
|
|
<http://mb.srb2.org/MS/tools/api/v1/>
|
|
|
|
*/
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#include "doomdef.h"
|
|
|
|
#include "d_clisrv.h"
|
|
|
|
#include "command.h"
|
2020-05-13 22:29:17 +00:00
|
|
|
#include "m_argv.h"
|
2020-04-27 02:46:35 +00:00
|
|
|
#include "m_menu.h"
|
2020-03-01 10:36:56 +00:00
|
|
|
#include "mserv.h"
|
|
|
|
#include "i_tcp.h"/* for current_port */
|
2020-04-28 21:21:57 +00:00
|
|
|
#include "i_threads.h"
|
2020-04-13 00:51:05 +00:00
|
|
|
|
2020-05-15 00:19:25 +00:00
|
|
|
/* reasonable default I guess?? */
|
|
|
|
#define DEFAULT_BUFFER_SIZE (4096)
|
|
|
|
|
2020-04-13 00:51:05 +00:00
|
|
|
/* I just stop myself from making macros anymore. */
|
|
|
|
#define Blame( ... ) \
|
|
|
|
CONS_Printf("\x85" __VA_ARGS__)
|
2020-03-01 10:36:56 +00:00
|
|
|
|
2020-05-13 02:22:15 +00:00
|
|
|
static void MasterServer_Debug_OnChange (void);
|
|
|
|
|
2020-05-15 20:20:40 +00:00
|
|
|
consvar_t cv_masterserver_timeout = {
|
|
|
|
"masterserver_timeout", "5", CV_SAVE, CV_Unsigned,
|
|
|
|
NULL, 0, NULL, NULL, 0, 0, NULL/* C90 moment */
|
|
|
|
};
|
|
|
|
|
2020-03-01 19:39:27 +00:00
|
|
|
consvar_t cv_masterserver_debug = {
|
2020-05-13 02:22:15 +00:00
|
|
|
"masterserver_debug", "Off", CV_SAVE|CV_CALL, CV_OnOff,
|
|
|
|
MasterServer_Debug_OnChange, 0, NULL, NULL, 0, 0, NULL/* C90 moment */
|
2020-03-01 19:39:27 +00:00
|
|
|
};
|
|
|
|
|
2020-06-13 06:47:08 +00:00
|
|
|
consvar_t cv_masterserver_token = {
|
|
|
|
"masterserver_token", "", CV_SAVE, NULL,
|
|
|
|
NULL, 0, NULL, NULL, 0, 0, NULL/* C90 moment */
|
|
|
|
};
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
static int hms_started;
|
|
|
|
|
2020-04-28 21:21:57 +00:00
|
|
|
static char *hms_api;
|
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
static I_mutex hms_api_mutex;
|
|
|
|
#endif
|
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
static char *hms_server_token;
|
2020-03-01 10:36:56 +00:00
|
|
|
|
|
|
|
struct HMS_buffer
|
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
char *buffer;
|
|
|
|
int needle;
|
|
|
|
int end;
|
|
|
|
};
|
|
|
|
|
2020-04-13 00:51:05 +00:00
|
|
|
static void
|
2020-04-13 01:27:01 +00:00
|
|
|
Contact_error (void)
|
2020-04-13 00:51:05 +00:00
|
|
|
{
|
|
|
|
CONS_Alert(CONS_ERROR,
|
|
|
|
"There was a problem contacting the master server...\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
static size_t
|
|
|
|
HMS_on_read (char *s, size_t _1, size_t n, void *userdata)
|
|
|
|
{
|
|
|
|
struct HMS_buffer *buffer;
|
2020-05-15 00:19:25 +00:00
|
|
|
size_t blocks;
|
|
|
|
|
2020-04-14 06:31:26 +00:00
|
|
|
(void)_1;
|
2020-05-15 00:19:25 +00:00
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
buffer = userdata;
|
2020-05-15 00:19:25 +00:00
|
|
|
|
|
|
|
if (n >= (size_t)( buffer->end - buffer->needle ))
|
2020-03-01 10:36:56 +00:00
|
|
|
{
|
2020-05-15 00:19:25 +00:00
|
|
|
/* resize to next multiple of buffer size */
|
|
|
|
blocks = ( n / DEFAULT_BUFFER_SIZE + 1 );
|
2020-06-15 02:13:22 +00:00
|
|
|
buffer->end += ( blocks * DEFAULT_BUFFER_SIZE );
|
2020-05-15 00:19:25 +00:00
|
|
|
|
|
|
|
buffer->buffer = realloc(buffer->buffer, buffer->end);
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
2020-05-15 00:19:25 +00:00
|
|
|
|
|
|
|
memcpy(&buffer->buffer[buffer->needle], s, n);
|
|
|
|
buffer->needle += n;
|
|
|
|
|
|
|
|
return n;
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct HMS_buffer *
|
|
|
|
HMS_connect (const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
CURL *curl;
|
|
|
|
char *url;
|
2020-06-13 06:47:08 +00:00
|
|
|
char *quack_token;
|
2020-03-01 10:36:56 +00:00
|
|
|
size_t seek;
|
2020-06-13 06:47:08 +00:00
|
|
|
size_t token_length;
|
2020-03-01 10:36:56 +00:00
|
|
|
struct HMS_buffer *buffer;
|
|
|
|
|
|
|
|
if (! hms_started)
|
|
|
|
{
|
2020-04-13 00:51:05 +00:00
|
|
|
if (curl_global_init(CURL_GLOBAL_ALL) != 0)
|
|
|
|
{
|
|
|
|
Contact_error();
|
|
|
|
Blame("From curl_global_init.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
atexit(curl_global_cleanup);
|
|
|
|
hms_started = 1;
|
|
|
|
}
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
curl = curl_easy_init();
|
|
|
|
|
2020-04-13 00:51:05 +00:00
|
|
|
if (! curl)
|
|
|
|
{
|
|
|
|
Contact_error();
|
|
|
|
Blame("From curl_easy_init.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-13 06:47:08 +00:00
|
|
|
if (cv_masterserver_token.string[0])
|
|
|
|
{
|
|
|
|
quack_token = curl_easy_escape(curl, cv_masterserver_token.string, 0);
|
|
|
|
token_length = ( sizeof "?token="-1 )+ strlen(quack_token);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quack_token = NULL;
|
|
|
|
token_length = 0;
|
|
|
|
}
|
|
|
|
|
2020-04-28 21:21:57 +00:00
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
I_lock_mutex(&hms_api_mutex);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
seek = strlen(hms_api) + 1;/* + '/' */
|
2020-03-01 10:36:56 +00:00
|
|
|
|
|
|
|
va_start (ap, format);
|
2020-06-13 06:47:08 +00:00
|
|
|
url = malloc(seek + vsnprintf(0, 0, format, ap) + token_length + 1);
|
2020-03-01 10:36:56 +00:00
|
|
|
va_end (ap);
|
|
|
|
|
2020-04-28 21:21:57 +00:00
|
|
|
sprintf(url, "%s/", hms_api);
|
|
|
|
|
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
I_unlock_mutex(hms_api_mutex);
|
|
|
|
#endif
|
2020-03-01 10:36:56 +00:00
|
|
|
|
|
|
|
va_start (ap, format);
|
2020-06-13 06:47:08 +00:00
|
|
|
seek += vsprintf(&url[seek], format, ap);
|
2020-03-01 10:36:56 +00:00
|
|
|
va_end (ap);
|
|
|
|
|
2020-06-13 06:47:08 +00:00
|
|
|
if (quack_token)
|
|
|
|
sprintf(&url[seek], "?token=%s", quack_token);
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
CONS_Printf("HMS: connecting '%s'...\n", url);
|
|
|
|
|
2020-04-27 04:56:59 +00:00
|
|
|
buffer = malloc(sizeof *buffer);
|
2020-03-01 10:36:56 +00:00
|
|
|
buffer->curl = curl;
|
2020-05-15 00:19:25 +00:00
|
|
|
buffer->end = DEFAULT_BUFFER_SIZE;
|
2020-04-27 04:56:59 +00:00
|
|
|
buffer->buffer = malloc(buffer->end);
|
2020-03-01 10:36:56 +00:00
|
|
|
buffer->needle = 0;
|
|
|
|
|
2020-03-01 19:39:27 +00:00
|
|
|
if (cv_masterserver_debug.value)
|
2020-03-01 19:46:02 +00:00
|
|
|
{
|
2020-03-01 19:39:27 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
2020-03-01 19:46:02 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_STDERR, logstream);
|
|
|
|
}
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-05-13 22:29:17 +00:00
|
|
|
if (M_CheckParm("-bindaddr") && M_IsNextParm())
|
|
|
|
{
|
|
|
|
curl_easy_setopt(curl, CURLOPT_INTERFACE, M_GetNextParm());
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
2020-05-13 01:52:05 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-05-15 20:20:40 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, cv_masterserver_timeout.value);
|
2020-03-01 10:36:56 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HMS_on_read);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer);
|
|
|
|
|
2020-06-13 06:47:08 +00:00
|
|
|
curl_free(quack_token);
|
2020-04-27 04:56:59 +00:00
|
|
|
free(url);
|
2020-03-01 10:36:56 +00:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
HMS_do (struct HMS_buffer *buffer)
|
|
|
|
{
|
2020-04-13 00:51:05 +00:00
|
|
|
CURLcode cc;
|
2020-03-01 10:36:56 +00:00
|
|
|
long status;
|
2020-04-13 00:51:05 +00:00
|
|
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
cc = curl_easy_perform(buffer->curl);
|
|
|
|
|
|
|
|
if (cc != CURLE_OK)
|
|
|
|
{
|
|
|
|
Contact_error();
|
|
|
|
Blame(
|
|
|
|
"From curl_easy_perform: %s\n",
|
|
|
|
curl_easy_strerror(cc)
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
buffer->buffer[buffer->needle] = '\0';
|
2020-04-13 00:51:05 +00:00
|
|
|
|
|
|
|
curl_easy_getinfo(buffer->curl, CURLINFO_RESPONSE_CODE, &status);
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
if (status != 200)
|
|
|
|
{
|
2020-04-13 00:51:05 +00:00
|
|
|
p = strchr(buffer->buffer, '\n');
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
Contact_error();
|
|
|
|
Blame(
|
|
|
|
"Master server error %ld: %s%s\n",
|
2020-03-01 10:36:56 +00:00
|
|
|
status,
|
2020-04-13 00:51:05 +00:00
|
|
|
buffer->buffer,
|
|
|
|
( (p) ? "" : " (malformed)" )
|
2020-03-01 10:36:56 +00:00
|
|
|
);
|
2020-04-13 00:51:05 +00:00
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
HMS_end (struct HMS_buffer *buffer)
|
|
|
|
{
|
|
|
|
curl_easy_cleanup(buffer->curl);
|
2020-04-27 04:56:59 +00:00
|
|
|
free(buffer->buffer);
|
|
|
|
free(buffer);
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-04-27 02:46:35 +00:00
|
|
|
HMS_fetch_rooms (int joining, int query_id)
|
2020-03-01 10:36:56 +00:00
|
|
|
{
|
|
|
|
struct HMS_buffer *hms;
|
2020-04-14 05:23:01 +00:00
|
|
|
int ok;
|
2020-03-01 10:36:56 +00:00
|
|
|
|
2020-04-27 02:46:35 +00:00
|
|
|
int doing_shit;
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
char *id;
|
|
|
|
char *title;
|
2020-04-14 06:31:26 +00:00
|
|
|
char *room_motd;
|
2020-03-01 10:36:56 +00:00
|
|
|
|
2020-04-14 05:31:11 +00:00
|
|
|
int id_no;
|
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
char *p;
|
|
|
|
char *end;
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
int i;
|
|
|
|
|
2020-04-27 02:46:35 +00:00
|
|
|
(void)query_id;
|
|
|
|
|
2020-03-21 03:25:34 +00:00
|
|
|
hms = HMS_connect("rooms");
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-04-14 23:55:14 +00:00
|
|
|
if (! hms)
|
|
|
|
return 0;
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
if (HMS_do(hms))
|
|
|
|
{
|
2020-04-27 02:46:35 +00:00
|
|
|
doing_shit = 1;
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
p = hms->buffer;
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-04-14 05:31:11 +00:00
|
|
|
for (i = 0; i < NUM_LIST_ROOMS && ( end = strstr(p, "\n\n\n") );)
|
2020-03-01 10:36:56 +00:00
|
|
|
{
|
|
|
|
*end = '\0';
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
id = strtok(p, "\n");
|
|
|
|
title = strtok(0, "\n");
|
2020-04-14 06:31:26 +00:00
|
|
|
room_motd = strtok(0, "");
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-04-14 06:31:26 +00:00
|
|
|
if (id && title && room_motd)
|
2020-03-01 10:36:56 +00:00
|
|
|
{
|
2020-04-14 05:31:11 +00:00
|
|
|
id_no = atoi(id);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Don't show the 'All' room if hosting. And it's a hack like this
|
|
|
|
because I'm way too lazy to add another feature to the MS.
|
|
|
|
*/
|
|
|
|
if (joining || id_no != 0)
|
|
|
|
{
|
2020-04-27 02:46:35 +00:00
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
I_lock_mutex(&ms_QueryId_mutex);
|
|
|
|
{
|
|
|
|
if (query_id != ms_QueryId)
|
|
|
|
doing_shit = 0;
|
|
|
|
}
|
|
|
|
I_unlock_mutex(ms_QueryId_mutex);
|
|
|
|
|
|
|
|
if (! doing_shit)
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2020-04-14 05:31:11 +00:00
|
|
|
room_list[i].header.buffer[0] = 1;
|
|
|
|
|
|
|
|
room_list[i].id = id_no;
|
|
|
|
strlcpy(room_list[i].name, title, sizeof room_list[i].name);
|
2020-04-14 06:31:26 +00:00
|
|
|
strlcpy(room_list[i].motd, room_motd, sizeof room_list[i].motd);
|
2020-04-14 05:31:11 +00:00
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
2020-03-01 10:36:56 +00:00
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
p = ( end + 3 );/* skip the three linefeeds */
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-04-27 02:46:35 +00:00
|
|
|
if (doing_shit)
|
|
|
|
room_list[i].header.buffer[0] = 0;
|
2020-04-14 05:23:01 +00:00
|
|
|
|
|
|
|
ok = 1;
|
2020-04-27 02:46:35 +00:00
|
|
|
|
|
|
|
if (doing_shit)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
I_lock_mutex(&m_menu_mutex);
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
for (i = 0; room_list[i].header.buffer[0]; i++)
|
|
|
|
{
|
|
|
|
if(*room_list[i].name != '\0')
|
|
|
|
{
|
|
|
|
MP_RoomMenu[i+1].text = room_list[i].name;
|
|
|
|
roomIds[i] = room_list[i].id;
|
|
|
|
MP_RoomMenu[i+1].status = IT_STRING|IT_CALL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
I_unlock_mutex(m_menu_mutex);
|
|
|
|
#endif
|
|
|
|
}
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
2020-04-14 05:23:01 +00:00
|
|
|
else
|
|
|
|
ok = 0;
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
HMS_end(hms);
|
2020-04-14 05:23:01 +00:00
|
|
|
|
|
|
|
return ok;
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
HMS_register (void)
|
|
|
|
{
|
|
|
|
struct HMS_buffer *hms;
|
|
|
|
int ok;
|
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
char post[256];
|
|
|
|
|
|
|
|
char *title;
|
|
|
|
|
2020-03-21 03:25:34 +00:00
|
|
|
hms = HMS_connect("rooms/%d/register", ms_RoomId);
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-04-14 23:55:14 +00:00
|
|
|
if (! hms)
|
|
|
|
return 0;
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
title = curl_easy_escape(hms->curl, cv_servername.string, 0);
|
2020-04-13 01:25:59 +00:00
|
|
|
|
|
|
|
snprintf(post, sizeof post,
|
|
|
|
"port=%d&"
|
|
|
|
"title=%s&"
|
2020-05-13 00:51:27 +00:00
|
|
|
"version=%d.%d",
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
current_port,
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-03-21 03:25:34 +00:00
|
|
|
title,
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-05-13 00:51:27 +00:00
|
|
|
VERSION,
|
2020-03-21 03:25:34 +00:00
|
|
|
SUBVERSION
|
2020-03-01 10:36:56 +00:00
|
|
|
);
|
2020-04-13 01:25:59 +00:00
|
|
|
|
|
|
|
curl_free(title);
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
curl_easy_setopt(hms->curl, CURLOPT_POSTFIELDS, post);
|
|
|
|
|
|
|
|
ok = HMS_do(hms);
|
|
|
|
|
|
|
|
if (ok)
|
|
|
|
{
|
2020-04-27 04:56:59 +00:00
|
|
|
hms_server_token = strdup(strtok(hms->buffer, "\n"));
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HMS_end(hms);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-04-28 19:33:50 +00:00
|
|
|
int
|
2020-03-01 10:36:56 +00:00
|
|
|
HMS_unlist (void)
|
|
|
|
{
|
|
|
|
struct HMS_buffer *hms;
|
2020-04-28 19:33:50 +00:00
|
|
|
int ok;
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-03-21 03:25:34 +00:00
|
|
|
hms = HMS_connect("servers/%s/unlist", hms_server_token);
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-04-14 23:55:14 +00:00
|
|
|
if (! hms)
|
2020-04-28 19:33:50 +00:00
|
|
|
return 0;
|
2020-04-14 23:55:14 +00:00
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
curl_easy_setopt(hms->curl, CURLOPT_CUSTOMREQUEST, "POST");
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-04-28 19:33:50 +00:00
|
|
|
ok = HMS_do(hms);
|
2020-03-01 10:36:56 +00:00
|
|
|
HMS_end(hms);
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-04-27 04:56:59 +00:00
|
|
|
free(hms_server_token);
|
2020-04-28 19:33:50 +00:00
|
|
|
|
|
|
|
return ok;
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 05:23:01 +00:00
|
|
|
int
|
2020-03-01 10:36:56 +00:00
|
|
|
HMS_update (void)
|
|
|
|
{
|
|
|
|
struct HMS_buffer *hms;
|
2020-04-14 05:23:01 +00:00
|
|
|
int ok;
|
2020-04-13 01:25:59 +00:00
|
|
|
|
|
|
|
char post[256];
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
char *title;
|
|
|
|
|
2020-03-21 03:25:34 +00:00
|
|
|
hms = HMS_connect("servers/%s/update", hms_server_token);
|
2020-03-01 10:36:56 +00:00
|
|
|
|
2020-04-14 23:55:14 +00:00
|
|
|
if (! hms)
|
|
|
|
return 0;
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
title = curl_easy_escape(hms->curl, cv_servername.string, 0);
|
2020-04-13 01:25:59 +00:00
|
|
|
|
|
|
|
snprintf(post, sizeof post,
|
|
|
|
"title=%s",
|
|
|
|
title
|
|
|
|
);
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
curl_free(title);
|
|
|
|
|
|
|
|
curl_easy_setopt(hms->curl, CURLOPT_POSTFIELDS, post);
|
|
|
|
|
2020-04-14 05:23:01 +00:00
|
|
|
ok = HMS_do(hms);
|
2020-03-01 10:36:56 +00:00
|
|
|
HMS_end(hms);
|
2020-04-14 05:23:01 +00:00
|
|
|
|
|
|
|
return ok;
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HMS_list_servers (void)
|
|
|
|
{
|
|
|
|
struct HMS_buffer *hms;
|
2020-04-13 01:25:59 +00:00
|
|
|
|
|
|
|
char *p;
|
|
|
|
|
2020-03-21 03:25:34 +00:00
|
|
|
hms = HMS_connect("servers");
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-04-14 23:55:14 +00:00
|
|
|
if (! hms)
|
|
|
|
return;
|
|
|
|
|
2020-03-01 18:59:25 +00:00
|
|
|
if (HMS_do(hms))
|
2020-04-13 01:25:59 +00:00
|
|
|
{
|
|
|
|
p = &hms->buffer[strlen(hms->buffer)];
|
|
|
|
while (*--p == '\n')
|
|
|
|
;
|
|
|
|
|
|
|
|
CONS_Printf("%s\n", hms->buffer);
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
HMS_end(hms);
|
|
|
|
}
|
|
|
|
|
2020-04-14 05:23:01 +00:00
|
|
|
msg_server_t *
|
2020-04-27 02:46:35 +00:00
|
|
|
HMS_fetch_servers (msg_server_t *list, int room_number, int query_id)
|
2020-03-01 10:36:56 +00:00
|
|
|
{
|
|
|
|
struct HMS_buffer *hms;
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-04-27 02:46:35 +00:00
|
|
|
int doing_shit;
|
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
char local_version[9];
|
2020-03-01 10:36:56 +00:00
|
|
|
|
|
|
|
char *room;
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
char *address;
|
|
|
|
char *port;
|
|
|
|
char *title;
|
2020-04-13 01:25:59 +00:00
|
|
|
char *version;
|
|
|
|
|
|
|
|
char *end;
|
|
|
|
char *section_end;
|
|
|
|
char *p;
|
2020-03-01 10:36:56 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
2020-04-27 02:46:35 +00:00
|
|
|
(void)query_id;
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
if (room_number > 0)
|
|
|
|
{
|
2020-03-21 03:25:34 +00:00
|
|
|
hms = HMS_connect("rooms/%d/servers", room_number);
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
|
|
|
else
|
2020-03-21 03:25:34 +00:00
|
|
|
hms = HMS_connect("servers");
|
2020-03-01 10:36:56 +00:00
|
|
|
|
2020-04-14 23:55:14 +00:00
|
|
|
if (! hms)
|
|
|
|
return NULL;
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
if (HMS_do(hms))
|
|
|
|
{
|
2020-04-27 02:46:35 +00:00
|
|
|
doing_shit = 1;
|
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
snprintf(local_version, sizeof local_version,
|
2020-05-13 00:51:27 +00:00
|
|
|
"%d.%d",
|
|
|
|
VERSION,
|
2020-04-13 01:25:59 +00:00
|
|
|
SUBVERSION
|
|
|
|
);
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
p = hms->buffer;
|
|
|
|
i = 0;
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
section_end = strstr(p, "\n\n");
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
room = strtok(p, "\n");
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
p = strtok(0, "");
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-03-01 20:15:48 +00:00
|
|
|
if (! p)
|
|
|
|
break;
|
2020-04-13 01:25:59 +00:00
|
|
|
|
|
|
|
while (i < MAXSERVERLIST && ( end = strchr(p, '\n') ))
|
2020-03-01 10:36:56 +00:00
|
|
|
{
|
|
|
|
*end = '\0';
|
|
|
|
|
|
|
|
address = strtok(p, " ");
|
|
|
|
port = strtok(0, " ");
|
2020-04-13 01:25:59 +00:00
|
|
|
title = strtok(0, " ");
|
|
|
|
version = strtok(0, "");
|
2020-03-01 10:36:56 +00:00
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
if (address && port && title && version)
|
2020-03-01 10:36:56 +00:00
|
|
|
{
|
2020-04-27 02:46:35 +00:00
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
I_lock_mutex(&ms_QueryId_mutex);
|
|
|
|
{
|
|
|
|
if (query_id != ms_QueryId)
|
|
|
|
doing_shit = 0;
|
|
|
|
}
|
|
|
|
I_unlock_mutex(ms_QueryId_mutex);
|
|
|
|
|
|
|
|
if (! doing_shit)
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
if (strcmp(version, local_version) == 0)
|
|
|
|
{
|
|
|
|
strlcpy(list[i].ip, address, sizeof list[i].ip);
|
|
|
|
strlcpy(list[i].port, port, sizeof list[i].port);
|
|
|
|
strlcpy(list[i].name, title, sizeof list[i].name);
|
|
|
|
strlcpy(list[i].version, version, sizeof list[i].version);
|
2020-03-01 10:36:56 +00:00
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
list[i].room = atoi(room);
|
2020-03-01 10:36:56 +00:00
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
list[i].header.buffer[0] = 1;
|
|
|
|
|
|
|
|
i++;
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 01:25:59 +00:00
|
|
|
if (end == section_end)/* end of list for this room */
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
p = ( end + 1 );/* skip server delimiter */
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-13 01:25:59 +00:00
|
|
|
section_end = 0;/* malformed so quit the parsing */
|
2020-03-01 10:36:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 01:25:59 +00:00
|
|
|
|
2020-04-27 02:46:35 +00:00
|
|
|
if (! doing_shit)
|
|
|
|
break;
|
|
|
|
|
2020-03-01 10:36:56 +00:00
|
|
|
p = ( section_end + 2 );
|
|
|
|
}
|
|
|
|
while (section_end) ;
|
|
|
|
|
2020-04-27 02:46:35 +00:00
|
|
|
if (doing_shit)
|
|
|
|
list[i].header.buffer[0] = 0;
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
2020-04-14 05:23:01 +00:00
|
|
|
else
|
|
|
|
list = NULL;
|
2020-03-01 10:36:56 +00:00
|
|
|
|
|
|
|
HMS_end(hms);
|
2020-04-14 05:23:01 +00:00
|
|
|
|
|
|
|
return list;
|
2020-03-01 10:36:56 +00:00
|
|
|
}
|
2020-04-13 03:37:46 +00:00
|
|
|
|
2020-04-14 05:23:01 +00:00
|
|
|
int
|
|
|
|
HMS_compare_mod_version (char *buffer, size_t buffer_size)
|
2020-04-13 03:37:46 +00:00
|
|
|
{
|
|
|
|
struct HMS_buffer *hms;
|
2020-04-14 05:23:01 +00:00
|
|
|
int ok;
|
2020-04-13 03:37:46 +00:00
|
|
|
|
|
|
|
char *version;
|
|
|
|
char *version_name;
|
|
|
|
|
|
|
|
hms = HMS_connect("versions/%d", MODID);
|
|
|
|
|
2020-04-14 23:55:14 +00:00
|
|
|
if (! hms)
|
|
|
|
return 0;
|
|
|
|
|
2020-04-14 05:23:01 +00:00
|
|
|
ok = 0;
|
|
|
|
|
2020-04-13 03:37:46 +00:00
|
|
|
if (HMS_do(hms))
|
|
|
|
{
|
|
|
|
version = strtok(hms->buffer, " ");
|
|
|
|
version_name = strtok(0, "\n");
|
|
|
|
|
|
|
|
if (version && version_name)
|
|
|
|
{
|
|
|
|
if (atoi(version) != MODVERSION)
|
|
|
|
{
|
2020-04-14 05:23:01 +00:00
|
|
|
strlcpy(buffer, version_name, buffer_size);
|
|
|
|
ok = 1;
|
2020-04-13 03:37:46 +00:00
|
|
|
}
|
|
|
|
else
|
2020-04-14 05:23:01 +00:00
|
|
|
ok = -1;
|
2020-04-13 03:37:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HMS_end(hms);
|
|
|
|
|
2020-04-14 05:23:01 +00:00
|
|
|
return ok;
|
2020-04-13 03:37:46 +00:00
|
|
|
}
|
2020-04-28 21:21:57 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
HMS_set_api (char *api)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
I_lock_mutex(&hms_api_mutex);
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
free(hms_api);
|
|
|
|
hms_api = api;
|
|
|
|
}
|
|
|
|
#ifdef HAVE_THREADS
|
|
|
|
I_unlock_mutex(hms_api_mutex);
|
|
|
|
#endif
|
|
|
|
}
|
2020-05-13 02:22:15 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
MasterServer_Debug_OnChange (void)
|
|
|
|
{
|
|
|
|
/* TODO: change to 'latest-log.txt' for log files revision. */
|
|
|
|
if (cv_masterserver_debug.value)
|
|
|
|
CONS_Printf("Master server debug messages will appear in log.txt\n");
|
|
|
|
}
|