00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "evaftpacket.h"
00022 #include "../evautil.h"
00023 #include <string.h>
00024 #include <stdio.h>
00025
00026
00027 EvaFTPacket::EvaFTPacket(const unsigned char *buf, const int len)
00028 : m_HeaderLength(0), m_HasTags(false), m_HasLength(false),
00029 m_LengthOffset(0), m_Buffer(NULL), m_BufferLength(len)
00030 {
00031 m_Buffer = new unsigned char[m_BufferLength];
00032 memcpy(m_Buffer, buf, m_BufferLength);
00033 }
00034
00035 EvaFTPacket::EvaFTPacket()
00036 : m_HeaderLength(0), m_HasTags(false), m_Buffer(NULL), m_BufferLength(0)
00037 {
00038 }
00039
00040 EvaFTPacket::~EvaFTPacket()
00041 {
00042 if(m_Buffer) delete m_Buffer;
00043 }
00044
00045 const bool EvaFTPacket::parse()
00046 {
00047 if(!m_Buffer) return false;
00048 int pos = 0;
00049 if(m_HasTags){
00050 m_Tag = m_Buffer[0];
00051 m_Tail = m_Buffer[m_BufferLength - 1];
00052 pos++;
00053 }
00054 m_HeaderLength = parseHeader(m_Buffer + pos);
00055 if(m_HeaderLength == -1) return false;
00056 pos += m_HeaderLength;
00057 return parseBody(m_Buffer + pos, m_BufferLength - pos - (m_HasTags?1:0));
00058 }
00059
00060 const bool EvaFTPacket::fill(unsigned char *buf, int *len)
00061 {
00062 int pos=0;
00063 if(m_HasTags){
00064 buf[pos++] = m_Tag;
00065 }
00066
00067 int partLen = 0;
00068
00069 partLen = fillHeader(buf + pos);
00070 if(partLen == -1) return false;
00071 pos += partLen;
00072
00073 partLen = fillBody(buf + pos);
00074 if(partLen == -1) return false;
00075 pos += partLen;
00076
00077 if(m_HasTags){
00078 buf[pos++] = m_Tail;
00079 }
00080 *len = pos;
00081
00082
00083 if(m_HasLength)
00084 EvaUtil::write16(buf + m_LengthOffset, *len);
00085 return true;
00086 }
00087
00088 void EvaFTPacket::setPacketTag(const unsigned char tag, const unsigned char tail)
00089 {
00090 m_HasTags = true;
00091 m_Tag = tag;
00092 m_Tail = tail;
00093 }
00094
00095 const int EvaFTPacket::parseHeader(unsigned char *)
00096 {
00097 fprintf(stderr, "EvaFTPacket::parseHeader -- Not implemented!\n");
00098 return -1;
00099 }
00100
00101 const bool EvaFTPacket::parseBody(unsigned char *, const int )
00102 {
00103 fprintf(stderr, "EvaFTPacket::parseBody -- Not implemented!\n");
00104 return false;
00105 }
00106
00107 const int EvaFTPacket::fillHeader(unsigned char *)
00108 {
00109 fprintf(stderr, "EvaFTPacket::fillHeader -- Not implemented!\n");
00110 return -1;
00111 }
00112
00113 const int EvaFTPacket::fillBody(unsigned char *)
00114 {
00115 fprintf(stderr, "EvaFTPacket::fillBody -- Not implemented!\n");
00116 return -1;
00117 }
00118
00119
00123 EvaFTAgentPacket::EvaFTAgentPacket(const unsigned char *buf, const int len)
00124 : EvaFTPacket(buf, len)
00125 {
00126 m_HasTags = true;
00127 }
00128
00129 EvaFTAgentPacket::EvaFTAgentPacket(const short cmd)
00130 : EvaFTPacket(), m_Command(cmd)
00131 {
00132 setPacketTag(QQ_FILE_AGENT_PACKET_TAG, QQ_FILE_AGENT_PACKET_TAIL);
00133 m_HasLength = true;
00134 m_LengthOffset = 3;
00135 }
00136
00137 const int EvaFTAgentPacket::parseHeader(unsigned char *buf)
00138 {
00139 if(getTag() != QQ_FILE_AGENT_PACKET_TAG || getTail() != QQ_FILE_AGENT_PACKET_TAIL)
00140 return -1;
00141 int pos = 0;
00142 m_Version = EvaUtil::read16(buf + pos); pos+=2;
00143
00144 unsigned short tmp2 = EvaUtil::read16(buf + pos); pos+=2;
00145 if(tmp2 != getBodyLength()) return -1;
00146
00147 m_Command = EvaUtil::read16(buf + pos); pos+=2;
00148 m_Sequence = EvaUtil::read16(buf + pos); pos+=2;
00149 m_Id = EvaUtil::read32(buf + pos); pos+=4;
00150 pos+=8;
00151 if(m_Command != QQ_FILE_AGENT_CMD_CREATE){
00152 m_Session = EvaUtil::read32(buf + pos); pos+=4;
00153 }
00154
00155 return pos;
00156 }
00157
00158 const int EvaFTAgentPacket::fillHeader(unsigned char *buf)
00159 {
00160 int pos = 0;
00161 pos += EvaUtil::write16(buf + pos, m_Version);
00162 pos +=2;
00163 pos += EvaUtil::write16(buf + pos, m_Command);
00164 pos += EvaUtil::write16(buf + pos, m_Sequence);
00165 pos += EvaUtil::write32(buf + pos, m_Id);
00166 pos += 8;
00167
00168 if(m_Command != QQ_FILE_AGENT_CMD_CREATE){
00169 pos += EvaUtil::write32(buf + pos, m_Session);
00170 }
00171 return pos;
00172 }
00173
00174 void EvaFTAgentPacket::setFileAgentKey(const unsigned char *key)
00175 {
00176 if(!key) return;
00177 memcpy(m_FileAgentKey, key, 16);
00178 }
00179
00180
00181
00182
00186 EvaFTSynPacket::EvaFTSynPacket(const unsigned char *buf, const int len)
00187 : EvaFTPacket(buf, len)
00188 {
00189 }
00190
00191 EvaFTSynPacket::EvaFTSynPacket(const short cmd)
00192 : EvaFTPacket(), m_Command(cmd)
00193 {
00194 m_HasLength = true;
00195 m_LengthOffset = 1;
00196 }
00197
00198 const int EvaFTSynPacket::parseHeader(unsigned char *buf)
00199 {
00200 int pos = 0;
00201 if(buf[pos++] != m_StartTag) return -1;
00202
00203 unsigned short tmp2 = EvaUtil::read16(buf + pos); pos+=2;
00204 if(tmp2 != getBodyLength()) return -1;
00205
00206 m_Version = EvaUtil::read16(buf + pos); pos+=2;
00207 m_Sequence = EvaUtil::read16(buf + pos); pos+=2;
00208 m_Id = EvaUtil::read32(buf + pos); pos+=4;
00209 m_Command = EvaUtil::read16(buf + pos); pos+=2;
00210
00211 return pos;
00212 }
00213
00214 const int EvaFTSynPacket::fillHeader(unsigned char *buf)
00215 {
00216 int pos = 0;
00217 buf[pos++] = m_StartTag;
00218 pos +=2;
00219 pos += EvaUtil::write16(buf + pos, m_Version);
00220 pos += EvaUtil::write16(buf + pos, m_Sequence);
00221 pos += EvaUtil::write32(buf + pos, m_Id);
00222 pos += EvaUtil::write16(buf + pos, m_Command);
00223
00224 return pos;
00225 }
00226
00227 void EvaFTSynPacket::setFileAgentKey(const unsigned char *key)
00228 {
00229 if(!key) return;
00230 memcpy(m_FileAgentKey, key, 16);
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265