summaryrefslogtreecommitdiff
path: root/src/login/lclif.c
blob: 97871922de8cb3166c510ff9c24fef6081b5753c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/**
 * This file is part of Hercules.
 * http://herc.ws - http://github.com/HerculesWS/Hercules
 *
 * Copyright (C) 2016  Hercules Dev Team
 *
 * Hercules is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#define HERCULES_CORE

#include "lclif.p.h"

#include "login/ipban.h"
#include "login/login.h"
#include "login/loginlog.h"
#include "login/packets_ac_struct.h"
#include "login/packets_ca_struct.h"
#include "common/HPM.h"
#include "common/cbasetypes.h"
#include "common/db.h"
#include "common/md5calc.h"
#include "common/memmgr.h"
#include "common/mmo.h"
#include "common/nullpo.h"
#include "common/random.h"
#include "common/showmsg.h"
#include "common/socket.h"
#include "common/strlib.h"
#include "common/utils.h"

/** @file
 * Implementation of the login client interface.
 */

static struct lclif_interface lclif_s;
static struct lclif_interface_private lclif_p;
static struct lclif_interface_dbs lclif_dbs;
struct lclif_interface *lclif;

/// @copydoc lclif_interface::connection_error()
static void lclif_connection_error(int fd, uint8 error)
{
	struct PACKET_SC_NOTIFY_BAN *packet = NULL;
	WFIFOHEAD(fd, sizeof(*packet));
	packet = WP2PTR(fd);
	packet->packet_id = HEADER_SC_NOTIFY_BAN;
	packet->error_code = error;
	WFIFOSET(fd, sizeof(*packet));
}

/// @copydoc lclif_interface_private::parse_CA_CONNECT_INFO_CHANGED()
static enum parsefunc_rcode lclif_parse_CA_CONNECT_INFO_CHANGED(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_CONNECT_INFO_CHANGED(int fd, struct login_session_data *sd)
{
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_EXE_HASHCHECK()
static enum parsefunc_rcode lclif_parse_CA_EXE_HASHCHECK(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_EXE_HASHCHECK(int fd, struct login_session_data *sd)
{
	const struct PACKET_CA_EXE_HASHCHECK *packet = RP2PTR(fd);
	sd->has_client_hash = 1;
	memcpy(sd->client_hash, packet->hash_value, 16);
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_LOGIN()
static enum parsefunc_rcode lclif_parse_CA_LOGIN(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_LOGIN(int fd, struct login_session_data *sd)
{
	const struct PACKET_CA_LOGIN *packet = RP2PTR(fd);

	sd->version = packet->version;
	sd->clienttype = packet->clienttype;
	safestrncpy(sd->userid, packet->id, NAME_LENGTH);
	safestrncpy(sd->passwd, packet->password, PASSWD_LEN);

	if (login->config->use_md5_passwds)
		md5->string(sd->passwd, sd->passwd);
	sd->passwdenc = PWENC_NONE;

	login->client_login(fd, sd);
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_LOGIN2()
static enum parsefunc_rcode lclif_parse_CA_LOGIN2(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_LOGIN2(int fd, struct login_session_data *sd)
{
	const struct PACKET_CA_LOGIN2 *packet = RP2PTR(fd);

	sd->version = packet->version;
	sd->clienttype = packet->clienttype;
	safestrncpy(sd->userid, packet->id, NAME_LENGTH);
	bin2hex(sd->passwd, packet->password_md5, 16);
	sd->passwdenc = PASSWORDENC;

	login->client_login(fd, sd);
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_LOGIN3()
static enum parsefunc_rcode lclif_parse_CA_LOGIN3(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_LOGIN3(int fd, struct login_session_data *sd)
{
	const struct PACKET_CA_LOGIN3 *packet = RP2PTR(fd);

	sd->version = packet->version;
	sd->clienttype = packet->clienttype;
	/* unused */
	/* sd->clientinfo = packet->clientinfo; */
	safestrncpy(sd->userid, packet->id, NAME_LENGTH);
	bin2hex(sd->passwd, packet->password_md5, 16);
	sd->passwdenc = PASSWORDENC;

	login->client_login(fd, sd);
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_LOGIN4()
static enum parsefunc_rcode lclif_parse_CA_LOGIN4(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_LOGIN4(int fd, struct login_session_data *sd)
{
	const struct PACKET_CA_LOGIN4 *packet = RP2PTR(fd);

	sd->version = packet->version;
	sd->clienttype = packet->clienttype;
	/* unused */
	/* safestrncpy(sd->mac_address, packet->mac_address, sizeof(sd->mac_address)); */
	safestrncpy(sd->userid, packet->id, NAME_LENGTH);
	bin2hex(sd->passwd, packet->password_md5, 16);
	sd->passwdenc = PASSWORDENC;

	login->client_login(fd, sd);
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_LOGIN_PCBANG()
static enum parsefunc_rcode lclif_parse_CA_LOGIN_PCBANG(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_LOGIN_PCBANG(int fd, struct login_session_data *sd)
{
	const struct PACKET_CA_LOGIN_PCBANG *packet = RP2PTR(fd);

	sd->version = packet->version;
	sd->clienttype = packet->clienttype;
	/* unused */
	/* safestrncpy(sd->ip, packet->ip, sizeof(sd->ip)); */
	/* safestrncpy(sd->mac_address, packet->mac_address, sizeof(sd->mac_address)); */
	safestrncpy(sd->userid, packet->id, NAME_LENGTH);
	safestrncpy(sd->passwd, packet->password, PASSWD_LEN);

	if (login->config->use_md5_passwds)
		md5->string(sd->passwd, sd->passwd);
	sd->passwdenc = PWENC_NONE;

	login->client_login(fd, sd);
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_LOGIN_HAN()
static enum parsefunc_rcode lclif_parse_CA_LOGIN_HAN(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_LOGIN_HAN(int fd, struct login_session_data *sd)
{
	const struct PACKET_CA_LOGIN_HAN *packet = RP2PTR(fd);

	sd->version = packet->version;
	sd->clienttype = packet->clienttype;
	/* unused */
	/* safestrncpy(sd->ip, packet->ip, sizeof(sd->ip)); */
	/* safestrncpy(sd->mac_address, packet->mac_address, sizeof(sd->mac_address)); */
	/* sd->ishan = packet->is_han_game_user; */
	safestrncpy(sd->userid, packet->id, NAME_LENGTH);
	safestrncpy(sd->passwd, packet->password, PASSWD_LEN);

	if (login->config->use_md5_passwds)
		md5->string(sd->passwd, sd->passwd);
	sd->passwdenc = PWENC_NONE;

	login->client_login(fd, sd);
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_SSO_LOGIN_REQ()
static enum parsefunc_rcode lclif_parse_CA_SSO_LOGIN_REQ(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_SSO_LOGIN_REQ(int fd, struct login_session_data *sd)
{
	const struct PACKET_CA_SSO_LOGIN_REQ *packet = RP2PTR(fd);
	int tokenlen = (int)RFIFOREST(fd) - (int)sizeof(*packet);

	if (tokenlen > PASSWD_LEN || tokenlen < 1) {
		ShowError("PACKET_CA_SSO_LOGIN_REQ: Token length is not between allowed password length, kicking player ('%s')", packet->id);
		sockt->eof(fd);
		return PACKET_VALID;
	}

	sd->clienttype = packet->clienttype;
	sd->version = packet->version;
	safestrncpy(sd->userid, packet->id, NAME_LENGTH);
	safestrncpy(sd->passwd, packet->t1, min(tokenlen + 1, PASSWD_LEN)); // Variable-length field, don't copy more than necessary

	if (login->config->use_md5_passwds)
		md5->string(sd->passwd, sd->passwd);
	sd->passwdenc = PWENC_NONE;

	login->client_login(fd, sd);
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_LOGIN_OTP()
static enum parsefunc_rcode lclif_parse_CA_LOGIN_OTP(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_LOGIN_OTP(int fd, struct login_session_data *sd)
{
	//const struct PACKET_CA_LOGIN_OTP *packet = RP2PTR(fd);
	login->client_login_otp(fd, sd);
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_ACK_MOBILE_OTP()
static enum parsefunc_rcode lclif_parse_CA_ACK_MOBILE_OTP(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_ACK_MOBILE_OTP(int fd, struct login_session_data *sd)
{
	// TODO: parsing packet data
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_OTP_CODE()
static enum parsefunc_rcode lclif_parse_CA_OTP_CODE(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_OTP_CODE(int fd, struct login_session_data *sd)
{
	// TODO: parsing packet data
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_REQ_HASH()
static enum parsefunc_rcode lclif_parse_CA_REQ_HASH(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_REQ_HASH(int fd, struct login_session_data *sd)
{
	memset(sd->md5key, '\0', sizeof(sd->md5key));
	sd->md5keylen = (uint16)(12 + rnd() % 4);
	md5->salt(sd->md5keylen, sd->md5key);

	lclif->coding_key(fd, sd);
	return PACKET_VALID;
}

/// @copydoc lclif_interface_private::parse_CA_CHARSERVERCONNECT()
static enum parsefunc_rcode lclif_parse_CA_CHARSERVERCONNECT(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static enum parsefunc_rcode lclif_parse_CA_CHARSERVERCONNECT(int fd, struct login_session_data *sd)
{
	char ip[16];
	uint32 ipl = sockt->session[fd]->client_addr;
	sockt->ip2str(ipl, ip);

	login->parse_request_connection(fd, sd, ip, ipl);

	return PACKET_STOPPARSE;
}

/// @copydoc lclif_interface::server_list()
static bool lclif_send_server_list(struct login_session_data *sd)
{
	int server_num = 0, i, n, length;
	uint32 ip;
	struct PACKET_AC_ACCEPT_LOGIN *packet = NULL;

	for (i = 0; i < ARRAYLENGTH(login->dbs->server); ++i) {
		if (sockt->session_is_active(login->dbs->server[i].fd))
			server_num++;
	}
	if (server_num == 0)
		return false;

	length = sizeof(*packet) + sizeof(packet->server_list[0]) * server_num;
	ip = sockt->session[sd->fd]->client_addr;

	// Allocate the packet
	WFIFOHEAD(sd->fd, length);
	packet = WP2PTR(sd->fd);

#if PACKETVER < 20170315
	packet->packet_id = HEADER_AC_ACCEPT_LOGIN;
#else
	packet->packet_id = HEADER_AC_ACCEPT_LOGIN2;
#endif
	packet->packet_len = length;
	packet->auth_code = sd->login_id1;
	packet->aid = sd->account_id;
	packet->user_level = sd->login_id2;
	packet->last_login_ip = 0; // Not used anymore
	memset(packet->last_login_time, '\0', sizeof(packet->last_login_time)); // Not used anymore
	packet->sex = sex_str2num(sd->sex);
	for (i = 0, n = 0; i < ARRAYLENGTH(login->dbs->server); ++i) {
		uint32 subnet_char_ip;

		if (!sockt->session_is_valid(login->dbs->server[i].fd))
			continue;

		subnet_char_ip = login->lan_subnet_check(ip);
		packet->server_list[n].ip = htonl((subnet_char_ip) ? subnet_char_ip : login->dbs->server[i].ip);
		packet->server_list[n].port = sockt->ntows(htons(login->dbs->server[i].port)); // [!] LE byte order here [!]
		safestrncpy(packet->server_list[n].name, login->dbs->server[i].name, 20);
		packet->server_list[n].usercount = login->convert_users_to_colors(login->dbs->server[i].users);

		if (login->dbs->server[i].type == CST_PAYING && sd->expiration_time > time(NULL))
			packet->server_list[n].property = CST_NORMAL;
		else
			packet->server_list[n].property = login->dbs->server[i].type;

		packet->server_list[n].state = login->dbs->server[i].new_;
		++n;
	}
	WFIFOSET(sd->fd, length);

	return true;
}

/// @copydoc lclif_interface::auth_failed()
static void lclif_send_auth_failed(int fd, time_t ban, uint32 error)
{
#if PACKETVER >= 20180627
	struct PACKET_AC_REFUSE_LOGIN_R2 *packet = NULL;
	int packet_id = HEADER_AC_REFUSE_LOGIN_R3;
#elif PACKETVER >= 20101123
	struct PACKET_AC_REFUSE_LOGIN_R2 *packet = NULL;
	int packet_id = HEADER_AC_REFUSE_LOGIN_R2;
#else
	struct PACKET_AC_REFUSE_LOGIN *packet = NULL;
	int packet_id = HEADER_AC_REFUSE_LOGIN;
#endif
	WFIFOHEAD(fd, sizeof(*packet));
	packet = WP2PTR(fd);
	packet->packet_id = packet_id;
	packet->error_code = error;
	if (error == 6)
		timestamp2string(packet->block_date, sizeof(packet->block_date), ban, login->config->date_format);
	else
		memset(packet->block_date, '\0', sizeof(packet->block_date));
	WFIFOSET(fd, sizeof(*packet));
}

/// @copydoc lclif_interface::login_error()
static void lclif_send_login_error(int fd, uint8 error)
{
	struct PACKET_AC_REFUSE_LOGIN *packet = NULL;
	WFIFOHEAD(fd, sizeof(*packet));
	packet = WP2PTR(fd);
	packet->packet_id = HEADER_AC_REFUSE_LOGIN;
	packet->error_code = error;
	memset(packet->block_date, '\0', sizeof(packet->block_date));
	WFIFOSET(fd, sizeof(*packet));
}

/// @copydoc lclif_interface::coding_key()
static void lclif_send_coding_key(int fd, struct login_session_data *sd) __attribute__((nonnull (2)));
static void lclif_send_coding_key(int fd, struct login_session_data *sd)
{
	struct PACKET_AC_ACK_HASH *packet = NULL;
	int16 size = sizeof(*packet) + sd->md5keylen;

	WFIFOHEAD(fd, size);
	packet = WP2PTR(fd);
	packet->packet_id = HEADER_AC_ACK_HASH;
	packet->packet_len = size;
	memcpy(packet->secret, sd->md5key, sd->md5keylen);
	WFIFOSET(fd, size);
}

/// @copydoc lclif_interface::parse()
static int lclif_parse(int fd)
{
	struct login_session_data *sd = NULL;
	int i;
	char ip[16];
	uint32 ipl = sockt->session[fd]->client_addr;
	sockt->ip2str(ipl, ip);

	if (sockt->session[fd]->flag.eof) {
		ShowInfo("Closed connection from '"CL_WHITE"%s"CL_RESET"'.\n", ip);
		sockt->close(fd);
		return 0;
	}

	if ((sd = sockt->session[fd]->session_data) == NULL) {
		// Perform ip-ban check
		if (login->config->ipban && !sockt->trusted_ip_check(ipl) && ipban->check(ipl)) {
			ShowStatus("Connection refused: IP isn't authorized (deny/allow, ip: %s).\n", ip);
			loginlog->log(ipl, "unknown", -3, "ip banned");
			lclif->login_error(fd, 3); // 3 = Rejected from Server
			sockt->eof(fd);
			return 0;
		}

		// create a session for this new connection
		CREATE(sockt->session[fd]->session_data, struct login_session_data, 1);
		sd = sockt->session[fd]->session_data;
		sd->fd = fd;
	}

	for (i = 0; i < MAX_PROCESSED_PACKETS; ++i) {
		enum parsefunc_rcode result;
		int16 packet_id = RFIFOW(fd, 0);
		int packet_len = (int)RFIFOREST(fd);

		if (packet_len < 2)
			return 0;

		result = lclif->p->parse_sub(fd, sd);

		switch (result) {
		case PACKET_SKIP:
			continue;
		case PACKET_INCOMPLETE:
		case PACKET_STOPPARSE:
			return 0;
		case PACKET_UNKNOWN:
			ShowWarning("lclif_parse: Received unsupported packet (packet 0x%04x, %d bytes received), disconnecting session #%d.\n", (unsigned int)packet_id, packet_len, fd);
#ifdef DUMP_INVALID_PACKET
			ShowDump(RFIFOP(fd, 0), RFIFOREST(fd));
#endif
			sockt->eof(fd);
			return 0;
		case PACKET_INVALIDLENGTH:
			ShowWarning("lclif_parse: Received packet 0x%04x specifies invalid packet_len (%d), disconnecting session #%d.\n", (unsigned int)packet_id, packet_len, fd);
#ifdef DUMP_INVALID_PACKET
			ShowDump(RFIFOP(fd, 0), RFIFOREST(fd));
#endif
			sockt->eof(fd);
			return 0;
		}
	}
	return 0;
}

/// @copydoc lclif_interface_private::parse_sub()
static enum parsefunc_rcode lclif_parse_sub(int fd, struct login_session_data *sd)
{
	int packet_len = (int)RFIFOREST(fd);
	int16 packet_id = RFIFOW(fd, 0);
	const struct login_packet_db *lpd;

	if (VECTOR_LENGTH(HPM->packets[hpParse_Login]) > 0) {
		int result = HPM->parse_packets(fd, packet_id, hpParse_Login);
		if (result == 1)
			return PACKET_VALID;
		if (result == 2)
			return PACKET_INCOMPLETE; // Packet not completed yet
	}

	lpd = lclif->packet(packet_id);

	if (lpd == NULL)
		return PACKET_UNKNOWN;

	if (lpd->len == 0)
		return PACKET_UNKNOWN;

	if (lpd->len > 0 && lpd->pFunc == NULL)
		return PACKET_UNKNOWN; //This Packet is defined for length purpose ? should never be sent from client ?

	if (lpd->len == -1) {
		uint16 packet_var_len = 0; //Max Variable Packet length is signed int16 size

		if (packet_len < 4)
			return PACKET_INCOMPLETE; //Packet incomplete

		packet_var_len = RFIFOW(fd, 2);

		if (packet_var_len < 4 || packet_var_len > SINT16_MAX)
			return PACKET_INVALIDLENGTH; //Something is wrong, close connection.

		if (RFIFOREST(fd) < packet_var_len)
			return PACKET_INCOMPLETE; //Packet incomplete again.

		return lclif->parse_packet(lpd, fd, sd);
	} else if (lpd->len <= packet_len) {
		return lclif->parse_packet(lpd, fd, sd);
	}

	return PACKET_VALID;
}

/// @copydoc lclif_interface::packet()
static const struct login_packet_db *lclif_packet(int16 packet_id)
{
	if (packet_id == HEADER_CA_CHARSERVERCONNECT)
		return &lclif->p->dbs->packet_db[0];

	if (packet_id > MAX_PACKET_LOGIN_DB || packet_id < MIN_PACKET_DB)
		return NULL;

	return &lclif->p->dbs->packet_db[packet_id];
}

/// @copydoc lclif_interface::parse_packet()
static enum parsefunc_rcode lclif_parse_packet(const struct login_packet_db *lpd, int fd, struct login_session_data *sd)
{
	int result;
	result = (*lpd->pFunc)(fd, sd);
	RFIFOSKIP(fd, (lpd->len == -1) ? RFIFOW(fd, 2) : lpd->len);
	return result;
}

/// @copydoc lclif_interface_private::packetdb_loaddb()
static void packetdb_loaddb(void)
{
	int i;
	struct packet {
		int16 packet_id;
		int16 packet_len;
		LoginParseFunc **pFunc;
	} packet[] = {
#define packet_def(name) { HEADER_ ## name, sizeof(struct PACKET_ ## name), &lclif->p->parse_ ## name }
#define packet_def2(name, len) { HEADER_ ## name, (len), &lclif->p->parse_ ## name }
		packet_def(CA_CONNECT_INFO_CHANGED),
		packet_def(CA_EXE_HASHCHECK),
		packet_def(CA_LOGIN),
		packet_def(CA_LOGIN2),
		packet_def(CA_LOGIN3),
		packet_def(CA_LOGIN4),
		packet_def(CA_LOGIN_PCBANG),
		packet_def(CA_LOGIN_HAN),
		packet_def2(CA_SSO_LOGIN_REQ, -1),
		packet_def(CA_LOGIN_OTP),
#if PACKETVER_MAIN_NUM >= 20181114 || PACKETVER_RE_NUM >= 20181114
		packet_def(CA_ACK_MOBILE_OTP),
#endif
#if PACKETVER_MAIN_NUM >= 20181114 || PACKETVER_RE_NUM >= 20181114 || defined(PACKETVER_ZERO)
		packet_def(CA_OTP_CODE),
#endif
		packet_def(CA_REQ_HASH),
#undef packet_def
#undef packet_def2
	};
	int length = ARRAYLENGTH(packet);

	memset(lclif->p->dbs->packet_db, '\0', sizeof(lclif->p->dbs->packet_db));

	for (i = 0; i < length; ++i) {
		int16 packet_id = packet[i].packet_id;
		Assert_retb(packet_id >= MIN_PACKET_DB && packet_id <= MAX_PACKET_LOGIN_DB);
		lclif->p->dbs->packet_db[packet_id].len = packet[i].packet_len;
		lclif->p->dbs->packet_db[packet_id].pFunc = packet[i].pFunc;
	}

	//Explict case, we will save character login packet in position 0 which is unused and not valid by normal
	lclif->p->dbs->packet_db[0].len = sizeof(struct PACKET_CA_CHARSERVERCONNECT);
	lclif->p->dbs->packet_db[0].pFunc = &lclif->p->parse_CA_CHARSERVERCONNECT;
}

/// @copydoc lclif_interface::init()
static void lclif_init(void)
{
	lclif->p->packetdb_loaddb();
}

/// @copydoc lclif_interface::final()
static void lclif_final(void)
{
}

/// Interface base initialization.
void lclif_defaults(void)
{
	lclif = &lclif_s;
	lclif->p = &lclif_p;
	lclif->p->dbs = &lclif_dbs;

	lclif->init = lclif_init;
	lclif->final = lclif_final;

	lclif->connection_error = lclif_connection_error;
	lclif->server_list = lclif_send_server_list;
	lclif->auth_failed = lclif_send_auth_failed;
	lclif->login_error = lclif_send_login_error;
	lclif->coding_key = lclif_send_coding_key;

	lclif->packet = lclif_packet;
	lclif->parse_packet = lclif_parse_packet;
	lclif->parse = lclif_parse;

	lclif->p->packetdb_loaddb = packetdb_loaddb;
	lclif->p->parse_sub = lclif_parse_sub;

	lclif->p->parse_CA_CONNECT_INFO_CHANGED = lclif_parse_CA_CONNECT_INFO_CHANGED;
	lclif->p->parse_CA_EXE_HASHCHECK        = lclif_parse_CA_EXE_HASHCHECK;
	lclif->p->parse_CA_LOGIN                = lclif_parse_CA_LOGIN;
	lclif->p->parse_CA_LOGIN2               = lclif_parse_CA_LOGIN2;
	lclif->p->parse_CA_LOGIN3               = lclif_parse_CA_LOGIN3;
	lclif->p->parse_CA_LOGIN4               = lclif_parse_CA_LOGIN4;
	lclif->p->parse_CA_LOGIN_PCBANG         = lclif_parse_CA_LOGIN_PCBANG;
	lclif->p->parse_CA_LOGIN_HAN            = lclif_parse_CA_LOGIN_HAN;
	lclif->p->parse_CA_SSO_LOGIN_REQ        = lclif_parse_CA_SSO_LOGIN_REQ;
	lclif->p->parse_CA_LOGIN_OTP            = lclif_parse_CA_LOGIN_OTP;
	lclif->p->parse_CA_ACK_MOBILE_OTP       = lclif_parse_CA_ACK_MOBILE_OTP;
	lclif->p->parse_CA_OTP_CODE             = lclif_parse_CA_OTP_CODE;
	lclif->p->parse_CA_REQ_HASH             = lclif_parse_CA_REQ_HASH;
	lclif->p->parse_CA_CHARSERVERCONNECT    = lclif_parse_CA_CHARSERVERCONNECT;
}