packet.h

Go to the documentation of this file.
00001 ///
00002 /// \file       packet.h
00003 ///             Low level protocol packet builder class.
00004 ///             Has knowledge of specific protocol commands in order
00005 ///             to hide protocol details behind an API.
00006 ///
00007 
00008 /*
00009     Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
00010 
00011     This program is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     This program is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00019 
00020     See the GNU General Public License in the COPYING file at the
00021     root directory of this project for more details.
00022 */
00023 
00024 #ifndef __BARRY_PACKET_H__
00025 #define __BARRY_PACKET_H__
00026 
00027 #include <string>
00028 #include <stdint.h>
00029 #include <unistd.h>
00030 #include "protocol.h"
00031 #include "data.h"
00032 
00033 namespace Barry { class Data; }
00034 
00035 namespace Barry {
00036 
00037 // forward declarations
00038 class Parser;
00039 class Builder;
00040 class SocketZero;
00041 class Socket;
00042 class IConverter;
00043 namespace Mode {
00044         class Desktop;
00045         class JavaLoader;
00046 }
00047 
00048 class Packet
00049 {
00050         friend class SocketZero;
00051         friend class SocketBase;
00052 
00053 protected:
00054         Data &m_send;
00055         Data *m_receive;
00056 
00057         Data& GetSend() { return m_send; }
00058         Data& GetReceive() { return *m_receive; }
00059 
00060 public:
00061         Packet(Data &send, Data &receive)
00062                 : m_send(send), m_receive(&receive)
00063                 {}
00064         virtual ~Packet() {}
00065 
00066         // allow user to override the receive buffer for
00067         // optimization purposes, to reduce copies... be
00068         // careful with this, since this new Data object
00069         // must outlive any usage of it via this Packet class
00070         void SetNewReceive(Data &receive) { m_receive = &receive; }
00071 
00072         //////////////////////////////////
00073         // common response analysis
00074 
00075         unsigned int Command() const;   // throws Error if receive isn't big enough
00076 };
00077 
00078 //
00079 // ZeroPacket class
00080 //
00081 /// Provides an API for building and analyzing socket-0 protocol packets.
00082 /// This class relies on 2 external objects: a send and receive Data buffer.
00083 ///
00084 /// Note that the receive buffer may be modified
00085 /// during a packet send, and this class provides API helpers
00086 /// to analyze the results.
00087 ///
00088 class ZeroPacket : public Packet
00089 {
00090         friend class Socket;
00091 
00092 public:
00093         ZeroPacket(Data &send, Data &receive);
00094         ~ZeroPacket();
00095 
00096         //////////////////////////////////
00097         // meta access
00098 
00099         //////////////////////////////////
00100         // packet building
00101 
00102         void GetAttribute(unsigned int object, unsigned int attribute);
00103         void Echo(uint64_t us_ticks);
00104         void Reset();
00105 
00106 
00107         //////////////////////////////////
00108         // response analysis
00109 
00110         unsigned int ObjectID() const;
00111         unsigned int AttributeID() const;
00112         uint32_t ChallengeSeed() const;
00113         unsigned int RemainingTries() const;
00114         unsigned int SocketResponse() const;
00115         unsigned char SocketSequence() const;
00116         uint8_t CommandResponse() const;
00117 };
00118 
00119 
00120 //
00121 // DBPacket class
00122 //
00123 /// Provides an API for building and analyzing raw DB protocol packets.
00124 /// This class relies on 3 external objects: a Mode::Desktop object,
00125 /// a send Data buffer, and a receive data buffer.  Socket and
00126 /// connection details are retrieved on a readonly basis from the
00127 /// Mode::Desktop object, but both send and receive buffers can be
00128 /// modified.
00129 ///
00130 /// Note that the receive buffer may be modified
00131 /// during a packet send, and this DBPacket class provides API helpers
00132 /// to analyze the results.
00133 ///
00134 class DBPacket : public Packet
00135 {
00136         friend class Socket;
00137 
00138 private:
00139         Mode::Desktop &m_con;
00140         unsigned int m_last_dbop;       // last database operation
00141 
00142 protected:
00143 
00144 public:
00145         DBPacket(Mode::Desktop &con, Data &send, Data &receive);
00146         ~DBPacket();
00147 
00148         //////////////////////////////////
00149         // meta access
00150 
00151         //////////////////////////////////
00152         // packet building
00153 
00154         // commands that correspond to the DB operation
00155         // constants in protocol.h
00156         void ClearDatabase(unsigned int dbId);
00157         void GetDBDB();
00158         void GetRecordStateTable(unsigned int dbId);
00159         void SetRecordFlags(unsigned int dbId, unsigned int stateTableIndex, uint8_t flag1);
00160         void DeleteRecordByIndex(unsigned int dbId, unsigned int stateTableIndex);
00161         void GetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex);
00162         bool SetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex, Builder &build, const IConverter *ic);
00163         void GetRecords(unsigned int dbId);
00164         bool AddRecord(unsigned int dbId, Builder &build, const IConverter *ic);
00165 
00166 
00167         //////////////////////////////////
00168         // response analysis
00169 
00170         // DB command response functions
00171         unsigned int ReturnCode() const;        // throws FIXME if packet doesn't support it
00172         unsigned int DBOperation() const; // throws Error on size trouble
00173 
00174         bool Parse(Parser &parser, const std::string &dbname,
00175                 const IConverter *ic); // switches based on last m_send command
00176         bool ParseMeta(DBData &data);
00177 
00178         // response parsers
00179 };
00180 
00181 
00182 //
00183 // JLPacket class
00184 //
00185 /// Provides an API for building and analyzing raw Javaloader protocol packets.
00186 /// This class relies on 3 external objects:
00187 /// a command send Data buffer (which can be fairly small), a data
00188 /// or argument send Data buffer, and a receive data buffer.  Socket and
00189 /// connection details are retrieved on a readonly basis from the
00190 /// Mode::JavaLoader object, but all buffers can be modified.
00191 ///
00192 /// Note that the receive buffer may be modified
00193 /// during a packet send, and this JLPacket class provides API helpers
00194 /// to analyze the results.
00195 ///
00196 class JLPacket : public Packet
00197 {
00198         friend class SocketBase;
00199 
00200 private:
00201         Data &m_cmd, &m_data;
00202         int m_last_set_size;
00203 
00204 public:
00205         JLPacket(Data &cmd, Data &send, Data &receive);
00206         ~JLPacket();
00207 
00208         //////////////////////////////////
00209         // meta access
00210 
00211         bool HasData() const    { return m_last_set_size == 2; }
00212         Data& GetReceive()      { return *m_receive; }
00213 
00214         //////////////////////////////////
00215         // packet building
00216 
00217         // commands that correspond to the operation
00218         // constants in protocol.h
00219 
00220         // returns 1 or 2 depending on whether cmd or cmd+send are available
00221         int SimpleCmd(uint8_t cmd, uint8_t unknown = 0, uint16_t size = 0);
00222         int SimpleData(const void *data, uint16_t size);
00223         int BigEndianData(uint16_t value);
00224         int BigEndianData(uint32_t value);
00225 
00226         int Hello()             { return SimpleCmd(SB_COMMAND_JL_HELLO); }
00227         int Goodbye()           { return SimpleCmd(SB_COMMAND_JL_GOODBYE); }
00228         int SetUnknown1();
00229         int SetCodFilename(const std::string &filename);
00230         int SetCodSize(off_t size);
00231         int SetTime(time_t when);
00232         int GetScreenshot();
00233         int GetData()           { return SimpleCmd(SB_COMMAND_JL_SEND_DATA); }
00234         int DeviceInfo()        { return SimpleCmd(SB_COMMAND_JL_DEVICE_INFO); }
00235         int OsMetrics()         { return SimpleCmd(SB_COMMAND_JL_OS_METRICS); }
00236         int BootromMetrics()    { return SimpleCmd(SB_COMMAND_JL_BOOTROM_METRICS); }
00237         int GetDirectory()      { return SimpleCmd(SB_COMMAND_JL_GET_DIRECTORY); }
00238         int GetSubDir(uint16_t id);
00239         int GetDirEntry(uint8_t entry_cmd, uint16_t id);
00240         int Erase(uint16_t cmd, uint16_t id);
00241         int GetEventlog()       { return SimpleCmd(SB_COMMAND_JL_GET_LOG); }
00242         int GetEventlogEntry(uint16_t entry_num);
00243         int ClearEventlog()     { return SimpleCmd(SB_COMMAND_JL_CLEAR_LOG); }
00244         int SaveModule(uint16_t id);
00245         int PutData(const void *data, uint16_t size);
00246         int WipeApps()          { return SimpleCmd(SB_COMMAND_JL_WIPE_APPS); }
00247         int WipeFs()            { return SimpleCmd(SB_COMMAND_JL_WIPE_FS); }
00248         int LogStackTraces()    { return SimpleCmd(SB_COMMAND_JL_LOG_STRACES); }
00249         int ResetToFactory()    { return SimpleCmd(SB_COMMAND_JL_RESET_FACTORY); }
00250 
00251         //////////////////////////////////
00252         // response analysis
00253         unsigned int Size();
00254 };
00255 
00256 
00257 //
00258 // JVMPacket class
00259 //
00260 /// Provides an API for building and analyzing raw JavaDebug protocol packets.
00261 /// This class relies on 3 external objects:
00262 /// a command send Data buffer (which can be fairly small), a data
00263 /// or argument send Data buffer, and a receive data buffer.  Socket and
00264 /// connection details are retrieved on a readonly basis from the
00265 /// Mode::JavaDebug object, but all buffers can be modified.
00266 ///
00267 /// Note that the receive buffer may be modified
00268 /// during a packet send, and this JVMPacket class provides API helpers
00269 /// to analyze the results.
00270 ///
00271 class JVMPacket : public Packet
00272 {
00273         friend class SocketBase;
00274 
00275 private:
00276         Data &m_cmd;
00277 
00278 public:
00279         JVMPacket(Data &cmd, Data &receive);
00280         ~JVMPacket();
00281 
00282         //////////////////////////////////
00283         // meta access
00284 
00285         Data& GetReceive()      { return *m_receive; }
00286 
00287         //////////////////////////////////
00288         // packet building
00289 
00290         // commands that correspond to the operation
00291         // constants in protocol.h
00292 
00293         // returns 1 or 2 depending on whether cmd or cmd+send are available
00294         void SimpleCmd(uint8_t cmd);
00295         void ComplexCmd(uint8_t cmd, const void *param, uint16_t size = 0);
00296 
00297         void Unknown01();       // Command 0x53
00298         void Unknown02();       // Command 0x01
00299         void Unknown03();       // Command 0x6f
00300         void Unknown04();       // Command 0x8a
00301         void Unknown05();       // Command 0x90
00302         void Unknown06();       // Command 0x44
00303         void Unknown07();       // Command 0x45
00304         void Unknown08();       // Command 0x54
00305         void Unknown09();       // Command 0x33
00306         void Unknown10();       // Command 0x46
00307         void Unknown11(uint32_t id);    // Command 0x0e
00308         void Unknown12(uint32_t id);    // Command 0x50
00309         void Unknown13(uint32_t id);    // Command 0x0d
00310         void Unknown14(uint32_t id);    // Command 0x85
00311         void Unknown15(uint32_t id);    // Command 0x84
00312         void GetModulesList(uint32_t id);       // Command 0x8d
00313         void GetThreadsList();  // Command 0x08
00314         void GetConsoleMessage();
00315         void Go();      // Command mal formed :)
00316         void Stop();    // Command 0x02
00317         void GetStatus();       // Command 0x06
00318 
00319         //////////////////////////////////
00320         // response analysis
00321         unsigned int Size();
00322 };
00323 
00324 
00325 } // namespace Barry
00326 
00327 #endif
00328