]> icculus.org git repositories - taylor/freespace2.git/blob - src/network/stand_server.cpp
add some basic security for standalone admin interface
[taylor/freespace2.git] / src / network / stand_server.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9
10
11 #include "pstypes.h"
12 #include "osregistry.h"
13 #include "multi_options.h"
14 #include "gamesequence.h"
15 #include "timer.h"
16 #include "version.h"
17 #include "multi.h"
18 #include "stand_server.h"
19 #include "multi_pmsg.h"
20 #include "multi_endgame.h"
21 #include "multimsgs.h"
22 #include "multiutil.h"
23 #include "freespace.h"
24 #include "missiongoals.h"
25 #include "cmdline.h"
26 #include "multi_kick.h"
27 #include "multi_fstracker.h"
28 #include "osregistry.h"
29
30 #include <libwebsockets.h>
31 #include <string>
32 #include <vector>
33
34
35
36 static std::string Standalone_debug_state = "";
37 static std::string Standalone_ping_str;
38 static std::string Standalone_player_info;
39 static std::string Standalone_message;
40 static std::string Standalone_pinfo_active_player;
41 static std::string Standalone_mission_name = "";
42 static std::string Standalone_mission_time = "";
43 static std::string Standalone_netgame_info;
44 static std::string Standalone_mission_goals = "";
45 static std::string Standalone_popup_title;
46 static std::string Standalone_popup_field1 = "";
47 static std::string Standalone_popup_field2 = "";
48 static float Standalone_fps = 0.0f;
49
50 #define STANDALONE_MAX_BAN              50
51 static std::vector<std::string> Standalone_ban_list;
52
53 #define STD_STATS_UPDATE_TIME           500             // ms between updating player stats
54 #define STD_NG_UPDATE_TIME                      1500    // ms between updating netgame information
55 #define STD_PING_UPDATE_TIME            1000    // ms between updating pings
56 #define STD_FPS_UPDATE_TIME                     250             // ms between fps updates
57
58 static int Standalone_stats_stamp = -1;
59 static int Standalone_ng_stamp = -1;
60 static int Standalone_ping_stamp = -1;
61 static int Standalone_fps_stamp = -1;
62
63 static int Standalone_update_flags = 0;
64
65 #define STD_UFLAG_DEBUG_STATE           (1<<0)
66 #define STD_UFLAG_TITLE                         (1<<1)
67 #define STD_UFLAG_CONN                          (1<<2)
68 #define STD_UFLAG_RESET                         (1<<3)
69
70 #define STD_UFLAG_SERVER_NAME           (1<<4)
71 #define STD_UFLAG_HOST_PASS                     (1<<5)
72 #define STD_UFLAG_SET_PING                      (1<<6)
73
74 #define STD_UFLAG_FPS                           (1<<7)
75 #define STD_UFLAG_MISSION_NAME          (1<<8)
76 #define STD_UFLAG_MISSION_TIME          (1<<9)
77 #define STD_UFLAG_NETGAME_INFO          (1<<10)
78 #define STD_UFLAG_MISSION_GOALS         (1<<11)
79
80 #define STD_UFLAG_PLAYER_INFO           (1<<12)
81
82 #define STD_UFLAG_S_MESSAGE                     (1<<13)
83
84 #define STD_UFLAG_POPUP                         (1<<14)         // DO NOT INCLUDE IN STD_UFLAG_ALL!!
85
86 #define STD_UFLAG_GENERAL                       (STD_UFLAG_DEBUG_STATE|STD_UFLAG_TITLE|STD_UFLAG_RESET)
87 #define STD_UFLAG_TAB_SERVER            (STD_UFLAG_SERVER_NAME|STD_UFLAG_HOST_PASS|STD_UFLAG_CONN|STD_UFLAG_SET_PING)
88 #define STD_UFLAG_TAB_MULTI                     (STD_UFLAG_FPS|STD_UFLAG_MISSION_NAME|STD_UFLAG_MISSION_TIME|STD_UFLAG_NETGAME_INFO|STD_UFLAG_MISSION_GOALS)
89 #define STD_UFLAG_TAB_PLAYER            (STD_UFLAG_PLAYER_INFO)
90 #define STD_UFLAG_TAB_GS                        (STD_UFLAG_S_MESSAGE)
91
92 #define STD_UFLAG_ALL                           (STD_UFLAG_GENERAL|STD_UFLAG_TAB_SERVER|STD_UFLAG_TAB_MULTI|STD_UFLAG_TAB_PLAYER|STD_UFLAG_TAB_GS)
93
94
95 static lws_context *stand_context = NULL;
96
97
98 static int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
99 {
100         bool try_reuse = false;
101
102         switch (reason) {
103                 case LWS_CALLBACK_HTTP: {
104                         if (len < 1) {
105                                 lws_return_http_status(wsi, HTTP_STATUS_BAD_REQUEST, NULL);
106                                 try_reuse = true;
107
108                                 break;
109                         }
110
111                         int     ret = lws_serve_http_file(wsi, "./standalone.html", "text/html", NULL, 0);
112
113                         if ( (ret < 0) || ((ret > 0) && lws_http_transaction_completed(wsi)) ) {
114                                 // error or can't reuse connection, close the socket
115                                 return -1;
116                         }
117
118                         break;
119                 }
120
121                 case LWS_CALLBACK_HTTP_BODY_COMPLETION: {
122                         lws_return_http_status(wsi, HTTP_STATUS_OK, NULL);
123                         try_reuse = true;
124
125                         break;
126                 }
127
128                 case LWS_CALLBACK_HTTP_FILE_COMPLETION: {
129                         try_reuse = true;
130
131                         break;
132                 }
133
134                 default:
135                         break;
136         }
137
138         if (try_reuse) {
139                 if (lws_http_transaction_completed(wsi)) {
140                         return -1;
141                 }
142         }
143
144         return 0;
145 }
146
147 static int callback_standalone(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
148 {
149         #define MAX_BUF_SIZE    1050
150         unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + MAX_BUF_SIZE + LWS_SEND_BUFFER_POST_PADDING];
151         unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
152         int rval;
153         int size;
154
155         switch (reason) {
156                 case LWS_CALLBACK_ESTABLISHED: {
157                         std_reset_standalone_gui();
158
159                         break;
160                 }
161
162                 case LWS_CALLBACK_SERVER_WRITEABLE: {
163                         // RESET: *must* come first
164                         if (Standalone_update_flags & STD_UFLAG_RESET) {
165                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "reset");
166
167                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
168
169                                 if (rval < size) {
170                                         lwsl_err("ERROR sending reset command!\n");
171                                         return -1;
172                                 }
173
174                                 Standalone_update_flags &= ~STD_UFLAG_RESET;
175
176                                 lws_callback_on_writable(wsi);
177
178                                 break;
179                         }
180
181                         // general messages
182                         if (Standalone_update_flags & STD_UFLAG_TITLE) {
183                                 size = SDL_snprintf((char *)p, 64, "T:%s %d.%02d.%02d", XSTR("FreeSpace Standalone", 935), FS_VERSION_MAJOR, FS_VERSION_MINOR, FS_VERSION_BUILD);
184
185                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
186
187                                 if (rval < size) {
188                                         lwsl_err("ERROR sending title string!\n");
189                                         return -1;
190                                 }
191
192                                 Standalone_update_flags &= ~STD_UFLAG_TITLE;
193
194                                 lws_callback_on_writable(wsi);
195
196                                 break;
197                         }
198
199                         if (Standalone_update_flags & STD_UFLAG_DEBUG_STATE) {
200                                 size = SDL_snprintf((char *)p, 32, "D:%s", Standalone_debug_state.c_str());
201
202                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
203
204                                 if (rval < size) {
205                                         lwsl_err("ERROR sending debug state!\n");
206                                         return -1;
207                                 }
208
209                                 Standalone_update_flags &= ~STD_UFLAG_DEBUG_STATE;
210
211                                 lws_callback_on_writable(wsi);
212
213                                 break;
214                         }
215
216                         if (Standalone_update_flags & STD_UFLAG_POPUP) {
217                                 if ( !Standalone_popup_title.empty() ) {
218                                         size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "popup %s;%s;%s", Standalone_popup_title.c_str(), Standalone_popup_field1.c_str(), Standalone_popup_field2.c_str());
219                                 } else {
220                                         size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "popup ");
221                                 }
222
223                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
224
225                                 if (rval < size) {
226                                         lwsl_err("ERROR sending popup!\n");
227                                         return -1;
228                                 }
229
230                                 Standalone_update_flags &= ~STD_UFLAG_POPUP;
231
232                                 lws_callback_on_writable(wsi);
233
234                                 break;
235                         }
236
237                         // server tab
238                         if (Standalone_update_flags & STD_UFLAG_SERVER_NAME) {
239                                 size = SDL_snprintf((char *)p, MAX_GAMENAME_LEN, "S:name %s", Netgame.name);
240
241                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
242
243                                 if (rval < size) {
244                                         lwsl_err("ERROR sending server name!\n");
245                                         return -1;
246                                 }
247
248                                 Standalone_update_flags &= ~STD_UFLAG_SERVER_NAME;
249
250                                 lws_callback_on_writable(wsi);
251
252                                 break;
253                         }
254
255                         if (Standalone_update_flags & STD_UFLAG_HOST_PASS) {
256                                 size = SDL_snprintf((char *)p, STD_PASSWD_LEN, "S:pass %s", Multi_options_g.std_passwd);
257
258                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
259
260                                 if (rval < size) {
261                                         lwsl_err("ERROR sending host password!\n");
262                                         return -1;
263                                 }
264
265                                 Standalone_update_flags &= ~STD_UFLAG_HOST_PASS;
266
267                                 lws_callback_on_writable(wsi);
268
269                                 break;
270                         }
271
272                         if (Standalone_update_flags & STD_UFLAG_CONN) {
273                                 std::string conn_str;
274                                 char ip_address[60];
275
276                                 conn_str.reserve(1024);
277
278                                 for (int i = 0; i < MAX_PLAYERS; i++) {
279                                         net_player *np = &Net_players[i];
280
281                                         if ( MULTI_CONNECTED((*np)) && (Net_player != np) ) {
282                                                 conn_str.append(np->player->callsign);
283                                                 conn_str.append(",");
284
285                                                 psnet_addr_to_string(ip_address, SDL_arraysize(ip_address), &np->p_info.addr);
286                                                 conn_str.append(ip_address);
287                                                 conn_str.append(",");
288
289                                                 if (np->s_info.ping.ping_avg > -1) {
290                                                         if (np->s_info.ping.ping_avg >= 1000) {
291                                                                 SDL_snprintf(ip_address, SDL_arraysize(ip_address), "%s", XSTR("> 1 sec", 914));
292                                                         } else {
293                                                                 SDL_snprintf(ip_address, SDL_arraysize(ip_address), "%d%s", np->s_info.ping.ping_avg, XSTR(" ms", 915));
294                                                         }
295                                                 }
296
297                                                 conn_str.append(";");
298                                         }
299                                 }
300
301                                 SDL_assert(conn_str.length() < 1024);
302
303                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "S:conn %s", conn_str.c_str());
304
305                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
306
307                                 if (rval < size) {
308                                         lwsl_err("ERROR sending connetions!\n");
309                                         return -1;
310                                 }
311
312                                 Standalone_update_flags &= ~STD_UFLAG_CONN;
313
314                                 lws_callback_on_writable(wsi);
315
316                                 break;
317                         }
318
319                         if ( (Standalone_update_flags & STD_UFLAG_SET_PING) && !Standalone_ping_str.empty() ) {
320                                 SDL_assert(Standalone_ping_str.length() < 1024);
321
322                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "S:ping %s", Standalone_ping_str.c_str());
323
324                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
325
326                                 if (rval < size) {
327                                         lwsl_err("ERROR sending conn ping!\n");
328                                         return -1;
329                                 }
330
331                                 Standalone_ping_str.clear();
332                                 Standalone_update_flags &= ~ STD_UFLAG_SET_PING;
333
334                                 lws_callback_on_writable(wsi);
335
336                                 break;
337                         }
338
339                         // multi-player tab
340                         if (Standalone_update_flags & STD_UFLAG_MISSION_NAME) {
341                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "M:name %s", Standalone_mission_name.c_str());
342
343                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
344
345                                 if (rval < size) {
346                                         lwsl_err("ERROR sending mission name!\n");
347                                         return -1;
348                                 }
349
350                                 Standalone_update_flags &= ~STD_UFLAG_MISSION_NAME;
351
352                                 lws_callback_on_writable(wsi);
353
354                                 break;
355                         }
356
357                         if (Standalone_update_flags & STD_UFLAG_MISSION_TIME) {
358                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "M:time %s", Standalone_mission_time.c_str());
359
360                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
361
362                                 if (rval < size) {
363                                         lwsl_err("ERROR sending mission time!\n");
364                                         return -1;
365                                 }
366
367                                 Standalone_update_flags &= ~STD_UFLAG_MISSION_TIME;
368
369                                 lws_callback_on_writable(wsi);
370
371                                 break;
372                         }
373
374                         if (Standalone_update_flags & STD_UFLAG_NETGAME_INFO) {
375                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "M:info %s", Standalone_netgame_info.c_str());
376
377                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
378
379                                 if (rval < size) {
380                                         lwsl_err("ERROR sending netgame info!\n");
381                                         return -1;
382                                 }
383
384                                 Standalone_update_flags &= ~STD_UFLAG_NETGAME_INFO;
385
386                                 lws_callback_on_writable(wsi);
387
388                                 break;
389                         }
390
391                         if (Standalone_update_flags & STD_UFLAG_FPS) {
392                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "M:fps %.1f", Standalone_fps);
393
394                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
395
396                                 if (rval < size) {
397                                         lwsl_err("ERROR sending fps!\n");
398                                         return -1;
399                                 }
400
401                                 Standalone_update_flags &= ~STD_UFLAG_FPS;
402
403                                 lws_callback_on_writable(wsi);
404
405                                 break;
406                         }
407
408                         if ( (Standalone_update_flags & STD_UFLAG_MISSION_GOALS) && !Standalone_mission_goals.empty() ) {
409                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "M:goal %s", Standalone_mission_goals.c_str());
410
411                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
412
413                                 if (rval < size) {
414                                         lwsl_err("ERROR sending mission goals!\n");
415                                         return -1;
416                                 }
417
418                                 Standalone_mission_goals.clear();
419                                 Standalone_update_flags &= ~STD_UFLAG_MISSION_GOALS;
420
421                                 lws_callback_on_writable(wsi);
422
423                                 break;
424                         }
425
426                         // player tab
427                         if ( (Standalone_update_flags & STD_UFLAG_PLAYER_INFO) && !Standalone_player_info.empty() ) {
428                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "P:info %s", Standalone_player_info.c_str());
429
430                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
431
432                                 if (rval < size) {
433                                         lwsl_err("ERROR sending player info!\n");
434                                         return -1;
435                                 }
436
437                                 Standalone_player_info.clear();
438                                 Standalone_update_flags &= ~STD_UFLAG_PLAYER_INFO;
439
440                                 lws_callback_on_writable(wsi);
441
442                                 break;
443                         }
444
445                         // god stuff tab
446                         if ( (Standalone_update_flags & STD_UFLAG_S_MESSAGE) && !Standalone_message.empty() ) {
447                                 size = SDL_snprintf((char *)p, MAX_BUF_SIZE, "G:mesg %s", Standalone_message.c_str());
448
449                                 rval = lws_write(wsi, p, size, LWS_WRITE_TEXT);
450
451                                 if (rval < size) {
452                                         lwsl_err("ERROR sending chat message!\n");
453                                         return -1;
454                                 }
455
456                                 Standalone_message.clear();
457                                 Standalone_update_flags &= ~STD_UFLAG_S_MESSAGE;
458
459                                 lws_callback_on_writable(wsi);
460
461                                 break;
462                         }
463
464                         break;
465                 }
466
467                 case LWS_CALLBACK_RECEIVE: {
468                         if (in != NULL && len > 0) {
469                                 const char *msg = (const char *)in;
470                                 char mtype = msg[0];
471
472                                 if ( !SDL_strcmp(msg, "shutdown") ) {
473                                         gameseq_post_event(GS_EVENT_QUIT_GAME);
474                                         return -1;
475                                 }
476
477                                 // server tab
478                                 if (mtype == 'S') {
479                                         if (len >= 7) {
480                                                 if ( !SDL_strncmp(msg+2, "name ", 5) ) {
481                                                         SDL_strlcpy(Netgame.name, msg+7, SDL_arraysize(Netgame.name));
482                                                         SDL_strlcpy(Multi_options_g.std_pname, Netgame.name, SDL_arraysize(Multi_options_g.std_pname));
483                                                 } else if ( !SDL_strncmp(msg+2, "pass ", 5) ) {
484                                                         SDL_strlcpy(Multi_options_g.std_passwd, msg+7, SDL_arraysize(Multi_options_g.std_passwd));
485                                                 } else if ( !SDL_strncmp(msg+2, "kick ", 5) ) {
486                                                         char ip_string[60];
487
488                                                         for (int i = 0; i < MAX_PLAYERS; i++) {
489                                                                 if ( MULTI_CONNECTED(Net_players[i]) ) {
490                                                                         psnet_addr_to_string(ip_string, SDL_arraysize(ip_string), &Net_players[i].p_info.addr);
491
492                                                                         if ( !SDL_strcmp(msg+7, ip_string) ) {
493                                                                                 multi_kick_player(i, 0);
494
495                                                                                 break;
496                                                                         }
497                                                                 }
498                                                         }
499                                                 }
500                                         }
501                                 }
502                                 // multi-player tab
503                                 else if (mtype == 'M') {
504                                         if (len >= 6) {
505                                                 if ( !SDL_strncmp(msg+2, "fps ", 4) ) {
506                                                         int fps = SDL_atoi(msg+6);
507
508                                                         Multi_options_g.std_framecap = fps;
509                                                 }
510                                         }
511                                 }
512                                 // player info tab
513                                 else if (mtype == 'P') {
514                                         if (len >= 7) {
515                                                 if ( !SDL_strncmp(msg+2, "info ", 5) ) {
516                                                         int i;
517
518                                                         for (i = 0; i < MAX_PLAYERS; i++) {
519                                                                 net_player *np = &Net_players[i];
520
521                                                                 if ( MULTI_CONNECTED((*np)) && (Net_player != np) ) {
522                                                                         if ( !SDL_strcmp(msg+7, np->player->callsign) ) {
523                                                                                 Standalone_pinfo_active_player = msg+7;
524                                                                                 std_pinfo_display_player_info(np);
525
526                                                                                 break;
527                                                                         }
528                                                                 }
529                                                         }
530
531                                                         if (i == MAX_PLAYERS) {
532                                                                 Standalone_pinfo_active_player.clear();
533                                                         }
534                                                 }
535                                         }
536                                 }
537                                 // god stuff tab
538                                 else if (mtype == 'G') {
539                                         if (len >= 7) {
540                                                 if ( !SDL_strncmp(msg+2, "smsg ", 5) ) {
541                                                         char txt[256];
542
543                                                         SDL_strlcpy(txt, msg+7, SDL_arraysize(txt));
544
545                                                         if (SDL_strlen(txt) > 0) {
546                                                                 send_game_chat_packet(Net_player, txt, MULTI_MSG_ALL, NULL);
547
548                                                                 std_add_chat_text(txt, MY_NET_PLAYER_NUM, 1);
549                                                         }
550                                                 } else if ( !SDL_strcmp(msg+2, "mrefresh") ) {
551                                                         if (MULTI_IS_TRACKER_GAME) {
552                                                                 cf_delete(MULTI_VALID_MISSION_FILE, CF_TYPE_DATA);
553
554                                                                 multi_update_valid_missions();
555                                                         }
556                                                 }
557                                         }
558                                 }
559                         }
560
561                         break;
562                 }
563
564                 default:
565                         break;
566         }
567
568         return 0;
569 }
570
571 static struct lws_protocols stand_protocols[] = {
572         {
573                 "http-only",
574                 callback_http,
575                 0,
576                 0
577         },
578         {
579                 "standalone",
580                 callback_standalone,
581                 0,
582                 0
583         },
584         // terminator
585         {
586                 NULL,
587                 NULL,
588                 0,
589                 0
590         }
591 };
592
593 static void std_lws_logger(int level, const char *line)
594 {
595         if (level & (LLL_WARN|LLL_ERR)) {
596                 mprintf(("STD: %s\n", line));
597         } else if (level & LLL_NOTICE) {
598                 nprintf(("lws", "STD: %s\n", line));
599         }
600 }
601
602
603
604
605 void std_deinit_standalone()
606 {
607         if (stand_context) {
608                 lws_cancel_service(stand_context);
609                 lws_context_destroy(stand_context);
610                 stand_context = NULL;
611         }
612 }
613
614 void std_init_standalone()
615 {
616         struct lws_context_creation_info info;
617
618         if (stand_context) {
619                 return;
620         }
621
622         SDL_zero(info);
623
624         // basic security measure for admin interface
625         //   "1" bind to loopback iface only *default*
626         //   "0" bind to any/all
627         //   any other value should be ip or iface name to bind
628         //   (invalid values will trigger error)
629
630         const char *stand_iface = os_config_read_string("Network", "RestrictStandAdmin", "1");
631
632         if ( !SDL_strcmp(stand_iface, "1") ) {
633                 info.iface = "127.0.0.1";
634         } else if ( !SDL_strcmp(stand_iface, "0") ) {
635                 info.iface = NULL;
636         } else {
637                 info.iface = stand_iface;
638         }
639
640         info.port = Multi_options_g.port;
641
642         info.protocols = stand_protocols;
643
644         info.gid = -1;
645         info.uid = -1;
646
647         info.ka_time = 0;
648         info.ka_probes = 0;
649         info.ka_interval = 0;
650
651         lws_set_log_level(LLL_ERR|LLL_WARN|LLL_NOTICE, std_lws_logger);
652
653         stand_context = lws_create_context(&info);
654
655         if (stand_context == NULL) {
656                 Error(LOCATION, "Unable to initialize standalone server!");
657         }
658
659         atexit(std_deinit_standalone);
660
661         // turn off all sound and music
662         Cmdline_freespace_no_sound = 1;
663         Cmdline_freespace_no_music = 1;
664
665         std_reset_standalone_gui();
666
667         std_multi_update_netgame_info_controls();
668 }
669
670 void std_do_gui_frame()
671 {
672         // maybe update selected player stats
673         if ( ((Standalone_stats_stamp == -1) || timestamp_elapsed(Standalone_stats_stamp)) && !Standalone_pinfo_active_player.empty() ) {
674                 Standalone_stats_stamp = timestamp(STD_STATS_UPDATE_TIME);
675
676                 for (int i = 0; i < MAX_PLAYERS; i++) {
677                         net_player *np = &Net_players[i];
678
679                         if ( MULTI_CONNECTED((*np)) && (Net_player != np) ) {
680                                 if ( !SDL_strcmp(Standalone_pinfo_active_player.c_str(), np->player->callsign) ) {
681                                         std_pinfo_display_player_info(np);
682
683                                         break;
684                                 }
685                         }
686                 }
687         }
688
689         // maybe update netgame info
690         if ( (Standalone_ng_stamp == -1) || timestamp_elapsed(Standalone_ng_stamp) ) {
691                 Standalone_ng_stamp = timestamp(STD_NG_UPDATE_TIME);
692
693                 std_multi_update_netgame_info_controls();
694         }
695
696         // update connection ping times
697         if ( ((Standalone_ping_stamp == -1) || timestamp_elapsed(Standalone_ping_stamp)) && !Standalone_ping_str.empty() ) {
698                 Standalone_ping_stamp = timestamp(STD_PING_UPDATE_TIME);
699                 Standalone_update_flags |= STD_UFLAG_SET_PING;
700         }
701
702         if (Standalone_update_flags) {
703                 lws_callback_on_writable_all_protocol(stand_context, &stand_protocols[1]);
704         }
705
706         lws_service(stand_context, 0);
707 }
708
709 void std_debug_set_standalone_state_string(const char *str)
710 {
711         Standalone_debug_state = str;
712
713         Standalone_update_flags |= STD_UFLAG_DEBUG_STATE;
714 }
715
716 void std_connect_set_gamename(const char *name)
717 {
718         if (name == NULL) {
719                 // if a permanent name exists, use that instead of the default
720                 if ( SDL_strlen(Multi_options_g.std_pname) ) {
721                         SDL_strlcpy(Netgame.name, Multi_options_g.std_pname, SDL_arraysize(Netgame.name));
722                 } else {
723                         SDL_strlcpy(Netgame.name, XSTR("Standalone Server", 916), SDL_arraysize(Netgame.name));
724                 }
725         } else {
726                 SDL_strlcpy(Netgame.name, name, SDL_arraysize(Netgame.name));
727         }
728
729         Standalone_update_flags |= STD_UFLAG_SERVER_NAME;
730 }
731
732 int std_connect_set_connect_count()
733 {
734         int count = 0;
735
736         for (int i = 0; i < MAX_PLAYERS; i++) {
737                 if (MULTI_CONNECTED(Net_players[i]) && (Net_player != &Net_players[i]) ) {
738                         count++;
739                 }
740         }
741
742         return count;
743 }
744
745 void std_add_player(net_player *p)
746 {
747         Standalone_update_flags |= STD_UFLAG_CONN;
748
749         // check to see if this guy is the host
750         std_connect_set_host_connect_status();
751 }
752
753 int std_remove_player(net_player *p)
754 {
755         int count;
756
757         Standalone_update_flags |= STD_UFLAG_CONN;
758
759         // update the host connect count
760         std_connect_set_host_connect_status();
761
762         // update the currently connected players
763         count = std_connect_set_connect_count();
764
765         if (count == 0) {
766                 multi_quit_game(PROMPT_NONE);
767                 return 1;
768         }
769
770         return 0;
771 }
772
773 void std_update_player_ping(net_player *p)
774 {
775         char ip_address[60];
776
777         if (p->s_info.ping.ping_avg > -1) {
778                 psnet_addr_to_string(ip_address, SDL_arraysize(ip_address), &p->p_info.addr);
779
780                 // only add it if address isn't already queued up
781                 if (Standalone_ping_str.find(ip_address) == std::string::npos) {
782                         Standalone_ping_str.append(ip_address);
783
784                         if (p->s_info.ping.ping_avg > 1000) {
785                                 SDL_snprintf(ip_address, SDL_arraysize(ip_address), ",%s;", XSTR("> 1 sec", 914));
786                         } else {
787                                 SDL_snprintf(ip_address, SDL_arraysize(ip_address), ",%d%s;", p->s_info.ping.ping_avg, XSTR(" ms", 915));
788                         }
789
790                         Standalone_ping_str.append(ip_address);
791                 }
792         }
793 }
794
795 void std_pinfo_display_player_info(net_player *p)
796 {
797         char sml_ping[30];
798
799         Standalone_player_info.clear();
800         Standalone_player_info.reserve(256);
801
802         // ship type
803         Standalone_player_info.append(Ship_info[p->p_info.ship_class].name);
804         Standalone_player_info.append(";");
805
806         // avg ping time
807         if (p->s_info.ping.ping_avg > 1000) {
808                 SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%s", XSTR("> 1 sec", 914));
809         } else {
810                 SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d%s", p->s_info.ping.ping_avg, XSTR(" ms", 915));
811         }
812
813         Standalone_player_info.append(sml_ping);
814         Standalone_player_info.append(";");
815
816         scoring_struct *ptr = &p->player->stats;
817
818         // all-time stats
819         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_fired);
820         Standalone_player_info.append(sml_ping);
821         Standalone_player_info.append(",");
822         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_hit);
823         Standalone_player_info.append(sml_ping);
824         Standalone_player_info.append(",");
825         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_bonehead_hits);
826         Standalone_player_info.append(sml_ping);
827         Standalone_player_info.append(",");
828         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_fired ? (int)(100.0f * ((float)ptr->p_shots_hit / (float)ptr->p_shots_fired)) : 0);
829         Standalone_player_info.append(sml_ping);
830         Standalone_player_info.append(",");
831         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->p_shots_fired ? (int)(100.0f * ((float)ptr->p_bonehead_hits / (float)ptr->p_shots_fired)) : 0);
832         Standalone_player_info.append(sml_ping);
833         Standalone_player_info.append(",");
834         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_fired);
835         Standalone_player_info.append(sml_ping);
836         Standalone_player_info.append(",");
837         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_hit);
838         Standalone_player_info.append(sml_ping);
839         Standalone_player_info.append(",");
840         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_bonehead_hits);
841         Standalone_player_info.append(sml_ping);
842         Standalone_player_info.append(",");
843         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_fired ? (int)(100.0f * ((float)ptr->s_shots_hit / (float)ptr->s_shots_fired)) : 0);
844         Standalone_player_info.append(sml_ping);
845         Standalone_player_info.append(",");
846         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->s_shots_fired ? (int)(100.0f * ((float)ptr->s_bonehead_hits / (float)ptr->s_shots_fired)) : 0);
847         Standalone_player_info.append(sml_ping);
848         Standalone_player_info.append(",");
849         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->assists);
850         Standalone_player_info.append(sml_ping);
851         Standalone_player_info.append(";");     // <- end of block
852
853         // mission stats
854         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_fired);
855         Standalone_player_info.append(sml_ping);
856         Standalone_player_info.append(",");
857         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_hit);
858         Standalone_player_info.append(sml_ping);
859         Standalone_player_info.append(",");
860         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_bonehead_hits);
861         Standalone_player_info.append(sml_ping);
862         Standalone_player_info.append(",");
863         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_fired ? (int)(100.0f * ((float)ptr->mp_shots_hit / (float)ptr->mp_shots_fired)) : 0);
864         Standalone_player_info.append(sml_ping);
865         Standalone_player_info.append(",");
866         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->mp_shots_fired ? (int)(100.0f * ((float)ptr->mp_bonehead_hits / (float)ptr->mp_shots_fired)) : 0);
867         Standalone_player_info.append(sml_ping);
868         Standalone_player_info.append(",");
869         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_fired);
870         Standalone_player_info.append(sml_ping);
871         Standalone_player_info.append(",");
872         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_hit);
873         Standalone_player_info.append(sml_ping);
874         Standalone_player_info.append(",");
875         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_bonehead_hits);
876         Standalone_player_info.append(sml_ping);
877         Standalone_player_info.append(",");
878         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_fired ? (int)(100.0f * ((float)ptr->ms_shots_hit / (float)ptr->ms_shots_fired)) : 0);
879         Standalone_player_info.append(sml_ping);
880         Standalone_player_info.append(",");
881         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->ms_shots_fired ? (int)(100.0f * ((float)ptr->ms_bonehead_hits / (float)ptr->ms_shots_fired)) : 0);
882         Standalone_player_info.append(sml_ping);
883         Standalone_player_info.append(",");
884         SDL_snprintf(sml_ping, SDL_arraysize(sml_ping), "%d", ptr->m_assists);
885         Standalone_player_info.append(sml_ping);
886
887         Standalone_update_flags |= STD_UFLAG_PLAYER_INFO;
888 }
889
890 void std_add_chat_text(const char *text, int player_index, int add_id)
891 {
892         char id[32];
893
894         if ( (player_index < 0) || (player_index >= MAX_PLAYERS) ) {
895                 return;
896         }
897
898         // format the chat text nicely
899         if (add_id) {
900                 if ( MULTI_STANDALONE(Net_players[player_index]) ) {
901                         SDL_snprintf(id, SDL_arraysize(id), XSTR("<SERVER> %s", 924), "");
902                 } else {
903                         SDL_snprintf(id, SDL_arraysize(id), "%s: ", Net_players[player_index].player->callsign);
904                 }
905
906                 Standalone_message.append(id);
907         }
908
909         Standalone_message.append(text);
910         Standalone_message.append("\n");
911
912         Standalone_update_flags |= STD_UFLAG_S_MESSAGE;
913 }
914
915 void std_reset_timestamps()
916 {
917         // reset the stats update stamp
918         Standalone_stats_stamp = timestamp(STD_STATS_UPDATE_TIME);
919
920         // reset the netgame controls update timestamp
921         Standalone_ng_stamp = timestamp(STD_NG_UPDATE_TIME);
922
923         // reset the ping update stamp
924         Standalone_ping_stamp = timestamp(STD_PING_UPDATE_TIME);
925
926         // reset fps update stamp
927         Standalone_fps_stamp = timestamp(STD_FPS_UPDATE_TIME);
928 }
929
930 void std_add_ban(const char *name)
931 {
932         if ( (name == NULL) || !SDL_strlen(name) ) {
933                 return;
934         }
935
936         if (Standalone_ban_list.size() >= STANDALONE_MAX_BAN) {
937                 return;
938         }
939
940         Standalone_ban_list.push_back(name);
941 }
942
943 int std_player_is_banned(const char *name)
944 {
945         if ( Standalone_ban_list.empty() ) {
946                 return 0;
947         }
948
949         for (size_t i = 0; i < Standalone_ban_list.size(); i++) {
950                 if ( !SDL_strcasecmp(name, Standalone_ban_list[i].c_str()) ) {
951                         return 1;
952                 }
953         }
954
955         return 0;
956 }
957
958 int std_is_host_passwd()
959 {
960         return (SDL_strlen(Multi_options_g.std_passwd) > 0) ? 1 : 0;
961 }
962
963 void std_multi_set_standalone_mission_name(const char *mission_name)
964 {
965         Standalone_mission_name = mission_name;
966         Standalone_update_flags |= STD_UFLAG_MISSION_NAME;
967 }
968
969 void std_multi_set_standalone_missiontime(float mission_time)
970 {
971         char txt[80];
972         char timestr[50];
973         fix m_time = fl2f(mission_time);
974
975         // format the time string and set the text
976         game_format_time(m_time, timestr, SDL_arraysize(timestr));
977         SDL_snprintf(txt, SDL_arraysize(txt), "%s  :  %.1f", timestr, mission_time);
978
979         Standalone_mission_time = txt;
980         Standalone_update_flags |= STD_UFLAG_MISSION_TIME;
981 }
982
983 void std_multi_update_netgame_info_controls()
984 {
985         char nginfo[50];
986
987         SDL_snprintf(nginfo, SDL_arraysize(nginfo), "%d,%d,%d,%d", Netgame.max_players, Netgame.options.max_observers, Netgame.security, Netgame.respawn);
988
989         Standalone_netgame_info = nginfo;
990         Standalone_update_flags |= STD_UFLAG_NETGAME_INFO;
991 }
992
993 void std_set_standalone_fps(float fps)
994 {
995         if ( (Standalone_fps_stamp == -1) || timestamp_elapsed(Standalone_fps_stamp) ) {
996                 Standalone_fps_stamp = timestamp(STD_FPS_UPDATE_TIME);
997
998                 Standalone_fps = fps;
999                 Standalone_update_flags |= STD_UFLAG_FPS;
1000         }
1001 }
1002
1003 void std_multi_setup_goal_tree()
1004 {
1005         std::string primary;
1006         std::string secondary;
1007         std::string bonus;
1008         std::string status;
1009
1010         Standalone_mission_goals.clear();
1011
1012         for (int i = 0; i < Num_goals; i++) {
1013                 switch (Mission_goals[i].satisfied) {
1014                         case GOAL_FAILED: {
1015                                 status = "f ";
1016                                 break;
1017                         }
1018
1019                         case GOAL_COMPLETE: {
1020                                 status = "c ";
1021                                 break;
1022                         }
1023
1024                         case GOAL_INCOMPLETE:
1025                         default: {
1026                                 status = "i ";
1027                                 break;
1028                         }
1029                 }
1030
1031                 switch (Mission_goals[i].type & GOAL_TYPE_MASK) {
1032                         case PRIMARY_GOAL: {
1033                                 primary.append(status);
1034                                 primary.append(Mission_goals[i].name);
1035                                 primary.append(",");
1036
1037                                 break;
1038                         }
1039
1040                         case SECONDARY_GOAL: {
1041                                 secondary.append(status);
1042                                 secondary.append(Mission_goals[i].name);
1043                                 secondary.append(",");
1044
1045                                 break;
1046                         }
1047
1048                         case BONUS_GOAL: {
1049                                 bonus.append(status);
1050                                 bonus.append(Mission_goals[i].name);
1051                                 bonus.append(",");
1052
1053                                 break;
1054                         }
1055
1056                         default:
1057                                 break;
1058                 }
1059         }
1060
1061         if ( primary.empty() ) {
1062                 Standalone_mission_goals.append("i none");
1063         } else {
1064                 Standalone_mission_goals.append(primary.substr(0, primary.size()-1));
1065         }
1066
1067         Standalone_mission_goals.append(";");
1068
1069         if ( secondary.empty() ) {
1070                 Standalone_mission_goals.append("i none");
1071         } else {
1072                 Standalone_mission_goals.append(secondary.substr(0, secondary.size()-1));
1073         }
1074
1075         Standalone_mission_goals.append(";");
1076
1077         if ( bonus.empty() ) {
1078                 Standalone_mission_goals.append("i none");
1079         } else {
1080                 Standalone_mission_goals.append(bonus.substr(0, bonus.size()-1));
1081         }
1082
1083         Standalone_update_flags |= STD_UFLAG_MISSION_GOALS;
1084 }
1085
1086 void std_multi_add_goals()
1087 {
1088         std_multi_setup_goal_tree();
1089 }
1090
1091 void std_multi_update_goals()
1092 {
1093         std_multi_setup_goal_tree();
1094 }
1095
1096 void std_reset_standalone_gui()
1097 {
1098         Standalone_stats_stamp = -1;
1099         Standalone_ng_stamp = -1;
1100         Standalone_ping_stamp = -1;
1101         Standalone_fps_stamp = -1;
1102
1103         Standalone_ping_str.clear();
1104         Standalone_player_info.clear();
1105         Standalone_message.clear();
1106         Standalone_pinfo_active_player.clear();
1107         Standalone_mission_name = "";
1108         Standalone_mission_time = "";
1109         Standalone_netgame_info.clear();
1110         Standalone_popup_title.clear();
1111         Standalone_popup_field1 = "";
1112         Standalone_popup_field2 = "";
1113
1114         std_set_standalone_fps(0.0f);
1115         std_multi_set_standalone_missiontime(0.0f);
1116
1117         Standalone_update_flags |= STD_UFLAG_ALL;
1118 }
1119
1120 void std_create_gen_dialog(const char *title)
1121 {
1122         Standalone_popup_title = title;
1123 }
1124
1125 void std_destroy_gen_dialog()
1126 {
1127         Standalone_popup_title.clear();
1128
1129         Standalone_update_flags |= STD_UFLAG_POPUP;
1130 }
1131
1132 void std_gen_set_text(const char *str, int field_num)
1133 {
1134         switch (field_num) {
1135                 case 0:
1136                         Standalone_popup_title = str;
1137                         break;
1138
1139                 case 1:
1140                         Standalone_popup_field1 = str;
1141                         break;
1142
1143                 case 2:
1144                         Standalone_popup_field2 = str;
1145                         break;
1146
1147                 default:
1148                         break;
1149         }
1150
1151         Standalone_update_flags |= STD_UFLAG_POPUP;
1152
1153         // force ws write since do_frame() may not happen until popup is done
1154         lws_callback_on_writable_all_protocol(stand_context, &stand_protocols[1]);
1155         lws_service(stand_context, 0);
1156 }
1157
1158 void std_tracker_notify_login_fail()
1159 {
1160
1161 }
1162
1163 void std_tracker_login()
1164 {
1165         if ( !Multi_options_g.pxo ) {
1166                 return;
1167         }
1168
1169         multi_fs_tracker_init();
1170
1171         if ( !multi_fs_tracker_inited() ) {
1172                 std_tracker_notify_login_fail();
1173                 return;
1174         }
1175
1176         multi_fs_tracker_login_freespace();
1177 }
1178
1179 void std_connect_set_host_connect_status()
1180 {
1181 }