char_mapif.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "char_mapif.hpp"
  4. #include <cstdlib>
  5. #include <cstring> //memcpy
  6. #include <memory>
  7. #include <common/malloc.hpp>
  8. #include <common/showmsg.hpp>
  9. #include <common/socket.hpp>
  10. #include <common/sql.hpp>
  11. #include <common/strlib.hpp>
  12. #include <common/timer.hpp>
  13. #include <common/utilities.hpp>
  14. #include "char.hpp"
  15. #include "char_logif.hpp"
  16. #include "inter.hpp"
  17. using namespace rathena;
  18. /**
  19. * Packet send to all map-servers, attach to ourself
  20. * @param buf: packet to send in form of an array buffer
  21. * @param len: size of packet
  22. * @return : the number of map-serv the packet was sent to
  23. */
  24. int32 chmapif_sendall(unsigned char *buf, uint32 len){
  25. int32 i, c;
  26. c = 0;
  27. for(i = 0; i < ARRAYLENGTH(map_server); i++) {
  28. int32 fd;
  29. if (session_isValid(fd = map_server[i].fd)) {
  30. WFIFOHEAD(fd,len);
  31. memcpy(WFIFOP(fd,0), buf, len);
  32. WFIFOSET(fd,len);
  33. c++;
  34. }
  35. }
  36. return c;
  37. }
  38. /**
  39. * Packet send to all map-servers, except one. (wos: without our self) attach to ourself
  40. * @param sfd: fd to discard sending to
  41. * @param buf: packet to send in form of an array buffer
  42. * @param len: size of packet
  43. * @return : the number of map-serv the packet was sent to
  44. */
  45. int32 chmapif_sendallwos(int32 sfd, unsigned char *buf, uint32 len){
  46. int32 i, c;
  47. c = 0;
  48. for(i = 0; i < ARRAYLENGTH(map_server); i++) {
  49. int32 fd;
  50. if (session_isValid(fd = map_server[i].fd) && fd != sfd) {
  51. WFIFOHEAD(fd,len);
  52. memcpy(WFIFOP(fd,0), buf, len);
  53. WFIFOSET(fd,len);
  54. c++;
  55. }
  56. }
  57. return c;
  58. }
  59. /**
  60. * Packet send to all char-servers, except one. (wos: without our self)
  61. * @param fd: fd to send packet too
  62. * @param buf: packet to send in form of an array buffer
  63. * @param len: size of packet
  64. * @return : the number of map-serv the packet was sent to (O|1)
  65. */
  66. int32 chmapif_send(int32 fd, unsigned char *buf, uint32 len){
  67. if (session_isValid(fd)) {
  68. int32 i;
  69. ARR_FIND( 0, ARRAYLENGTH(map_server), i, fd == map_server[i].fd );
  70. if( i < ARRAYLENGTH(map_server) )
  71. {
  72. WFIFOHEAD(fd,len);
  73. memcpy(WFIFOP(fd,0), buf, len);
  74. WFIFOSET(fd,len);
  75. return 1;
  76. }
  77. }
  78. return 0;
  79. }
  80. /**
  81. * Send map-servers fames ranking lists
  82. * Defaut fame list are 32B, (id+point+names)
  83. * S <len>.W <len bs + alchi>.W <len bs>.W <smith_rank>?B <alchi_rank>?B <taek_rank>?B
  84. * @param fd: fd to send packet too (map-serv) if -1 send to all
  85. * @return : 0 success
  86. */
  87. int32 chmapif_send_fame_list(int32 fd){
  88. int32 i, len = 8;
  89. unsigned char buf[32000];
  90. WBUFW(buf,0) = 0x2b1b;
  91. for(i = 0; i < fame_list_size_smith && smith_fame_list[i].id; i++) {
  92. memcpy(WBUFP(buf, len), &smith_fame_list[i], sizeof(struct fame_list));
  93. len += sizeof(struct fame_list);
  94. }
  95. // add blacksmith's block length
  96. WBUFW(buf, 6) = len;
  97. for(i = 0; i < fame_list_size_chemist && chemist_fame_list[i].id; i++) {
  98. memcpy(WBUFP(buf, len), &chemist_fame_list[i], sizeof(struct fame_list));
  99. len += sizeof(struct fame_list);
  100. }
  101. // add alchemist's block length
  102. WBUFW(buf, 4) = len;
  103. for(i = 0; i < fame_list_size_taekwon && taekwon_fame_list[i].id; i++) {
  104. memcpy(WBUFP(buf, len), &taekwon_fame_list[i], sizeof(struct fame_list));
  105. len += sizeof(struct fame_list);
  106. }
  107. // add total packet length
  108. WBUFW(buf, 2) = len;
  109. if (session_isValid(fd))
  110. chmapif_send(fd, buf, len);
  111. else
  112. chmapif_sendall(buf, len);
  113. return 0;
  114. }
  115. /**
  116. * Send to map-servers the updated fame ranking lists
  117. * We actually just send this one when we only need to update rankpoint but pos didn't change
  118. * @param type: ranking type
  119. * @param index: position in the ranking
  120. * @param fame: number of points
  121. */
  122. void chmapif_update_fame_list(int32 type, int32 index, int32 fame) {
  123. unsigned char buf[8];
  124. WBUFW(buf,0) = 0x2b22;
  125. WBUFB(buf,2) = type;
  126. WBUFB(buf,3) = index;
  127. WBUFL(buf,4) = fame;
  128. chmapif_sendall(buf, 8);
  129. }
  130. /**
  131. * Send to map-servers the users count on this char-serv, (meaning the total of all mapserv)
  132. * @param users: number of players on this char-serv
  133. */
  134. void chmapif_sendall_playercount(int32 users){
  135. uint8 buf[6];
  136. // send number of players to all map-servers
  137. WBUFW(buf,0) = 0x2b00;
  138. WBUFL(buf,2) = users;
  139. chmapif_sendall(buf,6);
  140. }
  141. /**
  142. * Send some misc info to new map-server.
  143. * - Server name for whisper name
  144. * - Default map
  145. * HZ 0x2afb <size>.W <status>.B <whisper name>.24B <server name>.24B
  146. * @param fd
  147. **/
  148. void chmapif_send_misc(int32 fd) {
  149. uint16 offs = 5;
  150. unsigned char buf[5+NAME_LENGTH+NAME_LENGTH];
  151. memset(buf, '\0', sizeof(buf));
  152. WBUFW(buf, 0) = 0x2afb;
  153. // 0 succes, 1:failure
  154. WBUFB(buf, 4) = 0;
  155. // Send name for wisp to player
  156. safestrncpy( WBUFCP( buf, offs ), charserv_config.wisp_server_name, NAME_LENGTH );
  157. offs += NAME_LENGTH;
  158. safestrncpy( WBUFCP( buf, offs ), charserv_config.server_name, sizeof( charserv_config.server_name ) );
  159. offs += NAME_LENGTH;
  160. // Length
  161. WBUFW(buf, 2) = offs;
  162. chmapif_send(fd, buf, offs);
  163. }
  164. /**
  165. * Sends maps to all map-server
  166. * HZ 0x2b04 <size>.W <ip>.L <port>.W { <map>.?B }.?B
  167. * @param fd
  168. * @param map_id
  169. * @param count Number of map from new map-server has
  170. **/
  171. void chmapif_send_maps( int32 fd, int32 map_id, size_t count, unsigned char* mapbuf ){
  172. uint16 x;
  173. if (count == 0) {
  174. ShowWarning("Map-server %d has NO maps.\n", map_id);
  175. }
  176. else {
  177. unsigned char buf[INT16_MAX];
  178. // Transmitting maps information to the other map-servers
  179. WBUFW(buf,0) = 0x2b04;
  180. WBUFW( buf, 2 ) = static_cast<int16>( count * MAP_NAME_LENGTH_EXT + 10 );
  181. WBUFL(buf,4) = htonl(map_server[map_id].ip);
  182. WBUFW(buf,8) = htons(map_server[map_id].port);
  183. memcpy( WBUFP( buf, 10 ), mapbuf, count * MAP_NAME_LENGTH_EXT );
  184. chmapif_sendallwos(fd, buf, WBUFW(buf,2));
  185. }
  186. // Transmitting the maps of the other map-servers to the new map-server
  187. for (x = 0; x < ARRAYLENGTH(map_server); x++) {
  188. if (session_isValid(map_server[x].fd) && x != map_id) {
  189. WFIFOHEAD( fd, 10 + MAP_NAME_LENGTH_EXT * map_server[x].maps.size() );
  190. WFIFOW(fd,0) = 0x2b04;
  191. WFIFOL(fd,4) = htonl(map_server[x].ip);
  192. WFIFOW(fd,8) = htons(map_server[x].port);
  193. uint16 j = 0;
  194. for( std::string& map : map_server[x].maps ){
  195. safestrncpy( WFIFOCP( fd, 10 + j * MAP_NAME_LENGTH_EXT ), map.c_str(), MAP_NAME_LENGTH_EXT );
  196. j++;
  197. }
  198. if (j > 0) {
  199. WFIFOW( fd, 2 ) = j * MAP_NAME_LENGTH_EXT + 10;
  200. WFIFOSET(fd,WFIFOW(fd,2));
  201. }
  202. }
  203. }
  204. }
  205. /**
  206. * This function is called when the map-serv initialise is chrif interface.
  207. * Map-serv sent us his map indexes so we can transfert a player from a map-serv to another when necessary
  208. * We reply by sending back the char_serv_wisp_name fame list and
  209. * @param fd: wich fd to parse from
  210. * @param id: wich map_serv id
  211. * @return : 0 not enough data received, 1 success
  212. */
  213. int32 chmapif_parse_getmapname(int32 fd, int32 id){
  214. int32 i = 0;
  215. unsigned char *mapbuf;
  216. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  217. return 0;
  218. //Retain what map-index that map-serv contains
  219. map_server[id].maps.clear();
  220. for( int32 i = 4; i < RFIFOW( fd, 2 ); i += MAP_NAME_LENGTH_EXT ){
  221. char mapname[MAP_NAME_LENGTH_EXT];
  222. safestrncpy( mapname, RFIFOCP( fd, i ), sizeof( mapname ) );
  223. map_server[id].maps.push_back( mapname );
  224. }
  225. mapbuf = RFIFOP(fd,4);
  226. RFIFOSKIP(fd,RFIFOW(fd,2));
  227. ShowStatus("Map-Server %d connected: %" PRIuPTR " maps, from IP %d.%d.%d.%d port %d.\n",
  228. id, map_server[id].maps.size(), CONVIP(map_server[id].ip), map_server[id].port);
  229. ShowStatus("Map-server %d loading complete.\n", id);
  230. chmapif_send_misc(fd);
  231. chmapif_send_fame_list(fd); //Send fame list.
  232. chmapif_send_maps(fd, id, map_server[id].maps.size(), mapbuf);
  233. return 1;
  234. }
  235. /**
  236. * Map-serv requesting to send the list of sc_data the player has saved
  237. * @author [Skotlex]
  238. * @param fd: wich fd to parse from
  239. * @return : 0 not enough data received, 1 success
  240. */
  241. int32 chmapif_parse_askscdata(int32 fd){
  242. if (RFIFOREST(fd) < 10)
  243. return 0;
  244. else {
  245. #ifdef ENABLE_SC_SAVING
  246. int32 aid, cid;
  247. aid = RFIFOL(fd,2);
  248. cid = RFIFOL(fd,6);
  249. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT type, tick, val1, val2, val3, val4 from `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",
  250. schema_config.scdata_db, aid, cid) )
  251. {
  252. Sql_ShowDebug(sql_handle);
  253. return 1;
  254. }
  255. if( Sql_NumRows(sql_handle) > 0 )
  256. {
  257. struct status_change_data scdata;
  258. int32 count;
  259. char* data;
  260. WFIFOHEAD(fd,14+50*sizeof(struct status_change_data));
  261. WFIFOW(fd,0) = 0x2b1d;
  262. WFIFOL(fd,4) = aid;
  263. WFIFOL(fd,8) = cid;
  264. for( count = 0; count < 50 && SQL_SUCCESS == Sql_NextRow(sql_handle); ++count )
  265. {
  266. Sql_GetData(sql_handle, 0, &data, nullptr); scdata.type = atoi(data);
  267. Sql_GetData(sql_handle, 1, &data, nullptr); scdata.tick = strtoll( data, nullptr, 10 );
  268. Sql_GetData(sql_handle, 2, &data, nullptr); scdata.val1 = atoi(data);
  269. Sql_GetData(sql_handle, 3, &data, nullptr); scdata.val2 = atoi(data);
  270. Sql_GetData(sql_handle, 4, &data, nullptr); scdata.val3 = atoi(data);
  271. Sql_GetData(sql_handle, 5, &data, nullptr); scdata.val4 = atoi(data);
  272. memcpy(WFIFOP(fd, 14+count*sizeof(struct status_change_data)), &scdata, sizeof(struct status_change_data));
  273. }
  274. if (count >= 50)
  275. ShowWarning("Too many status changes for %d:%d, some of them were not loaded.\n", aid, cid);
  276. if (count > 0)
  277. {
  278. WFIFOW( fd, 2 ) = static_cast<int16>( 14 + count * sizeof( struct status_change_data ) );
  279. WFIFOW(fd,12) = count;
  280. WFIFOSET(fd,WFIFOW(fd,2));
  281. }
  282. } else { // No Status Changes to load but still send a response
  283. WFIFOHEAD(fd,14);
  284. WFIFOW(fd,0) = 0x2b1d;
  285. WFIFOW(fd,2) = 14;
  286. WFIFOL(fd,4) = aid;
  287. WFIFOL(fd,8) = cid;
  288. WFIFOW(fd,12) = 0;
  289. WFIFOSET(fd,WFIFOW(fd,2));
  290. }
  291. Sql_FreeResult(sql_handle);
  292. #endif
  293. RFIFOSKIP(fd, 10);
  294. }
  295. return 1;
  296. }
  297. /**
  298. * Map-serv sent us his new users count, updating info
  299. * @param fd: wich fd to parse from
  300. * @param id: wich map_serv id
  301. * @return : 0 not enough data received, 1 success
  302. */
  303. int32 chmapif_parse_getusercount(int32 fd, int32 id){
  304. if (RFIFOREST(fd) < 4)
  305. return 0;
  306. if (RFIFOW(fd,2) != map_server[id].users) {
  307. map_server[id].users = RFIFOW(fd,2);
  308. ShowInfo("User Count: %d (Server: %d)\n", map_server[id].users, id);
  309. }
  310. RFIFOSKIP(fd, 4);
  311. return 1;
  312. }
  313. /**
  314. * Map-serv sent us all his users info, (aid and cid) so we can update online_char_db
  315. * @param fd: wich fd to parse from
  316. * @param id: wich map_serv id
  317. * @return : 0 not enough data received, 1 success
  318. */
  319. int32 chmapif_parse_regmapuser(int32 fd, int32 id){
  320. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2))
  321. return 0;
  322. else {
  323. //TODO: When data mismatches memory, update guild/party online/offline states.
  324. map_server[id].users = RFIFOW(fd,4);
  325. // Set all chars from this server as 'unknown'
  326. for( const auto& pair : char_get_onlinedb() ){
  327. char_db_setoffline( pair.second, id );
  328. }
  329. for( int32 i = 0; i < map_server[id].users; i++ ){
  330. uint32 aid = RFIFOL(fd,6+i*8);
  331. uint32 cid = RFIFOL(fd,6+i*8+4);
  332. std::shared_ptr<struct online_char_data> character = util::umap_find( char_get_onlinedb(), aid );
  333. if( character != nullptr ){
  334. if( character->server > -1 && character->server != id ){
  335. ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n",
  336. character->account_id, character->char_id, character->server, id, aid, cid);
  337. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 2);
  338. }
  339. }else{
  340. character = std::make_shared<struct online_char_data>( aid );
  341. char_get_onlinedb()[aid] = character;
  342. }
  343. character->server = id;
  344. character->char_id = cid;
  345. }
  346. //If any chars remain in -2, they will be cleaned in the cleanup timer.
  347. RFIFOSKIP(fd,RFIFOW(fd,2));
  348. }
  349. return 1;
  350. }
  351. /**
  352. * Map-serv request to save mmo_char_status in sql
  353. * Receive character data from map-server for saving
  354. * @param fd: wich fd to parse from
  355. * @param id: wich map_serv id
  356. * @return : 0 not enough data received, 1 success
  357. */
  358. int32 chmapif_parse_reqsavechar(int32 fd, int32 id){
  359. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  360. return 0;
  361. else {
  362. uint32 aid = RFIFOL( fd, 4 ), cid = RFIFOL( fd, 8 );
  363. uint16 size = RFIFOW( fd, 2 );
  364. if (size - 13 != sizeof(struct mmo_charstatus))
  365. {
  366. ShowError("parse_from_map (save-char): Size mismatch! %d != %" PRIuPTR "\n", size-13, sizeof(struct mmo_charstatus));
  367. RFIFOSKIP(fd,size);
  368. return 1;
  369. }
  370. std::shared_ptr<struct online_char_data> character = util::umap_find( char_get_onlinedb(), aid );
  371. //Check account only if this ain't final save. Final-save goes through because of the char-map reconnect
  372. if( RFIFOB( fd, 12 ) || RFIFOB( fd, 13 ) || ( character != nullptr && character->char_id == cid ) ){
  373. struct mmo_charstatus char_dat;
  374. memcpy(&char_dat, RFIFOP(fd,13), sizeof(struct mmo_charstatus));
  375. char_mmo_char_tosql(cid, &char_dat);
  376. } else { //This may be valid on char-server reconnection, when re-sending characters that already logged off.
  377. ShowError("parse_from_map (save-char): Received data for non-existant/offline character (%d:%d).\n", aid, cid);
  378. char_set_char_online(id, cid, aid);
  379. }
  380. if (RFIFOB(fd,12))
  381. { //Flag, set character offline after saving. [Skotlex]
  382. char_set_char_offline(cid, aid);
  383. WFIFOHEAD(fd,10);
  384. WFIFOW(fd,0) = 0x2b21; //Save ack only needed on final save.
  385. WFIFOL(fd,2) = aid;
  386. WFIFOL(fd,6) = cid;
  387. WFIFOSET(fd,10);
  388. }
  389. RFIFOSKIP(fd,size);
  390. }
  391. return 1;
  392. }
  393. /**
  394. * Inform mapserv of a new character selection request
  395. * @param fd : FD link tomapserv
  396. * @param aid : Player account id
  397. * @param res : result, 0=not ok, 1=ok
  398. */
  399. void chmapif_charselres(int32 fd, uint32 aid, uint8 res){
  400. WFIFOHEAD(fd,7);
  401. WFIFOW(fd,0) = 0x2b03;
  402. WFIFOL(fd,2) = aid;
  403. WFIFOB(fd,6) = res;
  404. WFIFOSET(fd,7);
  405. }
  406. /**
  407. * Player Requesting char-select from map_serv
  408. * @param fd: wich fd to parse from
  409. * @return : 0 not enough data received, 1 success
  410. */
  411. int32 chmapif_parse_authok(int32 fd){
  412. if( RFIFOREST(fd) < 18 )
  413. return 0;
  414. else{
  415. uint32 account_id = RFIFOL(fd,2);
  416. uint32 login_id1 = RFIFOL(fd,6);
  417. uint32 login_id2 = RFIFOL(fd,10);
  418. uint32 ip = RFIFOL(fd,14);
  419. RFIFOSKIP(fd,18);
  420. if( !global_core->is_running() ){
  421. chmapif_charselres(fd,account_id,0);
  422. }else{
  423. // create temporary auth entry
  424. std::shared_ptr<struct auth_node> node = std::make_shared<struct auth_node>();
  425. node->account_id = account_id;
  426. node->char_id = 0;
  427. node->login_id1 = login_id1;
  428. node->login_id2 = login_id2;
  429. //node->sex = 0;
  430. node->ip = ntohl(ip);
  431. //node->expiration_time = 0; // unlimited/unknown time by default (not display in map-server)
  432. //node->gmlevel = 0;
  433. char_get_authdb()[node->account_id] = node;
  434. //Set char to "@ char select" in online db [Kevin]
  435. char_set_charselect(account_id);
  436. std::shared_ptr<struct online_char_data> character = util::umap_find( char_get_onlinedb(), account_id );
  437. if( character != nullptr ){
  438. character->pincode_success = true;
  439. }
  440. chmapif_charselres(fd,account_id,1);
  441. }
  442. }
  443. return 1;
  444. }
  445. //Request to save skill cooldown data
  446. int32 chmapif_parse_req_saveskillcooldown(int32 fd){
  447. if( RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2) )
  448. return 0;
  449. else {
  450. int32 count, aid, cid;
  451. aid = RFIFOL(fd,4);
  452. cid = RFIFOL(fd,8);
  453. count = RFIFOW(fd,12);
  454. if( count > 0 )
  455. {
  456. struct skill_cooldown_data data;
  457. StringBuf buf;
  458. int32 i;
  459. StringBuf_Init(&buf);
  460. StringBuf_Printf(&buf, "INSERT INTO `%s` (`account_id`, `char_id`, `skill`, `tick`) VALUES ", schema_config.skillcooldown_db);
  461. for( i = 0; i < count; ++i )
  462. {
  463. memcpy(&data,RFIFOP(fd,14+i*sizeof(struct skill_cooldown_data)),sizeof(struct skill_cooldown_data));
  464. if( i > 0 )
  465. StringBuf_AppendStr(&buf, ", ");
  466. StringBuf_Printf(&buf, "('%d','%d','%d','%" PRtf "')", aid, cid, data.skill_id, data.tick);
  467. }
  468. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  469. Sql_ShowDebug(sql_handle);
  470. StringBuf_Destroy(&buf);
  471. }
  472. RFIFOSKIP(fd, RFIFOW(fd, 2));
  473. }
  474. return 1;
  475. }
  476. //Request skillcooldown data 0x2b0a
  477. int32 chmapif_parse_req_skillcooldown(int32 fd){
  478. if (RFIFOREST(fd) < 10)
  479. return 0;
  480. else {
  481. int32 aid, cid;
  482. aid = RFIFOL(fd,2);
  483. cid = RFIFOL(fd,6);
  484. RFIFOSKIP(fd, 10);
  485. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT skill, tick FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",
  486. schema_config.skillcooldown_db, aid, cid) )
  487. {
  488. Sql_ShowDebug(sql_handle);
  489. return 1;
  490. }
  491. if( Sql_NumRows(sql_handle) > 0 )
  492. {
  493. int32 count;
  494. char* data;
  495. struct skill_cooldown_data scd;
  496. WFIFOHEAD(fd,14 + MAX_SKILLCOOLDOWN * sizeof(struct skill_cooldown_data));
  497. WFIFOW(fd,0) = 0x2b0b;
  498. WFIFOL(fd,4) = aid;
  499. WFIFOL(fd,8) = cid;
  500. for( count = 0; count < MAX_SKILLCOOLDOWN && SQL_SUCCESS == Sql_NextRow(sql_handle); ++count )
  501. {
  502. Sql_GetData(sql_handle, 0, &data, nullptr); scd.skill_id = atoi(data);
  503. Sql_GetData(sql_handle, 1, &data, nullptr); scd.tick = strtoll( data, nullptr, 10 );
  504. memcpy(WFIFOP(fd,14+count*sizeof(struct skill_cooldown_data)), &scd, sizeof(struct skill_cooldown_data));
  505. }
  506. if( count >= MAX_SKILLCOOLDOWN )
  507. ShowWarning("Too many skillcooldowns for %d:%d, some of them were not loaded.\n", aid, cid);
  508. if( count > 0 )
  509. {
  510. WFIFOW( fd, 2 ) = static_cast<int16>( 14 + count * sizeof( struct skill_cooldown_data ) );
  511. WFIFOW(fd,12) = count;
  512. WFIFOSET(fd,WFIFOW(fd,2));
  513. //Clear the data once loaded.
  514. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", schema_config.skillcooldown_db, aid, cid) )
  515. Sql_ShowDebug(sql_handle);
  516. }
  517. }
  518. Sql_FreeResult(sql_handle);
  519. }
  520. return 1;
  521. }
  522. /**
  523. * Inform the mapserv, of a change mapserv request
  524. * @param fd :Link to mapserv
  525. * @param nok : 0=accepted or no=1
  526. */
  527. void chmapif_changemapserv_ack(int32 fd, bool nok){
  528. // TODO: Refactor... You crazy *** [Lemongrass]
  529. WFIFOHEAD( fd, 28 + MAP_NAME_LENGTH_EXT );
  530. WFIFOW(fd,0) = 0x2b06;
  531. memcpy( WFIFOP( fd, 2 ), RFIFOP( fd, 2 ), 26 + MAP_NAME_LENGTH_EXT );
  532. if(nok)
  533. WFIFOL(fd,6) = 0; //Set login1 to 0.(not ok)
  534. WFIFOSET( fd, 28 + MAP_NAME_LENGTH_EXT );
  535. }
  536. /**
  537. * Player requesting to change map-serv
  538. * @param fd: wich fd to parse from
  539. * @return : 0 not enough data received, 1 success
  540. */
  541. int32 chmapif_parse_reqchangemapserv(int32 fd){
  542. if( RFIFOREST( fd ) < ( 37 + MAP_NAME_LENGTH_EXT ) ){
  543. return 0;
  544. }
  545. else {
  546. int32 map_id, map_fd = -1;
  547. struct mmo_charstatus char_dat;
  548. int32 offset = 18 + MAP_NAME_LENGTH_EXT;
  549. map_id = char_search_mapserver( RFIFOCP( fd, 18 ), ntohl( RFIFOL( fd, offset + 4 ) ), ntohs( RFIFOW( fd, offset + 8 ) ) ); //Locate mapserver by ip and port.
  550. if (map_id >= 0)
  551. map_fd = map_server[map_id].fd;
  552. uint32 char_id = RFIFOL( fd, 14 );
  553. // Char should just had been saved before this packet, so this should be safe. [Skotlex]
  554. std::shared_ptr<struct mmo_charstatus> char_data = util::umap_find( char_get_chardb(), char_id );
  555. // Really shouldn't happen.
  556. if( char_data == nullptr ){
  557. char_mmo_char_fromsql( char_id, &char_dat, true );
  558. char_data = util::umap_find( char_get_chardb(), char_id );
  559. }
  560. if( global_core->is_running() &&
  561. session_isActive(map_fd) &&
  562. char_data )
  563. { //Send the map server the auth of this player.
  564. uint32 aid = RFIFOL( fd, 2 );
  565. //Update the "last map" as this is where the player must be spawned on the new map server.
  566. safestrncpy( char_data->last_point.map, RFIFOCP( fd, 18 ), MAP_NAME_LENGTH_EXT );
  567. char_data->last_point.x = RFIFOW( fd, offset + 0 );
  568. char_data->last_point.y = RFIFOW( fd, offset + 2 );
  569. char_data->sex = RFIFOB( fd, offset + 10 );
  570. // create temporary auth entry
  571. std::shared_ptr<struct auth_node> node = std::make_shared<struct auth_node>();
  572. node->account_id = aid;
  573. node->char_id = char_id;
  574. node->login_id1 = RFIFOL(fd,6);
  575. node->login_id2 = RFIFOL(fd,10);
  576. node->sex = char_data->sex;
  577. node->expiration_time = 0; // FIXME (this thing isn't really supported we could as well purge it instead of fixing)
  578. node->ip = ntohl( RFIFOL( fd, offset + 11 ) );
  579. node->group_id = RFIFOL( fd, offset + 15 );
  580. node->changing_mapservers = 1;
  581. char_get_authdb()[node->account_id] = node;
  582. std::shared_ptr<struct online_char_data> data = util::umap_find( char_get_onlinedb(), aid );
  583. if( data == nullptr ){
  584. data = std::make_shared<struct online_char_data>( aid );
  585. char_get_onlinedb()[aid] = data;
  586. }
  587. data->char_id = char_data->char_id;
  588. data->server = map_id; //Update server where char is.
  589. //Reply with an ack.
  590. chmapif_changemapserv_ack(fd,0);
  591. } else { //Reply with nak
  592. chmapif_changemapserv_ack(fd,1);
  593. }
  594. RFIFOSKIP( fd, 37 + MAP_NAME_LENGTH_EXT );
  595. }
  596. return 1;
  597. }
  598. /**
  599. * Player requesting to remove friend from list
  600. * Remove RFIFOL(fd,6) (friend_id) from RFIFOL(fd,2) (char_id) friend list
  601. * @author [Ind]
  602. * @param fd: wich fd to parse from
  603. * @return : 0 not enough data received, 1 success
  604. */
  605. int32 chmapif_parse_askrmfriend(int32 fd){
  606. if (RFIFOREST(fd) < 10)
  607. return 0;
  608. else {
  609. uint32 char_id, friend_id;
  610. char_id = RFIFOL(fd,2);
  611. friend_id = RFIFOL(fd,6);
  612. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `friend_id`='%d' LIMIT 1",
  613. schema_config.friend_db, char_id, friend_id) ) {
  614. Sql_ShowDebug(sql_handle);
  615. return 1;
  616. }
  617. RFIFOSKIP(fd,10);
  618. }
  619. return 1;
  620. }
  621. /**
  622. * Lookup to search if that char_id correspond to a name.
  623. * Comming from map-serv to search on other map-serv
  624. * @param fd: wich fd to parse from
  625. * @return : 0 not enough data received, 1 success
  626. */
  627. int32 chmapif_parse_reqcharname(int32 fd){
  628. if (RFIFOREST(fd) < 6)
  629. return 0;
  630. WFIFOHEAD(fd,30);
  631. WFIFOW(fd,0) = 0x2b09;
  632. WFIFOL(fd,2) = RFIFOL(fd,2);
  633. char_loadName((int)RFIFOL(fd,2), WFIFOCP(fd,6));
  634. WFIFOSET(fd,30);
  635. RFIFOSKIP(fd,6);
  636. return 1;
  637. }
  638. /**
  639. * Forward an email update request to login-serv
  640. * Map server send information to change an email of an account -> login-server
  641. * @param fd: wich fd to parse from
  642. * @return : 0 not enough data received, 1 success
  643. */
  644. int32 chmapif_parse_reqnewemail(int32 fd){
  645. if (RFIFOREST(fd) < 86)
  646. return 0;
  647. if (chlogif_isconnected()) { // don't send request if no login-server
  648. WFIFOHEAD(login_fd,86);
  649. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0),86); // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  650. WFIFOW(login_fd,0) = 0x2722;
  651. WFIFOSET(login_fd,86);
  652. }
  653. RFIFOSKIP(fd, 86);
  654. return 1;
  655. }
  656. /**
  657. * Forward a change of status for account to login-serv
  658. * @param fd: which fd to parse from
  659. * @return : 0 not enough data received, 1 success
  660. */
  661. int32 chmapif_parse_fwlog_changestatus(int32 fd){
  662. if (RFIFOREST(fd) < 44)
  663. return 0;
  664. else {
  665. int32 result = 0; // 0-login-server request done, 1-player not found, 2-gm level too low, 3-login-server offline, 4-current group level > VIP group level
  666. char esc_name[NAME_LENGTH*2+1];
  667. char answer = true;
  668. int32 aid = RFIFOL(fd,2); // account_id of who ask (-1 if server itself made this request)
  669. const char* name = RFIFOCP(fd,6); // name of the target character
  670. int32 operation = RFIFOW(fd,30); // type of operation @see enum chrif_req_op
  671. int32 timediff = 0;
  672. int32 val1 = 0, sex = SEX_MALE;
  673. if (operation == CHRIF_OP_LOGIN_BAN || operation == CHRIF_OP_LOGIN_VIP) {
  674. timediff = RFIFOL(fd, 32);
  675. val1 = RFIFOL(fd, 36);
  676. } else if (operation == CHRIF_OP_CHANGECHARSEX)
  677. sex = RFIFOB(fd, 32);
  678. RFIFOSKIP(fd,44);
  679. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  680. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`, `char_id` FROM `%s` WHERE `name` = '%s'", schema_config.char_db, esc_name) )
  681. Sql_ShowDebug(sql_handle);
  682. else if( Sql_NumRows(sql_handle) == 0 ) {
  683. result = 1; // 1-player not found
  684. }
  685. else if( SQL_SUCCESS != Sql_NextRow(sql_handle) ) {
  686. Sql_ShowDebug(sql_handle);
  687. result = 1;
  688. } else {
  689. int32 t_aid; // target account id
  690. int32 t_cid; // target char id
  691. char* data;
  692. Sql_GetData(sql_handle, 0, &data, nullptr); t_aid = atoi(data);
  693. Sql_GetData(sql_handle, 1, &data, nullptr); t_cid = atoi(data);
  694. Sql_FreeResult(sql_handle);
  695. if(!chlogif_isconnected())
  696. result = 3; // 3-login-server offline
  697. //FIXME: need to move this check to login server [ultramage]
  698. // if( acc != -1 && isGM(acc) < isGM(account_id) )
  699. // result = 2; // 2-gm level too low
  700. else {
  701. //! NOTE: See src/char/chrif.hpp::enum chrif_req_op for the number
  702. switch( operation ) {
  703. case CHRIF_OP_LOGIN_BLOCK: // block
  704. WFIFOHEAD(login_fd,10);
  705. WFIFOW(login_fd,0) = 0x2724;
  706. WFIFOL(login_fd,2) = t_aid;
  707. WFIFOL(login_fd,6) = 5; // new account status
  708. WFIFOSET(login_fd,10);
  709. break;
  710. case CHRIF_OP_LOGIN_BAN: // ban
  711. WFIFOHEAD(login_fd,10);
  712. WFIFOW(login_fd, 0) = 0x2725;
  713. WFIFOL(login_fd, 2) = t_aid;
  714. WFIFOL(login_fd, 6) = timediff;
  715. WFIFOSET(login_fd,10);
  716. break;
  717. case CHRIF_OP_LOGIN_UNBLOCK: // unblock
  718. WFIFOHEAD(login_fd,10);
  719. WFIFOW(login_fd,0) = 0x2724;
  720. WFIFOL(login_fd,2) = t_aid;
  721. WFIFOL(login_fd,6) = 0; // new account status
  722. WFIFOSET(login_fd,10);
  723. break;
  724. case CHRIF_OP_LOGIN_UNBAN: // unban
  725. WFIFOHEAD(login_fd,6);
  726. WFIFOW(login_fd,0) = 0x272a;
  727. WFIFOL(login_fd,2) = t_aid;
  728. WFIFOSET(login_fd,6);
  729. break;
  730. case CHRIF_OP_LOGIN_CHANGESEX: // changesex
  731. answer = false;
  732. WFIFOHEAD(login_fd,6);
  733. WFIFOW(login_fd,0) = 0x2727;
  734. WFIFOL(login_fd,2) = t_aid;
  735. WFIFOSET(login_fd,6);
  736. break;
  737. case CHRIF_OP_LOGIN_VIP: // vip
  738. answer = (val1&4); // vip_req val1=type, &1 login send return, &2 update timestamp, &4 map send answer
  739. chlogif_reqvipdata(t_aid, val1, timediff, fd);
  740. break;
  741. case CHRIF_OP_CHANGECHARSEX: // changecharsex
  742. answer = false;
  743. chlogif_parse_ackchangecharsex(t_cid, sex);
  744. break;
  745. } //end switch operation
  746. } //login is connected
  747. }
  748. // send answer if a player asks, not if the server asks
  749. if( aid != -1 && answer) { // Don't send answer for changesex/changecharsex
  750. WFIFOHEAD(fd,34);
  751. WFIFOW(fd, 0) = 0x2b0f;
  752. WFIFOL(fd, 2) = aid;
  753. safestrncpy(WFIFOCP(fd,6), name, NAME_LENGTH);
  754. WFIFOW(fd,30) = operation;
  755. WFIFOW(fd,32) = result;
  756. WFIFOSET(fd,34);
  757. }
  758. }
  759. return 1;
  760. }
  761. /**
  762. * Transmit the acknowledgement of divorce of partner_id1 and partner_id2
  763. * Update the list associated and transmit the new ranking
  764. * @param partner_id1: char id1 divorced
  765. * @param partner_id2: char id2 divorced
  766. */
  767. void chmapif_send_ackdivorce(int32 partner_id1, int32 partner_id2){
  768. unsigned char buf[11];
  769. WBUFW(buf,0) = 0x2b12;
  770. WBUFL(buf,2) = partner_id1;
  771. WBUFL(buf,6) = partner_id2;
  772. chmapif_sendall(buf,10);
  773. }
  774. /**
  775. * Received a divorce request
  776. * @param fd: wich fd to parse from
  777. * @return : 0 not enough data received, 1 success
  778. */
  779. int32 chmapif_parse_reqdivorce(int32 fd){
  780. if( RFIFOREST(fd) < 10 )
  781. return 0;
  782. char_divorce_char_sql(RFIFOL(fd,2), RFIFOL(fd,6));
  783. RFIFOSKIP(fd,10);
  784. return 1;
  785. }
  786. /**
  787. * Character disconnected set online 0
  788. * @author [Wizputer]
  789. * @param fd: wich fd to parse from
  790. * @return : 0 not enough data received, 1 success
  791. */
  792. int32 chmapif_parse_setcharoffline(int32 fd){
  793. if (RFIFOREST(fd) < 6)
  794. return 0;
  795. char_set_char_offline(RFIFOL(fd,2),RFIFOL(fd,6));
  796. RFIFOSKIP(fd,10);
  797. return 1;
  798. }
  799. /**
  800. * Reset all chars to offline
  801. * @author [Wizputer]
  802. * @param fd: wich fd to parse from
  803. * @param id: wich map_serv id
  804. * @return : 0 not enough data received, 1 success
  805. */
  806. int32 chmapif_parse_setalloffline(int32 fd, int32 id){
  807. char_set_all_offline(id);
  808. RFIFOSKIP(fd,2);
  809. return 1;
  810. }
  811. /**
  812. * Character set online
  813. * @author [Wizputer]
  814. * @param fd: wich fd to parse from
  815. * @param id: wich map_serv id
  816. * @return : 0 not enough data received, 1 success
  817. */
  818. int32 chmapif_parse_setcharonline(int32 fd, int32 id){
  819. if (RFIFOREST(fd) < 10)
  820. return 0;
  821. char_set_char_online(id, RFIFOL(fd,2),RFIFOL(fd,6));
  822. RFIFOSKIP(fd,10);
  823. return 1;
  824. }
  825. /**
  826. * Build and send fame ranking lists
  827. * @author [DracoRPG]
  828. * @param fd: wich fd to parse from
  829. * @param id: wich map_serv id
  830. * @return : 0 not enough data received, 1 success
  831. */
  832. int32 chmapif_parse_reqfamelist(int32 fd){
  833. if (RFIFOREST(fd) < 2)
  834. return 0;
  835. char_read_fame_list();
  836. chmapif_send_fame_list(-1);
  837. RFIFOSKIP(fd,2);
  838. return 1;
  839. }
  840. /**
  841. * Request to save status change data.s
  842. * @author [Skotlex]
  843. * @param fd: wich fd to parse from
  844. * @return : 0 not enough data received, 1 success
  845. */
  846. int32 chmapif_parse_save_scdata(int32 fd){
  847. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  848. return 0;
  849. {
  850. #ifdef ENABLE_SC_SAVING
  851. int32 count, aid, cid;
  852. aid = RFIFOL(fd, 4);
  853. cid = RFIFOL(fd, 8);
  854. count = RFIFOW(fd, 12);
  855. // Whatever comes from the mapserver, now is the time to drop previous entries
  856. if( Sql_Query( sql_handle, "DELETE FROM `%s` where `account_id` = %d and `char_id` = %d;", schema_config.scdata_db, aid, cid ) != SQL_SUCCESS ){
  857. Sql_ShowDebug( sql_handle );
  858. }
  859. else if( count > 0 )
  860. {
  861. struct status_change_data data;
  862. StringBuf buf;
  863. int32 i;
  864. StringBuf_Init(&buf);
  865. StringBuf_Printf(&buf, "INSERT INTO `%s` (`account_id`, `char_id`, `type`, `tick`, `val1`, `val2`, `val3`, `val4`) VALUES ", schema_config.scdata_db);
  866. for( i = 0; i < count; ++i )
  867. {
  868. memcpy (&data, RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data));
  869. if( i > 0 )
  870. StringBuf_AppendStr(&buf, ", ");
  871. StringBuf_Printf(&buf, "('%d','%d','%hu','%" PRtf "','%ld','%ld','%ld','%ld')", aid, cid,
  872. data.type, data.tick, data.val1, data.val2, data.val3, data.val4);
  873. }
  874. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  875. Sql_ShowDebug(sql_handle);
  876. StringBuf_Destroy(&buf);
  877. }
  878. #endif
  879. RFIFOSKIP(fd, RFIFOW(fd, 2));
  880. }
  881. return 1;
  882. }
  883. /**
  884. * map-server keep alive packet, awnser back map that we alive as well
  885. * @param fd: wich fd to parse from
  886. * @return : 0 not enough data received, 1 success
  887. */
  888. int32 chmapif_parse_keepalive(int32 fd){
  889. WFIFOHEAD(fd,2);
  890. WFIFOW(fd,0) = 0x2b24;
  891. WFIFOSET(fd,2);
  892. RFIFOSKIP(fd,2);
  893. return 1;
  894. }
  895. /**
  896. * auth request from map-server
  897. * @param fd: wich fd to parse from
  898. * @return : 0 not enough data received, 1 success
  899. */
  900. int32 chmapif_parse_reqauth(int32 fd, int32 id){
  901. if (RFIFOREST(fd) < 20)
  902. return 0;
  903. else {
  904. uint32 account_id;
  905. uint32 char_id;
  906. uint32 login_id1;
  907. unsigned char sex;
  908. uint32 ip;
  909. struct mmo_charstatus char_dat;
  910. bool autotrade;
  911. account_id = RFIFOL(fd,2);
  912. char_id = RFIFOL(fd,6);
  913. login_id1 = RFIFOL(fd,10);
  914. sex = RFIFOB(fd,14);
  915. ip = ntohl(RFIFOL(fd,15));
  916. autotrade = RFIFOB(fd,19) != 0;
  917. RFIFOSKIP(fd,20);
  918. std::shared_ptr<struct auth_node> node = util::umap_find( char_get_authdb(), account_id );
  919. std::shared_ptr<struct mmo_charstatus> cd = util::umap_find( char_get_chardb(), char_id );
  920. if( cd == nullptr ){
  921. // Really shouldn't happen. (or autotrade)
  922. char_mmo_char_fromsql( char_id, &char_dat, true );
  923. cd = util::umap_find( char_get_chardb(), char_id );
  924. }
  925. if( global_core->is_running() && autotrade && cd ){
  926. uint16 mmo_charstatus_len = sizeof(struct mmo_charstatus) + 25;
  927. WFIFOHEAD(fd,mmo_charstatus_len);
  928. WFIFOW(fd,0) = 0x2afd;
  929. WFIFOW(fd,2) = mmo_charstatus_len;
  930. WFIFOL(fd,4) = account_id;
  931. WFIFOL(fd,8) = 0;
  932. WFIFOL(fd,12) = 0;
  933. WFIFOL(fd,16) = 0;
  934. WFIFOL(fd,20) = 0;
  935. WFIFOB(fd,24) = 0;
  936. memcpy( WFIFOP( fd, 25 ), cd.get(), sizeof(struct mmo_charstatus));
  937. WFIFOSET(fd, WFIFOW(fd,2));
  938. char_set_char_online(id, char_id, account_id);
  939. } else if( global_core->is_running() &&
  940. cd != nullptr &&
  941. node != nullptr &&
  942. node->account_id == account_id &&
  943. node->char_id == char_id &&
  944. node->login_id1 == login_id1
  945. //&& node->ip == ip
  946. #if PACKETVER < 20141016
  947. && node->sex == sex
  948. #endif
  949. )
  950. {// auth ok
  951. uint16 mmo_charstatus_len = sizeof(struct mmo_charstatus) + 25;
  952. WFIFOHEAD(fd,mmo_charstatus_len);
  953. WFIFOW(fd,0) = 0x2afd;
  954. WFIFOW(fd,2) = mmo_charstatus_len;
  955. WFIFOL(fd,4) = account_id;
  956. WFIFOL(fd,8) = node->login_id1;
  957. WFIFOL(fd,12) = node->login_id2;
  958. WFIFOL(fd,16) = (uint32)node->expiration_time; // FIXME: will wrap to negative after "19-Jan-2038, 03:14:07 AM GMT"
  959. WFIFOL(fd,20) = node->group_id;
  960. WFIFOB(fd,24) = node->changing_mapservers;
  961. memcpy( WFIFOP( fd, 25 ), cd.get(), sizeof( struct mmo_charstatus ) );
  962. WFIFOSET(fd, WFIFOW(fd,2));
  963. // only use the auth once and mark user online
  964. char_get_authdb().erase( account_id );
  965. char_set_char_online(id, char_id, account_id);
  966. } else {// auth failed
  967. WFIFOHEAD(fd,19);
  968. WFIFOW(fd,0) = 0x2b27;
  969. WFIFOL(fd,2) = account_id;
  970. WFIFOL(fd,6) = char_id;
  971. WFIFOL(fd,10) = login_id1;
  972. WFIFOB(fd,14) = sex;
  973. WFIFOL(fd,15) = htonl(ip);
  974. WFIFOSET(fd,19);
  975. }
  976. }
  977. return 1;
  978. }
  979. /**
  980. * ip address update
  981. * @param fd: wich fd to parse from
  982. * @return : 0 not enough data received, 1 success
  983. */
  984. int32 chmapif_parse_updmapip(int32 fd, int32 id){
  985. if (RFIFOREST(fd) < 6)
  986. return 0;
  987. map_server[id].ip = ntohl(RFIFOL(fd, 2));
  988. ShowInfo("Updated IP address of map-server #%d to %d.%d.%d.%d.\n", id, CONVIP(map_server[id].ip));
  989. RFIFOSKIP(fd,6);
  990. return 1;
  991. }
  992. /**
  993. * Received an update of fame point for char_id cid
  994. * Update the list associated and transmit the new ranking
  995. * @param fd: wich fd to parse from
  996. * @return : 0 not enough data received, 1 success
  997. */
  998. int32 chmapif_parse_updfamelist(int32 fd){
  999. if (RFIFOREST(fd) < 11)
  1000. return 0;
  1001. {
  1002. int32 cid = RFIFOL(fd, 2);
  1003. int32 fame = RFIFOL(fd, 6);
  1004. char type = RFIFOB(fd, 10);
  1005. int32 size;
  1006. struct fame_list* list;
  1007. int32 player_pos;
  1008. int32 fame_pos;
  1009. switch(type)
  1010. {
  1011. case RANK_BLACKSMITH: size = fame_list_size_smith; list = smith_fame_list; break;
  1012. case RANK_ALCHEMIST: size = fame_list_size_chemist; list = chemist_fame_list; break;
  1013. case RANK_TAEKWON: size = fame_list_size_taekwon; list = taekwon_fame_list; break;
  1014. default: size = 0; list = nullptr; break;
  1015. }
  1016. ARR_FIND(0, size, player_pos, list[player_pos].id == cid);// position of the player
  1017. ARR_FIND(0, size, fame_pos, list[fame_pos].fame <= fame);// where the player should be
  1018. if( player_pos == size && fame_pos == size )
  1019. ;// not on list and not enough fame to get on it
  1020. else if( fame_pos == player_pos )
  1021. {// same position
  1022. list[player_pos].fame = fame;
  1023. chmapif_update_fame_list(type, player_pos, fame);
  1024. }
  1025. else
  1026. {// move in the list
  1027. if( player_pos == size )
  1028. {// new ranker - not in the list
  1029. ARR_MOVE(size - 1, fame_pos, list, struct fame_list);
  1030. list[fame_pos].id = cid;
  1031. list[fame_pos].fame = fame;
  1032. char_loadName(cid, list[fame_pos].name);
  1033. }
  1034. else
  1035. {// already in the list
  1036. if( fame_pos == size )
  1037. --fame_pos;// move to the end of the list
  1038. ARR_MOVE(player_pos, fame_pos, list, struct fame_list);
  1039. list[fame_pos].fame = fame;
  1040. }
  1041. chmapif_send_fame_list(-1);
  1042. }
  1043. RFIFOSKIP(fd,11);
  1044. }
  1045. return 1;
  1046. }
  1047. /*
  1048. * HZ 0x2b2b
  1049. * Transmist vip data to mapserv
  1050. */
  1051. int32 chmapif_vipack(int32 mapfd, uint32 aid, uint32 vip_time, uint32 groupid, uint8 flag) {
  1052. #ifdef VIP_ENABLE
  1053. uint8 buf[15];
  1054. WBUFW(buf,0) = 0x2b2b;
  1055. WBUFL(buf,2) = aid;
  1056. WBUFL(buf,6) = vip_time;
  1057. WBUFL(buf,10) = groupid;
  1058. WBUFB(buf,14) = flag;
  1059. chmapif_send(mapfd,buf,15); // inform the mapserv back
  1060. #endif
  1061. return 0;
  1062. }
  1063. int32 chmapif_parse_reqcharban(int32 fd){
  1064. if (RFIFOREST(fd) < 10+NAME_LENGTH)
  1065. return 0;
  1066. else {
  1067. //int32 aid = RFIFOL(fd,2); aid of player who as requested the ban
  1068. int32 timediff = RFIFOL(fd,6);
  1069. const char* name = RFIFOCP(fd,10); // name of the target character
  1070. char esc_name[NAME_LENGTH*2+1];
  1071. RFIFOSKIP(fd,10+NAME_LENGTH);
  1072. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1073. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`char_id`,`unban_time` FROM `%s` WHERE `name` = '%s'", schema_config.char_db, esc_name) )
  1074. Sql_ShowDebug(sql_handle);
  1075. else if( Sql_NumRows(sql_handle) == 0 ){
  1076. return 1; // 1-player not found
  1077. }
  1078. else if( SQL_SUCCESS != Sql_NextRow(sql_handle) ){
  1079. Sql_ShowDebug(sql_handle);
  1080. Sql_FreeResult(sql_handle);
  1081. return 1;
  1082. } else {
  1083. int32 t_cid=0,t_aid=0;
  1084. char* data;
  1085. time_t unban_time;
  1086. time_t now = time(nullptr);
  1087. SqlStmt stmt{ *sql_handle };
  1088. Sql_GetData(sql_handle, 0, &data, nullptr); t_aid = atoi(data);
  1089. Sql_GetData(sql_handle, 1, &data, nullptr); t_cid = atoi(data);
  1090. Sql_GetData(sql_handle, 2, &data, nullptr); unban_time = atol(data);
  1091. Sql_FreeResult(sql_handle);
  1092. if(timediff<0 && unban_time==0)
  1093. return 1; //attemp to reduce time of a non banned account ?!?
  1094. else if(unban_time<now) unban_time=now; //new entry
  1095. unban_time += timediff; //alterate the time
  1096. if( unban_time < now ) unban_time=0; //we have totally reduce the time
  1097. if( SQL_SUCCESS != stmt.Prepare(
  1098. "UPDATE `%s` SET `unban_time` = ? WHERE `char_id` = ? LIMIT 1",
  1099. schema_config.char_db)
  1100. || SQL_SUCCESS != stmt.BindParam(0, SQLDT_LONG, (void*)&unban_time, sizeof(unban_time))
  1101. || SQL_SUCCESS != stmt.BindParam(1, SQLDT_INT, (void*)&t_cid, sizeof(t_cid))
  1102. || SQL_SUCCESS != stmt.Execute()
  1103. )
  1104. {
  1105. SqlStmt_ShowDebug(stmt);
  1106. return 1;
  1107. }
  1108. // condition applies; send to all map-servers to disconnect the player
  1109. if( unban_time > now ) {
  1110. unsigned char buf[11];
  1111. WBUFW(buf,0) = 0x2b14;
  1112. WBUFL(buf,2) = t_cid;
  1113. WBUFB(buf,6) = 2;
  1114. WBUFL(buf,7) = (uint32)unban_time;
  1115. chmapif_sendall(buf, 11);
  1116. // disconnect player if online on char-server
  1117. char_disconnect_player(t_aid);
  1118. }
  1119. }
  1120. }
  1121. return 1;
  1122. }
  1123. int32 chmapif_parse_reqcharunban(int32 fd){
  1124. if (RFIFOREST(fd) < 6+NAME_LENGTH)
  1125. return 0;
  1126. else {
  1127. const char* name = RFIFOCP(fd,6);
  1128. char esc_name[NAME_LENGTH*2+1];
  1129. RFIFOSKIP(fd,6+NAME_LENGTH);
  1130. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1131. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `unban_time` = '0' WHERE `name` = '%s' LIMIT 1", schema_config.char_db, esc_name) ) {
  1132. Sql_ShowDebug(sql_handle);
  1133. return 1;
  1134. }
  1135. }
  1136. return 1;
  1137. }
  1138. /**
  1139. * ZA 0x2b2d
  1140. * <cmd>.W <char_id>.L
  1141. * AZ 0x2b2f
  1142. * <cmd>.W <len>.W <cid>.L <count>.B { <bonus_script_data>.?B }
  1143. * Get bonus_script data(s) from table to load then send to player
  1144. * @param fd
  1145. * @author [Cydh]
  1146. **/
  1147. int32 chmapif_bonus_script_get(int32 fd) {
  1148. if (RFIFOREST(fd) < 6)
  1149. return 0;
  1150. else {
  1151. uint8 num_rows = 0;
  1152. uint32 cid = RFIFOL(fd,2);
  1153. struct bonus_script_data tmp_bsdata;
  1154. SqlStmt stmt{ *sql_handle };
  1155. RFIFOSKIP(fd,6);
  1156. if (SQL_ERROR == stmt.Prepare(
  1157. "SELECT `script`, `tick`, `flag`, `type`, `icon` FROM `%s` WHERE `char_id` = '%d' LIMIT %d",
  1158. schema_config.bonus_script_db, cid, MAX_PC_BONUS_SCRIPT) ||
  1159. SQL_ERROR == stmt.Execute() ||
  1160. SQL_ERROR == stmt.BindColumn(0, SQLDT_STRING, &tmp_bsdata.script_str, sizeof(tmp_bsdata.script_str), nullptr, nullptr) ||
  1161. SQL_ERROR == stmt.BindColumn(1, SQLDT_INT64, &tmp_bsdata.tick, 0, nullptr, nullptr) ||
  1162. SQL_ERROR == stmt.BindColumn(2, SQLDT_UINT16, &tmp_bsdata.flag, 0, nullptr, nullptr) ||
  1163. SQL_ERROR == stmt.BindColumn(3, SQLDT_UINT8, &tmp_bsdata.type, 0, nullptr, nullptr) ||
  1164. SQL_ERROR == stmt.BindColumn(4, SQLDT_INT16, &tmp_bsdata.icon, 0, nullptr, nullptr)
  1165. )
  1166. {
  1167. SqlStmt_ShowDebug(stmt);
  1168. return 1;
  1169. }
  1170. if ((num_rows = (uint8)stmt.NumRows()) > 0) {
  1171. uint8 i;
  1172. uint32 size = 9 + num_rows * sizeof(struct bonus_script_data);
  1173. WFIFOHEAD(fd, size);
  1174. WFIFOW(fd, 0) = 0x2b2f;
  1175. WFIFOW(fd, 2) = size;
  1176. WFIFOL(fd, 4) = cid;
  1177. WFIFOB(fd, 8) = num_rows;
  1178. for (i = 0; i < num_rows && SQL_SUCCESS == stmt.NextRow(); i++) {
  1179. struct bonus_script_data bsdata;
  1180. memset(&bsdata, 0, sizeof(bsdata));
  1181. memset(bsdata.script_str, '\0', sizeof(bsdata.script_str));
  1182. safestrncpy(bsdata.script_str, tmp_bsdata.script_str, strlen(tmp_bsdata.script_str)+1);
  1183. bsdata.tick = tmp_bsdata.tick;
  1184. bsdata.flag = tmp_bsdata.flag;
  1185. bsdata.type = tmp_bsdata.type;
  1186. bsdata.icon = tmp_bsdata.icon;
  1187. memcpy(WFIFOP(fd, 9 + i * sizeof(struct bonus_script_data)), &bsdata, sizeof(struct bonus_script_data));
  1188. }
  1189. WFIFOSET(fd, size);
  1190. ShowInfo("Bonus Script loaded for CID=%d. Total: %d.\n", cid, i);
  1191. if (SQL_ERROR == stmt.Prepare("DELETE FROM `%s` WHERE `char_id`='%d'",schema_config.bonus_script_db,cid) ||
  1192. SQL_ERROR == stmt.Execute())
  1193. SqlStmt_ShowDebug(stmt);
  1194. }
  1195. }
  1196. return 1;
  1197. }
  1198. /**
  1199. * ZA 0x2b2e
  1200. * <cmd>.W <len>.W <char_id>.L <count>.B { <bonus_script>.?B }
  1201. * Save bonus_script data(s) to the table
  1202. * @param fd
  1203. * @author [Cydh]
  1204. **/
  1205. int32 chmapif_bonus_script_save(int32 fd) {
  1206. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1207. return 0;
  1208. else {
  1209. uint32 cid = RFIFOL(fd,4);
  1210. uint8 count = RFIFOB(fd,8);
  1211. if (SQL_ERROR == Sql_Query(sql_handle,"DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.bonus_script_db, cid))
  1212. Sql_ShowDebug(sql_handle);
  1213. if (count > 0) {
  1214. char esc_script[MAX_BONUS_SCRIPT_LENGTH*2];
  1215. struct bonus_script_data bsdata;
  1216. StringBuf buf;
  1217. uint8 i;
  1218. StringBuf_Init(&buf);
  1219. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `script`, `tick`, `flag`, `type`, `icon`) VALUES ", schema_config.bonus_script_db);
  1220. for (i = 0; i < count; ++i) {
  1221. memcpy(&bsdata, RFIFOP(fd, 9 + i*sizeof(struct bonus_script_data)), sizeof(struct bonus_script_data));
  1222. Sql_EscapeString(sql_handle, esc_script, bsdata.script_str);
  1223. if (i > 0)
  1224. StringBuf_AppendStr(&buf,", ");
  1225. StringBuf_Printf(&buf, "('%d','%s','%" PRtf "','%d','%d','%d')", cid, esc_script, bsdata.tick, bsdata.flag, bsdata.type, bsdata.icon);
  1226. }
  1227. if (SQL_ERROR == Sql_QueryStr(sql_handle,StringBuf_Value(&buf)))
  1228. Sql_ShowDebug(sql_handle);
  1229. StringBuf_Destroy(&buf);
  1230. ShowInfo("Bonus Script saved for CID=%d. Total: %d.\n", cid, count);
  1231. }
  1232. RFIFOSKIP(fd,RFIFOW(fd,2));
  1233. }
  1234. return 1;
  1235. }
  1236. /**
  1237. * Inform the mapserv wheater his login attemp to us was a success or not
  1238. * @param fd : file descriptor to parse, (link to mapserv)
  1239. * @param errCode 0:success, 3:fail
  1240. */
  1241. void chmapif_connectack(int32 fd, uint8 errCode){
  1242. WFIFOHEAD(fd,3);
  1243. WFIFOW(fd,0) = 0x2af9;
  1244. WFIFOB(fd,2) = errCode;
  1245. WFIFOSET(fd,3);
  1246. }
  1247. /**
  1248. * Entry point from map-server to char-server.
  1249. * Function that checks incoming command, then splits it to the correct handler.
  1250. * If not found any hander here transmis packet to inter
  1251. * @param fd: file descriptor to parse, (link to map-serv)
  1252. * @return 0=invalid server,marked for disconnection,unknow packet; 1=success
  1253. */
  1254. int32 chmapif_parse(int32 fd){
  1255. int32 id; //mapserv id
  1256. ARR_FIND( 0, ARRAYLENGTH(map_server), id, map_server[id].fd == fd );
  1257. if( id == ARRAYLENGTH(map_server) )
  1258. {// not a map server
  1259. ShowDebug("parse_frommap: Disconnecting invalid session #%d (is not a map-server)\n", fd);
  1260. do_close(fd);
  1261. return 0;
  1262. }
  1263. if( session[fd]->flag.eof )
  1264. {
  1265. do_close(fd);
  1266. map_server[id].fd = -1;
  1267. chmapif_on_disconnect(id);
  1268. return 0;
  1269. }
  1270. while(RFIFOREST(fd) >= 2){
  1271. int32 next=1;
  1272. switch(RFIFOW(fd,0)){
  1273. case 0x2afa: next=chmapif_parse_getmapname(fd,id); break;
  1274. case 0x2afc: next=chmapif_parse_askscdata(fd); break;
  1275. case 0x2afe: next=chmapif_parse_getusercount(fd,id); break; //get nb user
  1276. case 0x2aff: next=chmapif_parse_regmapuser(fd,id); break; //register users
  1277. case 0x2b01: next=chmapif_parse_reqsavechar(fd,id); break;
  1278. case 0x2b02: next=chmapif_parse_authok(fd); break;
  1279. case 0x2b05: next=chmapif_parse_reqchangemapserv(fd); break;
  1280. case 0x2b07: next=chmapif_parse_askrmfriend(fd); break;
  1281. case 0x2b08: next=chmapif_parse_reqcharname(fd); break;
  1282. case 0x2b0a: next=chmapif_parse_req_skillcooldown(fd); break;
  1283. case 0x2b0c: next=chmapif_parse_reqnewemail(fd); break;
  1284. case 0x2b0e: next=chmapif_parse_fwlog_changestatus(fd); break;
  1285. case 0x2b10: next=chmapif_parse_updfamelist(fd); break;
  1286. case 0x2b11: next=chmapif_parse_reqdivorce(fd); break;
  1287. case 0x2b13: next=chmapif_parse_updmapip(fd,id); break;
  1288. case 0x2b15: next=chmapif_parse_req_saveskillcooldown(fd); break;
  1289. case 0x2b17: next=chmapif_parse_setcharoffline(fd); break;
  1290. case 0x2b18: next=chmapif_parse_setalloffline(fd,id); break;
  1291. case 0x2b19: next=chmapif_parse_setcharonline(fd,id); break;
  1292. case 0x2b1a: next=chmapif_parse_reqfamelist(fd); break;
  1293. case 0x2b1c: next=chmapif_parse_save_scdata(fd); break;
  1294. case 0x2b23: next=chmapif_parse_keepalive(fd); break;
  1295. case 0x2b26: next=chmapif_parse_reqauth(fd,id); break;
  1296. case 0x2b28: next=chmapif_parse_reqcharban(fd); break; //charban
  1297. case 0x2b2a: next=chmapif_parse_reqcharunban(fd); break; //charunban
  1298. //case 0x2b2c: /*free*/; break;
  1299. case 0x2b2d: next=chmapif_bonus_script_get(fd); break; //Load data
  1300. case 0x2b2e: next=chmapif_bonus_script_save(fd); break;//Save data
  1301. default:
  1302. {
  1303. // inter server - packet
  1304. int32 r = inter_parse_frommap(fd);
  1305. if (r == 1) break; // processed
  1306. if (r == 2) return 0; // need more packet
  1307. // no inter server packet. no char server packet -> disconnect
  1308. ShowError("Unknown packet 0x%04x from map server, disconnecting.\n", RFIFOW(fd,0));
  1309. set_eof(fd);
  1310. return 0;
  1311. }
  1312. } // switch
  1313. if(next==0) return 0; //avoid processing rest of packet
  1314. } // while
  1315. return 1;
  1316. }
  1317. // Initialization process (currently only initialization inter_mapif)
  1318. int32 chmapif_init(int32 fd){
  1319. return inter_mapif_init(fd);
  1320. }
  1321. /**
  1322. * Initializes a server structure.
  1323. * @param id: id of map-serv (should be >0, FIXME)
  1324. */
  1325. void chmapif_server_init(int32 id) {
  1326. map_server[id] = {};
  1327. map_server[id].fd = -1;
  1328. }
  1329. /**
  1330. * Destroys a server structure.
  1331. * @param id: id of map-serv (should be >0, FIXME)
  1332. */
  1333. void chmapif_server_destroy(int32 id){
  1334. if( map_server[id].fd == -1 ){
  1335. do_close(map_server[id].fd);
  1336. map_server[id].fd = -1;
  1337. }
  1338. }
  1339. /**
  1340. * chmapif constructor
  1341. * Initialisation, function called at start of the char-serv.
  1342. */
  1343. void do_init_chmapif(void){
  1344. int32 i;
  1345. for( i = 0; i < ARRAYLENGTH(map_server); ++i )
  1346. chmapif_server_init(i);
  1347. }
  1348. /**
  1349. * Resets all the data related to a server.
  1350. * Actually destroys then recreates the struct.
  1351. * @param id: id of map-serv (should be >0, FIXME)
  1352. */
  1353. void chmapif_server_reset(int32 id){
  1354. int32 j = 0;
  1355. unsigned char buf[INT16_MAX];
  1356. int32 fd = map_server[id].fd;
  1357. //Notify other map servers that this one is gone. [Skotlex]
  1358. WBUFW(buf,0) = 0x2b20;
  1359. WBUFL(buf,4) = htonl(map_server[id].ip);
  1360. WBUFW(buf,8) = htons(map_server[id].port);
  1361. for( std::string& map : map_server[id].maps ){
  1362. safestrncpy( WBUFCP( buf, 10 + j * MAP_NAME_LENGTH_EXT ), map.c_str(), MAP_NAME_LENGTH_EXT );
  1363. j++;
  1364. }
  1365. if (j > 0) {
  1366. WBUFW(buf,2) = j * MAP_NAME_LENGTH_EXT + 10;
  1367. chmapif_sendallwos(fd, buf, WBUFW(buf,2));
  1368. }
  1369. // Tag relevant chars as 'in disconnected' server.
  1370. for( const auto& pair : char_get_onlinedb() ){
  1371. char_db_setoffline( pair.second, id );
  1372. }
  1373. chmapif_server_destroy(id);
  1374. chmapif_server_init(id);
  1375. }
  1376. /**
  1377. * Called when the connection to Map Server is disconnected.
  1378. * @param id: id of map-serv (should be >0, FIXME)
  1379. */
  1380. void chmapif_on_disconnect(int32 id){
  1381. ShowStatus("Map-server #%d has disconnected.\n", id);
  1382. chmapif_server_reset(id);
  1383. }
  1384. /**
  1385. * chmapif destructor
  1386. * dealloc..., function called at exit of the char-serv
  1387. */
  1388. void do_final_chmapif(void){
  1389. int32 i;
  1390. for( i = 0; i < ARRAYLENGTH(map_server); ++i )
  1391. chmapif_server_destroy(i);
  1392. }