chrif.c 48 KB

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