chrif.c 49 KB

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