9
3

EQStream.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifdef WIN32
  17. #include <WinSock2.h>
  18. #include <windows.h>
  19. #endif
  20. #include "debug.h"
  21. #include <string>
  22. #include <iomanip>
  23. #include <iostream>
  24. #include <vector>
  25. #include <time.h>
  26. #include <sys/types.h>
  27. #ifdef WIN32
  28. #include <time.h>
  29. #else
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <sys/time.h>
  33. #include <sys/socket.h>
  34. #include <netdb.h>
  35. #include <fcntl.h>
  36. #include <arpa/inet.h>
  37. #endif
  38. #include "EQPacket.h"
  39. #include "EQStream.h"
  40. #include "EQStreamFactory.h"
  41. #include "misc.h"
  42. #include "Mutex.h"
  43. #include "op_codes.h"
  44. #include "CRC16.h"
  45. #include "packet_dump.h"
  46. #ifdef LOGIN
  47. #include "../LoginServer/login_structs.h"
  48. #endif
  49. #include "EQ2_Common_Structs.h"
  50. #include "Log.h"
  51. uint16 EQStream::MaxWindowSize=2048;
  52. void EQStream::init(bool resetSession) {
  53. if (resetSession)
  54. {
  55. streamactive = false;
  56. sessionAttempts = 0;
  57. }
  58. timeout_delays = 0;
  59. MInUse.lock();
  60. active_users = 0;
  61. MInUse.unlock();
  62. Session=0;
  63. Key=0;
  64. MaxLen=0;
  65. NextInSeq=0;
  66. NextOutSeq=0;
  67. CombinedAppPacket=NULL;
  68. MAcks.lock();
  69. MaxAckReceived = -1;
  70. NextAckToSend = -1;
  71. LastAckSent = -1;
  72. MAcks.unlock();
  73. LastSeqSent=-1;
  74. MaxSends=5;
  75. LastPacket=Timer::GetCurrentTime2();
  76. oversize_buffer=NULL;
  77. oversize_length=0;
  78. oversize_offset=0;
  79. Factory = NULL;
  80. MRate.lock();
  81. RateThreshold=RATEBASE/250;
  82. DecayRate=DECAYBASE/250;
  83. MRate.unlock();
  84. BytesWritten=0;
  85. SequencedBase = 0;
  86. AverageDelta = 500;
  87. crypto->setRC4Key(0);
  88. retransmittimer = Timer::GetCurrentTime2();
  89. retransmittimeout = 500 * RETRANSMIT_TIMEOUT_MULT;
  90. if (uint16(SequencedBase + SequencedQueue.size()) != NextOutSeq) {
  91. LogWrite(PACKET__DEBUG, 9, "Packet", "init Invalid Sequenced queue: BS %u + SQ %u != NOS %u", SequencedBase, SequencedQueue.size(), NextOutSeq);
  92. }
  93. }
  94. EQStream::EQStream(sockaddr_in addr){
  95. crypto = new Crypto();
  96. resend_que_timer = new Timer(1000);
  97. combine_timer = new Timer(250); //250 milliseconds
  98. combine_timer->Start();
  99. resend_que_timer->Start();
  100. init();
  101. remote_ip=addr.sin_addr.s_addr;
  102. remote_port=addr.sin_port;
  103. State=CLOSED;
  104. StreamType=UnknownStream;
  105. compressed=true;
  106. encoded=false;
  107. app_opcode_size=2;
  108. #ifdef WIN32
  109. ZeroMemory(&stream, sizeof(z_stream));
  110. #else
  111. bzero(&stream, sizeof(z_stream));
  112. #endif
  113. stream.zalloc = (alloc_func)0;
  114. stream.zfree = (free_func)0;
  115. stream.opaque = (voidpf)0;
  116. deflateInit2(&stream, 9, Z_DEFLATED, 13, 9, Z_DEFAULT_STRATEGY);
  117. //deflateInit(&stream, 5);
  118. compressed_offset = 0;
  119. client_version = 0;
  120. received_packets = 0;
  121. sent_packets = 0;
  122. }
  123. EQProtocolPacket* EQStream::ProcessEncryptedData(uchar* data, int32 size, int16 opcode){
  124. //cout << "B4:\n";
  125. //DumpPacket(data, size);
  126. /*if(size >= 2 && data[0] == 0 && data[1] == 0){
  127. cout << "Attempting to fix packet!\n";
  128. //Have to fix bad packet from client or it will screw up encryption :P
  129. size--;
  130. data++;
  131. }*/
  132. crypto->RC4Decrypt(data,size);
  133. int8 offset = 0;
  134. if(data[0] == 0xFF && size > 2){
  135. offset = 3;
  136. memcpy(&opcode, data+sizeof(int8), sizeof(int16));
  137. }
  138. else{
  139. offset = 1;
  140. memcpy(&opcode, data, sizeof(int8));
  141. }
  142. //cout << "After:\n";
  143. //DumpPacket(data, size);
  144. return new EQProtocolPacket(opcode, data+offset, size - offset);
  145. }
  146. EQProtocolPacket* EQStream::ProcessEncryptedPacket(EQProtocolPacket *p){
  147. EQProtocolPacket* ret = NULL;
  148. if(p->opcode == OP_Packet && p->size > 2)
  149. ret = ProcessEncryptedData(p->pBuffer+2, p->size-2, p->opcode);
  150. else
  151. ret = ProcessEncryptedData(p->pBuffer, p->size, p->opcode);
  152. return ret;
  153. }
  154. bool EQStream::HandleEmbeddedPacket(EQProtocolPacket *p, int16 offset, int16 length){
  155. if(p && p->size >= ((uint32)(offset+2))){
  156. if(p->pBuffer[offset] == 0 && p->pBuffer[offset+1] == 0x19){
  157. if(length == 0)
  158. length = p->size-2-offset;
  159. else
  160. length-=2;
  161. #ifdef LE_DEBUG
  162. LogWrite(PACKET__DEBUG, 0, "Packet", "Creating OP_AppCombined Packet!");
  163. #endif
  164. EQProtocolPacket *subp=new EQProtocolPacket(OP_AppCombined, p->pBuffer+2+offset, length);
  165. subp->copyInfo(p);
  166. ProcessPacket(subp);
  167. safe_delete(subp);
  168. return true;
  169. }
  170. else if(p->pBuffer[offset] == 0 && p->pBuffer[offset+1] == 0){
  171. if(length == 0)
  172. length = p->size-1-offset;
  173. else
  174. length--;
  175. #ifdef LE_DEBUG
  176. LogWrite(PACKET__DEBUG, 0, "Packet", "Creating Opcode 0 Packet!");
  177. DumpPacket(p->pBuffer+1+offset, length);
  178. #endif
  179. EQProtocolPacket* newpacket = ProcessEncryptedData(p->pBuffer+1+offset, length, OP_Packet);
  180. if(newpacket){
  181. #ifdef LE_DEBUG
  182. LogWrite(PACKET__DEBUG, 0, "Packet", "Result: ");
  183. DumpPacket(newpacket);
  184. #endif
  185. EQApplicationPacket *ap = newpacket->MakeApplicationPacket(2);
  186. InboundQueuePush(ap);
  187. safe_delete(newpacket);
  188. }
  189. else
  190. LogWrite(PACKET__ERROR, 0, "Packet", "No Packet!");
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. void EQStream::ProcessPacket(EQProtocolPacket *p)
  197. {
  198. uint32 processed=0,subpacket_length=0;
  199. if (p == nullptr)
  200. return;
  201. if (p->opcode > 0xff) {
  202. p->opcode = htons(p->opcode); //byte order is backwards in the protocol packet
  203. EQApplicationPacket* ap = p->MakeApplicationPacket(2);
  204. if (ap)
  205. InboundQueuePush(ap);
  206. LogWrite(PACKET__ERROR, 0, "Packet", "*** Received 0xff opcode, updated to %i",p->opcode);
  207. return;
  208. }
  209. if (p) {
  210. if (p->opcode!=OP_SessionRequest && p->opcode!=OP_SessionResponse && !Session) {
  211. #ifdef EQN_DEBUG
  212. LogWrite(PACKET__ERROR, 0, "Packet", "*** Session not initialized, packet ignored ");
  213. //p->DumpRaw();
  214. #endif
  215. return;
  216. }
  217. //cout << "Received " << (int)p->opcode << ":\n";
  218. //DumpPacket(p->pBuffer, p->size);
  219. switch (p->opcode) {
  220. case OP_Combined: {
  221. processed=0;
  222. int8 offset = 0;
  223. int count = 0;
  224. #ifdef LE_DEBUG
  225. LogWrite(PACKET__DEBUG, 0, "Packet", "OP_Combined: ");
  226. DumpPacket(p);
  227. #endif
  228. while(processed<p->size) {
  229. if ((subpacket_length=(unsigned char)*(p->pBuffer+processed))==0xff) {
  230. subpacket_length = ntohs(*(uint16*)(p->pBuffer + processed + 1));
  231. offset = 3;
  232. }
  233. else
  234. offset = 1;
  235. count++;
  236. #ifdef LE_DEBUG
  237. LogWrite(PACKET__DEBUG, 0, "Packet", "OP_Combined Packet %i (%u) (%u): ", count, subpacket_length, processed);
  238. #endif
  239. bool isSubPacket = EQProtocolPacket::IsProtocolPacket(p->pBuffer + processed + offset, subpacket_length, false);
  240. if (isSubPacket) {
  241. EQProtocolPacket* subp = new EQProtocolPacket(p->pBuffer + processed + offset, subpacket_length);
  242. subp->copyInfo(p);
  243. //I've seen some garbage packets get sent with wrong protocol opcodes but the rest of the combine is still correct
  244. //So don't break if GetProtocolPacket fails
  245. #ifdef LE_DEBUG
  246. LogWrite(PACKET__DEBUG, 0, "Packet", "Opcode %i:", subp->opcode);
  247. DumpPacket(subp);
  248. #endif
  249. ProcessPacket(subp);
  250. #ifdef LE_DEBUG
  251. DumpPacket(subp);
  252. #endif
  253. delete subp;
  254. }
  255. else if (ntohs(*reinterpret_cast<uint16_t*>(p->pBuffer + processed + offset)) > 0x1e) {
  256. //Garbage packet?
  257. crypto->RC4Decrypt(p->pBuffer + processed + offset, subpacket_length);
  258. LogWrite(PACKET__ERROR, 0, "Packet", "Garbage packet?!:");
  259. printf("!!!!!!!!!Garbage Packet!!!!!!!!!!!!!:\n");
  260. DumpPacket(p->pBuffer + processed + offset, subpacket_length);
  261. }
  262. processed+=subpacket_length+offset;
  263. }
  264. break;
  265. }
  266. case OP_AppCombined: {
  267. processed=0;
  268. EQProtocolPacket* newpacket = 0;
  269. int8 offset = 0;
  270. #ifdef LE_DEBUG
  271. LogWrite(PACKET__DEBUG, 0, "Packet", "OP_AppCombined: ");
  272. DumpPacket(p);
  273. #endif
  274. int count = 0;
  275. while(processed<p->size) {
  276. count++;
  277. if ((subpacket_length=(unsigned char)*(p->pBuffer+processed))==0xff) {
  278. subpacket_length=ntohs(*(uint16 *)(p->pBuffer+processed+1));
  279. offset = 3;
  280. } else
  281. offset = 1;
  282. if(crypto->getRC4Key()==0 && p->size >= 70){
  283. processRSAKey(p);
  284. }
  285. else if(crypto->isEncrypted()){
  286. #ifdef LE_DEBUG
  287. LogWrite(PACKET__DEBUG, 0, "Packet", "OP_AppCombined Packet %i (%u) (%u): ", count, subpacket_length, processed);
  288. DumpPacket(p->pBuffer+processed+offset, subpacket_length);
  289. #endif
  290. if(!HandleEmbeddedPacket(p, processed + offset, subpacket_length)){
  291. #ifdef LE_DEBUG
  292. LogWrite(PACKET__DEBUG, 0, "Packet", "OP_AppCombined Here:");
  293. #endif
  294. newpacket = ProcessEncryptedData(p->pBuffer+processed + offset, subpacket_length, OP_AppCombined);
  295. if(newpacket){
  296. #ifdef LE_DEBUG
  297. LogWrite(PACKET__DEBUG, 0, "Packet", "Opcode %i:", newpacket->opcode);
  298. DumpPacket(newpacket);
  299. #endif
  300. EQApplicationPacket* ap = newpacket->MakeApplicationPacket(2);
  301. #ifdef LE_DEBUG
  302. LogWrite(PACKET__DEBUG, 0, "Packet", "OP_AppCombined Here2:");
  303. DumpPacket(ap);
  304. #endif
  305. InboundQueuePush(ap);
  306. safe_delete(newpacket);
  307. }
  308. }
  309. }
  310. processed+=subpacket_length+offset;
  311. }
  312. }
  313. break;
  314. case OP_Packet: {
  315. if (!p->pBuffer || (p->Size() < 4))
  316. {
  317. break;
  318. }
  319. uint16 seq=ntohs(*(uint16 *)(p->pBuffer));
  320. sint8 check=CompareSequence(NextInSeq,seq);
  321. if (check == SeqFuture) {
  322. #ifdef EQN_DEBUG
  323. LogWrite(PACKET__DEBUG, 1, "Packet", "*** Future packet: Expecting Seq=%i, but got Seq=%i", NextInSeq, seq);
  324. LogWrite(PACKET__DEBUG, 1, "Packet", "[Start]");
  325. p->DumpRawHeader(seq);
  326. LogWrite(PACKET__DEBUG, 1, "Packet", "[End]");
  327. #endif
  328. OutOfOrderpackets[seq] = p->Copy();
  329. // Image (2020): Removed as this is bad contributes to infinite loop
  330. //SendOutOfOrderAck(seq);
  331. } else if (check == SeqPast) {
  332. #ifdef EQN_DEBUG
  333. LogWrite(PACKET__DEBUG, 1, "Packet", "*** Duplicate packet: Expecting Seq=%i, but got Seq=%i", NextInSeq, seq);
  334. LogWrite(PACKET__DEBUG, 1, "Packet", "[Start]");
  335. p->DumpRawHeader(seq);
  336. LogWrite(PACKET__DEBUG, 1, "Packet", "[End]");
  337. #endif
  338. // Image (2020): Removed as this is bad contributes to infinite loop
  339. //OutOfOrderpackets[seq] = p->Copy();
  340. SendOutOfOrderAck(seq);
  341. } else {
  342. EQProtocolPacket* qp = RemoveQueue(seq);
  343. if (qp) {
  344. LogWrite(PACKET__DEBUG, 1, "Packet", "OP_Fragment: Removing older queued packet with sequence %i", seq);
  345. delete qp;
  346. }
  347. SetNextAckToSend(seq);
  348. NextInSeq++;
  349. if(HandleEmbeddedPacket(p))
  350. break;
  351. if(crypto->getRC4Key()==0 && p && p->size >= 70){
  352. processRSAKey(p);
  353. }
  354. else if(crypto->isEncrypted() && p){
  355. EQProtocolPacket* newpacket = ProcessEncryptedPacket(p);
  356. if(newpacket){
  357. EQApplicationPacket *ap = newpacket->MakeApplicationPacket(2);
  358. InboundQueuePush(ap);
  359. safe_delete(newpacket);
  360. }
  361. }
  362. }
  363. }
  364. break;
  365. case OP_Fragment: {
  366. if (!p->pBuffer || (p->Size() < 4))
  367. {
  368. break;
  369. }
  370. uint16 seq=ntohs(*(uint16 *)(p->pBuffer));
  371. sint8 check=CompareSequence(NextInSeq,seq);
  372. if (check == SeqFuture) {
  373. #ifdef EQN_DEBUG
  374. LogWrite(PACKET__DEBUG, 1, "Packet", "*** Future packet2: Expecting Seq=%i, but got Seq=%i", NextInSeq, seq);
  375. LogWrite(PACKET__DEBUG, 1, "Packet", "[Start]");
  376. //p->DumpRawHeader(seq);
  377. LogWrite(PACKET__DEBUG, 1, "Packet", "[End]");
  378. #endif
  379. OutOfOrderpackets[seq] = p->Copy();
  380. //SendOutOfOrderAck(seq);
  381. } else if (check == SeqPast) {
  382. #ifdef EQN_DEBUG
  383. LogWrite(PACKET__DEBUG, 1, "Packet", "*** Duplicate packet2: Expecting Seq=%i, but got Seq=%i", NextInSeq, seq);
  384. LogWrite(PACKET__DEBUG, 1, "Packet", "[Start]");
  385. //p->DumpRawHeader(seq);
  386. LogWrite(PACKET__DEBUG, 1, "Packet", "[End]");
  387. #endif
  388. //OutOfOrderpackets[seq] = p->Copy();
  389. SendOutOfOrderAck(seq);
  390. } else {
  391. // In case we did queue one before as well.
  392. EQProtocolPacket* qp = RemoveQueue(seq);
  393. if (qp) {
  394. LogWrite(PACKET__DEBUG, 1, "Packet", "OP_Fragment: Removing older queued packet with sequence %i", seq);
  395. delete qp;
  396. }
  397. SetNextAckToSend(seq);
  398. NextInSeq++;
  399. if (oversize_buffer) {
  400. memcpy(oversize_buffer+oversize_offset,p->pBuffer+2,p->size-2);
  401. oversize_offset+=p->size-2;
  402. //cout << "Oversized is " << oversize_offset << "/" << oversize_length << " (" << (p->size-2) << ") Seq=" << seq << endl;
  403. if (oversize_offset==oversize_length) {
  404. if (*(p->pBuffer+2)==0x00 && *(p->pBuffer+3)==0x19) {
  405. EQProtocolPacket *subp=new EQProtocolPacket(oversize_buffer,oversize_offset);
  406. subp->copyInfo(p);
  407. ProcessPacket(subp);
  408. delete subp;
  409. } else {
  410. if(crypto->isEncrypted() && p && p->size > 2){
  411. EQProtocolPacket* p2 = ProcessEncryptedData(oversize_buffer, oversize_offset, p->opcode);
  412. EQApplicationPacket *ap = p2->MakeApplicationPacket(2);
  413. ap->copyInfo(p);
  414. InboundQueuePush(ap);
  415. safe_delete(p2);
  416. }
  417. }
  418. delete[] oversize_buffer;
  419. oversize_buffer=NULL;
  420. oversize_offset=0;
  421. }
  422. } else if (!oversize_buffer) {
  423. oversize_length=ntohl(*(uint32 *)(p->pBuffer+2));
  424. oversize_buffer=new unsigned char[oversize_length];
  425. memcpy(oversize_buffer,p->pBuffer+6,p->size-6);
  426. oversize_offset=p->size-6;
  427. //cout << "Oversized is " << oversize_offset << "/" << oversize_length << " (" << (p->size-6) << ") Seq=" << seq << endl;
  428. }
  429. }
  430. }
  431. break;
  432. case OP_KeepAlive: {
  433. #ifndef COLLECTOR
  434. NonSequencedPush(new EQProtocolPacket(p->opcode,p->pBuffer,p->size));
  435. #endif
  436. }
  437. break;
  438. case OP_Ack: {
  439. if (!p->pBuffer || (p->Size() < 4))
  440. {
  441. LogWrite(PACKET__DEBUG, 9, "Packet", "Received OP_Ack that was of malformed size");
  442. break;
  443. }
  444. uint16 seq = ntohs(*(uint16*)(p->pBuffer));
  445. AckPackets(seq);
  446. retransmittimer = Timer::GetCurrentTime2();
  447. }
  448. break;
  449. case OP_SessionRequest: {
  450. if (p->Size() < sizeof(SessionRequest))
  451. {
  452. break;
  453. }
  454. if (GetState() == ESTABLISHED) {
  455. //_log(NET__ERROR, _L "Received OP_SessionRequest in ESTABLISHED state (%d) streamactive (%i) attempt (%i)" __L, GetState(), streamactive, sessionAttempts);
  456. // client seems to try a max of 4 times (initial +3 retries) then gives up, giving it a few more attempts just in case
  457. // streamactive means we identified the opcode, we cannot re-establish this connection
  458. if (streamactive || (sessionAttempts > 30))
  459. {
  460. SendDisconnect(false);
  461. SetState(CLOSED);
  462. break;
  463. }
  464. }
  465. sessionAttempts++;
  466. init(GetState() != ESTABLISHED);
  467. OutboundQueueClear();
  468. SessionRequest *Request=(SessionRequest *)p->pBuffer;
  469. Session=ntohl(Request->Session);
  470. SetMaxLen(ntohl(Request->MaxLength));
  471. #ifndef COLLECTOR
  472. NextInSeq=0;
  473. Key=0x33624702;
  474. SendSessionResponse();
  475. #endif
  476. SetState(ESTABLISHED);
  477. }
  478. break;
  479. case OP_SessionResponse: {
  480. if (p->Size() < sizeof(SessionResponse))
  481. {
  482. break;
  483. }
  484. init();
  485. OutboundQueueClear();
  486. SetActive(true);
  487. SessionResponse *Response=(SessionResponse *)p->pBuffer;
  488. SetMaxLen(ntohl(Response->MaxLength));
  489. Key=ntohl(Response->Key);
  490. NextInSeq=0;
  491. SetState(ESTABLISHED);
  492. if (!Session)
  493. Session=ntohl(Response->Session);
  494. compressed=(Response->Format&FLAG_COMPRESSED);
  495. encoded=(Response->Format&FLAG_ENCODED);
  496. // Kinda kludgy, but trie for now
  497. if (compressed) {
  498. if (remote_port==9000 || (remote_port==0 && p->src_port==9000))
  499. SetStreamType(WorldStream);
  500. else
  501. SetStreamType(ZoneStream);
  502. } else if (encoded)
  503. SetStreamType(ChatOrMailStream);
  504. else
  505. SetStreamType(LoginStream);
  506. }
  507. break;
  508. case OP_SessionDisconnect: {
  509. //NextInSeq=0;
  510. SendDisconnect();
  511. //SetState(CLOSED);
  512. }
  513. break;
  514. case OP_OutOfOrderAck: {
  515. if (!p->pBuffer || (p->Size() < 4))
  516. {
  517. LogWrite(PACKET__DEBUG, 9, "Packet", "Received OP_OutOfOrderAck that was of malformed size");
  518. break;
  519. }
  520. uint16 seq = ntohs(*(uint16*)(p->pBuffer));
  521. MOutboundQueue.lock();
  522. if (uint16(SequencedBase + SequencedQueue.size()) != NextOutSeq) {
  523. LogWrite(PACKET__DEBUG, 9, "Packet", "Pre-OOA Invalid Sequenced queue: BS %u + SQ %u != NOS %u", SequencedBase, SequencedQueue.size(), NextOutSeq);
  524. }
  525. //if the packet they got out of order is between our last acked packet and the last sent packet, then its valid.
  526. if (CompareSequence(SequencedBase, seq) != SeqPast && CompareSequence(NextOutSeq, seq) == SeqPast) {
  527. uint16 sqsize = SequencedQueue.size();
  528. uint16 index = seq - SequencedBase;
  529. LogWrite(PACKET__DEBUG, 9, "Packet", "OP_OutOfOrderAck marking packet acked in queue (queue index = %u, queue size = %u)", index, sqsize);
  530. if (index < sqsize) {
  531. SequencedQueue[index]->acked = true;
  532. // flag packets for a resend
  533. uint16 count = 0;
  534. uint32 timeout = AverageDelta * 2 + 100;
  535. for (auto sitr = SequencedQueue.begin(); sitr != SequencedQueue.end() && count < index; ++sitr, ++count) {
  536. if (!(*sitr)->acked && (*sitr)->sent_time > 0 && (((*sitr)->sent_time + timeout) < Timer::GetCurrentTime2())) {
  537. (*sitr)->sent_time = 0;
  538. LogWrite(PACKET__DEBUG, 9, "Packet", "OP_OutOfOrderAck Flagging packet %u for retransmission", SequencedBase + count);
  539. }
  540. }
  541. }
  542. if (RETRANSMIT_TIMEOUT_MULT) {
  543. retransmittimer = Timer::GetCurrentTime2();
  544. }
  545. }
  546. else {
  547. LogWrite(PACKET__DEBUG, 9, "Packet", "Received OP_OutOfOrderAck for out-of-window %u. Window (%u->%u)", seq, SequencedBase, NextOutSeq);
  548. }
  549. if (uint16(SequencedBase + SequencedQueue.size()) != NextOutSeq) {
  550. LogWrite(PACKET__DEBUG, 9, "Packet", "Post-OOA Invalid Sequenced queue: BS %u + SQ %u != NOS %u", SequencedBase, SequencedQueue.size(), NextOutSeq);
  551. }
  552. MOutboundQueue.unlock();
  553. }
  554. break;
  555. case OP_ServerKeyRequest:{
  556. if (p->Size() < sizeof(ClientSessionStats))
  557. {
  558. //_log(NET__ERROR, _L "Received OP_SessionStatRequest that was of malformed size" __L);
  559. break;
  560. }
  561. ClientSessionStats* Stats = (ClientSessionStats*)p->pBuffer;
  562. int16 request_id = Stats->RequestID;
  563. AdjustRates(ntohl(Stats->average_delta));
  564. ServerSessionStats* stats=(ServerSessionStats*)p->pBuffer;
  565. memset(stats, 0, sizeof(ServerSessionStats));
  566. stats->RequestID = request_id;
  567. stats->current_time = ntohl(Timer::GetCurrentTime2());
  568. stats->sent_packets = ntohl(sent_packets);
  569. stats->sent_packets2 = ntohl(sent_packets);
  570. stats->received_packets = ntohl(received_packets);
  571. stats->received_packets2 = ntohl(received_packets);
  572. NonSequencedPush(new EQProtocolPacket(OP_SessionStatResponse,p->pBuffer,p->size));
  573. if(!crypto->isEncrypted())
  574. SendKeyRequest();
  575. }
  576. break;
  577. case OP_SessionStatResponse: {
  578. LogWrite(PACKET__INFO, 0, "Packet", "OP_SessionStatResponse");
  579. }
  580. break;
  581. case OP_OutOfSession: {
  582. LogWrite(PACKET__INFO, 0, "Packet", "OP_OutOfSession");
  583. SendDisconnect();
  584. SetState(CLOSED);
  585. }
  586. break;
  587. default:
  588. EQApplicationPacket *ap = p->MakeApplicationPacket(app_opcode_size);
  589. InboundQueuePush(ap);
  590. LogWrite(PACKET__INFO, 0, "Packet", "Received unknown packet type, disconnecting client");
  591. //SendDisconnect();
  592. break;
  593. }
  594. }
  595. }
  596. int8 EQStream::EQ2_Compress(EQ2Packet* app, int8 offset){
  597. #ifdef LE_DEBUG
  598. LogWrite(PACKET__DEBUG, 0, "Packet", "Before Compress in %s, line %i:", __FUNCTION__, __LINE__);
  599. DumpPacket(app);
  600. #endif
  601. uchar* pDataPtr = app->pBuffer + offset;
  602. uchar* deflate_buff = new uchar[app->size];
  603. MCompressData.lock();
  604. stream.next_in = pDataPtr;
  605. stream.avail_in = app->size - offset;
  606. stream.next_out = deflate_buff;
  607. stream.avail_out = app->size;
  608. int ret = deflate(&stream, Z_SYNC_FLUSH);
  609. if (ret != Z_OK)
  610. {
  611. printf("ZLIB COMPRESSION RETFAIL: %i, %i (Ret: %i)\n", app->size, stream.avail_out, ret);
  612. MCompressData.unlock();
  613. safe_delete_array(deflate_buff);
  614. return 0;
  615. }
  616. int32 newsize = app->size - stream.avail_out;
  617. safe_delete_array(app->pBuffer);
  618. app->size = newsize + offset;
  619. app->pBuffer = new uchar[app->size];
  620. app->pBuffer[(offset - 1)] = 1;
  621. memcpy(app->pBuffer + offset, deflate_buff, newsize);
  622. MCompressData.unlock();
  623. safe_delete_array(deflate_buff);
  624. #ifdef LE_DEBUG
  625. LogWrite(PACKET__DEBUG, 0, "Packet", "After Compress in %s, line %i:", __FUNCTION__, __LINE__);
  626. DumpPacket(app);
  627. #endif
  628. return offset - 1;
  629. }
  630. int16 EQStream::processRSAKey(EQProtocolPacket *p){
  631. /*int16 limit = 0;
  632. int8 offset = 13;
  633. int8 offset2 = 0;
  634. if(p->pBuffer[2] == 0)
  635. limit = p->pBuffer[9];
  636. else{
  637. limit = p->pBuffer[5];
  638. offset2 = 5;
  639. offset-=1;
  640. }
  641. crypto->setRC4Key(Crypto::RSADecrypt(p->pBuffer + offset + (limit-8), 8));
  642. return (limit + offset +1) - offset2;*/
  643. if(p->pBuffer[0] == 0)
  644. crypto->setRC4Key(Crypto::RSADecrypt(p->pBuffer + 62, 8));
  645. else
  646. crypto->setRC4Key(Crypto::RSADecrypt(p->pBuffer + 61, 8));
  647. return 0;
  648. }
  649. void EQStream::SendKeyRequest(){
  650. int32 crypto_key_size = 60;
  651. int16 size = sizeof(KeyGen_Struct) + sizeof(KeyGen_End_Struct) + crypto_key_size;
  652. EQ2Packet *outapp=new EQ2Packet(OP_WSLoginRequestMsg,NULL,size);
  653. memcpy(&outapp->pBuffer[0], &crypto_key_size, sizeof(int32));
  654. memset(&outapp->pBuffer[4], 0xFF, crypto_key_size);
  655. memset(&outapp->pBuffer[size-5], 1, 1);
  656. memset(&outapp->pBuffer[size-1], 1, 1);
  657. EQ2QueuePacket(outapp);
  658. }
  659. void EQStream::EncryptPacket(EQ2Packet* app, int8 compress_offset, int8 offset){
  660. if(app->size>2 && crypto->isEncrypted()){
  661. app->packet_encrypted = true;
  662. uchar* crypt_buff = app->pBuffer;
  663. if(app->eq2_compressed)
  664. crypto->RC4Encrypt(crypt_buff + compress_offset, app->size - compress_offset);
  665. else
  666. crypto->RC4Encrypt(crypt_buff + 2 + offset, app->size - 2 - offset);
  667. }
  668. }
  669. void EQStream::EQ2QueuePacket(EQ2Packet* app, bool attempted_combine){
  670. if(CheckActive()){
  671. if(!attempted_combine){
  672. MCombineQueueLock.lock();
  673. combine_queue.push_back(app);
  674. MCombineQueueLock.unlock();
  675. }
  676. else{
  677. PreparePacket(app);
  678. #ifdef LE_DEBUG
  679. LogWrite(PACKET__DEBUG, 0, "Packet", "After B in %s, line %i:", __FUNCTION__, __LINE__);
  680. DumpPacket(app);
  681. #endif
  682. SendPacket(app);
  683. }
  684. }
  685. }
  686. void EQStream::UnPreparePacket(EQ2Packet* app){
  687. if(app->pBuffer[2] == 0 && app->pBuffer[3] == 19){
  688. uchar* new_buffer = new uchar[app->size-3];
  689. memcpy(new_buffer+2, app->pBuffer+5, app->size-3);
  690. delete[] app->pBuffer;
  691. app->size-=3;
  692. app->pBuffer = new_buffer;
  693. }
  694. }
  695. void EQStream::PreparePacket(EQ2Packet* app, int8 offset){
  696. app->setVersion(client_version);
  697. compressed_offset = 0;
  698. #ifdef LE_DEBUG
  699. LogWrite(PACKET__DEBUG, 0, "Packet", "Before A in %s, line %i:", __FUNCTION__, __LINE__);
  700. DumpPacket(app);
  701. #endif
  702. if(!app->packet_prepared){
  703. if(app->PreparePacket(MaxLen) == 255) //invalid version
  704. return;
  705. }
  706. #ifdef LE_DEBUG
  707. LogWrite(PACKET__DEBUG, 0, "Packet", "After Prepare in %s, line %i:", __FUNCTION__, __LINE__);
  708. DumpPacket(app);
  709. #endif
  710. if(!app->eq2_compressed && app->size>128){
  711. compressed_offset = EQ2_Compress(app);
  712. if (compressed_offset)
  713. app->eq2_compressed = true;
  714. }
  715. if(!app->packet_encrypted){
  716. EncryptPacket(app, compressed_offset, offset);
  717. if(app->size > 2 && app->pBuffer[2] == 0){
  718. uchar* new_buffer = new uchar[app->size+1];
  719. new_buffer[2] = 0;
  720. memcpy(new_buffer+3, app->pBuffer+2, app->size-2);
  721. delete[] app->pBuffer;
  722. app->pBuffer = new_buffer;
  723. app->size++;
  724. }
  725. }
  726. #ifdef LE_DEBUG
  727. LogWrite(PACKET__DEBUG, 0, "Packet", "After A in %s, line %i:", __FUNCTION__, __LINE__);
  728. DumpPacket(app);
  729. #endif
  730. }
  731. void EQStream::SendPacket(EQProtocolPacket *p)
  732. {
  733. uint32 chunksize,used;
  734. uint32 length;
  735. // Convert the EQApplicationPacket to 1 or more EQProtocolPackets
  736. if (p->size>( MaxLen-8)) { // proto-op(2), seq(2), app-op(2) ... data ... crc(2)
  737. uchar* tmpbuff=p->pBuffer;
  738. length=p->size - 2;
  739. EQProtocolPacket *out=new EQProtocolPacket(OP_Fragment,NULL,MaxLen-4);
  740. *(uint32 *)(out->pBuffer+2)=htonl(length);
  741. used=MaxLen-10;
  742. memcpy(out->pBuffer+6,tmpbuff+2,used);
  743. #ifdef LE_DEBUG
  744. LogWrite(PACKET__DEBUG, 0, "Packet", "(%s, %i) New Fragment: ", __FUNCTION__, __LINE__);
  745. DumpPacket(out);
  746. #endif
  747. SequencedPush(out);
  748. while (used<length) {
  749. chunksize=min(length-used,MaxLen-6);
  750. out=new EQProtocolPacket(OP_Fragment,NULL,chunksize+2);
  751. //memcpy(out->pBuffer+2,tmpbuff,1);
  752. memcpy(out->pBuffer+2,tmpbuff+used+2,chunksize);
  753. #ifdef LE_DEBUG
  754. LogWrite(PACKET__DEBUG, 0, "Packet", "Chunk: ");
  755. DumpPacket(out);
  756. #endif
  757. SequencedPush(out);
  758. used+=chunksize;
  759. }
  760. #ifdef LE_DEBUG
  761. LogWrite(PACKET__DEBUG, 0, "Packet", "Chunk: ");
  762. DumpPacket(out);
  763. cerr << "1: Deleting 0x" << hex << (uint32)(p) << dec << endl;
  764. #endif
  765. delete p;
  766. } else {
  767. SequencedPush(p);
  768. }
  769. }
  770. void EQStream::SendPacket(EQApplicationPacket *p)
  771. {
  772. uint32 chunksize,used;
  773. uint32 length;
  774. // Convert the EQApplicationPacket to 1 or more EQProtocolPackets
  775. if (p->size>(MaxLen-8)) { // proto-op(2), seq(2), app-op(2) ... data ... crc(2)
  776. //cout << "Making oversized packet for: " << endl;
  777. //cout << p->size << endl;
  778. //p->DumpRawHeader();
  779. //dump_message(p->pBuffer,p->size,timestamp());
  780. //cout << p->size << endl;
  781. unsigned char *tmpbuff=new unsigned char[p->size+2];
  782. //cout << hex << (int)tmpbuff << dec << endl;
  783. length=p->serialize(tmpbuff);
  784. EQProtocolPacket *out=new EQProtocolPacket(OP_Fragment,NULL,MaxLen-4);
  785. *(uint32 *)(out->pBuffer+2)=htonl(p->Size());
  786. memcpy(out->pBuffer+6,tmpbuff,MaxLen-10);
  787. used=MaxLen-10;
  788. SequencedPush(out);
  789. //cout << "Chunk #" << ++i << " size=" << used << ", length-used=" << (length-used) << endl;
  790. while (used<length) {
  791. out=new EQProtocolPacket(OP_Fragment,NULL,MaxLen-4);
  792. chunksize=min(length-used,MaxLen-6);
  793. memcpy(out->pBuffer+2,tmpbuff+used,chunksize);
  794. out->size=chunksize+2;
  795. SequencedPush(out);
  796. used+=chunksize;
  797. //cout << "Chunk #"<< ++i << " size=" << chunksize << ", length-used=" << (length-used) << endl;
  798. }
  799. //cerr << "1: Deleting 0x" << hex << (uint32)(p) << dec << endl;
  800. delete p;
  801. delete[] tmpbuff;
  802. } else {
  803. EQProtocolPacket *out=new EQProtocolPacket(OP_Packet,NULL,p->Size()+2);
  804. p->serialize(out->pBuffer+2);
  805. SequencedPush(out);
  806. //cerr << "2: Deleting 0x" << hex << (uint32)(p) << dec << endl;
  807. delete p;
  808. }
  809. }
  810. void EQStream::SequencedPush(EQProtocolPacket *p)
  811. {
  812. p->setVersion(client_version);
  813. MOutboundQueue.lock();
  814. *(uint16 *)(p->pBuffer)=htons(NextOutSeq);
  815. SequencedQueue.push_back(p);
  816. p->sequence = NextOutSeq;
  817. NextOutSeq++;
  818. MOutboundQueue.unlock();
  819. }
  820. void EQStream::NonSequencedPush(EQProtocolPacket *p)
  821. {
  822. p->setVersion(client_version);
  823. MOutboundQueue.lock();
  824. NonSequencedQueue.push(p);
  825. MOutboundQueue.unlock();
  826. }
  827. void EQStream::SendAck(uint16 seq)
  828. {
  829. uint16 Seq=htons(seq);
  830. SetLastAckSent(seq);
  831. NonSequencedPush(new EQProtocolPacket(OP_Ack,(unsigned char *)&Seq,sizeof(uint16)));
  832. }
  833. void EQStream::SendOutOfOrderAck(uint16 seq)
  834. {
  835. uint16 Seq=htons(seq);
  836. NonSequencedPush(new EQProtocolPacket(OP_OutOfOrderAck,(unsigned char *)&Seq,sizeof(uint16)));
  837. }
  838. bool EQStream::CheckCombineQueue(){
  839. bool ret = true; //processed all packets
  840. MCombineQueueLock.lock();
  841. if(combine_queue.size() > 0){
  842. EQ2Packet* first = combine_queue.front();
  843. combine_queue.pop_front();
  844. if(combine_queue.size() == 0){ //nothing to combine this with
  845. EQ2QueuePacket(first, true);
  846. }
  847. else{
  848. PreparePacket(first);
  849. EQ2Packet* second = 0;
  850. bool combine_worked = false;
  851. int16 count = 0;
  852. while(combine_queue.size()){
  853. count++;
  854. second = combine_queue.front();
  855. combine_queue.pop_front();
  856. MCombineQueueLock.unlock();
  857. PreparePacket(second);
  858. /*if(first->GetRawOpcode() != OP_AppCombined && first->pBuffer[2] == 0){
  859. EQ2Packet* tmp = second;
  860. second = first;
  861. first = tmp;
  862. }*/
  863. if(!first->AppCombine(second)){
  864. first->SetProtocolOpcode(OP_Packet);
  865. if(combine_worked){
  866. SequencedPush(first);
  867. }
  868. else{
  869. EQ2QueuePacket(first, true);
  870. }
  871. first = second;
  872. combine_worked = false;
  873. }
  874. else{
  875. combine_worked = true;
  876. //DumpPacket(first);
  877. }
  878. MCombineQueueLock.lock();
  879. if(count >= 60 || first->size > 4000){ //other clients need packets too
  880. ret = false;
  881. break;
  882. }
  883. }
  884. if(first){
  885. first->SetProtocolOpcode(OP_Packet);
  886. if(combine_worked){
  887. SequencedPush(first);
  888. }
  889. else{
  890. EQ2QueuePacket(first, true);
  891. }
  892. }
  893. }
  894. }
  895. MCombineQueueLock.unlock();
  896. return ret;
  897. }
  898. void EQStream::CheckResend(int eq_fd){
  899. int32 curr = Timer::GetCurrentTime2();
  900. EQProtocolPacket* packet = 0;
  901. deque<EQProtocolPacket*>::iterator itr;
  902. MResendQue.lock();
  903. for(itr=resend_que.begin();itr!=resend_que.end();itr++){
  904. packet = *itr;
  905. if(packet->attempt_count >= 5){//tried to resend this packet 5 times, client must already have it but didnt ack it
  906. safe_delete(packet);
  907. itr = resend_que.erase(itr);
  908. if(itr == resend_que.end())
  909. break;
  910. }
  911. else{
  912. if((curr - packet->sent_time) < 1000)
  913. continue;
  914. packet->sent_time -=1000;
  915. packet->attempt_count++;
  916. WritePacket(eq_fd, packet);
  917. }
  918. }
  919. MResendQue.unlock();
  920. }
  921. //returns SeqFuture if `seq` is later than `expected_seq`
  922. EQStream::SeqOrder EQStream::CompareSequence(uint16 expected_seq, uint16 seq)
  923. {
  924. if (expected_seq == seq) {
  925. // Curent
  926. return SeqInOrder;
  927. }
  928. else if ((seq > expected_seq && (uint32)seq < ((uint32)expected_seq + EQStream::MaxWindowSize)) || seq < (expected_seq - EQStream::MaxWindowSize)) {
  929. // Future
  930. return SeqFuture;
  931. }
  932. else {
  933. // Past
  934. return SeqPast;
  935. }
  936. }
  937. void EQStream::AckPackets(uint16 seq)
  938. {
  939. std::deque<EQProtocolPacket*>::iterator itr, tmp;
  940. MOutboundQueue.lock();
  941. SeqOrder ord = CompareSequence(SequencedBase, seq);
  942. if (ord == SeqInOrder) {
  943. //they are not acking anything new...
  944. LogWrite(PACKET__DEBUG, 9, "Packet", "Received an ack with no window advancement (seq %u)", seq);
  945. }
  946. else if (ord == SeqPast) {
  947. //they are nacking blocks going back before our buffer, wtf?
  948. LogWrite(PACKET__DEBUG, 9, "Packet", "Received an ack with backward window advancement (they gave %u, our window starts at %u). This is bad" , seq, SequencedBase);
  949. }
  950. else {
  951. LogWrite(PACKET__DEBUG, 9, "Packet", "Received an ack up through sequence %u. Our base is %u", seq, SequencedBase);
  952. //this is a good ack, we get to ack some blocks.
  953. seq++; //we stop at the block right after their ack, counting on the wrap of both numbers.
  954. while (SequencedBase != seq) {
  955. if (SequencedQueue.empty()) {
  956. LogWrite(PACKET__DEBUG, 9, "Packet", "OUT OF PACKETS acked packet with sequence %u. Next send is %u before this", (unsigned long)SequencedBase, SequencedQueue.size());
  957. SequencedBase = NextOutSeq;
  958. break;
  959. }
  960. LogWrite(PACKET__DEBUG, 9, "Packet", "Removing acked packet with sequence %u", (unsigned long)SequencedBase);
  961. //clean out the acked packet
  962. delete SequencedQueue.front();
  963. SequencedQueue.pop_front();
  964. //advance the base sequence number to the seq of the block after the one we just got rid of.
  965. SequencedBase++;
  966. }
  967. if (uint16(SequencedBase + SequencedQueue.size()) != NextOutSeq) {
  968. LogWrite(PACKET__DEBUG, 9, "Packet", "Post-Ack on %u Invalid Sequenced queue: BS %u + SQ %u != NOS %u", seq, SequencedBase, SequencedQueue.size(), NextOutSeq);
  969. }
  970. }
  971. MOutboundQueue.unlock();
  972. }
  973. void EQStream::Write(int eq_fd)
  974. {
  975. queue<EQProtocolPacket *> ReadyToSend;
  976. long maxack;
  977. // Check our rate to make sure we can send more
  978. MRate.lock();
  979. sint32 threshold=RateThreshold;
  980. MRate.unlock();
  981. if (BytesWritten > threshold) {
  982. //cout << "Over threshold: " << BytesWritten << " > " << threshold << endl;
  983. return;
  984. }
  985. MCombinedAppPacket.lock();
  986. EQApplicationPacket *CombPack=CombinedAppPacket;
  987. CombinedAppPacket=NULL;
  988. MCombinedAppPacket.unlock();
  989. if (CombPack) {
  990. SendPacket(CombPack);
  991. }
  992. // If we got more packets to we need to ack, send an ack on the highest one
  993. MAcks.lock();
  994. maxack=MaxAckReceived;
  995. // Added from peaks findings
  996. if (NextAckToSend>LastAckSent || LastAckSent == 0x0000ffff)
  997. SendAck(NextAckToSend);
  998. MAcks.unlock();
  999. // Lock the outbound queues while we process
  1000. MOutboundQueue.lock();
  1001. // Adjust where we start sending in case we get a late ack
  1002. //if (maxack>LastSeqSent)
  1003. // LastSeqSent=maxack;
  1004. // Place to hold the base packet t combine into
  1005. EQProtocolPacket *p=NULL;
  1006. std::deque<EQProtocolPacket*>::iterator sitr;
  1007. // Find the next sequenced packet to send from the "queue"
  1008. sitr = SequencedQueue.begin();
  1009. uint16 count = 0;
  1010. // get to start of packets
  1011. while (sitr != SequencedQueue.end() && (*sitr)->sent_time > 0) {
  1012. ++sitr;
  1013. ++count;
  1014. }
  1015. bool SeqEmpty = false, NonSeqEmpty = false;
  1016. // Loop until both are empty or MaxSends is reached
  1017. while (!SeqEmpty || !NonSeqEmpty) {
  1018. // See if there are more non-sequenced packets left
  1019. if (!NonSequencedQueue.empty()) {
  1020. if (!p) {
  1021. // If we don't have a packet to try to combine into, use this one as the base
  1022. // And remove it form the queue
  1023. p = NonSequencedQueue.front();
  1024. LogWrite(PACKET__DEBUG, 9, "Packet", "Starting combined packet with non-seq packet of len %u",p->size);
  1025. NonSequencedQueue.pop();
  1026. }
  1027. else if (!p->combine(NonSequencedQueue.front())) {
  1028. // Trying to combine this packet with the base didn't work (too big maybe)
  1029. // So just send the base packet (we'll try this packet again later)
  1030. LogWrite(PACKET__DEBUG, 9, "Packet", "Combined packet full at len %u, next non-seq packet is len %u", p->size, (NonSequencedQueue.front())->size);
  1031. ReadyToSend.push(p);
  1032. BytesWritten += p->size;
  1033. p = nullptr;
  1034. if (BytesWritten > threshold) {
  1035. // Sent enough this round, lets stop to be fair
  1036. LogWrite(PACKET__DEBUG, 9, "Packet", "Exceeded write threshold in nonseq (%u > %u)", BytesWritten, threshold);
  1037. break;
  1038. }
  1039. }
  1040. else {
  1041. // Combine worked, so just remove this packet and it's spot in the queue
  1042. LogWrite(PACKET__DEBUG, 9, "Packet", "Combined non-seq packet of len %u, yeilding %u combined", (NonSequencedQueue.front())->size, p->size);
  1043. delete NonSequencedQueue.front();
  1044. NonSequencedQueue.pop();
  1045. }
  1046. }
  1047. else {
  1048. // No more non-sequenced packets
  1049. NonSeqEmpty = true;
  1050. }
  1051. if (sitr != SequencedQueue.end()) {
  1052. uint16 seq_send = SequencedBase + count; //just for logging...
  1053. if (SequencedQueue.empty()) {
  1054. LogWrite(PACKET__DEBUG, 9, "Packet", "Tried to write a packet with an empty queue (%u is past next out %u)", seq_send, NextOutSeq);
  1055. SeqEmpty = true;
  1056. continue;
  1057. }
  1058. if ((*sitr)->acked || (*sitr)->sent_time != 0) {
  1059. ++sitr;
  1060. ++count;
  1061. if (p) {
  1062. LogWrite(PACKET__DEBUG, 9, "Packet", "Final combined packet not full, len %u", p->size);
  1063. ReadyToSend.push(p);
  1064. BytesWritten += p->size;
  1065. p = nullptr;
  1066. }
  1067. LogWrite(PACKET__DEBUG, 9, "Packet", "Not retransmitting seq packet %u because already marked as acked", seq_send);
  1068. }
  1069. else if (!p) {
  1070. // If we don't have a packet to try to combine into, use this one as the base
  1071. // Copy it first as it will still live until it is acked
  1072. p = (*sitr)->Copy();
  1073. LogWrite(PACKET__DEBUG, 9, "Packet", "Starting combined packet with seq packet %u of len %u", seq_send, p->size);
  1074. (*sitr)->sent_time = Timer::GetCurrentTime2();
  1075. ++sitr;
  1076. ++count;
  1077. }
  1078. else if (!p->combine(*sitr)) {
  1079. // Trying to combine this packet with the base didn't work (too big maybe)
  1080. // So just send the base packet (we'll try this packet again later)
  1081. LogWrite(PACKET__DEBUG, 9, "Packet", "Combined packet full at len %u, next seq packet %u is len %u", p->size, seq_send + 1, (*sitr)->size);
  1082. ReadyToSend.push(p);
  1083. BytesWritten += p->size;
  1084. p = nullptr;
  1085. if ((*sitr)->opcode != OP_Fragment && BytesWritten > threshold) {
  1086. // Sent enough this round, lets stop to be fair
  1087. LogWrite(PACKET__DEBUG, 9, "Packet", "Exceeded write threshold in seq (%u > %u)", BytesWritten, threshold);
  1088. break;
  1089. }
  1090. }
  1091. else {
  1092. // Combine worked
  1093. LogWrite(PACKET__DEBUG, 9, "Packet", "Combined seq packet %u of len %u, yeilding %u combined", seq_send, (*sitr)->size, p->size);
  1094. (*sitr)->sent_time = Timer::GetCurrentTime2();
  1095. ++sitr;
  1096. ++count;
  1097. }
  1098. if (uint16(SequencedBase + SequencedQueue.size()) != NextOutSeq) {
  1099. LogWrite(PACKET__DEBUG, 9, "Packet", "Post send Invalid Sequenced queue: BS %u + SQ %u != NOS %u", SequencedBase, SequencedQueue.size(), NextOutSeq);
  1100. }
  1101. }
  1102. else {
  1103. // No more sequenced packets
  1104. SeqEmpty = true;
  1105. }
  1106. }
  1107. MOutboundQueue.unlock(); // Unlock the queue
  1108. // We have a packet still, must have run out of both seq and non-seq, so send it
  1109. if (p) {
  1110. LogWrite(PACKET__DEBUG, 9, "Packet", "Final combined packet not full, len %u", p->size);
  1111. ReadyToSend.push(p);
  1112. BytesWritten += p->size;
  1113. }
  1114. // Send all the packets we "made"
  1115. while (!ReadyToSend.empty()) {
  1116. p = ReadyToSend.front();
  1117. WritePacket(eq_fd, p);
  1118. delete p;
  1119. ReadyToSend.pop();
  1120. }
  1121. //see if we need to send our disconnect and finish our close
  1122. if (SeqEmpty && NonSeqEmpty) {
  1123. //no more data to send
  1124. if (GetState() == CLOSING) {
  1125. MOutboundQueue.lock();
  1126. if (SequencedQueue.size() > 0 )
  1127. LogWrite(PACKET__DEBUG, 9, "Packet", "All outgoing data flushed, client should be disconnecting, awaiting acknowledgement of SequencedQueue.");
  1128. else
  1129. {
  1130. LogWrite(PACKET__DEBUG, 9, "Packet", "All outgoing data flushed, disconnecting client.");
  1131. //we are waiting for the queues to empty, now we can do our disconnect.
  1132. //this packet will not actually go out until the next call to Write().
  1133. SendDisconnect();
  1134. //SetState(CLOSED);
  1135. }
  1136. MOutboundQueue.unlock();
  1137. }
  1138. }
  1139. }
  1140. void EQStream::WritePacket(int eq_fd, EQProtocolPacket *p)
  1141. {
  1142. uint32 length = 0;
  1143. sockaddr_in address;
  1144. unsigned char tmpbuffer[2048];
  1145. address.sin_family = AF_INET;
  1146. address.sin_addr.s_addr=remote_ip;
  1147. address.sin_port=remote_port;
  1148. #ifdef NOWAY
  1149. uint32 ip=address.sin_addr.s_addr;
  1150. cout << "Sending to: "
  1151. << (int)*(unsigned char *)&ip
  1152. << "." << (int)*((unsigned char *)&ip+1)
  1153. << "." << (int)*((unsigned char *)&ip+2)
  1154. << "." << (int)*((unsigned char *)&ip+3)
  1155. << "," << (int)ntohs(address.sin_port) << "(" << p->size << ")" << endl;
  1156. p->DumpRaw();
  1157. cout << "-------------" << endl;
  1158. #endif
  1159. length=p->serialize(buffer);
  1160. if (p->opcode!=OP_SessionRequest && p->opcode!=OP_SessionResponse) {
  1161. if (compressed) {
  1162. BytesWritten -= p->size;
  1163. uint32 newlen=EQProtocolPacket::Compress(buffer,length,tmpbuffer,2048);
  1164. memcpy(buffer,tmpbuffer,newlen);
  1165. length=newlen;
  1166. BytesWritten += newlen;
  1167. }
  1168. if (encoded) {
  1169. EQProtocolPacket::ChatEncode(buffer,length,Key);
  1170. }
  1171. *(uint16 *)(buffer+length)=htons(CRC16(buffer,length,Key));
  1172. length+=2;
  1173. }
  1174. sent_packets++;
  1175. //dump_message_column(buffer,length,"Writer: ");
  1176. //cout << "Raw Data:\n";
  1177. //DumpPacket(buffer, length);
  1178. sendto(eq_fd,(char *)buffer,length,0,(sockaddr *)&address,sizeof(address));
  1179. }
  1180. EQProtocolPacket *EQStream::Read(int eq_fd, sockaddr_in *from)
  1181. {
  1182. int socklen;
  1183. int length=0;
  1184. unsigned char buffer[2048];
  1185. EQProtocolPacket *p=NULL;
  1186. char temp[15];
  1187. socklen=sizeof(sockaddr);
  1188. #ifdef WIN32
  1189. length=recvfrom(eq_fd, (char *)buffer, 2048, 0, (struct sockaddr*)from, (int *)&socklen);
  1190. #else
  1191. length=recvfrom(eq_fd, buffer, 2048, 0, (struct sockaddr*)from, (socklen_t *)&socklen);
  1192. #endif
  1193. if (length>=2) {
  1194. DumpPacket(buffer, length);
  1195. p=new EQProtocolPacket(buffer[1],&buffer[2],length-2);
  1196. uint32 ip=from->sin_addr.s_addr;
  1197. sprintf(temp,"%d.%d.%d.%d:%d",
  1198. *(unsigned char *)&ip,
  1199. *((unsigned char *)&ip+1),
  1200. *((unsigned char *)&ip+2),
  1201. *((unsigned char *)&ip+3),
  1202. ntohs(from->sin_port));
  1203. //cout << timestamp() << "Data from: " << temp << " OpCode 0x" << hex << setw(2) << setfill('0') << (int)p->opcode << dec << endl;
  1204. //dump_message(p->pBuffer,p->size,timestamp());
  1205. }
  1206. return p;
  1207. }
  1208. void EQStream::SendSessionResponse()
  1209. {
  1210. EQProtocolPacket *out=new EQProtocolPacket(OP_SessionResponse,NULL,sizeof(SessionResponse));
  1211. SessionResponse *Response=(SessionResponse *)out->pBuffer;
  1212. Response->Session=htonl(Session);
  1213. Response->MaxLength=htonl(MaxLen);
  1214. Response->UnknownA=2;
  1215. Response->Format=0;
  1216. if (compressed)
  1217. Response->Format|=FLAG_COMPRESSED;
  1218. if (encoded)
  1219. Response->Format|=FLAG_ENCODED;
  1220. Response->Key=htonl(Key);
  1221. out->size=sizeof(SessionResponse);
  1222. NonSequencedPush(out);
  1223. }
  1224. void EQStream::SendSessionRequest()
  1225. {
  1226. EQProtocolPacket *out=new EQProtocolPacket(OP_SessionRequest,NULL,sizeof(SessionRequest));
  1227. SessionRequest *Request=(SessionRequest *)out->pBuffer;
  1228. memset(Request,0,sizeof(SessionRequest));
  1229. Request->Session=htonl(time(NULL));
  1230. Request->MaxLength=htonl(512);
  1231. NonSequencedPush(out);
  1232. }
  1233. void EQStream::SendDisconnect(bool setstate)
  1234. {
  1235. try{
  1236. if(GetState() != ESTABLISHED)
  1237. return;
  1238. EQProtocolPacket *out=new EQProtocolPacket(OP_SessionDisconnect,NULL,sizeof(uint32)+sizeof(int16));
  1239. *(uint32 *)out->pBuffer=htonl(Session);
  1240. out->pBuffer[4] = 0;
  1241. out->pBuffer[5] = 6;
  1242. NonSequencedPush(out);
  1243. if(setstate)
  1244. SetState(CLOSING);
  1245. }
  1246. catch(...){}
  1247. }
  1248. void EQStream::InboundQueuePush(EQApplicationPacket *p)
  1249. {
  1250. MInboundQueue.lock();
  1251. InboundQueue.push_back(p);
  1252. MInboundQueue.unlock();
  1253. }
  1254. EQApplicationPacket *EQStream::PopPacket()
  1255. {
  1256. EQApplicationPacket *p=NULL;
  1257. MInboundQueue.lock();
  1258. if (InboundQueue.size()) {
  1259. p=InboundQueue.front();
  1260. InboundQueue.pop_front();
  1261. }
  1262. MInboundQueue.unlock();
  1263. if(p)
  1264. p->setVersion(client_version);
  1265. return p;
  1266. }
  1267. void EQStream::InboundQueueClear()
  1268. {
  1269. MInboundQueue.lock();
  1270. while(InboundQueue.size()){
  1271. delete InboundQueue.front();
  1272. InboundQueue.pop_front();
  1273. }
  1274. MInboundQueue.unlock();
  1275. }
  1276. void EQStream::EncryptPacket(uchar* data, int16 size){
  1277. if(size>6){
  1278. }
  1279. }
  1280. bool EQStream::HasOutgoingData()
  1281. {
  1282. bool flag;
  1283. //once closed, we have nothing more to say
  1284. if(CheckClosed())
  1285. return(false);
  1286. MOutboundQueue.lock();
  1287. flag=(!NonSequencedQueue.empty());
  1288. if (!flag) {
  1289. flag = (!SequencedQueue.empty());
  1290. }
  1291. MOutboundQueue.unlock();
  1292. if (!flag) {
  1293. MAcks.lock();
  1294. flag= (NextAckToSend>LastAckSent);
  1295. MAcks.unlock();
  1296. }
  1297. if (!flag) {
  1298. MCombinedAppPacket.lock();
  1299. flag=(CombinedAppPacket!=NULL);
  1300. MCombinedAppPacket.unlock();
  1301. }
  1302. return flag;
  1303. }
  1304. void EQStream::OutboundQueueClear()
  1305. {
  1306. MOutboundQueue.lock();
  1307. while(NonSequencedQueue.size()) {
  1308. delete NonSequencedQueue.front();
  1309. NonSequencedQueue.pop();
  1310. }
  1311. while(SequencedQueue.size()) {
  1312. delete SequencedQueue.front();
  1313. SequencedQueue.pop_front();
  1314. }
  1315. MOutboundQueue.unlock();
  1316. }
  1317. void EQStream::Process(const unsigned char *buffer, const uint32 length)
  1318. {
  1319. received_packets++;
  1320. static unsigned char newbuffer[2048];
  1321. uint32 newlength=0;
  1322. if (EQProtocolPacket::ValidateCRC(buffer,length,Key)) {
  1323. if (compressed) {
  1324. newlength=EQProtocolPacket::Decompress(buffer,length,newbuffer,2048);
  1325. } else {
  1326. memcpy(newbuffer,buffer,length);
  1327. newlength=length;
  1328. if (encoded)
  1329. EQProtocolPacket::ChatDecode(newbuffer,newlength-2,Key);
  1330. }
  1331. if (buffer[1]!=0x01 && buffer[1]!=0x02 && buffer[1]!=0x1d)
  1332. newlength-=2;
  1333. EQProtocolPacket p(newbuffer,newlength);
  1334. ProcessPacket(&p);
  1335. ProcessQueue();
  1336. } else {
  1337. #ifdef EQN_DEBUG
  1338. cout << "Incoming packet failed checksum:" <<endl;
  1339. dump_message_column(const_cast<unsigned char *>(buffer),length,"CRC failed: ");
  1340. #endif
  1341. }
  1342. }
  1343. long EQStream::GetMaxAckReceived()
  1344. {
  1345. MAcks.lock();
  1346. long l=MaxAckReceived;
  1347. MAcks.unlock();
  1348. return l;
  1349. }
  1350. long EQStream::GetNextAckToSend()
  1351. {
  1352. MAcks.lock();
  1353. long l=NextAckToSend;
  1354. MAcks.unlock();
  1355. return l;
  1356. }
  1357. long EQStream::GetLastAckSent()
  1358. {
  1359. MAcks.lock();
  1360. long l=LastAckSent;
  1361. MAcks.unlock();
  1362. return l;
  1363. }
  1364. void EQStream::SetMaxAckReceived(uint32 seq)
  1365. {
  1366. deque<EQProtocolPacket *>::iterator itr;
  1367. MAcks.lock();
  1368. MaxAckReceived=seq;
  1369. MAcks.unlock();
  1370. MOutboundQueue.lock();
  1371. if (long(seq) > LastSeqSent)
  1372. LastSeqSent=seq;
  1373. MResendQue.lock();
  1374. EQProtocolPacket* packet = 0;
  1375. for(itr=resend_que.begin();itr!=resend_que.end();itr++){
  1376. packet = *itr;
  1377. if(packet && packet->sequence <= seq){
  1378. safe_delete(packet);
  1379. itr = resend_que.erase(itr);
  1380. if(itr == resend_que.end())
  1381. break;
  1382. }
  1383. }
  1384. MResendQue.unlock();
  1385. MOutboundQueue.unlock();
  1386. }
  1387. void EQStream::SetNextAckToSend(uint32 seq)
  1388. {
  1389. MAcks.lock();
  1390. NextAckToSend=seq;
  1391. MAcks.unlock();
  1392. }
  1393. void EQStream::SetLastAckSent(uint32 seq)
  1394. {
  1395. MAcks.lock();
  1396. LastAckSent=seq;
  1397. MAcks.unlock();
  1398. }
  1399. void EQStream::SetLastSeqSent(uint32 seq)
  1400. {
  1401. MOutboundQueue.lock();
  1402. LastSeqSent=seq;
  1403. MOutboundQueue.unlock();
  1404. }
  1405. void EQStream::SetStreamType(EQStreamType type)
  1406. {
  1407. StreamType=type;
  1408. switch (StreamType) {
  1409. case LoginStream:
  1410. app_opcode_size=1;
  1411. compressed=false;
  1412. encoded=false;
  1413. break;
  1414. case EQ2Stream:
  1415. app_opcode_size=2;
  1416. compressed=false;
  1417. encoded=false;
  1418. break;
  1419. case ChatOrMailStream:
  1420. case ChatStream:
  1421. case MailStream:
  1422. app_opcode_size=1;
  1423. compressed=false;
  1424. encoded=true;
  1425. break;
  1426. case ZoneStream:
  1427. case WorldStream:
  1428. default:
  1429. app_opcode_size=2;
  1430. compressed=true;
  1431. encoded=false;
  1432. break;
  1433. }
  1434. }
  1435. void EQStream::ProcessQueue()
  1436. {
  1437. if (OutOfOrderpackets.empty()) {
  1438. return;
  1439. }
  1440. EQProtocolPacket* qp = NULL;
  1441. while ((qp = RemoveQueue(NextInSeq)) != NULL) {
  1442. //_log(NET__DEBUG, _L "Processing Queued Packet: Seq=%d" __L, NextInSeq);
  1443. ProcessPacket(qp);
  1444. delete qp;
  1445. //_log(NET__APP_TRACE, _L "OP_Packet Queue size=%d" __L, PacketQueue.size());
  1446. }
  1447. }
  1448. EQProtocolPacket* EQStream::RemoveQueue(uint16 seq)
  1449. {
  1450. map<unsigned short, EQProtocolPacket*>::iterator itr;
  1451. EQProtocolPacket* qp = NULL;
  1452. if ((itr = OutOfOrderpackets.find(seq)) != OutOfOrderpackets.end()) {
  1453. qp = itr->second;
  1454. OutOfOrderpackets.erase(itr);
  1455. //_log(NET__APP_TRACE, _L "OP_Packet Queue size=%d" __L, PacketQueue.size());
  1456. }
  1457. return qp;
  1458. }
  1459. void EQStream::Decay()
  1460. {
  1461. MRate.lock();
  1462. uint32 rate=DecayRate;
  1463. MRate.unlock();
  1464. if (BytesWritten>0) {
  1465. BytesWritten-=rate;
  1466. if (BytesWritten<0)
  1467. BytesWritten=0;
  1468. }
  1469. int count = 0;
  1470. MOutboundQueue.lock();
  1471. for (auto sitr = SequencedQueue.begin(); sitr != SequencedQueue.end(); ++sitr, count++) {
  1472. if (!(*sitr)->acked && (*sitr)->sent_time > 0 && ((*sitr)->sent_time + retransmittimeout) < Timer::GetCurrentTime2()) {
  1473. (*sitr)->sent_time = 0;
  1474. LogWrite(PACKET__DEBUG, 9, "Packet", "Timeout exceeded for seq %u. Flagging packet for retransmission", SequencedBase + count);
  1475. }
  1476. }
  1477. MOutboundQueue.unlock();
  1478. }
  1479. void EQStream::AdjustRates(uint32 average_delta)
  1480. {
  1481. if (average_delta && (average_delta <= AVERAGE_DELTA_MAX)) {
  1482. MRate.lock();
  1483. AverageDelta = average_delta;
  1484. RateThreshold = RATEBASE / average_delta;
  1485. DecayRate = DECAYBASE / average_delta;
  1486. if (BytesWritten > RateThreshold)
  1487. BytesWritten = RateThreshold + DecayRate;
  1488. MRate.unlock();
  1489. }
  1490. else {
  1491. AverageDelta = AVERAGE_DELTA_MAX;
  1492. }
  1493. }