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