chrif.c 48 KB

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