chrif.c 48 KB

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