· 7 years ago · Oct 16, 2018, 07:34 AM
1#include "../common/cbasetypes.h"
2#include "../common/malloc.h"
3#include "../common/socket.h"
4#include "../common/timer.h"
5#include "../common/nullpo.h"
6#include "../common/showmsg.h"
7#include "../common/strlib.h"
8#include "../common/ers.h"
9
10#include "map.h"
11#include "battle.h"
12#include "clif.h"
13#include "intif.h"
14#include "npc.h"
15#include "pc.h"
16#include "pet.h"
17#include "skill.h"
18#include "status.h"
19#include "homunculus.h"
20#include "instance.h"
21#include "mercenary.h"
22#include "chrif.h"
23#include "quest.h"
24#include "storage.h"
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/types.h>
30#include <time.h>
31
32static int check_connect_char_server(int tid, unsigned int tick, int id, intptr_t data);
33
34static struct eri *auth_db_ers; //For reutilizing player login structures.
35static DBMap* auth_db; // int id -> struct auth_node*
36
37static const int packet_len_table[0x3d] = { // U - used, F - free
38 60, 3,-1,27,10,-1, 6,-1, // 2af8-2aff: U->2af8, U->2af9, U->2afa, U->2afb, U->2afc, U->2afd, U->2afe, U->2aff
39 6,-1,18, 7,-1,35,30, 0, // 2b00-2b07: U->2b00, U->2b01, U->2b02, U->2b03, U->2b04, U->2b05, U->2b06, F->2b07
40 6,30, 0, 0,86, 7,44,34, // 2b08-2b0f: U->2b08, U->2b09, F->2b0a, F->2b0b, U->2b0c, U->2b0d, U->2b0e, U->2b0f
41 11,10,10, 0,11, 0,266,10, // 2b10-2b17: U->2b10, U->2b11, U->2b12, F->2b13, U->2b14, F->2b15, U->2b16, U->2b17
42 2,10, 2,-1,-1,-1, 2, 7, // 2b18-2b1f: U->2b18, U->2b19, U->2b1a, U->2b1b, U->2b1c, U->2b1d, U->2b1e, U->2b1f
43 -1,10, 8, 2, 2,14,19,19, // 2b20-2b27: U->2b20, U->2b21, U->2b22, U->2b23, U->2b24, U->2b25, U->2b26, U->2b27
44};
45
46//Used Packets:
47//2af8: Outgoing, chrif_connect -> 'connect to charserver / auth @ charserver'
48//2af9: Incoming, chrif_connectack -> 'answer of the 2af8 login(ok / fail)'
49//2afa: Outgoing, chrif_sendmap -> 'sending our maps'
50//2afb: Incoming, chrif_sendmapack -> 'Maps received successfully / or not ..'
51//2afc: Outgoing, chrif_scdata_request -> request sc_data for pc_authok'ed char. <- new command reuses previous one.
52//2afd: Incoming, chrif_authok -> 'client authentication ok'
53//2afe: Outgoing, send_usercount_tochar -> 'sends player count of this map server to charserver'
54//2aff: Outgoing, send_users_tochar -> 'sends all actual connected character ids to charserver'
55//2b00: Incoming, map_setusers -> 'set the actual usercount? PACKET.2B COUNT.L.. ?' (not sure)
56//2b01: Outgoing, chrif_save -> 'charsave of char XY account XY (complete struct)'
57//2b02: Outgoing, chrif_charselectreq -> 'player returns from ingame to charserver to select another char.., this packets includes sessid etc' ? (not 100% sure)
58//2b03: Incoming, clif_charselectok -> '' (i think its the packet after enterworld?) (not sure)
59//2b04: Incoming, chrif_recvmap -> 'getting maps from charserver of other mapserver's'
60//2b05: Outgoing, chrif_changemapserver -> 'Tell the charserver the mapchange / quest for ok...'
61//2b06: Incoming, chrif_changemapserverack -> 'awnser of 2b05, ok/fail, data: dunno^^'
62//2b07: FREE
63//2b08: Outgoing, chrif_searchcharid -> '...'
64//2b09: Incoming, map_addchariddb -> 'Adds a name to the nick db'
65//2b0a: FREE
66//2b0b: FREE
67//2b0c: Outgoing, chrif_changeemail -> 'change mail address ...'
68//2b0d: Incoming, chrif_changedsex -> 'Change sex of acc XY'
69//2b0e: Outgoing, chrif_char_ask_name -> 'Do some operations (change sex, ban / unban etc)'
70//2b0f: Incoming, chrif_char_ask_name_answer -> 'answer of the 2b0e'
71//2b10: Outgoing, chrif_updatefamelist -> 'Update the fame ranking lists and send them'
72//2b11: Outgoing, chrif_divorce -> 'tell the charserver to do divorce'
73//2b12: Incoming, chrif_divorceack -> 'divorce chars
74//2b13: FREE
75//2b14: Incoming, chrif_accountban -> 'not sure: kick the player with message XY'
76//2b15: FREE
77//2b16: Outgoing, chrif_ragsrvinfo -> 'sends base / job / drop rates ....'
78//2b17: Outgoing, chrif_char_offline -> 'tell the charserver that the char is now offline'
79//2b18: Outgoing, chrif_char_reset_offline -> 'set all players OFF!'
80//2b19: Outgoing, chrif_char_online -> 'tell the charserver that the char .. is online'
81
82
83int chrif_connected = 0;
84int char_fd = -1;
85int srvinfo;
86static char char_ip_str[128];
87static uint32 char_ip = 0;
88static uint16 char_port = 6121;
89static char userid[NAME_LENGTH], passwd[NAME_LENGTH];
90static int chrif_state = 0;
91int other_mapserver_count=0; //Holds count of how many other map servers are online (apart of this instance) [Skotlex]
92
93//Interval at which map server updates online listing. [Valaris]
94#define CHECK_INTERVAL 3600000
95//Interval at which map server sends number of connected users. [Skotlex]
96#define UPDATE_INTERVAL 10000
97//This define should spare writing the check in every function. [Skotlex]
98#define chrif_check(a) { if(!chrif_isconnected()) return a; }
99
100
101/// Resets all the data.
102void chrif_reset(void)
103{
104 // TODO kick everyone out and reset everything [FlavioJS]
105 exit(EXIT_FAILURE);
106}
107
108
109/// Checks the conditions for the server to stop.
110/// Releases the cookie when all characters are saved.
111/// If all the conditions are met, it stops the core loop.
112void chrif_check_shutdown(void)
113{
114 if( runflag != MAPSERVER_ST_SHUTDOWN )
115 return;
116 if( auth_db->size(auth_db) > 0 )
117 return;
118 runflag = CORE_ST_STOP;
119}
120
121
122struct auth_node* chrif_search(int account_id)
123{
124 return (struct auth_node*)idb_get(auth_db, account_id);
125}
126
127struct auth_node* chrif_auth_check(int account_id, int char_id, enum sd_state state)
128{
129 struct auth_node *node = chrif_search(account_id);
130 return (node && node->char_id == char_id && node->state == state)?node:NULL;
131}
132
133bool chrif_auth_delete(int account_id, int char_id, enum sd_state state)
134{
135 struct auth_node *node;
136 if ((node=chrif_auth_check(account_id, char_id, state)))
137 {
138 int fd = node->sd?node->sd->fd:node->fd;
139 if (session[fd] && session[fd]->session_data == node->sd)
140 session[fd]->session_data = NULL;
141 if (node->char_dat) aFree(node->char_dat);
142 if (node->sd) aFree(node->sd);
143 ers_free(auth_db_ers, node);
144 idb_remove(auth_db,account_id);
145 return true;
146 }
147 return false;
148}
149
150//Moves the sd character to the auth_db structure.
151static bool chrif_sd_to_auth(TBL_PC* sd, enum sd_state state)
152{
153 struct auth_node *node;
154 if (chrif_search(sd->status.account_id))
155 return false; //Already exists?
156
157 node = ers_alloc(auth_db_ers, struct auth_node);
158 memset(node, 0, sizeof(struct auth_node));
159 node->account_id = sd->status.account_id;
160 node->char_id = sd->status.char_id;
161 node->login_id1 = sd->login_id1;
162 node->login_id2 = sd->login_id2;
163 node->sex = sd->status.sex;
164 node->fd = sd->fd;
165 node->sd = sd; //Data from logged on char.
166 node->node_created = gettick(); //timestamp for node timeouts
167 node->state = state;
168
169 sd->state.active = 0;
170 idb_put(auth_db, node->account_id, node);
171 return true;
172}
173
174static bool chrif_auth_logout(TBL_PC* sd, enum sd_state state)
175{
176 if(sd->fd && state == ST_LOGOUT)
177 { //Disassociate player, and free it after saving ack returns. [Skotlex]
178 //fd info must not be lost for ST_MAPCHANGE as a final packet needs to be sent to the player.
179 if (session[sd->fd])
180 session[sd->fd]->session_data = NULL;
181 sd->fd = 0;
182 }
183 return chrif_sd_to_auth(sd, state);
184}
185
186bool chrif_auth_finished(TBL_PC* sd)
187{
188 struct auth_node *node= chrif_search(sd->status.account_id);
189 if (node && node->sd == sd && node->state == ST_LOGIN) {
190 node->sd = NULL;
191 return chrif_auth_delete(node->account_id, node->char_id, ST_LOGIN);
192 }
193 return false;
194}
195// sets char-server's user id
196void chrif_setuserid(char *id)
197{
198 memcpy(userid, id, NAME_LENGTH);
199}
200
201// sets char-server's password
202void chrif_setpasswd(char *pwd)
203{
204 memcpy(passwd, pwd, NAME_LENGTH);
205}
206
207// security check, prints warning if using default password
208void chrif_checkdefaultlogin(void)
209{
210 if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) {
211 ShowWarning("SECURITY WARNING - Using the default user/password s1/p1 is not recommended.\n");
212#ifdef TXT_ONLY
213 ShowNotice("Please edit your save/account.txt file to create a proper inter-server user/password (gender 'S')\n");
214#else
215 ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
216#endif
217 ShowNotice("and then edit your user/password in conf/map_athena.conf (or conf/import/map_conf.txt)\n");
218 }
219}
220
221// sets char-server's ip address
222int chrif_setip(const char* ip)
223{
224 char ip_str[16];
225 char_ip = host2ip(ip);
226 if (!char_ip) {
227 ShowWarning("Failed to Resolve Char Server Address! (%s)\n", ip);
228 return 0;
229 }
230 strncpy(char_ip_str, ip, sizeof(char_ip_str));
231 ShowInfo("Char Server IP Address : '"CL_WHITE"%s"CL_RESET"' -> '"CL_WHITE"%s"CL_RESET"'.\n", ip, ip2str(char_ip, ip_str));
232 return 1;
233}
234
235// sets char-server's port number
236void chrif_setport(uint16 port)
237{
238 char_port = port;
239}
240
241// says whether the char-server is connected or not
242int chrif_isconnected(void)
243{
244 return (char_fd > 0 && session[char_fd] != NULL && chrif_state == 2);
245}
246
247/*==========================================
248 * Saves character data.
249 * Flag = 1: Character is quitting
250 * Flag = 2: Character is changing map-servers
251 *------------------------------------------*/
252int chrif_save(struct map_session_data *sd, int flag)
253{
254 nullpo_retr(-1, sd);
255
256 pc_makesavestatus(sd);
257
258 if (flag && sd->state.active) //Store player data which is quitting.
259 {
260 //FIXME: SC are lost if there's no connection at save-time because of the way its related data is cleared immediately after this function. [Skotlex]
261 if (chrif_isconnected()) chrif_save_scdata(sd);
262 if (!chrif_auth_logout(sd, flag==1?ST_LOGOUT:ST_MAPCHANGE))
263 ShowError("chrif_save: Failed to set up player %d:%d for proper quitting!\n", sd->status.account_id, sd->status.char_id);
264 }
265
266 if(!chrif_isconnected())
267 return -1; //Character is saved on reconnect.
268
269 //For data sync
270 if (sd->state.storage_flag == 2)
271 storage_guild_storagesave(sd->status.account_id, sd->status.guild_id, flag);
272
273 if (flag)
274 sd->state.storage_flag = 0; //Force close it.
275
276 //Saving of registry values.
277 if (sd->state.reg_dirty&4)
278 intif_saveregistry(sd, 3); //Save char regs
279 if (sd->state.reg_dirty&2)
280 intif_saveregistry(sd, 2); //Save account regs
281 if (sd->state.reg_dirty&1)
282 intif_saveregistry(sd, 1); //Save account2 regs
283
284 WFIFOHEAD(char_fd, sizeof(sd->status) + 13);
285 WFIFOW(char_fd,0) = 0x2b01;
286 WFIFOW(char_fd,2) = sizeof(sd->status) + 13;
287 WFIFOL(char_fd,4) = sd->status.account_id;
288 WFIFOL(char_fd,8) = sd->status.char_id;
289 WFIFOB(char_fd,12) = (flag==1)?1:0; //Flag to tell char-server this character is quitting.
290 memcpy(WFIFOP(char_fd,13), &sd->status, sizeof(sd->status));
291 WFIFOSET(char_fd, WFIFOW(char_fd,2));
292
293 if( sd->status.pet_id > 0 && sd->pd )
294 intif_save_petdata(sd->status.account_id,&sd->pd->pet);
295 if( sd->hd && merc_is_hom_active(sd->hd) )
296 merc_save(sd->hd);
297 if( sd->md && mercenary_get_lifetime(sd->md) > 0 )
298 mercenary_save(sd->md);
299#ifndef TXT_ONLY
300 if( sd->save_quest )
301 intif_quest_save(sd);
302#endif
303
304 return 0;
305}
306
307// connects to char-server (plaintext)
308int chrif_connect(int fd)
309{
310 ShowStatus("Logging in to char server...\n", char_fd);
311 WFIFOHEAD(fd,60);
312 WFIFOW(fd,0) = 0x2af8;
313 memcpy(WFIFOP(fd,2), userid, NAME_LENGTH);
314 memcpy(WFIFOP(fd,26), passwd, NAME_LENGTH);
315 WFIFOL(fd,50) = 0;
316 WFIFOL(fd,54) = htonl(clif_getip());
317 WFIFOW(fd,58) = htons(clif_getport());
318 WFIFOSET(fd,60);
319
320 return 0;
321}
322
323// sends maps to char-server
324int chrif_sendmap(int fd)
325{
326 int i;
327 ShowStatus("Sending maps to char server...\n");
328 // Sending normal maps, not instances
329 WFIFOHEAD(fd, 4 + instance_start * 4);
330 WFIFOW(fd,0) = 0x2afa;
331 for(i = 0; i < instance_start; i++)
332 WFIFOW(fd,4+i*4) = map[i].index;
333 WFIFOW(fd,2) = 4 + i * 4;
334 WFIFOSET(fd,WFIFOW(fd,2));
335
336 return 0;
337}
338
339// receive maps from some other map-server (relayed via char-server)
340int chrif_recvmap(int fd)
341{
342 int i, j;
343 uint32 ip = ntohl(RFIFOL(fd,4));
344 uint16 port = ntohs(RFIFOW(fd,8));
345
346 for(i = 10, j = 0; i < RFIFOW(fd,2); i += 4, j++) {
347 map_setipport(RFIFOW(fd,i), ip, port);
348 }
349 if (battle_config.etc_log)
350 ShowStatus("Received maps from %d.%d.%d.%d:%d (%d maps)\n", CONVIP(ip), port, j);
351
352 other_mapserver_count++;
353 return 0;
354}
355
356// remove specified maps (used when some other map-server disconnects)
357int chrif_removemap(int fd)
358{
359 int i, j;
360 uint32 ip = RFIFOL(fd,4);
361 uint16 port = RFIFOW(fd,8);
362
363 for(i = 10, j = 0; i < RFIFOW(fd, 2); i += 4, j++)
364 map_eraseipport(RFIFOW(fd, i), ip, port);
365
366 other_mapserver_count--;
367 if(battle_config.etc_log)
368 ShowStatus("remove map of server %d.%d.%d.%d:%d (%d maps)\n", CONVIP(ip), port, j);
369 return 0;
370}
371
372// received after a character has been "final saved" on the char-server
373static void chrif_save_ack(int fd)
374{
375 chrif_auth_delete(RFIFOL(fd,2), RFIFOL(fd,6), ST_LOGOUT);
376 chrif_check_shutdown();
377}
378
379// request to move a character between mapservers
380int chrif_changemapserver(struct map_session_data* sd, uint32 ip, uint16 port)
381{
382 nullpo_retr(-1, sd);
383
384 if (other_mapserver_count < 1)
385 { //No other map servers are online!
386 clif_authfail_fd(sd->fd, 0);
387 return -1;
388 }
389
390 chrif_check(-1);
391
392 WFIFOHEAD(char_fd,35);
393 WFIFOW(char_fd, 0) = 0x2b05;
394 WFIFOL(char_fd, 2) = sd->bl.id;
395 WFIFOL(char_fd, 6) = sd->login_id1;
396 WFIFOL(char_fd,10) = sd->login_id2;
397 WFIFOL(char_fd,14) = sd->status.char_id;
398 WFIFOW(char_fd,18) = sd->mapindex;
399 WFIFOW(char_fd,20) = sd->bl.x;
400 WFIFOW(char_fd,22) = sd->bl.y;
401 WFIFOL(char_fd,24) = htonl(ip);
402 WFIFOW(char_fd,28) = htons(port);
403 WFIFOB(char_fd,30) = sd->status.sex;
404 WFIFOL(char_fd,31) = htonl(session[sd->fd]->client_addr);
405 WFIFOSET(char_fd,35);
406 return 0;
407}
408
409/// map-server change request acknowledgement (positive or negative)
410/// R 2b06 <account_id>.L <login_id1>.L <login_id2>.L <char_id>.L <map_index>.W <x>.W <y>.W <ip>.L <port>.W
411int chrif_changemapserverack(int account_id, int login_id1, int login_id2, int char_id, short map_index, short x, short y, uint32 ip, uint16 port)
412{
413 struct auth_node *node;
414 if (!(node=chrif_auth_check(account_id, char_id, ST_MAPCHANGE)))
415 return -1;
416
417 if (!login_id1) {
418 ShowError("map server change failed.\n");
419 clif_authfail_fd(node->fd, 0);
420 } else
421 clif_changemapserver(node->sd, map_index, x, y, ntohl(ip), ntohs(port));
422
423 //Player has been saved already, remove him from memory. [Skotlex]
424 chrif_auth_delete(account_id, char_id, ST_MAPCHANGE);
425
426 return 0;
427}
428
429/*==========================================
430 *
431 *------------------------------------------*/
432int chrif_connectack(int fd)
433{
434 static bool char_init_done = false;
435
436 if (RFIFOB(fd,2)) {
437 ShowFatalError("Connection to char-server failed %d.\n", RFIFOB(fd,2));
438 exit(EXIT_FAILURE);
439 }
440
441 ShowStatus("Successfully logged on to Char Server (Connection: '"CL_WHITE"%d"CL_RESET"').\n",fd);
442 chrif_state = 1;
443 chrif_connected = 1;
444
445 chrif_sendmap(fd);
446
447 ShowStatus("Event '"CL_WHITE"OnInterIfInit"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc_event_doall("OnInterIfInit"));
448 if( !char_init_done ) {
449 char_init_done = true;
450 ShowStatus("Event '"CL_WHITE"OnInterIfInitOnce"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc_event_doall("OnInterIfInitOnce"));
451 }
452
453 return 0;
454}
455static int chrif_reconnect(DBKey key,void *data,va_list ap)
456{
457 struct auth_node *node=(struct auth_node*)data;
458 switch (node->state) {
459 case ST_LOGIN:
460 if (node->sd && node->char_dat == NULL)
461 { //Since there is no way to request the char auth, make it fail.
462 pc_authfail(node->sd);
463 chrif_char_offline(node->sd);
464 chrif_auth_delete(node->account_id, node->char_id, ST_LOGIN);
465 }
466 break;
467 case ST_LOGOUT:
468 //Re-send final save
469 chrif_save(node->sd, 1);
470 break;
471 case ST_MAPCHANGE:
472 { //Re-send map-change request.
473 struct map_session_data *sd = node->sd;
474 uint32 ip;
475 uint16 port;
476 if(map_mapname2ipport(sd->mapindex,&ip,&port)==0)
477 chrif_changemapserver(sd, ip, port);
478 else //too much lag/timeout is the closest explanation for this error.
479 clif_authfail_fd(sd->fd, 3);
480 break;
481 }
482 }
483 return 0;
484}
485
486
487/// Called when all the connection steps are completed.
488void chrif_on_ready(void)
489{
490 ShowStatus("Map Server is now online.\n");
491 chrif_state = 2;
492 chrif_check_shutdown();
493
494 //If there are players online, send them to the char-server. [Skotlex]
495 send_users_tochar();
496
497 //Auth db reconnect handling
498 auth_db->foreach(auth_db,chrif_reconnect);
499
500 //Re-save any storages that were modified in the disconnection time. [Skotlex]
501 do_reconnect_storage();
502}
503
504
505/*==========================================
506 *
507 *------------------------------------------*/
508int chrif_sendmapack(int fd)
509{
510 if (RFIFOB(fd,2)) {
511 ShowFatalError("chrif : send map list to char server failed %d\n", RFIFOB(fd,2));
512 exit(EXIT_FAILURE);
513 }
514
515 memcpy(wisp_server_name, RFIFOP(fd,3), NAME_LENGTH);
516 chrif_on_ready();
517 return 0;
518}
519
520/*==========================================
521 * Request sc_data from charserver [Skotlex]
522 *------------------------------------------*/
523int chrif_scdata_request(int account_id, int char_id)
524{
525#ifdef ENABLE_SC_SAVING
526 chrif_check(-1);
527
528 WFIFOHEAD(char_fd,10);
529 WFIFOW(char_fd,0) = 0x2afc;
530 WFIFOL(char_fd,2) = account_id;
531 WFIFOL(char_fd,6) = char_id;
532 WFIFOSET(char_fd,10);
533#endif
534 return 0;
535}
536
537/*==========================================
538 * Request auth confirmation
539 *------------------------------------------*/
540void chrif_authreq(struct map_session_data *sd)
541{
542 struct auth_node *node= chrif_search(sd->bl.id);
543
544 if( node != NULL )
545 {
546 set_eof(sd->fd);
547 return;
548 }
549
550 if( !chrif_isconnected() )
551 return;
552
553 WFIFOHEAD(char_fd,19);
554 WFIFOW(char_fd,0) = 0x2b26;
555 WFIFOL(char_fd,2) = sd->status.account_id;
556 WFIFOL(char_fd,6) = sd->status.char_id;
557 WFIFOL(char_fd,10) = sd->login_id1;
558 WFIFOB(char_fd,14) = sd->status.sex;
559 WFIFOL(char_fd,15) = htonl(session[sd->fd]->client_addr);
560 WFIFOSET(char_fd,19);
561 chrif_sd_to_auth(sd, ST_LOGIN);
562}
563
564/*==========================================
565 * Auth confirmation ack
566 *------------------------------------------*/
567void chrif_authok(int fd)
568{
569 int account_id;
570 uint32 login_id1;
571 uint32 login_id2;
572 time_t expiration_time;
573 int gmlevel;
574 struct mmo_charstatus* status;
575 int char_id;
576 struct auth_node *node;
577 TBL_PC* sd;
578
579 //Check if both servers agree on the struct's size
580 if( RFIFOW(fd,2) - 24 != sizeof(struct mmo_charstatus) )
581 {
582 ShowError("chrif_authok: Data size mismatch! %d != %d\n", RFIFOW(fd,2) - 24, sizeof(struct mmo_charstatus));
583 return;
584 }
585
586 account_id = RFIFOL(fd,4);
587 login_id1 = RFIFOL(fd,8);
588 login_id2 = RFIFOL(fd,12);
589 expiration_time = (time_t)(int32)RFIFOL(fd,16);
590 gmlevel = RFIFOL(fd,20);
591 status = (struct mmo_charstatus*)RFIFOP(fd,24);
592
593 char_id = status->char_id;
594
595 //Check if we don't already have player data in our server
596 //Causes problems if the currently connected player tries to quit or this data belongs to an already connected player which is trying to re-auth.
597 if ((sd = map_id2sd(account_id)) != NULL)
598 return;
599
600 if ((node = chrif_search(account_id)) == NULL)
601 return; // should not happen
602
603 if (node->state != ST_LOGIN)
604 return; //character in logout phase, do not touch that data.
605
606 if (node->sd == NULL)
607 {
608 /*
609 //When we receive double login info and the client has not connected yet,
610 //discard the older one and keep the new one.
611 chrif_auth_delete(node->account_id, node->char_id, ST_LOGIN);
612 */
613 return; // should not happen
614 }
615
616 sd = node->sd;
617 if( runflag == MAPSERVER_ST_RUNNING &&
618 node->char_dat == NULL &&
619 node->account_id == account_id &&
620 node->char_id == char_id &&
621 node->login_id1 == login_id1 )
622 { //Auth Ok
623 if (pc_authok(sd, login_id2, expiration_time, gmlevel, status))
624 return;
625 } else { //Auth Failed
626 pc_authfail(sd);
627 }
628 chrif_char_offline(sd); //Set him offline, the char server likely has it set as online already.
629 chrif_auth_delete(account_id, char_id, ST_LOGIN);
630}
631
632// client authentication failed
633void chrif_authfail(int fd)
634{
635 int account_id;
636 int char_id;
637 uint32 login_id1;
638 char sex;
639 uint32 ip;
640 struct auth_node* node;
641
642 account_id = RFIFOL(fd,2);
643 char_id = RFIFOL(fd,6);
644 login_id1 = RFIFOL(fd,10);
645 sex = RFIFOB(fd,14);
646 ip = ntohl(RFIFOL(fd,15));
647
648 node = chrif_search(account_id);
649 if( node != NULL &&
650 node->account_id == account_id &&
651 node->char_id == char_id &&
652 node->login_id1 == login_id1 &&
653 node->sex == sex &&
654 node->state == ST_LOGIN )
655 {// found a match
656 clif_authfail_fd(node->fd, 0);
657 chrif_auth_delete(account_id, char_id, ST_LOGIN);
658 }
659}
660
661
662//This can still happen (client times out while waiting for char to confirm auth data)
663int auth_db_cleanup_sub(DBKey key,void *data,va_list ap)
664{
665 struct auth_node *node=(struct auth_node*)data;
666 const char* states[] = { "Login", "Logout", "Map change" };
667 if(DIFF_TICK(gettick(),node->node_created)>60000) {
668 switch (node->state)
669 {
670 case ST_LOGOUT:
671 //Re-save attempt (->sd should never be null here).
672 node->node_created = gettick(); //Refresh tick (avoid char-server load if connection is really bad)
673 chrif_save(node->sd, 1);
674 break;
675 default:
676 //Clear data. any connected players should have timed out by now.
677 ShowInfo("auth_db: Node (state %s) timed out for %d:%d\n", states[node->state], node->account_id, node->char_id);
678 chrif_char_offline_nsd(node->account_id, node->char_id);
679 chrif_auth_delete(node->account_id, node->char_id, node->state);
680 break;
681 }
682 return 1;
683 }
684 return 0;
685}
686
687int auth_db_cleanup(int tid, unsigned int tick, int id, intptr_t data)
688{
689 if(!chrif_isconnected()) return 0;
690 auth_db->foreach(auth_db, auth_db_cleanup_sub);
691 return 0;
692}
693
694/*==========================================
695 *
696 *------------------------------------------*/
697int chrif_charselectreq(struct map_session_data* sd, uint32 s_ip)
698{
699 nullpo_retr(-1, sd);
700
701 if( !sd || !sd->bl.id || !sd->login_id1 )
702 return -1;
703 chrif_check(-1);
704
705 WFIFOHEAD(char_fd,18);
706 WFIFOW(char_fd, 0) = 0x2b02;
707 WFIFOL(char_fd, 2) = sd->bl.id;
708 WFIFOL(char_fd, 6) = sd->login_id1;
709 WFIFOL(char_fd,10) = sd->login_id2;
710 WFIFOL(char_fd,14) = htonl(s_ip);
711 WFIFOSET(char_fd,18);
712
713 return 0;
714}
715
716/*==========================================
717 * Æ’Lƒƒƒ‰–¼–₢‡‚¹
718 *------------------------------------------*/
719int chrif_searchcharid(int char_id)
720{
721 if( !char_id )
722 return -1;
723 chrif_check(-1);
724
725 WFIFOHEAD(char_fd,6);
726 WFIFOW(char_fd,0) = 0x2b08;
727 WFIFOL(char_fd,2) = char_id;
728 WFIFOSET(char_fd,6);
729
730 return 0;
731}
732
733/*==========================================
734 * Change Email
735 *------------------------------------------*/
736int chrif_changeemail(int id, const char *actual_email, const char *new_email)
737{
738 if (battle_config.etc_log)
739 ShowInfo("chrif_changeemail: account: %d, actual_email: '%s', new_email: '%s'.\n", id, actual_email, new_email);
740
741 chrif_check(-1);
742
743 WFIFOHEAD(char_fd,86);
744 WFIFOW(char_fd,0) = 0x2b0c;
745 WFIFOL(char_fd,2) = id;
746 memcpy(WFIFOP(char_fd,6), actual_email, 40);
747 memcpy(WFIFOP(char_fd,46), new_email, 40);
748 WFIFOSET(char_fd,86);
749
750 return 0;
751}
752
753/*==========================================
754 * S 2b0e <accid>.l <name>.24B <type>.w { <year>.w <month>.w <day>.w <hour>.w <minute>.w <second>.w }
755 * Send an account modification request to the login server (via char server).
756 * type of operation:
757 * 1: block, 2: ban, 3: unblock, 4: unban, 5: changesex (use next function for 5)
758 *------------------------------------------*/
759int chrif_char_ask_name(int acc, const char* character_name, unsigned short operation_type, int year, int month, int day, int hour, int minute, int second)
760{
761 chrif_check(-1);
762
763 WFIFOHEAD(char_fd,44);
764 WFIFOW(char_fd,0) = 0x2b0e;
765 WFIFOL(char_fd,2) = acc;
766 safestrncpy((char*)WFIFOP(char_fd,6), character_name, NAME_LENGTH);
767 WFIFOW(char_fd,30) = operation_type;
768 if (operation_type == 2) {
769 WFIFOW(char_fd,32) = year;
770 WFIFOW(char_fd,34) = month;
771 WFIFOW(char_fd,36) = day;
772 WFIFOW(char_fd,38) = hour;
773 WFIFOW(char_fd,40) = minute;
774 WFIFOW(char_fd,42) = second;
775 }
776 WFIFOSET(char_fd,44);
777 return 0;
778}
779
780int chrif_changesex(struct map_session_data *sd)
781{
782 chrif_check(-1);
783 WFIFOHEAD(char_fd,44);
784 WFIFOW(char_fd,0) = 0x2b0e;
785 WFIFOL(char_fd,2) = sd->status.account_id;
786 safestrncpy((char*)WFIFOP(char_fd,6), sd->status.name, NAME_LENGTH);
787 WFIFOW(char_fd,30) = 5;
788 WFIFOSET(char_fd,44);
789
790 clif_displaymessage(sd->fd, "Need disconnection to perform change-sex request...");
791
792 if (sd->fd)
793 clif_authfail_fd(sd->fd, 15);
794 else
795 map_quit(sd);
796 return 0;
797}
798
799/*==========================================
800 * R 2b0f <accid>.l <name>.24B <type>.w <answer>.w
801 * Processing a reply to chrif_char_ask_name() (request to modify an account).
802 * type of operation:
803 * 1: block, 2: ban, 3: unblock, 4: unban, 5: changesex
804 * type of answer:
805 * 0: login-server request done
806 * 1: player not found
807 * 2: gm level too low
808 * 3: login-server offline
809 *------------------------------------------*/
810static void chrif_char_ask_name_answer(int acc, const char* player_name, uint16 type, uint16 answer)
811{
812 struct map_session_data* sd;
813 const char* action;
814 char output[256];
815
816 sd = map_id2sd(acc);
817 if( acc < 0 || sd == NULL ) {
818 ShowError("chrif_char_ask_name_answer failed - player not online.\n");
819 return;
820 }
821
822 switch( type ) {
823 case 1 : action = "block"; break;
824 case 2 : action = "ban"; break;
825 case 3 : action = "unblock"; break;
826 case 4 : action = "unban"; break;
827 case 5 : action = "change the sex of"; break;
828 default: action = "???"; break;
829 }
830
831 switch( answer ) {
832 case 0 : sprintf(output, "Login-server has been asked to %s the player '%.*s'.", action, NAME_LENGTH, player_name); break;
833 case 1 : sprintf(output, "The player '%.*s' doesn't exist.", NAME_LENGTH, player_name); break;
834 case 2 : sprintf(output, "Your GM level don't authorise you to %s the player '%.*s'.", action, NAME_LENGTH, player_name); break;
835 case 3 : sprintf(output, "Login-server is offline. Impossible to %s the player '%.*s'.", action, NAME_LENGTH, player_name); break;
836 default: output[0] = '\0'; break;
837 }
838
839 clif_displaymessage(sd->fd, output);
840}
841
842/*==========================================
843 * «•ʕÉ»ÂI—¹ (modified by Yor)
844 *------------------------------------------*/
845int chrif_changedsex(int fd)
846{
847 int acc, sex, i;
848 struct map_session_data *sd;
849
850 acc = RFIFOL(fd,2);
851 sex = RFIFOL(fd,6);
852 if (battle_config.etc_log)
853 ShowNotice("chrif_changedsex %d.\n", acc);
854 sd = map_id2sd(acc);
855 if (sd) { //Normally there should not be a char logged on right now!
856 if (sd->status.sex == sex)
857 return 0; //Do nothing? Likely safe.
858 sd->status.sex = !sd->status.sex;
859
860 // reset skill of some job
861 if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER) {
862 // remove specifical skills of Bard classes
863 for(i = 315; i <= 322; i++) {
864 if (sd->status.skill[i].id > 0 && sd->status.skill[i].flag == SKILL_FLAG_PERMANENT) {
865 sd->status.skill_point += sd->status.skill[i].lv;
866 sd->status.skill[i].id = 0;
867 sd->status.skill[i].lv = 0;
868 }
869 }
870 // remove specifical skills of Dancer classes
871 for(i = 323; i <= 330; i++) {
872 if (sd->status.skill[i].id > 0 && sd->status.skill[i].flag == SKILL_FLAG_PERMANENT) {
873 sd->status.skill_point += sd->status.skill[i].lv;
874 sd->status.skill[i].id = 0;
875 sd->status.skill[i].lv = 0;
876 }
877 }
878 pc_onstatuschanged(sd, SP_SKILLPOINT);
879 // change job if necessary
880 if (sd->status.sex) //Changed from Dancer
881 sd->status.class_ -= 1;
882 else //Changed from Bard
883 sd->status.class_ += 1;
884 //sd->class_ needs not be updated as both Dancer/Bard are the same.
885 }
886 // save character
887 sd->login_id1++; // change identify, because if player come back in char within the 5 seconds, he can change its characters
888 // do same modify in login-server for the account, but no in char-server (it ask again login_id1 to login, and don't remember it)
889 clif_displaymessage(sd->fd, "Your sex has been changed (need disconnection by the server)...");
890 set_eof(sd->fd); // forced to disconnect for the change
891 map_quit(sd); // Remove leftovers (e.g. autotrading) [Paradox924X]
892 }
893 return 0;
894}
895/*==========================================
896 * Request Char Server to Divorce Players
897 *------------------------------------------*/
898int chrif_divorce(int partner_id1, int partner_id2)
899{
900 chrif_check(-1);
901
902 WFIFOHEAD(char_fd,10);
903 WFIFOW(char_fd,0) = 0x2b11;
904 WFIFOL(char_fd,2) = partner_id1;
905 WFIFOL(char_fd,6) = partner_id2;
906 WFIFOSET(char_fd,10);
907
908 return 0;
909}
910
911/*==========================================
912 * Divorce players
913 * only used if 'partner_id' is offline
914 *------------------------------------------*/
915int chrif_divorceack(int char_id, int partner_id)
916{
917 struct map_session_data* sd;
918 int i;
919
920 if( !char_id || !partner_id )
921 return 0;
922
923 if( (sd = map_charid2sd(char_id)) != NULL && sd->status.partner_id == partner_id )
924 {
925 sd->status.partner_id = 0;
926 for(i = 0; i < MAX_INVENTORY; i++)
927 if (sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F)
928 pc_delitem(sd, i, 1, 0, 0);
929 }
930
931 if( (sd = map_charid2sd(partner_id)) != NULL && sd->status.partner_id == char_id )
932 {
933 sd->status.partner_id = 0;
934 for(i = 0; i < MAX_INVENTORY; i++)
935 if (sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F)
936 pc_delitem(sd, i, 1, 0, 0);
937 }
938
939 return 0;
940}
941/*==========================================
942 * Removes Baby from parents
943 *------------------------------------------*/
944int chrif_deadopt(int father_id, int mother_id, int child_id)
945{
946 struct map_session_data* sd;
947
948 if( father_id && (sd = map_charid2sd(father_id)) != NULL && sd->status.child == child_id )
949 {
950 sd->status.child = 0;
951 sd->status.skill[WE_CALLBABY].id = 0;
952 sd->status.skill[WE_CALLBABY].lv = 0;
953 sd->status.skill[WE_CALLBABY].flag = 0;
954 clif_deleteskill(sd,WE_CALLBABY);
955 }
956
957 if( mother_id && (sd = map_charid2sd(mother_id)) != NULL && sd->status.child == child_id )
958 {
959 sd->status.child = 0;
960 sd->status.skill[WE_CALLBABY].id = 0;
961 sd->status.skill[WE_CALLBABY].lv = 0;
962 sd->status.skill[WE_CALLBABY].flag = 0;
963 clif_deleteskill(sd,WE_CALLBABY);
964 }
965
966 return 0;
967}
968
969/*==========================================
970 * Disconnection of a player (account has been banned of has a status, from login-server) by [Yor]
971 *------------------------------------------*/
972int chrif_accountban(int fd)
973{
974 int acc;
975 struct map_session_data *sd;
976
977 acc = RFIFOL(fd,2);
978 if (battle_config.etc_log)
979 ShowNotice("chrif_accountban %d.\n", acc);
980 sd = map_id2sd(acc);
981
982 if (acc < 0 || sd == NULL) {
983 ShowError("chrif_accountban failed - player not online.\n");
984 return 0;
985 }
986
987 sd->login_id1++; // change identify, because if player come back in char within the 5 seconds, he can change its characters
988 if (RFIFOB(fd,6) == 0) // 0: change of statut, 1: ban
989 {
990 switch (RFIFOL(fd,7)) { // status or final date of a banishment
991 case 1: clif_displaymessage(sd->fd, "Your account has 'Unregistered'."); break;
992 case 2: clif_displaymessage(sd->fd, "Your account has an 'Incorrect Password'..."); break;
993 case 3: clif_displaymessage(sd->fd, "Your account has expired."); break;
994 case 4: clif_displaymessage(sd->fd, "Your account has been rejected from server."); break;
995 case 5: clif_displaymessage(sd->fd, "Your account has been blocked by the GM Team."); break;
996 case 6: clif_displaymessage(sd->fd, "Your Game's EXE file is not the latest version."); break;
997 case 7: clif_displaymessage(sd->fd, "Your account has been prohibited to log in."); break;
998 case 8: clif_displaymessage(sd->fd, "Server is jammed due to over populated."); break;
999 case 9: clif_displaymessage(sd->fd, "Your account has not more authorised."); break;
1000 case 100: clif_displaymessage(sd->fd, "Your account has been totally erased."); break;
1001 default: clif_displaymessage(sd->fd, "Your account has not more authorised."); break;
1002 }
1003 }
1004 else if (RFIFOB(fd,6) == 1) // 0: change of statut, 1: ban
1005 {
1006 time_t timestamp;
1007 char tmpstr[2048];
1008 timestamp = (time_t)RFIFOL(fd,7); // status or final date of a banishment
1009 strcpy(tmpstr, "Your account has been banished until ");
1010 strftime(tmpstr + strlen(tmpstr), 24, "%d-%m-%Y %H:%M:%S", localtime(×tamp));
1011 clif_displaymessage(sd->fd, tmpstr);
1012 }
1013
1014 set_eof(sd->fd); // forced to disconnect for the change
1015 map_quit(sd); // Remove leftovers (e.g. autotrading) [Paradox924X]
1016 return 0;
1017}
1018
1019//Disconnect the player out of the game, simple packet
1020//packet.w AID.L WHY.B 2+4+1 = 7byte
1021int chrif_disconnectplayer(int fd)
1022{
1023 struct map_session_data* sd;
1024 int account_id = RFIFOL(fd, 2);
1025
1026 sd = map_id2sd(account_id);
1027 if( sd == NULL )
1028 {
1029 struct auth_node* auth = chrif_search(account_id);
1030 if( auth != NULL && chrif_auth_delete(account_id, auth->char_id, ST_LOGIN) )
1031 return 0;
1032 return -1;
1033 }
1034
1035 if (!sd->fd)
1036 { //No connection
1037 if (sd->state.autotrade)
1038 map_quit(sd); //Remove it.
1039 //Else we don't remove it because the char should have a timer to remove the player because it force-quit before,
1040 //and we don't want them kicking their previous instance before the 10 secs penalty time passes. [Skotlex]
1041 return 0;
1042 }
1043
1044 switch(RFIFOB(fd, 6))
1045 {
1046 case 1: clif_authfail_fd(sd->fd, 1); break; //server closed
1047 case 2: clif_authfail_fd(sd->fd, 2); break; //someone else logged in
1048 case 3: clif_authfail_fd(sd->fd, 4); break; //server overpopulated
1049 case 4: clif_authfail_fd(sd->fd, 10); break; //out of available time paid for
1050 case 5: clif_authfail_fd(sd->fd, 15); break; //forced to dc by gm
1051 }
1052 return 0;
1053}
1054
1055/*==========================================
1056 * Request/Receive top 10 Fame character list
1057 *------------------------------------------*/
1058
1059int chrif_updatefamelist(struct map_session_data* sd)
1060{
1061 char type;
1062 chrif_check(-1);
1063
1064 switch(sd->class_ & MAPID_UPPERMASK)
1065 {
1066 case MAPID_BLACKSMITH: type = 1; break;
1067 case MAPID_ALCHEMIST: type = 2; break;
1068 case MAPID_TAEKWON: type = 3; break;
1069 default:
1070 return 0;
1071 }
1072
1073 WFIFOHEAD(char_fd, 11);
1074 WFIFOW(char_fd,0) = 0x2b10;
1075 WFIFOL(char_fd,2) = sd->status.char_id;
1076 WFIFOL(char_fd,6) = sd->status.fame;
1077 WFIFOB(char_fd,10) = type;
1078 WFIFOSET(char_fd,11);
1079
1080 return 0;
1081}
1082
1083int chrif_buildfamelist(void)
1084{
1085 chrif_check(-1);
1086
1087 WFIFOHEAD(char_fd,2);
1088 WFIFOW(char_fd,0) = 0x2b1a;
1089 WFIFOSET(char_fd,2);
1090
1091 return 0;
1092}
1093
1094int chrif_recvfamelist(int fd)
1095{
1096 int num, size;
1097 int total = 0, len = 8;
1098
1099 memset (smith_fame_list, 0, sizeof(smith_fame_list));
1100 memset (chemist_fame_list, 0, sizeof(chemist_fame_list));
1101 memset (taekwon_fame_list, 0, sizeof(taekwon_fame_list));
1102
1103 size = RFIFOW(fd, 6); //Blacksmith block size
1104 for (num = 0; len < size && num < MAX_FAME_LIST; num++) {
1105 memcpy(&smith_fame_list[num], RFIFOP(fd,len), sizeof(struct fame_list));
1106 len += sizeof(struct fame_list);
1107 }
1108 total += num;
1109
1110 size = RFIFOW(fd, 4); //Alchemist block size
1111 for (num = 0; len < size && num < MAX_FAME_LIST; num++) {
1112 memcpy(&chemist_fame_list[num], RFIFOP(fd,len), sizeof(struct fame_list));
1113 len += sizeof(struct fame_list);
1114 }
1115 total += num;
1116
1117 size = RFIFOW(fd, 2); //Total packet length
1118 for (num = 0; len < size && num < MAX_FAME_LIST; num++) {
1119 memcpy(&taekwon_fame_list[num], RFIFOP(fd,len), sizeof(struct fame_list));
1120 len += sizeof(struct fame_list);
1121 }
1122 total += num;
1123
1124 ShowInfo("Received Fame List of '"CL_WHITE"%d"CL_RESET"' characters.\n", total);
1125
1126 return 0;
1127}
1128
1129/// fame ranking update confirmation
1130/// R 2b22 <table>.B <index>.B <value>.L
1131int chrif_updatefamelist_ack(int fd)
1132{
1133 struct fame_list* list;
1134 uint8 index;
1135 switch (RFIFOB(fd,2))
1136 {
1137 case 1: list = smith_fame_list; break;
1138 case 2: list = chemist_fame_list; break;
1139 case 3: list = taekwon_fame_list; break;
1140 default: return 0;
1141 }
1142 index = RFIFOB(fd, 3);
1143 if (index >= MAX_FAME_LIST)
1144 return 0;
1145 list[index].fame = RFIFOL(fd,4);
1146 return 1;
1147}
1148
1149int chrif_save_scdata(struct map_session_data *sd)
1150{ //parses the sc_data of the player and sends it to the char-server for saving. [Skotlex]
1151#ifdef ENABLE_SC_SAVING
1152 int i, count=0;
1153 unsigned int tick;
1154 struct status_change_data data;
1155 struct status_change *sc = &sd->sc;
1156 const struct TimerData *timer;
1157
1158 chrif_check(-1);
1159 tick = gettick();
1160
1161 WFIFOHEAD(char_fd, 14 + SC_MAX*sizeof(struct status_change_data));
1162 WFIFOW(char_fd,0) = 0x2b1c;
1163 WFIFOL(char_fd,4) = sd->status.account_id;
1164 WFIFOL(char_fd,8) = sd->status.char_id;
1165 for (i = 0; i < SC_MAX; i++)
1166 {
1167 if (!sc->data[i])
1168 continue;
1169 if (sc->data[i]->timer != INVALID_TIMER)
1170 {
1171 timer = get_timer(sc->data[i]->timer);
1172 if (timer == NULL || timer->func != status_change_timer || DIFF_TICK(timer->tick,tick) < 0)
1173 continue;
1174 data.tick = DIFF_TICK(timer->tick,tick); //Duration that is left before ending.
1175 } else
1176 data.tick = -1; //Infinite duration
1177 data.type = i;
1178 data.val1 = sc->data[i]->val1;
1179 data.val2 = sc->data[i]->val2;
1180 data.val3 = sc->data[i]->val3;
1181 data.val4 = sc->data[i]->val4;
1182 memcpy(WFIFOP(char_fd,14 +count*sizeof(struct status_change_data)),
1183 &data, sizeof(struct status_change_data));
1184 count++;
1185 }
1186 if (count == 0)
1187 return 0; //Nothing to save.
1188 WFIFOW(char_fd,12) = count;
1189 WFIFOW(char_fd,2) = 14 +count*sizeof(struct status_change_data); //Total packet size
1190 WFIFOSET(char_fd,WFIFOW(char_fd,2));
1191#endif
1192 return 0;
1193}
1194
1195//Retrieve and load sc_data for a player. [Skotlex]
1196int chrif_load_scdata(int fd)
1197{
1198#ifdef ENABLE_SC_SAVING
1199 struct map_session_data *sd;
1200 struct status_change_data *data;
1201 int aid, cid, i, count;
1202
1203 aid = RFIFOL(fd,4); //Player Account ID
1204 cid = RFIFOL(fd,8); //Player Char ID
1205
1206 sd = map_id2sd(aid);
1207 if (!sd)
1208 {
1209 ShowError("chrif_load_scdata: Player of AID %d not found!\n", aid);
1210 return -1;
1211 }
1212 if (sd->status.char_id != cid)
1213 {
1214 ShowError("chrif_load_scdata: Receiving data for account %d, char id does not matches (%d != %d)!\n", aid, sd->status.char_id, cid);
1215 return -1;
1216 }
1217 count = RFIFOW(fd,12); //sc_count
1218 for (i = 0; i < count; i++)
1219 {
1220 data = (struct status_change_data*)RFIFOP(fd,14 + i*sizeof(struct status_change_data));
1221 status_change_start(&sd->bl, (sc_type)data->type, 10000, data->val1, data->val2, data->val3, data->val4, data->tick, 15);
1222 }
1223#endif
1224 return 0;
1225}
1226
1227/*==========================================
1228 * Send rates and motd to char server [Wizputer]
1229 * S 2b16 <base rate>.L <job rate>.L <drop rate>.L
1230 *------------------------------------------*/
1231int chrif_ragsrvinfo(int base_rate, int job_rate, int drop_rate)
1232{
1233 chrif_check(-1);
1234
1235 WFIFOHEAD(char_fd,14);
1236 WFIFOW(char_fd,0) = 0x2b16;
1237 WFIFOL(char_fd,2) = base_rate;
1238 WFIFOL(char_fd,6) = job_rate;
1239 WFIFOL(char_fd,10) = drop_rate;
1240 WFIFOSET(char_fd,14);
1241 return 0;
1242}
1243
1244
1245/*=========================================
1246 * Tell char-server charcter disconnected [Wizputer]
1247 *-----------------------------------------*/
1248int chrif_char_offline(struct map_session_data *sd)
1249{
1250 chrif_check(-1);
1251
1252 WFIFOHEAD(char_fd,10);
1253 WFIFOW(char_fd,0) = 0x2b17;
1254 WFIFOL(char_fd,2) = sd->status.char_id;
1255 WFIFOL(char_fd,6) = sd->status.account_id;
1256 WFIFOSET(char_fd,10);
1257
1258 return 0;
1259}
1260int chrif_char_offline_nsd(int account_id, int char_id)
1261{
1262 chrif_check(-1);
1263
1264 WFIFOHEAD(char_fd,10);
1265 WFIFOW(char_fd,0) = 0x2b17;
1266 WFIFOL(char_fd,2) = char_id;
1267 WFIFOL(char_fd,6) = account_id;
1268 WFIFOSET(char_fd,10);
1269
1270 return 0;
1271}
1272
1273/*=========================================
1274 * Tell char-server to reset all chars offline [Wizputer]
1275 *-----------------------------------------*/
1276int chrif_flush_fifo(void)
1277{
1278 chrif_check(-1);
1279
1280 set_nonblocking(char_fd, 0);
1281 flush_fifos();
1282 set_nonblocking(char_fd, 1);
1283
1284 return 0;
1285}
1286
1287/*=========================================
1288 * Tell char-server to reset all chars offline [Wizputer]
1289 *-----------------------------------------*/
1290int chrif_char_reset_offline(void)
1291{
1292 chrif_check(-1);
1293
1294 WFIFOHEAD(char_fd,2);
1295 WFIFOW(char_fd,0) = 0x2b18;
1296 WFIFOSET(char_fd,2);
1297
1298 return 0;
1299}
1300
1301/*=========================================
1302 * Tell char-server charcter is online [Wizputer]
1303 *-----------------------------------------*/
1304
1305int chrif_char_online(struct map_session_data *sd)
1306{
1307 chrif_check(-1);
1308
1309 WFIFOHEAD(char_fd,10);
1310 WFIFOW(char_fd,0) = 0x2b19;
1311 WFIFOL(char_fd,2) = sd->status.char_id;
1312 WFIFOL(char_fd,6) = sd->status.account_id;
1313 WFIFOSET(char_fd,10);
1314
1315 return 0;
1316}
1317
1318
1319/// Called when the connection to Char Server is disconnected.
1320void chrif_on_disconnect(void)
1321{
1322 if( chrif_connected != 1 )
1323 ShowWarning("Connection to Char Server lost.\n\n");
1324 chrif_connected = 0;
1325
1326 other_mapserver_count = 0; //Reset counter. We receive ALL maps from all map-servers on reconnect.
1327 map_eraseallipport();
1328
1329 //Attempt to reconnect in a second. [Skotlex]
1330 add_timer(gettick() + 1000, check_connect_char_server, 0, 0);
1331}
1332
1333
1334void chrif_update_ip(int fd)
1335{
1336 uint32 new_ip;
1337 WFIFOHEAD(fd,6);
1338 new_ip = host2ip(char_ip_str);
1339 if (new_ip && new_ip != char_ip)
1340 char_ip = new_ip; //Update char_ip
1341
1342 new_ip = clif_refresh_ip();
1343 if (!new_ip) return; //No change
1344 WFIFOW(fd,0) = 0x2736;
1345 WFIFOL(fd,2) = htonl(new_ip);
1346 WFIFOSET(fd,6);
1347}
1348
1349// pings the charserver
1350void chrif_keepalive(int fd)
1351{
1352 WFIFOHEAD(fd,2);
1353 WFIFOW(fd,0) = 0x2b23;
1354 WFIFOSET(fd,2);
1355}
1356
1357void chrif_keepalive_ack(int fd)
1358{
1359}
1360
1361/*==========================================
1362 *
1363 *------------------------------------------*/
1364int chrif_parse(int fd)
1365{
1366 int packet_len, cmd;
1367
1368 // only process data from the char-server
1369 if (fd != char_fd)
1370 {
1371 ShowDebug("chrif_parse: Disconnecting invalid session #%d (is not the char-server)\n", fd);
1372 do_close(fd);
1373 return 0;
1374 }
1375
1376 if (session[fd]->flag.eof)
1377 {
1378 do_close(fd);
1379 char_fd = -1;
1380 chrif_on_disconnect();
1381 return 0;
1382 }
1383
1384 while (RFIFOREST(fd) >= 2)
1385 {
1386 cmd = RFIFOW(fd,0);
1387 if (cmd < 0x2af8 || cmd >= 0x2af8 + ARRAYLENGTH(packet_len_table) || packet_len_table[cmd-0x2af8] == 0)
1388 {
1389 int r = intif_parse(fd); // intif‚É“n‚·
1390
1391 if (r == 1) continue; // intif‚ň—‚µ‚½
1392 if (r == 2) return 0; // intif‚ň—‚µ‚½‚ªÂAÆ’fÂ[Æ’^‚ª‘«‚è‚È‚¢
1393
1394 ShowWarning("chrif_parse: session #%d, intif_parse failed (unrecognized command 0x%.4x).\n", fd, cmd);
1395 set_eof(fd);
1396 return 0;
1397 }
1398
1399 packet_len = packet_len_table[cmd-0x2af8];
1400 if (packet_len == -1)
1401 { // dynamic-length packet, second WORD holds the length
1402 if (RFIFOREST(fd) < 4)
1403 return 0;
1404 packet_len = RFIFOW(fd,2);
1405 }
1406
1407 if ((int)RFIFOREST(fd) < packet_len)
1408 return 0;
1409
1410 //ShowDebug("Received packet 0x%4x (%d bytes) from char-server (connection %d)\n", RFIFOW(fd,0), packet_len, fd);
1411
1412 switch(cmd)
1413 {
1414 case 0x2af9: chrif_connectack(fd); break;
1415 case 0x2afb: chrif_sendmapack(fd); break;
1416 case 0x2afd: chrif_authok(fd); break;
1417 case 0x2b00: map_setusers(RFIFOL(fd,2)); chrif_keepalive(fd); break;
1418 case 0x2b03: clif_charselectok(RFIFOL(fd,2), RFIFOB(fd,6)); break;
1419 case 0x2b04: chrif_recvmap(fd); break;
1420 case 0x2b06: chrif_changemapserverack(RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10), RFIFOL(fd,14), RFIFOW(fd,18), RFIFOW(fd,20), RFIFOW(fd,22), RFIFOL(fd,24), RFIFOW(fd,28)); break;
1421 case 0x2b09: map_addnickdb(RFIFOL(fd,2), (char*)RFIFOP(fd,6)); break;
1422 case 0x2b0d: chrif_changedsex(fd); break;
1423 case 0x2b0f: chrif_char_ask_name_answer(RFIFOL(fd,2), (char*)RFIFOP(fd,6), RFIFOW(fd,30), RFIFOW(fd,32)); break;
1424 case 0x2b12: chrif_divorceack(RFIFOL(fd,2), RFIFOL(fd,6)); break;
1425 case 0x2b14: chrif_accountban(fd); break;
1426 case 0x2b1b: chrif_recvfamelist(fd); break;
1427 case 0x2b1d: chrif_load_scdata(fd); break;
1428 case 0x2b1e: chrif_update_ip(fd); break;
1429 case 0x2b1f: chrif_disconnectplayer(fd); break;
1430 case 0x2b20: chrif_removemap(fd); break;
1431 case 0x2b21: chrif_save_ack(fd); break;
1432 case 0x2b22: chrif_updatefamelist_ack(fd); break;
1433 case 0x2b24: chrif_keepalive_ack(fd); break;
1434 case 0x2b25: chrif_deadopt(RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)); break;
1435 case 0x2b27: chrif_authfail(fd); break;
1436 default:
1437 ShowError("chrif_parse : unknown packet (session #%d): 0x%x. Disconnecting.\n", fd, cmd);
1438 set_eof(fd);
1439 return 0;
1440 }
1441 if (fd == char_fd) //There's the slight chance we lost the connection during parse, in which case this would segfault if not checked [Skotlex]
1442 RFIFOSKIP(fd, packet_len);
1443 }
1444
1445 return 0;
1446}
1447
1448int ping_char_server(int tid, unsigned int tick, int id, intptr_t data)
1449{
1450 chrif_check(-1);
1451 chrif_keepalive(char_fd);
1452 return 0;
1453}
1454
1455// unused
1456int send_usercount_tochar(int tid, unsigned int tick, int id, intptr_t data)
1457{
1458 chrif_check(-1);
1459
1460 WFIFOHEAD(char_fd,4);
1461 WFIFOW(char_fd,0) = 0x2afe;
1462 WFIFOW(char_fd,2) = map_usercount();
1463 WFIFOSET(char_fd,4);
1464 return 0;
1465}
1466
1467/*==========================================
1468 * timerŠÖÂâ€
1469 * ¡‚±‚ÌmapŽI‚ÉŒq‚ª‚ÂĂ¢‚éƒNƒ‰ƒCÆ’Aƒ“ƒgÂlÂâ€â€šÃ°charŽI‚Ö‘—‚é
1470 *------------------------------------------*/
1471int send_users_tochar(void)
1472{
1473 int users = 0, i = 0;
1474 struct map_session_data* sd;
1475 struct s_mapiterator* iter;
1476
1477 chrif_check(-1);
1478
1479 users = map_usercount();
1480 WFIFOHEAD(char_fd, 6+8*users);
1481 WFIFOW(char_fd,0) = 0x2aff;
1482 iter = mapit_getallusers();
1483 for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
1484 {
1485 WFIFOL(char_fd,6+8*i) = sd->status.account_id;
1486 WFIFOL(char_fd,6+8*i+4) = sd->status.char_id;
1487 i++;
1488 }
1489 mapit_free(iter);
1490 WFIFOW(char_fd,2) = 6 + 8*users;
1491 WFIFOW(char_fd,4) = users;
1492 WFIFOSET(char_fd, 6+8*users);
1493
1494 return 0;
1495}
1496
1497/*==========================================
1498 * timerŠÖÂâ€
1499 * charŽI‚Æ‚ÌÂÚ‘±‚ðŠmâ€F‚µÂA‚à ‚µÂØ‚ê‚Ä‚¢‚½‚çÂÄ“xÂÚ‘±‚·‚é
1500 *------------------------------------------*/
1501static int check_connect_char_server(int tid, unsigned int tick, int id, intptr_t data)
1502{
1503 static int displayed = 0;
1504 if (char_fd <= 0 || session[char_fd] == NULL)
1505 {
1506 if (!displayed)
1507 {
1508 ShowStatus("Attempting to connect to Char Server. Please wait.\n");
1509 displayed = 1;
1510 }
1511
1512 chrif_state = 0;
1513 char_fd = make_connection(char_ip, char_port);
1514 if (char_fd == -1)
1515 { //Attempt to connect later. [Skotlex]
1516 return 0;
1517 }
1518
1519 session[char_fd]->func_parse = chrif_parse;
1520 session[char_fd]->flag.server = 1;
1521 realloc_fifo(char_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
1522
1523 chrif_connect(char_fd);
1524 chrif_connected = (chrif_state == 2);
1525#ifndef TXT_ONLY
1526 srvinfo = 0;
1527#endif /* not TXT_ONLY */
1528 } else {
1529#ifndef TXT_ONLY
1530 if (srvinfo == 0) {
1531 chrif_ragsrvinfo(battle_config.base_exp_rate, battle_config.job_exp_rate, battle_config.item_rate_common);
1532 srvinfo = 1;
1533 }
1534#endif /* not TXT_ONLY */
1535 }
1536 if (chrif_isconnected()) displayed = 0;
1537 return 0;
1538}
1539
1540int auth_db_final(DBKey k,void *d,va_list ap)
1541{
1542 struct auth_node *node=(struct auth_node*)d;
1543 if (node->char_dat)
1544 aFree(node->char_dat);
1545 if (node->sd)
1546 aFree(node->sd);
1547 ers_free(auth_db_ers, node);
1548 return 0;
1549}
1550
1551/*==========================================
1552 * ÂI—¹
1553 *------------------------------------------*/
1554int do_final_chrif(void)
1555{
1556 if( char_fd != -1 )
1557 {
1558 do_close(char_fd);
1559 char_fd = -1;
1560 }
1561
1562 auth_db->destroy(auth_db, auth_db_final);
1563 ers_destroy(auth_db_ers);
1564 return 0;
1565}
1566
1567/*==========================================
1568 *
1569 *------------------------------------------*/
1570int do_init_chrif(void)
1571{
1572 auth_db = idb_alloc(DB_OPT_BASE);
1573 auth_db_ers = ers_new(sizeof(struct auth_node));
1574
1575 add_timer_func_list(check_connect_char_server, "check_connect_char_server");
1576 add_timer_func_list(ping_char_server, "ping_char_server");
1577 add_timer_func_list(auth_db_cleanup, "auth_db_cleanup");
1578
1579 // establish map-char connection if not present
1580 add_timer_interval(gettick() + 1000, check_connect_char_server, 0, 0, 10 * 1000);
1581
1582 // keep the map-char connection alive
1583 add_timer_interval(gettick() + 1000, ping_char_server, 0, 0, ((int)stall_time-2) * 1000);
1584
1585 // wipe stale data for timed-out client connection requests
1586 add_timer_interval(gettick() + 1000, auth_db_cleanup, 0, 0, 30 * 1000);
1587
1588 // send the user count every 10 seconds, to hide the charserver's online counting problem
1589 add_timer_interval(gettick() + 1000, send_usercount_tochar, 0, 0, UPDATE_INTERVAL);
1590
1591 return 0;
1592}
1593
1594//posted by ArCiZei