chrif.c 59 KB

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