Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

evauhpacket.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004-2006 by yunfan                                     *
00003  *   yunfan_zg@163.com                                                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/ 
00020 
00021 #include "evauhpacket.h"
00022 #include "../evadefines.h"
00023 #ifdef _WIN32
00024 #include <winsock.h>
00025 #else
00026 #include <arpa/inet.h>
00027 #endif
00028 #include <string.h>
00029 #include <stdio.h>
00030 
00031 unsigned short EvaUHPacket::seq_random    = 0;
00032 unsigned short EvaUHPacket::seq_info      = 0;
00033 unsigned short EvaUHPacket::seq_transfer = 0;
00034 
00035 EvaUHPacket::EvaUHPacket(const unsigned char *buf, const int len)
00036         : buffer(NULL), mBufferLen(len), mVersion(QQ_CLIENT_VERSION),
00037          mIsData(false), mNumPackets(1), mPacketNum(0)
00038 {
00039         if(!buf) {
00040                 mBodyStart = -1; // means error occurred
00041                 return;
00042         }
00043 #ifdef WIN32
00044         buffer = (unsigned char*)_alloca(mBufferLen);
00045 #else
00046         buffer = new unsigned char[mBufferLen];
00047 #endif
00048         memcpy(buffer, buf, mBufferLen);
00049 }
00050 
00051 EvaUHPacket::EvaUHPacket(const bool isInfo)
00052         : buffer(NULL), mVersion(QQ_CLIENT_VERSION), mIsData(false), 
00053         mNumPackets(1), mPacketNum(0)
00054 {
00055         mCommand = isInfo?UH_CMD_INFO:UH_CMD_TRANSFER;
00056 }
00057 
00058 EvaUHPacket::~EvaUHPacket()
00059 {
00060         if(buffer) delete buffer;
00061 }
00062 
00063 const bool EvaUHPacket::parse()
00064 {
00065         if(mBodyStart == -1) return false;
00066         mBodyStart = parseHead();
00067         if(!mBodyStart) return false;
00068         return parseBody();
00069 }
00070 
00071 const bool EvaUHPacket::fill(unsigned char *buf, int *len)
00072 {
00073         if(!fillHead(buf)) return false;
00074         int tmp;
00075         bool result = fillBody(buf + mBodyStart,&tmp);
00076         (*len) = mBodyStart + tmp;
00077         return result;
00078 }
00079 
00080 const bool EvaUHPacket::parseBody()
00081 {
00082         fprintf(stderr, "EvaUHPacket::parseBody\n");
00083         return false;
00084 }
00085 
00086 const bool EvaUHPacket::fillBody(unsigned char *buf, int *len)
00087 {
00088         fprintf(stderr, "EvaUHPacket::fillBody\n");
00089         return false;
00090 }
00091 
00092 const int EvaUHPacket::parseHead()
00093 {
00094         int pos = 0;
00095         if(buffer[pos++] != UH_HEAD_TAG) return false;
00096         mCommand = buffer[pos++];
00097         pos+=2; // random sequence
00098         pos+=2; // 2 bytes 0x00
00099         pos+=2; // packet sequence which depends on the mCommand, don't care, ignore
00100         pos+=6; // 6 bytes 0x00
00101 
00102         unsigned short tmp2;
00103         memcpy(&tmp2, buffer+pos, 2);
00104         mIsData = ntohs(tmp2)?true:false;
00105         pos+=2; 
00106 
00107         pos+=24; // 24 bytes 0x00
00108         mNumPackets = buffer[pos++];
00109         mPacketNum = buffer[pos++];
00110         
00111         pos++; // 0x00, 1 byte
00112         pos+=2; // version, also always 0x0000 for server
00113         pos++; // 0x00, 1 byte
00114 
00115         return pos;
00116 }
00117 
00118 const bool EvaUHPacket::fillHead(unsigned char *buf)
00119 {
00120         if(!buf) return false;
00121         int pos = 0;
00122 
00123         buf[pos++]=UH_HEAD_TAG;
00124         buf[pos++]=mCommand;
00125 
00126         unsigned short tmp2;
00127         tmp2 = htons(seq_random++);
00128         memcpy(buf+pos, &tmp2, 2); pos+=2;
00129         
00130         memset(buf+pos, 0, 2); pos+=2;
00131 
00132         if(mCommand==UH_CMD_INFO){
00133                 tmp2 = htons(seq_info++);
00134         } else if(mCommand==UH_CMD_TRANSFER){
00135                         tmp2=htons(seq_transfer++);
00136                 } else {
00137                         return false;
00138                 }
00139         memcpy(buf+pos, &tmp2, 2); pos+=2;
00140 
00141         memset(buf+pos, 0, 6); pos+=6;
00142         memset(buf+pos, 0, 2); pos+=2; // mIsData always be 0x0000 in client side
00143         memset(buf+pos, 0, 24); pos+=24;        
00144         
00145         if(mPacketNum >= mNumPackets)
00146                 return false;
00147         buf[pos++] = mNumPackets;
00148         buf[pos++] = mPacketNum;
00149 
00150         buf[pos++] = 0x00;
00151 
00152         tmp2 = htons(mVersion);
00153         memcpy(buf+pos, &tmp2, 2); pos+=2;
00154 
00155         buf[pos++] = 0x00;
00156         mBodyStart = pos;
00157         return true;
00158 }

Generated on Mon May 15 20:48:42 2006 for libeva by  doxygen 1.4.4