Benvenuto! Per postare e godere delle funzionalità del forum registrati, occorrono pochi secondi ed è gratis!

Advanced Packet Processing
(Questo messaggio è stato modificato l'ultima volta il: 11/12/2012, 2:06 da Admin.)

maurow
Messaggi: 6,041
Discussioni: 2,066
Registrato: 03-2011
Reputazione:
133

Mix: 8,107
Hey guys,

Codice:
//
//  Packet.hpp
//
//  Created by underScore and Yazzn on 09. December 2012.
//  Copyright (c) 2012 WR-Cheats.net. All Rights reserved.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//

#ifndef __PACKET_HPP__
#define __PACKET_HPP__

#include <stdexcept>
#include <sstream>
#include <vector>
#include <windows.h>

#ifdef USE_ATOMIC_LOCK
#include <mutex>
#endif

#pragma comment (lib, "ws2_32.lib")
#pragma comment (lib, "Winmm.lib")

//
//  Packet
//
//  A tool for managing packets as from the free2play game War Rock.
//  Finalized packets are formatted like this:
//    <timestamp> <identifier> <parameter-0> .. <parameter-n> \n
//  Note that n is greater or equal to zero.
//  Define USE_ATOMIC_LOCK to avoid multithreading failure on Packet::send
//
class Packet
{
public:
  typedef unsigned short Identifier;
  typedef unsigned long Timestamp;
    
  typedef enum
  {
    ParameterTypeInteger,
    ParameterTypeFloat,
    ParameterTypeText
  } ParameterType;
    
  typedef struct
  {
    ParameterType type;
        
    union
    {
      int asInteger;
      float asFloat;
      char *asText;
    } value;
        
    /**
     * @symbol  public: const std::string Packet::Parameter::toString const
     * @return  Parameter.value converted to an object of type std::string
     **/
    const std::string toString () const;
  } Parameter;
    
private:
  static const char CHAR_GROUP_SEPERATOR = '\x1D';
    
#ifdef USE_ATOMIC_LOCK
  static std::mutex _atomicLock;
#endif
    
  Identifier _identifier;
  Timestamp _timestamp;
  std::vector<Parameter> _parameterList;
  bool _finalized;
  std::string _finalizedPacket;
    
  /**
   * @param   text: Parameter as zero terminated string
   * @return  Type of the given parameter as ParameterType
   * Finds out which type fits the given string best by matching expressions
   **/
  ParameterType findType (const char *text) const;
    
public:
  static const size_t INDEX_ERROR = 0xFFFFFFFF;
    
  /**
   * Initializes a new object of type Packet
   **/
  Packet ();
  
  /**
   * Frees an object of type Packet
   **/
  ~Packet ();
    
  /**
   * @param   finalizedPacket: A finalized packet string to be analysed
   * @param   xorKey: The key to decrypt the finalized packet string with
   * @return  this-pointer
   * Initializes the object based on a finalized packet string
   **/
  Packet *initWithFinalizedPacket (const std::string &finalizedPacket,
                                   const BYTE xorKey);
  /**
   * @param   socket: The socket to receive data from
   * @param   length: The number of bytes to receive
   * @param   flags: Flags to pass to ::recv
   * @param   xorKey: The key to decrypt the received packet with
   * @return  this-pointer
   * Initializes the object based on received data
   **/
  Packet *initWithRecv(const SOCKET socket,
                       const size_t length,
                       const int flags,
                       const BYTE xorKey);
  
  /**
   * @param   packet: The packet to copy data from
   * @return  this-pointer
   * Initializes the object as a copy of another Packet
   **/
  Packet *initWithPacket (const Packet &packet);
  
  /**
   * @param   identifier: The packets identifier
   * @return  this-pointer
   * Initializes the object with just an identifier
   **/
  Packet *initWithIdentifier (const Identifier identifier);
    
  /**
   * Resets the packets data to zero and unfinalizes it
   */
  void reset ();
    
  /**
   * @param   xorKey: The key to encrypt the packet with
   * Finalizes the packet to a string
   **/
  void finalize (const BYTE xorKey);
  
  /**
   * @return  Boolean indicating whether the object is finalized
   * Checks whether the obect is finalized
   **/
  const bool isFinalized () const;
  
  /**
   * @return  The finalized packet string
   * Returns the finalized packet string if the object is finalized
   **/
  const std::string &finalizedPacket () const;
    
  /**
   * @return  The identifier of the packet
   * Returns the packets identifier
   **/
  Identifier identifier () const;
  
  /**
   * @return  The packets timestamp
   * Returns the packets timestamp
   **/
  Timestamp timestamp () const;
  
  /**
   * @param   index: Index of the parameter
   * @return  The Parameter at the given index
   * Returns the parameter at a given index
   **/
  Parameter parameterAtIndex (const size_t index) const;
  
  /**
   * @return  The number of parameters the packet has
   * Returns the amount of parameters the packet has
   **/
  size_t parameterCount () const;
    
  /**
   * @param   value: An integer value
   * Appends the packet by an Parameter with ParameterTypeInteger
   **/
  void append (const int value);
  
    /**
   * @param   value: A floating point value
   * Appends the packet by an Parameter with ParameterTypeFloat
   **/
  void append (const float value);
    
  /**
   * @param   value: A zero terminated string
   * Appends the packet by an Parameter with ParameterTypeText
   **/
  void append (const char *value);
    
  /**
   * @param   value: The integer value to search for
   * @return  The index of the first Parameter found or Packet::INDEX_ERROR
   * Searches for a Parameter and returns the index
   **/
  const size_t find (const int value) const;

  /**
   * @param   value: The floating point value to search for
   * @return  The index of the first Parameter found or Packet::INDEX_ERROR
   * Searches for a Parameter and returns the index
   **/
  const size_t find (const float value) const;

  /**
   * @param   value: The zero terminated string to search for
   * @return  The index of the first Parameter found or Packet::INDEX_ERROR
   * Searches for a Parameter and returns the index
   **/
  const size_t find (const char *value) const;
    
  /**
   * @param   index: The index of the parameter to change
   * @param   value: The integer value to set
   * Sets a parameter at a given index to a given value
   **/
  void setParameterAtIndex (const size_t index,
                            const int value);

  /**
   * @param   index: The index of the parameter to change
   * @param   value: The floating point value to set
   * Sets a parameter at a given index to a given value
   **/
  void setParameterAtIndex (const size_t index,
                            const float value);
    
  /**
   * @param   index: The index of the parameter to change
   * @param   value: The zero terminated string to set
   * Sets a parameter at a given index to a given value
   **/
  void setParameterAtIndex (const size_t index,
                            const char *value);
    
  /**
   * @param   index: The index to insert the parameter at
   * @param   value: The integer value to insert
   * Inserts a parameter at a given index with a given value
   **/
  void insertParameterAtIndex (const size_t index,
                               const int value);

  /**
   * @param   index: The index to insert the parameter at
   * @param   value: The floating point value to insert
   * Inserts a parameter at a given index with a given value
   **/
  void insertParameterAtIndex (const size_t index,
                               const float value);

  /**
   * @param   index: The index to insert the parameter at
   * @param   value: The zro terminated string to insert
   * Inserts a parameter at a given index with a given value
   **/
  void insertParameterAtIndex (const size_t index,
                               const char *value);
    
  /**
   * @param   index: The index of the first parameter to delete
   * @param   count: The count of parameters to delete
   * Deletes a specified count of parameters starting at a given index
   **/
  void deleteParameterAtIndex (const size_t index,
                               const size_t count);
    
  /**
   * @param   timestamp: The timestamp to set
   * Sets a given timestamp
   **/
  void setTimestamp (const Timestamp timestamp);
  
  /**
   Sets the result of ::timeGetTime() as the timestamp
   **/
  void setCurrentTimestamp ();
    
  /**
   * @param   socket: The socket to send the data to
   * @param   flags: Flags to pass to ::send
   * @return  The result of ::send
   * Calls ::send with the finalized packet, a given socket and the given flags
   **/
  int send (const SOCKET socket,
            const int flags);
};

#endif /* __PACKET_HPP__ */


underScore




Packet.hpp

[Per vedere i link devi REGISTRARTI.]


Packet.cpp

[Per vedere i link devi REGISTRARTI.]




La libertà non si insegna, è una scelta individuale.
10/12/2012, 23:44
#1
Cerca

1 Life, 1 Avatar &lt;3
Messaggi: 9,074
Discussioni: 271
Registrato: 08-2011
Reputazione:
77

Mix: 0
proprio quello che mi serviva è.è

11/12/2012, 14:15
#2
Cerca


Discussioni simili
Discussione Autore Risposte Letto Ultimo messaggio
  packet sender catoplepas 1 433 09/04/2014, 14:38
Ultimo messaggio: Sbronzooo



Utenti che stanno guardando questa discussione: 1 Ospite(i)