library/src/blockclass/BP-BlockClass.cc
size_t Block::insertBaseHost(char * host_href, size_t result_id)
{

	// check to ensure session exists
	if(!this->mysqlSession)
		return 0;

	// enter critical sections
	BP_BLOCK_MARIADB_ENTER_CRITICAL_SECTION;

	// href argument is required
	if(!host_href)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(0);

	// ensure base is reasonable printable
	if(!BP_StringIsReasonablePrintableString(host_href, bpstrlen(host_href), BP_FALSE, BP_FALSE))
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);

	// create hex buffer
	char * hex_buff = BP_BufferToHexString(host_href,strnlen(host_href, 1024));
	if(!hex_buff)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(0);

	// run jit href check
	BP_StringUtils * query_builder = new BP_StringUtils(NULL);

	// build the string
	query_builder->BuildString((char *) "INSERT INTO `result_hosts` (`sha1`, `hostname`, `initial_result_id`) VALUES (");

	// add buffer
	query_builder->BuildString((char *) "sha1(0x");
	query_builder->BuildString((char *) hex_buff);
	query_builder->BuildString((char *) "), ");
	query_builder->BuildString((char *) "0x");
	query_builder->BuildString((char *) hex_buff);

	// add last result id (or zero if new/unreferenced)
	char result_id_buff[64] = {0};
	sprintf((char *) &result_id_buff, ", %u", result_id);

	// append result id buffer
	query_builder->BuildString((char *) &result_id_buff);

	// close query set
	query_builder->BuildString((char *) " ) ");

	// add duplicate query constraint
	query_builder->BuildString(" ON DUPLICATE KEY UPDATE hits = hits+1;");


	// prepare the query
	if(this->mysqlPrepareQuery(query_builder->build_str) == ERR_FAILURE)
	{
		if(hex_buff)
		{
			bpfree(hex_buff);
			hex_buff = NULL;
		}


		// exit routine
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(0);

	}

	// execute the query
	if(this->mysqlExecuteStatement() == ERR_FAILURE)
	{
		if(hex_buff)
		{
			bpfree(hex_buff);
			hex_buff = NULL;
		}

		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(0);
	}

	// destroy hex buffer here
	if(hex_buff)
	{
		bpfree(hex_buff);
		hex_buff=NULL;
	}

	// store result
	this->mysqlStoreResults();
	this->mysqlDestroyStatement();

	// fetch the result
	// this->mysqlFetchResult();


	// destroy the query builder
	delete query_builder;


#if USE_MARIADB == 1


	// check for insert or update id
	my_ulonglong insert_or_update_id = mysql_insert_id(this->mysqlSession->conn);

	// if the record was updated instead of updated, the id must be looked up
	if(!insert_or_update_id)
		insert_or_update_id = this->lookupBaseHostId(host_href);

	// add catch for undefined behavior
	if(!insert_or_update_id)
	{
		printf("\n [!!] Error: Record was not inserted, updated, or exited in defined pattern.  Undefined behavior.");
		// asm("int3");
	}

	// return the new insert id (should always be nonzero)
	BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(insert_or_update_id);


#else

	// check for insert or update id
	my_ulonglong insert_or_update_id = this->mysqlSession->stmt->insert_id;

	// if the record was updated instead of updated, the id must be looked up
	if(!insert_or_update_id)
		insert_or_update_id = this->lookupBaseHostId(host_href);

	// add catch for undefined behavior
	if(!insert_or_update_id)
	{
		printf("\n [!!] Error: Record was not inserted, updated, or exited in defined pattern.  Undefined behavior.");
		// asm("int3");
	}

	// return the new insert id (should always be nonzero)
	BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(insert_or_update_id);

#endif

}
// Insert a host into the db (linked to result in base field)
Insert a host into the db (linked to result in base field)
Insert a host into the db ( linked to result in base field )
size_t Block::insertBaseHost(char * host_href, size_t result_id)
{
	if(!this->mysqlSession)
		return 0;
	BP_BLOCK_MARIADB_ENTER_CRITICAL_SECTION;
	if(!host_href)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(0);
	if(!BP_StringIsReasonablePrintableString(host_href, bpstrlen(host_href), BP_FALSE, BP_FALSE))
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	char * hex_buff = BP_BufferToHexString(host_href,strnlen(host_href, 1024));
	if(!hex_buff)
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(0);
	BP_StringUtils * query_builder = new BP_StringUtils(NULL);
	query_builder->BuildString((char *) "INSERT INTO `result_hosts` (`sha1`, `hostname`, `initial_result_id`) VALUES (");
	query_builder->BuildString((char *) "sha1(0x");
	query_builder->BuildString((char *) hex_buff);
	query_builder->BuildString((char *) "), ");
	query_builder->BuildString((char *) "0x");
	query_builder->BuildString((char *) hex_buff);
	char result_id_buff[64] = {0};
	sprintf((char *) &result_id_buff, ", %u", result_id);
	query_builder->BuildString((char *) &result_id_buff);
	query_builder->BuildString((char *) " ) ");
	query_builder->BuildString(" ON DUPLICATE KEY UPDATE hits = hits+1;");
	if(this->mysqlPrepareQuery(query_builder->build_str) == ERR_FAILURE)
	{
		if(hex_buff)
		{
			bpfree(hex_buff);
			hex_buff = NULL;
		}
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(0);
	}
	if(this->mysqlExecuteStatement() == ERR_FAILURE)
	{
		if(hex_buff)
		{
			bpfree(hex_buff);
			hex_buff = NULL;
		}
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(0);
	}
	if(hex_buff)
	{
		bpfree(hex_buff);
		hex_buff=NULL;
	}
	this->mysqlStoreResults();
	this->mysqlDestroyStatement();
	delete query_builder;
#if USE_MARIADB == 1
	my_ulonglong insert_or_update_id = mysql_insert_id(this->mysqlSession->conn);
	if(!insert_or_update_id)
		insert_or_update_id = this->lookupBaseHostId(host_href);
	if(!insert_or_update_id)
	{
		printf("\n [!!] Error: Record was not inserted, updated, or exited in defined pattern.  Undefined behavior.");
	}
	BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(insert_or_update_id);
#else
	my_ulonglong insert_or_update_id = this->mysqlSession->stmt->insert_id;
	if(!insert_or_update_id)
		insert_or_update_id = this->lookupBaseHostId(host_href);
	if(!insert_or_update_id)
	{
		printf("\n [!!] Error: Record was not inserted, updated, or exited in defined pattern.  Undefined behavior.");
	}
	BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(insert_or_update_id);
#endif
}
size_t Block :: insertBaseHost ( char * host_href , size_t result_id ) { if ( ! this -> mysqlSession ) return 0 ; BP_BLOCK_MARIADB_ENTER_CRITICAL_SECTION ; if ( ! host_href ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( 0 ) ; if ( ! BP_StringIsReasonablePrintableString ( host_href , bpstrlen ( host_href ) , BP_FALSE , BP_FALSE ) ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; char * hex_buff = BP_BufferToHexString ( host_href , strnlen ( host_href , 1024 ) ) ; if ( ! hex_buff ) BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( 0 ) ; BP_StringUtils * query_builder = new BP_StringUtils ( NULL ) ; query_builder -> BuildString ( ( char * ) " " ) ; query_builder -> BuildString ( ( char * ) " " ) ; query_builder -> BuildString ( ( char * ) hex_buff ) ; query_builder -> BuildString ( ( char * ) " " ) ; query_builder -> BuildString ( ( char * ) " " ) ; query_builder -> BuildString ( ( char * ) hex_buff ) ; char result_id_buff [ 64 ] = { 0 } ; sprintf ( ( char * ) & result_id_buff , " " , result_id ) ; query_builder -> BuildString ( ( char * ) & result_id_buff ) ; query_builder -> BuildString ( ( char * ) " " ) ; query_builder -> BuildString ( " " ) ; if ( this -> mysqlPrepareQuery ( query_builder -> build_str ) == ERR_FAILURE ) { if ( hex_buff ) { bpfree ( hex_buff ) ; hex_buff = NULL ; } BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( 0 ) ; } if ( this -> mysqlExecuteStatement ( ) == ERR_FAILURE ) { if ( hex_buff ) { bpfree ( hex_buff ) ; hex_buff = NULL ; } BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( 0 ) ; } if ( hex_buff ) { bpfree ( hex_buff ) ; hex_buff = NULL ; } this -> mysqlStoreResults ( ) ; this -> mysqlDestroyStatement ( ) ; delete query_builder ; #if USE_MARIADB == 1 my_ulonglong insert_or_update_id = mysql_insert_id ( this -> mysqlSession -> conn ) ; if ( ! insert_or_update_id ) insert_or_update_id = this -> lookupBaseHostId ( host_href ) ; if ( ! insert_or_update_id ) { printf ( " \n " ) ; } BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( insert_or_update_id ) ; #else my_ulonglong insert_or_update_id = this -> mysqlSession -> stmt -> insert_id ; if ( ! insert_or_update_id ) insert_or_update_id = this -> lookupBaseHostId ( host_href ) ; if ( ! insert_or_update_id ) { printf ( " \n " ) ; } BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( insert_or_update_id ) ; #endif }
Insert a host into the db (linked to result in base field)
Insert a host into the db ( linked to result in base field )
// check to ensure session exists // enter critical sections // href argument is required // ensure base is reasonable printable // create hex buffer // run jit href check // build the string // add buffer // add last result id (or zero if new/unreferenced) // append result id buffer // close query set // add duplicate query constraint // prepare the query // exit routine // execute the query // destroy hex buffer here // store result // fetch the result // this->mysqlFetchResult(); // destroy the query builder // check for insert or update id // if the record was updated instead of updated, the id must be looked up // add catch for undefined behavior // asm("int3"); // return the new insert id (should always be nonzero) // check for insert or update id // if the record was updated instead of updated, the id must be looked up // add catch for undefined behavior // asm("int3"); // return the new insert id (should always be nonzero)