EQStream.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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. uint16 index = seq - SequencedBase;
  945. if (SequencedQueue.size() > 0 && index < SequencedQueue.size())
  946. {
  947. SequencedQueue[index]->acked = true;
  948. }
  949. LogWrite(PACKET__DEBUG, 9, "Packet", "Received an ack with no window advancement (seq %u)", seq);
  950. }
  951. else if (ord == SeqPast) {
  952. //they are nacking blocks going back before our buffer, wtf?
  953. 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);
  954. }
  955. else {
  956. LogWrite(PACKET__DEBUG, 9, "Packet", "Received an ack up through sequence %u. Our base is %u", seq, SequencedBase);
  957. //this is a good ack, we get to ack some blocks.
  958. seq++; //we stop at the block right after their ack, counting on the wrap of both numbers.
  959. while (SequencedBase != seq) {
  960. if (SequencedQueue.empty()) {
  961. LogWrite(PACKET__DEBUG, 9, "Packet", "OUT OF PACKETS acked packet with sequence %u. Next send is %u before this", (unsigned long)SequencedBase, SequencedQueue.size());
  962. SequencedBase = NextOutSeq;
  963. break;
  964. }
  965. LogWrite(PACKET__DEBUG, 9, "Packet", "Removing acked packet with sequence %u", (unsigned long)SequencedBase);
  966. //clean out the acked packet
  967. delete SequencedQueue.front();
  968. SequencedQueue.pop_front();
  969. //advance the base sequence number to the seq of the block after the one we just got rid of.
  970. SequencedBase++;
  971. }
  972. if (uint16(SequencedBase + SequencedQueue.size()) != NextOutSeq) {
  973. LogWrite(PACKET__DEBUG, 9, "Packet", "Post-Ack on %u Invalid Sequenced queue: BS %u + SQ %u != NOS %u", seq, SequencedBase, SequencedQueue.size(), NextOutSeq);
  974. }
  975. }
  976. MOutboundQueue.unlock();
  977. }
  978. void EQStream::Write(int eq_fd)
  979. {
  980. queue<EQProtocolPacket *> ReadyToSend;
  981. long maxack;
  982. // Check our rate to make sure we can send more
  983. MRate.lock();
  984. sint32 threshold=RateThreshold;
  985. MRate.unlock();
  986. if (BytesWritten > threshold) {
  987. //cout << "Over threshold: " << BytesWritten << " > " << threshold << endl;
  988. return;
  989. }
  990. MCombinedAppPacket.lock();
  991. EQApplicationPacket *CombPack=CombinedAppPacket;
  992. CombinedAppPacket=NULL;
  993. MCombinedAppPacket.unlock();
  994. if (CombPack) {
  995. SendPacket(CombPack);
  996. }
  997. // If we got more packets to we need to ack, send an ack on the highest one
  998. MAcks.lock();
  999. maxack=MaxAckReceived;
  1000. // Added from peaks findings
  1001. if (NextAckToSend>LastAckSent || LastAckSent == 0x0000ffff)
  1002. SendAck(NextAckToSend);
  1003. MAcks.unlock();
  1004. // Lock the outbound queues while we process
  1005. MOutboundQueue.lock();
  1006. // Adjust where we start sending in case we get a late ack
  1007. //if (maxack>LastSeqSent)
  1008. // LastSeqSent=maxack;
  1009. // Place to hold the base packet t combine into
  1010. EQProtocolPacket *p=NULL;
  1011. std::deque<EQProtocolPacket*>::iterator sitr;
  1012. // Find the next sequenced packet to send from the "queue"
  1013. sitr = SequencedQueue.begin();
  1014. uint16 count = 0;
  1015. // get to start of packets
  1016. while (sitr != SequencedQueue.end() && (*sitr)->sent_time > 0) {
  1017. ++sitr;
  1018. ++count;
  1019. }
  1020. bool SeqEmpty = false, NonSeqEmpty = false;
  1021. // Loop until both are empty or MaxSends is reached
  1022. while (!SeqEmpty || !NonSeqEmpty) {
  1023. // See if there are more non-sequenced packets left
  1024. if (!NonSequencedQueue.empty()) {
  1025. if (!p) {
  1026. // If we don't have a packet to try to combine into, use this one as the base
  1027. // And remove it form the queue
  1028. p = NonSequencedQueue.front();
  1029. LogWrite(PACKET__DEBUG, 9, "Packet", "Starting combined packet with non-seq packet of len %u",p->size);
  1030. NonSequencedQueue.pop();
  1031. }
  1032. else if (!p->combine(NonSequencedQueue.front())) {
  1033. // Trying to combine this packet with the base didn't work (too big maybe)
  1034. // So just send the base packet (we'll try this packet again later)
  1035. LogWrite(PACKET__DEBUG, 9, "Packet", "Combined packet full at len %u, next non-seq packet is len %u", p->size, (NonSequencedQueue.front())->size);
  1036. ReadyToSend.push(p);
  1037. BytesWritten += p->size;
  1038. p = nullptr;
  1039. if (BytesWritten > threshold) {
  1040. // Sent enough this round, lets stop to be fair
  1041. LogWrite(PACKET__DEBUG, 9, "Packet", "Exceeded write threshold in nonseq (%u > %u)", BytesWritten, threshold);
  1042. break;
  1043. }
  1044. }
  1045. else {
  1046. // Combine worked, so just remove this packet and it's spot in the queue
  1047. LogWrite(PACKET__DEBUG, 9, "Packet", "Combined non-seq packet of len %u, yeilding %u combined", (NonSequencedQueue.front())->size, p->size);
  1048. delete NonSequencedQueue.front();
  1049. NonSequencedQueue.pop();
  1050. }
  1051. }
  1052. else {
  1053. // No more non-sequenced packets
  1054. NonSeqEmpty = true;
  1055. }
  1056. if (sitr != SequencedQueue.end()) {
  1057. uint16 seq_send = SequencedBase + count; //just for logging...
  1058. if (SequencedQueue.empty()) {
  1059. LogWrite(PACKET__DEBUG, 9, "Packet", "Tried to write a packet with an empty queue (%u is past next out %u)", seq_send, NextOutSeq);
  1060. SeqEmpty = true;
  1061. continue;
  1062. }
  1063. if ((*sitr)->acked || (*sitr)->sent_time != 0) {
  1064. ++sitr;
  1065. ++count;
  1066. if (p) {
  1067. LogWrite(PACKET__DEBUG, 9, "Packet", "Final combined packet not full, len %u", p->size);
  1068. ReadyToSend.push(p);
  1069. BytesWritten += p->size;
  1070. p = nullptr;
  1071. }
  1072. LogWrite(PACKET__DEBUG, 9, "Packet", "Not retransmitting seq packet %u because already marked as acked", seq_send);
  1073. }
  1074. else if (!p) {
  1075. // If we don't have a packet to try to combine into, use this one as the base
  1076. // Copy it first as it will still live until it is acked
  1077. p = (*sitr)->Copy();
  1078. LogWrite(PACKET__DEBUG, 9, "Packet", "Starting combined packet with seq packet %u of len %u", seq_send, p->size);
  1079. (*sitr)->sent_time = Timer::GetCurrentTime2();
  1080. ++sitr;
  1081. ++count;
  1082. }
  1083. else if (!p->combine(*sitr)) {
  1084. // Trying to combine this packet with the base didn't work (too big maybe)
  1085. // So just send the base packet (we'll try this packet again later)
  1086. 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);
  1087. ReadyToSend.push(p);
  1088. BytesWritten += p->size;
  1089. p = nullptr;
  1090. if ((*sitr)->opcode != OP_Fragment && BytesWritten > threshold) {
  1091. // Sent enough this round, lets stop to be fair
  1092. LogWrite(PACKET__DEBUG, 9, "Packet", "Exceeded write threshold in seq (%u > %u)", BytesWritten, threshold);
  1093. break;
  1094. }
  1095. }
  1096. else {
  1097. // Combine worked
  1098. LogWrite(PACKET__DEBUG, 9, "Packet", "Combined seq packet %u of len %u, yeilding %u combined", seq_send, (*sitr)->size, p->size);
  1099. (*sitr)->sent_time = Timer::GetCurrentTime2();
  1100. ++sitr;
  1101. ++count;
  1102. }
  1103. if (uint16(SequencedBase + SequencedQueue.size()) != NextOutSeq) {
  1104. LogWrite(PACKET__DEBUG, 9, "Packet", "Post send Invalid Sequenced queue: BS %u + SQ %u != NOS %u", SequencedBase, SequencedQueue.size(), NextOutSeq);
  1105. }
  1106. }
  1107. else {
  1108. // No more sequenced packets
  1109. SeqEmpty = true;
  1110. }
  1111. }
  1112. MOutboundQueue.unlock(); // Unlock the queue
  1113. // We have a packet still, must have run out of both seq and non-seq, so send it
  1114. if (p) {
  1115. LogWrite(PACKET__DEBUG, 9, "Packet", "Final combined packet not full, len %u", p->size);
  1116. ReadyToSend.push(p);
  1117. BytesWritten += p->size;
  1118. }
  1119. // Send all the packets we "made"
  1120. while (!ReadyToSend.empty()) {
  1121. p = ReadyToSend.front();
  1122. WritePacket(eq_fd, p);
  1123. delete p;
  1124. ReadyToSend.pop();
  1125. }
  1126. //see if we need to send our disconnect and finish our close
  1127. if (SeqEmpty && NonSeqEmpty) {
  1128. //no more data to send
  1129. if (GetState() == CLOSING) {
  1130. MOutboundQueue.lock();
  1131. if (SequencedQueue.size() > 0 )
  1132. LogWrite(PACKET__DEBUG, 9, "Packet", "All outgoing data flushed, client should be disconnecting, awaiting acknowledgement of SequencedQueue.");
  1133. else
  1134. {
  1135. LogWrite(PACKET__DEBUG, 9, "Packet", "All outgoing data flushed, disconnecting client.");
  1136. //we are waiting for the queues to empty, now we can do our disconnect.
  1137. //this packet will not actually go out until the next call to Write().
  1138. SendDisconnect();
  1139. //SetState(CLOSED);
  1140. }
  1141. MOutboundQueue.unlock();
  1142. }
  1143. }
  1144. }
  1145. void EQStream::WritePacket(int eq_fd, EQProtocolPacket *p)
  1146. {
  1147. uint32 length = 0;
  1148. sockaddr_in address;
  1149. unsigned char tmpbuffer[2048];
  1150. address.sin_family = AF_INET;
  1151. address.sin_addr.s_addr=remote_ip;
  1152. address.sin_port=remote_port;
  1153. #ifdef NOWAY
  1154. uint32 ip=address.sin_addr.s_addr;
  1155. cout << "Sending to: "
  1156. << (int)*(unsigned char *)&ip
  1157. << "." << (int)*((unsigned char *)&ip+1)
  1158. << "." << (int)*((unsigned char *)&ip+2)
  1159. << "." << (int)*((unsigned char *)&ip+3)
  1160. << "," << (int)ntohs(address.sin_port) << "(" << p->size << ")" << endl;
  1161. p->DumpRaw();
  1162. cout << "-------------" << endl;
  1163. #endif
  1164. length=p->serialize(buffer);
  1165. if (p->opcode!=OP_SessionRequest && p->opcode!=OP_SessionResponse) {
  1166. if (compressed) {
  1167. BytesWritten -= p->size;
  1168. uint32 newlen=EQProtocolPacket::Compress(buffer,length,tmpbuffer,2048);
  1169. memcpy(buffer,tmpbuffer,newlen);
  1170. length=newlen;
  1171. BytesWritten += newlen;
  1172. }
  1173. if (encoded) {
  1174. EQProtocolPacket::ChatEncode(buffer,length,Key);
  1175. }
  1176. *(uint16 *)(buffer+length)=htons(CRC16(buffer,length,Key));
  1177. length+=2;
  1178. }
  1179. sent_packets++;
  1180. //dump_message_column(buffer,length,"Writer: ");
  1181. //cout << "Raw Data:\n";
  1182. //DumpPacket(buffer, length);
  1183. sendto(eq_fd,(char *)buffer,length,0,(sockaddr *)&address,sizeof(address));
  1184. }
  1185. EQProtocolPacket *EQStream::Read(int eq_fd, sockaddr_in *from)
  1186. {
  1187. int socklen;
  1188. int length=0;
  1189. unsigned char buffer[2048];
  1190. EQProtocolPacket *p=NULL;
  1191. char temp[15];
  1192. socklen=sizeof(sockaddr);
  1193. #ifdef WIN32
  1194. length=recvfrom(eq_fd, (char *)buffer, 2048, 0, (struct sockaddr*)from, (int *)&socklen);
  1195. #else
  1196. length=recvfrom(eq_fd, buffer, 2048, 0, (struct sockaddr*)from, (socklen_t *)&socklen);
  1197. #endif
  1198. if (length>=2) {
  1199. DumpPacket(buffer, length);
  1200. p=new EQProtocolPacket(buffer[1],&buffer[2],length-2);
  1201. uint32 ip=from->sin_addr.s_addr;
  1202. sprintf(temp,"%d.%d.%d.%d:%d",
  1203. *(unsigned char *)&ip,
  1204. *((unsigned char *)&ip+1),
  1205. *((unsigned char *)&ip+2),
  1206. *((unsigned char *)&ip+3),
  1207. ntohs(from->sin_port));
  1208. //cout << timestamp() << "Data from: " << temp << " OpCode 0x" << hex << setw(2) << setfill('0') << (int)p->opcode << dec << endl;
  1209. //dump_message(p->pBuffer,p->size,timestamp());
  1210. }
  1211. return p;
  1212. }
  1213. void EQStream::SendSessionResponse()
  1214. {
  1215. EQProtocolPacket *out=new EQProtocolPacket(OP_SessionResponse,NULL,sizeof(SessionResponse));
  1216. SessionResponse *Response=(SessionResponse *)out->pBuffer;
  1217. Response->Session=htonl(Session);
  1218. Response->MaxLength=htonl(MaxLen);
  1219. Response->UnknownA=2;
  1220. Response->Format=0;
  1221. if (compressed)
  1222. Response->Format|=FLAG_COMPRESSED;
  1223. if (encoded)
  1224. Response->Format|=FLAG_ENCODED;
  1225. Response->Key=htonl(Key);
  1226. out->size=sizeof(SessionResponse);
  1227. NonSequencedPush(out);
  1228. }
  1229. void EQStream::SendSessionRequest()
  1230. {
  1231. EQProtocolPacket *out=new EQProtocolPacket(OP_SessionRequest,NULL,sizeof(SessionRequest));
  1232. SessionRequest *Request=(SessionRequest *)out->pBuffer;
  1233. memset(Request,0,sizeof(SessionRequest));
  1234. Request->Session=htonl(time(NULL));
  1235. Request->MaxLength=htonl(512);
  1236. NonSequencedPush(out);
  1237. }
  1238. void EQStream::SendDisconnect(bool setstate)
  1239. {
  1240. try{
  1241. if(GetState() != ESTABLISHED)
  1242. return;
  1243. EQProtocolPacket *out=new EQProtocolPacket(OP_SessionDisconnect,NULL,sizeof(uint32)+sizeof(int16));
  1244. *(uint32 *)out->pBuffer=htonl(Session);
  1245. out->pBuffer[4] = 0;
  1246. out->pBuffer[5] = 6;
  1247. NonSequencedPush(out);
  1248. if(setstate)
  1249. SetState(CLOSING);
  1250. }
  1251. catch(...){}
  1252. }
  1253. void EQStream::InboundQueuePush(EQApplicationPacket *p)
  1254. {
  1255. MInboundQueue.lock();
  1256. InboundQueue.push_back(p);
  1257. MInboundQueue.unlock();
  1258. }
  1259. EQApplicationPacket *EQStream::PopPacket()
  1260. {
  1261. EQApplicationPacket *p=NULL;
  1262. MInboundQueue.lock();
  1263. if (InboundQueue.size()) {
  1264. p=InboundQueue.front();
  1265. InboundQueue.pop_front();
  1266. }
  1267. MInboundQueue.unlock();
  1268. if(p)
  1269. p->setVersion(client_version);
  1270. return p;
  1271. }
  1272. void EQStream::InboundQueueClear()
  1273. {
  1274. MInboundQueue.lock();
  1275. while(InboundQueue.size()){
  1276. delete InboundQueue.front();
  1277. InboundQueue.pop_front();
  1278. }
  1279. MInboundQueue.unlock();
  1280. }
  1281. void EQStream::EncryptPacket(uchar* data, int16 size){
  1282. if(size>6){
  1283. }
  1284. }
  1285. bool EQStream::HasOutgoingData()
  1286. {
  1287. bool flag;
  1288. //once closed, we have nothing more to say
  1289. if(CheckClosed())
  1290. return(false);
  1291. MOutboundQueue.lock();
  1292. flag=(!NonSequencedQueue.empty());
  1293. if (!flag) {
  1294. flag = (!SequencedQueue.empty());
  1295. }
  1296. MOutboundQueue.unlock();
  1297. if (!flag) {
  1298. MAcks.lock();
  1299. flag= (NextAckToSend>LastAckSent);
  1300. MAcks.unlock();
  1301. }
  1302. if (!flag) {
  1303. MCombinedAppPacket.lock();
  1304. flag=(CombinedAppPacket!=NULL);
  1305. MCombinedAppPacket.unlock();
  1306. }
  1307. return flag;
  1308. }
  1309. void EQStream::OutboundQueueClear()
  1310. {
  1311. MOutboundQueue.lock();
  1312. while(NonSequencedQueue.size()) {
  1313. delete NonSequencedQueue.front();
  1314. NonSequencedQueue.pop();
  1315. }
  1316. while(SequencedQueue.size()) {
  1317. delete SequencedQueue.front();
  1318. SequencedQueue.pop_front();
  1319. }
  1320. MOutboundQueue.unlock();
  1321. }
  1322. void EQStream::Process(const unsigned char *buffer, const uint32 length)
  1323. {
  1324. received_packets++;
  1325. static unsigned char newbuffer[2048];
  1326. uint32 newlength=0;
  1327. if (EQProtocolPacket::ValidateCRC(buffer,length,Key)) {
  1328. if (compressed) {
  1329. newlength=EQProtocolPacket::Decompress(buffer,length,newbuffer,2048);
  1330. } else {
  1331. memcpy(newbuffer,buffer,length);
  1332. newlength=length;
  1333. if (encoded)
  1334. EQProtocolPacket::ChatDecode(newbuffer,newlength-2,Key);
  1335. }
  1336. if (buffer[1]!=0x01 && buffer[1]!=0x02 && buffer[1]!=0x1d)
  1337. newlength-=2;
  1338. EQProtocolPacket p(newbuffer,newlength);
  1339. ProcessPacket(&p);
  1340. ProcessQueue();
  1341. } else {
  1342. #ifdef EQN_DEBUG
  1343. cout << "Incoming packet failed checksum:" <<endl;
  1344. dump_message_column(const_cast<unsigned char *>(buffer),length,"CRC failed: ");
  1345. #endif
  1346. }
  1347. }
  1348. long EQStream::GetMaxAckReceived()
  1349. {
  1350. MAcks.lock();
  1351. long l=MaxAckReceived;
  1352. MAcks.unlock();
  1353. return l;
  1354. }
  1355. long EQStream::GetNextAckToSend()
  1356. {
  1357. MAcks.lock();
  1358. long l=NextAckToSend;
  1359. MAcks.unlock();
  1360. return l;
  1361. }
  1362. long EQStream::GetLastAckSent()
  1363. {
  1364. MAcks.lock();
  1365. long l=LastAckSent;
  1366. MAcks.unlock();
  1367. return l;
  1368. }
  1369. void EQStream::SetMaxAckReceived(uint32 seq)
  1370. {
  1371. deque<EQProtocolPacket *>::iterator itr;
  1372. MAcks.lock();
  1373. MaxAckReceived=seq;
  1374. MAcks.unlock();
  1375. MOutboundQueue.lock();
  1376. if (long(seq) > LastSeqSent)
  1377. LastSeqSent=seq;
  1378. MResendQue.lock();
  1379. EQProtocolPacket* packet = 0;
  1380. for(itr=resend_que.begin();itr!=resend_que.end();itr++){
  1381. packet = *itr;
  1382. if(packet && packet->sequence <= seq){
  1383. safe_delete(packet);
  1384. itr = resend_que.erase(itr);
  1385. if(itr == resend_que.end())
  1386. break;
  1387. }
  1388. }
  1389. MResendQue.unlock();
  1390. MOutboundQueue.unlock();
  1391. }
  1392. void EQStream::SetNextAckToSend(uint32 seq)
  1393. {
  1394. MAcks.lock();
  1395. NextAckToSend=seq;
  1396. MAcks.unlock();
  1397. }
  1398. void EQStream::SetLastAckSent(uint32 seq)
  1399. {
  1400. MAcks.lock();
  1401. LastAckSent=seq;
  1402. MAcks.unlock();
  1403. }
  1404. void EQStream::SetLastSeqSent(uint32 seq)
  1405. {
  1406. MOutboundQueue.lock();
  1407. LastSeqSent=seq;
  1408. MOutboundQueue.unlock();
  1409. }
  1410. void EQStream::SetStreamType(EQStreamType type)
  1411. {
  1412. StreamType=type;
  1413. switch (StreamType) {
  1414. case LoginStream:
  1415. app_opcode_size=1;
  1416. compressed=false;
  1417. encoded=false;
  1418. break;
  1419. case EQ2Stream:
  1420. app_opcode_size=2;
  1421. compressed=false;
  1422. encoded=false;
  1423. break;
  1424. case ChatOrMailStream:
  1425. case ChatStream:
  1426. case MailStream:
  1427. app_opcode_size=1;
  1428. compressed=false;
  1429. encoded=true;
  1430. break;
  1431. case ZoneStream:
  1432. case WorldStream:
  1433. default:
  1434. app_opcode_size=2;
  1435. compressed=true;
  1436. encoded=false;
  1437. break;
  1438. }
  1439. }
  1440. void EQStream::ProcessQueue()
  1441. {
  1442. if (OutOfOrderpackets.empty()) {
  1443. return;
  1444. }
  1445. EQProtocolPacket* qp = NULL;
  1446. while ((qp = RemoveQueue(NextInSeq)) != NULL) {
  1447. //_log(NET__DEBUG, _L "Processing Queued Packet: Seq=%d" __L, NextInSeq);
  1448. ProcessPacket(qp);
  1449. delete qp;
  1450. //_log(NET__APP_TRACE, _L "OP_Packet Queue size=%d" __L, PacketQueue.size());
  1451. }
  1452. }
  1453. EQProtocolPacket* EQStream::RemoveQueue(uint16 seq)
  1454. {
  1455. map<unsigned short, EQProtocolPacket*>::iterator itr;
  1456. EQProtocolPacket* qp = NULL;
  1457. if ((itr = OutOfOrderpackets.find(seq)) != OutOfOrderpackets.end()) {
  1458. qp = itr->second;
  1459. OutOfOrderpackets.erase(itr);
  1460. //_log(NET__APP_TRACE, _L "OP_Packet Queue size=%d" __L, PacketQueue.size());
  1461. }
  1462. return qp;
  1463. }
  1464. void EQStream::Decay()
  1465. {
  1466. MRate.lock();
  1467. uint32 rate=DecayRate;
  1468. MRate.unlock();
  1469. if (BytesWritten>0) {
  1470. BytesWritten-=rate;
  1471. if (BytesWritten<0)
  1472. BytesWritten=0;
  1473. }
  1474. int count = 0;
  1475. MOutboundQueue.lock();
  1476. for (auto sitr = SequencedQueue.begin(); sitr != SequencedQueue.end(); ++sitr, count++) {
  1477. if (!(*sitr)->acked && (*sitr)->sent_time > 0 && ((*sitr)->sent_time + retransmittimeout) < Timer::GetCurrentTime2()) {
  1478. (*sitr)->sent_time = 0;
  1479. LogWrite(PACKET__DEBUG, 9, "Packet", "Timeout exceeded for seq %u. Flagging packet for retransmission", SequencedBase + count);
  1480. }
  1481. }
  1482. MOutboundQueue.unlock();
  1483. }
  1484. void EQStream::AdjustRates(uint32 average_delta)
  1485. {
  1486. if (average_delta && (average_delta <= AVERAGE_DELTA_MAX)) {
  1487. MRate.lock();
  1488. AverageDelta = average_delta;
  1489. RateThreshold = RATEBASE / average_delta;
  1490. DecayRate = DECAYBASE / average_delta;
  1491. if (BytesWritten > RateThreshold)
  1492. BytesWritten = RateThreshold + DecayRate;
  1493. MRate.unlock();
  1494. }
  1495. else {
  1496. AverageDelta = AVERAGE_DELTA_MAX;
  1497. }
  1498. }