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