9
3

EQPacket.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. #include "debug.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <iostream>
  20. #include <iomanip>
  21. #include "EQPacket.h"
  22. #include "misc.h"
  23. #include "op_codes.h"
  24. #include "CRC16.h"
  25. #include "opcodemgr.h"
  26. #include "packet_dump.h"
  27. #include <map>
  28. #include "Log.h"
  29. #include <time.h>
  30. using namespace std;
  31. extern map<int16,OpcodeManager*>EQOpcodeManager;
  32. uint8 EQApplicationPacket::default_opcode_size=2;
  33. EQPacket::EQPacket(const uint16 op, const unsigned char *buf, uint32 len)
  34. {
  35. this->opcode=op;
  36. this->pBuffer=NULL;
  37. this->size=0;
  38. version = 0;
  39. setTimeInfo(0,0);
  40. if (len>0) {
  41. this->size=len;
  42. pBuffer= new unsigned char[this->size];
  43. if (buf) {
  44. memcpy(this->pBuffer,buf,this->size);
  45. } else {
  46. memset(this->pBuffer,0,this->size);
  47. }
  48. }
  49. }
  50. int8 EQ2Packet::PreparePacket(int16 MaxLen){
  51. int16 OpcodeVersion = GetOpcodeVersion(version);
  52. // stops a crash for incorrect version
  53. if(EQOpcodeManager.count(OpcodeVersion) == 0)
  54. {
  55. LogWrite(PACKET__ERROR, 0, "Packet", "Version %i is not listed in the opcodes table.",version);
  56. return -1;
  57. }
  58. packet_prepared = true;
  59. int16 login_opcode = EQOpcodeManager[OpcodeVersion]->EmuToEQ(login_op);
  60. int8 offset = 0;
  61. //one of the int16s is for the seq, other is for the EQ2 opcode and compressed flag (OP_Packet is the header, not the opcode)
  62. int32 new_size = size + sizeof(int16) + sizeof(int8);
  63. bool oversized = false;
  64. if(login_opcode != 2){
  65. new_size+=sizeof(int8); //for opcode
  66. if(login_opcode >= 255){
  67. new_size += sizeof(int16);
  68. oversized = true;
  69. }
  70. else
  71. login_opcode = ntohs(login_opcode);
  72. }
  73. uchar* new_buffer = new uchar[new_size];
  74. memset(new_buffer,0,new_size);
  75. uchar* ptr = new_buffer + sizeof(int16); // sequence is first
  76. if(login_opcode != 2){
  77. if(oversized){
  78. ptr += sizeof(int8); //compressed flag
  79. int8 addon = 0xff;
  80. memcpy(ptr, &addon, sizeof(int8));
  81. ptr += sizeof(int8);
  82. }
  83. memcpy(ptr, &login_opcode, sizeof(int16));
  84. ptr += sizeof(int16);
  85. }
  86. else{
  87. memcpy(ptr, &login_opcode, sizeof(int8));
  88. ptr += sizeof(int8);
  89. }
  90. memcpy(ptr, pBuffer, size);
  91. safe_delete_array(pBuffer);
  92. pBuffer = new_buffer;
  93. offset = new_size - size - 1;
  94. size = new_size;
  95. return offset;
  96. }
  97. uint32 EQProtocolPacket::serialize(unsigned char *dest, int8 offset) const
  98. {
  99. if (opcode>0xff) {
  100. *(uint16 *)dest=opcode;
  101. } else {
  102. *(dest)=0;
  103. *(dest+1)=opcode;
  104. }
  105. memcpy(dest+2,pBuffer+offset,size-offset);
  106. return size+2;
  107. }
  108. uint32 EQApplicationPacket::serialize(unsigned char *dest) const
  109. {
  110. if (app_opcode_size==1)
  111. *(unsigned char *)dest=opcode;
  112. else
  113. *(uint16 *)dest=opcode;
  114. memcpy(dest+app_opcode_size,pBuffer,size);
  115. return size+app_opcode_size;
  116. }
  117. EQPacket::~EQPacket()
  118. {
  119. safe_delete_array(pBuffer);
  120. pBuffer=NULL;
  121. }
  122. void EQPacket::DumpRawHeader(uint16 seq, FILE* to) const
  123. {
  124. /*if (timestamp.tv_sec) {
  125. char temp[20];
  126. tm t;
  127. const time_t sec = timestamp.tv_sec;
  128. localtime_s(&t, &sec);
  129. strftime(temp, 20, "%F %T", &t);
  130. fprintf(to, "%s.%06lu ", temp, timestamp.tv_usec);
  131. }*/
  132. DumpRawHeaderNoTime(seq, to);
  133. }
  134. const char* EQPacket::GetOpcodeName(){
  135. int16 OpcodeVersion = GetOpcodeVersion(version);
  136. if(EQOpcodeManager.count(OpcodeVersion) > 0)
  137. return EQOpcodeManager[OpcodeVersion]->EQToName(opcode);
  138. else
  139. return NULL;
  140. }
  141. void EQPacket::DumpRawHeaderNoTime(uint16 seq, FILE *to) const
  142. {
  143. if (src_ip) {
  144. string sIP,dIP;;
  145. sIP=long2ip(src_ip);
  146. dIP=long2ip(dst_ip);
  147. fprintf(to, "[%s:%d->%s:%d] ",sIP.c_str(),src_port,dIP.c_str(),dst_port);
  148. }
  149. if (seq != 0xffff)
  150. fprintf(to, "[Seq=%u] ",seq);
  151. string name;
  152. int16 OpcodeVersion = GetOpcodeVersion(version);
  153. if(EQOpcodeManager.count(OpcodeVersion) > 0)
  154. name = EQOpcodeManager[OpcodeVersion]->EQToName(opcode);
  155. fprintf(to, "[OpCode 0x%04x (%s) Size=%u]\n",opcode,name.c_str(),size);
  156. }
  157. void EQPacket::DumpRaw(FILE *to) const
  158. {
  159. DumpRawHeader();
  160. if (pBuffer && size)
  161. dump_message_column(pBuffer, size, " ", to);
  162. fprintf(to, "\n");
  163. }
  164. EQProtocolPacket::EQProtocolPacket(const unsigned char *buf, uint32 len, int in_opcode)
  165. {
  166. uint32 offset = 0;
  167. if(in_opcode>=0)
  168. opcode = in_opcode;
  169. else{
  170. offset=2;
  171. opcode=ntohs(*(const uint16 *)buf);
  172. }
  173. if (len-offset) {
  174. pBuffer= new unsigned char[len-offset];
  175. size=len-offset;
  176. if(buf)
  177. memcpy(pBuffer,buf+offset,len-offset);
  178. else
  179. memset(pBuffer,0,size);
  180. } else {
  181. pBuffer=NULL;
  182. size=0;
  183. }
  184. version = 0;
  185. eq2_compressed = false;
  186. packet_prepared = false;
  187. packet_encrypted = false;
  188. sent_time = 0;
  189. attempt_count = 0;
  190. sequence = 0;
  191. }
  192. bool EQ2Packet::AppCombine(EQ2Packet* rhs){
  193. bool result = false;
  194. uchar* tmpbuffer = 0;
  195. bool over_sized_packet = false;
  196. int32 new_size = 0;
  197. //bool whee = false;
  198. // DumpPacket(this);
  199. // DumpPacket(rhs);
  200. /*if(rhs->size >= 255){
  201. DumpPacket(this);
  202. DumpPacket(rhs);
  203. whee = true;
  204. }*/
  205. if (opcode==OP_AppCombined && ((size + rhs->size + 3) < 512)){
  206. int16 tmp_size = rhs->size - 2;
  207. if(tmp_size >= 255){
  208. new_size = size+tmp_size+3;
  209. over_sized_packet = true;
  210. }
  211. else
  212. new_size = size+tmp_size+1;
  213. tmpbuffer = new uchar[new_size];
  214. uchar* ptr = tmpbuffer;
  215. memcpy(ptr, pBuffer, size);
  216. ptr += size;
  217. if(over_sized_packet){
  218. memset(ptr, 255, sizeof(int8));
  219. ptr += sizeof(int8);
  220. tmp_size = htons(tmp_size);
  221. memcpy(ptr, &tmp_size, sizeof(int16));
  222. ptr += sizeof(int16);
  223. }
  224. else{
  225. memcpy(ptr, &tmp_size, sizeof(int8));
  226. ptr += sizeof(int8);
  227. }
  228. memcpy(ptr, rhs->pBuffer+2, rhs->size-2);
  229. delete[] pBuffer;
  230. size = new_size;
  231. pBuffer=tmpbuffer;
  232. safe_delete(rhs);
  233. result=true;
  234. }
  235. else if((size + rhs->size + 6) < 512){
  236. int32 tmp_size = size - 2;
  237. int32 tmp_size2 = rhs->size - 2;
  238. opcode=OP_AppCombined;
  239. bool over_sized_packet2 = false;
  240. new_size = size;
  241. if(tmp_size >= 255){
  242. new_size += 5;
  243. over_sized_packet = true;
  244. }
  245. else
  246. new_size += 3;
  247. if(tmp_size2 >= 255){
  248. new_size += tmp_size2+3;
  249. over_sized_packet2 = true;
  250. }
  251. else
  252. new_size += tmp_size2+1;
  253. tmpbuffer = new uchar[new_size];
  254. tmpbuffer[2]=0;
  255. tmpbuffer[3]=0x19;
  256. uchar* ptr = tmpbuffer+4;
  257. if(over_sized_packet){
  258. memset(ptr, 255, sizeof(int8));
  259. ptr += sizeof(int8);
  260. tmp_size = htons(tmp_size);
  261. memcpy(ptr, &tmp_size, sizeof(int16));
  262. ptr += sizeof(int16);
  263. }
  264. else{
  265. memcpy(ptr, &tmp_size, sizeof(int8));
  266. ptr += sizeof(int8);
  267. }
  268. memcpy(ptr, pBuffer+2, size-2);
  269. ptr += (size-2);
  270. if(over_sized_packet2){
  271. memset(ptr, 255, sizeof(int8));
  272. ptr += sizeof(int8);
  273. tmp_size2 = htons(tmp_size2);
  274. memcpy(ptr, &tmp_size2, sizeof(int16));
  275. ptr += sizeof(int16);
  276. }
  277. else{
  278. memcpy(ptr, &tmp_size2, sizeof(int8));
  279. ptr += sizeof(int8);
  280. }
  281. memcpy(ptr, rhs->pBuffer+2, rhs->size-2);
  282. size = new_size;
  283. delete[] pBuffer;
  284. pBuffer=tmpbuffer;
  285. safe_delete(rhs);
  286. result=true;
  287. }
  288. /*if(whee){
  289. DumpPacket(this);
  290. cout << "fsdfsdf";
  291. }*/
  292. //DumpPacket(this);
  293. return result;
  294. }
  295. bool EQProtocolPacket::combine(const EQProtocolPacket *rhs)
  296. {
  297. bool result=false;
  298. //if(dont_combine)
  299. // return false;
  300. //if (opcode==OP_Combined && size+rhs->size+5<256) {
  301. if (opcode==OP_Combined && (rhs->size+3)<=255 && (rhs->size+3+size)<512) {
  302. unsigned char *tmpbuffer=new unsigned char [size+rhs->size+3];
  303. memcpy(tmpbuffer,pBuffer,size);
  304. uint32 offset=size;
  305. tmpbuffer[offset++]=rhs->Size();
  306. offset+=rhs->serialize(tmpbuffer+offset);
  307. size=offset;
  308. delete[] pBuffer;
  309. pBuffer=tmpbuffer;
  310. result=true;
  311. } else if( ((size+3) < 255) && ((rhs->size+3)<255) ) {
  312. unsigned char *tmpbuffer=new unsigned char [size+rhs->size+6];
  313. uint32 offset=0;
  314. tmpbuffer[offset++]=Size();
  315. offset+=serialize(tmpbuffer+offset);
  316. tmpbuffer[offset++]=rhs->Size();
  317. offset+=rhs->serialize(tmpbuffer+offset);
  318. size=offset;
  319. delete[] pBuffer;
  320. pBuffer=tmpbuffer;
  321. opcode=OP_Combined;
  322. result=true;
  323. }
  324. return result;
  325. }
  326. EQApplicationPacket::EQApplicationPacket(const unsigned char *buf, uint32 len, uint8 opcode_size)
  327. {
  328. uint32 offset=0;
  329. app_opcode_size=(opcode_size==0) ? EQApplicationPacket::default_opcode_size : opcode_size;
  330. if (app_opcode_size==1) {
  331. opcode=*(const unsigned char *)buf;
  332. offset++;
  333. } else {
  334. opcode=*(const uint16 *)buf;
  335. offset+=2;
  336. }
  337. if ((len-offset)>0) {
  338. pBuffer=new unsigned char[len-offset];
  339. memcpy(pBuffer,buf+offset,len-offset);
  340. size=len-offset;
  341. } else {
  342. pBuffer=NULL;
  343. size=0;
  344. }
  345. emu_opcode = OP_Unknown;
  346. }
  347. bool EQApplicationPacket::combine(const EQApplicationPacket *rhs)
  348. {
  349. cout << "CALLED AP COMBINE!!!!\n";
  350. return false;
  351. }
  352. void EQApplicationPacket::SetOpcode(EmuOpcode emu_op) {
  353. if(emu_op == OP_Unknown) {
  354. opcode = 0;
  355. emu_opcode = OP_Unknown;
  356. return;
  357. }
  358. opcode = EQOpcodeManager[GetOpcodeVersion(version)]->EmuToEQ(emu_op);
  359. if(opcode == OP_Unknown) {
  360. LogWrite(PACKET__DEBUG, 0, "Packet", "Unable to convert Emu opcode %s (%d) into an EQ opcode.", OpcodeNames[emu_op], emu_op);
  361. }
  362. //save the emu opcode we just set.
  363. emu_opcode = emu_op;
  364. }
  365. const EmuOpcode EQApplicationPacket::GetOpcodeConst() const {
  366. if(emu_opcode != OP_Unknown) {
  367. return(emu_opcode);
  368. }
  369. if(opcode == 10000) {
  370. return(OP_Unknown);
  371. }
  372. EmuOpcode emu_op;
  373. emu_op = EQOpcodeManager[GetOpcodeVersion(version)]->EQToEmu(opcode);
  374. if(emu_op == OP_Unknown) {
  375. LogWrite(PACKET__DEBUG, 1, "Packet", "Unable to convert EQ opcode 0x%.4X (%i) to an emu opcode (%s)", opcode, opcode, __FUNCTION__);
  376. }
  377. return(emu_op);
  378. }
  379. EQApplicationPacket *EQProtocolPacket::MakeApplicationPacket(uint8 opcode_size) const {
  380. EQApplicationPacket *res = new EQApplicationPacket;
  381. res->app_opcode_size=(opcode_size==0) ? EQApplicationPacket::default_opcode_size : opcode_size;
  382. if (res->app_opcode_size==1) {
  383. res->pBuffer= new unsigned char[size+1];
  384. memcpy(res->pBuffer+1,pBuffer,size);
  385. *(res->pBuffer)=htons(opcode)&0xff;
  386. res->opcode=opcode&0xff;
  387. res->size=size+1;
  388. } else {
  389. res->pBuffer= new unsigned char[size];
  390. memcpy(res->pBuffer,pBuffer,size);
  391. res->opcode=opcode;
  392. res->size=size;
  393. }
  394. res->copyInfo(this);
  395. return(res);
  396. }
  397. bool EQProtocolPacket::ValidateCRC(const unsigned char *buffer, int length, uint32 Key)
  398. {
  399. bool valid=false;
  400. // OP_SessionRequest, OP_SessionResponse, OP_OutOfSession are not CRC'd
  401. if (buffer[0]==0x00 && (buffer[1]==OP_SessionRequest || buffer[1]==OP_SessionResponse || buffer[1]==OP_OutOfSession)) {
  402. valid=true;
  403. } else if(buffer[2] == 0x00 && buffer[3] == 0x19){
  404. valid = true;
  405. }
  406. else {
  407. uint16 comp_crc=CRC16(buffer,length-2,Key);
  408. uint16 packet_crc=ntohs(*(const uint16 *)(buffer+length-2));
  409. #ifdef EQN_DEBUG
  410. if (packet_crc && comp_crc != packet_crc) {
  411. cout << "CRC mismatch: comp=" << hex << comp_crc << ", packet=" << packet_crc << dec << endl;
  412. }
  413. #endif
  414. valid = (!packet_crc || comp_crc == packet_crc);
  415. }
  416. return valid;
  417. }
  418. uint32 EQProtocolPacket::Decompress(const unsigned char *buffer, const uint32 length, unsigned char *newbuf, uint32 newbufsize)
  419. {
  420. uint32 newlen=0;
  421. uint32 flag_offset=0;
  422. newbuf[0]=buffer[0];
  423. if (buffer[0]==0x00) {
  424. flag_offset=2;
  425. newbuf[1]=buffer[1];
  426. } else
  427. flag_offset=1;
  428. if (length>2 && buffer[flag_offset]==0x5a) {
  429. LogWrite(PACKET__DEBUG, 0, "Packet", "In Decompress 1");
  430. newlen=Inflate(const_cast<unsigned char *>(buffer+flag_offset+1),length-(flag_offset+1)-2,newbuf+flag_offset,newbufsize-flag_offset)+2;
  431. newbuf[newlen++]=buffer[length-2];
  432. newbuf[newlen++]=buffer[length-1];
  433. } else if (length>2 && buffer[flag_offset]==0xa5) {
  434. LogWrite(PACKET__DEBUG, 0, "Packet", "In Decompress 2");
  435. memcpy(newbuf+flag_offset,buffer+flag_offset+1,length-(flag_offset+1));
  436. newlen=length-1;
  437. } else {
  438. memcpy(newbuf,buffer,length);
  439. newlen=length;
  440. }
  441. return newlen;
  442. }
  443. uint32 EQProtocolPacket::Compress(const unsigned char *buffer, const uint32 length, unsigned char *newbuf, uint32 newbufsize) {
  444. uint32 flag_offset=1,newlength;
  445. //dump_message_column(buffer,length,"Before: ");
  446. newbuf[0]=buffer[0];
  447. if (buffer[0]==0) {
  448. flag_offset=2;
  449. newbuf[1]=buffer[1];
  450. }
  451. if (length>30) {
  452. newlength=Deflate(const_cast<unsigned char *>(buffer+flag_offset),length-flag_offset,newbuf+flag_offset+1,newbufsize);
  453. *(newbuf+flag_offset)=0x5a;
  454. newlength+=flag_offset+1;
  455. } else {
  456. memmove(newbuf+flag_offset+1,buffer+flag_offset,length-flag_offset);
  457. *(newbuf+flag_offset)=0xa5;
  458. newlength=length+1;
  459. }
  460. //dump_message_column(newbuf,length,"After: ");
  461. return newlength;
  462. }
  463. void EQProtocolPacket::ChatDecode(unsigned char *buffer, int size, int DecodeKey)
  464. {
  465. if (buffer[1]!=0x01 && buffer[0]!=0x02 && buffer[0]!=0x1d) {
  466. int Key=DecodeKey;
  467. unsigned char *test=(unsigned char *)malloc(size);
  468. buffer+=2;
  469. size-=2;
  470. int i;
  471. for (i = 0 ; i+4 <= size ; i+=4)
  472. {
  473. int pt = (*(int*)&buffer[i])^(Key);
  474. Key = (*(int*)&buffer[i]);
  475. *(int*)&test[i]=pt;
  476. }
  477. unsigned char KC=Key&0xFF;
  478. for ( ; i < size ; i++)
  479. {
  480. test[i]=buffer[i]^KC;
  481. }
  482. memcpy(buffer,test,size);
  483. free(test);
  484. }
  485. }
  486. void EQProtocolPacket::ChatEncode(unsigned char *buffer, int size, int EncodeKey)
  487. {
  488. if (buffer[1]!=0x01 && buffer[0]!=0x02 && buffer[0]!=0x1d) {
  489. int Key=EncodeKey;
  490. char *test=(char*)malloc(size);
  491. int i;
  492. buffer+=2;
  493. size-=2;
  494. for ( i = 0 ; i+4 <= size ; i+=4)
  495. {
  496. int pt = (*(int*)&buffer[i])^(Key);
  497. Key = pt;
  498. *(int*)&test[i]=pt;
  499. }
  500. unsigned char KC=Key&0xFF;
  501. for ( ; i < size ; i++)
  502. {
  503. test[i]=buffer[i]^KC;
  504. }
  505. memcpy(buffer,test,size);
  506. free(test);
  507. }
  508. }
  509. void DumpPacketHex(const EQApplicationPacket* app)
  510. {
  511. DumpPacketHex(app->pBuffer, app->size);
  512. }
  513. void DumpPacketAscii(const EQApplicationPacket* app)
  514. {
  515. DumpPacketAscii(app->pBuffer, app->size);
  516. }
  517. void DumpPacket(const EQProtocolPacket* app) {
  518. DumpPacketHex(app->pBuffer, app->size);
  519. }
  520. void DumpPacket(const EQApplicationPacket* app, bool iShowInfo) {
  521. if (iShowInfo) {
  522. cout << "Dumping Applayer: 0x" << hex << setfill('0') << setw(4) << app->GetOpcode() << dec;
  523. cout << " size:" << app->size << endl;
  524. }
  525. DumpPacketHex(app->pBuffer, app->size);
  526. // DumpPacketAscii(app->pBuffer, app->size);
  527. }
  528. void DumpPacketBin(const EQApplicationPacket* app) {
  529. DumpPacketBin(app->pBuffer, app->size);
  530. }