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