| 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 "connection.h" |
| 21 | |
| 22 | #include <limits> |
| 23 | #include <cctype> |
| 24 | #include <cstring> |
| 25 | #include <ctime> |
| 26 | #include <cassert> |
| 27 | |
| 28 | #include "compat/psock.h" |
| 29 | #include "compat/strcasecmp.h" |
| 30 | #include "common/eventlog.h" |
| 31 | #include "common/introtate.h" |
| 32 | #include "common/addr.h" |
| 33 | #include "common/xalloc.h" |
| 34 | #include "common/network.h" |
| 35 | #include "common/xstring.h" |
| 36 | #include "prefs.h" |
| 37 | #include "game.h" |
| 38 | #include "net.h" |
| 39 | #include "handle_init.h" |
| 40 | #include "handle_bnetd.h" |
| 41 | #include "handle_d2cs.h" |
| 42 | #include "handle_d2gs.h" |
| 43 | #include "d2gs.h" |
| 44 | #include "s2s.h" |
| 45 | #include "common/setup_after.h" |
| 46 | |
| 47 | namespace pvpgn |
| 48 | { |
| 49 | |
| 50 | namespace d2cs |
| 51 | { |
| 52 | |
| 53 | static t_hashtable * connlist_head = NULL; |
| 54 | static t_hashtable * conn_charname_list_head = NULL; |
| 55 | static t_list * connlist_dead = NULL; |
| 56 | static unsigned int total_connection = 0; |
| 57 | |
| 58 | static int conn_handle_connecting(t_connection * c); |
| 59 | static t_packet * conn_create_packet(t_connection * c); |
| 60 | static int conn_handle_packet(t_connection * c, t_packet * packet); |
| 61 | static int conn_handle_read(t_connection * c); |
| 62 | static int conn_handle_write(t_connection * c); |
| 63 | static unsigned int conn_charname_hash(char const * charname); |
| 64 | static unsigned int conn_sessionnum_hash(unsigned int sessionnum); |
| 65 | |
| 66 | static unsigned int conn_sessionnum_hash(unsigned int sessionnum) |
| 67 | { |
| 68 | return sessionnum; |
| 69 | } |
| 70 | |
| 71 | static unsigned int conn_charname_hash(char const * charname) |
| 72 | { |
| 73 | unsigned int hash; |
| 74 | unsigned int i, len, pos; |
| 75 | unsigned int ch; |
| 76 | |
| 77 | ASSERT(charname, 0); |
| 78 | len = std::strlen(charname); |
| 79 | for (hash = 0, i = 0, pos = 0; i < len; i++) { |
| 80 | ch = safe_tolower(charname[i]); |
| 81 | hash ^= ROTL(ch, pos, (std::numeric_limits<unsigned int>::digits)); |
| 82 | pos += (std::numeric_limits<unsigned char>::digits) - 1; |
| 83 | } |
| 84 | return hash; |
| 85 | } |
| 86 | |
| 87 | extern t_hashtable * d2cs_connlist(void) |
| 88 | { |
| 89 | return connlist_head; |
| 90 | } |
| 91 | |
| 92 | extern int d2cs_connlist_create(void) |
| 93 | { |
| 94 | if (!(connlist_head = hashtable_create(200))) return -1; |
| 95 | if (!(conn_charname_list_head = hashtable_create(200))) return -1; |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | extern int d2cs_connlist_destroy(void) |
| 100 | { |
| 101 | t_connection * c; |
| 102 | t_elem * curr; |
| 103 | |
| 104 | |
| 105 | if (connlist_dead) { |
| 106 | d2cs_connlist_reap(); |
| 107 | if (list_destroy(connlist_dead)) |
| 108 | eventlog(eventlog_level_error, __FUNCTION__, "error destroy conndead list"); |
| 109 | connlist_dead = NULL; |
| 110 | } |
| 111 | |
| 112 | BEGIN_HASHTABLE_TRAVERSE_DATA(connlist_head, c, t_connection) |
| 113 | { |
| 114 | d2cs_conn_destroy(c, &curr); |
| 115 | } |
| 116 | END_HASHTABLE_TRAVERSE_DATA() |
| 117 | |
| 118 | if (hashtable_destroy(connlist_head) < 0) { |
| 119 | eventlog(eventlog_level_error, __FUNCTION__, "error destroy connection list"); |
| 120 | return -1; |
| 121 | } |
| 122 | connlist_head = NULL; |
| 123 | |
| 124 | if (hashtable_destroy(conn_charname_list_head) < 0) { |
| 125 | eventlog(eventlog_level_error, __FUNCTION__, "error destroy connection charname list"); |
| 126 | return -1; |
| 127 | } |
| 128 | conn_charname_list_head = NULL; |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | extern int d2cs_connlist_reap(void) |
| 133 | { |
| 134 | t_connection * c; |
| 135 | |
| 136 | if (!connlist_dead) return 0; |
| 137 | BEGIN_LIST_TRAVERSE_DATA(connlist_dead, c, t_connection) |
| 138 | { |
| 139 | d2cs_conn_destroy(c, &curr_elem_); |
| 140 | } |
| 141 | END_LIST_TRAVERSE_DATA() |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | extern int conn_check_multilogin(t_connection const * c, char const * charname) |
| 147 | { |
| 148 | t_connection * conn; |
| 149 | |
| 150 | ASSERT(charname, -1); |
| 151 | if (!prefs_check_multilogin()) return 0; |
| 152 | if (gamelist_find_character(charname)) { |
| 153 | return -1; |
| 154 | } |
| 155 | conn = d2cs_connlist_find_connection_by_charname(charname); |
| 156 | if (conn && conn != c) { |
| 157 | return -1; |
| 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | extern t_connection * d2cs_connlist_find_connection_by_sessionnum(unsigned int sessionnum) |
| 163 | { |
| 164 | t_connection * c; |
| 165 | t_entry * curr; |
| 166 | unsigned int hash; |
| 167 | |
| 168 | hash = conn_sessionnum_hash(sessionnum); |
| 169 | HASHTABLE_TRAVERSE_MATCHING(connlist_head, curr, hash) |
| 170 | { |
| 171 | if (!(c = (t_connection*)entry_get_data(curr))) { |
| 172 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection in list"); |
| 173 | } |
| 174 | else if (c->sessionnum == sessionnum) { |
| 175 | hashtable_entry_release(curr); |
| 176 | return c; |
| 177 | } |
| 178 | } |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | extern t_connection * d2cs_connlist_find_connection_by_charname(char const * charname) |
| 183 | { |
| 184 | t_entry * curr; |
| 185 | t_connection * c; |
| 186 | unsigned int hash; |
| 187 | |
| 188 | hash = conn_charname_hash(charname); |
| 189 | HASHTABLE_TRAVERSE_MATCHING(connlist_head, curr, hash) |
| 190 | { |
| 191 | if (!(c = (t_connection*)entry_get_data(curr))) { |
| 192 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection in list"); |
| 193 | } |
| 194 | else { |
| 195 | if (!c->charname) continue; |
| 196 | if (!strcmp_charname(c->charname, charname)) { |
| 197 | hashtable_entry_release(curr); |
| 198 | return c; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | static t_packet * conn_create_packet(t_connection * c) |
| 206 | { |
| 207 | t_packet * packet; |
| 208 | |
| 209 | switch (c->cclass) { |
| 210 | CASE(conn_class_init, packet = packet_create(packet_class_init)); |
| 211 | CASE(conn_class_d2cs, packet = packet_create(packet_class_d2cs)); |
| 212 | CASE(conn_class_d2gs, packet = packet_create(packet_class_d2gs)); |
| 213 | CASE(conn_class_bnetd, packet = packet_create(packet_class_d2cs_bnetd)); |
| 214 | default: |
| 215 | eventlog(eventlog_level_error, __FUNCTION__, "got bad connection class {}", c->cclass); |
| 216 | return NULL; |
| 217 | } |
| 218 | if (!packet) { |
| 219 | eventlog(eventlog_level_error, __FUNCTION__, "error create packet"); |
| 220 | return NULL; |
| 221 | } |
| 222 | d2cs_conn_set_in_queue(c, packet); |
| 223 | return packet; |
| 224 | } |
| 225 | |
| 226 | static int conn_handle_connecting(t_connection * c) |
| 227 | { |
| 228 | int retval; |
| 229 | |
| 230 | if (net_check_connected(c->sock) < 0) { |
| 231 | eventlog(eventlog_level_warn, __FUNCTION__, "can not connect to {}", addr_num_to_addr_str(c->addr, c->port)); |
| 232 | return -1; |
| 233 | } |
| 234 | eventlog(eventlog_level_info, __FUNCTION__, "connected to {}", addr_num_to_addr_str(c->addr, c->port)); |
| 235 | c->state = conn_state_init; |
| 236 | /* this is a kind of hack to not update fd but updating breaks kqueue |
| 237 | * and the clean fix would require a cache a userland copy of the kernel |
| 238 | * kqueue fds, considering that it also doesnt brake anything else should do |
| 239 | * for the moment |
| 240 | fdwatch_update_fd(c->sock, fdwatch_type_read); */ |
| 241 | switch (c->cclass) { |
| 242 | case conn_class_bnetd: |
| 243 | retval = handle_bnetd_init(c); |
| 244 | break; |
| 245 | default: |
| 246 | eventlog(eventlog_level_error, __FUNCTION__, "got bad connection class {}", c->cclass); |
| 247 | return -1; |
| 248 | } |
| 249 | return retval; |
| 250 | } |
| 251 | |
| 252 | static int conn_handle_packet(t_connection * c, t_packet * packet) |
| 253 | { |
| 254 | int retval; |
| 255 | |
| 256 | switch (c->cclass) { |
| 257 | CASE(conn_class_init, retval = d2cs_handle_init_packet(c, packet)); |
| 258 | CASE(conn_class_d2cs, retval = d2cs_handle_d2cs_packet(c, packet)); |
| 259 | CASE(conn_class_d2gs, retval = handle_d2gs_packet(c, packet)); |
| 260 | CASE(conn_class_bnetd, retval = handle_bnetd_packet(c, packet)); |
| 261 | default: |
| 262 | eventlog(eventlog_level_error, __FUNCTION__, "got bad connection class {} (close connection)", c->cclass); |
| 263 | retval = -1; |
| 264 | break; |
| 265 | } |
| 266 | return retval; |
| 267 | } |
| 268 | |
| 269 | static int conn_handle_read(t_connection * c) |
| 270 | { |
| 271 | t_packet * packet; |
| 272 | int retval; |
| 273 | |
| 274 | packet = d2cs_conn_get_in_queue(c); |
| 275 | if (!packet) { |
| 276 | packet = conn_create_packet(c); |
| 277 | if (!packet) return -1; |
| 278 | c->insize = 0; |
| 279 | } |
| 280 | |
| 281 | switch (net_recv_packet(c->sock, packet, &c->insize)) { |
| 282 | case -1: |
| 283 | retval = -1; |
| 284 | break; |
| 285 | case 0: |
| 286 | retval = 0; |
| 287 | break; |
| 288 | case 1: |
| 289 | c->insize = 0; |
| 290 | d2cs_conn_set_in_queue(c, NULL); |
| 291 | retval = conn_handle_packet(c, packet); |
| 292 | packet_del_ref(packet); |
| 293 | break; |
| 294 | default: |
| 295 | retval = 0; |
| 296 | break; |
| 297 | } |
| 298 | return retval; |
| 299 | } |
| 300 | |
| 301 | static int conn_handle_write(t_connection * c) |
| 302 | { |
| 303 | t_packet * packet; |
| 304 | int retval; |
| 305 | |
| 306 | if (c->state == conn_state_connecting) { |
| 307 | return conn_handle_connecting(c); |
| 308 | } |
| 309 | |
| 310 | if (!(packet = conn_peek_outqueue(c))) return 0; |
| 311 | |
| 312 | switch (net_send_packet(c->sock, packet, &c->outsize)) { |
| 313 | case -1: |
| 314 | retval = -1; |
| 315 | break; |
| 316 | case 0: |
| 317 | retval = 0; |
| 318 | break; |
| 319 | case 1: |
| 320 | c->outsize = 0; |
| 321 | packet = conn_pull_outqueue(c); |
| 322 | packet_del_ref(packet); |
| 323 | retval = 0; |
| 324 | break; |
| 325 | default: |
| 326 | retval = -1; |
| 327 | } |
| 328 | return retval; |
| 329 | } |
| 330 | |
| 331 | extern int conn_handle_socket(t_connection * c) |
| 332 | { |
| 333 | std::time_t now; |
| 334 | |
| 335 | ASSERT(c, -1); |
| 336 | now = std::time(NULL); |
| 337 | if (c->socket_flag & SOCKET_FLAG_READ) { |
| 338 | if (conn_handle_read(c) < 0) return -1; |
| 339 | c->last_active = now; |
| 340 | } |
| 341 | if (c->socket_flag & SOCKET_FLAG_WRITE) { |
| 342 | if (conn_handle_write(c) < 0) return -1; |
| 343 | c->last_active = now; |
| 344 | } |
| 345 | c->socket_flag = 0; |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | extern int connlist_check_timeout(void) |
| 350 | { |
| 351 | t_connection * c; |
| 352 | std::time_t now; |
| 353 | |
| 354 | now = std::time(NULL); |
| 355 | BEGIN_HASHTABLE_TRAVERSE_DATA(connlist_head, c, t_connection) |
| 356 | { |
| 357 | switch (c->cclass) { |
| 358 | case conn_class_d2cs: |
| 359 | if (prefs_get_idletime() && (now - c->last_active > prefs_get_idletime())) { |
| 360 | eventlog(eventlog_level_info, __FUNCTION__, "client {} idled too long std::time, destroy it", c->sessionnum); |
| 361 | d2cs_conn_set_state(c, conn_state_destroy); |
| 362 | } |
| 363 | break; |
| 364 | case conn_class_d2gs: |
| 365 | if (prefs_get_s2s_idletime() && now - c->last_active > prefs_get_s2s_idletime()) { |
| 366 | eventlog(eventlog_level_info, __FUNCTION__, "server {} timed out", c->sessionnum); |
| 367 | d2cs_conn_set_state(c, conn_state_destroy); |
| 368 | } |
| 369 | break; |
| 370 | case conn_class_bnetd: |
| 371 | break; |
| 372 | default: |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | END_HASHTABLE_TRAVERSE_DATA() |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | extern t_connection * d2cs_conn_create(int sock, unsigned int local_addr, unsigned short local_port, |
| 381 | unsigned int addr, unsigned short port) |
| 382 | { |
| 383 | static unsigned int sessionnum = 1; |
| 384 | t_connection * c; |
| 385 | |
| 386 | if (sock < 0) { |
| 387 | eventlog(eventlog_level_error, __FUNCTION__, "got bad socket"); |
| 388 | return NULL; |
| 389 | } |
| 390 | c = (t_connection*)xmalloc(sizeof(t_connection)); |
| 391 | c->charname = NULL; |
| 392 | c->account = NULL; |
| 393 | c->sock = sock; |
| 394 | c->fdw_idx = -1; |
| 395 | c->socket_flag = 0; |
| 396 | c->local_addr = local_addr; |
| 397 | c->local_port = local_port; |
| 398 | c->addr = addr; |
| 399 | c->port = port; |
| 400 | c->cclass = conn_class_init; |
| 401 | c->state = conn_state_init; |
| 402 | c->sessionnum = sessionnum++; |
| 403 | c->outqueue = NULL; |
| 404 | c->inqueue = NULL; |
| 405 | c->outsize = 0; |
| 406 | c->outsizep = 0; |
| 407 | c->insize = 0; |
| 408 | c->charinfo = NULL; |
| 409 | c->d2gs_id = 0; |
| 410 | c->gamequeue = NULL; |
| 411 | c->last_active = std::time(NULL); |
| 412 | c->sessionnum_hash = conn_sessionnum_hash(c->sessionnum); |
| 413 | c->bnetd_sessionnum = 0; |
| 414 | c->charname_hash = 0; |
| 415 | if (hashtable_insert_data(connlist_head, c, c->sessionnum_hash) < 0) { |
| 416 | xfree(c); |
| 417 | eventlog(eventlog_level_error, __FUNCTION__, "error add connection to list"); |
| 418 | return NULL; |
| 419 | } |
| 420 | total_connection++; |
| 421 | eventlog(eventlog_level_info, __FUNCTION__, "created session={} socket={} ({} current connections)", c->sessionnum, sock, total_connection); |
| 422 | return c; |
| 423 | } |
| 424 | |
| 425 | extern int d2cs_conn_destroy(t_connection * c, t_elem ** curr) |
| 426 | { |
| 427 | t_elem * elem; |
| 428 | |
| 429 | ASSERT(c, -1); |
| 430 | if (c->state == conn_state_destroying) return 0; |
| 431 | if (hashtable_remove_data(connlist_head, c, c->sessionnum_hash) < 0) { |
| 432 | eventlog(eventlog_level_error, __FUNCTION__, "error remove connection from list"); |
| 433 | return -1; |
| 434 | } |
| 435 | c->state = conn_state_destroying; |
| 436 | if (c->d2gs_id && c->cclass == conn_class_d2gs) { |
| 437 | d2gs_deactive(d2gslist_find_gs(c->d2gs_id), c); |
| 438 | } |
| 439 | if (c->cclass == conn_class_bnetd) { |
| 440 | s2s_destroy(c); |
| 441 | } |
| 442 | if (c->gamequeue) { |
| 443 | gq_destroy(c->gamequeue, &elem); |
| 444 | } |
| 445 | if (c->account) xfree((void *)c->account); |
| 446 | if (c->charinfo) xfree((void *)c->charinfo); |
| 447 | if (c->charname) d2cs_conn_set_charname(c, NULL); |
| 448 | if (c->inqueue) packet_del_ref(c->inqueue); |
| 449 | queue_clear(&c->outqueue); |
| 450 | |
| 451 | if (connlist_dead) list_remove_data(connlist_dead, c, curr); |
| 452 | fdwatch_del_fd(c->fdw_idx); |
| 453 | psock_shutdown(c->sock, PSOCK_SHUT_RDWR); |
| 454 | psock_close(c->sock); |
| 455 | |
| 456 | total_connection--; |
| 457 | eventlog(eventlog_level_info, __FUNCTION__, "[{}] closed connection {} ({} left)", c->sock, c->sessionnum, total_connection); |
| 458 | xfree(c); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | extern int d2cs_conn_get_socket(t_connection const * c) |
| 463 | { |
| 464 | ASSERT(c, -1); |
| 465 | return c->sock; |
| 466 | } |
| 467 | |
| 468 | extern t_conn_state d2cs_conn_get_state(t_connection const * c) |
| 469 | { |
| 470 | ASSERT(c, conn_state_none); |
| 471 | return c->state; |
| 472 | } |
| 473 | |
| 474 | extern int d2cs_conn_set_state(t_connection * c, t_conn_state state) |
| 475 | { |
| 476 | t_elem * curr; |
| 477 | |
| 478 | ASSERT(c, -1); |
| 479 | /* special case for destroying connections, add them to connlist_dead list */ |
| 480 | if (state == conn_state_destroy && c->state != conn_state_destroy) { |
| 481 | if (!connlist_dead) |
| 482 | connlist_dead = list_create(); |
| 483 | list_append_data(connlist_dead, c); |
| 484 | } |
| 485 | else if (state != conn_state_destroy && c->state == conn_state_destroy) { |
| 486 | if (list_remove_data(connlist_dead, c, &curr)) { |
| 487 | eventlog(eventlog_level_error, __FUNCTION__, "could not remove dead connection"); |
| 488 | return -1; |
| 489 | } |
| 490 | } |
| 491 | c->state = state; |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | extern t_conn_class d2cs_conn_get_class(t_connection const * c) |
| 496 | { |
| 497 | ASSERT(c, conn_class_none); |
| 498 | return c->cclass; |
| 499 | } |
| 500 | |
| 501 | extern int d2cs_conn_set_class(t_connection * c, t_conn_class cclass) |
| 502 | { |
| 503 | ASSERT(c, -1); |
| 504 | c->cclass = cclass; |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | extern t_packet * d2cs_conn_get_in_queue(t_connection const * c) |
| 509 | { |
| 510 | ASSERT(c, NULL); |
| 511 | return c->inqueue; |
| 512 | } |
| 513 | |
| 514 | extern void d2cs_conn_set_in_queue(t_connection * c, t_packet *packet) |
| 515 | { |
| 516 | assert(c); |
| 517 | c->inqueue = packet; |
| 518 | } |
| 519 | |
| 520 | extern unsigned int d2cs_conn_get_out_size(t_connection const * c) |
| 521 | { |
| 522 | ASSERT(c, 0); |
| 523 | return c->outsize; |
| 524 | } |
| 525 | |
| 526 | extern t_queue * * d2cs_conn_get_out_queue(t_connection const * c) |
| 527 | { |
| 528 | ASSERT(c, NULL); |
| 529 | return (t_queue * *)&c->outqueue; |
| 530 | } |
| 531 | |
| 532 | extern unsigned int d2cs_conn_get_in_size(t_connection const * c) |
| 533 | { |
| 534 | ASSERT(c, 0); |
| 535 | return c->insize; |
| 536 | } |
| 537 | |
| 538 | extern int conn_push_outqueue(t_connection * c, t_packet * packet) |
| 539 | { |
| 540 | if (!c) |
| 541 | { |
| 542 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 543 | return -1; |
| 544 | } |
| 545 | |
| 546 | if (!packet) |
| 547 | { |
| 548 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL packet"); |
| 549 | return -1; |
| 550 | } |
| 551 | |
| 552 | queue_push_packet((t_queue * *)&c->outqueue, packet); |
| 553 | if (!c->outsizep++) fdwatch_update_fd(c->fdw_idx, fdwatch_type_read | fdwatch_type_write); |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | extern t_packet * conn_peek_outqueue(t_connection * c) |
| 559 | { |
| 560 | if (!c) |
| 561 | { |
| 562 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | return queue_peek_packet((t_queue const * const *)&c->outqueue); |
| 567 | } |
| 568 | |
| 569 | extern t_packet * conn_pull_outqueue(t_connection * c) |
| 570 | { |
| 571 | if (!c) |
| 572 | { |
| 573 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 574 | return NULL; |
| 575 | } |
| 576 | |
| 577 | if (c->outsizep) { |
| 578 | if (!(--c->outsizep)) fdwatch_update_fd(c->fdw_idx, fdwatch_type_read); |
| 579 | return queue_pull_packet((t_queue * *)&c->outqueue); |
| 580 | } |
| 581 | |
| 582 | return NULL; |
| 583 | } |
| 584 | |
| 585 | extern int conn_add_socket_flag(t_connection * c, unsigned int flag) |
| 586 | { |
| 587 | ASSERT(c, -1); |
| 588 | c->socket_flag |= flag; |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | extern int conn_process_packet(t_connection * c, t_packet * packet, t_packet_handle_table * table, |
| 593 | unsigned int table_size) |
| 594 | { |
| 595 | unsigned int type; |
| 596 | unsigned int size; |
| 597 | |
| 598 | ASSERT(c, -1); |
| 599 | ASSERT(packet, -1); |
| 600 | ASSERT(table, -1); |
| 601 | type = packet_get_type(packet); |
| 602 | size = packet_get_size(packet); |
| 603 | |
| 604 | if (type >= table_size || !table[type].size) { |
| 605 | eventlog(eventlog_level_error, __FUNCTION__, "got bad packet type {} (class {})", type, packet_get_class(packet)); |
| 606 | return -1; |
| 607 | } |
| 608 | if (size < table[type].size) { |
| 609 | eventlog(eventlog_level_error, __FUNCTION__, "got bad packet size {} (type {} class {})", size, type, packet_get_class(packet)); |
| 610 | return -1; |
| 611 | } |
| 612 | if (!(c->state & table[type].state)) { |
| 613 | eventlog(eventlog_level_error, __FUNCTION__, "connection {} state mismatch for packet type {} (class {})", c->sessionnum, |
| 614 | type, packet_get_class(packet)); |
| 615 | return -1; |
| 616 | } |
| 617 | if (!table[type].handler) { |
| 618 | eventlog(eventlog_level_error, __FUNCTION__, "missing handler for packet type {} (class {})", type, packet_get_class(packet)); |
| 619 | return -1; |
| 620 | } |
| 621 | return table[type].handler(c, packet); |
| 622 | } |
| 623 | |
| 624 | extern int d2cs_conn_set_account(t_connection * c, char const * account) |
| 625 | { |
| 626 | ASSERT(c, -1); |
| 627 | if (!account) { |
| 628 | if (c->account) xfree((void *)c->account); |
| 629 | c->account = NULL; |
| 630 | } |
| 631 | if (c->account) xfree((void *)c->account); |
| 632 | c->account = xstrdup(account); |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | extern char const * d2cs_conn_get_account(t_connection const * c) |
| 638 | { |
| 639 | ASSERT(c, NULL); |
| 640 | return c->account; |
| 641 | } |
| 642 | |
| 643 | extern int d2cs_conn_set_charname(t_connection * c, char const * charname) |
| 644 | { |
| 645 | char const * temp; |
| 646 | |
| 647 | ASSERT(c, -1); |
| 648 | temp = NULL; |
| 649 | if (charname) temp = xstrdup(charname); |
| 650 | if (c->charname) { |
| 651 | if (hashtable_remove_data(conn_charname_list_head, c, c->charname_hash) < 0) { |
| 652 | eventlog(eventlog_level_error, __FUNCTION__, "error remove charname {} from list", charname); |
| 653 | if (temp) xfree((void *)temp); |
| 654 | return -1; |
| 655 | } |
| 656 | hashtable_purge(conn_charname_list_head); |
| 657 | xfree((void *)c->charname); |
| 658 | } |
| 659 | if (charname) { |
| 660 | c->charname = temp; |
| 661 | c->charname_hash = conn_charname_hash(charname); |
| 662 | if (hashtable_insert_data(conn_charname_list_head, c, c->charname_hash) < 0) { |
| 663 | eventlog(eventlog_level_error, __FUNCTION__, "error insert charname {} to list", charname); |
| 664 | xfree((void *)c->charname); |
| 665 | c->charname = NULL; |
| 666 | return -1; |
| 667 | } |
| 668 | } |
| 669 | else { |
| 670 | c->charname = NULL; |
| 671 | c->charname_hash = 0; |
| 672 | } |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | extern char const * d2cs_conn_get_charname(t_connection const * c) |
| 677 | { |
| 678 | ASSERT(c, NULL); |
| 679 | return c->charname; |
| 680 | } |
| 681 | |
| 682 | extern unsigned int d2cs_conn_get_sessionnum(t_connection const * c) |
| 683 | { |
| 684 | ASSERT(c, 0); |
| 685 | return c->sessionnum; |
| 686 | } |
| 687 | |
| 688 | extern unsigned int conn_get_charinfo_ladder(t_connection const * c) |
| 689 | { |
| 690 | ASSERT(c, 0); |
| 691 | return d2charinfo_get_ladder(c->charinfo); |
| 692 | } |
| 693 | |
| 694 | extern unsigned int conn_get_charinfo_expansion(t_connection const * c) |
| 695 | { |
| 696 | ASSERT(c, 0); |
| 697 | return d2charinfo_get_expansion(c->charinfo); |
| 698 | } |
| 699 | |
| 700 | extern unsigned int conn_get_charinfo_hardcore(t_connection const * c) |
| 701 | { |
| 702 | ASSERT(c, 0); |
| 703 | return d2charinfo_get_hardcore(c->charinfo); |
| 704 | } |
| 705 | |
| 706 | extern unsigned int conn_get_charinfo_dead(t_connection const * c) |
| 707 | { |
| 708 | ASSERT(c, 0); |
| 709 | return d2charinfo_get_dead(c->charinfo); |
| 710 | } |
| 711 | |
| 712 | extern unsigned int conn_get_charinfo_difficulty(t_connection const * c) |
| 713 | { |
| 714 | ASSERT(c, 0); |
| 715 | return d2charinfo_get_difficulty(c->charinfo); |
| 716 | } |
| 717 | |
| 718 | extern unsigned int conn_get_charinfo_level(t_connection const * c) |
| 719 | { |
| 720 | ASSERT(c, 0); |
| 721 | return d2charinfo_get_level(c->charinfo); |
| 722 | } |
| 723 | |
| 724 | extern unsigned int conn_get_charinfo_class(t_connection const * c) |
| 725 | { |
| 726 | ASSERT(c, 0); |
| 727 | return d2charinfo_get_class(c->charinfo); |
| 728 | } |
| 729 | |
| 730 | extern int conn_set_charinfo(t_connection * c, t_d2charinfo_summary const * charinfo) |
| 731 | { |
| 732 | ASSERT(c, -1); |
| 733 | if (!charinfo) { |
| 734 | if (c->charinfo) xfree((void *)c->charinfo); |
| 735 | c->charinfo = NULL; |
| 736 | return 0; |
| 737 | } |
| 738 | if (c->charinfo) xfree((void *)c->charinfo); |
| 739 | c->charinfo = (t_d2charinfo_summary*)xmalloc(sizeof(t_d2charinfo_summary)); |
| 740 | std::memcpy((void*)c->charinfo, charinfo, sizeof(t_d2charinfo_summary)); |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | extern int conn_set_d2gs_id(t_connection * c, unsigned int d2gs_id) |
| 745 | { |
| 746 | ASSERT(c, -1) |
| 747 | c->d2gs_id = d2gs_id; |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | extern unsigned int conn_get_d2gs_id(t_connection const * c) |
| 752 | { |
| 753 | ASSERT(c, 0) |
| 754 | return c->d2gs_id; |
| 755 | } |
| 756 | |
| 757 | extern unsigned int d2cs_conn_get_addr(t_connection const * c) |
| 758 | { |
| 759 | ASSERT(c, 0); |
| 760 | return c->addr; |
| 761 | } |
| 762 | |
| 763 | extern unsigned short d2cs_conn_get_port(t_connection const * c) |
| 764 | { |
| 765 | ASSERT(c, 0); |
| 766 | return c->port; |
| 767 | } |
| 768 | |
| 769 | extern t_gq * conn_get_gamequeue(t_connection const * c) |
| 770 | { |
| 771 | ASSERT(c, NULL); |
| 772 | return c->gamequeue; |
| 773 | } |
| 774 | |
| 775 | extern int conn_set_gamequeue(t_connection * c, t_gq * gq) |
| 776 | { |
| 777 | ASSERT(c, -1); |
| 778 | c->gamequeue = gq; |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | extern int conn_set_bnetd_sessionnum(t_connection * c, unsigned int sessionnum) |
| 783 | { |
| 784 | ASSERT(c, -1); |
| 785 | c->bnetd_sessionnum = sessionnum; |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | extern unsigned int conn_get_bnetd_sessionnum(t_connection const * c) |
| 790 | { |
| 791 | ASSERT(c, 0); |
| 792 | return c->bnetd_sessionnum; |
| 793 | } |
| 794 | |
| 795 | extern int conn_add_fd(t_connection * c, t_fdwatch_type rw, fdwatch_handler handler) |
| 796 | { |
| 797 | assert(c); |
| 798 | c->fdw_idx = fdwatch_add_fd(c->sock, rw, handler, c); |
| 799 | return c->fdw_idx; |
| 800 | } |
| 801 | |
| 802 | } |
| 803 | |
| 804 | } |