Esempio n. 1
0
static void down_to_transport(CnetEvent ev, CnetTimer timer, CnetData data) {

    /* our packet  */
    TR_PACKET p;

    /* destination address to pass to network layer */
    CnetAddr temp;


    p.length = sizeof(p.msg);
    CHECK(CNET_read_application(&temp, p.msg, &p.length));
    CNET_disable_application(temp);

    /* establish sequence number */
    unsigned long seq = getSequence(temp);
    if( seq == -1) {
        addAddress(temp,0);
        p.sequence_number = 0;
    }
    else {
        unsigned long newseq = seq + p.length;
        setSequence(temp,newseq);
        p.sequence_number = newseq;
    }


    p.checksum = checksum_crc32( ((char*)&p)+sizeof(unsigned int)
                                 , TR_PACKET_SIZE(p) - sizeof(unsigned int));

    down_to_network( (char*)&p , TR_PACKET_SIZE(p) , temp);
    /* enqueuePacket((char*)&p, TR_PACKET_SIZE(p), temp); */

    CNET_start_timer(EV_TIMER1, 1000, (CnetData)1);
}
Esempio n. 2
0
static void up_to_transport(char *msg, int *len, CnetAddr source) {

    printf("Transport Layer is getting a packet\n");

    TR_PACKET *p;
    p = (TR_PACKET*) msg;

    if( p->checksum == checksum_crc32( msg + sizeof(unsigned int),
                                       TR_PACKET_SIZE_PTR(p) - sizeof(unsigned int)) ) {
        /* good packet */
        printf("Mmmm...Good packet\n");
        CHECK(CNET_write_application(p->msg,&(p->length)));
    }
    else {
        /* bad packet */
        printf("Bad, bad packet!\n");
    }
}
/**
  Tests the checksum algorithm used for the binary log, and asserts in case
  if the checksum algorithm is invalid.

  @param   event_buf       point to the buffer containing serialized event
  @param   event_len       length of the event accounting possible
                           checksum alg
  @param   alg             checksum algorithm used for the binary log

  @retval  true            if test fails
  @retval  false           as success
*/
bool Log_event_footer::event_checksum_test(unsigned char *event_buf,
                                           unsigned long event_len,
                                           enum_binlog_checksum_alg alg)
{
  bool res= false;
  unsigned short flags= 0; // to store in FD's buffer flags orig value

  if (alg != BINLOG_CHECKSUM_ALG_OFF && alg != BINLOG_CHECKSUM_ALG_UNDEF)
  {
    uint32_t incoming;
    uint32_t computed;

    if (event_buf[EVENT_TYPE_OFFSET] == FORMAT_DESCRIPTION_EVENT)
    {
    #ifndef DBUG_OFF
      unsigned char fd_alg= event_buf[event_len - BINLOG_CHECKSUM_LEN -
                                      BINLOG_CHECKSUM_ALG_DESC_LEN];
    #endif
      /*
        FD event is checksummed and therefore verified w/o
        the binlog-in-use flag.
      */
      memcpy(&flags, event_buf + FLAGS_OFFSET, sizeof(flags));
      flags= le16toh(flags);
      if (flags & LOG_EVENT_BINLOG_IN_USE_F)
        event_buf[FLAGS_OFFSET] &= ~LOG_EVENT_BINLOG_IN_USE_F;
      /*
         The only algorithm currently is CRC32. Zero indicates
         the binlog file is checksum-free *except* the FD-event.
      */
    #ifndef DBUG_OFF
      BAPI_ASSERT(fd_alg == BINLOG_CHECKSUM_ALG_CRC32 || fd_alg == 0);
    #endif
      BAPI_ASSERT(alg == BINLOG_CHECKSUM_ALG_CRC32);
      /*
        Complile time guard to watch over  the max number of alg
      */
      do_compile_time_assert(BINLOG_CHECKSUM_ALG_ENUM_END <= 0x80);
    }
    memcpy(&incoming,
           event_buf + event_len - BINLOG_CHECKSUM_LEN, sizeof(incoming));
    incoming= le32toh(incoming);

    computed= checksum_crc32(0L, NULL, 0);
    /* checksum the event content but not the checksum part itself */
    computed= binary_log::checksum_crc32(computed,
                                         (const unsigned char*) event_buf,
                                         event_len - BINLOG_CHECKSUM_LEN);

    if (flags != 0)
    {
      /* restoring the orig value of flags of FD */
      BAPI_ASSERT(event_buf[EVENT_TYPE_OFFSET] == FORMAT_DESCRIPTION_EVENT);
      event_buf[FLAGS_OFFSET]= static_cast<unsigned char>(flags);
    }

    res= !(computed == incoming);
  }
#ifndef DBUG_OFF
  if (binary_log_debug::debug_checksum_test)
    return true;
#endif
  return res;
}