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-2010, 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 "protocol.h"
00030 
00031 namespace Barry { class Data; }
00032 
00033 namespace Barry {
00034 
00035 // forward declarations
00036 class Parser;
00037 class Builder;
00038 class SocketZero;
00039 class Socket;
00040 class IConverter;
00041 namespace Mode {
00042         class Desktop;
00043         class JavaLoader;
00044 }
00045 
00046 class Packet
00047 {
00048         friend class SocketZero;
00049         friend class Socket;
00050 
00051 protected:
00052         Data &m_send, &m_receive;
00053 
00054         Data& GetSend() { return m_send; }
00055         Data& GetReceive() { return m_receive; }
00056 
00057 public:
00058         Packet(Data &send, Data &receive)
00059                 : m_send(send), m_receive(receive)
00060                 {}
00061         virtual ~Packet() {}
00062 
00063         //////////////////////////////////
00064         // common response analysis
00065 
00066         unsigned int Command() const;   // throws Error if receive isn't big enough
00067 };
00068 
00069 //
00070 // ZeroPacket class
00071 //
00072 /// Provides an API for building and analyzing socket-0 protocol packets.
00073 /// This class relies on 2 external objects: a send and receive Data buffer.
00074 ///
00075 /// Note that the receive buffer may be modified
00076 /// during a packet send, and this class provides API helpers
00077 /// to analyze the results.
00078 ///
00079 class ZeroPacket : public Packet
00080 {
00081         friend class Socket;
00082 
00083 public:
00084         ZeroPacket(Data &send, Data &receive);
00085         ~ZeroPacket();
00086 
00087         //////////////////////////////////
00088         // meta access
00089 
00090         //////////////////////////////////
00091         // packet building
00092 
00093         void GetAttribute(unsigned int object, unsigned int attribute);
00094         void Echo(uint64_t us_ticks);
00095         void Reset();
00096 
00097 
00098         //////////////////////////////////
00099         // response analysis
00100 
00101         unsigned int ObjectID() const;
00102         unsigned int AttributeID() const;
00103         uint32_t ChallengeSeed() const;
00104         unsigned int RemainingTries() const;
00105         unsigned int SocketResponse() const;
00106         unsigned char SocketSequence() const;
00107         uint8_t CommandResponse() const;
00108 };
00109 
00110 
00111 //
00112 // DBPacket class
00113 //
00114 /// Provides an API for building and analyzing raw DB protocol packets.
00115 /// This class relies on 3 external objects: a Mode::Desktop object,
00116 /// a send Data buffer, and a receive data buffer.  Socket and
00117 /// connection details are retrieved on a readonly basis from the
00118 /// Mode::Desktop object, but both send and receive buffers can be
00119 /// modified.
00120 ///
00121 /// Note that the receive buffer may be modified
00122 /// during a packet send, and this DBPacket class provides API helpers
00123 /// to analyze the results.
00124 ///
00125 class DBPacket : public Packet
00126 {
00127         friend class Socket;
00128 
00129 private:
00130         Mode::Desktop &m_con;
00131         unsigned int m_last_dbop;       // last database operation
00132 
00133 protected:
00134 
00135 public:
00136         DBPacket(Mode::Desktop &con, Data &send, Data &receive);
00137         ~DBPacket();
00138 
00139         //////////////////////////////////
00140         // meta access
00141 
00142         //////////////////////////////////
00143         // packet building
00144 
00145         // commands that correspond to the DB operation
00146         // constants in protocol.h
00147         void ClearDatabase(unsigned int dbId);
00148         void GetDBDB();
00149         void GetRecordStateTable(unsigned int dbId);
00150         void SetRecordFlags(unsigned int dbId, unsigned int stateTableIndex, uint8_t flag1);
00151         void DeleteRecordByIndex(unsigned int dbId, unsigned int stateTableIndex);
00152         void GetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex);
00153         bool SetRecordByIndex(unsigned int dbId, unsigned int stateTableIndex, Builder &build, const IConverter *ic);
00154         void GetRecords(unsigned int dbId);
00155         bool SetRecord(unsigned int dbId, Builder &build, const IConverter *ic);
00156 
00157 
00158         //////////////////////////////////
00159         // response analysis
00160 
00161         // DB command response functions
00162         unsigned int ReturnCode() const;        // throws FIXME if packet doesn't support it
00163         unsigned int DBOperation() const; // throws Error on size trouble
00164 
00165         bool Parse(Parser &parser, const IConverter *ic); // switches based on last m_send command
00166 
00167         // response parsers
00168 };
00169 
00170 
00171 //
00172 // JLPacket class
00173 //
00174 /// Provides an API for building and analyzing raw Javaloader protocol packets.
00175 /// This class relies on 3 external objects:
00176 /// a command send Data buffer (which can be fairly small), a data
00177 /// or argument send Data buffer, and a receive data buffer.  Socket and
00178 /// connection details are retrieved on a readonly basis from the
00179 /// Mode::JavaLoader object, but all buffers can be modified.
00180 ///
00181 /// Note that the receive buffer may be modified
00182 /// during a packet send, and this JLPacket class provides API helpers
00183 /// to analyze the results.
00184 ///
00185 class JLPacket : public Packet
00186 {
00187         friend class Socket;
00188 
00189 private:
00190         Data &m_cmd, &m_data;
00191         int m_last_set_size;
00192 
00193 public:
00194         JLPacket(Data &cmd, Data &send, Data &receive);
00195         ~JLPacket();
00196 
00197         //////////////////////////////////
00198         // meta access
00199 
00200         bool HasData() const    { return m_last_set_size == 2; }
00201         Data& GetReceive()      { return m_receive; }
00202 
00203         //////////////////////////////////
00204         // packet building
00205 
00206         // commands that correspond to the operation
00207         // constants in protocol.h
00208 
00209         // returns 1 or 2 depending on whether cmd or cmd+send are available
00210         int SimpleCmd(uint8_t cmd, uint8_t unknown = 0, uint16_t size = 0);
00211         int SimpleData(const void *data, uint16_t size);
00212         int BigEndianData(uint16_t value);
00213         int BigEndianData(uint32_t value);
00214 
00215         int Hello()             { return SimpleCmd(SB_COMMAND_JL_HELLO); }
00216         int Goodbye()           { return SimpleCmd(SB_COMMAND_JL_GOODBYE); }
00217         int SetUnknown1();
00218         int SetCodFilename(const std::string &filename);
00219         int SetCodSize(off_t size);
00220         int SetTime(time_t when);
00221         int GetScreenshot();
00222         int GetData()           { return SimpleCmd(SB_COMMAND_JL_SEND_DATA); }
00223         int DeviceInfo()        { return SimpleCmd(SB_COMMAND_JL_DEVICE_INFO); }
00224         int OsMetrics()         { return SimpleCmd(SB_COMMAND_JL_OS_METRICS); }
00225         int BootromMetrics()    { return SimpleCmd(SB_COMMAND_JL_BOOTROM_METRICS); }
00226         int GetDirectory()      { return SimpleCmd(SB_COMMAND_JL_GET_DIRECTORY); }
00227         int GetSubDir(uint16_t id);
00228         int GetDirEntry(uint8_t entry_cmd, uint16_t id);
00229         int Erase(uint16_t cmd, uint16_t id);
00230         int GetEventlog()       { return SimpleCmd(SB_COMMAND_JL_GET_LOG); }
00231         int GetEventlogEntry(uint16_t entry_num);
00232         int ClearEventlog()     { return SimpleCmd(SB_COMMAND_JL_CLEAR_LOG); }
00233         int SaveModule(uint16_t id);
00234         int PutData(const void *data, uint16_t size);
00235         int WipeApps()          { return SimpleCmd(SB_COMMAND_JL_WIPE_APPS); }
00236         int WipeFs()            { return SimpleCmd(SB_COMMAND_JL_WIPE_FS); }
00237         int LogStackTraces()    { return SimpleCmd(SB_COMMAND_JL_LOG_STRACES); }
00238         int ResetToFactory()    { return SimpleCmd(SB_COMMAND_JL_RESET_FACTORY); }
00239 
00240         //////////////////////////////////
00241         // response analysis
00242         unsigned int Size();
00243 };
00244 
00245 
00246 //
00247 // JVMPacket class
00248 //
00249 /// Provides an API for building and analyzing raw JavaDebug protocol packets.
00250 /// This class relies on 3 external objects:
00251 /// a command send Data buffer (which can be fairly small), a data
00252 /// or argument send Data buffer, and a receive data buffer.  Socket and
00253 /// connection details are retrieved on a readonly basis from the
00254 /// Mode::JavaDebug object, but all buffers can be modified.
00255 ///
00256 /// Note that the receive buffer may be modified
00257 /// during a packet send, and this JVMPacket class provides API helpers
00258 /// to analyze the results.
00259 ///
00260 class JVMPacket : public Packet
00261 {
00262         friend class Socket;
00263 
00264 private:
00265         Data &m_cmd;
00266 
00267 public:
00268         JVMPacket(Data &cmd, Data &receive);
00269         ~JVMPacket();
00270 
00271         //////////////////////////////////
00272         // meta access
00273 
00274         Data& GetReceive()      { return m_receive; }
00275 
00276         //////////////////////////////////
00277         // packet building
00278 
00279         // commands that correspond to the operation
00280         // constants in protocol.h
00281 
00282         // returns 1 or 2 depending on whether cmd or cmd+send are available
00283         void SimpleCmd(uint8_t cmd);
00284         void ComplexCmd(uint8_t cmd, const void *param, uint16_t size = 0);
00285 
00286         void Unknown01();       // Command 0x53
00287         void Unknown02();       // Command 0x01
00288         void Unknown03();       // Command 0x6f
00289         void Unknown04();       // Command 0x8a
00290         void Unknown05();       // Command 0x90
00291         void Unknown06();       // Command 0x44
00292         void Unknown07();       // Command 0x45
00293         void Unknown08();       // Command 0x54
00294         void Unknown09();       // Command 0x33
00295         void Unknown10();       // Command 0x46
00296         void Unknown11(uint32_t id);    // Command 0x0e
00297         void Unknown12(uint32_t id);    // Command 0x50
00298         void Unknown13(uint32_t id);    // Command 0x0d
00299         void Unknown14(uint32_t id);    // Command 0x85
00300         void Unknown15(uint32_t id);    // Command 0x84
00301         void GetModulesList(uint32_t id);       // Command 0x8d
00302         void GetThreadsList();  // Command 0x08
00303         void GetConsoleMessage();
00304         void Go();      // Command mal formed :)
00305         void Stop();    // Command 0x02
00306         void GetStatus();       // Command 0x06
00307 
00308         //////////////////////////////////
00309         // response analysis
00310         unsigned int Size();
00311 };
00312 
00313 
00314 } // namespace Barry
00315 
00316 #endif
00317 
Generated by  doxygen 1.6.2-20100208