Atrinik Client  4.0
client.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
44 #include <global.h>
45 #include <resources.h>
46 #include <toolkit/socket_crypto.h>
47 #include <toolkit/packet.h>
48 
51 
53 static socket_command_struct commands[CLIENT_CMD_NROF] = {
78  {NULL},
83 };
84 
89 void DoClient(void)
90 {
91  command_buffer *cmd;
92  /* Handle all enqueued commands */
93  while ((cmd = get_next_input_command()) != NULL) {
94  uint8_t *data = cmd->data;
95  size_t len = cmd->len;
96 
97  uint8_t *decrypted_data;
98  size_t decrypted_len;
99  bool was_decrypted = true;
100  if (socket_is_secure(csocket.sc)) {
101  if (!socket_crypto_decrypt(csocket.sc,
102  data,
103  len,
104  &decrypted_data,
105  &decrypted_len)) {
107  "!!! Cryptography decryption failed; someone is "
108  "likely hijacking your connection (MITM attack) !!!");
109  cpl.state = ST_START;
110  break;
111  }
112  } else {
113  decrypted_data = data;
114  decrypted_len = len;
115  was_decrypted = false;
116  }
117 
118  size_t pos = 0;
119  uint8_t type = packet_to_uint8(decrypted_data, decrypted_len, &pos);
120 
121  if (socket_is_secure(csocket.sc) &&
122  type != CLIENT_CMD_CRYPTO &&
123  !socket_crypto_is_done(socket_get_crypto(csocket.sc))) {
124  LOG(PACKET,
125  "Received non-crypto packet before crypto exchange from %s",
126  socket_get_str(csocket.sc));
127  cpl.state = ST_START;
128  break;
129  }
130 
131  if (type >= CLIENT_CMD_NROF || commands[type].handle_func == NULL) {
132  LOG(ERROR, "Bad command from server (%d)", type);
133  } else {
134  commands[type].handle_func(decrypted_data, decrypted_len, pos);
135  }
136 
137  if (was_decrypted) {
138  efree(decrypted_data);
139  }
140 
141  command_buffer_free(cmd);
142  }
143 }
144 
151 {
152  /* Check if it has been loaded. */
153  if (animations[anum].loaded) {
154  return;
155  }
156 
157  /* Mark this animation as loaded. */
158  animations[anum].loaded = 1;
159 
160  /* Same as server sends it */
161  socket_command_anim(anim_table[anum].anim_cmd, anim_table[anum].len, 0);
162 }
void socket_command_map(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:648
void socket_command_mapstats(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:618
Definition: main.h:252
void draw_info(const char *color, const char *str)
Definition: textwin.c:448
_anim_table * anim_table
Definition: main.c:68
void check_animation_status(int anum)
Definition: client.c:150
uint8_t data[1]
Definition: client.h:83
void socket_command_setup(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:47
void socket_command_item(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:494
void socket_command_crypto(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:1527
void DoClient(void)
Definition: client.c:89
size_t len
Definition: client.h:80
void socket_command_file_update(uint8_t *data, size_t len, size_t pos)
Definition: updates.c:61
void socket_command_notification(uint8_t *data, size_t len, size_t pos)
Definition: notification.c:106
void socket_command_sound_ambient(uint8_t *data, size_t len, size_t pos)
Definition: sound.c:844
void socket_command_book(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:40
void socket_command_compressed(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:931
#define COLOR_RED
Definition: text.h:295
void socket_command_item_update(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:567
void socket_command_item_delete(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:585
void socket_command_anim(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:76
client_socket_t csocket
Definition: main.c:51
void socket_command_sound(uint8_t *data, size_t len, size_t pos)
Definition: sound.c:701
void socket_command_image(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:101
void socket_command_characters(uint8_t *data, size_t len, size_t pos)
Definition: characters.c:583
void socket_command_player(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:346
Client_Player cpl
Definition: client.c:50
void command_buffer_free(command_buffer *buf)
Definition: socket.c:88
command_buffer * get_next_input_command(void)
Definition: socket.c:197
void socket_command_version(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:918
void socket_command_party(uint8_t *data, size_t len, size_t pos)
Definition: party.c:115
void socket_command_painting(uint8_t *data, size_t len, size_t pos)
Definition: painting.c:299
static socket_command_struct commands[CLIENT_CMD_NROF]
Definition: client.c:53
void socket_command_quickslots(uint8_t *data, size_t len, size_t pos)
Definition: quickslots.c:403
void socket_command_keepalive(uint8_t *data, size_t len, size_t pos)
Definition: main.c:159
void socket_command_resource(uint8_t *data, size_t len, size_t pos)
Definition: resources.c:100
void socket_command_drawinfo(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:134
void socket_command_target(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:153
void socket_command_control(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:956
void socket_command_interface(uint8_t *data, size_t len, size_t pos)
Definition: interface.c:391
void socket_command_stats(uint8_t *data, size_t len, size_t pos)
Definition: commands.c:166