| 1 |
/******************************************************************************* |
| 2 |
* |
| 3 |
* Copyright (c) 2004-2010 Guillaume Cottenceau |
| 4 |
* |
| 5 |
* Portions from Mandriva's stage1. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License version 2, as |
| 9 |
* published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
* |
| 20 |
******************************************************************************/ |
| 21 |
|
| 22 |
/* |
| 23 |
* this file holds network transmission operations. |
| 24 |
* it should be as far away as possible from game considerations |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <stdlib.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <errno.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdarg.h> |
| 32 |
#include <string.h> |
| 33 |
#include <ctype.h> |
| 34 |
#include <sys/socket.h> |
| 35 |
#include <netinet/in.h> |
| 36 |
#include <arpa/inet.h> |
| 37 |
#include <netdb.h> |
| 38 |
#include <poll.h> |
| 39 |
#include <sys/utsname.h> |
| 40 |
#include <sys/time.h> |
| 41 |
#include <pwd.h> |
| 42 |
#include <signal.h> |
| 43 |
#include <regex.h> |
| 44 |
|
| 45 |
#include <glib.h> |
| 46 |
|
| 47 |
#include "game.h" |
| 48 |
#include "tools.h" |
| 49 |
#include "log.h" |
| 50 |
#include "net.h" |
| 51 |
|
| 52 |
/* this is set in game.c and used here for answering with |
| 53 |
* the requested command without passing this additional arg */ |
| 54 |
char* current_command; |
| 55 |
|
| 56 |
const int proto_major = 1; |
| 57 |
const int proto_minor = 2; |
| 58 |
|
| 59 |
static char greets_msg_base[] = "SERVER_READY %s %s"; |
| 60 |
static char* servername = NULL; |
| 61 |
static char* serverlanguage = NULL; |
| 62 |
|
| 63 |
static char ok_generic[] = "OK"; |
| 64 |
|
| 65 |
static char fl_client_nolf[] = "NO_LF_WITHIN_TOO_MUCH_DATA (I bet you're not a regular FB client, hu?)"; |
| 66 |
static char fl_client_nulbyte[] = "NUL_BYTE_BEFORE_NEWLINE (I bet you're not a regular FB client, hu?)"; |
| 67 |
static char fl_server_full[] = "SERVER_IS_FULL"; |
| 68 |
static char fl_server_overloaded[] = "SERVER_IS_OVERLOADED"; |
| 69 |
static char fl_client_noactivity[] = "NO_ACTIVITY_WITHIN_GRACETIME"; |
| 70 |
static char fl_client_blacklisted[] = "YOU_ARE_BLACKLISTED"; |
| 71 |
|
| 72 |
static double date_amount_transmitted_reset; |
| 73 |
|
| 74 |
#define DEFAULT_PORT 1511 // a.k.a 0xF 0xB thx misc |
| 75 |
#define DEFAULT_MAX_USERS 255 |
| 76 |
#define DEFAULT_INTERVAL_REREGISTER 60 |
| 77 |
#define DEFAULT_MAX_TRANSMISSION_RATE 100000 |
| 78 |
#define DEFAULT_OUTPUT "INFO" |
| 79 |
#define DEFAULT_GRACETIME 900 |
| 80 |
static int port = DEFAULT_PORT; |
| 81 |
static int max_users = DEFAULT_MAX_USERS; |
| 82 |
int interval_reregister = DEFAULT_INTERVAL_REREGISTER; |
| 83 |
static int max_transmission_rate = DEFAULT_MAX_TRANSMISSION_RATE; |
| 84 |
static int gracetime = DEFAULT_GRACETIME; |
| 85 |
|
| 86 |
static int lan_game_mode = 0; |
| 87 |
|
| 88 |
static int tcp_server_socket; |
| 89 |
static int udp_server_socket = -1; |
| 90 |
|
| 91 |
static int quiet = 0; |
| 92 |
|
| 93 |
static char* external_hostname = "DISTANT_END"; |
| 94 |
static int external_port = -1; |
| 95 |
|
| 96 |
static char* blacklisted_IPs = NULL; |
| 97 |
char* pidfile = NULL; |
| 98 |
char* user_to_switch = NULL; |
| 99 |
|
| 100 |
static char* alert_words_file = NULL; |
| 101 |
GList * alert_words = NULL; |
| 102 |
|
| 103 |
static GList * conns = NULL; |
| 104 |
static GList * conns_prio = NULL; |
| 105 |
|
| 106 |
#define INCOMING_DATA_BUFSIZE 16384 |
| 107 |
static char * incoming_data_buffers[256]; |
| 108 |
static int incoming_data_buffers_count[256]; |
| 109 |
static time_t last_data_in[256]; |
| 110 |
static time_t minute_for_talk_flood[256]; |
| 111 |
int amount_talk_flood[256]; |
| 112 |
static int prio[256]; |
| 113 |
|
| 114 |
/* send line adding the protocol in front of the supplied msg */ |
| 115 |
static ssize_t send_line(int fd, char* msg) |
| 116 |
{ |
| 117 |
static char buf[16384] __attribute__((aligned(4096))); |
| 118 |
int size; |
| 119 |
if (current_command) |
| 120 |
size = snprintf(buf, sizeof(buf), "FB/%d.%d %s: %s\n", proto_major, proto_minor, current_command, msg); |
| 121 |
else |
| 122 |
size = snprintf(buf, sizeof(buf), "FB/%d.%d ???: %s\n", proto_major, proto_minor, msg); |
| 123 |
if (size > sizeof(buf)-1) { |
| 124 |
size = sizeof(buf)-1; |
| 125 |
buf[sizeof(buf)-2] = '\n'; |
| 126 |
} |
| 127 |
if (size > 0) { |
| 128 |
return send(fd, buf, size, MSG_NOSIGNAL); |
| 129 |
} else { |
| 130 |
l2(OUTPUT_TYPE_ERROR, "[%d] Format failure, impossible to send message '%s'", fd, msg); |
| 131 |
return 0; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
ssize_t send_line_log(int fd, char* dest_msg, char* inco_msg) |
| 136 |
{ |
| 137 |
l3(OUTPUT_TYPE_DEBUG, "[%d] %s -> %s", fd, inco_msg, dest_msg); |
| 138 |
return send_line(fd, dest_msg); |
| 139 |
} |
| 140 |
|
| 141 |
ssize_t send_line_log_push(int fd, char* dest_msg) |
| 142 |
{ |
| 143 |
// drop pre-prio messages for connections in prio mode; there can be some remaining uninteresting |
| 144 |
// messages arriving right in between (TALK) |
| 145 |
if (prio[fd]) |
| 146 |
return 0; |
| 147 |
else { |
| 148 |
char * tmp = current_command; |
| 149 |
ssize_t b; |
| 150 |
current_command = "PUSH"; |
| 151 |
l2(OUTPUT_TYPE_DEBUG, "[%d] PUSH %s", fd, dest_msg); |
| 152 |
b = send_line(fd, dest_msg); |
| 153 |
current_command = tmp; |
| 154 |
return b; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
ssize_t send_line_log_push_binary(int fd, char* dest_msg, char* printable_msg) |
| 159 |
{ |
| 160 |
char * tmp = current_command; |
| 161 |
ssize_t b; |
| 162 |
current_command = "PUSH"; |
| 163 |
l2(OUTPUT_TYPE_DEBUG, "[%d] PUSH (binary message) %s", fd, printable_msg); |
| 164 |
b = send_line(fd, dest_msg); |
| 165 |
current_command = tmp; |
| 166 |
return b; |
| 167 |
} |
| 168 |
|
| 169 |
ssize_t send_ok(int fd, char* inco_msg) |
| 170 |
{ |
| 171 |
return send_line_log(fd, ok_generic, inco_msg); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
static void fill_conns_set(gpointer data, gpointer user_data) |
| 176 |
{ |
| 177 |
FD_SET(GPOINTER_TO_INT(data), (fd_set *) user_data); |
| 178 |
} |
| 179 |
|
| 180 |
static int recalculate_list_games = 0; |
| 181 |
static GList * new_conns; |
| 182 |
static int interrupt_loop_processing = 0; |
| 183 |
void conn_terminated(int fd, char* reason) |
| 184 |
{ |
| 185 |
if (g_list_find(new_conns, GINT_TO_POINTER(fd))) { // we can be recursively called if two players disconnect at the exact same time |
| 186 |
l2(OUTPUT_TYPE_CONNECT, "[%d] Closing connection: %s", fd, reason); |
| 187 |
close(fd); |
| 188 |
free(incoming_data_buffers[fd]); |
| 189 |
if (nick[fd] != NULL) { |
| 190 |
free(nick[fd]); |
| 191 |
} |
| 192 |
if (geoloc[fd] != NULL) { |
| 193 |
free(geoloc[fd]); |
| 194 |
} |
| 195 |
free(IP[fd]); |
| 196 |
new_conns = g_list_remove(new_conns, GINT_TO_POINTER(fd)); |
| 197 |
player_part_game(fd); // this is where the recursive call can come from (process_msg_prio with a failed send) |
| 198 |
player_disconnects(fd); |
| 199 |
recalculate_list_games = 1; |
| 200 |
interrupt_loop_processing = 1; |
| 201 |
if (lan_game_mode && g_list_length(new_conns) == 0 && udp_server_socket == -1) { |
| 202 |
l0(OUTPUT_TYPE_INFO, "LAN game mode server exiting on last client exit."); |
| 203 |
exit(EXIT_SUCCESS); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
static int prio_processed; |
| 209 |
static time_t current_time; |
| 210 |
static int need_another_run; |
| 211 |
static void handle_incoming_data_generic(gpointer data, gpointer user_data, int prio) |
| 212 |
{ |
| 213 |
int fd = GPOINTER_TO_INT(data); |
| 214 |
|
| 215 |
if (!interrupt_loop_processing |
| 216 |
&& (FD_ISSET(fd, (fd_set *) user_data) |
| 217 |
|| (incoming_data_buffers_count[fd] > 0 && incoming_data_buffers[fd][incoming_data_buffers_count[fd]-1] == '\n'))) { |
| 218 |
char buf[INCOMING_DATA_BUFSIZE]; |
| 219 |
ssize_t len; |
| 220 |
ssize_t offset = incoming_data_buffers_count[fd]; |
| 221 |
incoming_data_buffers_count[fd] = 0; |
| 222 |
memcpy(buf, incoming_data_buffers[fd], offset); |
| 223 |
len = recv(fd, buf + offset, INCOMING_DATA_BUFSIZE - 1 - offset, MSG_DONTWAIT); |
| 224 |
if (len == -1 && errno != EAGAIN) { |
| 225 |
l2(OUTPUT_TYPE_DEBUG, "[%d] System error on recv: %s", fd, strerror(errno)); |
| 226 |
conn_terminated(fd, "system error on recv"); |
| 227 |
return; |
| 228 |
|
| 229 |
} else if (len == 0) { |
| 230 |
conn_terminated(fd, "peer shutdown"); |
| 231 |
return; |
| 232 |
|
| 233 |
} else { |
| 234 |
|
| 235 |
char* ptr; |
| 236 |
char* eol; |
| 237 |
|
| 238 |
if (len == -1) |
| 239 |
len = 0; |
| 240 |
|
| 241 |
last_data_in[fd] = current_time; |
| 242 |
if (minute_for_talk_flood[fd] != current_time/60) { |
| 243 |
minute_for_talk_flood[fd] = current_time/60; |
| 244 |
amount_talk_flood[fd] = 0; |
| 245 |
} |
| 246 |
|
| 247 |
len += offset; |
| 248 |
// If we don't have a newline, it means we are seeing a partial send. Buffer |
| 249 |
// them, since we can't synchronously wait for newline now or else we'd offer a |
| 250 |
// nice easy shot for DOS (and beside, this would slow down the whole rest). |
| 251 |
if (buf[len-1] != '\n') { |
| 252 |
if (len == INCOMING_DATA_BUFSIZE - 1) { |
| 253 |
send_line_log_push(fd, fl_client_nolf); |
| 254 |
conn_terminated(fd, "too much data without LF"); |
| 255 |
return; |
| 256 |
} |
| 257 |
l2(OUTPUT_TYPE_DEBUG, "[%d] buffering %zd bytes (this is normal)", fd, len); |
| 258 |
memcpy(incoming_data_buffers[fd], buf, len); |
| 259 |
incoming_data_buffers_count[fd] = len; |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
/* string operations will need a NULL conn_terminated string */ |
| 264 |
buf[len] = '\0'; |
| 265 |
ptr = buf; |
| 266 |
|
| 267 |
while (1) { |
| 268 |
if (!(eol = strchr(ptr, '\n'))) { |
| 269 |
// the bad bad guy sent a 0 byte before the \n |
| 270 |
if (!prio) |
| 271 |
send_line_log_push(fd, fl_client_nulbyte); |
| 272 |
conn_terminated(fd, "NUL byte before newline"); |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
if (prio) { |
| 277 |
// prio e.g. in game; we split messages because p and ! must be treated specially (see game::process_msg_prio) |
| 278 |
process_msg_prio(fd, ptr, eol - ptr + 1); |
| 279 |
len -= eol - ptr + 1; |
| 280 |
if (len == 0) { |
| 281 |
prio_processed = 1; |
| 282 |
break; |
| 283 |
} |
| 284 |
ptr = eol + 1; |
| 285 |
|
| 286 |
} else { |
| 287 |
eol[0] = '\0'; |
| 288 |
if (process_msg(fd, ptr)) { |
| 289 |
conn_terminated(fd, "process_msg said to shutdown this connection"); |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
// process_msg > talk (flooding) > conn_terminated |
| 294 |
if (interrupt_loop_processing) |
| 295 |
return; |
| 296 |
|
| 297 |
if (eol + 1 - ptr < len) { |
| 298 |
ssize_t remaining = len - (eol + 1 - ptr); |
| 299 |
l2(OUTPUT_TYPE_DEBUG, "multiple non-prio messages for %d, buffering (%zd bytes)", fd, remaining); |
| 300 |
memcpy(incoming_data_buffers[fd], eol + 1, remaining); |
| 301 |
incoming_data_buffers_count[fd] = remaining; |
| 302 |
need_another_run = 1; |
| 303 |
} |
| 304 |
/* must handle only one message, because we might have multiple and subsequent might need to be |
| 305 |
treated as prio message if the one before is about starting the game */ |
| 306 |
break; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
return; |
| 311 |
} |
| 312 |
if (prio) { |
| 313 |
// 5 seconds is in game gracetime after which player is kicked out |
| 314 |
if (current_time - last_data_in[fd] > 5) { |
| 315 |
conn_terminated(fd, "no activity within gracetime (during game, gracetime of 5 secs)"); |
| 316 |
} |
| 317 |
} else { |
| 318 |
if (current_time - last_data_in[fd] > gracetime) { |
| 319 |
send_line_log_push(fd, fl_client_noactivity); |
| 320 |
conn_terminated(fd, "no activity within gracetime (not during game)"); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
static void handle_incoming_data(gpointer data, gpointer user_data) |
| 326 |
{ |
| 327 |
handle_incoming_data_generic(data, user_data, 0); |
| 328 |
} |
| 329 |
static void handle_incoming_data_prio(gpointer data, gpointer user_data) |
| 330 |
{ |
| 331 |
handle_incoming_data_generic(data, user_data, 1); |
| 332 |
} |
| 333 |
|
| 334 |
static void handle_udp_request(void) |
| 335 |
{ |
| 336 |
static char ok_input_beginning_base[] = "FB/%d."; |
| 337 |
static char * ok_input_beginning = NULL; |
| 338 |
static char ok_input_end[] = " SERVER PROBE"; |
| 339 |
static char fl_unrecognized_base[] = "FB/%d.%d You don't exist, go away.\n"; |
| 340 |
static char * fl_unrecognized = NULL; |
| 341 |
static char ok_answer_base[] = "FB/%d.%d SERVER HERE AT PORT %d"; |
| 342 |
static char * ok_answer = NULL; |
| 343 |
int n; |
| 344 |
char msg[128]; |
| 345 |
struct sockaddr_in client_addr; |
| 346 |
socklen_t client_len = sizeof(client_addr); |
| 347 |
char * answer; |
| 348 |
|
| 349 |
if (!ok_input_beginning) // C sux |
| 350 |
ok_input_beginning = asprintf_(ok_input_beginning_base, proto_major); |
| 351 |
if (!ok_answer) |
| 352 |
ok_answer = asprintf_(ok_answer_base, proto_major, proto_minor, port); |
| 353 |
if (!fl_unrecognized) |
| 354 |
fl_unrecognized = asprintf_(fl_unrecognized_base, proto_major, proto_minor); |
| 355 |
|
| 356 |
memset(msg, 0, sizeof(msg)); |
| 357 |
n = recvfrom(udp_server_socket, msg, sizeof(msg), 0, (struct sockaddr *) &client_addr, &client_len); |
| 358 |
if (n == -1) { |
| 359 |
l1(OUTPUT_TYPE_ERROR, "recvfrom: %s", strerror(errno)); |
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
l2(OUTPUT_TYPE_DEBUG, "UDP server receives %d bytes from %s.", n, inet_ntoa(client_addr.sin_addr)); |
| 364 |
if (strncmp(msg, ok_input_beginning, strlen(ok_input_beginning)) || !strstr(msg, ok_input_end) || (lan_game_mode && g_list_length(conns_prio) > 0)) { |
| 365 |
answer = fl_unrecognized; |
| 366 |
l0(OUTPUT_TYPE_DEBUG, "Unrecognized/full."); |
| 367 |
} else { |
| 368 |
answer = ok_answer; |
| 369 |
l0(OUTPUT_TYPE_DEBUG, "Valid FB server probe, answering."); |
| 370 |
} |
| 371 |
|
| 372 |
if (sendto(udp_server_socket, answer, strlen(answer), 0, (struct sockaddr *) &client_addr, sizeof(client_addr)) != strlen(answer)) { |
| 373 |
l1(OUTPUT_TYPE_ERROR, "sendto: %s", strerror(errno)); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
static char * greets_msg = NULL; |
| 378 |
static char * get_greets_msg(void) |
| 379 |
{ |
| 380 |
if (!greets_msg) { |
| 381 |
greets_msg = asprintf_(greets_msg_base, servername, serverlanguage); |
| 382 |
} |
| 383 |
return greets_msg; |
| 384 |
} |
| 385 |
|
| 386 |
static char * http_get(char* host, int port, char* path); |
| 387 |
static void download_blacklisted_IPs() |
| 388 |
{ |
| 389 |
char* doc = http_get("www.frozen-bubble.org", 80, "/servers/blacklisted_IPs"); |
| 390 |
if (doc) { |
| 391 |
if (blacklisted_IPs) |
| 392 |
free(blacklisted_IPs); |
| 393 |
blacklisted_IPs = asprintf_(",%s", doc); |
| 394 |
free(doc); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
void connections_manager(void) |
| 399 |
{ |
| 400 |
struct sockaddr_in client_addr; |
| 401 |
ssize_t len = sizeof(client_addr); |
| 402 |
struct timeval tv; |
| 403 |
double now, delta, rate; |
| 404 |
|
| 405 |
if (!quiet) |
| 406 |
download_blacklisted_IPs(); |
| 407 |
date_amount_transmitted_reset = get_current_time_exact(); |
| 408 |
|
| 409 |
while (1) { |
| 410 |
int fd; |
| 411 |
int retval; |
| 412 |
fd_set conns_set; |
| 413 |
|
| 414 |
reregister_server_if_needed(); |
| 415 |
|
| 416 |
if (recalculate_list_games) |
| 417 |
calculate_list_games(); |
| 418 |
recalculate_list_games = 0; |
| 419 |
|
| 420 |
if (!need_another_run) { |
| 421 |
FD_ZERO(&conns_set); |
| 422 |
g_list_foreach(conns, fill_conns_set, &conns_set); |
| 423 |
g_list_foreach(conns_prio, fill_conns_set, &conns_set); |
| 424 |
if (tcp_server_socket != -1) |
| 425 |
FD_SET(tcp_server_socket, &conns_set); |
| 426 |
if (udp_server_socket != -1) |
| 427 |
FD_SET(udp_server_socket, &conns_set); |
| 428 |
|
| 429 |
tv.tv_sec = 30; |
| 430 |
tv.tv_usec = 0; |
| 431 |
|
| 432 |
if ((retval = select(FD_SETSIZE, &conns_set, NULL, NULL, &tv)) == -1) { |
| 433 |
l1(OUTPUT_TYPE_ERROR, "select: %s", strerror(errno)); |
| 434 |
exit(EXIT_FAILURE); |
| 435 |
} |
| 436 |
|
| 437 |
// timeout |
| 438 |
if (!retval) |
| 439 |
continue; |
| 440 |
} |
| 441 |
|
| 442 |
current_time = get_current_time(); // a bit of caching |
| 443 |
prio_processed = 0; |
| 444 |
interrupt_loop_processing = 0; |
| 445 |
need_another_run = 0; |
| 446 |
new_conns = g_list_copy(conns_prio); |
| 447 |
g_list_foreach(conns_prio, handle_incoming_data_prio, &conns_set); |
| 448 |
g_list_free(conns_prio); |
| 449 |
conns_prio = new_conns; |
| 450 |
|
| 451 |
// prio has higher priority (astounding statement, eh?) |
| 452 |
if (prio_processed || interrupt_loop_processing) |
| 453 |
continue; |
| 454 |
|
| 455 |
new_conns = g_list_copy(conns); |
| 456 |
g_list_foreach(conns, handle_incoming_data, &conns_set); |
| 457 |
g_list_free(conns); |
| 458 |
conns = new_conns; |
| 459 |
|
| 460 |
if (tcp_server_socket != -1 && FD_ISSET(tcp_server_socket, &conns_set)) { |
| 461 |
if ((fd = accept(tcp_server_socket, (struct sockaddr *) &client_addr, (socklen_t *) &len)) == -1) { |
| 462 |
l1(OUTPUT_TYPE_ERROR, "accept: %s", strerror(errno)); |
| 463 |
continue; |
| 464 |
} |
| 465 |
|
| 466 |
l2(OUTPUT_TYPE_CONNECT, "Accepted connection from %s: fd %d", inet_ntoa(client_addr.sin_addr), fd); |
| 467 |
if (fd > 255 || conns_nb() >= max_users || (lan_game_mode && g_list_length(conns_prio) > 0)) { |
| 468 |
// don't overrun prio in send_line_log_push |
| 469 |
if (fd <= 255) { |
| 470 |
send_line_log_push(fd, fl_server_full); |
| 471 |
} |
| 472 |
l1(OUTPUT_TYPE_INFO, "[%d] Closing connection (server full)", fd); |
| 473 |
close(fd); |
| 474 |
continue; |
| 475 |
} |
| 476 |
|
| 477 |
if (blacklisted_IPs != NULL) { |
| 478 |
char* blacklist_search = asprintf_(",%s", inet_ntoa(client_addr.sin_addr)); |
| 479 |
if (strstr(blacklisted_IPs, blacklist_search) != NULL) { |
| 480 |
send_line_log_push(fd, fl_client_blacklisted); |
| 481 |
l2(OUTPUT_TYPE_INFO, "[%d] Blacklisted client (%s)", fd, inet_ntoa(client_addr.sin_addr)); |
| 482 |
close(fd); |
| 483 |
free(blacklist_search); |
| 484 |
continue; |
| 485 |
} |
| 486 |
free(blacklist_search); |
| 487 |
} |
| 488 |
|
| 489 |
now = get_current_time_exact(); |
| 490 |
delta = now - date_amount_transmitted_reset; |
| 491 |
if (delta > 0) { |
| 492 |
rate = get_reset_amount_transmitted() / delta; |
| 493 |
l1(OUTPUT_TYPE_DEBUG, "Transmission rate: %.2f bytes/sec", rate); |
| 494 |
} else { |
| 495 |
rate = 0; |
| 496 |
} |
| 497 |
date_amount_transmitted_reset = now; |
| 498 |
if (rate > max_transmission_rate) { |
| 499 |
send_line_log_push(fd, fl_server_overloaded); |
| 500 |
l1(OUTPUT_TYPE_INFO, "[%d] Closing connection (maximum transmission rate reached)", fd); |
| 501 |
close(fd); |
| 502 |
continue; |
| 503 |
} |
| 504 |
|
| 505 |
// We've really accepted this new connection. Init data. |
| 506 |
last_data_in[fd] = current_time; |
| 507 |
minute_for_talk_flood[fd] = current_time/60; |
| 508 |
amount_talk_flood[fd] = 0; |
| 509 |
nick[fd] = NULL; |
| 510 |
geoloc[fd] = NULL; |
| 511 |
IP[fd] = strdup_(inet_ntoa(client_addr.sin_addr)); |
| 512 |
prio[fd] = 0; |
| 513 |
remote_proto_minor[fd] = -1; |
| 514 |
send_line_log_push(fd, get_greets_msg()); |
| 515 |
conns = g_list_append(conns, GINT_TO_POINTER(fd)); |
| 516 |
player_connects(fd); |
| 517 |
incoming_data_buffers[fd] = malloc_(sizeof(char) * INCOMING_DATA_BUFSIZE); |
| 518 |
incoming_data_buffers_count[fd] = 0; |
| 519 |
admin_authorized[fd] = streq("127.0.0.1", inet_ntoa(client_addr.sin_addr)); |
| 520 |
recalculate_list_games = 1; |
| 521 |
} |
| 522 |
|
| 523 |
if (udp_server_socket != -1 && FD_ISSET(udp_server_socket, &conns_set)) |
| 524 |
handle_udp_request(); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
int conns_nb(void) |
| 530 |
{ |
| 531 |
return g_list_length(conns_prio) + g_list_length(conns); |
| 532 |
} |
| 533 |
|
| 534 |
void add_prio(int fd) |
| 535 |
{ |
| 536 |
conns_prio = g_list_append(conns_prio, GINT_TO_POINTER(fd)); |
| 537 |
new_conns = g_list_remove(new_conns, GINT_TO_POINTER(fd)); |
| 538 |
prio[fd] = 1; |
| 539 |
if (lan_game_mode && g_list_length(conns_prio) > 0 && udp_server_socket != -1) { |
| 540 |
close(tcp_server_socket); |
| 541 |
close(udp_server_socket); |
| 542 |
tcp_server_socket = udp_server_socket = -1; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
void close_server() { |
| 547 |
if (tcp_server_socket != -1) { |
| 548 |
close(tcp_server_socket); |
| 549 |
} |
| 550 |
if (udp_server_socket != -1) { |
| 551 |
close(udp_server_socket); |
| 552 |
} |
| 553 |
tcp_server_socket = udp_server_socket = -1; |
| 554 |
} |
| 555 |
|
| 556 |
static void help(void) |
| 557 |
{ |
| 558 |
printf("Usage: fb-server [OPTION]...\n"); |
| 559 |
printf("\n"); |
| 560 |
printf(" -a lang set the preferred language of the server (it is just an indication used by players when choosing a server, so that they can chat using their native language - you can choose none with -z)\n"); |
| 561 |
printf(" -A alert_words_file set the file containing alert words (one POSIX regexp by line) - this file is reread when receiving the ADMIN_REREAD command from 127.0.0.1\n"); |
| 562 |
printf(" -c conffile specify the path of the configuration file\n"); |
| 563 |
printf(" -d debug mode: do not daemonize, and log on STDERR rather than through syslog (implies -q)\n"); |
| 564 |
printf(" -f pidfile set the file in which the pid of the daemon must be written\n"); |
| 565 |
printf(" -g gracetime set the gracetime after which a client with no network activity is terminated (in seconds, defaults to %d)\n", DEFAULT_GRACETIME); |
| 566 |
printf(" -h display this help then exits\n"); |
| 567 |
printf(" -H host set the hostname (or IP) as seen from outside (by default, when registering the server to www.frozen-bubble.org, the distant end at IP level will be used)\n"); |
| 568 |
printf(" -i minutes set the minutes interval for reregistering on the master server (except if -q is provided); use 0 to disable the feature; defaults to %d minutes)\n", DEFAULT_INTERVAL_REREGISTER); |
| 569 |
printf(" -l LAN mode: create an UDP server (on port %d) to answer broadcasts of clients discovering where are the servers\n", DEFAULT_PORT); |
| 570 |
printf(" -L LAN/game mode: create an UDP server as above, but limit number of games to 1 (this is for an FB client hosting a LAN server)\n"); |
| 571 |
printf(" -m max_users set the maximum of connected users (defaults to %d, physical maximum 255 in non debug mode)\n", DEFAULT_MAX_USERS); |
| 572 |
printf(" -n name set the server name presented to players (if unset, defaults to hostname)\n"); |
| 573 |
printf(" -o outputtype set the output type; can be DEBUG, CONNECT, INFO, ERROR; each level includes messages of next level; defaults to INFO\n"); |
| 574 |
printf(" -p port set the server port (defaults to %d)\n", DEFAULT_PORT); |
| 575 |
printf(" -P port set the server port as seen from outside (defaults to the port specified with -p)\n"); |
| 576 |
printf(" -q \"quiet\" mode: don't automatically register the server to www.frozen-bubble.org\n"); |
| 577 |
printf(" -t max_transmission_rate set the maximum transmission rate, in bytes per second (defaults to %d)\n", DEFAULT_MAX_TRANSMISSION_RATE); |
| 578 |
printf(" -u user switch daemon to specified user\n"); |
| 579 |
printf(" -z set that there is no preferred language for the server (see -a)\n"); |
| 580 |
} |
| 581 |
|
| 582 |
static void create_udp_server(void) |
| 583 |
{ |
| 584 |
struct sockaddr_in server_addr; |
| 585 |
|
| 586 |
printf("-l: creating UDP server for answering broadcast server discover, on default port %d\n", DEFAULT_PORT); |
| 587 |
|
| 588 |
udp_server_socket = socket(AF_INET, SOCK_DGRAM, 0); |
| 589 |
if (udp_server_socket < 0) { |
| 590 |
perror("socket"); |
| 591 |
exit(EXIT_FAILURE); |
| 592 |
} |
| 593 |
|
| 594 |
server_addr.sin_family = AF_INET; |
| 595 |
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 596 |
server_addr.sin_port = htons(DEFAULT_PORT); |
| 597 |
if (bind(udp_server_socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) { |
| 598 |
perror("bind UDP 1511"); |
| 599 |
exit(EXIT_FAILURE); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
static void cleanup_alert_words(gpointer data, gpointer user_data) |
| 604 |
{ |
| 605 |
regex_t* preg = data; |
| 606 |
regfree(preg); |
| 607 |
free(preg); |
| 608 |
} |
| 609 |
|
| 610 |
static char* read_alert_words(char* file) |
| 611 |
{ |
| 612 |
char buf[512]; |
| 613 |
FILE* f; |
| 614 |
if (alert_words) { |
| 615 |
g_list_foreach(alert_words, cleanup_alert_words, NULL); |
| 616 |
g_list_free(alert_words); |
| 617 |
alert_words = NULL; |
| 618 |
} |
| 619 |
f = fopen(file, "r"); |
| 620 |
if (!f) { |
| 621 |
return asprintf_("Error opening file"); |
| 622 |
} else { |
| 623 |
while (fgets(buf, sizeof(buf), f)) { |
| 624 |
char errbuf[512]; |
| 625 |
int errcode; |
| 626 |
regex_t* preg = (regex_t*) malloc_(sizeof(regex_t)); |
| 627 |
if (buf[strlen(buf)-1] == '\n') |
| 628 |
buf[strlen(buf)-1] = '\0'; |
| 629 |
if ((errcode = regcomp(preg, buf, REG_EXTENDED|REG_ICASE)) != 0) { |
| 630 |
regerror(errcode, preg, errbuf, sizeof(errbuf)); |
| 631 |
free(preg); |
| 632 |
fclose(f); |
| 633 |
return asprintf_("Problem compiling '%s': %s", buf, errbuf); |
| 634 |
} else { |
| 635 |
alert_words = g_list_append(alert_words, preg); |
| 636 |
} |
| 637 |
} |
| 638 |
if (ferror(f)) { |
| 639 |
fclose(f); |
| 640 |
return asprintf_("Error reading file"); |
| 641 |
} |
| 642 |
fclose(f); |
| 643 |
} |
| 644 |
return NULL; |
| 645 |
} |
| 646 |
|
| 647 |
void reread() |
| 648 |
{ |
| 649 |
if (alert_words_file) { |
| 650 |
char* response; |
| 651 |
l1(OUTPUT_TYPE_INFO, "Rereading alert words file '%s'.", alert_words_file); |
| 652 |
response = read_alert_words(alert_words_file); |
| 653 |
if (response) { |
| 654 |
l1(OUTPUT_TYPE_ERROR, "Error rereading alert words file: '%s'\n", response); |
| 655 |
free(response); |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
l0(OUTPUT_TYPE_INFO, "Downloading latest blacklisted IPs."); |
| 660 |
download_blacklisted_IPs(); |
| 661 |
} |
| 662 |
|
| 663 |
static void handle_parameter(char command, char * param) { |
| 664 |
char* response; |
| 665 |
switch (command) { |
| 666 |
case 'a': |
| 667 |
if (streq(param, "af") || streq(param, "ar") || streq(param, "az") || streq(param, "bg") || streq(param, "br") |
| 668 |
|| streq(param, "bs") || streq(param, "ca") || streq(param, "cs") || streq(param, "cy") || streq(param, "da") |
| 669 |
|| streq(param, "de") || streq(param, "el") || streq(param, "en") || streq(param, "eo") || streq(param, "eu") |
| 670 |
|| streq(param, "es") || streq(param, "fi") || streq(param, "fr") || streq(param, "ga") || streq(param, "gl") |
| 671 |
|| streq(param, "hr") || streq(param, "hu") || streq(param, "id") || streq(param, "ir") || streq(param, "is") || streq(param, "it") |
| 672 |
|| streq(param, "ja") || streq(param, "ko") || streq(param, "lt") || streq(param, "lv") || streq(param, "mk") |
| 673 |
|| streq(param, "ms") || streq(param, "nl") || streq(param, "ne") || streq(param, "no") || streq(param, "pl") || streq(param, "pt") |
| 674 |
|| streq(param, "pt_BR") || streq(param, "ro") || streq(param, "ru") || streq(param, "sk") || streq(param, "sl") |
| 675 |
|| streq(param, "sq") || streq(param, "sv") || streq(param, "tg") || streq(param, "tr") || streq(param, "uk") |
| 676 |
|| streq(param, "uz") || streq(param, "vi") || streq(param, "wa") || streq(param, "zh_CN") || streq(param, "zh_TW")) { |
| 677 |
serverlanguage = strdup(param); |
| 678 |
printf("-a: setting preferred language for users of the server to '%s'\n", serverlanguage); |
| 679 |
} else { |
| 680 |
fprintf(stderr, "-a: '%s' not a valid language, ignoring\n", param); |
| 681 |
fprintf(stderr, " valid languages are: af, ar, az, bg, br, bs, ca, cs, cy, da, de, el, en, eo, eu, es, fi, fr, ga, gl, hr, hu, id, ir, is, it, ja, ko, lt, lv, mk, ms, nl, ne, no, pl, pt, pt_BR, ro, ru, sk, sl, sq, sv, tg, tr, uk, uz, vi, wa, zh_CN, zh_TW\n" ); |
| 682 |
} |
| 683 |
break; |
| 684 |
case 'A': |
| 685 |
response = read_alert_words(param); |
| 686 |
if (response) { |
| 687 |
fprintf(stderr, "-A: error reading alert words file '%s': %s\n", param, response); |
| 688 |
free(response); |
| 689 |
} else { |
| 690 |
printf("-A: successfully read alert words file '%s'\n", param); |
| 691 |
alert_words_file = strdup(param); |
| 692 |
} |
| 693 |
break; |
| 694 |
case 'd': |
| 695 |
printf("-d: debug mode on: will not daemonize and will display log messages on STDERR\n"); |
| 696 |
debug_mode = TRUE; |
| 697 |
quiet = TRUE; |
| 698 |
break; |
| 699 |
case 'f': |
| 700 |
printf("-f: will store pid of daemon into file '%s'\n", param); |
| 701 |
pidfile = strdup(param); |
| 702 |
break; |
| 703 |
case 'g': |
| 704 |
gracetime = charstar_to_int(param); |
| 705 |
if (gracetime != 0) |
| 706 |
printf("-g: setting gracetime to '%d' seconds (equals '%d' minutes)\n", gracetime, gracetime/60); |
| 707 |
else { |
| 708 |
fprintf(stderr, "-g: '%s' not convertible to int, ignoring\n", param); |
| 709 |
gracetime = DEFAULT_GRACETIME; |
| 710 |
} |
| 711 |
break; |
| 712 |
case 'h': |
| 713 |
help(); |
| 714 |
exit(EXIT_SUCCESS); |
| 715 |
case 'H': |
| 716 |
printf("-H: setting hostname as seen from outside to '%s'\n", param); |
| 717 |
external_hostname = strdup(param); |
| 718 |
break; |
| 719 |
case 'i': |
| 720 |
interval_reregister = charstar_to_int(param); |
| 721 |
if (interval_reregister > 0) { |
| 722 |
printf("-i: setting interval for re-registering to master server to %s minutes\n", param); |
| 723 |
} else { |
| 724 |
printf("-i: interval for re-registering to master server set to 0, e.g. disabled\n"); |
| 725 |
} |
| 726 |
break; |
| 727 |
case 'l': |
| 728 |
create_udp_server(); |
| 729 |
break; |
| 730 |
case 'L': |
| 731 |
create_udp_server(); |
| 732 |
lan_game_mode = 1; |
| 733 |
break; |
| 734 |
case 'm': |
| 735 |
max_users = charstar_to_int(param); |
| 736 |
if (max_users > 0 && max_users <= 255) |
| 737 |
printf("-m: setting maximum users to '%d'\n", max_users); |
| 738 |
else { |
| 739 |
fprintf(stderr, "-m: '%s' not convertible to int or not in 1..255, ignoring\n", param); |
| 740 |
max_users = DEFAULT_MAX_USERS; |
| 741 |
} |
| 742 |
break; |
| 743 |
case 'n': |
| 744 |
if (strlen(param) > 12) { |
| 745 |
fprintf(stderr, "-n: name is too long, maximum is 12 characters\n"); |
| 746 |
exit(EXIT_FAILURE); |
| 747 |
} else { |
| 748 |
int i; |
| 749 |
for (i = 0; i < strlen(param); i++) { |
| 750 |
if (!((param[i] >= 'a' && param[i] <= 'z') |
| 751 |
|| (param[i] >= 'A' && param[i] <= 'Z') |
| 752 |
|| (param[i] >= '0' && param[i] <= '9') |
| 753 |
|| param[i] == '.' || param[i] == '-')) { |
| 754 |
fprintf(stderr, "-n: name must contain only chars in [a-zA-Z0-9.-]\n"); |
| 755 |
exit(EXIT_FAILURE); |
| 756 |
} |
| 757 |
} |
| 758 |
printf("-n: setting servername to '%s'\n", param); |
| 759 |
servername = strdup(param); |
| 760 |
} |
| 761 |
break; |
| 762 |
case 'o': |
| 763 |
if (streq(param, "DEBUG")) { |
| 764 |
printf("-o: setting output type to DEBUG\n"); |
| 765 |
output_type = OUTPUT_TYPE_DEBUG; |
| 766 |
} else if (streq(param, "CONNECT")) { |
| 767 |
printf("-o: setting output type to CONNECT\n"); |
| 768 |
output_type = OUTPUT_TYPE_CONNECT; |
| 769 |
} else if (streq(param, "INFO")) { |
| 770 |
printf("-o: setting output type to INFO\n"); |
| 771 |
output_type = OUTPUT_TYPE_INFO; |
| 772 |
} else if (streq(param, "ERROR")) { |
| 773 |
printf("-o: setting output type to ERROR\n"); |
| 774 |
output_type = OUTPUT_TYPE_ERROR; |
| 775 |
} |
| 776 |
break; |
| 777 |
case 'p': |
| 778 |
port = charstar_to_int(param); |
| 779 |
if (port != 0) { |
| 780 |
printf("-p: setting port to '%d'\n", port); |
| 781 |
if (external_port == -1) |
| 782 |
external_port = port; |
| 783 |
} else { |
| 784 |
port = DEFAULT_PORT; |
| 785 |
fprintf(stderr, "-p: '%s' not convertible to int, ignoring\n", param); |
| 786 |
} |
| 787 |
break; |
| 788 |
case 'P': |
| 789 |
external_port = charstar_to_int(param); |
| 790 |
if (external_port != 0) |
| 791 |
printf("-P: setting port as seen from outside to '%d'\n", port); |
| 792 |
else { |
| 793 |
fprintf(stderr, "-P: '%s' not convertible to int, ignoring\n", param); |
| 794 |
external_port = -1; |
| 795 |
} |
| 796 |
break; |
| 797 |
case 'q': |
| 798 |
printf("-q: quiet mode: will not register to www.frozen-bubble.org\n"); |
| 799 |
quiet = TRUE; |
| 800 |
break; |
| 801 |
case 't': |
| 802 |
max_transmission_rate = charstar_to_int(param); |
| 803 |
if (max_transmission_rate != 0) |
| 804 |
printf("-t: setting maximum transmission rate to '%d' bytes/sec\n", max_transmission_rate); |
| 805 |
else { |
| 806 |
fprintf(stderr, "-t: '%s' not convertible to int, ignoring\n", param); |
| 807 |
max_transmission_rate = DEFAULT_MAX_TRANSMISSION_RATE; |
| 808 |
} |
| 809 |
break; |
| 810 |
case 'u': |
| 811 |
if (getpwnam(param) != NULL) { |
| 812 |
printf("-u: will switch user of daemon to '%s'\n", param); |
| 813 |
user_to_switch = strdup(param); |
| 814 |
} else { |
| 815 |
fprintf(stderr, "-u: '%s' is not a valid user, ignoring\n", param); |
| 816 |
} |
| 817 |
break; |
| 818 |
case 'z': |
| 819 |
printf("-z: no preferred language for users of the server\n"); |
| 820 |
serverlanguage = "zz"; |
| 821 |
break; |
| 822 |
default: |
| 823 |
fprintf(stderr, "unrecognized option '%c', ignoring\n", command); |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
void sigterm_catcher(int signum) { |
| 828 |
l0(OUTPUT_TYPE_INFO, "Received SIGTERM, terminating."); |
| 829 |
close_server(); |
| 830 |
unregister_server(); |
| 831 |
exit(EXIT_SUCCESS); |
| 832 |
} |
| 833 |
|
| 834 |
void create_server(int argc, char **argv) |
| 835 |
{ |
| 836 |
struct sockaddr_in client_addr; |
| 837 |
int valone = 1; |
| 838 |
|
| 839 |
while (1) { |
| 840 |
int c = getopt(argc, argv, "a:A:c:df:g:hH:i:lLm:n:o:p:P:qt:u:z"); |
| 841 |
if (c == -1) |
| 842 |
break; |
| 843 |
|
| 844 |
if (c == 'c') { |
| 845 |
FILE* f; |
| 846 |
printf("-c: reading configuration file %s\n", optarg); |
| 847 |
f = fopen(optarg, "r"); |
| 848 |
if (!f) { |
| 849 |
fprintf(stderr, "-c: error opening %s, ignoring\n", optarg); |
| 850 |
} else { |
| 851 |
char buf[8192]; |
| 852 |
while (fgets(buf, sizeof(buf), f)) { |
| 853 |
char command, param[256]; |
| 854 |
if (buf[0] == '#' || buf[0] == '\n' || buf[0] == '\r') |
| 855 |
continue; |
| 856 |
if (sscanf(buf, "%c %255s\n", &command, param) == 2) { |
| 857 |
handle_parameter(command, param); |
| 858 |
} else if (sscanf(buf, "%c\n", &command) == 1) { |
| 859 |
handle_parameter(command, NULL); |
| 860 |
} else { |
| 861 |
fprintf(stderr, "-c: ignoring line %s\n", buf); |
| 862 |
} |
| 863 |
} |
| 864 |
if (ferror(f)) { |
| 865 |
fprintf(stderr, "-c: error reading %s\n", optarg); |
| 866 |
} |
| 867 |
fclose(f); |
| 868 |
} |
| 869 |
|
| 870 |
} else { |
| 871 |
handle_parameter(c, optarg); |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
if (external_port == -1) |
| 876 |
external_port = port; |
| 877 |
|
| 878 |
if (!servername) { |
| 879 |
// Fallback to hostname |
| 880 |
char hostname[128]; |
| 881 |
if (!gethostname(hostname, 127)) { |
| 882 |
int i; |
| 883 |
hostname[127] = '\0'; // manpage says "It is unspecified whether the truncated hostname will be null-terminated" |
| 884 |
servername = g_strndup(hostname, 12); |
| 885 |
for (i = 0; i < strlen(servername); i++) { |
| 886 |
if (!((servername[i] >= 'a' && servername[i] <= 'z') |
| 887 |
|| (servername[i] >= 'A' && servername[i] <= 'Z') |
| 888 |
|| (servername[i] >= '0' && servername[i] <= '9') |
| 889 |
|| servername[i] == '-')) { |
| 890 |
servername[i] = '.'; |
| 891 |
} |
| 892 |
} |
| 893 |
printf("Notice: no -n parameter, using hostname as server name presented to players: %s\n", servername); |
| 894 |
} else { |
| 895 |
fprintf(stderr, "gethostname: %s\n", strerror(errno)); |
| 896 |
exit(EXIT_FAILURE); |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
if (!serverlanguage) { |
| 901 |
fprintf(stderr, "Must set the preferred language of users of the server with -a <language> or specify there is none with -z.\n"); |
| 902 |
exit(EXIT_FAILURE); |
| 903 |
} |
| 904 |
|
| 905 |
tcp_server_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 906 |
if (tcp_server_socket < 0) { |
| 907 |
fprintf(stderr, "creating socket: %s\n", strerror(errno)); |
| 908 |
exit(EXIT_FAILURE); |
| 909 |
} |
| 910 |
|
| 911 |
setsockopt(tcp_server_socket, SOL_SOCKET, SO_REUSEADDR, &valone, sizeof(valone)); |
| 912 |
|
| 913 |
client_addr.sin_family = AF_INET; |
| 914 |
client_addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 915 |
client_addr.sin_port = htons(port); |
| 916 |
if (bind(tcp_server_socket, (struct sockaddr *) &client_addr, sizeof(client_addr))) { |
| 917 |
fprintf(stderr, "binding port %d: %s\n", port, strerror(errno)); |
| 918 |
exit(EXIT_FAILURE); |
| 919 |
} |
| 920 |
|
| 921 |
if (listen(tcp_server_socket, 1000) < 0) { |
| 922 |
fprintf(stderr, "listen: %s\n", strerror(errno)); |
| 923 |
exit(EXIT_FAILURE); |
| 924 |
} |
| 925 |
|
| 926 |
// Binded correctly, now we can init logging specifying the port (useful for multiple servers) |
| 927 |
logging_init(port); |
| 928 |
|
| 929 |
l2(OUTPUT_TYPE_INFO, "Created TCP game server on port %d. Servername is '%s'.", port, servername); |
| 930 |
|
| 931 |
signal(SIGTERM, sigterm_catcher); |
| 932 |
} |
| 933 |
|
| 934 |
static int mygethostbyname(char * name, struct in_addr * addr) |
| 935 |
{ |
| 936 |
struct hostent * h; |
| 937 |
|
| 938 |
h = gethostbyname(name); |
| 939 |
if (!h) { |
| 940 |
l1(OUTPUT_TYPE_DEBUG, "Unknown host %s", name); |
| 941 |
return -1; |
| 942 |
|
| 943 |
} else if (h->h_addr_list && (h->h_addr_list)[0]) { |
| 944 |
memcpy(addr, (h->h_addr_list)[0], sizeof(*addr)); |
| 945 |
l2(OUTPUT_TYPE_DEBUG, "%s is at %s", name, inet_ntoa(*addr)); |
| 946 |
return 0; |
| 947 |
} |
| 948 |
return -1; |
| 949 |
} |
| 950 |
|
| 951 |
static char * http_get(char * host, int port, char * path) |
| 952 |
{ |
| 953 |
char * buf, * ptr, * user_agent; |
| 954 |
char headers[4096]; |
| 955 |
char * nextChar = headers; |
| 956 |
int checkedCode; |
| 957 |
struct in_addr serverAddress; |
| 958 |
struct pollfd polls; |
| 959 |
int sock; |
| 960 |
int size, bufsize, dlsize; |
| 961 |
int rc; |
| 962 |
ssize_t bytes; |
| 963 |
struct sockaddr_in destPort; |
| 964 |
char * header_content_length = "Content-Length: "; |
| 965 |
struct utsname uname_; |
| 966 |
|
| 967 |
l3(OUTPUT_TYPE_DEBUG, "HTTP_GET: retrieving http://%s:%d%s", host, port, path); |
| 968 |
|
| 969 |
if ((rc = mygethostbyname(host, &serverAddress))) { |
| 970 |
l1(OUTPUT_TYPE_ERROR, "HTTP_GET: cannot resolve %s", host); |
| 971 |
return NULL; |
| 972 |
} |
| 973 |
|
| 974 |
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); |
| 975 |
if (sock < 0) { |
| 976 |
l2(OUTPUT_TYPE_ERROR, "HTTP_GET: cannot create socket for connection to %s:%d", host, port); |
| 977 |
return NULL; |
| 978 |
} |
| 979 |
|
| 980 |
destPort.sin_family = AF_INET; |
| 981 |
destPort.sin_port = htons(port); |
| 982 |
destPort.sin_addr = serverAddress; |
| 983 |
|
| 984 |
if (connect(sock, (struct sockaddr *) &destPort, sizeof(destPort))) { |
| 985 |
close(sock); |
| 986 |
l2(OUTPUT_TYPE_ERROR, "HTTP_GET: cannot connect to %s:%d", host, port); |
| 987 |
return NULL; |
| 988 |
} |
| 989 |
|
| 990 |
uname(&uname_); |
| 991 |
user_agent = asprintf_("Frozen-Bubble server version " VERSION " (protocol version %d.%d) on %s/%s\n", proto_major, proto_minor, uname_.sysname, uname_.machine); |
| 992 |
buf = asprintf_("GET %s HTTP/0.9\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n", path, host, user_agent); |
| 993 |
free(user_agent); |
| 994 |
if (write(sock, buf, strlen(buf)) != strlen(buf)) { |
| 995 |
close(sock); |
| 996 |
free(buf); |
| 997 |
l2(OUTPUT_TYPE_ERROR, "HTTP_GET: cannot write to socket for connection to %s:%d", host, port); |
| 998 |
return NULL; |
| 999 |
} |
| 1000 |
free(buf); |
| 1001 |
|
| 1002 |
/* This is fun (well, fun for ewt); read the response a character at a time until we: |
| 1003 |
|
| 1004 |
1) Get our first \r\n; which lets us check the return code |
| 1005 |
2) Get a \r\n\r\n, which means we're done */ |
| 1006 |
|
| 1007 |
*nextChar = '\0'; |
| 1008 |
checkedCode = 0; |
| 1009 |
while (!strstr(headers, "\r\n\r\n")) { |
| 1010 |
polls.fd = sock; |
| 1011 |
polls.events = POLLIN; |
| 1012 |
rc = poll(&polls, 1, 20*1000); |
| 1013 |
|
| 1014 |
if (rc == 0) { |
| 1015 |
close(sock); |
| 1016 |
l3(OUTPUT_TYPE_ERROR, "HTTP_GET: timeout retrieving http://%s:%d%s", host, port, path); |
| 1017 |
return NULL; |
| 1018 |
} else if (rc < 0) { |
| 1019 |
close(sock); |
| 1020 |
l3(OUTPUT_TYPE_ERROR, "HTTP_GET: I/O error retrieving http://%s:%d%s", host, port, path); |
| 1021 |
return NULL; |
| 1022 |
} |
| 1023 |
|
| 1024 |
if (read(sock, nextChar, 1) != 1) { |
| 1025 |
close(sock); |
| 1026 |
l3(OUTPUT_TYPE_ERROR, "HTTP_GET: I/O error retrieving http://%s:%d%s", host, port, path); |
| 1027 |
return NULL; |
| 1028 |
} |
| 1029 |
|
| 1030 |
nextChar++; |
| 1031 |
*nextChar = '\0'; |
| 1032 |
|
| 1033 |
if (nextChar + 1 - headers == sizeof(headers)) { |
| 1034 |
close(sock); |
| 1035 |
l3(OUTPUT_TYPE_ERROR, "HTTP_GET: I/O error retrieving http://%s:%d%s", host, port, path); |
| 1036 |
return NULL; |
| 1037 |
} |
| 1038 |
|
| 1039 |
if (!checkedCode && strstr(headers, "\r\n")) { |
| 1040 |
char * start, * end; |
| 1041 |
|
| 1042 |
checkedCode = 1; |
| 1043 |
start = headers; |
| 1044 |
while (!isspace(*start) && *start) |
| 1045 |
start++; |
| 1046 |
if (!*start) { |
| 1047 |
close(sock); |
| 1048 |
l3(OUTPUT_TYPE_ERROR, "HTTP_GET: I/O error retrieving http://%s:%d%s", host, port, path); |
| 1049 |
return NULL; |
| 1050 |
} |
| 1051 |
start++; |
| 1052 |
|
| 1053 |
end = start; |
| 1054 |
while (!isspace(*end) && *end) |
| 1055 |
end++; |
| 1056 |
if (!*end) { |
| 1057 |
close(sock); |
| 1058 |
l3(OUTPUT_TYPE_ERROR, "HTTP_GET: I/O error retrieving http://%s:%d%s", host, port, path); |
| 1059 |
return NULL; |
| 1060 |
} |
| 1061 |
|
| 1062 |
*end = '\0'; |
| 1063 |
l1(OUTPUT_TYPE_DEBUG, "HTTP_GET: server response '%s'", start); |
| 1064 |
if (strcmp(start, "200")) { |
| 1065 |
close(sock); |
| 1066 |
l4(OUTPUT_TYPE_ERROR, "HTTP_GET: bad server response %s retrieving http://%s:%d%s", start, host, port, path); |
| 1067 |
return NULL; |
| 1068 |
} |
| 1069 |
|
| 1070 |
*end = ' '; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
if ((buf = strstr(headers, header_content_length))) { |
| 1075 |
size = charstar_to_int(buf + strlen(header_content_length)); |
| 1076 |
bufsize = size + 1; |
| 1077 |
} else { |
| 1078 |
size = -1; |
| 1079 |
bufsize = 4096; |
| 1080 |
} |
| 1081 |
|
| 1082 |
dlsize = 0; |
| 1083 |
buf = ptr = malloc_(bufsize); |
| 1084 |
while (1) { |
| 1085 |
bytes = read(sock, ptr, bufsize - (ptr - buf) - 1); |
| 1086 |
if (bytes == -1) { |
| 1087 |
l1(OUTPUT_TYPE_ERROR, "HTTP_GET: read: %s", strerror(errno)); |
| 1088 |
close(sock); |
| 1089 |
free(buf); |
| 1090 |
return NULL; |
| 1091 |
} else if (bytes == 0) { |
| 1092 |
// 0 == EOF |
| 1093 |
ptr[0] = '\0'; |
| 1094 |
buf = realloc_(buf, dlsize + 1); |
| 1095 |
close(sock); |
| 1096 |
return buf; |
| 1097 |
} else { |
| 1098 |
l1(OUTPUT_TYPE_DEBUG, "HTTP_GET: read %zd bytes", bytes); |
| 1099 |
dlsize += bytes; |
| 1100 |
ptr = buf + dlsize; |
| 1101 |
if (size > -1 && dlsize == size) { |
| 1102 |
ptr[0] = '\0'; |
| 1103 |
buf = realloc_(buf, dlsize + 1); |
| 1104 |
close(sock); |
| 1105 |
return buf; |
| 1106 |
} |
| 1107 |
if (bufsize - (ptr - buf) - 1 < 2048) { |
| 1108 |
bufsize += 4096; |
| 1109 |
if (bufsize >= 1024*1024*1024) { |
| 1110 |
l0(OUTPUT_TYPE_ERROR, "HTTP_GET: maximum download size of 1024*1024*1024 reached"); |
| 1111 |
ptr[0] = '\0'; |
| 1112 |
buf = realloc_(buf, dlsize + 1); |
| 1113 |
close(sock); |
| 1114 |
return buf; |
| 1115 |
} |
| 1116 |
buf = realloc_(buf, bufsize); |
| 1117 |
ptr = buf + dlsize; |
| 1118 |
} |
| 1119 |
l2(OUTPUT_TYPE_DEBUG, "HTTP_GET: dlsize %d bytes, bufsize %d bytes", dlsize, bufsize); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
} |
| 1123 |
|
| 1124 |
void register_server(int silent) { |
| 1125 |
if (!quiet && !lan_game_mode) { |
| 1126 |
char* path = asprintf_("/servers/servers.php?server-add=%s&server-add-port=%d", external_hostname, external_port); |
| 1127 |
char* doc = http_get("www.frozen-bubble.org", 80, path); |
| 1128 |
free(path); |
| 1129 |
if (doc != NULL) { |
| 1130 |
if (silent) { |
| 1131 |
free(doc); |
| 1132 |
return; |
| 1133 |
} |
| 1134 |
if (strstr(doc, "FB_TAG_SERVER_ADDED")) { |
| 1135 |
if (streq(external_hostname, "DISTANT_END")) { |
| 1136 |
// don't confuse admin printing a cryptic DISTANT_END hostname |
| 1137 |
l1(OUTPUT_TYPE_INFO, "Successfully registered server (port:%d) to 'www.frozen-bubble.org'.", external_port); |
| 1138 |
} else { |
| 1139 |
l2(OUTPUT_TYPE_INFO, "Successfully registered server (host:%s port:%d) to 'www.frozen-bubble.org'.", external_hostname, external_port); |
| 1140 |
} |
| 1141 |
} else { |
| 1142 |
char * ptr = doc; |
| 1143 |
l2(OUTPUT_TYPE_ERROR, "Problem registering server (host:%s port:%d) to 'www.frozen-bubble.org'.", external_hostname, external_port); |
| 1144 |
l0(OUTPUT_TYPE_ERROR, "Notice: for successful registering, using the said host and port from outside must reach this server!"); |
| 1145 |
while ((ptr = strstr(doc, "FB_TAG_"))) { |
| 1146 |
char * end = strchr(ptr, ' '); |
| 1147 |
if (end) { |
| 1148 |
*end = '\0'; |
| 1149 |
l1(OUTPUT_TYPE_ERROR, "-> %s", ptr + 7); |
| 1150 |
ptr = end + 1; |
| 1151 |
} else { |
| 1152 |
break; |
| 1153 |
} |
| 1154 |
} |
| 1155 |
} |
| 1156 |
free(doc); |
| 1157 |
} |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
void unregister_server() { |
| 1162 |
if (!quiet && !lan_game_mode) { |
| 1163 |
char* path = asprintf_("/servers/servers.php?server-remove=%s&server-remove-port=%d", external_hostname, external_port); |
| 1164 |
char* doc = http_get("www.frozen-bubble.org", 80, path); |
| 1165 |
free(path); |
| 1166 |
if (doc != NULL) { |
| 1167 |
if (strstr(doc, "FB_TAG_SERVER_REMOVED")) { |
| 1168 |
if (streq(external_hostname, "DISTANT_END")) { |
| 1169 |
// don't confuse admin printing a cryptic DISTANT_END hostname |
| 1170 |
l1(OUTPUT_TYPE_INFO, "Successfully unregistered server (port:%d) to 'www.frozen-bubble.org'.", external_port); |
| 1171 |
} else { |
| 1172 |
l2(OUTPUT_TYPE_INFO, "Successfully unregistered server (host:%s port:%d) to 'www.frozen-bubble.org'.", external_hostname, external_port); |
| 1173 |
} |
| 1174 |
} else { |
| 1175 |
char * ptr = doc; |
| 1176 |
l0(OUTPUT_TYPE_ERROR, "Problem unregistering server to 'www.frozen-bubble.org'."); |
| 1177 |
while ((ptr = strstr(doc, "FB_TAG_"))) { |
| 1178 |
char * end = strchr(ptr, ' '); |
| 1179 |
if (end) { |
| 1180 |
*end = '\0'; |
| 1181 |
l1(OUTPUT_TYPE_ERROR, "-> %s", ptr + 7); |
| 1182 |
ptr = end + 1; |
| 1183 |
} else { |
| 1184 |
break; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
} |
| 1188 |
free(doc); |
| 1189 |
} |
| 1190 |
} |
| 1191 |
} |