void ndpi_free_lru_cache(struct ndpi_LruCache *cache) { int i; if(unlikely(traceLRU)) printf("%s()", __FUNCTION__); for(i=0; i<(int)cache->hash_size; i++) { struct ndpi_LruCacheEntry *head = cache->hash[i]; while(head != NULL) { struct ndpi_LruCacheEntry *next = head->next; free_lru_cache_entry(cache, head); ndpi_free(head); #ifdef FULL_STATS cache->mem_size -= sizeof(struct ndpi_LruCacheEntry); #endif head = next; } } ndpi_free(cache->hash); #ifdef FULL_STATS cache->mem_size -= cache->hash_size*sizeof(struct ndpi_LruCacheEntry*); #endif ndpi_free(cache->current_hash_size); #ifdef FULL_STATS cache->mem_size -= cache->hash_size*sizeof(u_int32_t); #endif }
static void free_lru_cache_entry(struct ndpi_LruCache *cache, struct ndpi_LruCacheEntry *entry) { if(entry->numeric_node) { ; /* Nothing to do */ } else { #ifdef FULL_STATS cache->mem_size -= strlen(entry->u.str.key); cache->mem_size -= strlen(entry->u.str.value); #endif ndpi_free(entry->u.str.key); ndpi_free(entry->u.str.value); } }
static void trim_subhash(struct ndpi_LruCache *cache, u_int32_t hash_id) { if(unlikely(traceLRU)) printf("%s()", __FUNCTION__); if(cache->current_hash_size[hash_id] >= cache->max_cache_node_len) { struct ndpi_LruCacheEntry *head = cache->hash[hash_id], *prev = NULL; /* Find the last entry and remove it */ while(head->next != NULL) { prev = head; head = head->next; } if(prev) { prev->next = head->next; free_lru_cache_entry(cache, head); ndpi_free(head); #ifdef FULL_STATS cache->mem_size -= sizeof(struct ndpi_LruCacheEntry); #endif cache->current_hash_size[hash_id]--; } else printf("ERROR: Internal error in %s()", __FUNCTION__); } }
static void ndpi_http_subprotocol_conf(struct ndpi_detection_module_struct *ndpi_struct, char *attr, char *value, int protocol_id) { #ifdef AHOCORASICK AC_PATTERN_t ac_pattern; #else ndpi_protocol_match *tmp_host_match; #endif /* e.g attr = "host" value = ".facebook.com" protocol_id = NDPI_PROTOCOL_FACEBOOK */ #ifdef AHOCORASICK if (strcmp(attr, "finalize") == 0) { /* trick to finalize automa */ if (ac_automa != NULL) ac_automata_finalize(ac_automa); return; } #endif if (strcmp(attr, "host") != 0) { #ifdef DEBUG printf("[NTOP] attribute %s not supported\n", attr); #endif return; } #ifdef AHOCORASICK if (ac_automa == NULL) ac_automa = ac_automata_init(ac_match_handler); /* call ac_automata_release(ac_automa); to free ac_automa */ ac_pattern.astring = value; ac_pattern.rep.number = protocol_id; ac_pattern.length = strlen(tmp_patt.astring); ac_automata_add(ac_automa, &ac_pattern); #else /* realloc */ tmp_host_match = ndpi_malloc(sizeof(ndpi_protocol_match) * (host_match_num_items + 1)); if (tmp_host_match == NULL) { return; } if (host_match != NULL) { memcpy(tmp_host_match, host_match, sizeof(ndpi_protocol_match) * host_match_num_items); ndpi_free(host_match); } host_match = tmp_host_match; host_match[host_match_num_items].string_to_match = ndpi_strdup(value); if (host_match[host_match_num_items].string_to_match == NULL) { return; } host_match[host_match_num_items].protocol_id = protocol_id; host_match_num_items++; #endif #ifdef DEBUG printf("[NTOP] new http subprotocol: %s = %s -> %d\n", attr, value, protocol_id); #endif }
char*ndpi_find_lru_cache_str(struct ndpi_LruCache *cache, char *key) { if(cache->hash_size == 0) return(0); else { u_int32_t hash_val = lru_hash_string(key); u_int32_t hash_id = hash_val % cache->hash_size; struct ndpi_LruCacheEntry *head, *prev = NULL; char *ret_val = NULL; time_t now = get_now(); if(unlikely(traceLRU)) printf("%s(%s)", __FUNCTION__, key); // validate_unit_len(cache, hash_id); cache->num_cache_find++; head = cache->hash[hash_id]; while(head != NULL) { if(strcmp(head->u.str.key, key) == 0) { if(head->u.str.expire_time < now) { /* The node has expired */ if(prev == NULL) cache->hash[hash_id] = head->next; else prev->next = head->next; free_lru_cache_entry(cache, head); ndpi_free(head); #ifdef FULL_STATS cache->mem_size -= sizeof(struct ndpi_LruCacheEntry); #endif ret_val = NULL; cache->current_hash_size[hash_id]--; } else ret_val = head->u.str.value; break; } else { prev = head; head = head->next; } } if(ret_val == NULL) cache->num_cache_misses++; // validate_unit_len(cache, hash_id); return(ret_val); } }
static void ndpi_flow_freer(void *node) { struct ndpi_flow *flow = (struct ndpi_flow*)node; free_ndpi_flow(flow); ndpi_free(flow); }
static void free_ndpi_flow(struct ndpi_flow *flow) { if(flow->ndpi_flow) { ndpi_free(flow->ndpi_flow); flow->ndpi_flow = NULL; } if(flow->src_id) { ndpi_free(flow->src_id); flow->src_id = NULL; } if(flow->dst_id) { ndpi_free(flow->dst_id); flow->dst_id = NULL; } }
int ndpi_add_to_lru_cache_str_timeout(struct ndpi_LruCache *cache, char *key, char *value, u_int32_t timeout) { if(cache->hash_size == 0) return(0); else { u_int32_t hash_val = lru_hash_string(key); u_int32_t hash_id = hash_val % cache->hash_size; struct ndpi_LruCacheEntry *node; u_int8_t node_already_existing = 0; int rc = 0; if(unlikely(traceLRU)) printf("%s(key=%s, value=%s)", __FUNCTION__, key, value); // validate_unit_len(cache, hash_id); cache->num_cache_add++; /* [1] Add to hash */ if(cache->hash[hash_id] == NULL) { if((node = lru_allocCacheStringNode(cache, key, value, timeout)) == NULL) { rc = -1; goto ret_add_to_lru_cache; } cache->hash[hash_id] = node; cache->current_hash_size[hash_id]++; } else { /* Check if the element exists */ struct ndpi_LruCacheEntry *head = cache->hash[hash_id]; while(head != NULL) { if(strcmp(head->u.str.key, key) == 0) { /* Duplicated key found */ node = head; if(node->u.str.value) { #ifdef FULL_STATS cache->mem_size -= strlen(node->u.str.value); #endif ndpi_free(node->u.str.value); } node->u.str.value = ndpi_strdup(value); /* Overwrite old value */ #ifdef FULL_STATS cache->mem_size += strlen(value); #endif node->u.str.expire_time = (timeout == 0) ? 0 : (compute_timeout(timeout) + get_now()); node_already_existing = 1; break; } else head = head->next; } if(!node_already_existing) { if((node = lru_allocCacheStringNode(cache, key, value, timeout)) == NULL) { rc = -2; goto ret_add_to_lru_cache; } node->next = cache->hash[hash_id]; cache->hash[hash_id] = node; cache->current_hash_size[hash_id]++; } } trim_subhash(cache, hash_id); // validate_unit_len(cache, hash_id); ret_add_to_lru_cache: return(rc); } }