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