Crossfire Server, Trunk  R22047
server.c File Reference
#include "global.h"
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "object.h"
#include "path.h"
#include "random_maps/random_map.h"
#include "random_maps/rproto.h"
#include "sproto.h"
#include "tod.h"
#include "version.h"
#include "server.h"
+ Include dependency graph for server.c:

Go to the source code of this file.

Macros

#define PORTAL_DESTINATION_NAME   "Town portal destination" /* this one should really be in a header file */
 
#define SPEED_DEBUG
 

Functions

bool check_password (const char *typed, const char *crypted)
 
static bool check_shutdown (void)
 
static char * clean_path (const char *file, char *newpath, int size)
 
void clean_tmp_files (void)
 
void cleanup (void)
 
static char const * crypt_string (char const str[static 1], char const *salt)
 
static void do_shutdown (void)
 
static void do_specials (void)
 
void enter_exit (object *op, object *exit_ob)
 
static void enter_fixed_template_map (object *pl, object *exit_ob)
 
static void enter_map (object *op, mapstruct *newmap, int x, int y)
 
void enter_player_maplevel (object *op)
 
void enter_player_savebed (object *op)
 
static void enter_random_map (object *pl, object *exit_ob)
 
static void enter_random_template_map (object *pl, object *exit_ob)
 
static void enter_unique_map (object *op, object *exit_ob)
 
int forbid_play (void)
 
void leave (player *pl, int draw_exit)
 
void login_check_shutdown (object *const op)
 
char const * newhash (char const password[static 1])
 
static bool object_in_icecube (object *op)
 
void player_map_change_common (object *op, mapstruct *const oldmap, mapstruct *const newmap)
 
void process_events (void)
 
static void process_players1 (void)
 
static void process_players2 (void)
 
void server_main (int argc, char *argv[])
 
void set_map_timeout (mapstruct *oldmap)
 
static char * unclean_path (const char *src, char *newpath, int size)
 

Variables

static const char * days [] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
 
volatile sig_atomic_t shutdown_flag
 
static const int shutdown_warn_times [] = {120, 90, 60, 45, 30, 15, 10, 5, 4, 3, 2, 1}
 
unsigned long todtick
 

Detailed Description

Main server functions.

Definition in file server.c.

Macro Definition Documentation

◆ PORTAL_DESTINATION_NAME

#define PORTAL_DESTINATION_NAME   "Town portal destination" /* this one should really be in a header file */

Referenced by enter_exit().

◆ SPEED_DEBUG

#define SPEED_DEBUG

Definition at line 970 of file server.c.

Function Documentation

◆ check_password()

bool check_password ( const char *  typed,
const char *  crypted 
)

Hash a password and compare it to the stored version.

Parameters
typedPassword entered by the user (not hashed).
cryptedStored password to check against (hashed).
Returns
True if the passwords match, false otherwise.

Definition at line 109 of file server.c.

References crypt_string(), llevError, and LOG().

Referenced by account_change_password(), account_login(), check_login(), receive_player_password(), and verify_player().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ check_shutdown()

static bool check_shutdown ( void  )
static

Check if we're ready to shut the server down.

Definition at line 1312 of file server.c.

References cmd_shutdown_time, draw_ext_info_format(), llevInfo, LOG(), MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_DM, NDI_ALL, NDI_UNIQUE, shutdown_flag, and shutdown_warn_times.

Referenced by do_specials().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ clean_path()

static char* clean_path ( const char *  file,
char *  newpath,
int  size 
)
static

Takes a path and replaces all / with _ We do a strcpy so that we do not change the original string.

Parameters
filepath to clean.
newpathbuffer that will contain the cleaned path. Should be at least as long as file.
sizelength of newpath.
Returns
newpath.

Definition at line 320 of file server.c.

References strlcpy().

Referenced by enter_unique_map().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ clean_tmp_files()

void clean_tmp_files ( void  )

Save unique maps and clean up temporary map files unless recycling temporary maps. The function name is somewhat misleading.

Definition at line 1127 of file server.c.

References clean_tmp_map(), first_map, mapdef::in_memory, MAP_IN_MEMORY, quest_definition::next, mapdef::next, Settings::recycle_tmp_maps, save_map(), SAVE_MODE_NORMAL, settings, swap_map(), and write_todclock().

Referenced by cleanup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cleanup()

◆ crypt_string()

static char const* crypt_string ( char const  str[static 1],
char const *  salt 
)
static

Encrypt a string. Used for password storage on disk.

Really, there is no reason to crypt the passwords any system. But easier to just leave this enabled for backward compatibility. Put the simple case at top - no encryption - makes it easier to read.

Parameters
strstring to crypt.
saltsalt to crypt with.
Returns
crypted str.
Todo:
make thread-safe?

Definition at line 70 of file server.c.

References Settings::crypt_mode, RANDOM, and settings.

Referenced by check_password(), and newhash().

+ Here is the caller graph for this function:

◆ do_shutdown()

static void do_shutdown ( void  )
static

Definition at line 1301 of file server.c.

References cleanup(), draw_ext_info(), MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_DM, NDI_ALL, and NDI_UNIQUE.

Referenced by do_specials().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ do_specials()

static void do_specials ( void  )
static

Collection of functions to call from time to time.

Modified 2000-1-14 MSW to use the global pticks count to determine how often to do things. This will allow us to spred them out more often. I use prime numbers for the factor count - in that way, it is less likely these actions will fall on the same tick (compared to say using 500/2500/15000 which would mean on that 15,000 tick count a whole bunch of stuff gets done). Of course, there can still be times where multiple specials are done on the same tick, but that will happen very infrequently

I also think this code makes it easier to see how often we really are doing the various things.

Definition at line 1376 of file server.c.

References accounts_save(), check_shutdown(), clean_friendly_list(), CS_LOGTIME, cst_lst, do_shutdown(), fix_luck(), fix_weight(), flush_old_maps(), knowledge_process_incremental(), metaserver_update(), party_obsolete_parties(), pticks, PTICKS_PER_CLOCK, tick_the_clock(), CS_Stats::time_start, watchdog(), write_book_archive(), and write_cs_stats().

Referenced by server_main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enter_exit()

void enter_exit ( object *  op,
object *  exit_ob 
)

Tries to move 'op' to exit_ob.

Largely redone by MSW 2001-01-21 - this function was overly complex and had some obscure bugs.

May call abort() in some situations.

Parameters
opcharacter or monster that is using the exit.
exit_obexit object (boat, door, teleporter, etc.)

Definition at line 708 of file server.c.

References AP_UNAPPLY, draw_ext_info_format(), enter_fixed_template_map(), enter_map(), enter_random_map(), enter_random_template_map(), enter_unique_map(), EXIT_PATH, EXIT_X, EXIT_Y, FLAG_DAMNED, FLAG_UNIQUE, hit_player(), HUGE_BUF, llevDebug, Settings::localdir, LOG(), MAP_ENTER_X, MAP_ENTER_Y, MAP_PLAYER_UNIQUE, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_FAILURE, NDI_UNIQUE, ob_apply(), object_find_by_type_and_slaying(), object_free(), object_remove(), path_combine_and_normalize(), PORTAL_DESTINATION_NAME, QUERY_FLAG, ready_map_name(), save_player(), and settings.

Referenced by command_reset(), command_summon(), command_teleport(), create_player_cmd(), do_goto(), enter_player_savebed(), execute_word_of_recall(), exit_type_apply(), exit_type_move_on(), key_change_class(), move_teleporter(), player_arrest(), and player_changer_type_process().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enter_fixed_template_map()

static void enter_fixed_template_map ( object *  pl,
object *  exit_ob 
)
static

The player is trying to enter a non-randomly generated template map. In this case, use a map file for a template.

Parameters
plplayer.
exit_obexit containing template map parameters.

Definition at line 441 of file server.c.

References create_pathname(), create_template_pathname(), draw_ext_info_format(), enter_map(), EXIT_PATH, EXIT_X, EXIT_Y, HUGE_BUF, mapdef::is_template, llevError, LOG(), MAP_PLAYER_UNIQUE, mapfile_load(), MAX_BUF, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_FAILURE, NDI_UNIQUE, mapdef::path, path_combine_and_normalize(), ready_map_name(), replace(), snprintf, and strlcpy().

Referenced by enter_exit().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enter_map()

static void enter_map ( object *  op,
mapstruct newmap,
int  x,
int  y 
)
static

Moves the player and pets from current map (if any) to new map.

Parameters
opplayer to move.
newmap
x
ynew location. If (x, y) point to an out of map point, will use default map coordinates.

Definition at line 174 of file server.c.

References draw_ext_info(), find_dir_2(), FLAG_REMOVED, freearr_x, freearr_y, llevError, llevInfo, LOG(), MAP_ENTER_X, MAP_ENTER_Y, MAP_HEIGHT, map_newmap_cmd(), MAP_WIDTH, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_FAILURE, NDI_UNIQUE, ob_blocked(), object_find_free_spot(), object_free_drop_inventory(), object_insert_in_map_at(), object_remove(), object_set_enemy(), out_of_map(), mapdef::path, pets_remove_all(), player_map_change_common(), QUERY_FLAG, range_golem, remove_friendly_object(), safe_strncpy, SIZEOFFREE, SIZEOFFREE1, and SIZEOFFREE2.

Referenced by enter_exit(), enter_fixed_template_map(), enter_player_maplevel(), enter_random_map(), enter_random_template_map(), and enter_unique_map().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enter_player_maplevel()

void enter_player_maplevel ( object *  op)

Move a player to its stored map level. This function is used to place the player just after logging in. Since the map may no longer exist, dump the player to an alternative 'emergency' location if that's the case.

Definition at line 663 of file server.c.

References draw_ext_info_format(), Settings::emergency_mapname, Settings::emergency_x, Settings::emergency_y, enter_map(), flags, llevError, LOG(), MAP_ENTER_X, MAP_ENTER_Y, MSG_TYPE_MISC, NDI_UNIQUE, ready_map_name(), and settings.

Referenced by check_login(), and set_first_map().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enter_player_savebed()

void enter_player_savebed ( object *  op)

This is a basic little function to put the player back to his savebed. We do some error checking - its possible that the savebed map may no longer exist, so we make sure the player goes someplace.

Parameters
opplayer.

Definition at line 133 of file server.c.

References add_string(), Settings::emergency_mapname, Settings::emergency_x, Settings::emergency_y, enter_exit(), EXIT_PATH, EXIT_X, EXIT_Y, free_string(), llevDebug, LOG(), MAX_BUF, object_free(), object_new(), mapdef::path, safe_strncpy, and settings.

Referenced by kill_player_not_permadeath(), and save_life().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enter_random_map()

static void enter_random_map ( object *  pl,
object *  exit_ob 
)
static

The player is trying to enter a randomly generated map. In this case, generate the random map as needed.

Parameters
plplayer.
exit_obexit containing random map parameters.

Definition at line 374 of file server.c.

References add_string(), buf, enter_map(), EXIT_PATH, EXIT_X, EXIT_Y, RMParms::final_map, generate_random_map(), get_region_by_map(), HUGE_BUF, MAP_ENTER_X, MAP_ENTER_Y, RMParms::origin_map, RMParms::origin_x, RMParms::origin_y, mapdef::path, RMParms::region, safe_strncpy, set_random_map_variable(), snprintf, strlcpy(), RMParms::Xsize, and RMParms::Ysize.

Referenced by enter_exit().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enter_random_template_map()

static void enter_random_template_map ( object *  pl,
object *  exit_ob 
)
static

The player is trying to enter a randomly generated template map. In this case, generate the map as needed.

Parameters
plplayer.
exit_obexit containing random template map parameters.

Definition at line 532 of file server.c.

References create_template_pathname(), enter_map(), EXIT_PATH, EXIT_X, EXIT_Y, generate_random_map(), get_region_by_map(), HUGE_BUF, mapdef::is_template, MAP_ENTER_X, MAP_ENTER_Y, MAP_PLAYER_UNIQUE, MAX_BUF, RMParms::origin_map, RMParms::origin_x, RMParms::origin_y, path_combine_and_normalize(), ready_map_name(), RMParms::region, replace(), safe_strncpy, set_random_map_variable(), snprintf, strlcpy(), RMParms::Xsize, and RMParms::Ysize.

Referenced by enter_exit().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ enter_unique_map()

static void enter_unique_map ( object *  op,
object *  exit_ob 
)
static

Player is entering a unique map.

Parameters
opplayer.
exit_obexit containing unique map information.

Definition at line 600 of file server.c.

References apply_auto_fix(), clean_path(), draw_ext_info_format(), enter_map(), EXIT_PATH, EXIT_X, EXIT_Y, HUGE_BUF, llevDebug, LOG(), MAP_PLAYER_UNIQUE, mapfile_load(), MAX_BUF, MSG_TYPE_COMMAND, MSG_TYPE_COMMAND_FAILURE, NDI_UNIQUE, mapdef::path, path_combine_and_normalize(), ready_map_name(), snprintf, strlcpy(), unclean_path(), and mapdef::unique.

Referenced by enter_exit().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ forbid_play()

int forbid_play ( void  )

Checks if server should be started.

Returns
1 if play is forbidden, 0 if ok.
Todo:
document forbidden stuff.

Definition at line 1256 of file server.c.

References buf, Settings::confdir, days, llevDebug, LOG(), logfile, MAX_BUF, PERM_FILE, settings, and snprintf.

Referenced by init_startup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ leave()

void leave ( player pl,
int  draw_exit 
)

Player logs out, or was disconnected.

Parameters
plplayer.
draw_exitif set, display leaving message to other players.

Definition at line 1195 of file server.c.

References draw_ext_info_format(), FLAG_REMOVED, FLAG_WIZ, hiscore_check(), socket_struct::host, llevInfo, LOG(), MAP_IN_MEMORY, MAP_TIMEOUT, MAX_BUF, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_PLAYER, NDI_ALL, NDI_DK_ORANGE, NDI_UNIQUE, Ns_Dead, pl::ob, object_remove(), party_leave(), pets_terminate_all(), QUERY_FLAG, query_name(), pl::socket, ST_CONFIRM_PASSWORD, ST_GET_NAME, ST_GET_PASSWORD, pl::state, socket_struct::status, and pl::transport.

Referenced by check_login(), do_server(), receive_play_again(), and shop_mat_type_move_on().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ login_check_shutdown()

void login_check_shutdown ( object *const  op)

Warn op if a server shutdown is scheduled. Used to inform players about scheduled shutdowns when they log in.

Definition at line 1347 of file server.c.

References cmd_shutdown_time, draw_ext_info_format(), i18n(), MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_DM, NDI_ALL, NDI_UNIQUE, shutdown_warn_times, and todtick.

Referenced by check_login().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ newhash()

char const* newhash ( char const  password[static 1])

Definition at line 96 of file server.c.

References crypt_string().

+ Here is the call graph for this function:

◆ object_in_icecube()

static bool object_in_icecube ( object *  op)
static

Definition at line 972 of file server.c.

Referenced by process_events().

+ Here is the caller graph for this function:

◆ player_map_change_common()

void player_map_change_common ( object *  op,
mapstruct *const  oldmap,
mapstruct *const  newmap 
)

Definition at line 265 of file server.c.

References EVENT_MAPENTER, EVENT_MAPLEAVE, events_execute_global_event(), player_update_bg_music(), mapdef::players, set_map_timeout(), and mapdef::timeout.

Referenced by enter_map(), and move_player_attack().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ process_events()

void process_events ( void  )

Process all active objects.

Definition at line 979 of file server.c.

References active_objects, animate_object(), Settings::casting_time, FABS, FLAG_FREED, FLAG_REMOVED, llevError, LOG(), MAP_IN_MEMORY, object_dump(), object_free(), object_in_icecube(), object_update_speed(), process_object(), process_players1(), process_players2(), QUERY_FLAG, settings, stringbuffer_finish(), stringbuffer_new(), and TRUE.

Referenced by server_main(), and START_TEST().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ process_players1()

static void process_players1 ( void  )
static

◆ process_players2()

static void process_players2 ( void  )
static

Do all player-related stuff after objects have been updated.

See also
process_players1().
Todo:
explain why 2 passes for players.

Definition at line 949 of file server.c.

References first_player, pl::has_hit, pl::next, and pl::ob.

Referenced by process_events().

+ Here is the caller graph for this function:

◆ server_main()

void server_main ( int  argc,
char *  argv[] 
)

Server main function.

Parameters
argclength of argv.
argvcommand-line options.

Definition at line 1430 of file server.c.

References bRunning, cftimer_process_timers(), check_active_maps(), do_server(), do_specials(), EVENT_CLOCK, events_execute_global_event(), init(), initPlugins(), llevInfo, LOG(), nroferrors, process_events(), tick_game_time(), TRUE, and update_players().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_map_timeout()

void set_map_timeout ( mapstruct oldmap)

Applies the map timeout.

Parameters
oldmapmap to process.

Definition at line 287 of file server.c.

References MAP_MAXTIMEOUT, MAP_MINTIMEOUT, MAP_TIMEOUT, swap_map(), and mapdef::timeout.

Referenced by flush_old_maps(), place_exits(), and player_map_change_common().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unclean_path()

static char* unclean_path ( const char *  src,
char *  newpath,
int  size 
)
static

Takes a path and replaces all _ with / This basically undoes clean_path(). We do a strcpy so that we do not change the original string. We are smart enough to start after the last / in case we are getting passed a string that points to a unique map path.

Parameters
srcpath to unclean.
newpathbuffer that will contain the uncleaned path. Should be at least as long as file.
sizelength of newpath.
Returns
newpath.

Definition at line 348 of file server.c.

References strlcpy().

Referenced by enter_unique_map().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ days

const char* days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
static

Ingame days.

Definition at line 51 of file server.c.

Referenced by forbid_play().

◆ shutdown_flag

volatile sig_atomic_t shutdown_flag

Definition at line 53 of file server.c.

Referenced by check_shutdown(), and signal_shutdown().

◆ shutdown_warn_times

const int shutdown_warn_times[] = {120, 90, 60, 45, 30, 15, 10, 5, 4, 3, 2, 1}
static

How many minutes before a shutdown to warn players, in reverse order.

Definition at line 48 of file server.c.

Referenced by check_shutdown(), and login_check_shutdown().

◆ todtick

unsigned long todtick

Ingame time

Definition at line 417 of file init.c.

Referenced by login_check_shutdown().