35 #include <arpa/inet.h> 36 #include <sys/types.h> 38 #include <netinet/in.h> 137 char *params = NULL, *cp;
141 if (len <= 0 || !buf) {
142 LOG(
llevDebug,
"IP '%s' sent bogus request_info_cmd information\n", ns->
host);
156 for (cp = buf; *cp !=
'\0'; cp++)
162 if (!strcmp(buf,
"image_info"))
164 else if (!strcmp(buf,
"image_sums"))
166 else if (!strcmp(buf,
"skill_info"))
168 else if (!strcmp(buf,
"skill_extra"))
170 else if (!strcmp(buf,
"spell_paths"))
172 else if (!strcmp(buf,
"exp_table"))
174 else if (!strcmp(buf,
"race_list"))
176 else if (!strcmp(buf,
"race_info"))
178 else if (!strcmp(buf,
"class_list"))
180 else if (!strcmp(buf,
"class_info"))
182 else if (!strcmp(buf,
"rules"))
184 else if (!strcmp(buf,
"motd"))
186 else if (!strcmp(buf,
"news"))
188 else if (!strcmp(buf,
"newcharinfo"))
190 else if (!strcmp(buf,
"startingmap"))
192 else if (!strcmp(buf,
"knowledge_info"))
219 for (
int i = 0; client_commands[i].
cmdname != NULL; i++) {
220 if (strcmp(cmd, client_commands[i].
cmdname) == 0) {
221 if (client_commands[i].
cmdproc != NULL) {
222 client_commands[i].
cmdproc(data, len, ns);
235 for (
int i = 0; player_commands[i].
cmdname != NULL; i++) {
236 if (strcmp(cmd, player_commands[i].
cmdname) == 0) {
238 player_commands[i].
cmdproc(data, len, pl);
268 int command_count = 0;
269 while (command_count < 5 || (pl && command_count < 25)) {
292 char *cmd = strtok_r((
char *)ns->
inbuf.
buf + 2,
" ", &data);
297 got_player_cmd =
handle_cmd(ns, pl, cmd, data, rem);
299 got_player_cmd =
handle_cmd(ns, pl, cmd, NULL, 0);
303 if (got_player_cmd) {
338 static struct sockaddr_in insock;
341 struct protoent *protoent;
343 if ((protoent = getprotobyname(
"udp")) == NULL
344 || (fd = socket(PF_INET, SOCK_DGRAM, protoent->p_proto)) == -1) {
347 insock.sin_family = AF_INET;
348 insock.sin_port = htons((
unsigned short)13325);
349 insock.sin_addr.s_addr = inet_addr(
"127.0.0.1");
352 snprintf(buf,
sizeof(buf),
"%d\n",
pticks);
353 sendto(fd, buf, strlen(buf), 0, (
struct sockaddr *)&insock,
sizeof(insock));
367 return fcntl(fd, F_GETFL) != -1 || errno != EBADF;
378 int newsocknum = -1, j;
379 #ifdef HAVE_GETNAMEINFO 380 struct sockaddr_storage addr;
382 struct sockaddr_in addr;
396 if (newsocknum == -1) {
412 if (newsocknum < 0) {
417 init_sockets[newsocknum].
fd = accept(listen_fd, (
struct sockaddr *)&addr, &addrlen);
422 #ifndef HAVE_GETNAMEINFO 429 #ifdef HAVE_GETNAMEINFO 430 getnameinfo((
struct sockaddr *) &addr, addrlen, buf,
sizeof(buf), NULL, 0, NI_NUMERICHOST);
432 ip = ntohl(addr.sin_addr.s_addr);
433 snprintf(buf,
sizeof(buf),
"%ld.%ld.%ld.%ld", (ip>>24)&255, (ip>>16)&255, (ip>>8)&255, ip&255);
437 LOG(
llevInfo,
"Banned host tried to connect: [%s]\n", buf);
476 LOG(
llevError,
"esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n", (
unsigned long)pl->
last_weight, (
unsigned long)
WEIGHT(pl->
ob));
528 fd_set tmp_read, tmp_exceptions;
530 FD_ZERO(&tmp_exceptions);
560 FD_SET((uint32_t)pl->
socket->
fd, &tmp_read);
561 FD_SET((uint32_t)pl->
socket->
fd, &tmp_exceptions);
567 if (sleep_time < 0) {
568 LOG(
llevInfo,
"skipping time (over by %ld ms)\n", -sleep_time/1000);
587 while (sleep_time > 0) {
593 if (errno == EINTR) {
596 }
else if (errno == EBADF) {
601 }
else if (!pollret) {
630 if (FD_ISSET(pl->
socket->
fd, &tmp_exceptions)) {
636 if (!keep_processing) {
const char * cmdname
Command name.
static const struct player_cmd_mapping player_commands[]
Dispatch tables for the server.
uint32_t tick
Client wishes to get tick commands.
static int is_fd_valid(int fd)
Checks if file descriptor is valid.
void leave(player *pl, int draw_exit)
Player logs out, or was disconnected.
const func_uint8_int_ns cmdproc
Function to call.
void SockList_Init(SockList *sl)
Initializes the SockList instance.
unsigned char buf[MAXSOCKBUF]
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
void send_skill_extra(socket_struct *ns, char *params)
Send extra skill information.
void account_login_cmd(char *buf, int len, socket_struct *ns)
Handles the account login.
void SockList_ResetRead(SockList *sl)
Resets the length of the stored data for reading.
void mark_item_cmd(uint8_t *data, int len, player *pl)
Client wants to mark some object.
void look_at_cmd(char *buf, int len, player *pl)
Client wants to look at some object.
unsigned long max_ticktime
void version_cmd(char *buf, int len, socket_struct *ns)
Client tells its version.
const func_uint8_int_pl cmdproc
Function to call.
uint32_t last_tick
Number of ticks since last communication.
player * first_player
First player.
bool connection_alive(const socket_struct *socket)
Check whether the given socket's connection is alive or not.
int save_player(object *op, int flag)
Saves a player to disk.
long get_sleep_remaining()
void request_info_cmd(char *buf, int len, socket_struct *ns)
request_info_cmd is sort of a meta command.
static const struct client_cmd_mapping client_commands[]
Commands sent directly by client, when connecting or when needed.
void esrv_update_item(int flags, object *pl, object *op)
Updates object *op for player *pl.
void send_map_info(socket_struct *ns)
Send information on available start maps.
void send_image_sums(socket_struct *ns, char *params)
Sends requested face information.
Socket structure, represents a client-server connection.
void esrv_send_inventory(object *pl, object *op)
Sends inventory of a container.
bool heartbeat
Client will send hearbeats.
void new_player_cmd(uint8_t *buf, int len, player *pl)
This handles the commands issued by the player (ie, north, fire, cast, etc.).
int checkbanned(const char *login, const char *host)
Check if a player and/or host is banned.
void inscribe_scroll_cmd(char *buf, int len, player *pl)
int max_filedescriptor
max filedescriptor on the system.
Global type definitions and header inclusions.
void send_skill_info(socket_struct *ns, char *params)
This sends the skill number to name mapping.
#define ST_PLAYING
Usual state.
char * host
Which host it is connected from (ip address).
socket_struct * init_sockets
Established connections for clients not yet playing.
void free_newsocket(socket_struct *ns)
Frees a socket.
void update_players()
Send updates to players.
size_t faces_sent_len
This is the number of elements allocated in faces_sent[].
static void send_updates(player *pl)
Send updated stats, map, look, and inventory to the player.
uint32_t update_look
If true, we need to send the look window.
void check_all_fds()
Check all file descriptors and mark sockets with invalid ones as dead.
const uint8_t flag
If set, the player must be in the ST_PLAYING state for this command to be available.
player * next
Pointer to next player, NULL if this is last.
unsigned long ticks_overtime
Socket_Info socket_info
Socket information.
void do_server(void)
This checks the sockets for input and exceptions, does the right thing.
Image-related structures.
#define BEAT_INTERVAL
Interval between heartbeat requests.
void SockList_Term(SockList *sl)
Frees all resources allocated by a SockList instance.
Definition of a function the client sends without player interaction.
void send_exp_table(socket_struct *ns)
This sends the experience table the sever is using.
void account_password(char *buf, int len, socket_struct *ns)
Handles the account password change.
void(* func_uint8_int_ns)(char *, int, socket_struct *)
Prototype for functions the client sends without player interaction.
socket_struct * socket
Socket information for this player.
#define WEIGHT(op)
Returns the weight of the given object.
void init_listening_socket(socket_struct *ns)
This opens *ns for listening to connections.
void SockList_AddString(SockList *sl, const char *data)
Adds a string without length.
uint8_t state
Input state of the player (name, password, etc).
object * ob
The object representing the player.
Defines various structures and values that are used for the new client server communication method...
void account_new_cmd(char *buf, int len, socket_struct *ns)
Handles the account creation This function shares a fair amount of the same logic as account_login_cm...
bool handle_client(socket_struct *ns, player *pl)
Handle commands from a client.
int32_t last_weight
Last weight as sent to client; -1 means do not send weight.
uint32_t update_inventory
If true, we need to send the inventory list.
unsigned long total_ticktime
void send_race_info(socket_struct *ns, char *params)
Sends information on specified race to the client.
static int handle_cmd(socket_struct *ns, player *pl, char *cmd, char *data, int len)
Handle a command, either directly sent by the client, or sent for a player's action.
uint32_t pticks
Number of ticks since time reset.
int allocated_sockets
Number of allocated items in init_sockets.
void send_image_info(socket_struct *ns)
Sends the number of images, checksum of the face file, and the image_info file information.
void fatal(enum fatal_error err)
fatal() is meant to be called whenever a fatal signal is intercepted.
#define MAX_BUF
Used for all kinds of things.
float speed_left
How much speed is left to spend this round.
void send_class_info(socket_struct *ns, char *params)
Send information on the specified class.
void set_up_cmd(char *buf, int len, socket_struct *ns)
This is the Setup cmd - easy first implementation.
void draw_client_map(object *pl)
Draws client map.
void esrv_draw_look(object *pl)
Send the look window.
void send_spell_paths(socket_struct *ns)
This sends the spell path to name mapping.
object * container
Current container being used.
void init_connection(socket_struct *ns, const char *from_ip)
Initializes a connection.
const char * cmdname
Command name.
void send_tick(player *pl)
void send_class_list(socket_struct *ns)
Sends the list of classes to the client.
void send_race_list(socket_struct *ns)
Send the list of player races to the client.
void lock_item_cmd(uint8_t *data, int len, player *pl)
Client wants to apply some object.
Only for debugging purposes.
struct timeval timeout
Timeout for select.
static void new_connection(int listen_fd)
Handle a new connection from a client.
void ask_smooth_cmd(char *buf, int len, socket_struct *ns)
Tells client the picture it has to use to smooth a picture number given as argument.
void(* func_uint8_int_pl)(char *, int, player *)
Prototype for functions used to handle player actions.
void send_file(socket_struct *ns, const char *file)
Sends the desired file to the client.
void examine_cmd(char *buf, int len, player *pl)
Client wants to examine some object.
void apply_cmd(char *buf, int len, player *pl)
Client wants to apply some object.
void account_add_player_cmd(char *buf, int len, socket_struct *ns)
Handle accountaddplayer from server (add a character to this account).
void final_free_player(player *pl)
Sends the 'goodbye' command to the player, and closes connection.
void add_me_cmd(char *buf, int len, socket_struct *ns)
The client has requested to be added to the game.
Definition of a function called in reaction to player's action.
void reply_cmd(char *buf, int len, player *pl)
This is a reply to a previous query.
void send_new_char_info(socket_struct *ns)
Sends information related to creating a new character to the client.
void knowledge_send_info(socket_struct *ns)
Send the reply_info for 'knowledge_info'.
void player_cancel_repeat(player *pl)
If the player is repeating an action, cancel it.
Contains the base information we use to make up a packet we want to send.
#define FREE_AND_CLEAR(xyz)
Free the pointer and then set it to NULL.
void SockList_NullTerminate(SockList *sl)
Adds a NUL byte without changing the length.
void esrv_update_stats(player *pl)
Sends a statistics update.
int SockList_ReadPacket(int fd, SockList *sl, int len)
This reads from fd and puts the data in sl.
void send_face_cmd(char *buff, int len, socket_struct *ns)
Client has requested pixmap that it somehow missed getting.
struct listen_info * listen
uint8_t * faces_sent
This is a bitmap on sent face status.
void move_cmd(char *buf, int len, player *pl)
Moves an object (typically, container to inventory).
void create_player_cmd(char *buf, int len, socket_struct *ns)
We have received a createplayer command.
SockList inbuf
If we get an incomplete packet, this is used to hold the data.
unsigned int tick_length(float seconds)
Calculate the number of ticks that correspond to real time.
void Send_With_Handling(socket_struct *ns, SockList *sl)
Calls Write_To_Socket to send data to the client.
void account_play_cmd(char *buf, int len, socket_struct *ns)
We have received an accountplay command.