chrif.c 50 KB

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