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