Ejemplo n.º 1
0
void FileCodeWriter::WriteBuffer()
{
	const static unsigned char MICROSOFT_BOM[3] = { 0xEF, 0xBB, 0xBF };

	// Compare buffer with existing file (if any) to determine if
	// writing the file is necessary
	bool shouldWrite = true;
	std::ifstream fileIn(m_filename.mb_str(wxConvFile), std::ios::binary | std::ios::in);

	std::string buf;

	if (fileIn)
	{
		MD5 diskHash(fileIn);
		unsigned char* diskDigest = diskHash.raw_digest();

		MD5 bufferHash;
		if (m_useMicrosoftBOM) {
			bufferHash.update(MICROSOFT_BOM, 3);
		}
		const std::string& data = m_useUtf8 ? _STDSTR( m_buffer ) : _ANSISTR( m_buffer );

		if (!m_useUtf8) buf = data;

		bufferHash.update( reinterpret_cast< const unsigned char* >( data.c_str() ), data.size() );

		bufferHash.finalize();

		unsigned char* bufferDigest = bufferHash.raw_digest();

		shouldWrite = ( 0 != std::memcmp( diskDigest, bufferDigest, 16 ) );

		delete [] diskDigest;

		delete [] bufferDigest;
	}

	if ( shouldWrite )
	{
		wxFile fileOut;
		if (!fileOut.Create(m_filename, true))
		{
			wxLogError( _("Unable to create file: %s"), m_filename.c_str() );
			return;
		}

		if (m_useMicrosoftBOM)
		{
			fileOut.Write(MICROSOFT_BOM, 3);
		}

		if (!m_useUtf8)
			fileOut.Write(buf.c_str(), buf.length());
		else
			fileOut.Write(m_buffer);
	}
}
Ejemplo n.º 2
0
/**
 * Generate BMP router HASH
 *
 * \param [in,out] client   Reference to client info used to generate the hash.
 *
 * \return client.hash_id will be updated with the generated hash
 */
void BMPListener::hashRouter(ClientInfo &client) {
    string c_hash_str;
    MsgBusInterface::hash_toStr(cfg->c_hash_id, c_hash_str);

    MD5 hash;
    hash.update((unsigned char *)client.c_ip, strlen(client.c_ip));
    hash.update((unsigned char *)c_hash_str.c_str(), c_hash_str.length());
    hash.finalize();

    // Save the hash
    unsigned char *hash_bin = hash.raw_digest();
    memcpy(client.hash_id, hash_bin, 16);
    delete[] hash_bin;
}
Ejemplo n.º 3
0
int CalcHashBytes(LPTSTR xHashing, LPCTSTR xString)
{
	MD5 ctx; BYTE pHash[16];
	ctx.update( (LPBYTE)xString, _tcslen(xString) );
	ctx.finalize(); ctx.raw_digest( pHash );
	
	ctx.tostring( (LPBYTE)xHashing );
	return 32;
}
Ejemplo n.º 4
0
IUserPacket::IUserPacket(LPCTSTR xUserId, LPCTSTR xPass) : IPacket( ICP_USER )
{
	MD5 ctx; BYTE xMD5Hash[16];
	ctx.update( (LPBYTE)xPass, _tcslen(xPass) );
	ctx.finalize(); ctx.raw_digest( xMD5Hash );
	
	CopyMemory( UserId, xUserId, 21 );
	ctx.tostring( (LPBYTE)MD5Hashing );
}
Ejemplo n.º 5
0
const wxString wxMD5::GetDigest()
{
	MD5 context;
	context.update((unsigned char*)m_szText.mb_str().data(), m_szText.Len());
	context.finalize();
	
	wxString md5(context.hex_digest());
	md5.MakeUpper();
	return md5;
}
Ejemplo n.º 6
0
bool CdbInterface::authenticate(const char* username, const char* password)
{
    SingleLock l(&dbMutex_);
    if (!l.lock())
        return false;

    ReadLockSQLTable sqlLock(dbase_, "pokeruser");
    if (!sqlLock.success_)
        return false;

    char query[MAXQUERYSIZE];
    char* dbPassword = NULL;
    MD5 context;

    string u = sqlfy(username);

    context.update((unsigned char *)password, (int)strlen(password));
    context.finalize();


    memset(query, 0x0, MAXQUERYSIZE);

    sprintf(query, "SELECT password FROM pokeruser WHERE username='******'", u.c_str());
    
    if (!dbase_->dbQuery(query))
    {
        if (DEBUG & DEBUG_DATABASE)
        {
            printf("Query to authenticate %s failed!\n", username);
            printf("Reason: %s\n", mysql_error(dbase_->mySQLQuery_));
        }
        return false;
    }

    dbPassword = dbase_->dbFetchRow();

    if (dbPassword == NULL)
    {
        char s[200];
        sprintf(s, "CTable::tableLogin: User %s does not exist in database!", username);
        Sys_LogError(s);
        return false;
    }
    else if (strcmp(dbPassword, context.hex_digest()))
    {
        char s[200];
        sprintf(s, "CTable::tableLogin: wrong password %s for user %s.", password, username);
        Sys_LogError(s);
        return false;
    }

    return true;
};
Ejemplo n.º 7
0
int main(int argc, char ** argv)
{
    unsigned int iterations = 1 << 15;
    unsigned int tests = 10000;
    unsigned char salt1[] = {0x97, 0x48, 0x6C, 0xAA, 0x22, 0x5F, 0xE8, 0x77, 0xC0, 0x35, 0xCC, 0x03, 0x73, 0x23, 0x6D, 0x51};
    char path[] = "C:\\Documents and Settings\\john\\Local Settings\\Application Data\\Google\\Chrome\\Application~dir1";
    unsigned char null = 0x00;
    cout << "Number of tests: " << iterations << endl;
    cout << "Iterations per hash: " << tests << endl;
    int start = clock();
    for(int y = 0; y < iterations; y++)
    {
        MD5 md5;
        for(int x = 0; x < strlen(path); x++)
        {
            md5.update((unsigned char *)path + x, 1);
            md5.update((unsigned char *)&null, 1);
        }
        md5.update(salt1, 16);
        unsigned char hash[16];
        md5.finalize();
        memcpy(hash, md5.digest, 16);
        for(int x = 0; x < tests; x++)
        {
            MD5 m;
            m.update(hash, 16);
            m.finalize();
            memcpy(hash, m.digest, 16);
        }
    }
    int end = clock();
    double orig = end - start;
    printf ("Original: (%f seconds) %f hashes per second.\n", ((float)end - start)/CLOCKS_PER_SEC, ((double)iterations + 1)/(((float)end - start)/CLOCKS_PER_SEC));

    start = clock();
    for(int y = 0; y < iterations; y++)
    {
        MD5 md5i;
        for(int x = 0; x < strlen(path); x++)
        {
            md5i.update((unsigned char *)path + x, 1);
            md5i.update((unsigned char *)&null, 1);
        }
        md5i.update(salt1, 16);
        md5i.finalize(tests);
    }
    end = clock();
    orig = end - start;
    printf ("Original: (%f seconds) %f hashes per second.\n", ((float)end - start)/CLOCKS_PER_SEC, ((double)iterations + 1)/(((float)end - start)/CLOCKS_PER_SEC));

    cout << endl << "Should be 76405ce7f4e75e352c1cd4d9aeb6be41" << endl;
}
Ejemplo n.º 8
0
bool CdbInterface::authenticate(const char* username, const char* password)
{
  ReadLockSQLTable sqlLock(dbase_, "pokeruser");
  
  CStrOut query;
  char* dbPassword = NULL;
  
  string u = sqlfy(username);
  
  MD5 context;
  context.update((unsigned char*)password, strlen(password));
  context.finalize();
  
  query << "SELECT password FROM pokeruser WHERE username='******'\'';
  
  if (!dbase_->dbQuery(query.str()))
  {
    if (DEBUG & DEBUG_DATABASE)
    {
      printf("Query to authenticate %s failed!\n", username);
    }
    
    return false;
  }
  
  dbPassword = dbase_->dbFetchRow();
  
  if (dbPassword == NULL)
  {
    if (DEBUG & DEBUG_DATABASE)
    {
      printf("User %s does not exist in database!\n", username);
    }
    
    return false;
    
  }
  //MP
  else if (strcmp(dbPassword, password))
  {
    if (DEBUG & DEBUG_DATABASE)
    {
      printf("User supplied password and password on file don't match!\n");            
    }
    return false;
  }
  
  return true;
};
Ejemplo n.º 9
0
TEST( Utils, MD5 )
{
    MD5 checksum;

    // abc
    checksum.update("abc");
    EXPECT_EQ( "900150983cd24fb0d6963f7d28e17f72", checksum.final_hex() );

    // abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
    checksum.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
    EXPECT_EQ( "8215ef0796a20bcaaae116d3876c664a", checksum.final_hex() );

    // A million repetitions of 'a'.
    for (int i = 0; i < 1000000/200; ++i)
    {
        checksum.update(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        );
    }
    EXPECT_EQ( "7707d6ae4e027c70eea2a935c2296f21", checksum.final_hex() );

    // No string
    EXPECT_EQ( "d41d8cd98f00b204e9800998ecf8427e", checksum.final_hex());

    // Empty string
    checksum.update("");
    EXPECT_EQ( "d41d8cd98f00b204e9800998ecf8427e", checksum.final_hex());

    // Two concurrent checksum calculations
    MD5 checksum1, checksum2;
    checksum1.update("abc");
    EXPECT_EQ( "900150983cd24fb0d6963f7d28e17f72", checksum1.final_hex());
    EXPECT_EQ( "d41d8cd98f00b204e9800998ecf8427e", checksum2.final_hex());
}
Ejemplo n.º 10
0
void output_directory( set< string >& file_names,
 const string& path_name, int level, ofstream& outf, bool use_zlib, gzFile& gzf )
#endif
{
   string::size_type pos = path_name.find_last_of( '/' );
   if( level > 1 && pos != string::npos && !g_output_directories.count( path_name.substr( 0, pos ) ) )
   {
#ifndef ZLIB_SUPPORT
      output_directory( file_names, path_name.substr( 0, pos ), level - 1, outf );
#else
      output_directory( file_names, path_name.substr( 0, pos ), level - 1, outf, use_zlib, gzf );
#endif
   }

   if( !file_names.count( path_name ) )
   {
      string rel_path( "." );
      string::size_type pos = path_name.find( '/' );
      if( pos != string::npos )
         rel_path += path_name.substr( pos );

      string perms = file_perms( rel_path );

      MD5 md5;
      md5.update( ( unsigned char* )perms.c_str( ), perms.length( ) );
      md5.update( ( unsigned char* )path_name.c_str( ), path_name.length( ) );
      md5.finalize( );

      auto_ptr< char > ap_digest( md5.hex_digest( ) );

      ostringstream osstr;
      osstr << "D " << level << ' ' << perms << ' ' << path_name << ' ' << ap_digest.get( );

      g_md5.update( ( unsigned char* )ap_digest.get( ), 32 );

#ifndef ZLIB_SUPPORT
      outf << osstr.str( ) << '\n';
#else
      if( !use_zlib )
         outf << osstr.str( ) << '\n';
      else
         write_zlib_line( gzf, osstr.str( ) );
#endif

      file_names.insert( path_name );
   }

   g_output_directories.insert( path_name );
}
Ejemplo n.º 11
0
// This mfunction creates 'numPlayers' player entries
// with username and password 'bot_X'.
bool CdbInterface::createTestPlayers(int numPlayers)
{
    bool rc = true;

    for (int i = 0; i < numPlayers; i++)
    {
        char buf[17];
        sprintf(buf, "bot_%i", i + 1);

        // First check that the player is not already there!
        char query[MAXQUERYSIZE];
        sprintf(query, "SELECT username FROM pokeruser where username='******';",
                buf);

        if (!dbase_->dbQuery(query))
        {
            Sys_LogError("Query to create test player failed(0).");
            return false;
        }

        const char* fetched = dbase_->dbFetchRow();
        if (fetched == NULL)
        {   // Okay now create the player
            MD5 context;
            context.update((unsigned char*)buf, strlen(buf));
            context.finalize();

            sprintf(query, "INSERT INTO customers (username, password, cc1_type, cc1_number, spam) VALUES ('%s', '%s', 'asiV', '1', 1);",
                    buf, context.hex_digest());
    
            if (!dbase_->dbQuery(query))
            {
                Sys_LogError("Query to create test player failed(1).");
                return false;
            }        

            sprintf(query, "INSERT INTO pokeruser (username, password) VALUES ('%s', '%s');",
                    buf, context.hex_digest());
    
            if (!dbase_->dbQuery(query))
            {
                Sys_LogError("Query to create test player failed(2).");
                return false;
            }        
        }
    }

    return true;
}
Ejemplo n.º 12
0
static void md5_block(MD5& md5, const Pel* plane, unsigned n)
{
  /* create a 64 byte buffer for packing Pel's into */
  unsigned char buf[64/OUTPUT_BITDEPTH_DIV8][OUTPUT_BITDEPTH_DIV8];
  for (unsigned i = 0; i < n; i++)
  {
    Pel pel = plane[i];
    /* perform bitdepth and endian conversion */
    for (unsigned d = 0; d < OUTPUT_BITDEPTH_DIV8; d++)
    {
      buf[i][d] = pel >> (d*8);
    }
  }
  md5.update((unsigned char*)buf, n * OUTPUT_BITDEPTH_DIV8);
}
Ejemplo n.º 13
0
void main()
{
	HMODULE hVDLL;
	IBaseVerify* pCV;
	_NxModule_GetType NxModule_GetType;
	_NxVerify_GetInterface NxVerify_GetInterface;

	size_t cEncode_Id = 1;
	//	_GetVersion pGetVersion;
	hVDLL = LoadLibrary("NxVerify.dll");
	NxModule_GetType = (_NxModule_GetType)GetProcAddress(hVDLL, "NxModule_GetType");
	NxVerify_GetInterface = (_NxVerify_GetInterface)GetProcAddress(hVDLL, "NxVerify_GetInterface");

	pCV = NxVerify_GetInterface();
	unsigned char buf[128];
	
	ZeroMemory(buf, sizeof(buf));
	//memcpy(buf, (const char*)"A", sizeof("A"));

	MD5 alg;
	alg.update(buf, sizeof(buf));
	alg.finalize();
	
	UI08 output[16];



	unsigned int *p;

	p = (unsigned int *)alg.digest;
	for (int i = 0; i < 4; i++)
	{
		printf("0x%08x\n", *p);
		p++;
	}

	pCV->EncodeFunc(cEncode_Id, alg.digest, output);


	p = (unsigned int *)output;
	for (int i = 0; i < 4; i++)
	{
		printf("0x%08x\n", *p);
		p++;
	}
	getchar();
}
Ejemplo n.º 14
0
void process_directory( const string& directory, const string& filespec_path,
 const vector< string >& filename_filters, const vector< string >* p_filename_exclusions,
 set< string >& matched_filters, set< string >& file_names, bool recurse, bool prune,
 bool is_quieter, bool is_append, encoding_type encoding, ofstream& outf, bool use_zlib, gzFile& gzf )
#endif
{
   directory_filter df;
   fs_iterator dfsi( filespec_path, &df );

   do
   {
      file_filter ff;
      fs_iterator ffsi( dfsi.get_path_name( ), &ff );

      string path_name( dfsi.get_path_name( ) );

#ifdef _WIN32
      string::size_type pos;
      while( ( pos = path_name.find( '\\' ) ) != string::npos )
         path_name[ pos ] = '/';
#endif

      if( path_name.find( filespec_path ) == 0 )
         path_name.erase( 0, filespec_path.length( ) );

      if( path_name.empty( ) )
         path_name = directory;
      else
         path_name = directory + path_name;

      bool has_output_directory = false;

      if( !prune || dfsi.get_level( ) == 0 )
      {
         has_output_directory = true;
#ifndef ZLIB_SUPPORT
         output_directory( file_names, path_name, dfsi.get_level( ), outf );
#else
         output_directory( file_names, path_name, dfsi.get_level( ), outf, use_zlib, gzf );
#endif
      }

      while( ffsi.has_next( ) )
      {
         bool matched = false;
         for( int i = 0; i < filename_filters.size( ); i++ )
         {
            string next_filter( filename_filters[ i ] );

            string::size_type pos = next_filter.find_last_of( '/' );
            if( pos != string::npos )
            {
               if( g_cwd != filespec_path )
                  continue;

               string sub_path( directory + "/" + next_filter.substr( 0, pos ) );

               if( path_name.find( sub_path ) != 0 )
                  continue;

               next_filter.erase( 0, pos + 1 );
            }

            if( wildcard_match( next_filter, ffsi.get_name( ) ) )
            {
               matched = true;
               matched_filters.insert( filename_filters[ i ] );

               break;
            }
         }

         if( file_names.count( ffsi.get_name( ) ) )
            matched = false;

         if( p_filename_exclusions )
         {
            for( int j = 0; j < p_filename_exclusions->size( ); j++ )
            {
               string next_filter( ( *p_filename_exclusions )[ j ] );

               string::size_type pos = next_filter.find_last_of( '/' );
               if( pos != string::npos )
               {
                  if( g_cwd != filespec_path )
                     continue;

                  string sub_path( directory + "/" + next_filter.substr( 0, pos ) );

                  if( path_name.find( sub_path ) != 0 )
                     continue;

                  next_filter.erase( 0, pos + 1 );
               }

               if( wildcard_match( next_filter, ffsi.get_name( ) ) )
               {
                  matched = false;
                  break;
               }
            }
         }

         if( !matched )
            continue;

         string absolute_file_name;
         if( !absolute_path( ffsi.get_full_name( ), absolute_file_name ) )
            throw runtime_error( "unable to determine absolute path for '" + ffsi.get_full_name( ) + "'" );

         if( absolute_file_name == g_bundle_file_name || absolute_file_name == g_output_file_name )
            continue;

         if( !has_output_directory )
         {
            has_output_directory = true;
#ifndef ZLIB_SUPPORT
            output_directory( file_names, path_name, dfsi.get_level( ), outf );
#else
            output_directory( file_names, path_name, dfsi.get_level( ), outf, use_zlib, gzf );
#endif
         }

         if( !is_quieter )
         {
            string next_path( path_name );

            if( next_path.find( directory ) == 0 )
            {
               next_path.erase( 0, directory.size( ) );
               if( !next_path.empty( ) && next_path[ 0 ] == '/' )
                  next_path.erase( 0, 1 );
            }

            if( !next_path.empty( ) )
               next_path += "/";

            cout << "adding \"" << next_path << ffsi.get_name( ) << "\"";
         }

         int64_t size = file_size( ffsi.get_full_name( ) );
         string perms = file_perms( ffsi.get_full_name( ) );

         string fname( ffsi.get_name( ) );

         ifstream inpf( ffsi.get_full_name( ).c_str( ), ios::in | ios::binary );
         if( !inpf )
            throw runtime_error( "unable to open file '" + ffsi.get_full_name( ) + "' for input" );

         MD5 md5;
         unsigned char buffer[ c_buffer_size ];

         md5.update( ( unsigned char* )perms.c_str( ), perms.length( ) );
         md5.update( ( unsigned char* )fname.c_str( ), fname.length( ) );

         int64_t size_left = size;
         while( size_left )
         {
            int next_len = c_buffer_size;
            if( size_left < c_buffer_size )
               next_len = ( int )size_left;

            inpf.read( ( char* )buffer, next_len );

            int len = inpf.gcount( );
            md5.update( buffer, len );

            size_left -= next_len;
         }
         md5.finalize( );
         inpf.seekg( 0, ios::beg );

         int max_size = c_max_bytes_per_line;
         if( encoding != e_encoding_type_b64 )
            max_size = c_buffer_size;

         int64_t num;
         if( encoding == e_encoding_type_raw )
            num = size;
         else
         {
            num = size / max_size;
            if( size % max_size )
               ++num;
         }

         auto_ptr< char > ap_digest( md5.hex_digest( ) );

         ostringstream osstr;
         osstr << "F " << num << ' ' << perms << ' ' << fname << ' ' << ap_digest.get( );

         file_names.insert( ffsi.get_name( ) );

         g_md5.update( ( unsigned char* )ap_digest.get( ), 32 );

#ifndef ZLIB_SUPPORT
         outf << osstr.str( ) << '\n';
#else
         if( !use_zlib )
            outf << osstr.str( ) << '\n';
         else
            write_zlib_line( gzf, osstr.str( ) );
#endif

         int line = 0;

         while( size > 0 )
         {
            char buffer[ c_buffer_size ];

            streamsize count = inpf.rdbuf( )->sgetn( buffer, max_size );

            if( count == 0 )
               throw runtime_error( "read failed for file '" + ffsi.get_full_name( ) + "'" );

            if( !is_quieter && ++line % c_progress_lines == 0 )
            {
               if( line == c_progress_lines )
                  cout << ' ';
               cout << '.';
               cout.flush( );
            }

            string encoded;
            if( encoding == e_encoding_type_esc )
               encoded = escaped_line( string( buffer, count ) );
            else if( encoding == e_encoding_type_b64 )
               encoded = base64::encode( string( buffer, count ) );

#ifndef ZLIB_SUPPORT
            if( encoding != e_encoding_type_raw )
               outf << encoded << '\n';
            else
               outf.rdbuf( )->sputn( buffer, count );

            if( !outf.good( ) )
               throw runtime_error( "unexpected bad output file stream" );
#else
            if( !use_zlib )
            {
               if( encoding != e_encoding_type_raw )
                  outf << encoded << '\n';
               else
                  outf.rdbuf( )->sputn( buffer, count );

               if( !outf.good( ) )
                  throw runtime_error( "unexpected bad output file stream" );
            }
            else
            {
               if( encoding != e_encoding_type_raw )
                  write_zlib_line( gzf, encoded );
               else if( !gzwrite( gzf, buffer, count ) )
                  throw runtime_error( "writing zlib block" );
            }
#endif

            size -= count;
         }

         if( !is_quieter )
            cout << endl;
      }
   } while( recurse && dfsi.has_next( ) );
}
Ejemplo n.º 15
0
/**
 * Abstract method Implementation - See DbInterface.hpp for details
 */
void mysqlBMP::add_Rib(vector<tbl_rib> &rib_entry) {
    char    *buf = new char[800000];            // Misc working buffer
    char    buf2[4096];                         // Second working buffer
    size_t  buf_len = 0;                        // query buffer length

    try {

        // Build the initial part of the query
        //buf_len = sprintf(buf, "REPLACE into %s (%s) values ", TBL_NAME_RIB,
        //        "hash_id,path_attr_hash_id,peer_hash_id,prefix, prefix_len");
        buf_len = sprintf(buf, "INSERT into %s (%s) values ", TBL_NAME_RIB,
                "hash_id,path_attr_hash_id,peer_hash_id,prefix, prefix_len,timestamp");

        string rib_hash_str;
        string path_hash_str;
        string p_hash_str;

        // Loop through the vector array of rib entries
        for (size_t i = 0; i < rib_entry.size(); i++) {
            /*
             * Generate router table hash from the following fields
             *     rib_entry.peer_hash_id, rib_entry.prefix, rib_entry.prefix_len
             *
             */
            MD5 hash;

            // Generate the hash

            hash.update(rib_entry[i].peer_hash_id, HASH_SIZE);
            hash.update((unsigned char *) rib_entry[i].prefix,
                    strlen(rib_entry[i].prefix));
            hash.update(&rib_entry[i].prefix_len,
                    sizeof(rib_entry[i].prefix_len));
            hash.finalize();

            // Save the hash
            unsigned char *hash_raw = hash.raw_digest();
            memcpy(rib_entry[i].hash_id, hash_raw, 16);
            delete[] hash_raw;

            // Build the query
            hash_toStr(rib_entry[i].hash_id, rib_hash_str);
            hash_toStr(rib_entry[i].path_attr_hash_id, path_hash_str);
            hash_toStr(rib_entry[i].peer_hash_id, p_hash_str);

            buf_len += snprintf(buf2, sizeof(buf2),
                    " ('%s','%s','%s','%s', %d, from_unixtime(%u)),", rib_hash_str.c_str(),
                    path_hash_str.c_str(), p_hash_str.c_str(),
                    rib_entry[i].prefix, rib_entry[i].prefix_len,
                    rib_entry[i].timestamp_secs);

            // Cat the entry to the query buff
            if (buf_len < 800000 /* size of buf */)
                strcat(buf, buf2);
        }

        // Remove the last comma since we don't need it
        buf[buf_len - 1] = 0;

        // Add the on duplicate statement
        snprintf(buf2, sizeof(buf2),
                " ON DUPLICATE KEY UPDATE timestamp=values(timestamp),path_attr_hash_id=values(path_attr_hash_id),db_timestamp=current_timestamp");
        strcat(buf, buf2);


        SELF_DEBUG("QUERY=%s", buf);

        // Run the query to add the record
        stmt = con->createStatement();
        stmt->execute(buf);

        // Free the query statement
        delete stmt;

    } catch (sql::SQLException &e) {
        LOG_ERR("mysql error: %s, error Code = %d, state = %s",
                e.what(), e.getErrorCode(), e.getSQLState().c_str() );
    }

    // Free the large buffer
    delete[] buf;
}
Ejemplo n.º 16
0
/**
 * Abstract method Implementation - See DbInterface.hpp for details
 */
void mysqlBMP::add_PathAttrs(tbl_path_attr &path_entry) {
    try {
        char    buf[64000];                 // Misc working buffer
        char    buf2[24000];                // Second working buffer
        size_t  buf_len;                    // size of the query buff

        // Setup the initial MySQL query
        buf_len =
                sprintf(buf, "INSERT into %s (%s) values ", TBL_NAME_PATH_ATTRS,
                        "hash_id,peer_hash_id,origin,as_path,next_hop,med,local_pref,isAtomicAgg,aggregator,community_list,ext_community_list,cluster_list,originator_id,origin_as,as_path_count,timestamp");

        /*
         * Generate router table hash from the following fields
         *     peer_hash_id, as_path, next_hop, aggregator,
         *     origin, med, local_pref
         *
         */
        MD5 hash;

        // Generate the hash
        hash.update(path_entry.peer_hash_id, HASH_SIZE);
        hash.update((unsigned char *) path_entry.as_path,
                strlen(path_entry.as_path));
        hash.update((unsigned char *) path_entry.next_hop,
                strlen(path_entry.next_hop));
        hash.update((unsigned char *) path_entry.aggregator,
                strlen(path_entry.aggregator));
        hash.update((unsigned char *) path_entry.origin,
                strlen(path_entry.origin));
        hash.update((unsigned char *) &path_entry.med, sizeof(path_entry.med));
        hash.update((unsigned char *) &path_entry.local_pref,
                sizeof(path_entry.local_pref));
        hash.finalize();

        // Save the hash
        unsigned char *hash_raw = hash.raw_digest();
        memcpy(path_entry.hash_id, hash_raw, 16);
        delete[] hash_raw;

        // Build the query
        string path_hash_str;
        string p_hash_str;
        hash_toStr(path_entry.hash_id, path_hash_str);
        hash_toStr(path_entry.peer_hash_id, p_hash_str);

        buf_len +=
                snprintf(buf2, sizeof(buf2),
                        "('%s','%s','%s','%s','%s', %u,%u,%d,'%s','%s','%s','%s','%s','%"PRIu32"','%hu', from_unixtime(%u)),",
                        path_hash_str.c_str(), p_hash_str.c_str(),
                        path_entry.origin, path_entry.as_path,
                        path_entry.next_hop, path_entry.med,
                        path_entry.local_pref, path_entry.atomic_agg,
                        path_entry.aggregator, path_entry.community_list,
                        path_entry.ext_community_list, path_entry.cluster_list,
                        path_entry.originator_id, path_entry.origin_as,
                        path_entry.as_path_count,
                        path_entry.timestamp_secs);

        // Cat the string to our query buffer
        if (buf_len < sizeof(buf))
            strcat(buf, buf2);

        // Remove the last comma since we don't need it
        buf[buf_len - 1] = 0;

        // Add the on duplicate statement
        snprintf(buf2, sizeof(buf2),
                " ON DUPLICATE KEY UPDATE timestamp=values(timestamp)  ");
        strcat(buf, buf2);

        SELF_DEBUG("QUERY=%s\n", buf);

        // Run the query to add the record
        stmt = con->createStatement();
        stmt->execute(buf);

        // Free the query statement
        delete stmt;

    } catch (sql::SQLException &e) {
        LOG_ERR("mysql error: %s, error Code = %d, state = %s",
                e.what(), e.getErrorCode(), e.getSQLState().c_str() );
    }
}
Ejemplo n.º 17
0
int main()
{
  string s;
  while (getline(cin,s))
  {
    //for each bracket [] the content must be replied
    //as many times as the number before
    
    //since resulting string can be very long, we
    //process the md5 value in sequences of 64 chars
    
    //preprocess s so that we know for each bracket []
    //the number of times it must be repeated
    int n = s.size();
    vector<int> initialBrackets (n,0); //in positions with [, brackets
    //will contain the remaining loops; in positions with ],
    //brackets will contain a pointer to its corresponding [
    
    stack<int> unclosedBrackets; //pointers to unclosed brackets
    int value = 0; //number of iterations
    for (int i = 0; i < n; i++)
    {
      if (s[i] >= '0' and s[i] <= '9') //new digit
      {
        value *= 10;
        value += s[i] - '0';
      }
      if (s[i] == '[')
      {
        initialBrackets[i] = value;
        value = 0;
        unclosedBrackets.push(i);
      }
      if (s[i] == ']')
      {
        initialBrackets[i] = unclosedBrackets.top();
        unclosedBrackets.pop();
      }
    }
    
    //ans will be updated with groups of 64 chars  
    MD5 ans;
    ans.init();
    char buffer[64];
    int current = 0; //number of already read chars
    int pointer = 0; //pointer to positions in s
    vector<int> brackets = initialBrackets;
    while (pointer < s.size())
    {
      if (s[pointer] >= 'a' and s[pointer] <= 'z')
      {
        buffer[current] = s[pointer];
        current ++;
        if (current >= 64)
        {
          current = 0;
          ans.update (buffer, 64);
        }
      }
      
      if (s[pointer] == '[') //next iteration
      {
        if (brackets[pointer] == 0) //new external iteration
          brackets[pointer] = initialBrackets[pointer];
        brackets[pointer] --;
      }
      
      if (s[pointer] == ']')
      {
        if (brackets[brackets[pointer]] > 0) //at least one more iteration
        {
          pointer = brackets[pointer] - 1; //compensate the final pointer++
        }
      }
      
      pointer ++;
    }
    
    //update the last chars
    ans.update (buffer, current);
    
    ans.finalize();
    cout << ans.hexdigest() << endl;
  }
}
Ejemplo n.º 18
0
	bool append_file(const char* path,const char* file_name,MPQPackage* pPkg,FILE* fp_log)
	{
 		if ( !path || !file_name || !pPkg)
 		{
 			return false;
 		}
 
		string fullPathKey = stringTrim(path,"\n");
		string filePathKey = stringTrim(file_name,"\n");
		if ( filePathKey.empty() )
		{
			return false;
		}
		if ( fullPathKey.empty() )
		{
			fullPathKey = filePathKey;
		}
		else
		{
			fullPathKey.append("/");
			fullPathKey.append(filePathKey);
		}

 		FILE* fp = fopen(fullPathKey.c_str(),"rb");
 		if ( !fp )
 		{
			if ( fp_log )
			{
				fprintf(fp_log,"can't open file %s\n",fullPathKey.c_str());
			}
 			return false;
 		}
 
 		// md5 check code
 		MD5 m;
 		m.update(fp);
 
 		fseek(fp,0,SEEK_END);
 		unsigned int file_size = ftell(fp);
		m.update(&file_size,sizeof(unsigned int));
 
 		unsigned char* pData = new unsigned char[file_size];
 		if ( !pData )
 		{
			if ( fp_log )
			{
				fprintf(fp_log,"can't assign memory for file %s\n",fullPathKey.c_str());
			}
 			fclose(fp);
 			return false;
 		}
 
 		fseek(fp,0,SEEK_SET);
 		fread(pData,1,file_size,fp);
 		fclose(fp);

		bool bCompress = true;
		if ((std::string::npos != fullPathKey.find(".jpg")) ||
			(std::string::npos != fullPathKey.find(".png")) ||
			(std::string::npos != fullPathKey.find(".pvr")))
		{
			bCompress = false;
		}
 
 		MPQHashNode* pNode = pPkg->get_hash_node_new(filePathKey.c_str());
 		if ( !pNode )
 		{
 			// no space
			if ( fp_log )
			{
				fprintf(fp_log,"no hash node left for file %s\n",filePathKey.c_str());
			}
 			delete pData;
 			return false;
 		}
 
 		unsigned int blockIndex = pPkg->append_data(pData,file_size,bCompress);
 		if ( blockIndex == MPQ_INVALID )
 		{
 			pPkg->reset_hash_node(pNode);
 			delete pData;
			if ( fp_log )
			{
				fprintf(fp_log,"append data failed for file %s\n",filePathKey.c_str());
			}
 			return false;
 		}
 
 		pNode->block_index = blockIndex;
 		MPQBlock* pBlock = pPkg->get_block(pNode);
 		memcpy(pBlock->md5_code,m.result(),MPQ_MD5_CODE_LEN);
		pBlock->name = filePathKey;
 
 		delete pData;

		if ( fp_log )
		{
			fprintf(fp_log,"added file %s \n",filePathKey.c_str());
		}
		
		return true;
	}
Ejemplo n.º 19
0
    /**
* Parses the BGP prefixes (advertised and withdrawn) in the update
*
* \details
*     Parses all attributes.  Decoded values are updated in 'parsed_data'
*
* \param [in]   data       Pointer to the start of the prefixes to be parsed
* \param [in]   len        Length of the data in bytes to be read
* \param [in]   nlri_list Reference to parsed_update_data nlri list;
*/
void parseBgpLib::parseBgpNlri_v4(u_char *data, uint16_t len, std::list<parse_bgp_lib_nlri> &nlri_list) {
    u_char ipv4_raw[4];
    char ipv4_char[16];
    u_char addr_bytes;
    uint32_t path_id;
    u_char prefix_len;
    std::ostringstream numString;


    if (len <= 0 or data == NULL)
        return;

        // Loop through all prefixes
    for (size_t read_size = 0; read_size < len; read_size++) {
        parse_bgp_lib_nlri nlri;
        nlri.afi = parse_bgp_lib::BGP_AFI_IPV4;
        nlri.safi = parse_bgp_lib::BGP_SAFI_UNICAST;
        nlri.type = parse_bgp_lib::LIB_NLRI_TYPE_NONE;

        // Generate the hash
        MD5 hash;

        bzero(ipv4_raw, sizeof(ipv4_raw));

        // Parse add-paths if enabled
        bool peer_info_addpath = p_info and p_info->add_path_capability.isAddPathEnabled(bgp::BGP_AFI_IPV4, bgp::BGP_SAFI_UNICAST);

        if ((peer_info_addpath or addPathCap[BGP_AFI_IPV4_INTERNAL][BGP_SAFI_UNICAST_INTERNAL])
            and (len - read_size) >= 4) {
            memcpy(&path_id, data, 4);
            parse_bgp_lib::SWAP_BYTES(&path_id);
            data += 4;
            read_size += 4;
        } else
            path_id = 0;
        numString.str(std::string());
        numString << path_id;
        nlri.nlri[LIB_NLRI_PATH_ID].name = parse_bgp_lib::parse_bgp_lib_nlri_names[LIB_NLRI_PATH_ID];
        nlri.nlri[LIB_NLRI_PATH_ID].value.push_back(numString.str());

        if (path_id > 0)
            update_hash(&nlri.nlri[LIB_NLRI_PATH_ID].value, &hash);

        // set the address in bits length
        prefix_len = *data++;
        numString.str(std::string());
        numString << static_cast<unsigned>(prefix_len);

        nlri.nlri[LIB_NLRI_PREFIX_LENGTH].name = parse_bgp_lib::parse_bgp_lib_nlri_names[LIB_NLRI_PREFIX_LENGTH];
        nlri.nlri[LIB_NLRI_PREFIX_LENGTH].value.push_back(numString.str());
        update_hash(&nlri.nlri[LIB_NLRI_PREFIX_LENGTH].value, &hash);

        // Figure out how many bytes the bits requires
        addr_bytes = prefix_len / 8;
        if (prefix_len % 8)
            ++addr_bytes;

        SELF_DEBUG("%sReading NLRI data prefix bits=%d bytes=%d", debug_prepend_string.c_str(), prefix_len, addr_bytes);

        if (addr_bytes <= 4) {
            memcpy(ipv4_raw, data, addr_bytes);
            read_size += addr_bytes;
            data += addr_bytes;

            // Convert the IP to string printed format
            inet_ntop(AF_INET, ipv4_raw, ipv4_char, sizeof(ipv4_char));
            nlri.nlri[LIB_NLRI_PREFIX].name = parse_bgp_lib::parse_bgp_lib_nlri_names[LIB_NLRI_PREFIX];
            nlri.nlri[LIB_NLRI_PREFIX].value.push_back(ipv4_char);
            update_hash(&nlri.nlri[LIB_NLRI_PREFIX].value, &hash);
            SELF_DEBUG("%sAdding prefix %s len %d", debug_prepend_string.c_str(), ipv4_char, prefix_len);

            // set the raw/binary address
            nlri.nlri[LIB_NLRI_PREFIX_BIN].name = parse_bgp_lib::parse_bgp_lib_nlri_names[LIB_NLRI_PREFIX_BIN];
            nlri.nlri[LIB_NLRI_PREFIX_BIN].value.push_back(std::string(ipv4_raw, ipv4_raw + 4));

            //Update hash to include peer hash id
            if (p_info)
                hash.update((unsigned char *) p_info->peer_hash_str.c_str(), p_info->peer_hash_str.length());

            hash.finalize();

            // Save the hash
            unsigned char *hash_raw = hash.raw_digest();
            nlri.nlri[LIB_NLRI_HASH].name = parse_bgp_lib::parse_bgp_lib_nlri_names[LIB_NLRI_HASH];
            nlri.nlri[LIB_NLRI_HASH].value.push_back(parse_bgp_lib::hash_toStr(hash_raw));
            delete[] hash_raw;

            nlri.nlri[LIB_NLRI_IS_IPV4].name =parse_bgp_lib_nlri_names[LIB_NLRI_IS_IPV4];
            nlri.nlri[LIB_NLRI_IS_IPV4].value.push_back(string("1"));

            // Add tuple to prefix list
            nlri_list.push_back(nlri);
        } else if (addr_bytes > 4) {
            LOG_NOTICE("%sNRLI v4 address is larger than 4 bytes bytes=%d len=%d", debug_prepend_string.c_str(), addr_bytes, prefix_len);
        }
    }
}
Ejemplo n.º 20
0
/**
* Parse attribute data based on attribute type
*
* \details
*      Parses the attribute data based on the passed attribute type.
*      Parsed_data will be updated based on the attribute data parsed.
*
* \param [in]   attr_type      Attribute type
* \param [in]   attr_len       Length of the attribute data
* \param [in]   data           Pointer to the attribute data
* \param [out]  parsed_update  Reference to parsed_update; will be updated with all parsed data
*/
void parseBgpLib::parseAttrData(u_char attr_type, uint16_t attr_len, u_char *data, parsed_update &update, MD5 &hash) {
    u_char ipv4_raw[4];
    char ipv4_char[16];
    uint32_t value32bit;
    uint16_t value16bit;

    if (p_info)
        hash.update((unsigned char *) p_info->peer_hash_str.c_str(), p_info->peer_hash_str.length());

        /*
         * Parse based on attribute type
         */
    switch (attr_type) {

        case ATTR_TYPE_ORIGIN : // Origin
            update.attrs[LIB_ATTR_ORIGIN].official_type = ATTR_TYPE_ORIGIN;
            update.attrs[LIB_ATTR_ORIGIN].name = parse_bgp_lib::parse_bgp_lib_attr_names[LIB_ATTR_ORIGIN];
            switch (data[0]) {
                case 0 :
                    update.attrs[LIB_ATTR_ORIGIN].value.push_back(std::string("igp"));
                    break;
                case 1 :
                    update.attrs[LIB_ATTR_ORIGIN].value.push_back(std::string("egp"));
                    break;
                case 2 :
                    update.attrs[LIB_ATTR_ORIGIN].value.push_back(std::string("incomplete"));
                    break;
            }
            update_hash(&update.attrs[LIB_ATTR_ORIGIN].value, &hash);

            break;

        case ATTR_TYPE_AS_PATH : // AS_PATH
            parseAttrDataAsPath(attr_len, data, update);
            update_hash(&update.attrs[LIB_ATTR_AS_PATH].value, &hash);
            break;

        case ATTR_TYPE_NEXT_HOP : // Next hop v4
            memcpy(ipv4_raw, data, 4);
            inet_ntop(AF_INET, ipv4_raw, ipv4_char, sizeof(ipv4_char));
            update.attrs[LIB_ATTR_NEXT_HOP].official_type = ATTR_TYPE_NEXT_HOP;
            update.attrs[LIB_ATTR_NEXT_HOP].name = parse_bgp_lib::parse_bgp_lib_attr_names[LIB_ATTR_NEXT_HOP];
            update.attrs[LIB_ATTR_NEXT_HOP].value.push_back(std::string(ipv4_char));
            update_hash(&update.attrs[LIB_ATTR_NEXT_HOP].value, &hash);

            break;

        case ATTR_TYPE_MED : // MED value
        {
            memcpy(&value32bit, data, 4);
            parse_bgp_lib::SWAP_BYTES(&value32bit);

            std::ostringstream numString;
            numString << value32bit;
            update.attrs[LIB_ATTR_MED].official_type = ATTR_TYPE_MED;
            update.attrs[LIB_ATTR_MED].name = parse_bgp_lib::parse_bgp_lib_attr_names[LIB_ATTR_MED];
            update.attrs[LIB_ATTR_MED].value.push_back(numString.str());
            update_hash(&update.attrs[LIB_ATTR_MED].value, &hash);
            break;
        }
        case ATTR_TYPE_LOCAL_PREF : // local pref value
        {
            memcpy(&value32bit, data, 4);
            parse_bgp_lib::SWAP_BYTES(&value32bit);

             std::ostringstream numString;
            numString << value32bit;
            char numstring[100] = {0};
            update.attrs[LIB_ATTR_LOCAL_PREF].official_type = ATTR_TYPE_LOCAL_PREF;
            update.attrs[LIB_ATTR_LOCAL_PREF].name = parse_bgp_lib::parse_bgp_lib_attr_names[LIB_ATTR_LOCAL_PREF];
            update.attrs[LIB_ATTR_LOCAL_PREF].value.push_back(numString.str());
            update_hash(&update.attrs[LIB_ATTR_LOCAL_PREF].value, &hash);

            break;
        }
        case ATTR_TYPE_ATOMIC_AGGREGATE : // Atomic aggregate
            update.attrs[LIB_ATTR_ATOMIC_AGGREGATE].official_type = ATTR_TYPE_ATOMIC_AGGREGATE;
            update.attrs[LIB_ATTR_ATOMIC_AGGREGATE].name = parse_bgp_lib::parse_bgp_lib_attr_names[LIB_ATTR_ATOMIC_AGGREGATE];
            update.attrs[LIB_ATTR_ATOMIC_AGGREGATE].value.push_back(std::string("1"));
            break;

        case ATTR_TYPE_AGGREGATOR : // Aggregator
            parseAttrDataAggregator(attr_len, data, update);
            update_hash(&update.attrs[LIB_ATTR_AGGREGATOR].value, &hash);

            break;

        case ATTR_TYPE_ORIGINATOR_ID : // Originator ID
            memcpy(ipv4_raw, data, 4);
            inet_ntop(AF_INET, ipv4_raw, ipv4_char, sizeof(ipv4_char));
            update.attrs[LIB_ATTR_ORIGINATOR_ID].official_type = ATTR_TYPE_ORIGINATOR_ID;
            update.attrs[LIB_ATTR_ORIGINATOR_ID].name = parse_bgp_lib::parse_bgp_lib_attr_names[LIB_ATTR_ORIGINATOR_ID];
            update.attrs[LIB_ATTR_ORIGINATOR_ID].value.push_back(std::string(ipv4_char));
            break;

        case ATTR_TYPE_CLUSTER_LIST : // Cluster List (RFC 4456)
            // According to RFC 4456, the value is a sequence of cluster id's
            update.attrs[LIB_ATTR_CLUSTER_LIST].official_type = ATTR_TYPE_CLUSTER_LIST;
            update.attrs[LIB_ATTR_CLUSTER_LIST].name = parse_bgp_lib::parse_bgp_lib_attr_names[LIB_ATTR_CLUSTER_LIST];
            for (int i = 0; i < attr_len; i += 4) {
                memcpy(ipv4_raw, data, 4);
                data += 4;
                inet_ntop(AF_INET, ipv4_raw, ipv4_char, sizeof(ipv4_char));
                update.attrs[LIB_ATTR_CLUSTER_LIST].value.push_back(std::string(ipv4_char));
            }
            break;

        case ATTR_TYPE_COMMUNITIES : // Community list
        {
            update.attrs[LIB_ATTR_COMMUNITIES].official_type = ATTR_TYPE_COMMUNITIES;
            update.attrs[LIB_ATTR_COMMUNITIES].name = parse_bgp_lib::parse_bgp_lib_attr_names[LIB_ATTR_COMMUNITIES];
            for (int i = 0; i < attr_len; i += 4) {
                std::ostringstream numString;
                // Add entry
                memcpy(&value16bit, data, 2);
                data += 2;
                parse_bgp_lib::SWAP_BYTES(&value16bit);
                numString << value16bit;
                numString << ":";

                memcpy(&value16bit, data, 2);
                data += 2;
                parse_bgp_lib::SWAP_BYTES(&value16bit);
                numString << value16bit;
                update.attrs[LIB_ATTR_COMMUNITIES].value.push_back(numString.str());
            }
            update_hash(&update.attrs[LIB_ATTR_COMMUNITIES].value, &hash);

            break;
        }
        case ATTR_TYPE_EXT_COMMUNITY : // extended community list (RFC 4360)
        {
            update.attrs[LIB_ATTR_EXT_COMMUNITY].official_type = ATTR_TYPE_EXT_COMMUNITY;
            update.attrs[LIB_ATTR_EXT_COMMUNITY].name = parse_bgp_lib::parse_bgp_lib_attr_names[LIB_ATTR_EXT_COMMUNITY];
            parse_bgp_lib::ExtCommunity ec(this, logger, debug);
            ec.parseExtCommunities(attr_len, data, update);
            update_hash(&update.attrs[LIB_ATTR_EXT_COMMUNITY].value, &hash);

            break;
        }

        case ATTR_TYPE_IPV6_EXT_COMMUNITY : // IPv6 specific extended community list (RFC 5701)
        {
            update.attrs[LIB_ATTR_IPV6_EXT_COMMUNITY].official_type = ATTR_TYPE_IPV6_EXT_COMMUNITY;
            update.attrs[LIB_ATTR_IPV6_EXT_COMMUNITY].name = parse_bgp_lib::parse_bgp_lib_attr_names[LIB_ATTR_IPV6_EXT_COMMUNITY];
            parse_bgp_lib::ExtCommunity ec6(this, logger, debug);
            ec6.parsev6ExtCommunities(attr_len, data, update);
            break;
        }

        case ATTR_TYPE_MP_REACH_NLRI :  // RFC4760
        {
            parse_bgp_lib::MPReachAttr mp(this, logger, debug);
            mp.parseReachNlriAttr(attr_len, data, update);
            break;
        }

        case ATTR_TYPE_MP_UNREACH_NLRI : // RFC4760
        {
            parse_bgp_lib::MPUnReachAttr mp(this, logger, debug);
            mp.parseUnReachNlriAttr(attr_len, data, update);
            break;
        }

        case ATTR_TYPE_AS_PATHLIMIT : // deprecated
        {
            break;
        }

        case ATTR_TYPE_BGP_LS: {
            MPLinkStateAttr ls(this, logger, &update, debug);
            ls.parseAttrLinkState(attr_len, data);
            break;
        }

        case ATTR_TYPE_AS4_PATH: {
            SELF_DEBUG("%sAttribute type AS4_PATH is not yet implemented, skipping for now.", debug_prepend_string.c_str());
            break;
        }

        case ATTR_TYPE_AS4_AGGREGATOR: {
            SELF_DEBUG("%sAttribute type AS4_AGGREGATOR is not yet implemented, skipping for now.", debug_prepend_string.c_str());
            break;
        }

        default:
            LOG_INFO("%sAttribute type %d is not yet implemented or intentionally ignored, skipping for now.",
                     debug_prepend_string.c_str(), attr_type);
            break;

    } // END OF SWITCH ATTR TYPE
}
Ejemplo n.º 21
0
int main( int argc, char* argv[ ] )
{
   int first_arg = 0;
   bool prune = false;
   bool recurse = false;
   bool invalid = false;
   bool is_quiet = false;
   bool use_zlib = true;
   bool is_delete = false;
   bool is_quieter = false;

   string open_mode( "wb" );

#ifndef ZLIB_SUPPORT
   encoding_type encoding = e_encoding_type_esc;
#else
   encoding_type encoding = e_encoding_type_raw;
#endif

   if( argc > first_arg + 1 )
   {
      if( string( argv[ first_arg + 1 ] ) == "-0" )
      {
         ++first_arg;
         open_mode += "0";
      }
      else if( string( argv[ first_arg + 1 ] ) == "-1" )
      {
         ++first_arg;
         open_mode += "1";
      }
      else if( string( argv[ first_arg + 1 ] ) == "-9" )
      {
         ++first_arg;
         open_mode += "9";
      }
   }

   if( argc > first_arg + 1 )
   {
      if( string( argv[ first_arg + 1 ] ) == "-d" )
      {
         ++first_arg;
         is_delete = true;
      }
   }

   if( !is_delete )
   {
      if( argc > first_arg + 1 )
      {
         if( string( argv[ first_arg + 1 ] ) == "-r" )
         {
            ++first_arg;
            recurse = true;
         }
      }

      if( argc > first_arg + 1 )
      {
         if( string( argv[ first_arg + 1 ] ) == "-p" )
         {
            ++first_arg;
            prune = true;

            if( !recurse )
               invalid = true;
         }
      }
   }

   if( argc > first_arg + 1 )
   {
      if( string( argv[ first_arg + 1 ] ) == "-q" )
      {
         ++first_arg;
         is_quiet = true;
      }
   }

   if( argc > first_arg + 1 )
   {
      if( string( argv[ first_arg + 1 ] ) == "-qq" )
      {
         ++first_arg;
         is_quiet = true;
         is_quieter = true;
      }
   }

   if( argc > first_arg + 1 )
   {
      // NOTE: Ignore '-y' for compatibility with zip.
      if( string( argv[ first_arg + 1 ] ) == "-y" )
         ++first_arg;
   }

   if( argc > first_arg + 1 )
   {
      if( string( argv[ first_arg + 1 ] ) == "-b64" )
      {
         ++first_arg;
         encoding = e_encoding_type_b64;
      }
      else if( string( argv[ first_arg + 1 ] ) == "-esc" )
      {
         ++first_arg;
         encoding = e_encoding_type_esc;
      }
   }

#ifdef ZLIB_SUPPORT
   if( argc > first_arg + 1 )
   {
      if( string( argv[ first_arg + 1 ] ) == "-ngz" )
      {
         ++first_arg;
         use_zlib = false;
      }
   }
#endif

   if( !is_quiet )
      cout << "bundle v0.1e\n";

   if( invalid || ( argc - first_arg < 2 )
    || string( argv[ 1 ] ) == "?" || string( argv[ 1 ] ) == "/?" || string( argv[ 1 ] ) == "-?" )
   {
#ifndef ZLIB_SUPPORT
      cout << "usage: bundle [-0|-1|-9] [-d]|[-r [-p]] [-q[q]] [-b64|-esc] <fname> [<fspec1> [<fspec2> [...]]] [-x <fspec1> [...]]" << endl;
#else
      cout << "usage: bundle [-0|-1|-9] [-d]|[-r [-p]] [-q[q]] [-b64|-esc] [-ngz] <fname> [<fspec1> [<fspec2> [...]]] [-x <fspec1> [...]]" << endl;
#endif

      cout << "\nwhere: -0/-1/-9 is used for setting zero/fastest/best compression level" << endl;
      cout << "  and: -d is to delete matching files which exist in an existing bundle" << endl;
      cout << "  and: -r is to recurse sub-directories (-p to prune empty directories)" << endl;
      cout << "  and: -q for quiet mode (-qq to suppress all output apart from errors)" << endl;
      cout << "  and: -b64/-esc stores file data using b64/esc encoding for text lines" << endl;
#ifdef ZLIB_SUPPORT
      cout << "  and: -ngz in order to not preform zlib compression" << endl;
#endif
      cout << " also: -x identifies one or more filespecs that are to be excluded" << endl;
      return 0;
   }

   try
   {
      g_cwd = get_cwd( );

      string::size_type pos;

#ifdef _WIN32
      while( ( pos = g_cwd.find( '\\' ) ) != string::npos )
         g_cwd[ pos ] = '/';
#endif

      pos = g_cwd.find_last_of( '/' );

      if( is_root_path( g_cwd ) )
         throw runtime_error( "cannot created a bundle in the root directory" );

      map< string, vector< string > > all_filespecs;
      map< string, vector< string > > all_exclude_filespecs;

      string filename( argv[ first_arg + 1 ] );

      if( !filename.empty( ) && filename[ 0 ] == '-' )
         throw runtime_error( "unknown or bad option '" + filename + "' use -? to see options" );

      string ext( c_default_extension );
      if( use_zlib )
         ext += c_zlib_extension;

      if( filename.find( ext ) == string::npos )
         filename += ext;

      bool get_exclude_filespecs = false;
      string directory = g_cwd.substr( pos + 1 );

      if( !is_delete )
      {
         for( int i = first_arg + 2; i < argc; i++ )
         {
            string next( argv[ i ] );

            if( !next.empty( ) && next[ 0 ] == '-' )
            {
               if( next == "-x" && !get_exclude_filespecs )
               {
                  next.erase( );
                  get_exclude_filespecs = true;
               }
               else
                  throw runtime_error( "unknown or bad option '" + next + "' use -? to see options" );
            }

            if( !next.empty( ) )
            {
               string::size_type wpos = next.find_first_of( "?*" );

               string filespec_path;
               if( wpos == 0 )
               {
                  filespec_path = g_cwd + "/" + next;
                  wpos = string::npos;
               }
               else
               {
                  if( !absolute_path( next.substr( 0, wpos ), filespec_path ) )
                     throw runtime_error( "unable to determine absolute path for '" + next + "'" );
               }

#ifdef _WIN32
               string::size_type pos;
               while( ( pos = filespec_path.find( '\\' ) ) != string::npos )
                  filespec_path[ pos ] = '/';
#endif

               if( is_root_path( filespec_path ) )
                  throw runtime_error( "cannot bundle directory '" + next + "' (need to specify a non-root directory)" );

               if( wpos != string::npos )
#ifdef _WIN32
                  filespec_path += next.substr( wpos );
#else
                  filespec_path += "/" + next.substr( wpos );
#endif
               string::size_type rpos = filespec_path.find_last_of( '/' );

               string filename_filter;
               if( rpos != string::npos )
               {
                  filename_filter = filespec_path.substr( rpos + 1 );
                  filespec_path.erase( rpos );
               }

               if( recurse == true
                && filespec_path.length( ) > g_cwd.length( ) && filespec_path.find( g_cwd ) == 0 )
               {
                  filename_filter = filespec_path.substr( g_cwd.length( ) + 1 ) + "/" + filename_filter;
                  filespec_path = g_cwd;
               }

               if( !get_exclude_filespecs )
                  all_filespecs[ filespec_path ].push_back( filename_filter );
               else
                  all_exclude_filespecs[ filespec_path ].push_back( filename_filter );
            }
         }

         if( all_filespecs.empty( ) )
            all_filespecs[ g_cwd ].push_back( "*" );
      }

      int num_deleted = 0;

      set< string > file_names;
      set< string > matched_filters;

      bool is_append = false;
      if( file_exists( filename ) )
         is_append = true;

      if( is_delete && first_arg + 2 >= argc )
         throw runtime_error( "cannot delete files without specifying at least one filespec" );

      if( is_delete && !is_append )
         throw runtime_error( "no bundle file '" + filename + "' was found" );

      string output_filename( filename );
      if( is_append )
         output_filename = output_filename + ".tmp";

      // NOTE: Empty code block for scope purposes.
      {
#ifndef ZLIB_SUPPORT
         ofstream outf( output_filename.c_str( ) );
         if( !outf )
            throw runtime_error( "unable to open file '" + output_filename + "' for output" );
#else
         gzFile gzf;
         ofstream outf;

         if( !use_zlib )
            outf.open( output_filename.c_str( ) );
         else
            gzf = gzopen( output_filename.c_str( ), open_mode.c_str( ) );

         if( ( use_zlib && !gzf ) || ( !use_zlib && !outf ) )
            throw runtime_error( "unable to open file '" + output_filename + "' for output" );
#endif

         if( !is_quiet )
         {
            if( !is_append )
               cout << "==> started bundling '" << filename << "'\n";
            else
               cout << "==> continue bundling '" << filename << "'\n";
         }

         absolute_path( filename, g_bundle_file_name );
         absolute_path( output_filename, g_output_file_name );

         string header( "B " );
         header += to_string( c_format_version );

         switch( encoding )
         {
            case e_encoding_type_b64:
            header += " B64";
            break;

            case e_encoding_type_esc:
            header += " ESC";
            break;

            case e_encoding_type_raw:
            header += " RAW";
            break;
         }

#ifndef ZLIB_SUPPORT
         outf << header << '\n';
#else
         if( !use_zlib )
            outf << header << '\n';
         else
            write_zlib_line( gzf, header );
#endif

         if( !is_delete )
         {
            for( map< string, vector< string > >::iterator i = all_filespecs.begin( ); i != all_filespecs.end( ); ++i )
            {
               string filespec_path( i->first );
               vector< string >& filename_filters( i->second );

               vector< string >* p_filename_exclusions = 0;
               if( all_exclude_filespecs.count( i->first ) )
                  p_filename_exclusions = &all_exclude_filespecs[ i->first ];

#ifndef ZLIB_SUPPORT
               process_directory( directory, filespec_path, filename_filters, p_filename_exclusions,
                matched_filters, file_names, recurse, prune, is_quieter, is_append, encoding, outf );
#else
               process_directory( directory, filespec_path, filename_filters, p_filename_exclusions,
                matched_filters, file_names, recurse, prune, is_quieter, is_append, encoding, outf, use_zlib, gzf );
#endif
               if( !is_quieter )
               {
                  for( size_t i = 0; i < filename_filters.size( ); i++ )
                  {
                     if( matched_filters.count( filename_filters[ i ] ) == 0 )
                        cout << "warning: found no files matching '" << filename_filters[ i ] << "'" << endl;
                  }
               }
            }
         }

         if( is_append )
         {
#ifdef ZLIB_SUPPORT
            gzFile igzf;
            ifstream inpf;

            if( !use_zlib )
               inpf.open( filename.c_str( ) );
            else
               igzf = gzopen( filename.c_str( ), "rb" );

            if( ( use_zlib && !igzf ) || ( !use_zlib && !inpf ) )
               throw runtime_error( "unable to open file '" + filename + "' for input" );
#else
            ifstream inpf( filename.c_str( ) );
            if( !inpf )
               throw runtime_error( "unable to open file '" + filename + "' for input" );
#endif

            encoding_type old_encoding;
#ifndef ZLIB_SUPPORT
            check_file_header( inpf, filename, old_encoding );
#else
            check_file_header( inpf, filename, old_encoding, use_zlib, igzf );
#endif

            if( old_encoding != encoding )
               throw runtime_error( "*** cannot combine different bundle data encoding types ***" );

            string next;
            int line = 0;
            int count = 0;
            int line_size = 0;
            int file_data_lines = 0;
            int64_t raw_file_size = 0;
            bool skip_existing_file = false;

            string current_sub_path;

            while( true )
            {
               if( raw_file_size )
               {
                  if( skip_existing_file )
                  {
#ifdef ZLIB_SUPPORT
                     if( use_zlib )
                        gzseek( igzf, raw_file_size, SEEK_CUR );
                     else
                        inpf.seekg( raw_file_size, ios::cur );
#else
                     inpf.seekg( raw_file_size, ios::cur );
#endif
                     raw_file_size = 0;
                  }
                  else
                  {
                     int64_t chunk = 0;
                     while( raw_file_size > 0 )
                     {
                        char buffer[ c_buffer_size ];

                        int count = c_buffer_size;
                        if( raw_file_size < c_buffer_size )
                           count = raw_file_size;

#ifdef ZLIB_SUPPORT
                        if( use_zlib )
                        {
                           if( !gzread( igzf, buffer, count ) )
                              throw runtime_error( "reading zlib input" );
                        }
                        else
                        {
                           if( inpf.rdbuf( )->sgetn( buffer, count ) != count )
                              throw runtime_error( "reading file input" );
                        }
#else
                        if( inpf.rdbuf( )->sgetn( buffer, count ) != count )
                           throw runtime_error( "reading file input" );
#endif

                        if( !is_quieter && ++chunk % c_progress_lines == 0 )
                        {
                           if( line == c_progress_lines )
                              cout << ' ';
                           cout << '.';
                           cout.flush( );
                        }

#ifndef ZLIB_SUPPORT
                        outf.rdbuf( )->sputn( buffer, count );
                        if( !outf.good( ) )
                           throw runtime_error( "unexpected bad output file stream" );
#else
                        if( !use_zlib )
                        {
                           outf.rdbuf( )->sputn( buffer, count );

                           if( !outf.good( ) )
                              throw runtime_error( "unexpected bad output file stream" );
                        }
                        else if( !gzwrite( gzf, buffer, count ) )
                           throw runtime_error( "writing zlib block" );
#endif

                        raw_file_size -= count;
                     }

                     if( !is_quieter )
                        cout << endl;

                     continue;
                  }
               }

#ifdef ZLIB_SUPPORT
               if( use_zlib )
               {
                  if( !read_zlib_line( igzf, next, false ) )
                     break;
               }
               else if( !getline( inpf, next ) )
                  break;
#else
               if( !getline( inpf, next ) )
                  break;
#endif
               ++line;
               if( file_data_lines )
               {
                  --file_data_lines;

                  if( count == 0 )
                     line_size = unescaped( next ).size( );

                  // NOTE: If skipping a file then there is no need to actually
                  // read the data so by determining the line size of the first
                  // line (after unescaping) it is safe to "seek" one byte less
                  // forwards and then read the remainder of the line (the size
                  // of this will depend upon how much escaping is being used).
                  if( skip_existing_file && line_size && file_data_lines > 1 )
                  {
#ifdef ZLIB_SUPPORT
                     if( use_zlib )
                        gzseek( igzf, line_size - 1, SEEK_CUR );
                     else
                        inpf.seekg( line_size - 1, ios::cur );
#else
                     inpf.seekg( line_size - 1, ios::cur );
#endif
                  }

                  if( ++count % c_progress_lines == 0 && !is_quieter && !skip_existing_file )
                  {
                     if( count == c_progress_lines )
                        cout << ' ';
                     cout << '.';
                     cout.flush( );
                  }

                  if( !is_quieter && !skip_existing_file && file_data_lines == 0 )
                     cout << endl;

                  if( !skip_existing_file )
                  {
#ifndef ZLIB_SUPPORT
                     outf << line << '\n';
#else
                     if( !use_zlib )
                        outf << next << '\n';
                     else
                        write_zlib_line( gzf, next );
#endif
                  }
               }
               else
               {
                  string::size_type pos = next.find( ' ' );
                  if( pos != 1 )
                     throw runtime_error( "unexpected format in line #" + to_string( line ) );

                  char type( next[ 0 ] );

                  string next_line( next );

                  if( type == c_type_file )
                  {
                     next.erase( 0, 2 );
                     pos = next.find( ' ' );
                     if( pos == string::npos )
                        throw runtime_error( "unexpected format in line #" + to_string( line ) );

                     if( encoding == e_encoding_type_raw )
                        raw_file_size = unformat_bytes( next.substr( 0, pos ) );
                     else
                     {
                        int num_lines( atoi( next.substr( 0, pos ).c_str( ) ) );
                        if( num_lines < 0 )
                           throw runtime_error( "invalid number of lines "
                            + to_string( num_lines ) + " specified in line #" + to_string( line ) );

                        file_data_lines = num_lines;
                     }

                     next.erase( 0, pos + 1 );

                     string::size_type pos = next.find_last_of( " " );
                     if( pos == string::npos )
                        throw runtime_error( "unexpected file entry format in line #" + to_string( line ) );

                     string check = next.substr( pos + 1 );
                     next.erase( pos );

                     pos = next.find( ' ' );
                     if( pos == string::npos )
                        throw runtime_error( "unexpected file entry format in line #" + to_string( line ) );

                     string rwx_perms( next.substr( 0, pos ) );
                     next.erase( 0, pos + 1 );

                     count = 0;

                     if( is_delete )
                     {
                        bool matched = false;
                        vector< string >& exprs( all_filespecs[ c_delete_dummy_path ] );

                        string next_path( current_sub_path + '/' + next );
                        for( size_t i = 0; i < exprs.size( ); i++ )
                        {
                           if( wildcard_match( exprs[ i ], next_path ) )
                           {
                              if( !is_quieter )
                                 cout << "*kill* \"" << next << "\"" << endl;
                              ++num_deleted;
                              matched = true;
                              break;
                           }
                        }

                        if( matched )
                        {
                           skip_existing_file = true;
                           continue;
                        }
                     }

                     pos = current_sub_path.find( '/' );
                     if( pos != string::npos )
                        next = current_sub_path.substr( pos + 1 ) + '/' + next;

                     if( file_names.count( next ) )
                     {
                        skip_existing_file = true;
                        continue;
                     }
                     else
                     {
                        skip_existing_file = false;

                        if( !is_quieter )
                           cout << "append \"" << next << "\"";

                        file_names.insert( next );
                        g_md5.update( ( unsigned char* )check.c_str( ), check.length( ) );
                     }
                  }
                  else if( type == c_type_directory )
                  {
                     next.erase( 0, 2 );
                     pos = next.find( ' ' );
                     if( pos == string::npos )
                        throw runtime_error( "unexpected format in line #" + to_string( line ) );

                     string::size_type cpos = next.find_last_of( ' ' );
                     if( cpos == pos || cpos == string::npos )
                        throw runtime_error( "unexpected format in line #" + to_string( line ) );

                     string check( next.substr( cpos + 1 ) );
                     next.erase( cpos );

                     pos = next.find( ' ' );
                     if( pos == string::npos )
                        throw runtime_error( "unexpected format in line #" + to_string( line ) );

                     int next_level( atoi( next.substr( 0, pos ).c_str( ) ) );
                     next.erase( 0, pos + 1 );

                     pos = next.find( ' ' );
                     if( pos == string::npos )
                        throw runtime_error( "unexpected format in line #" + to_string( line ) );

                     string rwx_perms( next.substr( 0, pos ) );
                     next.erase( 0, pos + 1 );

                     if( is_delete && all_filespecs.empty( ) )
                     {
                        for( int i = first_arg + 2; i < argc; i++ )
                           all_filespecs[ c_delete_dummy_path ].push_back( next + '/' + string( argv[ i ] ) );
                     }

                     current_sub_path = next;

                     if( file_names.count( next ) )
                        continue;
                     else
                     {
                        pos = next.find( '/' );
                        if( !is_quieter && pos != string::npos )
                           cout << "append \"" << next.substr( pos + 1 ) << "/\"\n";

                        file_names.insert( next );
                        g_md5.update( ( unsigned char* )check.c_str( ), check.length( ) );
                     }
                  }
                  else if( type == c_type_checksum )
                     break;
                  else
                     throw runtime_error( "unexpected entry type '" + to_string( type ) + "' found in line #" + to_string( line ) );

#ifndef ZLIB_SUPPORT
                  outf << next_line << '\n';
#else
                  if( !use_zlib )
                     outf << next_line << '\n';
                  else
                     write_zlib_line( gzf, next_line );
#endif
               }
            }

#ifdef ZLIB_SUPPORT
            if( use_zlib )
               gzclose( igzf );
#endif
         }

         g_md5.finalize( );
         ostringstream osstr;
         auto_ptr< char > ap_digest( g_md5.hex_digest( ) );

         osstr << "C " << ap_digest.get( );

#ifndef ZLIB_SUPPORT
         outf << osstr.str( ) << '\n';
#else
         if( !use_zlib )
            outf << osstr.str( ) << '\n';
         else
            write_zlib_line( gzf, osstr.str( ) );
#endif

#ifdef ZLIB_SUPPORT
         if( use_zlib )
            gzclose( gzf );
#endif

         if( !use_zlib )
         {
            outf.flush( );
            if( !outf.good( ) )
               throw runtime_error( "unexpected write failure for output file '" + filename + "'" );
         }
      }

      if( is_append )
      {
         if( !file_remove( filename ) || rename( output_filename.c_str( ), filename.c_str( ) ) != 0 )
            throw runtime_error( "unable to replace original '" + filename + "' with '" + output_filename + "'" );

         if( is_delete && !is_quieter && num_deleted == 0 )
            cout << "warning: found no matching files to delete" << endl;
      }
      else if( matched_filters.empty( ) )
      {
         file_remove( filename );
         throw runtime_error( "*** nothing to do ***" );
      }

      if( !is_quiet )
         cout << "==> finished bundling '" << filename << "'" << endl;
   }
   catch( exception& x )
   {
      cerr << "error: " << x.what( ) << endl;
      return 1;
   }

   return 0;
}
Ejemplo n.º 22
0
/**
 * Abstract method Implementation - See DbInterface.hpp for details
 */
void mysqlBMP::add_Router(tbl_router &r_entry) {
    try {
        char buf[4096]; // Misc working buffer

        /*
         * Generate router table hash from the following fields
         *     r_entry.name, r_entry.src_addr
         *
         */
        MD5 hash;

        // Generate the hash
        //hash.update (r_entry.name, strlen((char *)r_entry.name));
        hash.update(r_entry.src_addr, strlen((char *) r_entry.src_addr));
        hash.finalize();

        // Save the hash
        unsigned char *hash_raw = hash.raw_digest();
        memcpy(r_entry.hash_id, hash_raw, 16);
        delete [] hash_raw;

        // Convert binary hash to string
        string r_hash_str;
        hash_toStr(r_entry.hash_id, r_hash_str);

        // Check if we have already processed this entry, if so update it an return
        if (router_list.find(r_hash_str) != router_list.end()) {
            router_list[r_hash_str] = time(NULL);
            return;
        }

        // Insert/Update map entry
        router_list[r_hash_str] = time(NULL);

        // Convert the init data to string for storage
        string initData(r_entry.initiate_data);
        std::replace(initData.begin(), initData.end(), '\'', '"');

        // Build the query
        snprintf(buf, sizeof(buf),
                "INSERT into %s (%s) values ('%s', '%s', '%s','%s','%s')",
                TBL_NAME_ROUTERS, "hash_id,name,description,ip_address,init_data", r_hash_str.c_str(),
                r_entry.name, r_entry.descr, r_entry.src_addr, initData.c_str());

        // Add the on duplicate statement
        strcat(buf, " ON DUPLICATE KEY UPDATE timestamp=current_timestamp,isConnected=1,name=values(name),description=values(description),init_data=values(init_data)");

        // Run the query to add the record
        stmt = con->createStatement();
        stmt->execute(buf);

        // Update all peers to indicate peers are not up - till proven other wise
        snprintf(buf, sizeof(buf), "UPDATE %s SET state=0 where router_hash_id='%s'",
                TBL_NAME_BGP_PEERS, r_hash_str.c_str());
        stmt->execute(buf);

        // Free the query statement
        delete stmt;

    } catch (sql::SQLException &e) {
        LOG_ERR("mysql error: %s, error Code = %d, state = %s",
                e.what(), e.getErrorCode(), e.getSQLState().c_str() );

    }
}
Ejemplo n.º 23
0
/**
 * Run Server loop
 *
 * \param [in]  cfg    Reference to the config options
 */
void runServer(Cfg_Options &cfg) {
    msgBus_kafka *kafka;
    int active_connections = 0;                 // Number of active connections/threads
    time_t last_heartbeat_time = 0;

    LOG_INFO("Initializing server");

    try {
        // Define the collector hash
        MD5 hash;
        hash.update((unsigned char *)cfg.admin_id, strlen(cfg.admin_id));
        hash.finalize();

        // Save the hash
        unsigned char *hash_raw = hash.raw_digest();
        memcpy(cfg.c_hash_id, hash_raw, 16);
        delete[] hash_raw;


        // Kafka connection
        kafka = new msgBus_kafka(logger, cfg.kafka_brokers, cfg.c_hash_id);

        // allocate and start a new bmp server
        BMPListener *bmp_svr = new BMPListener(logger, &cfg);

        BMPListener::ClientInfo client;
        collector_update_msg(kafka, cfg, client, MsgBusInterface::COLLECTOR_ACTION_STARTED);
        last_heartbeat_time = time(NULL);

        LOG_INFO("Ready. Waiting for connections");

        // Loop to accept new connections
        while (run) {
            /*
             * Check for any stale threads/connections
             */
             for (size_t i=0; i < thr_list.size(); i++) {

                // If thread is not running, it means it terminated, so close it out
                if (!thr_list.at(i)->running) {

                    // Join the thread to clean up
                    pthread_join(thr_list.at(i)->thr, NULL);
                    --active_connections;

                    collector_update_msg(kafka, cfg, thr_list.at(i)->client,
                                         MsgBusInterface::COLLECTOR_ACTION_CHANGE);

                    // free the vector entry
                    delete thr_list.at(i);
                    thr_list.erase(thr_list.begin() + i);
                }

                //TODO: Add code to check for a socket that is open, but not really connected/half open
            }

            /*
             * Create a new client thread if we aren't at the max number of active sessions
             */
            if (active_connections <= MAX_THREADS) {
                ThreadMgmt *thr = new ThreadMgmt;
                thr->cfg = &cfg;
                thr->log = logger;

                // wait for a new connection and accept
                if (bmp_svr->wait_and_accept_connection(thr->client, 500)) {
                    // Bump the current thread count
                    ++active_connections;

                    LOG_INFO("Accepted new connection; active connections = %d", active_connections);

                    /*
                     * Start a new thread for every new router connection
                     */
                    LOG_INFO("Client Connected => %s:%s, sock = %d",
                             thr->client.c_ip, thr->client.c_port, thr->client.c_sock);

                    pthread_attr_t thr_attr;            // thread attribute
                    pthread_attr_init(&thr_attr);
                    //pthread_attr_setdetachstate(&thr.thr_attr, PTHREAD_CREATE_DETACHED);
                    pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_JOINABLE);
                    thr->running = 1;

                    // Start the thread to handle the client connection
                    pthread_create(&thr->thr, &thr_attr,
                                   ClientThread, thr);

                    // Add thread to vector
                    thr_list.insert(thr_list.end(), thr);

                    // Free attribute
                    pthread_attr_destroy(&thr_attr);

                    collector_update_msg(kafka, cfg, thr->client,
                                         MsgBusInterface::COLLECTOR_ACTION_CHANGE);

                    last_heartbeat_time = time(NULL);
                }
                else {
                    delete thr;

                    // Send heartbeat if needed
                    if ( (time(NULL) - last_heartbeat_time) >= cfg.heartbeat_interval) {
                        BMPListener::ClientInfo client;
                        collector_update_msg(kafka, cfg, client, MsgBusInterface::COLLECTOR_ACTION_HEARTBEAT);
                        last_heartbeat_time = time(NULL);
                    }

                    usleep(10000);
                }

            } else {
                LOG_WARN("Reached max number of threads, cannot accept new BMP connections at this time.");
                sleep (1);
            }
        }

        collector_update_msg(kafka, cfg, client, MsgBusInterface::COLLECTOR_ACTION_STOPPED);
        delete kafka;

    } catch (char const *str) {
        LOG_WARN(str);
    }
}
Ejemplo n.º 24
0
int main( int argc, char* argv[ ] )
{
    int first_arg = 0;
    bool junk = false;
    bool prune = false;
    bool include = false;
    bool use_zlib = false;
    bool is_quiet = false;
    bool list_only = false;
    bool overwrite = false;
    bool is_quieter = false;

    if( argc > first_arg + 1 )
    {
        if( string( argv[ first_arg + 1 ] ) == "-i" )
        {
            ++first_arg;
            junk = true;
        }
        else if( string( argv[ first_arg + 1 ] ) == "-j" )
        {
            ++first_arg;
            include = true;
        }
    }

    if( argc > first_arg + 1 )
    {
        if( string( argv[ first_arg + 1 ] ) == "-l" )
        {
            ++first_arg;
            list_only = true;
        }
    }

    if( argc > first_arg + 1 )
    {
        if( string( argv[ first_arg + 1 ] ) == "-o" )
        {
            ++first_arg;
            overwrite = true;
        }
    }

    if( argc > first_arg + 1 )
    {
        if( string( argv[ first_arg + 1 ] ) == "-p" )
        {
            ++first_arg;
            prune = true;
        }
    }

    if( argc > first_arg + 1 )
    {
        if( string( argv[ first_arg + 1 ] ) == "-q" )
        {
            ++first_arg;
            is_quiet = true;
        }
    }

    if( argc > first_arg + 1 )
    {
        if( string( argv[ first_arg + 1 ] ) == "-qq" )
        {
            ++first_arg;
            is_quiet = true;
            is_quieter = true;
        }
    }

    if( !is_quiet )
        cout << "unbundle v0.1d\n";

    if( ( argc - first_arg < 2 )
            || string( argv[ 1 ] ) == "?" || string( argv[ 1 ] ) == "/?" || string( argv[ 1 ] ) == "-?" )
    {
        cout << "usage: unbundle [-i|-j] [-l] [-o] [-p] [-q[q]] <fname> [<fspec1> [<fspec2> [...]]] [-x <fspec1> [...]] [-d <directory>]" << endl;

        cout << "\nwhere: -i to include top level directory and -j to junk all directories" << endl;
        cout << "  and: -l to list rather than create all matching files and directories" << endl;
        cout << "  and: -o to overwrite existing files and -p to prune empty directories" << endl;
        cout << "  and: -q for quiet mode (-qq to suppress all output apart from errors)" << endl;
        cout << "  and: -x identifies one or more filespecs that are to be excluded" << endl;
        cout << " also: -d <directory> to set a directory origin for output" << endl;
        return 0;
    }

#ifdef __GNUG__
    umask( DEFAULT_UMASK );
#endif

    try
    {
        string filename( argv[ first_arg + 1 ] );

        if( !filename.empty( ) && filename[ 0 ] == '-' )
            throw runtime_error( "unknown or bad option '" + filename + "' use -? to see options" );

        if( filename.length( ) > 3
                && filename.substr( filename.length( ) - 3 ) == string( c_zlib_extension ) )
        {
#ifdef ZLIB_SUPPORT
            use_zlib = true;
#else
            throw runtime_error( "this program has not been compiled with ZLIB support" );
#endif
        }

        string::size_type pos = filename.find( '.' );

        if( pos == string::npos || _access( filename.c_str( ), 0 ) != 0 )
            filename += c_default_extension;

#ifdef ZLIB_SUPPORT
        if( _access( filename.c_str( ), 0 ) != 0 )
        {
            use_zlib = true;
            filename += c_zlib_extension;
        }

        gzFile gzf;
        ifstream inpf;

        if( !use_zlib )
            inpf.open( filename.c_str( ) );
        else
            gzf = gzopen( filename.c_str( ), "rb" );

        if( ( use_zlib && !gzf ) || ( !use_zlib && !inpf ) )
            throw runtime_error( "unable to open file '" + filename + "' for input" );
#else
        ifstream inpf( filename.c_str( ) );
        if( !inpf )
            throw runtime_error( "unable to open file '" + filename + "' for input" );
#endif

        if( !is_quiet && !list_only )
            cout << "==> started unbundling '" << filename << "'\n";

        bool get_exclude_filespecs = false;

        string destination_directory;
        bool get_destination_directory = false;

        vector< string > filename_filters;
        vector< string > exclude_filename_filters;
        for( int i = first_arg + 2; i < argc; i++ )
        {
            string next( argv[ i ] );

            if( !next.empty( ) && next[ 0 ] == '-' )
            {
                if( next == "-d" && !get_destination_directory )
                {
                    next.erase( );
                    get_destination_directory = true;
                }
                else if( next == "-x" && !get_exclude_filespecs )
                {
                    next.erase( );
                    get_exclude_filespecs = true;
                }
                else
                    throw runtime_error( "unknown or bad option '" + next + "' use -? to see options" );
            }

            if( !next.empty( ) )
            {
                if( get_destination_directory && destination_directory.empty( ) )
                {
                    destination_directory = next;
#ifndef _WIN32
                    if( destination_directory[ destination_directory.size( ) - 1 ] != '/' )
                        destination_directory += '/';
#else
                    if( destination_directory[ destination_directory.size( ) - 1 ] != '/'
                            && destination_directory[ destination_directory.size( ) - 1 ] != '\\'
                            && destination_directory[ destination_directory.size( ) - 1 ] != ':' )
                        destination_directory += '/';

                    replace( destination_directory, "\\", "/" );
#endif
                }
                else if( get_exclude_filespecs )
                    exclude_filename_filters.push_back( next );
                else
                    filename_filters.push_back( next );
            }
        }

        int level = -1;
        bool is_first = false;

        string next_file;
        string rwx_perms;
        int line_size = 0;
        int file_data_lines = 0;
        int64_t raw_file_size = 0;

        stack< string > paths;
        deque< string > create_directories;
        map< string, string > directory_perms;

        set< string > created;
        set< string > matched_filters;

        auto_ptr< ofstream > ap_ofstream;

        MD5 md5;
        string next;
        string next_md5;
        size_t line = 1;
        size_t count = 0;

        bool finished = false;
        bool replace_all = false;
        bool replace_none = false;

        encoding_type encoding;
        string top_level_directory;

        int progress = c_progress_lines;

#ifndef ZLIB_SUPPORT
        check_file_header( inpf, filename, encoding );
#else
        check_file_header( inpf, filename, encoding, use_zlib, gzf );
#endif

        while( true )
        {
            if( raw_file_size )
            {
                if( !ap_ofstream.get( ) )
                {
#ifdef ZLIB_SUPPORT
                    if( use_zlib )
                        gzseek( gzf, raw_file_size, SEEK_CUR );
                    else
                        inpf.seekg( raw_file_size, ios::cur );
#else
                    inpf.seekg( raw_file_size, ios::cur );
#endif
                    raw_file_size = 0;
                }
                else
                {
                    int64_t chunk = 0;
                    while( raw_file_size > 0 )
                    {
                        char buffer[ c_buffer_size ];

                        int count = c_buffer_size;
                        if( raw_file_size < c_buffer_size )
                            count = raw_file_size;

#ifdef ZLIB_SUPPORT
                        if( use_zlib )
                        {
                            if( !gzread( gzf, buffer, count ) )
                                throw runtime_error( "reading zlib input" );
                        }
                        else
                        {
                            if( inpf.rdbuf( )->sgetn( buffer, count ) != count )
                                throw runtime_error( "reading file input" );
                        }
#else
                        if( inpf.rdbuf( )->sgetn( buffer, count ) != count )
                            throw runtime_error( "reading file input" );
#endif

                        if( !is_quieter && ++chunk % c_progress_lines == 0 )
                        {
                            if( line == c_progress_lines )
                                cout << ' ';
                            cout << '.';
                            cout.flush( );
                        }

                        md5.update( ( unsigned char* )buffer, count );

                        ap_ofstream->rdbuf( )->sputn( buffer, count );
                        if( !ap_ofstream->good( ) )
                            throw runtime_error( "unexpected bad output file stream" );

                        raw_file_size -= count;
                    }

                    md5.finalize( );
                    auto_ptr< char > ap_digest( md5.hex_digest( ) );

                    if( next_md5 != string( ap_digest.get( ) ) )
                        cerr << "*** error: file '" << next_file << "' failed MD5 digest check ***" << endl;

                    ap_ofstream.reset( );
                    file_perms( next_file, rwx_perms );

                    if( !is_quieter )
                        cout << endl;

                    continue;
                }
            }

#ifdef ZLIB_SUPPORT
            if( use_zlib )
            {
                if( !read_zlib_line( gzf, next ) )
                    break;
            }
            else if( !getline( inpf, next ) )
                break;
#else
            if( !getline( inpf, next ) )
                break;
#endif
            ++line;

            if( next.empty( ) )
                throw runtime_error( "unexpected empty line #" + to_string( line ) );

            if( file_data_lines )
            {
                --file_data_lines;

                if( count == 0 )
                {
                    line_size = unescaped( next ).size( );

                    if( line_size >= 1048576 ) // i.e. 1 MB
                        progress = 2;
                    else
                        progress = c_progress_lines;
                }

                // NOTE: If skipping a file then there is no need to actually
                // read the data so by determining the line size of the first
                // line (after unescaping) it is safe to "seek" one byte less
                // forwards and then read the remainder of the line (the size
                // of this will depend upon how much escaping is being used).
                if( !ap_ofstream.get( ) && line_size && file_data_lines > 1 )
                {
#ifdef ZLIB_SUPPORT
                    if( use_zlib )
                        gzseek( gzf, line_size - 1, SEEK_CUR );
                    else
                        inpf.seekg( line_size - 1, ios::cur );
#else
                    inpf.seekg( line_size - 1, ios::cur );
#endif
                }

                if( ++count % progress == 0 && !is_quieter && ap_ofstream.get( ) )
                {
                    if( count == progress )
                        cout << ' ';
                    cout << '.';
                    cout.flush( );
                }

                if( !is_quieter && ap_ofstream.get( ) && file_data_lines == 0 )
                    cout << endl;

                if( ap_ofstream.get( ) )
                {
                    string fdata;

                    if( use_zlib )
                        fdata = next;
                    else if( encoding != e_encoding_type_b64 )
                        fdata = unescaped_line( next );
                    else
                        fdata = base64::decode( next );

                    if( ap_ofstream->rdbuf( )->sputn( fdata.c_str( ), fdata.length( ) ) != ( int )fdata.length( ) )
                        throw runtime_error( "write failed for file '" + next_file + "'" );

                    md5.update( ( unsigned char* )fdata.c_str( ), fdata.length( ) );

                    if( file_data_lines == 0 )
                    {
                        ap_ofstream->flush( );
                        if( !ap_ofstream->good( ) )
                            throw runtime_error( "flush failed for file '" + next_file + "'" );
                        ap_ofstream->close( );

                        ap_ofstream.reset( );
                        file_perms( next_file, rwx_perms );

                        md5.finalize( );
                        auto_ptr< char > ap_digest( md5.hex_digest( ) );

                        if( next_md5 != string( ap_digest.get( ) ) )
                            cerr << "*** error: file '" << next_file << "' failed MD5 digest check ***" << endl;
                    }
                }

                continue;
            }

            if( finished )
                throw runtime_error( "unexpected end of file not found at line #" + to_string( line ) );

            string::size_type pos = next.find( ' ' );
            if( pos != 1 )
                throw runtime_error( "unexpected format in line #" + to_string( line ) );

            char type( next[ 0 ] );
            if( type == c_type_file )
            {
                if( is_first )
                    throw runtime_error( "unexpected file entry in line #" + to_string( line ) );

                md5.init( );

                next.erase( 0, 2 );
                pos = next.find( ' ' );
                if( pos == string::npos )
                    throw runtime_error( "unexpected format in line #" + to_string( line ) );

                if( encoding == e_encoding_type_raw )
                    raw_file_size = unformat_bytes( next.substr( 0, pos ) );
                else
                {
                    int num_lines( atoi( next.substr( 0, pos ).c_str( ) ) );
                    if( num_lines < 0 )
                        throw runtime_error( "invalid number of lines "
                                             + to_string( num_lines ) + " specified in line #" + to_string( line ) );

                    file_data_lines = num_lines;
                }

                next.erase( 0, pos + 1 );

                string::size_type pos = next.find_last_of( " " );
                if( pos == string::npos )
                    throw runtime_error( "unexpected file entry format in line #" + to_string( line ) );

                next_md5 = next.substr( pos + 1 );
                next.erase( pos );

                g_md5.update( ( unsigned char* )next_md5.c_str( ), next_md5.length( ) );

                pos = next.find( ' ' );
                if( pos == string::npos )
                    throw runtime_error( "unexpected file entry format in line #" + to_string( line ) );

                rwx_perms = next.substr( 0, pos );
                next.erase( 0, pos + 1 );

                string test_file( next );
                if( !paths.empty( ) )
                    test_file = paths.top( ) + "/" + next;

                if( junk )
                    next_file = destination_directory + next;
                else
                    next_file = destination_directory + test_file;

                md5.update( ( unsigned char* )rwx_perms.c_str( ), rwx_perms.length( ) );
                md5.update( ( unsigned char* )next.c_str( ), next.length( ) );

                bool matched = false;
                if( filename_filters.empty( ) )
                    matched = true;
                else
                {
                    for( size_t i = 0; i < filename_filters.size( ); i++ )
                    {
                        string wildcard( filename_filters[ i ] );

                        string::size_type pos = wildcard.find( "/" );
                        if( pos == string::npos )
                        {
                            pos = test_file.find_last_of( "/" );
                            if( pos != string::npos )
                                test_file.erase( 0, pos + 1 );
                        }
                        else if( junk )
                        {
                            if( wildcard.find( top_level_directory + "/" ) != 0 )
                                wildcard = top_level_directory + "/" + wildcard;
                        }

                        if( wildcard_match( wildcard, test_file ) )
                        {
                            matched = true;
                            matched_filters.insert( filename_filters[ i ] );

                            break;
                        }
                    }
                }

                if( !exclude_filename_filters.empty( ) )
                {
                    for( size_t i = 0; i < exclude_filename_filters.size( ); i++ )
                    {
                        string wildcard( exclude_filename_filters[ i ] );

                        string::size_type pos = wildcard.find( "/" );
                        if( pos == string::npos )
                        {
                            pos = test_file.find_last_of( "/" );
                            if( pos != string::npos )
                                test_file.erase( 0, pos + 1 );
                        }
                        else if( junk )
                        {
                            if( wildcard.find( top_level_directory + "/" ) != 0 )
                                wildcard = top_level_directory + "/" + wildcard;
                        }

                        if( wildcard_match( wildcard, test_file ) )
                        {
                            matched = false;
                            break;
                        }
                    }
                }

                if( matched && !list_only && !overwrite && !replace_all && file_exists( next_file ) )
                {
                    bool replace = false;
                    if( !is_quiet && !replace_none )
                    {
                        char ch;
                        string prompt( "File '" + next_file + "' already exists. Replace [y/n/A/N]? " );
                        while( true )
                        {
                            ch = get_char( prompt.c_str( ) );

                            if( ch == 'A' )
                                replace_all = true;
                            else if( ch == 'N' )
                                replace_none = true;

                            if( ch == 'y' || ch == 'A' )
                                replace = true;

                            if( replace || replace_all || replace_none || ch == 'n' )
                            {
                                cout << ch << '\n';
                                break;
                            }
                        }
                    }

                    if( !replace )
                    {
                        matched = false;

                        if( is_quieter )
                            cerr << "*** error: file '" << next_file << "' already exists ***" << endl;
                    }
                }

                count = 0;

                if( matched )
                    create_all_directories( create_directories,
                                            directory_perms, destination_directory, list_only, is_quieter );

                if( !matched )
                    ap_ofstream.reset( );
                else if( list_only )
                {
                    ap_ofstream.reset( );
                    cout << next_file << endl;
                }
                else
                {
                    if( !is_quieter )
                        cout << "extracting \"" << next_file << "\"";

                    ap_ofstream = auto_ptr< ofstream >( new ofstream( next_file.c_str( ), ios::out | ios::binary ) );
                    if( !*ap_ofstream.get( ) )
                        throw runtime_error( "unable to open file '" + next_file + "' for output" );
                }
            }
            else if( type == c_type_directory )
            {
                next.erase( 0, 2 );
                pos = next.find( ' ' );
                if( pos == string::npos )
                    throw runtime_error( "unexpected format in line #" + to_string( line ) );

                string::size_type cpos = next.find_last_of( ' ' );
                if( cpos == pos || cpos == string::npos )
                    throw runtime_error( "unexpected format in line #" + to_string( line ) );

                string check( next.substr( cpos + 1 ) );
                next.erase( cpos );

                int next_level( atoi( next.substr( 0, pos ).c_str( ) ) );
                if( next_level > level )
                {
                    if( next_level != level + 1 )
                        throw runtime_error( "expecting level " + to_string( level + 1 )
                                             + " but found " + to_string( next_level ) + " in line #" + to_string( line ) );

                    level = next_level;
                }
                else
                {
                    if( next_level < 0 )
                        throw runtime_error( "invalid level " + to_string( next_level ) + " found in line #" + to_string( line ) );

                    if( !include || create_directories.size( ) > 1 )
                    {
                        size_t test_level( next_level );
                        if( next_level > 0 && !include )
                            --test_level;

                        while( create_directories.size( ) > test_level )
                            create_directories.pop_back( );
                    }

                    level = next_level;

                    while( ( int )paths.size( ) > level )
                        paths.pop( );
                }

                next.erase( 0, pos + 1 );

                pos = next.find( ' ' );
                if( pos == string::npos )
                    throw runtime_error( "unexpected format in line #" + to_string( line ) );

                string rwx_perms( next.substr( 0, pos ) );
                next.erase( 0, pos + 1 );

                string path_name( next );

                if( top_level_directory.empty( ) )
                    top_level_directory = path_name;

                MD5 md5;
                md5.update( ( unsigned char* )rwx_perms.c_str( ), rwx_perms.length( ) );
                md5.update( ( unsigned char* )path_name.c_str( ), path_name.length( ) );
                md5.finalize( );

                auto_ptr< char > ap_digest( md5.hex_digest( ) );

                if( check != string( ap_digest.get( ) ) )
                    cerr << "*** error: directory '" << path_name << "' failed MD5 digest check ***" << endl;

                g_md5.update( ( unsigned char* )ap_digest.get( ), 32 );

                if( include || level > 0 )
                {
                    if( !include )
                    {
                        string::size_type pos = path_name.find( '/' );
                        if( pos == string::npos )
                            throw runtime_error( "unexpected path_name '" + path_name + " found in line #" + to_string( line ) );

                        path_name.erase( 0, pos + 1 );
                    }

                    if( !junk && !created.count( path_name ) )
                    {
                        created.insert( path_name );
                        create_directories.push_back( path_name );
                        directory_perms.insert( make_pair( path_name, rwx_perms ) );

                        if( !prune )
                            create_all_directories( create_directories,
                                                    directory_perms, destination_directory, list_only, is_quieter );
                    }

                    paths.push( path_name );
                }

                is_first = false;
            }
            else if( type == c_type_checksum )
            {
                g_md5.finalize( );
                auto_ptr< char > ap_digest( g_md5.hex_digest( ) );

                next.erase( 0, 2 );

                if( next != string( ap_digest.get( ) ) )
                    cerr << "*** error: bundle file failed MD5 digest check ***" << endl;

                finished = true;
            }
            else
                throw runtime_error( "unexpected entry type '" + to_string( type ) + "' found in line #" + to_string( line ) );
        }

        if( !is_quieter )
        {
            for( size_t i = 0; i < filename_filters.size( ); i++ )
            {
                if( matched_filters.count( filename_filters[ i ] ) == 0 )
                    cout << "warning: found no files matching '" << filename_filters[ i ] << "'" << endl;
            }
        }

        if( !finished )
            cerr << "*** error: final MD5 digest not found (file truncated?) ***" << endl;

#ifdef ZLIB_SUPPORT
        if( use_zlib && !gzeof( gzf ) )
            throw runtime_error( "unexpected error occurred whilst reading '" + filename + "' for input" );
#endif
        if( !use_zlib && !inpf.eof( ) )
            throw runtime_error( "unexpected error occurred whilst reading '" + filename + "' for input" );

        if( !is_quiet && !list_only )
            cout << "==> finished unbundling '" << filename << "'" << endl;
    }
    catch( exception& x )
    {
        cerr << "error: " << x.what( ) << endl;
        return 1;
    }

    return 0;
}
Ejemplo n.º 25
0
static void add_shader_parts(MD5& md5, const SCP_vector<SCP_string>& parts) {
	for (auto& part : parts) {
		md5.update(part.c_str(), (MD5::size_type) part.size());
	}
}
Ejemplo n.º 26
0
/**
 * Abstract method Implementation - See DbInterface.hpp for details
 */
void mysqlBMP::add_Peer(tbl_bgp_peer &p_entry) {
    try {
        char buf[4096]; // Misc working buffer

        /*
         * Generate peer table hash from the following fields
         *  p_entry.router_hash_id, p_entry.peer_rd, p_entry.peer_addr,
         *         p_entry.peer_bgp_id
         *
         */
        MD5 hash;

        // Generate the hash
        hash.update(p_entry.router_hash_id, HASH_SIZE);
        hash.update((unsigned char *) p_entry.peer_rd, strlen(p_entry.peer_rd));
        hash.update((unsigned char *) p_entry.peer_addr,
                strlen(p_entry.peer_addr));
        hash.update((unsigned char *) p_entry.peer_bgp_id,
                strlen(p_entry.peer_bgp_id));
        hash.finalize();

        // Save the hash
        unsigned char *hash_raw = hash.raw_digest();
        memcpy(p_entry.hash_id, hash_raw, 16);
        delete[] hash_raw;

        // Convert binary hash to string
        string p_hash_str;
        hash_toStr(p_entry.hash_id, p_hash_str);
        string r_hash_str;
        hash_toStr(p_entry.router_hash_id, r_hash_str);

        // Check if we have already processed this entry, if so update it an return
        if (peer_list.find(p_hash_str) != peer_list.end()) {
            peer_list[p_hash_str] = time(NULL);
            return;
        }

        // Insert/Update map entry
        peer_list[p_hash_str] = time(NULL);

        // Build the query
        snprintf(buf, sizeof(buf),
                "REPLACE into %s (%s) values ('%s','%s','%s',%d, '%s', '%s', %u, %d, %d, current_timestamp,1)",
                TBL_NAME_BGP_PEERS,
                "hash_id,router_hash_id, peer_rd,isIPv4,peer_addr,peer_bgp_id,peer_as,isL3VPNpeer,isPrePolicy,timestamp,state",
                p_hash_str.c_str(), r_hash_str.c_str(), p_entry.peer_rd,
                p_entry.isIPv4, p_entry.peer_addr, p_entry.peer_bgp_id,
                p_entry.peer_as, p_entry.isL3VPN, p_entry.isPrePolicy);

        SELF_DEBUG("QUERY=%s", buf);

        // Run the query to add the record
        stmt = con->createStatement();
        stmt->execute(buf);

        // Free the query statement
        delete stmt;

    } catch (sql::SQLException &e) {
        LOG_ERR("mysql error: %s, error Code = %d, state = %s",
                e.what(), e.getErrorCode(), e.getSQLState().c_str() );
    }
}