Atrinik Server  4.0
init.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 
30 #include "zlib.h"
31 
32 #include <global.h>
33 #include <toolkit/packet.h>
34 #include <toolkit/string.h>
35 #include <exp.h>
36 #include <toolkit/path.h>
37 
42 
52 {
53  if (!socket_opt_non_blocking(ns->sc, true)) {
54  return false;
55  }
56 
57  if (!socket_opt_send_buffer(ns->sc, 65535)) {
58  return false;
59  }
60 
61  ns->login_count = 0;
62  ns->keepalive = 0;
63  ns->addme = 0;
64  ns->faceset = 0;
65  ns->sound = 0;
66  ns->ext_title_flag = 1;
67  ns->state = ST_LOGIN;
68  ns->mapx = 17;
69  ns->mapy = 17;
70  ns->mapx_2 = 8;
71  ns->mapy_2 = 8;
72  ns->password_fails = 0;
73  ns->is_bot = 0;
74  ns->account = NULL;
75  ns->socket_version = 0;
76 
77  ns->packet_recv = packet_new(0, 1024 * 3, 0);
78  ns->packet_recv_cmd = packet_new(0, 1024 * 64, 0);
79 
80  memset(&ns->lastmap, 0, sizeof(struct Map));
81  ns->packets = NULL;
82 
83  return true;
84 }
85 
90 {
91  int i;
92 
94 
95  for (i = 1; i < socket_info.allocated_sockets; i++) {
96  if (init_sockets[i].state != ST_AVAILABLE) {
97  free_newsocket(&init_sockets[i]);
98  }
99  }
100 
101  socket_destroy(init_sockets[0].sc);
102  efree(init_sockets);
103 }
104 
114 {
115  socket_destroy(ns->sc);
116 
117  if (ns->account) {
118  efree(ns->account);
119  }
120 
121  if (ns->packet_recv != NULL) {
122  packet_free(ns->packet_recv);
123  }
124 
125  if (ns->packet_recv_cmd != NULL) {
126  packet_free(ns->packet_recv_cmd);
127  }
128 
130  efree(ns);
131 }
132 
142 static void load_srv_file(char *fname, FILE *listing)
143 {
144  FILE *fp;
145  char *contents, *compressed, *cp;
146  StringBuffer *sb;
147  size_t fsize, numread;
148  struct stat statbuf;
149  unsigned long crc;
150 
151  if ((fp = fopen(fname, "rb")) == NULL) {
152  LOG(ERROR, "Can't open file %s", fname);
153  exit(1);
154  }
155 
156  fstat(fileno(fp), &statbuf);
157  fsize = statbuf.st_size;
158  /* Allocate a buffer to hold the whole file. */
159  contents = emalloc(fsize);
160  numread = fread(contents, 1, fsize, fp);
161  fclose(fp);
162 
163  /* Get a crc from the uncompressed file. */
164  crc = crc32(1L, (const unsigned char FAR *) contents, numread);
165 
166  /* Calculate the upper bound of the compressed size. */
167  numread = compressBound(fsize);
168  /* Allocate a buffer to hold the compressed file. */
169  compressed = emalloc(numread);
170  compress2((Bytef *) compressed, (uLong *) & numread,
171  (const unsigned char FAR *) contents, fsize, Z_BEST_COMPRESSION);
172 
173  sb = stringbuffer_new();
174  cp = path_basename(fname);
175  stringbuffer_append_printf(sb, "%s/http/data/%s.zz", settings.datapath, cp);
176  fprintf(listing, "%s:%"PRIx64":%"PRIx64"\n", cp, (uint64_t) crc,
177  (uint64_t) fsize);
178 
179  efree(cp);
180  cp = stringbuffer_finish(sb);
181  path_ensure_directories(cp);
182  fp = fopen(cp, "wb");
183 
184  if (fp == NULL) {
185  LOG(ERROR, "Could not open %s for writing.", cp);
186  exit(1);
187  }
188 
189  fwrite(compressed, 1, numread, fp);
190  fclose(fp);
191 
192  /* Free temporary buffers. */
193  efree(cp);
194  efree(contents);
195  efree(compressed);
196 }
197 
202 static void create_server_settings(void)
203 {
204  char buf[MAX_BUF];
205  size_t i;
206  FILE *fp;
207 
208  snprintf(buf, sizeof(buf), "%s/settings", settings.datapath);
209 
210  fp = fopen(buf, "wb");
211 
212  if (!fp) {
213  LOG(ERROR, "Couldn't create %s.", buf);
214  exit(1);
215  }
216 
217  /* Copy the default. */
218  snprintf(buf, sizeof(buf), "%s/server_settings", settings.libpath);
219 
220  if (!path_copy_file(buf, fp, "r")) {
221  LOG(ERROR, "Couldn't copy %s.", buf);
222  exit(1);
223  }
224 
225  for (i = 0; i < ALLOWED_CHARS_NUM; i++) {
226  fprintf(fp, "text %s\n", settings.allowed_chars[i]);
227  fprintf(fp, "text %"PRIu64 "-%"PRIu64 "\n", (uint64_t) settings.limits[i][0], (uint64_t) settings.limits[i][1]);
228  }
229 
230  /* Add the level information. */
231  fprintf(fp, "level %d\n", MAXLEVEL);
232 
233  for (i = 0; i <= MAXLEVEL; i++) {
234  fprintf(fp, "%"PRIx64 "\n", new_levels[i]);
235  }
236 
237  fclose(fp);
238 }
239 
243 static void create_server_animations(void)
244 {
245  char buf[MAX_BUF];
246  snprintf(VS(buf), "%s/anims", settings.datapath);
247 
248  FILE *fp = fopen(buf, "wb");
249  if (fp == NULL) {
250  LOG(ERROR, "Couldn't create %s.", buf);
251  exit(1);
252  }
253 
254  snprintf(VS(buf), "%s/animations", settings.libpath);
255  FILE *fp2 = fopen(buf, "rb");
256  if (fp2 == NULL) {
257  LOG(ERROR, "Couldn't open %s.", buf);
258  exit(1);
259  }
260 
261  while (fgets(VS(buf), fp2)) {
262  /* Copy anything but face names. */
263  if (strncmp(buf, "anim ", 5) == 0 || strcmp(buf, "mina\n") == 0 ||
264  strncmp(buf, "facings ", 8) == 0) {
265  fputs(buf, fp);
266  } else {
267  char *end = strchr(buf, '\n');
268  if (end != NULL) {
269  *end = '\0';
270  }
271 
272  /* Transform face names into IDs. */
273  fprintf(fp, "%d\n", find_face(buf, 0));
274  }
275  }
276 
277  fclose(fp2);
278  fclose(fp);
279 }
280 
287 void init_srv_files(void)
288 {
289  char buf[MAX_BUF];
290  FILE *fp;
291 
292  snprintf(buf, sizeof(buf), "%s/http/data/listing.txt", settings.datapath);
293  path_ensure_directories(buf);
294  fp = fopen(buf, "w");
295 
296  if (fp == NULL) {
297  LOG(ERROR, "Could not open %s for writing.", buf);
298  exit(1);
299  }
300 
301  snprintf(buf, sizeof(buf), "%s/bmaps", settings.datapath);
302  load_srv_file(buf, fp);
303 
304  snprintf(buf, sizeof(buf), "%s/"UPDATES_FILE_NAME, settings.datapath);
305  load_srv_file(buf, fp);
306 
308  snprintf(buf, sizeof(buf), "%s/settings", settings.datapath);
309  load_srv_file(buf, fp);
310 
312  snprintf(buf, sizeof(buf), "%s/anims", settings.datapath);
313  load_srv_file(buf, fp);
314 
315  snprintf(buf, sizeof(buf), "%s/effects", settings.libpath);
316  load_srv_file(buf, fp);
317 
318  snprintf(buf, sizeof(buf), "%s/hfiles", settings.libpath);
319  load_srv_file(buf, fp);
320 
321  fclose(fp);
322 }
socket_t * sc
Definition: newserver.h:109
int find_face(const char *name, int error)
Definition: image.c:172
uint8_t faceset
Definition: newserver.h:152
char datapath[MAX_BUF]
Definition: global.h:343
struct Map lastmap
Definition: newserver.h:167
struct packet_struct * packets
Definition: newserver.h:170
#define UPDATES_FILE_NAME
Definition: newserver.h:231
void free_newsocket(socket_struct *ns)
Definition: init.c:113
static void load_srv_file(char *fname, FILE *listing)
Definition: init.c:142
uint32_t socket_version
Definition: newserver.h:130
uint64_t new_levels[MAXLEVEL+2]
Definition: exp.c:38
bool init_connection(socket_struct *ns)
Definition: init.c:51
char libpath[MAX_BUF]
Definition: global.h:338
void free_socket_images(void)
Definition: image.c:94
size_t limits[ALLOWED_CHARS_NUM][2]
Definition: global.h:434
uint32_t sound
Definition: newserver.h:143
uint32_t keepalive
Definition: newserver.h:177
uint8_t is_bot
Definition: newserver.h:146
void free_all_newserver(void)
Definition: init.c:89
Definition: newserver.h:91
uint8_t ext_title_flag
Definition: newserver.h:161
uint32_t addme
Definition: newserver.h:140
void socket_buffer_clear(socket_struct *ns)
Definition: lowlevel.c:73
void init_srv_files(void)
Definition: init.c:287
static void create_server_settings(void)
Definition: init.c:202
char allowed_chars[ALLOWED_CHARS_NUM][MAX_BUF]
Definition: global.h:429
struct settings_struct settings
Definition: init.c:55
#define MAXLEVEL
Definition: global.h:221
uint8_t password_fails
Definition: newserver.h:158
static void create_server_animations(void)
Definition: init.c:243
Socket_Info socket_info
Definition: init.c:39
socket_struct * init_sockets
Definition: init.c:41