db.c
changeset 15 2706fc514dea
parent 14 51eb85ae4de4
child 16 e6a640ad2cc2
equal deleted inserted replaced
14:51eb85ae4de4 15:2706fc514dea
   138 	debug( 0, LOC, "Added %d rules to %s.\n", parsed, v.dbname );
   138 	debug( 0, LOC, "Added %d rules to %s.\n", parsed, v.dbname );
   139 	return( 0 );
   139 	return( 0 );
   140 }
   140 }
   141 
   141 
   142 
   142 
   143 /* Fast single record lookup.
   143 /* 
   144  * Returns a pointer to the found value or NULL if there is no match.
   144  * Search the CDB for all occurances of the given +key+, checking
   145  *
   145  * each one against the +p_request+.  First match wins and is
   146  * The returned pointer should be freed after use.
   146  * returned.  NULL on no match.
   147  *
   147  *
   148  */
   148  */
   149 char *
   149 parsed *
   150 find_record( char *key )
   150 find_rule( char *key, parsed *p_request )
   151 {
   151 {
   152 	if ( key == NULL ) return( NULL );
   152 	if ( key == NULL ) return( NULL );
   153 
       
   154 	char *val = NULL;
       
   155 	cdbi_t vlen;
       
   156 
       
   157 	if ( cdb_seek( v.db_fd, key, (int)strlen(key), &vlen) > 0 ) {
       
   158 
       
   159 		if ( (val = malloc( vlen + 1 )) == NULL ) {
       
   160 			debug( 5, LOC, "Unable to allocate memory for value storage: %s\n", strerror(errno) );
       
   161 			return( NULL );
       
   162 		}
       
   163 
       
   164 		cdb_bread( v.db_fd, val, vlen );
       
   165 		val[vlen] = '\0';
       
   166 		debug( 4, LOC, "Match for key '%s': %s\n", key, val );
       
   167 	}
       
   168 
       
   169 	return val;
       
   170 }
       
   171 
       
   172 
       
   173 /* 
       
   174  * Search the CDB for all occurences of the given +key+,
       
   175  * populating the +results+ array with pointers to parsed rule structs.
       
   176  *
       
   177  * Returns the number of successful matches.  reset_results()
       
   178  * should be called after the result set is examined.
       
   179  *
       
   180  */
       
   181 unsigned int
       
   182 find_records( char *key, parsed **results )
       
   183 {
       
   184 	if ( key == NULL ) return( 0 );
       
   185 
   153 
   186 	struct cdb cdb;
   154 	struct cdb cdb;
   187 	struct cdb_find cdbf; /* structure to hold current find position */
   155 	struct cdb_find cdbf; /* structure to hold current find position */
   188 
   156 
   189 	unsigned int match = 0;
   157 	parsed *rule = NULL;
   190 	parsed *result     = NULL;
   158 	char *val    = NULL;
   191 	char *val          = NULL;
       
   192 	unsigned int vlen, vpos;
   159 	unsigned int vlen, vpos;
   193 
   160 
   194 	/* initialize search structs */
   161 	/* initialize search structs */
   195 	if ( db_attach() == -1 ) return( 0 );
   162 	if ( db_attach() == -1 ) return( NULL );
   196 	cdb_init( &cdb, v.db_fd );
   163 	cdb_init( &cdb, v.db_fd );
   197 	cdb_findinit( &cdbf, &cdb, key, (int)strlen(key) );
   164 	cdb_findinit( &cdbf, &cdb, key, (int)strlen(key) );
   198 
   165 
   199 	while ( cdb_findnext( &cdbf ) > 0 && match < DB_RESULTS_MAX ) {
   166 	while ( cdb_findnext( &cdbf ) > 0 ) {
   200 		vpos = cdb_datapos( &cdb );
   167 		vpos = cdb_datapos( &cdb );
   201 		vlen = cdb_datalen( &cdb );
   168 		vlen = cdb_datalen( &cdb );
   202 
   169 
   203 		/* pull the value from the db */
   170 		/* pull the value from the db */
   204 		if ( (val = calloc( vlen, sizeof(char) )) == NULL ) {
   171 		if ( (val = calloc( vlen, sizeof(char) )) == NULL ) {
   205 			debug( 5, LOC, "Unable to allocate memory for DB value storage: %s\n",
   172 			debug( 5, LOC, "Unable to allocate memory for DB value storage: %s\n",
   206 					strerror(errno) );
   173 					strerror(errno) );
   207 			return( 0 );
   174 			cdb_free( &cdb );
       
   175 			return( NULL );
   208 		}
   176 		}
   209 		cdb_read( &cdb, val, vlen, vpos );
   177 		cdb_read( &cdb, val, vlen, vpos );
   210 
   178 
   211 		/* if it parses properly, add it to the result set. */
   179 		/* check it against the request */
   212 		result = parse_rule( val );
   180 		debug( 4, LOC, "DB match for key '%s': %s\n", key, val );
   213 		if ( result != NULL ) {
   181 		rule = parse_rule( val );
   214 			results[match] = result;
   182 		free( val );
   215 			debug( 4, LOC, "DB match %d for key '%s': %s\n", match+1, key, val );
   183 		if ( rule != NULL ) {
       
   184 			if ( check_rule( rule, p_request ) == 0 ) {
       
   185 				finish_parsed( rule );
       
   186 			}
       
   187 			else {
       
   188 				break;
       
   189 			}
   216 		}
   190 		}
   217 
       
   218 		match++;
       
   219 		free( val );
       
   220 	}
   191 	}
   221 
   192 
   222 	cdb_free( &cdb );
   193 	cdb_free( &cdb );
   223 	return match;
   194 	return( rule );
   224 }
   195 }
   225 
   196