| 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_bnetd.h" |
| 21 | |
| 22 | #include "common/init_protocol.h" |
| 23 | #include "common/eventlog.h" |
| 24 | /* |
| 25 | #include "common/d2cs_bnetd_protocol.h" |
| 26 | #include "common/d2cs_protocol.h" |
| 27 | #include "common/packet.h" |
| 28 | #include "connection.h" |
| 29 | */ |
| 30 | #include "version.h" |
| 31 | #include "prefs.h" |
| 32 | #include "serverqueue.h" |
| 33 | #include "game.h" |
| 34 | #include "common/setup_after.h" |
| 35 | |
| 36 | namespace pvpgn |
| 37 | { |
| 38 | |
| 39 | namespace d2cs |
| 40 | { |
| 41 | |
| 42 | DECLARE_PACKET_HANDLER(on_bnetd_accountloginreply) |
| 43 | DECLARE_PACKET_HANDLER(on_bnetd_charloginreply) |
| 44 | DECLARE_PACKET_HANDLER(on_bnetd_authreq) |
| 45 | DECLARE_PACKET_HANDLER(on_bnetd_authreply) |
| 46 | DECLARE_PACKET_HANDLER(on_bnetd_gameinforeq) |
| 47 | |
| 48 | static t_packet_handle_table bnetd_packet_handle_table[] = { |
| 49 | /* 0x00 */{ 0, conn_state_none, NULL }, |
| 50 | /* 0x01 */{ sizeof(t_bnetd_d2cs_authreq), conn_state_connected, on_bnetd_authreq }, |
| 51 | /* 0x02 */{ sizeof(t_bnetd_d2cs_authreply), conn_state_connected, on_bnetd_authreply }, |
| 52 | /* 0x03 */{ 0, conn_state_none, NULL }, |
| 53 | /* 0x04 */{ 0, conn_state_none, NULL }, |
| 54 | /* 0x05 */{ 0, conn_state_none, NULL }, |
| 55 | /* 0x06 */{ 0, conn_state_none, NULL }, |
| 56 | /* 0x07 */{ 0, conn_state_none, NULL }, |
| 57 | /* 0x08 */{ 0, conn_state_none, NULL }, |
| 58 | /* 0x09 */{ 0, conn_state_none, NULL }, |
| 59 | /* 0x0a */{ 0, conn_state_none, NULL }, |
| 60 | /* 0x0b */{ 0, conn_state_none, NULL }, |
| 61 | /* 0x0c */{ 0, conn_state_none, NULL }, |
| 62 | /* 0x0d */{ 0, conn_state_none, NULL }, |
| 63 | /* 0x03 */{ 0, conn_state_none, NULL }, |
| 64 | /* 0x0f */{ 0, conn_state_none, NULL }, |
| 65 | /* 0x10 */{ sizeof(t_bnetd_d2cs_accountloginreply), conn_state_authed, on_bnetd_accountloginreply }, |
| 66 | /* 0x11 */{ sizeof(t_bnetd_d2cs_charloginreply), conn_state_authed, on_bnetd_charloginreply }, |
| 67 | /* 0x12 */{ sizeof(t_bnetd_d2cs_gameinforeq), conn_state_authed, on_bnetd_gameinforeq } |
| 68 | }; |
| 69 | |
| 70 | extern int handle_bnetd_packet(t_connection * c, t_packet * packet) |
| 71 | { |
| 72 | conn_process_packet(c, packet, bnetd_packet_handle_table, NELEMS(bnetd_packet_handle_table)); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | extern int handle_bnetd_init(t_connection * c) |
| 77 | { |
| 78 | t_packet * packet; |
| 79 | |
| 80 | packet = packet_create(packet_class_init); |
| 81 | packet_set_size(packet, sizeof(t_client_initconn)); |
| 82 | bn_byte_set(&packet->u.client_initconn.cclass, CLIENT_INITCONN_CLASS_D2CS_BNETD); |
| 83 | conn_push_outqueue(c, packet); |
| 84 | packet_del_ref(packet); |
| 85 | d2cs_conn_set_state(c, conn_state_connected); |
| 86 | eventlog(eventlog_level_info, __FUNCTION__, "sent init class packet to bnetd"); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int on_bnetd_authreq(t_connection * c, t_packet * packet) |
| 91 | { |
| 92 | t_packet * rpacket; |
| 93 | unsigned int sessionnum; |
| 94 | |
| 95 | sessionnum = bn_int_get(packet->u.bnetd_d2cs_authreq.sessionnum); |
| 96 | eventlog(eventlog_level_info, __FUNCTION__, "received bnetd sessionnum {}", sessionnum); |
| 97 | if ((rpacket = packet_create(packet_class_d2cs_bnetd))) { |
| 98 | packet_set_size(rpacket, sizeof(t_d2cs_bnetd_authreply)); |
| 99 | packet_set_type(rpacket, D2CS_BNETD_AUTHREPLY); |
| 100 | bn_int_set(&rpacket->u.d2cs_bnetd_authreply.h.seqno, 1); |
| 101 | bn_int_set(&rpacket->u.d2cs_bnetd_authreply.version, D2CS_VERSION_NUMBER); |
| 102 | packet_append_string(rpacket, prefs_get_realmname()); |
| 103 | conn_push_outqueue(c, rpacket); |
| 104 | packet_del_ref(rpacket); |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int on_bnetd_authreply(t_connection * c, t_packet * packet) |
| 110 | { |
| 111 | unsigned int reply; |
| 112 | |
| 113 | reply = bn_int_get(packet->u.bnetd_d2cs_authreply.reply); |
| 114 | if (reply == BNETD_D2CS_AUTHREPLY_SUCCEED) { |
| 115 | eventlog(eventlog_level_info, __FUNCTION__, "authed by bnetd"); |
| 116 | d2cs_conn_set_state(c, conn_state_authed); |
| 117 | } |
| 118 | else { |
| 119 | eventlog(eventlog_level_error, __FUNCTION__, "failed to auth by bnetd (error={})", reply); |
| 120 | d2cs_conn_set_state(c, conn_state_destroy); |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int on_bnetd_accountloginreply(t_connection * c, t_packet * packet) |
| 126 | { |
| 127 | unsigned int seqno; |
| 128 | t_sq * sq; |
| 129 | t_packet * opacket, *rpacket; |
| 130 | t_connection * client; |
| 131 | int result, reply; |
| 132 | char const * account; |
| 133 | t_elem * elem; |
| 134 | |
| 135 | if (!packet || !c) |
| 136 | return -1; |
| 137 | |
| 138 | seqno = bn_int_get(packet->u.d2cs_bnetd.h.seqno); |
| 139 | if (!(sq = sqlist_find_sq(seqno))) { |
| 140 | eventlog(eventlog_level_error, __FUNCTION__, "seqno {} not found", seqno); |
| 141 | return -1; |
| 142 | } |
| 143 | if (!(client = d2cs_connlist_find_connection_by_sessionnum(sq_get_clientid(sq)))) { |
| 144 | eventlog(eventlog_level_error, __FUNCTION__, "client {} not found", sq_get_clientid(sq)); |
| 145 | sq_destroy(sq, &elem); |
| 146 | return -1; |
| 147 | } |
| 148 | if (!(opacket = sq_get_packet(sq))) { |
| 149 | eventlog(eventlog_level_error, __FUNCTION__, "previous packet missing (seqno: {})", seqno); |
| 150 | sq_destroy(sq, &elem); |
| 151 | return -1; |
| 152 | } |
| 153 | result = bn_int_get(packet->u.bnetd_d2cs_accountloginreply.reply); |
| 154 | if (result == BNETD_D2CS_CHARLOGINREPLY_SUCCEED) { |
| 155 | reply = D2CS_CLIENT_LOGINREPLY_SUCCEED; |
| 156 | account = packet_get_str_const(opacket, sizeof(t_client_d2cs_loginreq), MAX_CHARNAME_LEN); |
| 157 | d2cs_conn_set_account(client, account); |
| 158 | d2cs_conn_set_state(client, conn_state_authed); |
| 159 | eventlog(eventlog_level_info, __FUNCTION__, "account {} authed", account); |
| 160 | } |
| 161 | else { |
| 162 | eventlog(eventlog_level_warn, __FUNCTION__, "client {} login request was rejected by bnetd", sq_get_clientid(sq)); |
| 163 | reply = D2CS_CLIENT_LOGINREPLY_BADPASS; |
| 164 | } |
| 165 | if ((rpacket = packet_create(packet_class_d2cs))) { |
| 166 | packet_set_size(rpacket, sizeof(t_d2cs_client_loginreply)); |
| 167 | packet_set_type(rpacket, D2CS_CLIENT_LOGINREPLY); |
| 168 | bn_int_set(&rpacket->u.d2cs_client_loginreply.reply, reply); |
| 169 | conn_push_outqueue(client, rpacket); |
| 170 | packet_del_ref(rpacket); |
| 171 | } |
| 172 | sq_destroy(sq, &elem); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int on_bnetd_charloginreply(t_connection * c, t_packet * packet) |
| 177 | { |
| 178 | unsigned int seqno; |
| 179 | t_sq * sq; |
| 180 | t_connection * client; |
| 181 | t_packet * opacket, *rpacket; |
| 182 | int result, reply, type; |
| 183 | char const * charname; |
| 184 | t_elem * elem; |
| 185 | |
| 186 | if (!packet || !c) |
| 187 | return -1; |
| 188 | |
| 189 | seqno = bn_int_get(packet->u.d2cs_bnetd.h.seqno); |
| 190 | if (!(sq = sqlist_find_sq(seqno))) { |
| 191 | eventlog(eventlog_level_error, __FUNCTION__, "seqno {} not found", seqno); |
| 192 | return -1; |
| 193 | } |
| 194 | if (!(client = d2cs_connlist_find_connection_by_sessionnum(sq_get_clientid(sq)))) { |
| 195 | eventlog(eventlog_level_error, __FUNCTION__, "client {} not found", sq_get_clientid(sq)); |
| 196 | sq_destroy(sq, &elem); |
| 197 | return -1; |
| 198 | } |
| 199 | if (!(opacket = sq_get_packet(sq))) { |
| 200 | eventlog(eventlog_level_error, __FUNCTION__, "previous packet missing (seqno: {})", seqno); |
| 201 | sq_destroy(sq, &elem); |
| 202 | return -1; |
| 203 | } |
| 204 | type = packet_get_type(opacket); |
| 205 | result = bn_int_get(packet->u.bnetd_d2cs_charloginreply.reply); |
| 206 | if (type == CLIENT_D2CS_CREATECHARREQ) { |
| 207 | charname = packet_get_str_const(opacket, sizeof(t_client_d2cs_createcharreq), MAX_CHARNAME_LEN); |
| 208 | if (result == BNETD_D2CS_CHARLOGINREPLY_SUCCEED) { |
| 209 | if (conn_check_multilogin(client, charname) < 0) { |
| 210 | eventlog(eventlog_level_error, __FUNCTION__, "character {} is already logged in", charname); |
| 211 | reply = D2CS_CLIENT_CHARLOGINREPLY_FAILED; |
| 212 | } |
| 213 | else { |
| 214 | reply = D2CS_CLIENT_CREATECHARREPLY_SUCCEED; |
| 215 | eventlog(eventlog_level_info, __FUNCTION__, "character {} authed", charname); |
| 216 | d2cs_conn_set_charname(client, charname); |
| 217 | d2cs_conn_set_state(client, conn_state_char_authed); |
| 218 | } |
| 219 | } |
| 220 | else { |
| 221 | reply = D2CS_CLIENT_CREATECHARREPLY_FAILED; |
| 222 | eventlog(eventlog_level_error, __FUNCTION__, "failed to auth character {}", charname); |
| 223 | } |
| 224 | if ((rpacket = packet_create(packet_class_d2cs))) { |
| 225 | packet_set_size(rpacket, sizeof(t_d2cs_client_createcharreply)); |
| 226 | packet_set_type(rpacket, D2CS_CLIENT_CREATECHARREPLY); |
| 227 | bn_int_set(&rpacket->u.d2cs_client_createcharreply.reply, reply); |
| 228 | conn_push_outqueue(client, rpacket); |
| 229 | packet_del_ref(rpacket); |
| 230 | } |
| 231 | } |
| 232 | else if (type == CLIENT_D2CS_CHARLOGINREQ) { |
| 233 | charname = packet_get_str_const(opacket, sizeof(t_client_d2cs_charloginreq), MAX_CHARNAME_LEN); |
| 234 | if (result == BNETD_D2CS_CHARLOGINREPLY_SUCCEED) { |
| 235 | if (conn_check_multilogin(client, charname) < 0) { |
| 236 | eventlog(eventlog_level_error, __FUNCTION__, "character {} is already logged in", charname); |
| 237 | reply = D2CS_CLIENT_CHARLOGINREPLY_FAILED; |
| 238 | } |
| 239 | else { |
| 240 | reply = D2CS_CLIENT_CHARLOGINREPLY_SUCCEED; |
| 241 | eventlog(eventlog_level_info, __FUNCTION__, "character {} authed", charname); |
| 242 | d2cs_conn_set_charname(client, charname); |
| 243 | d2cs_conn_set_state(client, conn_state_char_authed); |
| 244 | } |
| 245 | } |
| 246 | else { |
| 247 | reply = D2CS_CLIENT_CHARLOGINREPLY_FAILED; |
| 248 | eventlog(eventlog_level_error, __FUNCTION__, "failed to auth character {}", charname); |
| 249 | } |
| 250 | if ((rpacket = packet_create(packet_class_d2cs))) { |
| 251 | packet_set_size(rpacket, sizeof(t_d2cs_client_charloginreply)); |
| 252 | packet_set_type(rpacket, D2CS_CLIENT_CHARLOGINREPLY); |
| 253 | bn_int_set(&rpacket->u.d2cs_client_charloginreply.reply, reply); |
| 254 | conn_push_outqueue(client, rpacket); |
| 255 | packet_del_ref(rpacket); |
| 256 | } |
| 257 | } |
| 258 | else { |
| 259 | eventlog(eventlog_level_error, __FUNCTION__, "got bad packet type {}", type); |
| 260 | sq_destroy(sq, &elem); |
| 261 | return -1; |
| 262 | } |
| 263 | sq_destroy(sq, &elem); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | int on_bnetd_gameinforeq(t_connection * c, t_packet * packet) |
| 268 | { |
| 269 | t_packet * rpacket; |
| 270 | t_game * game; |
| 271 | |
| 272 | char const * gamename; |
| 273 | |
| 274 | if (!(c)) { |
| 275 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection"); |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | if (!(gamename = packet_get_str_const(packet, sizeof(t_bnetd_d2cs_gameinforeq), MAX_GAMENAME_LEN))) |
| 280 | { |
| 281 | eventlog(eventlog_level_error, __FUNCTION__, "missing or too long gamename"); |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | if (!(game = d2cs_gamelist_find_game(gamename))) |
| 286 | { |
| 287 | eventlog(eventlog_level_error, __FUNCTION__, "request for unknown game \"{}\"", gamename); |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | if ((rpacket = packet_create(packet_class_d2cs_bnetd))) { |
| 292 | packet_set_size(rpacket, sizeof(t_d2cs_bnetd_gameinforeply)); |
| 293 | packet_set_type(rpacket, D2CS_BNETD_GAMEINFOREPLY); |
| 294 | bn_int_set(&rpacket->u.d2cs_bnetd_gameinforeply.h.seqno, 0); |
| 295 | packet_append_string(rpacket, gamename); |
| 296 | |
| 297 | bn_byte_set(&rpacket->u.d2cs_bnetd_gameinforeply.difficulty, game_get_gameflag_difficulty(game)); |
| 298 | |
| 299 | conn_push_outqueue(c, rpacket); |
| 300 | packet_del_ref(rpacket); |
| 301 | } |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | } |
| 306 | |
| 307 | } |