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 ); }
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); } }
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; }
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; }
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; };
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; }
/** * 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; }
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; };
// 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; }
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(); }
/** * 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; }
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; }
/** * 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() ); } }
/** * 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() ); } }
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; }
/** * Parses the BGP attributes 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 [out] parsed_update Reference to parsed_update; will be updated with all parsed data */ void parseBgpLib::parseBgpAttr(u_char *data, uint16_t len, parsed_update &update) { /* * Per RFC4271 Section 4.3, flag indicates if the length is 1 or 2 octets */ u_char attr_flags; u_char attr_type; uint16_t attr_len; if (len == 0) return; else if (len < 3) { LOG_WARN("%sCannot parse the attributes due to the data being too short, error in update message. len=%d", debug_prepend_string.c_str(), len); return; } // Generate the hash MD5 hash; /* * Iterate through all attributes and parse them */ for (int read_size = 0; read_size < len; read_size += 2) { attr_flags = *data++; attr_type = *data++; // Check if the length field is 1 or two bytes if (ATTR_FLAG_EXTENDED(attr_flags)) { SELF_DEBUG("%sExtended length path attribute bit set for an entry", debug_prepend_string.c_str()); memcpy(&attr_len, data, 2); data += 2; read_size += 2; parse_bgp_lib::SWAP_BYTES(&attr_len); } else attr_len = *data++; read_size++; // Get the attribute data, if we have any; making sure to not overrun buffer if (attr_len > 0 and (read_size + attr_len) <= len) { // Data pointer is currently at the data position of the attribute /* * Parse data based on attribute type */ parseAttrData(attr_type, attr_len, data, update, hash); data += attr_len; read_size += attr_len; SELF_DEBUG("%sParsed attr Type=%d, size=%hu", debug_prepend_string.c_str(), attr_type, attr_len); } else if (attr_len) { LOG_NOTICE("%sAttribute data len of %hu is larger than available data in update message of %hu", debug_prepend_string.c_str(), attr_len, (len - read_size)); return; } } //Now save the generate hash hash.finalize(); // Save the hash unsigned char *hash_raw = hash.raw_digest(); update.attrs[LIB_ATTR_BASE_ATTR_HASH].name = parse_bgp_lib::parse_bgp_lib_attr_names[LIB_ATTR_BASE_ATTR_HASH]; update.attrs[LIB_ATTR_BASE_ATTR_HASH].value.push_back(parse_bgp_lib::hash_toStr(hash_raw)); delete[] hash_raw; }
/** * 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); } } }
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; } }
/** * 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); } }
/** * 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() ); } }