Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | |
| 3 | #include <common/cfgparse.h> |
| 4 | #include <common/chunk.h> |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 5 | #include <common/buffer.h> |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 6 | #include <proto/arg.h> |
| 7 | #include <proto/log.h> |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 8 | #include <proto/proto_http.h> |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 9 | #include <proto/sample.h> |
| 10 | #include <import/xxhash.h> |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 11 | #include <import/lru.h> |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 12 | #include <import/51d.h> |
| 13 | |
| 14 | struct _51d_property_names { |
| 15 | struct list list; |
| 16 | char *name; |
| 17 | }; |
| 18 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 19 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 20 | static struct lru64_head *_51d_lru_tree = NULL; |
| 21 | static unsigned long long _51d_lru_seed; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 22 | #endif |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 23 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 24 | static int _51d_data_file(char **args, int section_type, struct proxy *curpx, |
| 25 | struct proxy *defpx, const char *file, int line, |
| 26 | char **err) |
| 27 | { |
| 28 | if (*(args[1]) == 0) { |
| 29 | memprintf(err, |
| 30 | "'%s' expects a filepath to a 51Degrees trie or pattern data file.", |
| 31 | args[0]); |
| 32 | return -1; |
| 33 | } |
| 34 | |
| 35 | if (global._51degrees.data_file_path) |
| 36 | free(global._51degrees.data_file_path); |
| 37 | global._51degrees.data_file_path = strdup(args[1]); |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static int _51d_property_name_list(char **args, int section_type, struct proxy *curpx, |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 43 | struct proxy *defpx, const char *file, int line, |
| 44 | char **err) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 45 | { |
| 46 | int cur_arg = 1; |
| 47 | struct _51d_property_names *name; |
| 48 | |
| 49 | if (*(args[cur_arg]) == 0) { |
| 50 | memprintf(err, |
| 51 | "'%s' expects at least one 51Degrees property name.", |
| 52 | args[0]); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | while (*(args[cur_arg])) { |
| 57 | name = calloc(1, sizeof(struct _51d_property_names)); |
| 58 | name->name = strdup(args[cur_arg]); |
| 59 | LIST_ADDQ(&global._51degrees.property_names, &name->list); |
| 60 | ++cur_arg; |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int _51d_property_separator(char **args, int section_type, struct proxy *curpx, |
| 67 | struct proxy *defpx, const char *file, int line, |
| 68 | char **err) |
| 69 | { |
| 70 | if (*(args[1]) == 0) { |
| 71 | memprintf(err, |
| 72 | "'%s' expects a single character.", |
| 73 | args[0]); |
| 74 | return -1; |
| 75 | } |
| 76 | if (strlen(args[1]) > 1) { |
| 77 | memprintf(err, |
| 78 | "'%s' expects a single character, got '%s'.", |
| 79 | args[0], args[1]); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | global._51degrees.property_separator = *args[1]; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 88 | static int _51d_cache_size(char **args, int section_type, struct proxy *curpx, |
| 89 | struct proxy *defpx, const char *file, int line, |
| 90 | char **err) |
| 91 | { |
| 92 | if (*(args[1]) == 0) { |
| 93 | memprintf(err, |
| 94 | "'%s' expects a positive numeric value.", |
| 95 | args[0]); |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | global._51degrees.cache_size = atoi(args[1]); |
| 100 | if (global._51degrees.cache_size < 0) { |
| 101 | memprintf(err, |
| 102 | "'%s' expects a positive numeric value, got '%s'.", |
| 103 | args[0], args[1]); |
| 104 | return -1; |
| 105 | } |
| 106 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int _51d_fetch_check(struct arg *arg, char **err_msg) |
| 111 | { |
| 112 | if (global._51degrees.data_file_path) |
| 113 | return 1; |
| 114 | |
| 115 | memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')"); |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 119 | static int _51d_conv_check(struct arg *arg, struct sample_conv *conv, |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 120 | const char *file, int line, char **err_msg) |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 121 | { |
| 122 | if (global._51degrees.data_file_path) |
| 123 | return 1; |
| 124 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 125 | memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')"); |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 129 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 130 | /* Insert the data associated with the sample into the cache as a fresh item. |
| 131 | */ |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame^] | 132 | static void _51d_insert_cache_entry(struct sample *smp, struct lru64 *lru, void* domain) |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 133 | { |
| 134 | struct chunk *cache_entry = (struct chunk*)malloc(sizeof(struct chunk)); |
| 135 | |
| 136 | if (!cache_entry) |
| 137 | return; |
| 138 | |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 139 | cache_entry->str = malloc(smp->data.u.str.len + 1); |
| 140 | if (!cache_entry->str) |
| 141 | return; |
| 142 | |
| 143 | memcpy(cache_entry->str, smp->data.u.str.str, smp->data.u.str.len); |
| 144 | cache_entry->str[smp->data.u.str.len] = 0; |
| 145 | cache_entry->len = smp->data.u.str.len; |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame^] | 146 | lru64_commit(lru, cache_entry, domain, 0, free); |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* Retrieves the data from the cache and sets the sample data to this string. |
| 150 | */ |
| 151 | static void _51d_retrieve_cache_entry(struct sample *smp, struct lru64 *lru) |
| 152 | { |
| 153 | struct chunk *cache_entry = (struct chunk*)lru->data; |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 154 | smp->data.u.str.str = cache_entry->str; |
| 155 | smp->data.u.str.len = cache_entry->len; |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 160 | /* Sets the important HTTP headers ahead of the detection |
| 161 | */ |
| 162 | static void _51d_set_headers(struct sample *smp, fiftyoneDegreesWorkset *ws) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 163 | { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 164 | struct hdr_idx *idx; |
| 165 | struct hdr_ctx ctx; |
| 166 | const struct http_msg *msg; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 167 | int i; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 168 | |
| 169 | idx = &smp->strm->txn->hdr_idx; |
| 170 | msg = &smp->strm->txn->req; |
| 171 | |
| 172 | ws->importantHeadersCount = 0; |
| 173 | |
| 174 | for (i = 0; i < global._51degrees.header_count; i++) { |
| 175 | ctx.idx = 0; |
| 176 | if (http_find_full_header2( |
| 177 | (global._51degrees.header_names + i)->str, |
| 178 | (global._51degrees.header_names + i)->len, |
| 179 | msg->chn->buf->p, idx, &ctx) == 1) { |
| 180 | ws->importantHeaders[ws->importantHeadersCount].header = ws->dataSet->httpHeaders + i; |
| 181 | ws->importantHeaders[ws->importantHeadersCount].headerValue = ctx.line + ctx.val; |
| 182 | ws->importantHeaders[ws->importantHeadersCount].headerValueLength = ctx.vlen; |
| 183 | ws->importantHeadersCount++; |
| 184 | } |
| 185 | } |
| 186 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 187 | #endif |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 188 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 189 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 190 | static void _51d_set_device_offsets(struct sample *smp) |
| 191 | { |
| 192 | struct hdr_idx *idx; |
| 193 | struct hdr_ctx ctx; |
| 194 | const struct http_msg *msg; |
| 195 | int index; |
| 196 | fiftyoneDegreesDeviceOffsets *offsets = &global._51degrees.device_offsets; |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 197 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 198 | idx = &smp->strm->txn->hdr_idx; |
| 199 | msg = &smp->strm->txn->req; |
| 200 | offsets->size = 0; |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 201 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 202 | for (index = 0; index < global._51degrees.header_count; index++) { |
| 203 | ctx.idx = 0; |
| 204 | if (http_find_full_header2( |
| 205 | (global._51degrees.header_names + index)->str, |
| 206 | (global._51degrees.header_names + index)->len, |
| 207 | msg->chn->buf->p, idx, &ctx) == 1) { |
| 208 | (offsets->firstOffset + offsets->size)->httpHeaderOffset = *(global._51degrees.header_offsets + index); |
| 209 | (offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(ctx.line + ctx.val); |
| 210 | offsets->size++; |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 211 | } |
| 212 | } |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 213 | } |
| 214 | #endif |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 215 | |
| 216 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 217 | /* Provides a hash code for the important HTTP headers. |
| 218 | */ |
| 219 | unsigned long long _51d_req_hash(const struct arg *args, fiftyoneDegreesWorkset* ws) |
| 220 | { |
| 221 | unsigned long long seed = _51d_lru_seed ^ (long)args; |
| 222 | unsigned long long hash = 0; |
| 223 | int i; |
| 224 | for(i = 0; i < ws->importantHeadersCount; i++) { |
| 225 | hash ^= ws->importantHeaders[i].header->headerNameOffset; |
| 226 | hash ^= XXH64(ws->importantHeaders[i].headerValue, |
| 227 | ws->importantHeaders[i].headerValueLength, |
| 228 | seed); |
| 229 | } |
| 230 | return hash; |
| 231 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 232 | #endif |
| 233 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 234 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 235 | static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesWorkset* ws) |
| 236 | { |
| 237 | char *methodName; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 238 | #endif |
| 239 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 240 | static void _51d_process_match(const struct arg *args, struct sample *smp) |
| 241 | { |
| 242 | char valuesBuffer[1024]; |
| 243 | char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(); |
| 244 | int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(); |
| 245 | fiftyoneDegreesDeviceOffsets *deviceOffsets = &global._51degrees.device_offsets; |
| 246 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 247 | #endif |
| 248 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 249 | char no_data[] = "NoData"; /* response when no data could be found */ |
| 250 | struct chunk *temp = get_trash_chunk(); |
| 251 | int j, i = 0, found; |
| 252 | const char* property_name; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 253 | |
| 254 | /* Loop through property names passed to the filter and fetch them from the dataset. */ |
| 255 | while (args[i].data.str.str) { |
| 256 | /* Try to find request property in dataset. */ |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 257 | found = 0; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 258 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 259 | if (strcmp("Method", args[i].data.str.str) == 0) { |
| 260 | switch(ws->method) { |
| 261 | case EXACT: methodName = "Exact"; break; |
| 262 | case NUMERIC: methodName = "Numeric"; break; |
| 263 | case NEAREST: methodName = "Nearest"; break; |
| 264 | case CLOSEST: methodName = "Closest"; break; |
| 265 | default: |
| 266 | case NONE: methodName = "None"; break; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 267 | } |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 268 | chunk_appendf(temp, "%s", methodName); |
| 269 | found = 1; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 270 | } |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 271 | else if (strcmp("Difference", args[i].data.str.str) == 0) { |
| 272 | chunk_appendf(temp, "%d", ws->difference); |
| 273 | found = 1; |
| 274 | } |
| 275 | else if (strcmp("Rank", args[i].data.str.str) == 0) { |
| 276 | chunk_appendf(temp, "%d", fiftyoneDegreesGetSignatureRank(ws)); |
| 277 | found = 1; |
| 278 | } |
| 279 | else { |
| 280 | for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) { |
| 281 | property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]); |
| 282 | if (strcmp(property_name, args[i].data.str.str) == 0) { |
| 283 | found = 1; |
| 284 | fiftyoneDegreesSetValues(ws, j); |
| 285 | chunk_appendf(temp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values)); |
| 286 | break; |
| 287 | } |
| 288 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 289 | } |
| 290 | #endif |
| 291 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 292 | found = 0; |
| 293 | for (j = 0; j < requiredPropertiesCount; j++) { |
| 294 | property_name = requiredProperties[j]; |
| 295 | if (strcmp(property_name, args[i].data.str.str) == 0 && |
| 296 | fiftyoneDegreesGetValueFromOffsets(deviceOffsets, j, valuesBuffer, 1024) > 0) { |
| 297 | found = 1; |
| 298 | chunk_appendf(temp, "%s", valuesBuffer); |
| 299 | break; |
| 300 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 301 | } |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 302 | #endif |
| 303 | if (!found) { |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 304 | chunk_appendf(temp, "%s", no_data); |
| 305 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 306 | /* Add separator. */ |
| 307 | chunk_appendf(temp, "%c", global._51degrees.property_separator); |
| 308 | ++i; |
| 309 | } |
| 310 | |
| 311 | if (temp->len) { |
| 312 | --temp->len; |
| 313 | temp->str[temp->len] = '\0'; |
| 314 | } |
| 315 | |
Thierry FOURNIER | 2046c46 | 2015-08-20 13:42:12 +0200 | [diff] [blame] | 316 | smp->data.u.str.str = temp->str; |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 317 | smp->data.u.str.len = temp->len; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 321 | { |
| 322 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 323 | fiftyoneDegreesWorkset* ws; /* workset for detection */ |
| 324 | struct lru64 *lru = NULL; |
| 325 | #endif |
| 326 | |
| 327 | /* Needed to ensure that the HTTP message has been fully recieved when |
| 328 | * used with TCP operation. Not required for HTTP operation. |
| 329 | * Data type has to be reset to ensure the string output is processed |
| 330 | * correctly. |
| 331 | */ |
| 332 | CHECK_HTTP_MESSAGE_FIRST(); |
| 333 | smp->data.type = SMP_T_STR; |
| 334 | |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame^] | 335 | /* Flags the sample to show it uses constant memory*/ |
| 336 | smp->flags |= SMP_F_CONST; |
| 337 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 338 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 339 | |
| 340 | /* Get only the headers needed for device detection so they can be used |
| 341 | * with the cache to return previous results. Pattern is slower than |
| 342 | * Trie so caching will help improve performance. |
| 343 | */ |
| 344 | |
| 345 | /* Get a workset from the pool which will later contain detection results. */ |
| 346 | ws = fiftyoneDegreesWorksetPoolGet(global._51degrees.pool); |
| 347 | if (!ws) |
| 348 | return 0; |
| 349 | |
| 350 | /* Set the important HTTP headers for this request in the workset. */ |
| 351 | _51d_set_headers(smp, ws); |
| 352 | |
| 353 | /* Check the cache to see if there's results for these headers already. */ |
| 354 | if (_51d_lru_tree) { |
| 355 | lru = lru64_get(_51d_req_hash(args, ws), |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame^] | 356 | _51d_lru_tree, (void*)args, 0); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 357 | if (lru && lru->domain) { |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 358 | _51d_retrieve_cache_entry(smp, lru); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 359 | return 1; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | fiftyoneDegreesMatchForHttpHeaders(ws); |
| 364 | |
| 365 | _51d_process_match(args, smp, ws); |
| 366 | |
| 367 | #endif |
| 368 | |
| 369 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 370 | |
| 371 | /* Trie is very fast so all the headers can be passed in and the result |
| 372 | * returned faster than the hashing algorithm process. |
| 373 | */ |
| 374 | _51d_set_device_offsets(smp); |
| 375 | _51d_process_match(args, smp); |
| 376 | |
| 377 | #endif |
| 378 | |
| 379 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 380 | fiftyoneDegreesWorksetPoolRelease(global._51degrees.pool, ws); |
| 381 | if (lru) { |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame^] | 382 | _51d_insert_cache_entry(smp, lru, (void*)args); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 383 | } |
| 384 | #endif |
| 385 | |
| 386 | return 1; |
| 387 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 388 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 389 | static int _51d_conv(const struct arg *args, struct sample *smp, void *private) |
| 390 | { |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 391 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 392 | fiftyoneDegreesWorkset* ws; /* workset for detection */ |
| 393 | struct lru64 *lru = NULL; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 394 | #endif |
| 395 | |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame^] | 396 | /* Flags the sample to show it uses constant memory*/ |
| 397 | smp->flags |= SMP_F_CONST; |
| 398 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 399 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 400 | |
| 401 | /* Look in the list. */ |
| 402 | if (_51d_lru_tree) { |
| 403 | unsigned long long seed = _51d_lru_seed ^ (long)args; |
| 404 | |
| 405 | lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed), |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame^] | 406 | _51d_lru_tree, (void*)args, 0); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 407 | if (lru && lru->domain) { |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 408 | _51d_retrieve_cache_entry(smp, lru); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 409 | return 1; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /* Create workset. This will later contain detection results. */ |
| 414 | ws = fiftyoneDegreesWorksetPoolGet(global._51degrees.pool); |
| 415 | if (!ws) |
| 416 | return 0; |
| 417 | #endif |
| 418 | |
| 419 | /* Duplicate the data and remove the "const" flag before device detection. */ |
| 420 | if (!smp_dup(smp)) |
| 421 | return 0; |
| 422 | |
| 423 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 424 | |
| 425 | /* Perform detection. */ |
| 426 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 427 | fiftyoneDegreesMatch(ws, smp->data.u.str.str); |
| 428 | _51d_process_match(args, smp, ws); |
| 429 | #endif |
| 430 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 431 | global._51degrees.device_offsets.firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(smp->data.u.str.str); |
| 432 | global._51degrees.device_offsets.size = 1; |
| 433 | _51d_process_match(args, smp); |
| 434 | #endif |
| 435 | |
| 436 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 437 | fiftyoneDegreesWorksetPoolRelease(global._51degrees.pool, ws); |
Dragan Dosen | 96a0be7 | 2015-07-07 16:10:43 +0200 | [diff] [blame] | 438 | if (lru) { |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame^] | 439 | _51d_insert_cache_entry(smp, lru, (void*)args); |
Dragan Dosen | 96a0be7 | 2015-07-07 16:10:43 +0200 | [diff] [blame] | 440 | } |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 441 | #endif |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 442 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 443 | return 1; |
| 444 | } |
| 445 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 446 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 447 | void _51d_init_http_headers() |
| 448 | { |
| 449 | int index = 0; |
| 450 | const fiftyoneDegreesAsciiString *headerName; |
| 451 | fiftyoneDegreesDataSet *ds = &global._51degrees.data_set; |
| 452 | global._51degrees.header_count = ds->httpHeadersCount; |
| 453 | global._51degrees.header_names = (struct chunk*)malloc(global._51degrees.header_count * sizeof(struct chunk)); |
| 454 | for (index = 0; index < global._51degrees.header_count; index++) { |
| 455 | headerName = fiftyoneDegreesGetString(ds, ds->httpHeaders[index].headerNameOffset); |
| 456 | (global._51degrees.header_names + index)->str = (char*)&headerName->firstByte; |
| 457 | (global._51degrees.header_names + index)->len = headerName->length - 1; |
| 458 | (global._51degrees.header_names + index)->size = (global._51degrees.header_names + index)->len; |
| 459 | } |
| 460 | } |
| 461 | #endif |
| 462 | |
| 463 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 464 | void _51d_init_http_headers() |
| 465 | { |
| 466 | int index = 0; |
| 467 | global._51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(); |
| 468 | global._51degrees.device_offsets.firstOffset = (fiftyoneDegreesDeviceOffset*)malloc( |
| 469 | global._51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset)); |
| 470 | global._51degrees.header_names = (struct chunk*)malloc(global._51degrees.header_count * sizeof(struct chunk)); |
| 471 | global._51degrees.header_offsets = (int32_t*)malloc(global._51degrees.header_count * sizeof(int32_t)); |
| 472 | for (index = 0; index < global._51degrees.header_count; index++) { |
| 473 | global._51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(index); |
| 474 | global._51degrees.header_names[index].str = fiftyoneDegreesGetHttpHeaderNamePointer(index); |
| 475 | global._51degrees.header_names[index].len = strlen(global._51degrees.header_names[index].str); |
| 476 | global._51degrees.header_names[index].size = global._51degrees.header_names[index].len; |
| 477 | } |
| 478 | } |
| 479 | #endif |
| 480 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 481 | int init_51degrees(void) |
| 482 | { |
| 483 | int i = 0; |
| 484 | struct chunk *temp; |
| 485 | struct _51d_property_names *name; |
| 486 | char **_51d_property_list = NULL; |
| 487 | fiftyoneDegreesDataSetInitStatus _51d_dataset_status = DATA_SET_INIT_STATUS_NOT_SET; |
| 488 | |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 489 | if (!global._51degrees.data_file_path) |
| 490 | return -1; |
| 491 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 492 | if (!LIST_ISEMPTY(&global._51degrees.property_names)) { |
| 493 | i = 0; |
| 494 | list_for_each_entry(name, &global._51degrees.property_names, list) |
| 495 | ++i; |
| 496 | _51d_property_list = calloc(i, sizeof(char *)); |
| 497 | |
| 498 | i = 0; |
| 499 | list_for_each_entry(name, &global._51degrees.property_names, list) |
| 500 | _51d_property_list[i++] = name->name; |
| 501 | } |
| 502 | |
| 503 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 504 | _51d_dataset_status = fiftyoneDegreesInitWithPropertyArray(global._51degrees.data_file_path, &global._51degrees.data_set, _51d_property_list, i); |
| 505 | #endif |
| 506 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 507 | _51d_dataset_status = fiftyoneDegreesInitWithPropertyArray(global._51degrees.data_file_path, _51d_property_list, i); |
| 508 | #endif |
| 509 | |
| 510 | temp = get_trash_chunk(); |
| 511 | chunk_reset(temp); |
| 512 | |
| 513 | switch (_51d_dataset_status) { |
| 514 | case DATA_SET_INIT_STATUS_SUCCESS: |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 515 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 516 | /* only 1 workset in the pool because HAProxy is currently single threaded |
| 517 | * this value should be set to the number of threads in future versions. |
| 518 | */ |
| 519 | global._51degrees.pool = fiftyoneDegreesWorksetPoolCreate(&global._51degrees.data_set, NULL, 1); |
| 520 | #endif |
| 521 | _51d_init_http_headers(); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 522 | break; |
| 523 | case DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY: |
| 524 | chunk_printf(temp, "Insufficient memory."); |
| 525 | break; |
| 526 | case DATA_SET_INIT_STATUS_CORRUPT_DATA: |
| 527 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 528 | chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Pattern data format."); |
| 529 | #endif |
| 530 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 531 | chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Trie data format."); |
| 532 | #endif |
| 533 | break; |
| 534 | case DATA_SET_INIT_STATUS_INCORRECT_VERSION: |
| 535 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 536 | chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Pattern data format."); |
| 537 | #endif |
| 538 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 539 | chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Trie data format."); |
| 540 | #endif |
| 541 | break; |
| 542 | case DATA_SET_INIT_STATUS_FILE_NOT_FOUND: |
| 543 | chunk_printf(temp, "File not found."); |
| 544 | break; |
| 545 | case DATA_SET_INIT_STATUS_NOT_SET: |
| 546 | chunk_printf(temp, "Data set not initialised."); |
| 547 | break; |
| 548 | } |
| 549 | if (_51d_dataset_status != DATA_SET_INIT_STATUS_SUCCESS) { |
| 550 | if (temp->len) |
| 551 | Alert("51Degrees Setup - Error reading 51Degrees data file. %s\n", temp->str); |
| 552 | else |
| 553 | Alert("51Degrees Setup - Error reading 51Degrees data file.\n"); |
| 554 | exit(1); |
| 555 | } |
| 556 | free(_51d_property_list); |
| 557 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 558 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 559 | _51d_lru_seed = random(); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 560 | if (global._51degrees.cache_size) { |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 561 | _51d_lru_tree = lru64_new(global._51degrees.cache_size); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 562 | } |
| 563 | #endif |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 564 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | void deinit_51degrees(void) |
| 569 | { |
| 570 | struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb; |
| 571 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 572 | free(global._51degrees.header_names); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 573 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 574 | fiftyoneDegreesWorksetPoolFree(global._51degrees.pool); |
| 575 | fiftyoneDegreesDataSetFree(&global._51degrees.data_set); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 576 | #endif |
| 577 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 578 | free(global._51degrees.device_offsets.firstOffset); |
| 579 | free(global._51degrees.header_offsets); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 580 | fiftyoneDegreesDestroy(); |
| 581 | #endif |
| 582 | |
| 583 | free(global._51degrees.data_file_path); global._51degrees.data_file_path = NULL; |
| 584 | list_for_each_entry_safe(_51d_prop_name, _51d_prop_nameb, &global._51degrees.property_names, list) { |
| 585 | LIST_DEL(&_51d_prop_name->list); |
| 586 | free(_51d_prop_name); |
| 587 | } |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 588 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 589 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 590 | while (lru64_destroy(_51d_lru_tree)); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 591 | #endif |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | static struct cfg_kw_list _51dcfg_kws = {{ }, { |
| 595 | { CFG_GLOBAL, "51degrees-data-file", _51d_data_file }, |
| 596 | { CFG_GLOBAL, "51degrees-property-name-list", _51d_property_name_list }, |
| 597 | { CFG_GLOBAL, "51degrees-property-separator", _51d_property_separator }, |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 598 | { CFG_GLOBAL, "51degrees-cache-size", _51d_cache_size }, |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 599 | { 0, NULL, NULL }, |
| 600 | }}; |
| 601 | |
| 602 | /* Note: must not be declared <const> as its list will be overwritten */ |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 603 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 604 | { "51d.all", _51d_fetch, ARG5(1,STR,STR,STR,STR,STR), _51d_fetch_check, SMP_T_STR, SMP_USE_HRQHV }, |
| 605 | { NULL, NULL, 0, 0, 0 }, |
| 606 | }}; |
| 607 | |
| 608 | /* Note: must not be declared <const> as its list will be overwritten */ |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 609 | static struct sample_conv_kw_list conv_kws = {ILH, { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 610 | { "51d.single", _51d_conv, ARG5(1,STR,STR,STR,STR,STR), _51d_conv_check, SMP_T_STR, SMP_T_STR }, |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 611 | { NULL, NULL, 0, 0, 0 }, |
| 612 | }}; |
| 613 | |
| 614 | __attribute__((constructor)) |
| 615 | static void __51d_init(void) |
| 616 | { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 617 | /* register sample fetch and conversion keywords */ |
| 618 | sample_register_fetches(&sample_fetch_keywords); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 619 | sample_register_convs(&conv_kws); |
| 620 | cfg_register_keywords(&_51dcfg_kws); |
| 621 | } |