db.c
changeset 17 bd746609ba46
parent 16 e6a640ad2cc2
child 18 d4ce82194b64
equal deleted inserted replaced
16:e6a640ad2cc2 17:bd746609ba46
    42 {
    42 {
    43 	/* only re-open the db at most every 10 seconds */
    43 	/* only re-open the db at most every 10 seconds */
    44 	time_t now = time( NULL );
    44 	time_t now = time( NULL );
    45 	if ( v.timer.db_lastcheck > 0 ) {
    45 	if ( v.timer.db_lastcheck > 0 ) {
    46 		if ( now - v.timer.db_lastcheck >= 10 ) {
    46 		if ( now - v.timer.db_lastcheck >= 10 ) {
       
    47 			cdb_free( &v.db );
    47 			close( v.db_fd );
    48 			close( v.db_fd );
    48 		}
    49 		}
    49 		else {
    50 		else {
    50 			return( 0 );
    51 			return( 0 );
    51 		}
    52 		}
    63 		debug( 1, LOC, "Error when attaching to database: %s\n", strerror(errno) );
    64 		debug( 1, LOC, "Error when attaching to database: %s\n", strerror(errno) );
    64 		return( -1 );
    65 		return( -1 );
    65 	}
    66 	}
    66 
    67 
    67 	v.timer.db_lastcheck = now;
    68 	v.timer.db_lastcheck = now;
       
    69 	cdb_init( &v.db, v.db_fd );
       
    70 
    68 	return( 0 );
    71 	return( 0 );
    69 }
    72 }
    70 
    73 
    71 
    74 
    72 /*
    75 /*
   149 parsed *
   152 parsed *
   150 find_rule( char *key, parsed *p_request )
   153 find_rule( char *key, parsed *p_request )
   151 {
   154 {
   152 	if ( key == NULL ) return( NULL );
   155 	if ( key == NULL ) return( NULL );
   153 
   156 
   154 	struct cdb cdb;
       
   155 	struct cdb_find cdbf; /* structure to hold current find position */
   157 	struct cdb_find cdbf; /* structure to hold current find position */
   156 
   158 
   157 	parsed *rule = NULL;
   159 	parsed *rule = NULL;
   158 	char *val    = NULL;
   160 	char *val    = NULL;
   159 	unsigned int vlen, vpos;
   161 	unsigned int vlen, vpos;
   160 
   162 
   161 	/* initialize search structs */
   163 	/* initialize search structs */
   162 	if ( db_attach() == -1 ) return( NULL );
   164 	if ( db_attach() == -1 ) return( NULL );
   163 	cdb_init( &cdb, v.db_fd );
   165 	cdb_findinit( &cdbf, &v.db, key, (int)strlen(key) );
   164 	cdb_findinit( &cdbf, &cdb, key, (int)strlen(key) );
       
   165 
   166 
   166 	while ( cdb_findnext( &cdbf ) > 0 ) {
   167 	while ( cdb_findnext( &cdbf ) > 0 ) {
   167 		vpos = cdb_datapos( &cdb );
   168 		vpos = cdb_datapos( &v.db );
   168 		vlen = cdb_datalen( &cdb );
   169 		vlen = cdb_datalen( &v.db );
   169 
   170 
   170 		/* pull the value from the db */
   171 		/* pull the value from the db */
   171 		if ( (val = calloc( vlen, sizeof(char) )) == NULL ) {
   172 		if ( (val = calloc( vlen, sizeof(char) )) == NULL ) {
   172 			debug( 5, LOC, "Unable to allocate memory for DB value storage: %s\n",
   173 			debug( 5, LOC, "Unable to allocate memory for DB value storage: %s\n",
   173 					strerror(errno) );
   174 					strerror(errno) );
   174 			cdb_free( &cdb );
       
   175 			return( NULL );
   175 			return( NULL );
   176 		}
   176 		}
   177 		cdb_read( &cdb, val, vlen, vpos );
   177 		cdb_read( &v.db, val, vlen, vpos );
   178 
   178 
   179 		/* check it against the request */
   179 		/* check it against the request */
   180 		debug( 4, LOC, "DB match for key '%s': %s\n", key, val );
   180 		debug( 4, LOC, "DB match for key '%s': %s\n", key, val );
   181 		rule = parse_rule( val );
   181 		rule = parse_rule( val );
   182 		free( val );
   182 		free( val );
   188 				break;
   188 				break;
   189 			}
   189 			}
   190 		}
   190 		}
   191 	}
   191 	}
   192 
   192 
   193 	cdb_free( &cdb );
       
   194 	return( rule );
   193 	return( rule );
   195 }
   194 }
   196 
   195