chrif.c 44 KB

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