EQPacket.cpp 15 KB

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