Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 1336


Ignore:
Timestamp:
May 20, 2008, 5:58:37 PM (17 years ago)
Author:
scheusso
Message:

added crc checksum testing for submitted gamestates (can also be used for other packet types)

Location:
code/branches/merge/src/network
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/merge/src/network/PacketDecoder.cc

    r1299 r1336  
    8888      return decodeConnectRequest( packet, clientId );
    8989    }
     90    return false;
     91  }
     92 
     93  bool PacketDecoder::testAndRemoveCRC(ENetPacket *packet){
     94    uint32_t submittetcrc;
     95    int dataLength = packet->dataLength;
     96    // get the submittet crc code
     97    memcpy(&submittetcrc, &packet->data[dataLength-sizeof(uint32_t)], sizeof(uint32_t));
     98    unsigned char *data = packet->data;
     99    uint32_t crc32=calcCRC(data, packet->dataLength-sizeof(uint32_t));
     100    // now append the crc to the packet data
     101    if(crc32==submittetcrc){
     102      dataLength-=sizeof(uint32_t);
     103      enet_packet_resize(packet, dataLength);
     104      return true;
     105    }
     106    COUT(3) << "gamestate crc: " << crc32 << std::endl;
     107    COUT(3) << "submitted crc: " << submittetcrc << std::endl;
    90108    return false;
    91109  }
     
    157175  void PacketDecoder::gstate( ENetPacket* packet, int clientID )
    158176  {
     177    if(!testAndRemoveCRC(packet)){
     178      COUT(3) << "crc test of gamestate failed - dropping packet" << std::endl;
     179      return;
     180    }
    159181    GameStateCompressed* currentState = NULL;
    160182    currentState = new GameStateCompressed;
  • code/branches/merge/src/network/PacketGenerator.cc

    r1294 r1336  
    4646namespace network
    4747{
     48  void calcCRCBit(uint32_t &crc32, int bit){
     49    int hbit;
     50 
     51    hbit=(crc32 & 0x80000000) ? 1 : 0;
     52    if (hbit != bit)
     53      crc32=(crc32<<1) ^ NETWORK_CRC32POLY;
     54    else
     55      crc32=crc32<<1;
     56  }
     57 
     58  uint32_t calcCRC(unsigned char *data, unsigned int dataLength){
     59    uint32_t crc32=0;
     60    for(unsigned int i=0; i<dataLength; i++){
     61      calcCRCBit(crc32, (data[i]&0x1)>>0); // 1st bit
     62      calcCRCBit(crc32, (data[i]&0x2)>>1); // 2nd bit
     63      calcCRCBit(crc32, (data[i]&0x3)>>2); // 3rd bit
     64      calcCRCBit(crc32, (data[i]&0x4)>>3); // 4th bit
     65      calcCRCBit(crc32, (data[i]&0x5)>>4); // 5th bit
     66      calcCRCBit(crc32, (data[i]&0x6)>>5); // 6th bit
     67      calcCRCBit(crc32, (data[i]&0x7)>>6); // 7th bit
     68      calcCRCBit(crc32, (data[i]&0x8)>>7); // 8th bit
     69    }
     70    return crc32;
     71  }
     72 
    4873  PacketGenerator::PacketGenerator() { }
    4974
     
    135160    memcpy( (void*)(data+5*sizeof( int ) + sizeof(bool)), (const void*)&(states->complete), sizeof(bool) );
    136161    memcpy( (void*)(data+5*sizeof( int ) + 2*sizeof(bool)), (const void*)states->data, states->compsize );
     162   
    137163    //create an enet packet with the generated bytestream
    138164    COUT(4) << "PacketGenerator generating totalLen " << totalLen << std::endl;
    139165    ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
    140166    delete[] data;
     167    if(!addCRC(packet))
     168      COUT(3) << "could not add crc to gamestate packet" << std::endl;
    141169    return packet;
    142170  }
     
    174202    return packet;
    175203  }
     204 
     205 
     206  bool PacketGenerator::addCRC( ENetPacket *packet){
     207    unsigned char *data = packet->data;
     208    uint32_t crc32=calcCRC(data, packet->dataLength);
     209    // now append the crc to the packet data
     210    int oldlength = packet->dataLength;
     211    if(enet_packet_resize(packet, packet->dataLength+sizeof(uint32_t))==0){
     212      memcpy(&packet->data[oldlength], &crc32, sizeof(uint32_t));
     213      return true;
     214    }else{
     215      COUT(3) << "could not add crc to gamestate" << std::endl;
     216      return false;
     217    }
     218  }
    176219
    177220}
  • code/branches/merge/src/network/PacketManager.h

    r1264 r1336  
    3333
    3434#include <string>
     35#include <inttypes.h>
    3536#include <enet/enet.h>
    3637
     
    3839
    3940#define CLIENTID_CLIENT -1
     41#define NETWORK_CRC32POLY 0x04C11DB7 /* CRC-32 Polynom */
    4042
    4143//enum netowk generally used to set the type ID of a packet
     
    4850  *
    4951  */
     52  //void calcCRC(uint32_t &crc32, int bit);
     53  uint32_t calcCRC(unsigned char *data, unsigned int dataLength);
     54 
    5055  class PacketGenerator
    5156  {
     
    6368    ENetPacket* generateConnectRequest( int reliable = ENET_PACKET_FLAG_RELIABLE );
    6469  private:
     70    bool addCRC( ENetPacket *packet);
    6571  };
    6672
     
    8490
    8591  private:
    86 
     92    bool testAndRemoveCRC(ENetPacket *packet);
    8793
    8894
Note: See TracChangeset for help on using the changeset viewer.