/[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.6 - (show annotations)
Wed Jun 14 03:13:19 2006 UTC (19 years, 9 months ago) by sysadm
Branch: MAIN
Changes since 1.5: +8 -3 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 || maxchar==1) && 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 <= prefs_create_game_interval())
361 cs->frequently_create_count++;
362
363 if (prefs_enable_antibot() && cs->frequently_create_count > prefs_frequently_create_limit()
364 && d2cs_conn_verify_checknum(c, *gamedesc)) {
365 eventlog(eventlog_level_info,__FUNCTION__,"checksum %c from packet not match checksum from connection",*gamedesc);
366 reply=D2CS_CLIENT_CREATEGAMEREPLY_FAILED;
367 } else {
368 if (strlen(gamedesc)!=0) {
369 if (!d2cs_conn_verify_checknum(c, *gamedesc))
370 cs->frequently_create_count = 0;
371 else
372 eventlog(eventlog_level_info,__FUNCTION__,"checksum %c from packet not match checksum from connection",
373 *gamedesc);
374 }
375
376 if (time(NULL) - cs->last_create_time > prefs_create_game_interval())
377 {
378 cs->frequently_create_count--;
379 if (cs->frequently_create_count<0)
380 cs->frequently_create_count=0;
381 }
382 cs->last_create_time = time(NULL);
383
384 reply=D2CS_CLIENT_CREATEGAMEREPLY_SUCCEED;
385 game_set_d2gs(game,gs);
386 d2gs_add_gamenum(gs, 1);
387 game_set_gameflag_ladder(game,ladder);
388 game_set_gameflag_expansion(game,expansion);
389 game_set_created(game,0);
390 game_set_leveldiff(game,leveldiff);
391 game_set_charlevel(game,conn_get_charinfo_level(c));
392 game_set_maxchar(game,maxchar);
393 game_set_gameflag_difficulty(game,difficulty);
394 game_set_gameflag_hardcore(game,hardcore);
395 }
396 }
397
398 seqno=bn_short_get(packet->u.client_d2cs_creategamereq.seqno);
399 if (reply!=D2CS_CLIENT_CREATEGAMEREPLY_SUCCEED) {
400 t_packet * rpacket;
401
402 if ((rpacket=packet_create(packet_class_d2cs))) {
403 packet_set_size(rpacket,sizeof(t_d2cs_client_creategamereply));
404 packet_set_type(rpacket,D2CS_CLIENT_CREATEGAMEREPLY);
405 bn_short_set(&rpacket->u.d2cs_client_creategamereply.seqno,seqno);
406 bn_short_set(&rpacket->u.d2cs_client_creategamereply.u1,0);
407 bn_short_set(&rpacket->u.d2cs_client_creategamereply.gameid,0);
408 bn_int_set(&rpacket->u.d2cs_client_creategamereply.reply,reply);
409 conn_push_outqueue(c,rpacket);
410 packet_del_ref(rpacket);
411 }
412 } else {
413 t_packet * gspacket;
414 t_sq * sq;
415 struct in_addr addr;
416
417 if ((gspacket=packet_create(packet_class_d2gs))) {
418 if ((sq=sq_create(d2cs_conn_get_sessionnum(c),packet,d2cs_game_get_id(game)))) {
419 packet_set_size(gspacket,sizeof(t_d2cs_d2gs_creategamereq));
420 packet_set_type(gspacket,D2CS_D2GS_CREATEGAMEREQ);
421 bn_int_set(&gspacket->u.d2cs_d2gs_creategamereq.h.seqno,sq_get_seqno(sq));
422 bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.difficulty,difficulty);
423 bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.hardcore,hardcore);
424 bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.expansion,expansion);
425 bn_byte_set(&gspacket->u.d2cs_d2gs_creategamereq.ladder,ladder);
426 packet_append_string(gspacket,gamename);
427 packet_append_string(gspacket,gamepass);
428 packet_append_string(gspacket,gamedesc);
429 packet_append_string(gspacket,d2cs_conn_get_account(c));
430 packet_append_string(gspacket,d2cs_conn_get_charname(c));
431 addr.s_addr = htonl(d2cs_conn_get_addr(c));
432 packet_append_string(gspacket,inet_ntoa(addr));
433 conn_push_outqueue(d2gs_get_connection(gs),gspacket);
434 }
435 packet_del_ref(gspacket);
436 eventlog(eventlog_level_info,__FUNCTION__,"request create game %s on gs %d",gamename,d2gs_get_id(gs));
437 }
438 }
439 return 0;
440 }
441
442 static int on_client_joingamereq(t_connection * c, t_packet * packet)
443 {
444 char const * gamename;
445 char const * gamepass;
446 char const * charname;
447 char const * account;
448 t_game * game;
449 t_d2gs * gs;
450 int reply;
451 unsigned int pos;
452 unsigned int seqno;
453
454 gs = NULL;
455 pos=sizeof(t_client_d2cs_joingamereq);
456 if (!(gamename=packet_get_str_const(packet,pos,MAX_GAMENAME_LEN))) {
457 eventlog(eventlog_level_error,__FUNCTION__,"got bad game name");
458 return -1;
459 }
460 pos+=strlen(gamename)+1;
461 if (!(gamepass=packet_get_str_const(packet,pos,MAX_GAMEPASS_LEN))) {
462 eventlog(eventlog_level_error,__FUNCTION__,"got bad game pass");
463 return -1;
464 }
465 if (!(charname=d2cs_conn_get_charname(c))) {
466 eventlog(eventlog_level_error,__FUNCTION__,"missing character name for connection");
467 return -1;
468 }
469 if (!(account=d2cs_conn_get_account(c))) {
470 eventlog(eventlog_level_error,__FUNCTION__,"missing account for connection");
471 return -1;
472 }
473 if (conn_check_multilogin(c,charname)<0) {
474 eventlog(eventlog_level_error,__FUNCTION__,"character %s is already logged in",charname);
475 return -1;
476 }
477 if (!(game=d2cs_gamelist_find_game(gamename))) {
478 eventlog(eventlog_level_info,__FUNCTION__,"game %s not found",gamename);
479 reply=D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST;
480 } else if (!(gs=game_get_d2gs(game))) {
481 eventlog(eventlog_level_error,__FUNCTION__,"missing game server for game %s",gamename);
482 reply=D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST;
483 } else {
484 reply=d2cs_try_joingame(c,game,gamepass);
485 }
486
487 seqno=bn_short_get(packet->u.client_d2cs_joingamereq.seqno);
488 if (reply!=D2CS_CLIENT_JOINGAMEREPLY_SUCCEED) {
489 t_packet * rpacket;
490
491 if ((rpacket=packet_create(packet_class_d2cs))) {
492 packet_set_size(rpacket,sizeof(t_d2cs_client_joingamereply));
493 packet_set_type(rpacket,D2CS_CLIENT_JOINGAMEREPLY);
494 bn_short_set(&rpacket->u.d2cs_client_joingamereply.seqno,seqno);
495 bn_short_set(&rpacket->u.d2cs_client_joingamereply.u1,0);
496 bn_short_set(&rpacket->u.d2cs_client_joingamereply.gameid,0);
497 bn_int_set(&rpacket->u.d2cs_client_joingamereply.addr,0);
498 bn_int_set(&rpacket->u.d2cs_client_joingamereply.token,0);
499 bn_int_set(&rpacket->u.d2cs_client_joingamereply.reply,reply);
500 conn_push_outqueue(c,rpacket);
501 packet_del_ref(rpacket);
502 }
503 } else {
504 t_packet * gspacket;
505 t_sq * sq;
506 struct in_addr addr;
507
508 if ((gspacket=packet_create(packet_class_d2gs))) {
509 if ((sq=sq_create(d2cs_conn_get_sessionnum(c),packet,d2cs_game_get_id(game)))) {
510 packet_set_size(gspacket,sizeof(t_d2cs_d2gs_joingamereq));
511 packet_set_type(gspacket,D2CS_D2GS_JOINGAMEREQ);
512 bn_int_set(&gspacket->u.d2cs_d2gs_joingamereq.h.seqno,sq_get_seqno(sq));
513 bn_int_set(&gspacket->u.d2cs_d2gs_joingamereq.gameid,
514 game_get_d2gs_gameid(game));
515 sq_set_gametoken(sq,d2gs_make_token(gs));
516 bn_int_set(&gspacket->u.d2cs_d2gs_joingamereq.token,sq_get_gametoken(sq));
517 packet_append_string(gspacket,charname);
518 packet_append_string(gspacket,account);
519 addr.s_addr = htonl(d2cs_conn_get_addr(c));
520 packet_append_string(gspacket,inet_ntoa(addr));
521 conn_push_outqueue(d2gs_get_connection(gs),gspacket);
522 }
523 packet_del_ref(gspacket);
524 eventlog(eventlog_level_info,__FUNCTION__,"request join game %s for character %s on gs %d",gamename,
525 charname,d2gs_get_id(gs));
526 }
527 }
528 return 0;
529 }
530
531 static int on_client_gamelistreq(t_connection * c, t_packet * packet)
532 {
533 t_packet * rpacket;
534 t_game * game;
535 unsigned int count;
536 unsigned int seqno;
537 time_t now;
538 unsigned int maxlifetime;
539 t_elem const * start_elem;
540 t_elem const * elem;
541
542 seqno=bn_short_get(packet->u.client_d2cs_gamelistreq.seqno);
543 /* if (seqno%2) return 0; */
544 count=0;
545 now=time(NULL);
546 maxlifetime=prefs_get_game_maxlifetime();
547
548 elem=start_elem=gamelist_get_curr_elem();
549 if (!elem) elem=list_get_first_const(d2cs_gamelist());
550 else elem=elem_get_next_const(d2cs_gamelist(),elem);
551
552 for (; elem != start_elem; elem=elem_get_next_const(d2cs_gamelist(),elem)) {
553 if (!elem) {
554 elem=list_get_first_const(d2cs_gamelist());
555 if (elem == start_elem) break;
556 }
557 if (!(game=elem_get_data(elem))) {
558 eventlog(eventlog_level_error,__FUNCTION__,"got NULL game");
559 break;
560 }
561 if (maxlifetime && (now-game->create_time>maxlifetime)) continue;
562 if (!game_get_currchar(game)) continue;
563 if (!prefs_allow_gamelist_showall()) {
564 if (conn_get_charinfo_difficulty(c)!=game_get_gameflag_difficulty(game)) continue;
565 }
566 if (d2cs_try_joingame(c,game,"")!=D2CS_CLIENT_JOINGAMEREPLY_SUCCEED) continue;
567 if ((rpacket=packet_create(packet_class_d2cs))) {
568 packet_set_size(rpacket,sizeof(t_d2cs_client_gamelistreply));
569 packet_set_type(rpacket,D2CS_CLIENT_GAMELISTREPLY);
570 bn_short_set(&rpacket->u.d2cs_client_gamelistreply.seqno,seqno);
571 bn_int_set(&rpacket->u.d2cs_client_gamelistreply.token,d2cs_game_get_id(game));
572 bn_byte_set(&rpacket->u.d2cs_client_gamelistreply.currchar,game_get_currchar(game));
573 bn_int_set(&rpacket->u.d2cs_client_gamelistreply.gameflag,game_get_gameflag(game));
574 packet_append_string(rpacket,d2cs_game_get_name(game));
575 packet_append_string(rpacket,game_get_desc(game));
576 conn_push_outqueue(c,rpacket);
577 packet_del_ref(rpacket);
578 count++;
579 if (prefs_get_maxgamelist() && count>=prefs_get_maxgamelist()) break;
580 }
581 }
582 gamelist_set_curr_elem(elem);
583 if (count) {
584 if ((rpacket=packet_create(packet_class_d2cs))) {
585 packet_set_size(rpacket,sizeof(t_d2cs_client_gamelistreply));
586 packet_set_type(rpacket,D2CS_CLIENT_GAMELISTREPLY);
587 bn_short_set(&rpacket->u.d2cs_client_gamelistreply.seqno,seqno);
588 bn_int_set(&rpacket->u.d2cs_client_gamelistreply.token,0);
589 bn_byte_set(&rpacket->u.d2cs_client_gamelistreply.currchar,0);
590 bn_int_set(&rpacket->u.d2cs_client_gamelistreply.gameflag,0);
591 packet_append_string(rpacket,"");
592 packet_append_string(rpacket,"");
593 packet_append_string(rpacket,"");
594 conn_push_outqueue(c,rpacket);
595 packet_del_ref(rpacket);
596 }
597 }
598 return 0;
599 }
600
601 static int on_client_gameinforeq(t_connection * c, t_packet * packet)
602 {
603 t_game_charinfo * info;
604 t_packet * rpacket;
605 char const * gamename;
606 t_game * game;
607 unsigned int seqno, n;
608
609 if (!(gamename=packet_get_str_const(packet,sizeof(t_client_d2cs_gameinforeq),MAX_GAMENAME_LEN))) {
610 eventlog(eventlog_level_error,__FUNCTION__,"got bad game name");
611 return -1;
612 }
613 if (!(game=d2cs_gamelist_find_game(gamename))) {
614 eventlog(eventlog_level_error,__FUNCTION__,"game %s not found",gamename);
615 return 0;
616 }
617 seqno=bn_short_get(packet->u.client_d2cs_gameinforeq.seqno);
618 if ((rpacket=packet_create(packet_class_d2cs))) {
619 packet_set_size(rpacket,sizeof(t_d2cs_client_gameinforeply));
620 packet_set_type(rpacket,D2CS_CLIENT_GAMEINFOREPLY);
621 bn_short_set(&rpacket->u.d2cs_client_gameinforeply.seqno,seqno);
622 bn_int_set(&rpacket->u.d2cs_client_gameinforeply.gameflag,game_get_gameflag(game));
623 bn_int_set(&rpacket->u.d2cs_client_gameinforeply.etime,time(NULL)-d2cs_game_get_create_time(game));
624 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.charlevel,game_get_charlevel(game));
625 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.leveldiff,game_get_leveldiff(game));
626 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.maxchar,game_get_maxchar(game));
627 packet_append_string(rpacket, game_get_desc(game) ? game_get_desc(game) : NULL);
628
629 n=0;
630 BEGIN_LIST_TRAVERSE_DATA_CONST(game_get_charlist(game),info)
631 {
632 if (!info->charname) {
633 eventlog(eventlog_level_error,__FUNCTION__,"got NULL charname in game %s char list",gamename);
634 continue;
635 }
636 packet_append_string(rpacket,info->charname);
637 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.class[n],info->class);
638 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.level[n],info->level);
639 n++;
640 }
641 END_LIST_TRAVERSE_DATA_CONST()
642
643 bn_byte_set(&rpacket->u.d2cs_client_gameinforeply.currchar,n);
644 if (n!=game_get_currchar(game)) {
645 eventlog(eventlog_level_error,__FUNCTION__,"game %s character list corrupted",gamename);
646 }
647 conn_push_outqueue(c,rpacket);
648 packet_del_ref(rpacket);
649 }
650 return 0;
651 }
652
653 static int on_client_charloginreq(t_connection * c, t_packet * packet)
654 {
655 t_packet * bnpacket;
656 char const * charname;
657 char const * account;
658 t_sq * sq;
659 t_d2charinfo_file data;
660 unsigned int expire_time;
661 char checknum;
662
663 if (!(charname=packet_get_str_const(packet,sizeof(t_client_d2cs_charloginreq),MAX_CHARNAME_LEN))) {
664 eventlog(eventlog_level_error,__FUNCTION__,"got bad character name");
665 return -1;
666 }
667 if (!(account=d2cs_conn_get_account(c))) {
668 eventlog(eventlog_level_error,__FUNCTION__,"missing account for connection");
669 return -1;
670 }
671 if (d2charinfo_load(account,charname,&data)<0) {
672 eventlog(eventlog_level_error,__FUNCTION__,"error loading charinfo for character %s(*%s)",charname,account);
673 return -1;
674 } else if (!bnetd_conn()) {
675 eventlog(eventlog_level_error,__FUNCTION__,"no bnetd connection available,character login rejected");
676 return -1;
677 }
678 expire_time = prefs_get_char_expire_time();
679 if (expire_time && (time(NULL) > bn_int_get(data.header.last_time) + expire_time)) {
680 t_packet * rpacket;
681
682 if ((rpacket=packet_create(packet_class_d2cs))) {
683 packet_set_size(rpacket,sizeof(t_d2cs_client_charloginreply));
684 packet_set_type(rpacket,D2CS_CLIENT_CHARLOGINREPLY);
685 bn_int_set(&rpacket->u.d2cs_client_charloginreply.reply, D2CS_CLIENT_CHARLOGINREPLY_EXPIRED);
686 conn_push_outqueue(c,rpacket);
687 packet_del_ref(rpacket);
688 }
689 eventlog(eventlog_level_info,__FUNCTION__,"character %s(*%s) login rejected due to char expired",charname,account);
690 return 0;
691 }
692
693 conn_set_charinfo(c,&data.summary);
694 eventlog(eventlog_level_info,__FUNCTION__,"got character %s(*%s) login request",charname,account);
695
696 /* generate checknum, save it in t_connection, and send it to bnetd, by sowater */
697 do {
698 checknum=d2cs_random_char();
699 }while(d2cs_conn_get_checknum(c)==checknum);
700 d2cs_conn_set_checknum(c,checknum);
701 eventlog(eventlog_level_info,__FUNCTION__,"set checknum %c to connection",checknum);
702
703 if ((bnpacket=packet_create(packet_class_d2cs_bnetd))) {
704 if ((sq=sq_create(d2cs_conn_get_sessionnum(c),packet,0))) {
705 packet_set_size(bnpacket,sizeof(t_d2cs_bnetd_charloginreq));
706 packet_set_type(bnpacket,D2CS_BNETD_CHARLOGINREQ);
707 bn_int_set(&bnpacket->u.d2cs_bnetd_charloginreq.h.seqno,sq_get_seqno(sq));
708 bn_int_set(&bnpacket->u.d2cs_bnetd_charloginreq.sessionnum,
709 conn_get_bnetd_sessionnum(c));
710 packet_append_string(bnpacket,charname);
711 packet_append_string(bnpacket,(char const *)&data.portrait);
712
713 /*sowater */
714 packet_append_data(bnpacket,&checknum,sizeof(checknum));
715
716 conn_push_outqueue(bnetd_conn(),bnpacket);
717 }
718 packet_del_ref(bnpacket);
719 }
720 return 0;
721 }
722
723 static int on_client_deletecharreq(t_connection * c, t_packet * packet)
724 {
725 t_packet * rpacket;
726 char const * charname;
727 char const * account;
728 unsigned int reply;
729
730 if (!(charname=packet_get_str_const(packet,sizeof(t_client_d2cs_deletecharreq),MAX_CHARNAME_LEN))) {
731 eventlog(eventlog_level_error,__FUNCTION__,"got bad character name");
732 return -1;
733 }
734 if (conn_check_multilogin(c,charname)<0) {
735 eventlog(eventlog_level_error,__FUNCTION__,"character %s is already logged in",charname);
736 return -1;
737 }
738 d2cs_conn_set_charname(c,NULL);
739 account=d2cs_conn_get_account(c);
740 if (d2char_delete(account,charname)<0) {
741 eventlog(eventlog_level_error,__FUNCTION__,"failed to delete character %s(*%s)",charname,account);
742 reply = D2CS_CLIENT_DELETECHARREPLY_FAILED;
743 } else {
744 reply = D2CS_CLIENT_DELETECHARREPLY_SUCCEED;
745 }
746 if ((rpacket=packet_create(packet_class_d2cs))) {
747 packet_set_size(rpacket,sizeof(t_d2cs_client_deletecharreply));
748 packet_set_type(rpacket,D2CS_CLIENT_DELETECHARREPLY);
749 bn_short_set(&rpacket->u.d2cs_client_deletecharreply.u1,0);
750 bn_int_set(&rpacket->u.d2cs_client_deletecharreply.reply,reply);
751 conn_push_outqueue(c,rpacket);
752 packet_del_ref(rpacket);
753 }
754 return 0;
755 }
756
757 static int on_client_ladderreq(t_connection * c, t_packet * packet)
758 {
759 unsigned char type;
760 unsigned short start_pos;
761
762 type=bn_byte_get(packet->u.client_d2cs_ladderreq.type);
763 start_pos=bn_short_get(packet->u.client_d2cs_ladderreq.start_pos);
764 d2cs_send_client_ladder(c,type,start_pos);
765 return 0;
766 }
767
768 static int d2cs_send_client_ladder(t_connection * c, unsigned char type, unsigned short from)
769 {
770 t_packet * rpacket;
771 t_d2cs_client_ladderinfo const * ladderinfo;
772 unsigned int curr_len, cont_len, total_len;
773 t_d2cs_client_ladderheader ladderheader;
774 t_d2cs_client_ladderinfoheader infoheader;
775 unsigned int start_pos, count, count_per_packet, npacket;
776 unsigned int i, n, curr_pos;
777
778 start_pos=from;
779 count=prefs_get_ladderlist_count();
780 if (d2ladder_get_ladder(&start_pos,&count,type,&ladderinfo)<0) {
781 eventlog(eventlog_level_error,__FUNCTION__,"error get ladder for type %d start_pos %d",type,from);
782 return 0;
783 }
784
785 count_per_packet=14;
786 npacket=count/count_per_packet;
787 if (count % count_per_packet) npacket++;
788
789 curr_len=0;
790 cont_len=0;
791 total_len = count * sizeof(*ladderinfo) + sizeof(ladderheader) + sizeof(infoheader) * npacket;
792 total_len -= 4;
793 bn_short_set(&ladderheader.start_pos,start_pos);
794 bn_short_set(&ladderheader.u1,0);
795 bn_int_set(&ladderheader.count1,count);
796
797 for (i=0; i< npacket; i++) {
798 curr_len=0;
799 if ((rpacket=packet_create(packet_class_d2cs))) {
800 packet_set_size(rpacket,sizeof(t_d2cs_client_ladderreply));
801 packet_set_type(rpacket,D2CS_CLIENT_LADDERREPLY);
802 bn_byte_set(&rpacket->u.d2cs_client_ladderreply.type, type);
803 bn_short_set(&rpacket->u.d2cs_client_ladderreply.total_len, total_len);
804 bn_short_set(&rpacket->u.d2cs_client_ladderreply.cont_len,cont_len);
805 if (i==0) {
806 bn_int_set(&infoheader.count2,count);
807 packet_append_data(rpacket,&ladderheader,sizeof(ladderheader));
808 curr_len += sizeof(ladderheader);
809 } else {
810 bn_int_set(&infoheader.count2,0);
811 }
812 packet_append_data(rpacket,&infoheader,sizeof(infoheader));
813 curr_len += sizeof(infoheader);
814 for (n=0; n< count_per_packet; n++) {
815 curr_pos = n + i * count_per_packet;
816 if (curr_pos >= count) break;
817 packet_append_data(rpacket, ladderinfo+curr_pos, sizeof(*ladderinfo));
818 curr_len += sizeof(*ladderinfo);
819 }
820 if (i==0) {
821 packet_set_size(rpacket, packet_get_size(rpacket)-4);
822 curr_len -= 4;
823 }
824 bn_short_set(&rpacket->u.d2cs_client_ladderreply.curr_len,curr_len);
825 conn_push_outqueue(c,rpacket);
826 packet_del_ref(rpacket);
827 }
828 cont_len += curr_len;
829 }
830 return 0;
831 }
832
833 static int on_client_motdreq(t_connection * c, t_packet * packet)
834 {
835 t_packet * rpacket;
836
837 if (!packet)
838 return -1;
839
840 if ((rpacket=packet_create(packet_class_d2cs))) {
841 packet_set_size(rpacket,sizeof(t_d2cs_client_motdreply));
842 packet_set_type(rpacket,D2CS_CLIENT_MOTDREPLY);
843 bn_byte_set(&rpacket->u.d2cs_client_motdreply.u1,0);
844 packet_append_string(rpacket,prefs_get_motd());
845 conn_push_outqueue(c,rpacket);
846 packet_del_ref(rpacket);
847 }
848 return 0;
849 }
850
851 static int on_client_cancelcreategame(t_connection * c, t_packet * packet)
852 {
853 t_gq * gq;
854 t_elem * elem;
855
856 if (!packet)
857 return -1;
858
859 if (!(gq=conn_get_gamequeue(c))) {
860 return 0;
861 }
862 conn_set_gamequeue(c,NULL);
863 gq_destroy(gq,&elem);
864 return 0;
865 }
866
867 static int on_client_charladderreq(t_connection * c, t_packet * packet)
868 {
869 t_packet * rpacket;
870 char const * charname;
871 unsigned int expansion, hardcore, type;
872 int pos;
873
874 if (!(charname=packet_get_str_const(packet,sizeof(t_client_d2cs_charladderreq),MAX_CHARNAME_LEN))) {
875 eventlog(eventlog_level_error,__FUNCTION__,"got bad character name");
876 return -1;
877 }
878 expansion=bn_int_get(packet->u.client_d2cs_charladderreq.expansion);
879 hardcore=bn_int_get(packet->u.client_d2cs_charladderreq.hardcore);
880 type=0; /* avoid warning */
881 if (hardcore && expansion) {
882 type=D2LADDER_EXP_HC_OVERALL;
883 } else if (!hardcore && expansion) {
884 type=D2LADDER_EXP_STD_OVERALL;
885 } else if (hardcore && !expansion) {
886 type=D2LADDER_HC_OVERALL;
887 } else if (!hardcore && !expansion) {
888 type=D2LADDER_STD_OVERALL;
889 }
890 if ((pos=d2ladder_find_character_pos(type,charname))<0) {
891 if ((rpacket=packet_create(packet_class_d2cs))) {
892 packet_set_size(rpacket,sizeof(t_d2cs_client_ladderreply));
893 packet_set_type(rpacket,D2CS_CLIENT_LADDERREPLY);
894 bn_byte_set(&rpacket->u.d2cs_client_ladderreply.type, type);
895 bn_short_set(&rpacket->u.d2cs_client_ladderreply.total_len,0);
896 bn_short_set(&rpacket->u.d2cs_client_ladderreply.curr_len,0);
897 bn_short_set(&rpacket->u.d2cs_client_ladderreply.cont_len,0);
898 conn_push_outqueue(c,rpacket);
899 packet_del_ref(rpacket);
900 }
901 return 0;
902 }
903 pos -= prefs_get_ladderlist_count()/2;
904 if (pos < 0) pos=0;
905 d2cs_send_client_ladder(c,type,pos);
906 return 0;
907 }
908
909 static int on_client_charlistreq(t_connection * c, t_packet * packet)
910 {
911 t_packet * rpacket;
912 t_pdir * dir;
913 char const * account;
914 char const * charname;
915 char * path;
916 t_d2charinfo_file * charinfo;
917 unsigned int n, maxchar;
918 t_elist charlist_head;
919 char const * charlist_sort_order;
920
921 if (!packet)
922 return -1;
923
924 if (!(account=d2cs_conn_get_account(c))) {
925 eventlog(eventlog_level_error,__FUNCTION__,"missing account for connection");
926 return -1;
927 }
928 path=xmalloc(strlen(prefs_get_charinfo_dir())+1+strlen(account)+1);
929 charlist_sort_order = prefs_get_charlist_sort_order();
930
931 elist_init(&charlist_head);
932
933 d2char_get_infodir_name(path,account);
934 maxchar=prefs_get_maxchar();
935 if ((rpacket=packet_create(packet_class_d2cs))) {
936 packet_set_size(rpacket,sizeof(t_d2cs_client_charlistreply));
937 packet_set_type(rpacket,D2CS_CLIENT_CHARLISTREPLY);
938 bn_short_set(&rpacket->u.d2cs_client_charlistreply.u1,0);
939 n=0;
940 if (!(dir=p_opendir(path))) {
941 eventlog(eventlog_level_info,__FUNCTION__,"(*%s) charinfo directory do not exist, building it",account);
942 p_mkdir(path,S_IRWXU);
943 } else {
944 while ((charname=p_readdir(dir))) {
945 if (charname[0]=='.') continue;
946 charinfo = xmalloc(sizeof(t_d2charinfo_file));
947 if (d2charinfo_load(account,charname,charinfo)<0) {
948 eventlog(eventlog_level_error,__FUNCTION__,"error loading charinfo for %s(*%s)",charname,account);
949 xfree((void *)charinfo);
950 continue;
951 }
952 eventlog(eventlog_level_debug,__FUNCTION__,"adding char %s (*%s)", charname, account);
953 d2charlist_add_char(&charlist_head,charinfo,0);
954 n++;
955 if (n>=maxchar) break;
956 }
957 if (prefs_allow_newchar() && (n<maxchar)) {
958 bn_short_set(&rpacket->u.d2cs_client_charlistreply.maxchar,maxchar);
959 } else {
960 bn_short_set(&rpacket->u.d2cs_client_charlistreply.maxchar,0);
961 }
962 p_closedir(dir);
963 if (!strcmp(charlist_sort_order, "ASC"))
964 {
965 t_elist * curr, * safe;
966 t_d2charlist * ccharlist;
967
968 elist_for_each_safe(curr,&charlist_head,safe)
969 {
970 ccharlist = elist_entry(curr,t_d2charlist,list);
971 packet_append_string(rpacket,ccharlist->charinfo->header.charname);
972 packet_append_string(rpacket,(char *)&ccharlist->charinfo->portrait);
973 xfree((void *)ccharlist->charinfo);
974 xfree((void *)ccharlist);
975 }
976 }
977 else
978 {
979 t_elist * curr, * safe;
980 t_d2charlist * ccharlist;
981
982 elist_for_each_safe_rev(curr,&charlist_head,safe)
983 {
984 ccharlist = elist_entry(curr,t_d2charlist,list);
985 packet_append_string(rpacket,ccharlist->charinfo->header.charname);
986 packet_append_string(rpacket,(char *)&ccharlist->charinfo->portrait);
987 xfree((void *)ccharlist->charinfo);
988 xfree((void *)ccharlist);
989
990 }
991 }
992 }
993 bn_short_set(&rpacket->u.d2cs_client_charlistreply.currchar,n);
994 bn_short_set(&rpacket->u.d2cs_client_charlistreply.currchar2,n);
995 conn_push_outqueue(c,rpacket);
996 packet_del_ref(rpacket);
997 }
998 xfree(path);
999 return 0;
1000 }
1001
1002 static int on_client_charlistreq_110(t_connection * c, t_packet * packet)
1003 {
1004 t_packet * rpacket;
1005 t_pdir * dir;
1006 char const * account;
1007 char const * charname;
1008 char * path;
1009
1010 t_d2charinfo_file * charinfo;
1011 unsigned int n, maxchar;
1012 t_elist charlist_head;
1013
1014 unsigned int exp_time;
1015 unsigned int curr_exp_time;
1016 char const * charlist_sort_order;
1017
1018 if (!packet)
1019 return -1;
1020
1021 if (!(account=d2cs_conn_get_account(c))) {
1022 eventlog(eventlog_level_error,__FUNCTION__,"missing account for connection");
1023 return -1;
1024 }
1025 path=xmalloc(strlen(prefs_get_charinfo_dir())+1+strlen(account)+1);
1026 charlist_sort_order = prefs_get_charlist_sort_order();
1027
1028 elist_init(&charlist_head);
1029
1030 d2char_get_infodir_name(path,account);
1031 if (prefs_allow_newchar())
1032 maxchar=prefs_get_maxchar();
1033 else
1034 maxchar=0;
1035
1036 if ((rpacket=packet_create(packet_class_d2cs))) {
1037 packet_set_size(rpacket,sizeof(t_d2cs_client_charlistreply_110));
1038 packet_set_type(rpacket,D2CS_CLIENT_CHARLISTREPLY_110);
1039 bn_short_set(&rpacket->u.d2cs_client_charlistreply_110.u1,0);
1040 n=0;
1041 if (!(dir=p_opendir(path))) {
1042 eventlog(eventlog_level_info,__FUNCTION__,"(*%s) charinfo directory do not exist, building it",account);
1043 p_mkdir(path,S_IRWXU);
1044 } else {
1045 exp_time = prefs_get_char_expire_time();
1046 while ((charname=p_readdir(dir))) {
1047 if (charname[0]=='.') continue;
1048 charinfo = xmalloc(sizeof(t_d2charinfo_file));
1049 if (d2charinfo_load(account,charname,charinfo)<0) {
1050 eventlog(eventlog_level_error,__FUNCTION__,"error loading charinfo for %s(*%s)",charname,account);
1051 xfree(charinfo);
1052 continue;
1053 }
1054 if (exp_time) {
1055 curr_exp_time = bn_int_get(charinfo->header.last_time)+exp_time;
1056 } else {
1057 curr_exp_time = 0x7FFFFFFF;
1058 }
1059 eventlog(eventlog_level_debug,__FUNCTION__,"adding char %s (*%s)", charname, account);
1060 d2charlist_add_char(&charlist_head,charinfo,curr_exp_time);
1061 n++;
1062 if (n>=maxchar) break;
1063 }
1064 if (n>=maxchar)
1065 maxchar = 0;
1066
1067 p_closedir(dir);
1068 if (!strcmp(charlist_sort_order, "ASC"))
1069 {
1070 t_elist * curr, *safe;
1071 t_d2charlist * ccharlist;
1072
1073 elist_for_each_safe(curr,&charlist_head,safe)
1074 {
1075 bn_int bn_exp_time;
1076
1077 ccharlist = elist_entry(curr,t_d2charlist,list);
1078 bn_int_set(&bn_exp_time,ccharlist->expiration_time);
1079 packet_append_data(rpacket,bn_exp_time,sizeof(bn_exp_time));
1080 packet_append_string(rpacket,ccharlist->charinfo->header.charname);
1081 packet_append_string(rpacket,(char *)&ccharlist->charinfo->portrait);
1082 xfree((void *)ccharlist->charinfo);
1083 xfree((void *)ccharlist);
1084 }
1085 }
1086 else
1087 {
1088 t_elist * curr, *safe;
1089 t_d2charlist * ccharlist;
1090
1091 elist_for_each_safe_rev(curr,&charlist_head,safe)
1092 {
1093 bn_int bn_exp_time;
1094
1095 ccharlist = elist_entry(curr,t_d2charlist,list);
1096 bn_int_set(&bn_exp_time,ccharlist->expiration_time);
1097 packet_append_data(rpacket,bn_exp_time,sizeof(bn_exp_time));
1098 packet_append_string(rpacket,ccharlist->charinfo->header.charname);
1099 packet_append_string(rpacket,(char *)&ccharlist->charinfo->portrait);
1100 xfree((void *)ccharlist->charinfo);
1101 xfree((void *)ccharlist);
1102 }
1103 }
1104 }
1105 bn_short_set(&rpacket->u.d2cs_client_charlistreply.currchar,n);
1106 bn_short_set(&rpacket->u.d2cs_client_charlistreply.currchar2,n);
1107 bn_short_set(&rpacket->u.d2cs_client_charlistreply.maxchar,maxchar);
1108
1109 conn_push_outqueue(c,rpacket);
1110 packet_del_ref(rpacket);
1111 }
1112 xfree(path);
1113 return 0;
1114 }
1115
1116 static int on_client_convertcharreq(t_connection * c, t_packet * packet)
1117 {
1118 t_packet * rpacket;
1119 char const * charname;
1120 char const * account;
1121 unsigned int reply;
1122
1123 if (!(charname=packet_get_str_const(packet,sizeof(t_client_d2cs_convertcharreq),MAX_CHARNAME_LEN))) {
1124 eventlog(eventlog_level_error,__FUNCTION__,"got bad character name");
1125 return -1;
1126 }
1127 if (conn_check_multilogin(c,charname)<0) {
1128 eventlog(eventlog_level_error,__FUNCTION__,"character %s is already logged in",charname);
1129 return -1;
1130 }
1131 account=d2cs_conn_get_account(c);
1132 if (d2char_convert(account,charname)<0) {
1133 eventlog(eventlog_level_error,__FUNCTION__,"failed to convert character %s(*%s)",charname,account);
1134 reply = D2CS_CLIENT_CONVERTCHARREPLY_FAILED;
1135 } else {
1136 reply = D2CS_CLIENT_CONVERTCHARREPLY_SUCCEED;
1137 }
1138 if ((rpacket=packet_create(packet_class_d2cs))) {
1139 packet_set_size(rpacket,sizeof(t_d2cs_client_convertcharreply));
1140 packet_set_type(rpacket,D2CS_CLIENT_CONVERTCHARREPLY);
1141 bn_int_set(&rpacket->u.d2cs_client_convertcharreply.reply,reply);
1142 conn_push_outqueue(c,rpacket);
1143 packet_del_ref(rpacket);
1144 }
1145 return 0;
1146 }
1147
1148 extern int d2cs_send_client_creategamewait(t_connection * c, unsigned int position)
1149 {
1150 t_packet * packet;
1151
1152 ASSERT(c,-1);
1153 if ((packet=packet_create(packet_class_d2cs))) {
1154 packet_set_size(packet,sizeof(t_d2cs_client_creategamewait));
1155 packet_set_type(packet,D2CS_CLIENT_CREATEGAMEWAIT);
1156 bn_int_set(&packet->u.d2cs_client_creategamewait.position,position);
1157 conn_push_outqueue(c,packet);
1158 packet_del_ref(packet);
1159 }
1160 return 0;
1161 }
1162
1163 extern int d2cs_handle_client_creategame(t_connection * c, t_packet * packet)
1164 {
1165 return on_client_creategamereq(c,packet);
1166 }
1167
1168 static unsigned int d2cs_try_joingame(t_connection const * c, t_game const * game, char const * gamepass)
1169 {
1170 unsigned int reply;
1171
1172 ASSERT(c,D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST);
1173 ASSERT(game,D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST);
1174 if (!game_get_created(game)) {
1175 reply=D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST;
1176 } else if (!game_get_d2gs(game)) {
1177 reply=D2CS_CLIENT_JOINGAMEREPLY_NOT_EXIST;
1178 } else if (conn_get_charinfo_ladder(c) != game_get_gameflag_ladder(game)) {
1179 reply=D2CS_CLIENT_JOINGAMEREPLY_NORMAL_LADDER;
1180 } else if (conn_get_charinfo_expansion(c) != game_get_gameflag_expansion(game)) {
1181 reply=D2CS_CLIENT_JOINGAMEREPLY_CLASSIC_EXPANSION;
1182 } else if (conn_get_charinfo_hardcore(c) != game_get_gameflag_hardcore(game)) {
1183 reply=D2CS_CLIENT_JOINGAMEREPLY_HARDCORE_SOFTCORE;
1184 } else if (conn_get_charinfo_difficulty(c) < game_get_gameflag_difficulty(game)) {
1185 reply=D2CS_CLIENT_JOINGAMEREPLY_NORMAL_NIGHTMARE;
1186 } else if (prefs_allow_gamelimit()) {
1187 if (game_get_maxchar(game) <= game_get_currchar(game)) {
1188 reply=D2CS_CLIENT_JOINGAMEREPLY_GAME_FULL;
1189 } else if (conn_get_charinfo_level(c) > game_get_maxlevel(game)) {
1190 reply=D2CS_CLIENT_JOINGAMEREPLY_LEVEL_LIMIT;
1191 } else if (conn_get_charinfo_level(c) < game_get_minlevel(game)) {
1192 reply=D2CS_CLIENT_JOINGAMEREPLY_LEVEL_LIMIT;
1193 } else if (strcmp(d2cs_game_get_pass(game),gamepass)) {
1194 reply=D2CS_CLIENT_JOINGAMEREPLY_BAD_PASS;
1195 } else {
1196 reply=D2CS_CLIENT_JOINGAMEREPLY_SUCCEED;
1197 }
1198 } else {
1199 if (game_get_currchar(game) >= MAX_CHAR_PER_GAME) {
1200 reply=D2CS_CLIENT_JOINGAMEREPLY_GAME_FULL;
1201 } else {
1202 reply=D2CS_CLIENT_JOINGAMEREPLY_SUCCEED;
1203 }
1204 }
1205 return reply;
1206 }

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