chrif.c 49 KB

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