@ -815,7 +815,7 @@ error:
*/
static int DetectAddressParse2 ( const DetectEngineCtx * de_ctx ,
DetectAddressHead * gh , DetectAddressHead * ghn ,
char * s , int negate , ResolvedVariablesList * var_list )
const char * s , int negate , ResolvedVariablesList * var_list )
{
size_t x = 0 ;
size_t u = 0 ;
@ -1023,6 +1023,8 @@ static int DetectAddressParse2(const DetectEngineCtx *de_ctx,
if ( DetectAddressParse2 ( de_ctx , gh , ghn , temp_rule_var_address ,
( negate + n_set ) % 2 , var_list ) < 0 ) {
SCLogDebug ( " DetectAddressParse2 hates us " ) ;
if ( temp_rule_var_address ! = rule_var_address )
SCFree ( temp_rule_var_address ) ;
goto error ;
}
d_set = 0 ;
@ -1345,6 +1347,98 @@ int DetectAddressTestConfVars(void)
return - 1 ;
}
# include "util-hash-lookup3.h"
typedef struct DetectAddressMap_ {
char * string ;
DetectAddressHead * address ;
} DetectAddressMap ;
static uint32_t DetectAddressMapHashFunc ( HashListTable * ht , void * data , uint16_t datalen )
{
const DetectAddressMap * map = ( DetectAddressMap * ) data ;
uint32_t hash = 0 ;
hash = hashlittle_safe ( map - > string , strlen ( map - > string ) , 0 ) ;
hash % = ht - > array_size ;
return hash ;
}
static char DetectAddressMapCompareFunc ( void * data1 , uint16_t len1 , void * data2 ,
uint16_t len2 )
{
DetectAddressMap * map1 = ( DetectAddressMap * ) data1 ;
DetectAddressMap * map2 = ( DetectAddressMap * ) data2 ;
int r = ( strcmp ( map1 - > string , map2 - > string ) = = 0 ) ;
return r ;
}
static void DetectAddressMapFreeFunc ( void * data )
{
DetectAddressMap * map = ( DetectAddressMap * ) data ;
if ( map ! = NULL ) {
DetectAddressHeadFree ( map - > address ) ;
SCFree ( map - > string ) ;
}
SCFree ( map ) ;
}
int DetectAddressMapInit ( DetectEngineCtx * de_ctx )
{
de_ctx - > address_table = HashListTableInit ( 4096 , DetectAddressMapHashFunc ,
DetectAddressMapCompareFunc ,
DetectAddressMapFreeFunc ) ;
if ( de_ctx - > address_table = = NULL )
return - 1 ;
return 0 ;
}
void DetectAddressMapFree ( DetectEngineCtx * de_ctx )
{
if ( de_ctx - > address_table = = NULL )
return ;
HashListTableFree ( de_ctx - > address_table ) ;
de_ctx - > address_table = NULL ;
return ;
}
int DetectAddressMapAdd ( DetectEngineCtx * de_ctx , const char * string ,
DetectAddressHead * address )
{
DetectAddressMap * map = SCCalloc ( 1 , sizeof ( * map ) ) ;
if ( map = = NULL )
return - 1 ;
map - > string = SCStrdup ( string ) ;
if ( map - > string = = NULL ) {
SCFree ( map ) ;
return - 1 ;
}
map - > address = address ;
BUG_ON ( HashListTableAdd ( de_ctx - > address_table , ( void * ) map , 0 ) ! = 0 ) ;
return 0 ;
}
const DetectAddressHead * DetectAddressMapLookup ( DetectEngineCtx * de_ctx ,
const char * string )
{
DetectAddressMap map = { ( char * ) string , NULL } ;
const DetectAddressMap * res = HashListTableLookup ( de_ctx - > address_table ,
& map , 0 ) ;
if ( res = = NULL )
return NULL ;
else {
return ( const DetectAddressHead * ) res - > address ;
}
}
/**
* \ brief Parses an address group sent as a character string and updates the
* DetectAddressHead sent as the argument with the relevant address
@ -1358,7 +1452,7 @@ int DetectAddressTestConfVars(void)
* \ retval - 1 On failure .
*/
int DetectAddressParse ( const DetectEngineCtx * de_ctx ,
DetectAddressHead * gh , char * str )
DetectAddressHead * gh , const char * str )
{
int r ;
DetectAddressHead * ghn = NULL ;
@ -1401,6 +1495,31 @@ error:
return - 1 ;
}
const DetectAddressHead * DetectParseAddress ( DetectEngineCtx * de_ctx ,
const char * string )
{
const DetectAddressHead * h = DetectAddressMapLookup ( de_ctx , string ) ;
if ( h ! = NULL ) {
SCLogDebug ( " found: %s :: %p " , string , h ) ;
return h ;
}
SCLogDebug ( " %s not found " , string ) ;
DetectAddressHead * head = DetectAddressHeadInit ( ) ;
if ( head = = NULL )
return NULL ;
if ( DetectAddressParse ( de_ctx , head , string ) = = - 1 )
{
DetectAddressHeadFree ( head ) ;
return NULL ;
}
DetectAddressMapAdd ( ( DetectEngineCtx * ) de_ctx , string , head ) ;
return head ;
}
/**
* \ brief Returns a new instance of DetectAddressHead .
*