| 1 | /* |
| 2 | * Copyright (C) 2000,2001 Onlyer ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #include "common/setup_before.h" |
| 19 | #include "setup.h" |
| 20 | #include "handle_d2gs.h" |
| 21 | |
| 22 | #include <ctime> |
| 23 | |
| 24 | #ifdef HAVE_SYS_TYPES_H |
| 25 | # include <sys/types.h> |
| 26 | #endif |
| 27 | #ifdef HAVE_SYS_STAT_H |
| 28 | # include <sys/stat.h> |
| 29 | #endif |
| 30 | #ifdef HAVE_UNISTD_H |
| 31 | # include <unistd.h> |
| 32 | #endif |
| 33 | |
| 34 | #include "common/eventlog.h" |
| 35 | #include "common/addr.h" |
| 36 | #include "common/trans.h" |
| 37 | #include "d2gs.h" |
| 38 | #include "serverqueue.h" |
| 39 | #include "game.h" |
| 40 | #include "prefs.h" |
| 41 | #include "common/setup_after.h" |
| 42 | |
| 43 | namespace pvpgn |
| 44 | { |
| 45 | |
| 46 | namespace d2cs |
| 47 | { |
| 48 | |
| 49 | DECLARE_PACKET_HANDLER(on_d2gs_authreply) |
| 50 | DECLARE_PACKET_HANDLER(on_d2gs_setgsinfo) |
| 51 | DECLARE_PACKET_HANDLER(on_d2gs_echoreply) |
| 52 | DECLARE_PACKET_HANDLER(on_d2gs_creategamereply) |
| 53 | DECLARE_PACKET_HANDLER(on_d2gs_joingamereply) |
| 54 | DECLARE_PACKET_HANDLER(on_d2gs_updategameinfo) |
| 55 | DECLARE_PACKET_HANDLER(on_d2gs_closegame) |
| 56 | |
| 57 | static t_packet_handle_table d2gs_packet_handle_table[]={ |
| 58 | /* 0x00 */ { 0, conn_state_none, NULL }, |
| 59 | /* 0x01 */ { 0, conn_state_none, NULL }, |
| 60 | /* 0x02 */ { 0, conn_state_none, NULL }, |
| 61 | /* 0x03 */ { 0, conn_state_none, NULL }, |
| 62 | /* 0x04 */ { 0, conn_state_none, NULL }, |
| 63 | /* 0x05 */ { 0, conn_state_none, NULL }, |
| 64 | /* 0x06 */ { 0, conn_state_none, NULL }, |
| 65 | /* 0x07 */ { 0, conn_state_none, NULL }, |
| 66 | /* 0x08 */ { 0, conn_state_none, NULL }, |
| 67 | /* 0x09 */ { 0, conn_state_none, NULL }, |
| 68 | /* 0x0a */ { 0, conn_state_none, NULL }, |
| 69 | /* 0x0b */ { 0, conn_state_none, NULL }, |
| 70 | /* 0x0c */ { 0, conn_state_none, NULL }, |
| 71 | /* 0x0d */ { 0, conn_state_none, NULL }, |
| 72 | /* 0x0e */ { 0, conn_state_none, NULL }, |
| 73 | /* 0x0f */ { 0, conn_state_none, NULL }, |
| 74 | /* 0x10 */ { 0, conn_state_none, NULL }, |
| 75 | /* FIXME: shouldn't these three be at 0x10-0x12? (But I'm pretty sure I preserved the padding) */ |
| 76 | /* 0x11 */ { sizeof(t_d2gs_d2cs_authreply), conn_state_connected, on_d2gs_authreply }, |
| 77 | /* 0x12 */ { sizeof(t_d2gs_d2cs_setgsinfo), conn_state_authed, on_d2gs_setgsinfo }, |
| 78 | /* 0x13 */ { sizeof(t_d2gs_d2cs_echoreply), conn_state_any, on_d2gs_echoreply }, |
| 79 | /* 0x14 */ { 0, conn_state_none, NULL }, |
| 80 | /* 0x15 */ { 0, conn_state_none, NULL }, |
| 81 | /* 0x16 */ { 0, conn_state_none, NULL }, |
| 82 | /* 0x17 */ { 0, conn_state_none, NULL }, |
| 83 | /* 0x18 */ { 0, conn_state_none, NULL }, |
| 84 | /* 0x19 */ { 0, conn_state_none, NULL }, |
| 85 | /* 0x1a */ { 0, conn_state_none, NULL }, |
| 86 | /* 0x1b */ { 0, conn_state_none, NULL }, |
| 87 | /* 0x1c */ { 0, conn_state_none, NULL }, |
| 88 | /* 0x1d */ { 0, conn_state_none, NULL }, |
| 89 | /* 0x1e */ { 0, conn_state_none, NULL }, |
| 90 | /* 0x1f */ { 0, conn_state_none, NULL }, |
| 91 | /* 0x20 */ { sizeof(t_d2gs_d2cs_creategamereply),conn_state_authed, on_d2gs_creategamereply }, |
| 92 | /* 0x21 */ { sizeof(t_d2gs_d2cs_joingamereply), conn_state_authed, on_d2gs_joingamereply }, |
| 93 | /* 0x22 */ { sizeof(t_d2gs_d2cs_updategameinfo), conn_state_authed, on_d2gs_updategameinfo }, |
| 94 | /* 0x23 */ { sizeof(t_d2gs_d2cs_closegame), conn_state_authed, on_d2gs_closegame }, |
| 95 | }; |
| 96 | |
| 97 | static void d2gs_send_init_info(t_d2gs * gs, t_connection * c) |
| 98 | { |
| 99 | t_packet * rpacket; |
| 100 | |
| 101 | if ((rpacket=packet_create(packet_class_d2gs))) { |
| 102 | packet_set_size(rpacket,sizeof(t_d2cs_d2gs_setinitinfo)); |
| 103 | packet_set_type(rpacket,D2CS_D2GS_SETINITINFO); |
| 104 | bn_int_set(&rpacket->u.d2cs_d2gs_setinitinfo.h.seqno,0); |
| 105 | bn_int_set(&rpacket->u.d2cs_d2gs_setinitinfo.time, std::time(NULL)); |
| 106 | bn_int_set(&rpacket->u.d2cs_d2gs_setinitinfo.gs_id, d2gs_get_id(gs)); |
| 107 | bn_int_set(&rpacket->u.d2cs_d2gs_setinitinfo.ac_version, 0); |
| 108 | // packet_append_string(rpacket,prefs_get_d2gs_ac_checksum()); |
| 109 | packet_append_string(rpacket,"bogus_ac_checksum"); |
| 110 | // packet_append_string(rpacket,prefs_get_d2gs_ac_string()); |
| 111 | packet_append_string(rpacket,"bogus_ac_string"); |
| 112 | conn_push_outqueue(c,rpacket); |
| 113 | packet_del_ref(rpacket); |
| 114 | } |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | static void d2gs_send_server_conffile(t_d2gs *gs, t_connection *c) |
| 119 | { |
| 120 | t_packet *rpacket; |
| 121 | std::FILE *fp; |
| 122 | char buff[256]; |
| 123 | int rno; |
| 124 | struct stat sfile; |
| 125 | |
| 126 | /* open d2gs server config file */ |
| 127 | fp = std::fopen(prefs_get_d2gsconffile(), "rb"); |
| 128 | if (!fp) goto err; |
| 129 | |
| 130 | /* get file size */ |
| 131 | if (fstat(fileno(fp),&sfile)) { |
| 132 | eventlog(eventlog_level_debug,__FUNCTION__,"failed fstat()"); |
| 133 | goto err_fp; |
| 134 | } |
| 135 | |
| 136 | rpacket = packet_create(packet_class_d2cs); |
| 137 | packet_set_size(rpacket,sizeof(t_d2cs_d2gs_setconffile)); |
| 138 | packet_set_type(rpacket,D2CS_D2GS_SETCONFFILE); |
| 139 | bn_int_set(&rpacket->u.d2cs_d2gs_setconffile.h.seqno,0); |
| 140 | bn_int_set(&rpacket->u.d2cs_d2gs_setconffile.size, sfile.st_size); |
| 141 | bn_int_set(&rpacket->u.d2cs_d2gs_setconffile.reserved1, std::time(NULL)); |
| 142 | |
| 143 | while((rno = std::fread(buff,1,sizeof(buff),fp)) > 0) { |
| 144 | if (packet_append_data(rpacket,buff,rno) < 0) { /* file too big */ |
| 145 | eventlog(eventlog_level_debug,__FUNCTION__,"conffile too big for a single packet"); |
| 146 | goto err_packet; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | conn_push_outqueue(c,rpacket); |
| 151 | packet_del_ref(rpacket); |
| 152 | std::fclose(fp); |
| 153 | |
| 154 | return; |
| 155 | |
| 156 | err_packet: |
| 157 | packet_del_ref(rpacket); |
| 158 | |
| 159 | err_fp: |
| 160 | std::fclose(fp); |
| 161 | |
| 162 | err: |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | static int on_d2gs_authreply(t_connection * c, t_packet * packet) |
| 167 | { |
| 168 | t_d2gs * gs; |
| 169 | t_packet * rpacket; |
| 170 | unsigned int reply; |
| 171 | unsigned int try_checksum, checksum; |
| 172 | unsigned int version, conf_version; |
| 173 | unsigned int randnum, signlen; |
| 174 | unsigned char *sign; |
| 175 | |
| 176 | if (!(gs=d2gslist_find_gs(conn_get_d2gs_id(c)))) { |
| 177 | eventlog(eventlog_level_error,__FUNCTION__,"game server {} not found",conn_get_d2gs_id(c)); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | version=bn_int_get(packet->u.d2gs_d2cs_authreply.version); |
| 182 | try_checksum=bn_int_get(packet->u.d2gs_d2cs_authreply.checksum); |
| 183 | checksum=d2gs_calc_checksum(c); |
| 184 | |
| 185 | conf_version = prefs_get_d2gs_version(); |
| 186 | randnum = bn_int_get(packet->u.d2gs_d2cs_authreply.randnum); |
| 187 | signlen = bn_int_get(packet->u.d2gs_d2cs_authreply.signlen); |
| 188 | sign = packet->u.d2gs_d2cs_authreply.sign; |
| 189 | if (conf_version && conf_version != version) { |
| 190 | eventlog(eventlog_level_warn,__FUNCTION__,"game server {} version mismatch 0x{:X} - 0x{:X}",conn_get_d2gs_id(c), |
| 191 | version,conf_version); |
| 192 | } |
| 193 | if (conf_version && !MAJOR_VERSION_EQUAL(version, conf_version, D2GS_MAJOR_VERSION_MASK)) { |
| 194 | eventlog(eventlog_level_error,__FUNCTION__,"game server {} major version mismatch 0x{:X} - 0x{:X}",conn_get_d2gs_id(c), |
| 195 | version,conf_version); |
| 196 | reply=D2CS_D2GS_AUTHREPLY_BAD_VERSION; |
| 197 | } else if (prefs_get_d2gs_checksum() && try_checksum != checksum) { |
| 198 | eventlog(eventlog_level_error,__FUNCTION__,"game server {} checksum mismach 0x{:X} - 0x{:X}",conn_get_d2gs_id(c),try_checksum,checksum); |
| 199 | reply=D2CS_D2GS_AUTHREPLY_BAD_CHECKSUM; |
| 200 | // } else if (license_verify_reply(c, randnum, sign, signlen)) { |
| 201 | // eventlog(eventlog_level_error,__FUNCTION__,"game server {} std::signal mismach", conn_get_d2gs_id(c)); |
| 202 | // reply=D2CS_D2GS_AUTHREPLY_BAD_CHECKSUM; |
| 203 | } else { |
| 204 | reply=D2CS_D2GS_AUTHREPLY_SUCCEED; |
| 205 | } |
| 206 | |
| 207 | if (reply==D2CS_D2GS_AUTHREPLY_SUCCEED) { |
| 208 | eventlog(eventlog_level_info,__FUNCTION__,"game server {} authed",addr_num_to_ip_str(d2cs_conn_get_addr(c))); |
| 209 | d2cs_conn_set_state(c,conn_state_authed); |
| 210 | d2gs_send_server_conffile(gs, c); |
| 211 | d2gs_send_init_info(gs, c); |
| 212 | d2gs_active(gs,c); |
| 213 | } else { |
| 214 | eventlog(eventlog_level_error,__FUNCTION__,"game server {} failed to auth",addr_num_to_ip_str(d2cs_conn_get_addr(c))); |
| 215 | /* |
| 216 | d2cs_conn_set_state(c,conn_state_destroy); |
| 217 | */ |
| 218 | } |
| 219 | if ((rpacket=packet_create(packet_class_d2gs))) { |
| 220 | packet_set_size(rpacket,sizeof(t_d2cs_d2gs_authreply)); |
| 221 | packet_set_type(rpacket,D2CS_D2GS_AUTHREPLY); |
| 222 | bn_int_set(&rpacket->u.d2cs_d2gs_authreply.h.seqno,0); |
| 223 | bn_int_set(&rpacket->u.d2cs_d2gs_authreply.reply,reply); |
| 224 | conn_push_outqueue(c,rpacket); |
| 225 | packet_del_ref(rpacket); |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int on_d2gs_setgsinfo(t_connection * c, t_packet * packet) |
| 232 | { |
| 233 | t_d2gs * gs; |
| 234 | t_packet * rpacket; |
| 235 | unsigned int maxgame, prev_maxgame; |
| 236 | unsigned int currgame, gameflag; |
| 237 | |
| 238 | if (!(gs=d2gslist_find_gs(conn_get_d2gs_id(c)))) { |
| 239 | eventlog(eventlog_level_error,__FUNCTION__,"game server {} not found",conn_get_d2gs_id(c)); |
| 240 | return -1; |
| 241 | } |
| 242 | maxgame=bn_int_get(packet->u.d2gs_d2cs_setgsinfo.maxgame); |
| 243 | gameflag=bn_int_get(packet->u.d2gs_d2cs_setgsinfo.gameflag); |
| 244 | prev_maxgame=d2gs_get_maxgame(gs); |
| 245 | currgame = d2gs_get_gamenum(gs); |
| 246 | eventlog(eventlog_level_info, __FUNCTION__, "change game server {} max game from {} to {} ({} current)",addr_num_to_ip_str(d2cs_conn_get_addr(c)),prev_maxgame, maxgame, currgame); |
| 247 | d2gs_set_maxgame(gs,maxgame); |
| 248 | |
| 249 | if ((rpacket=packet_create(packet_class_d2gs))) { |
| 250 | packet_set_size(rpacket,sizeof(t_d2cs_d2gs_setgsinfo)); |
| 251 | packet_set_type(rpacket,D2CS_D2GS_SETGSINFO); |
| 252 | bn_int_set(&rpacket->u.d2cs_d2gs_setgsinfo.h.seqno,0); |
| 253 | bn_int_set(&rpacket->u.d2cs_d2gs_setgsinfo.maxgame,maxgame); |
| 254 | bn_int_set(&rpacket->u.d2cs_d2gs_setgsinfo.gameflag,gameflag); |
| 255 | conn_push_outqueue(c,rpacket); |
| 256 | packet_del_ref(rpacket); |
| 257 | } |
| 258 | |
| 259 | gqlist_check_creategame(maxgame - currgame); |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int on_d2gs_echoreply(t_connection * c, t_packet * packet) |
| 264 | { |
| 265 | if (!c || !packet) |
| 266 | return 0; |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int on_d2gs_creategamereply(t_connection * c, t_packet * packet) |
| 271 | { |
| 272 | t_packet * opacket, * rpacket; |
| 273 | t_sq * sq; |
| 274 | t_connection * client; |
| 275 | t_game * game; |
| 276 | int result; |
| 277 | int reply; |
| 278 | int seqno; |
| 279 | t_elem * curr; |
| 280 | |
| 281 | seqno=bn_int_get(packet->u.d2cs_d2gs.h.seqno); |
| 282 | if (!(sq=sqlist_find_sq(seqno))) { |
| 283 | eventlog(eventlog_level_error,__FUNCTION__,"seqno {} not found",seqno); |
| 284 | return 0; |
| 285 | } |
| 286 | if (!(client=d2cs_connlist_find_connection_by_sessionnum(sq_get_clientid(sq)))) { |
| 287 | eventlog(eventlog_level_error,__FUNCTION__,"client {} not found",sq_get_clientid(sq)); |
| 288 | sq_destroy(sq,&curr); |
| 289 | return 0; |
| 290 | } |
| 291 | if (!(game=gamelist_find_game_by_id(sq_get_gameid(sq)))) { |
| 292 | eventlog(eventlog_level_error,__FUNCTION__,"game {} not found",sq_get_gameid(sq)); |
| 293 | sq_destroy(sq,&curr); |
| 294 | return 0; |
| 295 | } |
| 296 | if (!(opacket=sq_get_packet(sq))) { |
| 297 | eventlog(eventlog_level_error,__FUNCTION__,"previous packet not found (seqno: {})",seqno); |
| 298 | sq_destroy(sq,&curr); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | result=bn_int_get(packet->u.d2gs_d2cs_creategamereply.result); |
| 303 | if (result==D2GS_D2CS_CREATEGAME_SUCCEED) { |
| 304 | game_set_d2gs_gameid(game,bn_int_get(packet->u.d2gs_d2cs_creategamereply.gameid)); |
| 305 | game_set_created(game,1); |
| 306 | eventlog(eventlog_level_info,__FUNCTION__,"game {} created on gs {}",d2cs_game_get_name(game),conn_get_d2gs_id(c)); |
| 307 | reply=D2CS_CLIENT_CREATEGAMEREPLY_SUCCEED; |
| 308 | } else { |
| 309 | eventlog(eventlog_level_warn,__FUNCTION__,"failed to create game {} on gs {}",d2cs_game_get_name(game),conn_get_d2gs_id(c)); |
| 310 | game_destroy(game,&curr); |
| 311 | reply=D2CS_CLIENT_CREATEGAMEREPLY_FAILED; |
| 312 | } |
| 313 | |
| 314 | if ((rpacket=packet_create(packet_class_d2cs))) { |
| 315 | packet_set_size(rpacket,sizeof(t_d2cs_client_creategamereply)); |
| 316 | packet_set_type(rpacket,D2CS_CLIENT_CREATEGAMEREPLY); |
| 317 | bn_short_set(&rpacket->u.d2cs_client_creategamereply.seqno, |
| 318 | bn_short_get(opacket->u.client_d2cs_creategamereq.seqno)); |
| 319 | bn_short_set(&rpacket->u.d2cs_client_creategamereply.gameid,1); |
| 320 | bn_short_set(&rpacket->u.d2cs_client_creategamereply.u1,1); |
| 321 | bn_int_set(&rpacket->u.d2cs_client_creategamereply.reply,reply); |
| 322 | conn_push_outqueue(client,rpacket); |
| 323 | packet_del_ref(rpacket); |
| 324 | } |
| 325 | sq_destroy(sq,&curr); |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static int on_d2gs_joingamereply(t_connection * c, t_packet * packet) |
| 330 | { |
| 331 | t_sq * sq; |
| 332 | t_d2gs * gs; |
| 333 | t_connection * client; |
| 334 | t_game * game; |
| 335 | t_packet * opacket, * rpacket; |
| 336 | int result; |
| 337 | int reply; |
| 338 | int seqno; |
| 339 | unsigned int gsaddr; |
| 340 | t_elem * curr; |
| 341 | unsigned short gsport; |
| 342 | |
| 343 | |
| 344 | seqno=bn_int_get(packet->u.d2cs_d2gs.h.seqno); |
| 345 | if (!(sq=sqlist_find_sq(seqno))) { |
| 346 | eventlog(eventlog_level_error,__FUNCTION__,"seqno {} not found",seqno); |
| 347 | return 0; |
| 348 | } |
| 349 | if (!(client=d2cs_connlist_find_connection_by_sessionnum(sq_get_clientid(sq)))) { |
| 350 | eventlog(eventlog_level_error,__FUNCTION__,"client {} not found",sq_get_clientid(sq)); |
| 351 | sq_destroy(sq,&curr); |
| 352 | return 0; |
| 353 | } |
| 354 | if (!(game=gamelist_find_game_by_id(sq_get_gameid(sq)))) { |
| 355 | eventlog(eventlog_level_error,__FUNCTION__,"game {} not found",sq_get_gameid(sq)); |
| 356 | sq_destroy(sq,&curr); |
| 357 | return 0; |
| 358 | } |
| 359 | if (!(gs=game_get_d2gs(game))) { |
| 360 | eventlog(eventlog_level_error,__FUNCTION__,"try join game without game server set"); |
| 361 | sq_destroy(sq,&curr); |
| 362 | return 0; |
| 363 | } |
| 364 | if (!(opacket=sq_get_packet(sq))) { |
| 365 | eventlog(eventlog_level_error,__FUNCTION__,"previous packet not found (seqno: {})",seqno); |
| 366 | sq_destroy(sq,&curr); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | result=bn_int_get(packet->u.d2gs_d2cs_joingamereply.result); |
| 371 | switch (result) { |
| 372 | case D2GS_D2CS_JOINGAME_SUCCEED: |
| 373 | eventlog(eventlog_level_info,__FUNCTION__,"added {} to game {} on gs {}",d2cs_conn_get_charname(client), |
| 374 | d2cs_game_get_name(game),conn_get_d2gs_id(c)); |
| 375 | reply=D2CS_CLIENT_JOINGAMEREPLY_SUCCEED; |
| 376 | break; |
| 377 | |
| 378 | case D2GS_D2CS_JOINGAME_GAME_FULL: |
| 379 | eventlog(eventlog_level_info,__FUNCTION__,"failed to add {} to game {} on gs {} (game full)",d2cs_conn_get_charname(client), |
| 380 | d2cs_game_get_name(game),conn_get_d2gs_id(c)); |
| 381 | reply=D2CS_CLIENT_JOINGAMEREPLY_GAME_FULL; |
| 382 | break; |
| 383 | default: |
| 384 | eventlog(eventlog_level_info,__FUNCTION__,"failed to add {} to game {} on gs {}",d2cs_conn_get_charname(client), |
| 385 | d2cs_game_get_name(game),conn_get_d2gs_id(c)); |
| 386 | reply=D2CS_CLIENT_JOINGAMEREPLY_FAILED; |
| 387 | } |
| 388 | |
| 389 | if ((rpacket=packet_create(packet_class_d2cs))) { |
| 390 | packet_set_size(rpacket,sizeof(t_d2cs_client_joingamereply)); |
| 391 | packet_set_type(rpacket,D2CS_CLIENT_JOINGAMEREPLY); |
| 392 | bn_short_set(&rpacket->u.d2cs_client_joingamereply.seqno, |
| 393 | bn_short_get(opacket->u.client_d2cs_joingamereq.seqno)); |
| 394 | /* specific flag for anti-cheating support */ |
| 395 | /* |
| 396 | bn_short_set(&rpacket->u.d2cs_client_joingamereply.gameid,(unsigned short)game_get_d2gs_gameid(game)|0x8000); |
| 397 | */ |
| 398 | bn_short_set(&rpacket->u.d2cs_client_joingamereply.gameid,(unsigned short)game_get_d2gs_gameid(game)); |
| 399 | |
| 400 | bn_short_set(&rpacket->u.d2cs_client_joingamereply.u1,0); |
| 401 | bn_int_set(&rpacket->u.d2cs_client_joingamereply.reply,reply); |
| 402 | // if (reply == SERVER_JOINGAMEREPLY2_REPLY_OK) { |
| 403 | if (reply == D2CS_CLIENT_JOINGAMEREPLY_SUCCEED) { |
| 404 | bn_int_set(&rpacket->u.d2cs_client_joingamereply.token,sq_get_gametoken(sq)); |
| 405 | |
| 406 | gsaddr = d2gs_get_ip(gs); |
| 407 | gsport = 4000; |
| 408 | trans_net(d2cs_conn_get_addr(client), &gsaddr, &gsport); |
| 409 | |
| 410 | if(d2gs_get_ip(gs)!=gsaddr) |
| 411 | { |
| 412 | eventlog(eventlog_level_info,__FUNCTION__,"translated gameserver {} -> {}",addr_num_to_ip_str(d2gs_get_ip(gs)),addr_num_to_ip_str(gsaddr)); |
| 413 | } else { |
| 414 | eventlog(eventlog_level_info,__FUNCTION__,"no translation required for gamserver {}",addr_num_to_ip_str(gsaddr)); |
| 415 | } |
| 416 | |
| 417 | bn_int_nset(&rpacket->u.d2cs_client_joingamereply.addr,gsaddr); |
| 418 | |
| 419 | } else { |
| 420 | bn_int_set(&rpacket->u.d2cs_client_joingamereply.token,0); |
| 421 | bn_int_set(&rpacket->u.d2cs_client_joingamereply.addr,0); |
| 422 | } |
| 423 | conn_push_outqueue(client,rpacket); |
| 424 | packet_del_ref(rpacket); |
| 425 | } |
| 426 | sq_destroy(sq,&curr); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static int on_d2gs_updategameinfo(t_connection * c, t_packet * packet) |
| 431 | { |
| 432 | unsigned int flag; |
| 433 | char const * charname; |
| 434 | t_game * game; |
| 435 | unsigned int charclass; |
| 436 | unsigned int charlevel; |
| 437 | unsigned int d2gs_gameid; |
| 438 | unsigned int d2gs_id; |
| 439 | |
| 440 | if (!(charname=packet_get_str_const(packet,sizeof(t_d2gs_d2cs_updategameinfo),MAX_CHARNAME_LEN))) { |
| 441 | eventlog(eventlog_level_error,__FUNCTION__,"got bad charname"); |
| 442 | return 0; |
| 443 | } |
| 444 | d2gs_id=conn_get_d2gs_id(c); |
| 445 | d2gs_gameid=bn_int_get(packet->u.d2gs_d2cs_updategameinfo.gameid); |
| 446 | charclass=bn_int_get(packet->u.d2gs_d2cs_updategameinfo.charclass); |
| 447 | charlevel=bn_int_get(packet->u.d2gs_d2cs_updategameinfo.charlevel); |
| 448 | flag=bn_int_get(packet->u.d2gs_d2cs_updategameinfo.flag); |
| 449 | if (!(game=gamelist_find_game_by_d2gs_and_id(d2gs_id,d2gs_gameid))) { |
| 450 | eventlog(eventlog_level_error,__FUNCTION__,"game {} not found on gs {}",d2gs_gameid,d2gs_id); |
| 451 | return -1; |
| 452 | } |
| 453 | if (flag==D2GS_D2CS_UPDATEGAMEINFO_FLAG_ENTER) { |
| 454 | game_add_character(game,charname,charclass,charlevel); |
| 455 | } else if (flag==D2GS_D2CS_UPDATEGAMEINFO_FLAG_LEAVE) { |
| 456 | game_del_character(game,charname); |
| 457 | } else if (flag==D2GS_D2CS_UPDATEGAMEINFO_FLAG_UPDATE) { |
| 458 | game_add_character(game,charname,charclass,charlevel); |
| 459 | } else { |
| 460 | eventlog(eventlog_level_error,__FUNCTION__,"got bad updategameinfo flag {}",flag); |
| 461 | } |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static int on_d2gs_closegame(t_connection * c, t_packet * packet) |
| 466 | { |
| 467 | t_game * game; |
| 468 | t_elem * curr; |
| 469 | |
| 470 | if (!(game=gamelist_find_game_by_d2gs_and_id(conn_get_d2gs_id(c), |
| 471 | bn_int_get(packet->u.d2gs_d2cs_closegame.gameid)))) { |
| 472 | return 0; |
| 473 | } |
| 474 | game_destroy(game, &curr); |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | extern int handle_d2gs_packet(t_connection * c, t_packet * packet) |
| 479 | { |
| 480 | conn_process_packet(c,packet,d2gs_packet_handle_table,NELEMS(d2gs_packet_handle_table)); |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | extern int handle_d2gs_init(t_connection * c) |
| 485 | { |
| 486 | t_packet * packet; |
| 487 | |
| 488 | if ((packet=packet_create(packet_class_d2gs))) { |
| 489 | packet_set_size(packet,sizeof(t_d2cs_d2gs_authreq)); |
| 490 | packet_set_type(packet,D2CS_D2GS_AUTHREQ); |
| 491 | bn_int_set(&packet->u.d2cs_d2gs_authreq.h.seqno,0); |
| 492 | bn_int_set(&packet->u.d2cs_d2gs_authreq.sessionnum,d2cs_conn_get_sessionnum(c)); |
| 493 | bn_int_set(&packet->u.d2cs_d2gs_authreq.signlen, 0); |
| 494 | packet_append_string(packet,prefs_get_realmname()); |
| 495 | // packet_append_data(packet, sign, signlen); |
| 496 | conn_push_outqueue(c,packet); |
| 497 | packet_del_ref(packet); |
| 498 | } |
| 499 | eventlog(eventlog_level_info,__FUNCTION__,"sent init packet to d2gs {} (sessionnum={})",conn_get_d2gs_id(c),d2cs_conn_get_sessionnum(c)); |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | } |
| 504 | |
| 505 | } |