/[LeafOK_CVS]/pvpgn-1.7.4/src/d2cs/handle_d2cs.c
ViewVC logotype

Contents of /pvpgn-1.7.4/src/d2cs/handle_d2cs.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Tue Jun 13 12:19:11 2006 UTC (19 years, 9 months ago) by sysadm
Branch: MAIN
Changes since 1.3: +38 -12 lines
Content type: text/x-csrc
Antibot (for non-password protected game)

1 /*
2 * Copyright (C) 2000,2001 Onlyer (onlyer@263.net)
3 * Copyright (C) 2004 Olaf Freyer (aaron@cs.tu-berlin.de)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include "common/setup_before.h"
20 #include "setup.h"
21
22 #include <stdio.h>
23 #ifdef HAVE_STRING_H
24 # include <string.h>
25 #else
26 # ifdef HAVE_STRINGS_H
27 # include <strings.h>
28 # endif
29 # ifdef HAVE_MEMORY_H
30 # include <memory.h>
31 # endif
32 #endif
33 #ifdef STDC_HEADERS
34 # include <stdlib.h>
35 #else
36 # ifdef HAVE_MALLOC_H
37 # include <malloc.h>
38 # endif
39 #endif
40 #include "compat/memcpy.h"
41 #ifdef TIME_WITH_SYS_TIME
42 # include <time.h>
43 # include <sys/time.h>
44 #else
45 # ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h>
47 # else
48 # include <time.h>
49 # endif
50 #endif
51 #ifdef HAVE_UNISTD_H
52 # include <unistd.h>
53 #endif
54 #ifdef HAVE_SYS_STAT_H
55 # include <sys/stat.h>
56 #endif
57
58 #ifdef HAVE_SYS_SOCKET_H
59 # include <sys/socket.h>
60 #endif
61 #ifdef HAVE_NETINET_IN_H
62 # include <netinet/in.h>
63 #endif
64 #include "compat/socket.h"
65 #include "compat/netinet_in.h"
66 #ifdef HAVE_ARPA_INET_H
67 # include <arpa/inet.h>
68 #endif
69 #include "compat/inet_ntoa.h"
70
71 #include "compat/pdir.h"
72 #include "compat/mkdir.h"
73 #include "d2charfile.h"
74 #include "connection.h"
75 #include "d2charstatus.h"
76 #include "game.h"
77 #include "bnetd.h"
78 #include "d2cs_protocol.h"
79 #include "d2cs_bnetd_protocol.h"
80 #include "handle_d2cs.h"
81 #include "d2ladder.h"
82 #include "gamequeue.h"
83 #include "serverqueue.h"
84 #include "prefs.h"
85 #include "common/bn_type.h"
86 #include "common/queue.h"
87 #include "common/packet.h"
88 #include "common/eventlog.h"
89 #include "d2charlist.h"
90 #include "common/elist.h"
91 #include "common/xalloc.h"
92 #include "common/setup_after.h"
93
94 /* sowater */
95 #include "d2cs_random.h"
96
97 static int d2cs_send_client_ladder(t_connection * c, unsigned char type, unsigned short from);
98 static unsigned int d2cs_try_joingame(t_connection const * c, t_game const * game, char const * gamepass);
99
100 DECLARE_PACKET_HANDLER(on_client_loginreq)
101 DECLARE_PACKET_HANDLER(on_client_createcharreq)
102 DECLARE_PACKET_HANDLER(on_client_creategamereq)
103 DECLARE_PACKET_HANDLER(on_client_joingamereq)
104 DECLARE_PACKET_HANDLER(on_client_gamelistreq)
105 DECLARE_PACKET_HANDLER(on_client_gameinforeq)
106 DECLARE_PACKET_HANDLER(on_client_charloginreq)
107 DECLARE_PACKET_HANDLER(on_client_deletecharreq)
108 DECLARE_PACKET_HANDLER(on_client_ladderreq)
109 DECLARE_PACKET_HANDLER(on_client_motdreq)
110 DECLARE_PACKET_HANDLER(on_client_cancelcreategame)
111 DECLARE_PACKET_HANDLER(on_client_charladderreq)
112 DECLARE_PACKET_HANDLER(on_client_charlistreq)
113 DECLARE_PACKET_HANDLER(on_client_charlistreq_110)
114 DECLARE_PACKET_HANDLER(on_client_convertcharreq)
115
116
117 static t_packet_handle_table d2cs_packet_handle_table[]={
118 /* 0x00 */ { 0, conn_state_none, NULL },
119 /* 0x01 */ { sizeof(t_client_d2cs_loginreq), conn_state_connected, on_client_loginreq },
120 /* 0x02 */ { sizeof(t_client_d2cs_createcharreq), conn_state_authed|conn_state_char_authed, on_client_createcharreq },
121 /* 0x03 */ { sizeof(t_client_d2cs_creategamereq), conn_state_char_authed, on_client_creategamereq },
122 /* 0x04 */ { sizeof(t_client_d2cs_joingamereq), conn_state_char_authed, on_client_joingamereq },
123 /* 0x05 */ { sizeof(t_client_d2cs_gamelistreq), conn_state_char_authed, on_client_gamelistreq },
124 /* 0x06 */ { sizeof(t_client_d2cs_gameinforeq), conn_state_char_authed, on_client_gameinforeq },
125 /* 0x07 */ { sizeof(t_client_d2cs_charloginreq), conn_state_authed|conn_state_char_authed, on_client_charloginreq },
126 /* 0x08 */ { 0, conn_state_none, NULL },
127 /* 0x09 */ { 0, conn_state_none, NULL },
128 /* 0x0a */ { sizeof(t_client_d2cs_deletecharreq), conn_state_authed|conn_state_char_authed, on_client_deletecharreq },
129 /* 0x0b */ { 0, conn_state_none, NULL },
130 /* 0x0c */ { 0, conn_state_none, NULL },
131 /* 0x0d */ { 0, conn_state_none, NULL },
132 /* 0x0e */ { 0, conn_state_none, NULL },
133 /* 0x0f */ { 0, conn_state_none, NULL },
134 /* 0x10 */ { 0, conn_state_none, NULL },
135 /* 0x11 */ { sizeof(t_client_d2cs_ladderreq), conn_state_char_authed, on_client_ladderreq },
136 /* 0x12 */ { sizeof(t_client_d2cs_motdreq), conn_state_char_authed, on_client_motdreq },
137 /* 0x13 */ { sizeof(t_client_d2cs_cancelcreategame),conn_state_char_authed, on_client_cancelcreategame},
138 /* 0x14 */ { 0, conn_state_none, NULL },
139 /* 0x15 */ { 0, conn_state_none, NULL },
140 /* 0x16 */ { sizeof(t_client_d2cs_charladderreq), conn_state_char_authed, on_client_charladderreq },
141 /* 0x17 */ { sizeof(t_client_d2cs_charlistreq), conn_state_authed|conn_state_char_authed, on_client_charlistreq },
142 /* 0x18 */ { sizeof(t_client_d2cs_convertcharreq), conn_state_authed|conn_state_char_authed, on_client_convertcharreq },
143 /* 0x19 */ { sizeof(t_client_d2cs_charlistreq_110), conn_state_authed|conn_state_char_authed, on_client_charlistreq_110 }
144 };
145
146
147 extern int d2cs_handle_d2cs_packet(t_connection * c, t_packet * packet)
148 {
149 return conn_process_packet(c,packet,d2cs_packet_handle_table,NELEMS(d2cs_packet_handle_table));
150 }
151
152 static int on_client_loginreq(t_connection * c, t_packet * packet)
153 {
154 t_packet * bnpacket;
155 char const * account;
156 t_sq * sq;
157 unsigned int sessionnum;
158
159 if (!(account=packet_get_str_const(packet,sizeof(t_client_d2cs_loginreq),MAX_CHARNAME_LEN))) {
160 eventlog(eventlog_level_error,__FUNCTION__,"got bad account name");
161 return -1;
162 }
163 if (d2char_check_acctname(account)<0) {
164 eventlog(eventlog_level_error,__FUNCTION__,"got bad account name");
165 return -1;
166 }
167 if (!bnetd_conn()) {
168 eventlog(eventlog_level_warn,__FUNCTION__,"d2cs is offline with bnetd, login request will be rejected");
169 return -1;
170 }
171 sessionnum=bn_int_get(packet->u.client_d2cs_loginreq.sessionnum);
172 conn_set_bnetd_sessionnum(c,sessionnum);
173 eventlog(eventlog_level_info,__FUNCTION__,"got client (*%s) login request sessionnum=0x%X",account,sessionnum);
174 if ((bnpacket=packet_create(packet_class_d2cs_bnetd))) {
175 if ((sq=sq_create(d2cs_conn_get_sessionnum(c),packet,0))) {
176 packet_set_size(bnpacket,sizeof(t_d2cs_bnetd_accountloginreq));
177 packet_set_type(bnpacket,D2CS_BNETD_ACCOUNTLOGINREQ);
178 bn_int_set(&bnpacket->u.d2cs_bnetd_accountloginreq.h.seqno,sq_get_seqno(sq));
179 bn_int_set(&bnpacket->u.d2cs_bnetd_accountloginreq.seqno,
180 bn_int_get(packet->u.client_d2cs_loginreq.seqno));
181 bn_int_set(&bnpacket->u.d2cs_bnetd_accountloginreq.sessionkey,
182 bn_int_get(packet->u.client_d2cs_loginreq.sessionkey));
183 bn_int_set(&bnpacket->u.d2cs_bnetd_accountloginreq.sessionnum,sessionnum);
184 memcpy(bnpacket->u.d2cs_bnetd_accountloginreq.secret_hash,
185 packet->u.client_d2cs_loginreq.secret_hash,
186 sizeof(bnpacket->u.d2cs_bnetd_accountloginreq.secret_hash));
187 packet_append_string(bnpacket,account);
188 conn_push_outqueue(bnetd_conn(),bnpacket);
189 }
190 packet_del_ref(bnpacket);
191 }
192 return 0;
193 }
194
195 static int on_client_createcharreq(t_connection * c, t_packet * packet)
196 {
197 t_packet * rpacket, * bnpacket;
198 char const * charname;
199 char const * account;
200 char * path;
201 t_pdir * dir;
202 t_sq * sq;
203 unsigned int reply;
204 unsigned short status, class;
205 t_d2charinfo_file data;
206 char checknum;
207 if (!(charname=packet_get_str_const(packet,sizeof(t_client_d2cs_createcharreq),MAX_CHARNAME_LEN))) {
208 eventlog(eventlog_level_error,__FUNCTION__,"got bad character name");
209 return -1;
210 }
211 if (!(account=d2cs_conn_get_account(c))) {
212 eventlog(eventlog_level_error,__FUNCTION__,"missing account for character %s",charname);
213 return -1;
214 }
215 class=bn_short_get(packet->u.client_d2cs_createcharreq.class);
216 status=bn_short_get(packet->u.client_d2cs_createcharreq.status);
217
218 path=xmalloc(strlen(prefs_get_charinfo_dir())+1+strlen(account)+1);
219 d2char_get_infodir_name(path,account);
220 if (!(dir=p_opendir(path))) {
221 eventlog(eventlog_level_info,__FUNCTION__,"(*%s) charinfo directory do not exist, building it",account);
222 p_mkdir(path,S_IRWXU);
223 }
224 else
225 p_closedir(dir);
226 xfree(path);
227
228 /* generate checknum, save it in t_connection, and send it to bnetd, by sowater */
229 do {
230 checknum=d2cs_random_char();
231 }while(d2cs_conn_get_checknum(c)==checknum);
232 d2cs_conn_set_checknum(c,checknum);
233 eventlog(eventlog_level_info,__FUNCTION__,"set checknum %c to connection",checknum);
234
235 if (d2char_create(account,charname,class,status)<0) {
236 eventlog(eventlog_level_warn,__FUNCTION__,"error create character %s for account %s",charname,account);
237 reply=D2CS_CLIENT_CREATECHARREPLY_ALREADY_EXIST;
238 } else if (d2charinfo_load(account,charname,&data)<0) {
239 eventlog(eventlog_level_error,__FUNCTION__,"error loading charinfo for character %s(*%s)",charname,account);
240 reply=D2CS_CLIENT_CREATECHARREPLY_FAILED;
241 } else {
242 eventlog(eventlog_level_info,__FUNCTION__,"character %s(*%s) created",charname,account);
243 reply=D2CS_CLIENT_CREATECHARREPLY_SUCCEED;
244 conn_set_charinfo(c,&data.summary);
245 if ((bnpacket=packet_create(packet_class_d2cs_bnetd))) {
246 if ((sq=sq_create(d2cs_conn_get_sessionnum(c),packet,0))) {
247 packet_set_size(bnpacket,sizeof(t_d2cs_bnetd_charloginreq));
248 packet_set_type(bnpacket,D2CS_BNETD_CHARLOGINREQ);
249 bn_int_set(&bnpacket->u.d2cs_bnetd_charloginreq.h.seqno,sq_get_seqno(sq));
250 bn_int_set(&bnpacket->u.d2cs_bnetd_charloginreq.sessionnum,
251 conn_get_bnetd_sessionnum(c));
252 packet_append_string(bnpacket,charname);
253 packet_append_string(bnpacket,(char const *)&data.portrait);
254
255 /* sowater */
256 packet_append_data(bnpacket,&checknum,sizeof(checknum));
257
258 conn_push_outqueue(bnetd_conn(),bnpacket);
259 }
260 packet_del_ref(bnpacket);
261 }
262 return 0;
263 }
264 if ((rpacket=packet_create(packet_class_d2cs))) {
265 packet_set_size(rpacket,sizeof(t_d2cs_client_createcharreply));
266 packet_set_type(rpacket,D2CS_CLIENT_CREATECHARREPLY);
267 bn_int_set(&rpacket->u.d2cs_client_createcharreply.reply,reply);
268 conn_push_outqueue(c,rpacket);
269 packet_del_ref(rpacket);
270 }
271 return 0;
272 }
273
274 static int on_client_creategamereq(t_connection * c, t_packet * packet)
275 {
276 char const * gamename;
277 char const * gamepass;
278 char const * gamedesc;
279 t_game * game;
280 t_d2gs * gs;
281 t_gq * gq;
282 unsigned int tempflag,gameflag;
283 unsigned int leveldiff, maxchar, difficulty, expansion, hardcore, ladder;
284 unsigned int seqno, reply;
285 unsigned int pos;
286 t_elem * elem;
287 t_charstatus * cs;
288
289
290 pos=sizeof(t_client_d2cs_creategamereq);
291 if (!(gamename=packet_get_str_const(packet,pos,MAX_GAMENAME_LEN))) {
292 eventlog(eventlog_level_error,__FUNCTION__,"got bad game name");
293 return -1;
294 }
295 pos+=strlen(gamename)+1;
296 if (!(gamepass=packet_get_str_const(packet,pos,MAX_GAMEPASS_LEN))) {
297 eventlog(eventlog_level_error,__FUNCTION__,"got bad game pass");
298 return -1;
299 }
300 pos+=strlen(gamepass)+1;
301 if (!(gamedesc=packet_get_str_const(packet,pos,MAX_GAMEDESC_LEN))) {
302 eventlog(eventlog_level_error,__FUNCTION__,"got bad game desc");
303 return -1;
304 }
305
306 tempflag=bn_int_get(packet->u.client_d2cs_creategamereq.gameflag);
307 leveldiff=bn_byte_get(packet->u.client_d2cs_creategamereq.leveldiff);
308 maxchar=bn_byte_get(packet->u.client_d2cs_creategamereq.maxchar);
309 difficulty=gameflag_get_difficulty(tempflag);
310 if (difficulty > conn_get_charinfo_difficulty(c)) {
311 eventlog(eventlog_level_error,__FUNCTION__,"game difficulty exceed character limit %d %d",difficulty,
312 conn_get_charinfo_difficulty(c));
313 return 0;
314 }
315 expansion=conn_get_charinfo_expansion(c);
316 hardcore=conn_get_charinfo_hardcore(c);
317 ladder=conn_get_charinfo_ladder(c);
318 gameflag=gameflag_create(ladder,expansion,hardcore,difficulty);
319
320 gs = NULL;
321 game = NULL;
322 gq=conn_get_gamequeue(c);
323 if (d2cs_gamelist_find_game(gamename)) {
324 eventlog(eventlog_level_info,__FUNCTION__,"game name %s is already exist in gamelist",gamename);
325 reply=D2CS_CLIENT_CREATEGAMEREPLY_NAME_EXIST;
326
327 /* right checknum? sowater */
328 } else if((prefs_antibot_when_password()==0) && prefs_enable_antibot() && d2cs_conn_verify_checknum(c, *gamedesc)) {
329 eventlog(eventlog_level_info,__FUNCTION__,"checksum %c from packet not match checksum from connection",*gamedesc);
330 reply=D2CS_CLIENT_CREATEGAMEREPLY_FAILED;
331 /* when antibot_when_password is true, by sowater, 20051001 */
332 } else if((strlen(gamepass)!=0) && prefs_antibot_when_password() && prefs_enable_antibot()
333 && d2cs_conn_verify_checknum(c, *gamedesc)) {
334 eventlog(eventlog_level_info,__FUNCTION__,"checksum %c from packet not match checksum from connection",*gamedesc);
335 reply=D2CS_CLIENT_CREATEGAMEREPLY_FAILED;
336 } else if (!gq && gqlist_find_game(gamename)) {
337 eventlog(eventlog_level_info,__FUNCTION__,"game name %s is already exist in game queue",gamename);
338 reply=D2CS_CLIENT_CREATEGAMEREPLY_NAME_EXIST;
339 } else if (!(gs=d2gslist_choose_server(c))) {
340 if (gq) {
341 eventlog(eventlog_level_error,__FUNCTION__,"client %d is already in game queue",d2cs_conn_get_sessionnum(c));
342 conn_set_gamequeue(c,NULL);
343 gq_destroy(gq,&elem);
344 return 0;
345 } else if ((gq=gq_create(d2cs_conn_get_sessionnum(c), packet, gamename))) {
346 conn_set_gamequeue(c,gq);
347 d2cs_send_client_creategamewait(c,gqlist_get_length());
348 return 0;
349 }
350 reply=D2CS_CLIENT_CREATEGAMEREPLY_FAILED;
351 } else if (hardcore && conn_get_charinfo_dead(c)) {
352 reply=D2CS_CLIENT_CREATEGAMEREPLY_FAILED;
353 } else if (!(game=d2cs_game_create(gamename,gamepass,gamedesc,gameflag))) {
354 reply=D2CS_CLIENT_CREATEGAMEREPLY_NAME_EXIST;
355 } else {
356 /* By Leaflet, 20060613 */
357 if ((cs=charstatus_list_find_charstatus_by_charname(c->charname)) == NULL)
358 cs = charstatus_create(c->charname);
359
360 if (time(NULL) - cs->last_create_time <= 90)
361 {
362 cs->frequently_create_count++;
363 }
364
365 if (prefs_enable_antibot() && cs->frequently_create_count > 5 && d2cs_conn_verify_checknum(c, *gamedesc)) {
366 eventlog(eventlog_level_info,__FUNCTION__,"checksum %c from packet not match checksum from connection",*gamedesc);
367 reply=D2CS_CLIENT_CREATEGAMEREPLY_FAILED;
368 } else {
369 if (strlen(gamedesc)!=0 && !d2cs_conn_verify_checknum(c, *gamedesc))
370 cs->frequently_create_count = 0;
371
372 if (time(NULL) - cs->last_create_time > 90)
373 {
374 cs->frequently_create_count--;
375 if (cs->frequently_create_count<0)
376 cs->frequently_create_count=0;
377 }
378 cs->last_create_time = time(NULL);
379
380 reply=D2CS_CLIENT_CREATEGAMEREPLY_SUCCEED;
381 game_set_d2gs(game,gs);
382 d2gs_add_gamenum(gs, 1);
383 game_set_gameflag_ladder(game,ladder);
384 game_set_gameflag_expansion(game,expansion);
385 game_set_created(game,0);
386 game_set_leveldiff(game,leveldiff);
387 game_set_charlevel(game,conn_get_charinfo_level(c));
388 game_set_maxchar(game,maxchar);
389 game_set_gameflag_difficulty(game,difficulty);
390 game_set_gameflag_hardcore(game,hardcore);
391 }
392 }
393
394 seqno=bn_short_get(packet->u.client_d2cs_creategamereq.seqno);
395 if (reply!=D2CS_CLIENT_CREATEGAMEREPLY_SUCCEED) {
396 t_packet * rpacket;
397
398 if ((rpacket=packet_create(packet_class_d2cs))) {
399 packet_set_size(rpacket,sizeof(t_d2cs_client_creategamereply));
400 packet_set_type(rpacket,D2CS_CLIENT_CREATEGAMEREPLY);
401 bn_short_set(&rpacket->u.d2cs_client_creategamereply.seqno,seqno);
402 bn_short_set(&rpacket->u.d2cs_client_creategamereply.u1,0);
403 bn_short_set(&rpacket->u.d2cs_client_creategamereply.gameid,0);
404 bn_int_set(&rpacket->u.d2cs_client_creategamereply.reply,reply);
405 conn_push_outqueue(c,rpacket);
406 packet_del_ref(rpacket);
407 }
408 } else {
409 t_packet * gspacket;
410 t_sq * sq;
411 struct in_addr addr;
412
413 if ((gspacket=packet_create(packet_class_d2gs))) {
414 if ((sq=sq_create(d2cs_conn_get_sessionnum(c),packet,d2cs_game_get_id(game)))) {
415 packet_set_size(gspacket,sizeof(t_d2cs_d2gs_creategamereq));
416 packet_set_type(gspacket,D2CS_D2GS_CREATEGAMEREQ);
417 bn_int_set(&gspacket->u.d2cs_d2gs_creategamereq.h.seqno,sq_get_seqno(sq));
418 bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.difficulty,difficulty);
419 bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.hardcore,hardcore);
420 bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.expansion,expansion);
421 bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.ladder,ladder);
422 packet_append_string(gspacket,gamename);
423 packet_append_string(gspacket,gamepass);
424 packet_append_string(gspacket,gamedesc);
425 packet_append_string(gspacket,d2cs_conn_get_account(c));
426 packet_append_string(gspacket,d2cs_conn_get_charname(c));
427 addr.s_addr = htonl(d2cs_conn_get_addr(c));
428 packet_append_string(gspacket,inet_ntoa(addr));
429 conn_push_outqueue(d2gs_get_connection(gs),gspacket);
430 }
431 packet_del_ref(gspacket);
432 eventlog(eventlog_level_info,__FUNCTION__,"request create game %s on gs %d",gamename,d2gs_get_id(gs));
433 }
434 }
435 return 0;
436 }
437
438 static int on_client_joingamereq(t_connection * c, t_packet * packet)
439 {
440 char const * gamename;
441 char const * gamepass;
442 char const * charname;
443 char const * account;
444 t_game * game;
445 t_d2gs * gs;
446 int reply;
447 unsigned int pos;
448 unsigned int seqno;
449
450 gs = NULL;
451 pos=sizeof(t_client_d2cs_joingamereq);
452 if (!(gamename=packet_get_str_const(packet,pos,MAX_GAMENAME_LEN))) {
453 eventlog(eventlog_level_error,__FUNCTION__,"got bad game name");
454 return -1;
455 }
456 pos+=strlen(gamename)+1;
457 if (!(gamepass=packet_get_str_const(packet,pos,MAX_GAMEPASS_LEN))) {
458 eventlog(eventlog_level_error,__FUNCTION__,"got bad game pass");
459 return -1;
460 }
461 if (!(charname=d2cs_conn_get_charname(c))) {
462 eventlog(eventlog_level_error,__FUNCTION__,"missing character name for connection");
463 return -1;
464 }
465 if (!(account=d2cs_conn_get_account(c))) {
466 eventlog(eventlog_level_error,__FUNCTION__,"missing account for connection");
467 return -1;
468 }
469 if (conn_check_multilogin(c,charname)<0) {
470 eventlog(eventlog_level_error,__FUNCTION__,"character %s is already logged in",charname);
471 return -1;
472 }
473 if (!(game=d2cs_gamelist_find_game(gamename))) {
474 eventlog(eventlog_level_info,__FUNCTION__,"game %s not found",gamename);
475 reply=D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST;
476 } else if (!(gs=game_get_d2gs(game))) {
477 eventlog(eventlog_level_error,__FUNCTION__,"missing game server for game %s",gamename);
478 reply=D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST;
479 } else {
480 reply=d2cs_try_joingame(c,game,gamepass);
481 }
482
483 seqno=bn_short_get(packet->u.client_d2cs_joingamereq.seqno);
484 if (reply!=D2CS_CLIENT_JOINGAMEREPLY_SUCCEED) {
485 t_packet * rpacket;
486
487 if ((rpacket=packet_create(packet_class_d2cs))) {
488 packet_set_size(rpacket,sizeof(t_d2cs_client_joingamereply));
489 packet_set_type(rpacket,D2CS_CLIENT_JOINGAMEREPLY);
490 bn_short_set(&rpacket->u.d2cs_client_joingamereply.seqno,seqno);
491 bn_short_set(&rpacket->u.d2cs_client_joingamereply.u1,0);
492 bn_short_set(&rpacket->u.d2cs_client_joingamereply.gameid,0);
493 bn_int_set(&rpacket->u.d2cs_client_joingamereply.addr,0);
494 bn_int_set(&rpacket->u.d2cs_client_joingamereply.token,0);
495 bn_int_set(&rpacket->u.d2cs_client_joingamereply.reply,reply);
496 conn_push_outqueue(c,rpacket);
497 packet_del_ref(rpacket);
498 }
499 } else {
500 t_packet * gspacket;
501 t_sq * sq;
502 struct in_addr addr;
503
504 if ((gspacket=packet_create(packet_class_d2gs))) {
505 if ((sq=sq_create(d2cs_conn_get_sessionnum(c),packet,d2cs_game_get_id(game)))) {
506 packet_set_size(gspacket,sizeof(t_d2cs_d2gs_joingamereq));
507 packet_set_type(gspacket,D2CS_D2GS_JOINGAMEREQ);
508 bn_int_set(&gspacket->u.d2cs_d2gs_joingamereq.h.seqno,sq_get_seqno(sq));
509 bn_int_set(&gspacket->u.d2cs_d2gs_joingamereq.gameid,
510 game_get_d2gs_gameid(game));
511 sq_set_gametoken(sq,d2gs_make_token(gs));
512 bn_int_set(&gspacket->u.d2cs_d2gs_joingamereq.token,sq_get_gametoken(sq));
513 packet_append_string(gspacket,charname);
514 packet_append_string(gspacket,account);
515 addr.s_addr = htonl(d2cs_conn_get_addr(c));
516 packet_append_string(gspacket,inet_ntoa(addr));
517 conn_push_outqueue(d2gs_get_connection(gs),gspacket);
518 }
519 packet_del_ref(gspacket);
520 eventlog(eventlog_level_info,__FUNCTION__,"request join game %s for character %s on gs %d",gamename,
521 charname,d2gs_get_id(gs));
522 }
523 }
524 return 0;
525 }
526
527 static int on_client_gamelistreq(t_connection * c, t_packet * packet)
528 {
529 t_packet * rpacket;
530 t_game * game;
531 unsigned int count;
532 unsigned int seqno;
533 time_t now;
534 unsigned int maxlifetime;
535 t_elem const * start_elem;
536 t_elem const * elem;
537
538 seqno=bn_short_get(packet->u.client_d2cs_gamelistreq.seqno);
539 /* if (seqno%2) return 0; */
540 count=0;
541 now=time(NULL);
542 maxlifetime=prefs_get_game_maxlifetime();
543
544 elem=start_elem=gamelist_get_curr_elem();
545 if (!elem) elem=list_get_first_const(d2cs_gamelist());
546 else elem=elem_get_next_const(d2cs_gamelist(),elem);
547
548 for (; elem != start_elem; elem=elem_get_next_const(d2cs_gamelist(),elem)) {
549 if (!elem) {
550 elem=list_get_first_const(d2cs_gamelist());
551 if (elem == start_elem) break;
552 }
553 if (!(game=elem_get_data(elem))) {
554 eventlog(eventlog_level_error,__FUNCTION__,"got NULL game");
555 break;
556 }
557 if (maxlifetime && (now-game->create_time>maxlifetime)) continue;
558 if (!game_get_currchar(game)) continue;
559 if (!prefs_allow_gamelist_showall()) {
560 if (conn_get_charinfo_difficulty(c)!=game_get_gameflag_difficulty(game)) continue;
561 }
562 if (d2cs_try_joingame(c,game,"")!=D2CS_CLIENT_JOINGAMEREPLY_SUCCEED) continue;
563 if ((rpacket=packet_create(packet_class_d2cs))) {
564 packet_set_size(rpacket,sizeof(t_d2cs_client_gamelistreply));
565 packet_set_type(rpacket,D2CS_CLIENT_GAMELISTREPLY);
566 bn_short_set(&rpacket->u.d2cs_client_gamelistreply.seqno,seqno);
567 bn_int_set(&rpacket->u.d2cs_client_gamelistreply.token,d2cs_game_get_id(game));
568 bn_byte_set(&rpacket->u.d2cs_client_gamelistreply.currchar,game_get_currchar(game));
569 bn_int_set(&rpacket->u.d2cs_client_gamelistreply.gameflag,game_get_gameflag(game));
570 packet_append_string(rpacket,d2cs_game_get_name(game));
571 packet_append_string(rpacket,game_get_desc(game));
572 conn_push_outqueue(c,rpacket);
573 packet_del_ref(rpacket);
574 count++;
575 if (prefs_get_maxgamelist() && count>=prefs_get_maxgamelist()) break;
576 }
577 }
578 gamelist_set_curr_elem(elem);
579 if (count) {
580 if ((rpacket=packet_create(packet_class_d2cs))) {
581 packet_set_size(rpacket,sizeof(t_d2cs_client_gamelistreply));
582 packet_set_type(rpacket,D2CS_CLIENT_GAMELISTREPLY);
583 bn_short_set(&rpacket->u.d2cs_client_gamelistreply.seqno,seqno);
584 bn_int_set(&rpacket->u.d2cs_client_gamelistreply.token,0);
585 bn_byte_set(&rpacket->u.d2cs_client_gamelistreply.currchar,0);
586 bn_int_set(&rpacket->u.d2cs_client_gamelistreply.gameflag,0);
587 packet_append_string(rpacket,"");
588 packet_append_string(rpacket,"");
589 packet_append_string(rpacket,"");
590 conn_push_outqueue(c,rpacket);
591 packet_del_ref(rpacket);
592 }
593 }
594 return 0;
595 }
596
597 static int on_client_gameinforeq(t_connection * c, t_packet * packet)
598 {
599 t_game_charinfo * info;
600 t_packet * rpacket;
601 char const * gamename;
602 t_game * game;
603 unsigned int seqno, n;
604
605 if (!(gamename=packet_get_str_const(packet,sizeof(t_client_d2cs_gameinforeq),MAX_GAMENAME_LEN))) {
606 eventlog(eventlog_level_error,__FUNCTION__,"got bad game name");
607 return -1;
608 }
609 if (!(game=d2cs_gamelist_find_game(gamename))) {
610 eventlog(eventlog_level_error,__FUNCTION__,"game %s not found",gamename);
611 return 0;
612 }
613 seqno=bn_short_get(packet->u.client_d2cs_gameinforeq.seqno);
614 if ((rpacket=packet_create(packet_class_d2cs))) {
615 packet_set_size(rpacket,sizeof(t_d2cs_client_gameinforeply));
616 packet_set_type(rpacket,D2CS_CLIENT_GAMEINFOREPLY);
617 bn_short_set(&rpacket->u.d2cs_client_gameinforeply.seqno,seqno);
618 bn_int_set(&rpacket->u.d2cs_client_gameinforeply.gameflag,game_get_gameflag(game));
619 bn_int_set(&rpacket->u.d2cs_client_gameinforeply.etime,time(NULL)-d2cs_game_get_create_time(game));
620 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.charlevel,game_get_charlevel(game));
621 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.leveldiff,game_get_leveldiff(game));
622 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.maxchar,game_get_maxchar(game));
623 packet_append_string(rpacket, game_get_desc(game) ? game_get_desc(game) : NULL);
624
625 n=0;
626 BEGIN_LIST_TRAVERSE_DATA_CONST(game_get_charlist(game),info)
627 {
628 if (!info->charname) {
629 eventlog(eventlog_level_error,__FUNCTION__,"got NULL charname in game %s char list",gamename);
630 continue;
631 }
632 packet_append_string(rpacket,info->charname);
633 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.class[n],info->class);
634 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.level[n],info->level);
635 n++;
636 }
637 END_LIST_TRAVERSE_DATA_CONST()
638
639 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.currchar,n);
640 if (n!=game_get_currchar(game)) {
641 eventlog(eventlog_level_error,__FUNCTION__,"game %s character list corrupted",gamename);
642 }
643 conn_push_outqueue(c,rpacket);
644 packet_del_ref(rpacket);
645 }
646 return 0;
647 }
648
649 static int on_client_charloginreq(t_connection * c, t_packet * packet)
650 {
651 t_packet * bnpacket;
652 char const * charname;
653 char const * account;
654 t_sq * sq;
655 t_d2charinfo_file data;
656 unsigned int expire_time;
657 char checknum;
658
659 if (!(charname=packet_get_str_const(packet,sizeof(t_client_d2cs_charloginreq),MAX_CHARNAME_LEN))) {
660 eventlog(eventlog_level_error,__FUNCTION__,"got bad character name");
661 return -1;
662 }
663 if (!(account=d2cs_conn_get_account(c))) {
664 eventlog(eventlog_level_error,__FUNCTION__,"missing account for connection");
665 return -1;
666 }
667 if (d2charinfo_load(account,charname,&data)<0) {
668 eventlog(eventlog_level_error,__FUNCTION__,"error loading charinfo for character %s(*%s)",charname,account);
669 return -1;
670 } else if (!bnetd_conn()) {
671 eventlog(eventlog_level_error,__FUNCTION__,"no bnetd connection available,character login rejected");
672 return -1;
673 }
674 expire_time = prefs_get_char_expire_time();
675 if (expire_time && (time(NULL) > bn_int_get(data.header.last_time) + expire_time)) {
676 t_packet * rpacket;
677
678 if ((rpacket=packet_create(packet_class_d2cs))) {
679 packet_set_size(rpacket,sizeof(t_d2cs_client_charloginreply));
680 packet_set_type(rpacket,D2CS_CLIENT_CHARLOGINREPLY);
681 bn_int_set(&rpacket->u.d2cs_client_charloginreply.reply, D2CS_CLIENT_CHARLOGINREPLY_EXPIRED);
682 conn_push_outqueue(c,rpacket);
683 packet_del_ref(rpacket);
684 }
685 eventlog(eventlog_level_info,__FUNCTION__,"character %s(*%s) login rejected due to char expired",charname,account);
686 return 0;
687 }
688
689 conn_set_charinfo(c,&data.summary);
690 eventlog(eventlog_level_info,__FUNCTION__,"got character %s(*%s) login request",charname,account);
691
692 /* generate checknum, save it in t_connection, and send it to bnetd, by sowater */
693 do {
694 checknum=d2cs_random_char();
695 }while(d2cs_conn_get_checknum(c)==checknum);
696 d2cs_conn_set_checknum(c,checknum);
697 eventlog(eventlog_level_info,__FUNCTION__,"set checknum %c to connection",checknum);
698
699 if ((bnpacket=packet_create(packet_class_d2cs_bnetd))) {
700 if ((sq=sq_create(d2cs_conn_get_sessionnum(c),packet,0))) {
701 packet_set_size(bnpacket,sizeof(t_d2cs_bnetd_charloginreq));
702 packet_set_type(bnpacket,D2CS_BNETD_CHARLOGINREQ);
703 bn_int_set(&bnpacket->u.d2cs_bnetd_charloginreq.h.seqno,sq_get_seqno(sq));
704 bn_int_set(&bnpacket->u.d2cs_bnetd_charloginreq.sessionnum,
705 conn_get_bnetd_sessionnum(c));
706 packet_append_string(bnpacket,charname);
707 packet_append_string(bnpacket,(char const *)&data.portrait);
708
709 /*sowater */
710 packet_append_data(bnpacket,&checknum,sizeof(checknum));
711
712 conn_push_outqueue(bnetd_conn(),bnpacket);
713 }
714 packet_del_ref(bnpacket);
715 }
716 return 0;
717 }
718
719 static int on_client_deletecharreq(t_connection * c, t_packet * packet)
720 {
721 t_packet * rpacket;
722 char const * charname;
723 char const * account;
724 unsigned int reply;
725
726 if (!(charname=packet_get_str_const(packet,sizeof(t_client_d2cs_deletecharreq),MAX_CHARNAME_LEN))) {
727 eventlog(eventlog_level_error,__FUNCTION__,"got bad character name");
728 return -1;
729 }
730 if (conn_check_multilogin(c,charname)<0) {
731 eventlog(eventlog_level_error,__FUNCTION__,"character %s is already logged in",charname);
732 return -1;
733 }
734 d2cs_conn_set_charname(c,NULL);
735 account=d2cs_conn_get_account(c);
736 if (d2char_delete(account,charname)<0) {
737 eventlog(eventlog_level_error,__FUNCTION__,"failed to delete character %s(*%s)",charname,account);
738 reply = D2CS_CLIENT_DELETECHARREPLY_FAILED;
739 } else {
740 reply = D2CS_CLIENT_DELETECHARREPLY_SUCCEED;
741 }
742 if ((rpacket=packet_create(packet_class_d2cs))) {
743 packet_set_size(rpacket,sizeof(t_d2cs_client_deletecharreply));
744 packet_set_type(rpacket,D2CS_CLIENT_DELETECHARREPLY);
745 bn_short_set(&rpacket->u.d2cs_client_deletecharreply.u1,0);
746 bn_int_set(&rpacket->u.d2cs_client_deletecharreply.reply,reply);
747 conn_push_outqueue(c,rpacket);
748 packet_del_ref(rpacket);
749 }
750 return 0;
751 }
752
753 static int on_client_ladderreq(t_connection * c, t_packet * packet)
754 {
755 unsigned char type;
756 unsigned short start_pos;
757
758 type=bn_byte_get(packet->u.client_d2cs_ladderreq.type);
759 start_pos=bn_short_get(packet->u.client_d2cs_ladderreq.start_pos);
760 d2cs_send_client_ladder(c,type,start_pos);
761 return 0;
762 }
763
764 static int d2cs_send_client_ladder(t_connection * c, unsigned char type, unsigned short from)
765 {
766 t_packet * rpacket;
767 t_d2cs_client_ladderinfo const * ladderinfo;
768 unsigned int curr_len, cont_len, total_len;
769 t_d2cs_client_ladderheader ladderheader;
770 t_d2cs_client_ladderinfoheader infoheader;
771 unsigned int start_pos, count, count_per_packet, npacket;
772 unsigned int i, n, curr_pos;
773
774 start_pos=from;
775 count=prefs_get_ladderlist_count();
776 if (d2ladder_get_ladder(&start_pos,&count,type,&ladderinfo)<0) {
777 eventlog(eventlog_level_error,__FUNCTION__,"error get ladder for type %d start_pos %d",type,from);
778 return 0;
779 }
780
781 count_per_packet=14;
782 npacket=count/count_per_packet;
783 if (count % count_per_packet) npacket++;
784
785 curr_len=0;
786 cont_len=0;
787 total_len = count * sizeof(*ladderinfo) + sizeof(ladderheader) + sizeof(infoheader) * npacket;
788 total_len -= 4;
789 bn_short_set(&ladderheader.start_pos,start_pos);
790 bn_short_set(&ladderheader.u1,0);
791 bn_int_set(&ladderheader.count1,count);
792
793 for (i=0; i< npacket; i++) {
794 curr_len=0;
795 if ((rpacket=packet_create(packet_class_d2cs))) {
796 packet_set_size(rpacket,sizeof(t_d2cs_client_ladderreply));
797 packet_set_type(rpacket,D2CS_CLIENT_LADDERREPLY);
798 bn_byte_set(&rpacket->u.d2cs_client_ladderreply.type, type);
799 bn_short_set(&rpacket->u.d2cs_client_ladderreply.total_len, total_len);
800 bn_short_set(&rpacket->u.d2cs_client_ladderreply.cont_len,cont_len);
801 if (i==0) {
802 bn_int_set(&infoheader.count2,count);
803 packet_append_data(rpacket,&ladderheader,sizeof(ladderheader));
804 curr_len += sizeof(ladderheader);
805 } else {
806 bn_int_set(&infoheader.count2,0);
807 }
808 packet_append_data(rpacket,&infoheader,sizeof(infoheader));
809 curr_len += sizeof(infoheader);
810 for (n=0; n< count_per_packet; n++) {
811 curr_pos = n + i * count_per_packet;
812 if (curr_pos >= count) break;
813 packet_append_data(rpacket, ladderinfo+curr_pos, sizeof(*ladderinfo));
814 curr_len += sizeof(*ladderinfo);
815 }
816 if (i==0) {
817 packet_set_size(rpacket, packet_get_size(rpacket)-4);
818 curr_len -= 4;
819 }
820 bn_short_set(&rpacket->u.d2cs_client_ladderreply.curr_len,curr_len);
821 conn_push_outqueue(c,rpacket);
822 packet_del_ref(rpacket);
823 }
824 cont_len += curr_len;
825 }
826 return 0;
827 }
828
829 static int on_client_motdreq(t_connection * c, t_packet * packet)
830 {
831 t_packet * rpacket;
832
833 if (!packet)
834 return -1;
835
836 if ((rpacket=packet_create(packet_class_d2cs))) {
837 packet_set_size(rpacket,sizeof(t_d2cs_client_motdreply));
838 packet_set_type(rpacket,D2CS_CLIENT_MOTDREPLY);
839 bn_byte_set(&rpacket->u.d2cs_client_motdreply.u1,0);
840 packet_append_string(rpacket,prefs_get_motd());
841 conn_push_outqueue(c,rpacket);
842 packet_del_ref(rpacket);
843 }
844 return 0;
845 }
846
847 static int on_client_cancelcreategame(t_connection * c, t_packet * packet)
848 {
849 t_gq * gq;
850 t_elem * elem;
851
852 if (!packet)
853 return -1;
854
855 if (!(gq=conn_get_gamequeue(c))) {
856 return 0;
857 }
858 conn_set_gamequeue(c,NULL);
859 gq_destroy(gq,&elem);
860 return 0;
861 }
862
863 static int on_client_charladderreq(t_connection * c, t_packet * packet)
864 {
865 t_packet * rpacket;
866 char const * charname;
867 unsigned int expansion, hardcore, type;
868 int pos;
869
870 if (!(charname=packet_get_str_const(packet,sizeof(t_client_d2cs_charladderreq),MAX_CHARNAME_LEN))) {
871 eventlog(eventlog_level_error,__FUNCTION__,"got bad character name");
872 return -1;
873 }
874 expansion=bn_int_get(packet->u.client_d2cs_charladderreq.expansion);
875 hardcore=bn_int_get(packet->u.client_d2cs_charladderreq.hardcore);
876 type=0; /* avoid warning */
877 if (hardcore && expansion) {
878 type=D2LADDER_EXP_HC_OVERALL;
879 } else if (!hardcore && expansion) {
880 type=D2LADDER_EXP_STD_OVERALL;
881 } else if (hardcore && !expansion) {
882 type=D2LADDER_HC_OVERALL;
883 } else if (!hardcore && !expansion) {
884 type=D2LADDER_STD_OVERALL;
885 }
886 if ((pos=d2ladder_find_character_pos(type,charname))<0) {
887 if ((rpacket=packet_create(packet_class_d2cs))) {
888 packet_set_size(rpacket,sizeof(t_d2cs_client_ladderreply));
889 packet_set_type(rpacket,D2CS_CLIENT_LADDERREPLY);
890 bn_byte_set(&rpacket->u.d2cs_client_ladderreply.type, type);
891 bn_short_set(&rpacket->u.d2cs_client_ladderreply.total_len,0);
892 bn_short_set(&rpacket->u.d2cs_client_ladderreply.curr_len,0);
893 bn_short_set(&rpacket->u.d2cs_client_ladderreply.cont_len,0);
894 conn_push_outqueue(c,rpacket);
895 packet_del_ref(rpacket);
896 }
897 return 0;
898 }
899 pos -= prefs_get_ladderlist_count()/2;
900 if (pos < 0) pos=0;
901 d2cs_send_client_ladder(c,type,pos);
902 return 0;
903 }
904
905 static int on_client_charlistreq(t_connection * c, t_packet * packet)
906 {
907 t_packet * rpacket;
908 t_pdir * dir;
909 char const * account;
910 char const * charname;
911 char * path;
912 t_d2charinfo_file * charinfo;
913 unsigned int n, maxchar;
914 t_elist charlist_head;
915 char const * charlist_sort_order;
916
917 if (!packet)
918 return -1;
919
920 if (!(account=d2cs_conn_get_account(c))) {
921 eventlog(eventlog_level_error,__FUNCTION__,"missing account for connection");
922 return -1;
923 }
924 path=xmalloc(strlen(prefs_get_charinfo_dir())+1+strlen(account)+1);
925 charlist_sort_order = prefs_get_charlist_sort_order();
926
927 elist_init(&charlist_head);
928
929 d2char_get_infodir_name(path,account);
930 maxchar=prefs_get_maxchar();
931 if ((rpacket=packet_create(packet_class_d2cs))) {
932 packet_set_size(rpacket,sizeof(t_d2cs_client_charlistreply));
933 packet_set_type(rpacket,D2CS_CLIENT_CHARLISTREPLY);
934 bn_short_set(&rpacket->u.d2cs_client_charlistreply.u1,0);
935 n=0;
936 if (!(dir=p_opendir(path))) {
937 eventlog(eventlog_level_info,__FUNCTION__,"(*%s) charinfo directory do not exist, building it",account);
938 p_mkdir(path,S_IRWXU);
939 } else {
940 while ((charname=p_readdir(dir))) {
941 if (charname[0]=='.') continue;
942 charinfo = xmalloc(sizeof(t_d2charinfo_file));
943 if (d2charinfo_load(account,charname,charinfo)<0) {
944 eventlog(eventlog_level_error,__FUNCTION__,"error loading charinfo for %s(*%s)",charname,account);
945 xfree((void *)charinfo);
946 continue;
947 }
948 eventlog(eventlog_level_debug,__FUNCTION__,"adding char %s (*%s)", charname, account);
949 d2charlist_add_char(&charlist_head,charinfo,0);
950 n++;
951 if (n>=maxchar) break;
952 }
953 if (prefs_allow_newchar() && (n<maxchar)) {
954 bn_short_set(&rpacket->u.d2cs_client_charlistreply.maxchar,maxchar);
955 } else {
956 bn_short_set(&rpacket->u.d2cs_client_charlistreply.maxchar,0);
957 }
958 p_closedir(dir);
959 if (!strcmp(charlist_sort_order, "ASC"))
960 {
961 t_elist * curr, * safe;
962 t_d2charlist * ccharlist;
963
964 elist_for_each_safe(curr,&charlist_head,safe)
965 {
966 ccharlist = elist_entry(curr,t_d2charlist,list);
967 packet_append_string(rpacket,ccharlist->charinfo->header.charname);
968 packet_append_string(rpacket,(char *)&ccharlist->charinfo->portrait);
969 xfree((void *)ccharlist->charinfo);
970 xfree((void *)ccharlist);
971 }
972 }
973 else
974 {
975 t_elist * curr, * safe;
976 t_d2charlist * ccharlist;
977
978 elist_for_each_safe_rev(curr,&charlist_head,safe)
979 {
980 ccharlist = elist_entry(curr,t_d2charlist,list);
981 packet_append_string(rpacket,ccharlist->charinfo->header.charname);
982 packet_append_string(rpacket,(char *)&ccharlist->charinfo->portrait);
983 xfree((void *)ccharlist->charinfo);
984 xfree((void *)ccharlist);
985
986 }
987 }
988 }
989 bn_short_set(&rpacket->u.d2cs_client_charlistreply.currchar,n);
990 bn_short_set(&rpacket->u.d2cs_client_charlistreply.currchar2,n);
991 conn_push_outqueue(c,rpacket);
992 packet_del_ref(rpacket);
993 }
994 xfree(path);
995 return 0;
996 }
997
998 static int on_client_charlistreq_110(t_connection * c, t_packet * packet)
999 {
1000 t_packet * rpacket;
1001 t_pdir * dir;
1002 char const * account;
1003 char const * charname;
1004 char * path;
1005
1006 t_d2charinfo_file * charinfo;
1007 unsigned int n, maxchar;
1008 t_elist charlist_head;
1009
1010 unsigned int exp_time;
1011 unsigned int curr_exp_time;
1012 char const * charlist_sort_order;
1013
1014 if (!packet)
1015 return -1;
1016
1017 if (!(account=d2cs_conn_get_account(c))) {
1018 eventlog(eventlog_level_error,__FUNCTION__,"missing account for connection");
1019 return -1;
1020 }
1021 path=xmalloc(strlen(prefs_get_charinfo_dir())+1+strlen(account)+1);
1022 charlist_sort_order = prefs_get_charlist_sort_order();
1023
1024 elist_init(&charlist_head);
1025
1026 d2char_get_infodir_name(path,account);
1027 if (prefs_allow_newchar())
1028 maxchar=prefs_get_maxchar();
1029 else
1030 maxchar=0;
1031
1032 if ((rpacket=packet_create(packet_class_d2cs))) {
1033 packet_set_size(rpacket,sizeof(t_d2cs_client_charlistreply_110));
1034 packet_set_type(rpacket,D2CS_CLIENT_CHARLISTREPLY_110);
1035 bn_short_set(&rpacket->u.d2cs_client_charlistreply_110.u1,0);
1036 n=0;
1037 if (!(dir=p_opendir(path))) {
1038 eventlog(eventlog_level_info,__FUNCTION__,"(*%s) charinfo directory do not exist, building it",account);
1039 p_mkdir(path,S_IRWXU);
1040 } else {
1041 exp_time = prefs_get_char_expire_time();
1042 while ((charname=p_readdir(dir))) {
1043 if (charname[0]=='.') continue;
1044 charinfo = xmalloc(sizeof(t_d2charinfo_file));
1045 if (d2charinfo_load(account,charname,charinfo)<0) {
1046 eventlog(eventlog_level_error,__FUNCTION__,"error loading charinfo for %s(*%s)",charname,account);
1047 xfree(charinfo);
1048 continue;
1049 }
1050 if (exp_time) {
1051 curr_exp_time = bn_int_get(charinfo->header.last_time)+exp_time;
1052 } else {
1053 curr_exp_time = 0x7FFFFFFF;
1054 }
1055 eventlog(eventlog_level_debug,__FUNCTION__,"adding char %s (*%s)", charname, account);
1056 d2charlist_add_char(&charlist_head,charinfo,curr_exp_time);
1057 n++;
1058 if (n>=maxchar) break;
1059 }
1060 if (n>=maxchar)
1061 maxchar = 0;
1062
1063 p_closedir(dir);
1064 if (!strcmp(charlist_sort_order, "ASC"))
1065 {
1066 t_elist * curr, *safe;
1067 t_d2charlist * ccharlist;
1068
1069 elist_for_each_safe(curr,&charlist_head,safe)
1070 {
1071 bn_int bn_exp_time;
1072
1073 ccharlist = elist_entry(curr,t_d2charlist,list);
1074 bn_int_set(&bn_exp_time,ccharlist->expiration_time);
1075 packet_append_data(rpacket,bn_exp_time,sizeof(bn_exp_time));
1076 packet_append_string(rpacket,ccharlist->charinfo->header.charname);
1077 packet_append_string(rpacket,(char *)&ccharlist->charinfo->portrait);
1078 xfree((void *)ccharlist->charinfo);
1079 xfree((void *)ccharlist);
1080 }
1081 }
1082 else
1083 {
1084 t_elist * curr, *safe;
1085 t_d2charlist * ccharlist;
1086
1087 elist_for_each_safe_rev(curr,&charlist_head,safe)
1088 {
1089 bn_int bn_exp_time;
1090
1091 ccharlist = elist_entry(curr,t_d2charlist,list);
1092 bn_int_set(&bn_exp_time,ccharlist->expiration_time);
1093 packet_append_data(rpacket,bn_exp_time,sizeof(bn_exp_time));
1094 packet_append_string(rpacket,ccharlist->charinfo->header.charname);
1095 packet_append_string(rpacket,(char *)&ccharlist->charinfo->portrait);
1096 xfree((void *)ccharlist->charinfo);
1097 xfree((void *)ccharlist);
1098 }
1099 }
1100 }
1101 bn_short_set(&rpacket->u.d2cs_client_charlistreply.currchar,n);
1102 bn_short_set(&rpacket->u.d2cs_client_charlistreply.currchar2,n);
1103 bn_short_set(&rpacket->u.d2cs_client_charlistreply.maxchar,maxchar);
1104
1105 conn_push_outqueue(c,rpacket);
1106 packet_del_ref(rpacket);
1107 }
1108 xfree(path);
1109 return 0;
1110 }
1111
1112 static int on_client_convertcharreq(t_connection * c, t_packet * packet)
1113 {
1114 t_packet * rpacket;
1115 char const * charname;
1116 char const * account;
1117 unsigned int reply;
1118
1119 if (!(charname=packet_get_str_const(packet,sizeof(t_client_d2cs_convertcharreq),MAX_CHARNAME_LEN))) {
1120 eventlog(eventlog_level_error,__FUNCTION__,"got bad character name");
1121 return -1;
1122 }
1123 if (conn_check_multilogin(c,charname)<0) {
1124 eventlog(eventlog_level_error,__FUNCTION__,"character %s is already logged in",charname);
1125 return -1;
1126 }
1127 account=d2cs_conn_get_account(c);
1128 if (d2char_convert(account,charname)<0) {
1129 eventlog(eventlog_level_error,__FUNCTION__,"failed to convert character %s(*%s)",charname,account);
1130 reply = D2CS_CLIENT_CONVERTCHARREPLY_FAILED;
1131 } else {
1132 reply = D2CS_CLIENT_CONVERTCHARREPLY_SUCCEED;
1133 }
1134 if ((rpacket=packet_create(packet_class_d2cs))) {
1135 packet_set_size(rpacket,sizeof(t_d2cs_client_convertcharreply));
1136 packet_set_type(rpacket,D2CS_CLIENT_CONVERTCHARREPLY);
1137 bn_int_set(&rpacket->u.d2cs_client_convertcharreply.reply,reply);
1138 conn_push_outqueue(c,rpacket);
1139 packet_del_ref(rpacket);
1140 }
1141 return 0;
1142 }
1143
1144 extern int d2cs_send_client_creategamewait(t_connection * c, unsigned int position)
1145 {
1146 t_packet * packet;
1147
1148 ASSERT(c,-1);
1149 if ((packet=packet_create(packet_class_d2cs))) {
1150 packet_set_size(packet,sizeof(t_d2cs_client_creategamewait));
1151 packet_set_type(packet,D2CS_CLIENT_CREATEGAMEWAIT);
1152 bn_int_set(&packet->u.d2cs_client_creategamewait.position,position);
1153 conn_push_outqueue(c,packet);
1154 packet_del_ref(packet);
1155 }
1156 return 0;
1157 }
1158
1159 extern int d2cs_handle_client_creategame(t_connection * c, t_packet * packet)
1160 {
1161 return on_client_creategamereq(c,packet);
1162 }
1163
1164 static unsigned int d2cs_try_joingame(t_connection const * c, t_game const * game, char const * gamepass)
1165 {
1166 unsigned int reply;
1167
1168 ASSERT(c,D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST);
1169 ASSERT(game,D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST);
1170 if (!game_get_created(game)) {
1171 reply=D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST;
1172 } else if (!game_get_d2gs(game)) {
1173 reply=D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST;
1174 } else if (conn_get_charinfo_ladder(c) != game_get_gameflag_ladder(game)) {
1175 reply=D2CS_CLIENT_JOINGAMEREPLY_NORMAL_LADDER;
1176 } else if (conn_get_charinfo_expansion(c) != game_get_gameflag_expansion(game)) {
1177 reply=D2CS_CLIENT_JOINGAMEREPLY_CLASSIC_EXPANSION;
1178 } else if (conn_get_charinfo_hardcore(c) != game_get_gameflag_hardcore(game)) {
1179 reply=D2CS_CLIENT_JOINGAMEREPLY_HARDCORE_SOFTCORE;
1180 } else if (conn_get_charinfo_difficulty(c) < game_get_gameflag_difficulty(game)) {
1181 reply=D2CS_CLIENT_JOINGAMEREPLY_NORMAL_NIGHTMARE;
1182 } else if (prefs_allow_gamelimit()) {
1183 if (game_get_maxchar(game) <= game_get_currchar(game)) {
1184 reply=D2CS_CLIENT_JOINGAMEREPLY_GAME_FULL;
1185 } else if (conn_get_charinfo_level(c) > game_get_maxlevel(game)) {
1186 reply=D2CS_CLIENT_JOINGAMEREPLY_LEVEL_LIMIT;
1187 } else if (conn_get_charinfo_level(c) < game_get_minlevel(game)) {
1188 reply=D2CS_CLIENT_JOINGAMEREPLY_LEVEL_LIMIT;
1189 } else if (strcmp(d2cs_game_get_pass(game),gamepass)) {
1190 reply=D2CS_CLIENT_JOINGAMEREPLY_BAD_PASS;
1191 } else {
1192 reply=D2CS_CLIENT_JOINGAMEREPLY_SUCCEED;
1193 }
1194 } else {
1195 if (game_get_currchar(game) >= MAX_CHAR_PER_GAME) {
1196 reply=D2CS_CLIENT_JOINGAMEREPLY_GAME_FULL;
1197 } else {
1198 reply=D2CS_CLIENT_JOINGAMEREPLY_SUCCEED;
1199 }
1200 }
1201 return reply;
1202 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1