From 1f077ebff2cfb2d12cf2e8437ffa9ccb4d8ef96b Mon Sep 17 00:00:00 2001 From: ben51degrees Date: Wed, 6 Jul 2016 12:07:21 +0100 Subject: [PATCH] BUILD/MAJOR:updated 51d Trie implementation to incorperate latest update to 51Degrees.c Trie now uses a dataset structure just like Pattern, so this has been defined in includes/types/global.h for both Pattern and Trie where it was just Pattern. In src/51d.c all functions used by the Trie implementation which need a dataset as an argument now use the global dataset. The fiftyoneDegreesDestroy method has now been replaced with fiftyoneDegreesDataSetFree which is common to Pattern and Trie. In addition, two extra dataset init status' have been added to the switch statement in init_51degrees. --- include/types/global.h | 2 +- src/51d.c | 32 +++++++++++++++++--------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/include/types/global.h b/include/types/global.h index 61f0391676..71eacf2960 100644 --- a/include/types/global.h +++ b/include/types/global.h @@ -197,8 +197,8 @@ struct global { char *data_file_path; int header_count; /* number of HTTP headers related to device detection. */ struct chunk *header_names; /* array of HTTP header names. */ + fiftyoneDegreesDataSet data_set; /* data set used with the pattern and trie detection methods. */ #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED - fiftyoneDegreesDataSet data_set; /* data set used with the pattern detection method. */ fiftyoneDegreesWorksetPool *pool; /* pool of worksets to avoid creating a new one for each request. */ #endif #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED diff --git a/src/51d.c b/src/51d.c index 3aa5b860b2..74b7928e72 100644 --- a/src/51d.c +++ b/src/51d.c @@ -235,7 +235,7 @@ static void _51d_set_device_offsets(struct sample *smp) (global._51degrees.header_names + index)->len, msg->chn->buf->p, idx, &ctx) == 1) { (offsets->firstOffset + offsets->size)->httpHeaderOffset = *(global._51degrees.header_offsets + index); - (offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(ctx.line + ctx.val); + (offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global._51degrees.data_set, ctx.line + ctx.val); offsets->size++; } } @@ -269,8 +269,8 @@ static void _51d_process_match(const struct arg *args, struct sample *smp, fifty static void _51d_process_match(const struct arg *args, struct sample *smp) { char valuesBuffer[1024]; - const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(); - int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(); + const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(&global._51degrees.data_set); + int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(&global._51degrees.data_set); fiftyoneDegreesDeviceOffsets *deviceOffsets = &global._51degrees.device_offsets; #endif @@ -322,7 +322,7 @@ static void _51d_process_match(const struct arg *args, struct sample *smp) for (j = 0; j < requiredPropertiesCount; j++) { property_name = requiredProperties[j]; if (strcmp(property_name, args[i].data.str.str) == 0 && - fiftyoneDegreesGetValueFromOffsets(deviceOffsets, j, valuesBuffer, 1024) > 0) { + fiftyoneDegreesGetValueFromOffsets(&global._51degrees.data_set, deviceOffsets, j, valuesBuffer, 1024) > 0) { found = 1; chunk_appendf(temp, "%s", valuesBuffer); break; @@ -457,7 +457,7 @@ static int _51d_conv(const struct arg *args, struct sample *smp, void *private) _51d_process_match(args, smp, ws); #endif #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED - global._51degrees.device_offsets.firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(smp->data.u.str.str); + global._51degrees.device_offsets.firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global._51degrees.data_set, smp->data.u.str.str); global._51degrees.device_offsets.size = 1; _51d_process_match(args, smp); #endif @@ -492,14 +492,15 @@ void _51d_init_http_headers() void _51d_init_http_headers() { int index = 0; - global._51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(); + fiftyoneDegreesDataSet *ds = &global._51degrees.data_set; + global._51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(ds); global._51degrees.device_offsets.firstOffset = malloc( global._51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset)); global._51degrees.header_names = malloc(global._51degrees.header_count * sizeof(struct chunk)); global._51degrees.header_offsets = malloc(global._51degrees.header_count * sizeof(int32_t)); for (index = 0; index < global._51degrees.header_count; index++) { - global._51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(index); - global._51degrees.header_names[index].str = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(index); + global._51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(ds, index); + global._51degrees.header_names[index].str = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(ds, index); global._51degrees.header_names[index].len = strlen(global._51degrees.header_names[index].str); global._51degrees.header_names[index].size = global._51degrees.header_names[index].len; } @@ -528,12 +529,7 @@ int init_51degrees(void) _51d_property_list[i++] = name->name; } -#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED _51d_dataset_status = fiftyoneDegreesInitWithPropertyArray(global._51degrees.data_file_path, &global._51degrees.data_set, (const char**)_51d_property_list, i); -#endif -#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED - _51d_dataset_status = fiftyoneDegreesInitWithPropertyArray(global._51degrees.data_file_path, (const char**)_51d_property_list, i); -#endif temp = get_trash_chunk(); chunk_reset(temp); @@ -570,6 +566,13 @@ int init_51degrees(void) case DATA_SET_INIT_STATUS_FILE_NOT_FOUND: chunk_printf(temp, "File not found."); break; + case DATA_SET_INIT_STATUS_NULL_POINTER: + chunk_printf(temp, "Null pointer to the existing dataset or memory location."); + break; + case DATA_SET_INIT_STATUS_POINTER_OUT_OF_BOUNDS: + chunk_printf(temp, "Allocated continuous memory containing 51Degrees data file appears to be smaller than expected. Most likely" + " because the data file was not fully loaded into the allocated memory."); + break; case DATA_SET_INIT_STATUS_NOT_SET: chunk_printf(temp, "Data set not initialised."); break; @@ -600,13 +603,12 @@ void deinit_51degrees(void) free(global._51degrees.header_names); #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED fiftyoneDegreesWorksetPoolFree(global._51degrees.pool); - fiftyoneDegreesDataSetFree(&global._51degrees.data_set); #endif #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED free(global._51degrees.device_offsets.firstOffset); free(global._51degrees.header_offsets); - fiftyoneDegreesDestroy(); #endif + fiftyoneDegreesDataSetFree(&global._51degrees.data_set); free(global._51degrees.data_file_path); global._51degrees.data_file_path = NULL; list_for_each_entry_safe(_51d_prop_name, _51d_prop_nameb, &global._51degrees.property_names, list) {