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])) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 57 | name = calloc(1, sizeof(*name)); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 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 | { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 161 | struct chunk *cache_entry = _51d_malloc(sizeof(*cache_entry)); |
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 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 182 | struct chunk *cache_entry = 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); |
ben51degrees | 1f077eb | 2016-07-06 12:07:21 +0100 | [diff] [blame] | 238 | (offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global._51degrees.data_set, ctx.line + ctx.val); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 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]; |
ben51degrees | 1f077eb | 2016-07-06 12:07:21 +0100 | [diff] [blame] | 272 | const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(&global._51degrees.data_set); |
| 273 | int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(&global._51degrees.data_set); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 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 && |
ben51degrees | 1f077eb | 2016-07-06 12:07:21 +0100 | [diff] [blame] | 325 | fiftyoneDegreesGetValueFromOffsets(&global._51degrees.data_set, deviceOffsets, j, valuesBuffer, 1024) > 0) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 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 |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 332 | if (!found) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 333 | chunk_appendf(temp, "%s", no_data); |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 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); |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 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 | #endif |
| 414 | |
| 415 | return 1; |
| 416 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 417 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 418 | static int _51d_conv(const struct arg *args, struct sample *smp, void *private) |
| 419 | { |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 420 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 421 | fiftyoneDegreesWorkset* ws; /* workset for detection */ |
| 422 | struct lru64 *lru = NULL; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 423 | #endif |
| 424 | |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 425 | /* Flags the sample to show it uses constant memory*/ |
| 426 | smp->flags |= SMP_F_CONST; |
| 427 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 428 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 429 | |
| 430 | /* Look in the list. */ |
| 431 | if (_51d_lru_tree) { |
| 432 | unsigned long long seed = _51d_lru_seed ^ (long)args; |
| 433 | |
| 434 | 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] | 435 | _51d_lru_tree, (void*)args, 0); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 436 | if (lru && lru->domain) { |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 437 | _51d_retrieve_cache_entry(smp, lru); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 438 | return 1; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | /* Create workset. This will later contain detection results. */ |
| 443 | ws = fiftyoneDegreesWorksetPoolGet(global._51degrees.pool); |
| 444 | if (!ws) |
| 445 | return 0; |
| 446 | #endif |
| 447 | |
| 448 | /* Duplicate the data and remove the "const" flag before device detection. */ |
| 449 | if (!smp_dup(smp)) |
| 450 | return 0; |
| 451 | |
| 452 | smp->data.u.str.str[smp->data.u.str.len] = '\0'; |
| 453 | |
| 454 | /* Perform detection. */ |
| 455 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 456 | fiftyoneDegreesMatch(ws, smp->data.u.str.str); |
| 457 | _51d_process_match(args, smp, ws); |
| 458 | #endif |
| 459 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
ben51degrees | 1f077eb | 2016-07-06 12:07:21 +0100 | [diff] [blame] | 460 | global._51degrees.device_offsets.firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global._51degrees.data_set, smp->data.u.str.str); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 461 | global._51degrees.device_offsets.size = 1; |
| 462 | _51d_process_match(args, smp); |
| 463 | #endif |
| 464 | |
| 465 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 466 | fiftyoneDegreesWorksetPoolRelease(global._51degrees.pool, ws); |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 467 | if (lru) |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 468 | _51d_insert_cache_entry(smp, lru, (void*)args); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 469 | #endif |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 470 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 471 | return 1; |
| 472 | } |
| 473 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 474 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 475 | void _51d_init_http_headers() |
| 476 | { |
| 477 | int index = 0; |
| 478 | const fiftyoneDegreesAsciiString *headerName; |
| 479 | fiftyoneDegreesDataSet *ds = &global._51degrees.data_set; |
| 480 | global._51degrees.header_count = ds->httpHeadersCount; |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 481 | global._51degrees.header_names = malloc(global._51degrees.header_count * sizeof(struct chunk)); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 482 | for (index = 0; index < global._51degrees.header_count; index++) { |
| 483 | headerName = fiftyoneDegreesGetString(ds, ds->httpHeaders[index].headerNameOffset); |
| 484 | (global._51degrees.header_names + index)->str = (char*)&headerName->firstByte; |
| 485 | (global._51degrees.header_names + index)->len = headerName->length - 1; |
| 486 | (global._51degrees.header_names + index)->size = (global._51degrees.header_names + index)->len; |
| 487 | } |
| 488 | } |
| 489 | #endif |
| 490 | |
| 491 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 492 | void _51d_init_http_headers() |
| 493 | { |
| 494 | int index = 0; |
ben51degrees | 1f077eb | 2016-07-06 12:07:21 +0100 | [diff] [blame] | 495 | fiftyoneDegreesDataSet *ds = &global._51degrees.data_set; |
| 496 | global._51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(ds); |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 497 | global._51degrees.device_offsets.firstOffset = malloc( |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 498 | global._51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset)); |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 499 | global._51degrees.header_names = malloc(global._51degrees.header_count * sizeof(struct chunk)); |
| 500 | global._51degrees.header_offsets = malloc(global._51degrees.header_count * sizeof(int32_t)); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 501 | for (index = 0; index < global._51degrees.header_count; index++) { |
ben51degrees | 1f077eb | 2016-07-06 12:07:21 +0100 | [diff] [blame] | 502 | global._51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(ds, index); |
| 503 | global._51degrees.header_names[index].str = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(ds, index); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 504 | global._51degrees.header_names[index].len = strlen(global._51degrees.header_names[index].str); |
| 505 | global._51degrees.header_names[index].size = global._51degrees.header_names[index].len; |
| 506 | } |
| 507 | } |
| 508 | #endif |
| 509 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 510 | int init_51degrees(void) |
| 511 | { |
| 512 | int i = 0; |
| 513 | struct chunk *temp; |
| 514 | struct _51d_property_names *name; |
| 515 | char **_51d_property_list = NULL; |
| 516 | fiftyoneDegreesDataSetInitStatus _51d_dataset_status = DATA_SET_INIT_STATUS_NOT_SET; |
| 517 | |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 518 | if (!global._51degrees.data_file_path) |
| 519 | return -1; |
| 520 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 521 | if (!LIST_ISEMPTY(&global._51degrees.property_names)) { |
| 522 | i = 0; |
| 523 | list_for_each_entry(name, &global._51degrees.property_names, list) |
| 524 | ++i; |
| 525 | _51d_property_list = calloc(i, sizeof(char *)); |
| 526 | |
| 527 | i = 0; |
| 528 | list_for_each_entry(name, &global._51degrees.property_names, list) |
| 529 | _51d_property_list[i++] = name->name; |
| 530 | } |
| 531 | |
ben@51degrees.com | 496299a | 2016-01-08 13:49:32 +0000 | [diff] [blame] | 532 | _51d_dataset_status = fiftyoneDegreesInitWithPropertyArray(global._51degrees.data_file_path, &global._51degrees.data_set, (const char**)_51d_property_list, i); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 533 | |
| 534 | temp = get_trash_chunk(); |
| 535 | chunk_reset(temp); |
| 536 | |
| 537 | switch (_51d_dataset_status) { |
| 538 | case DATA_SET_INIT_STATUS_SUCCESS: |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 539 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 540 | /* only 1 workset in the pool because HAProxy is currently single threaded |
| 541 | * this value should be set to the number of threads in future versions. |
| 542 | */ |
| 543 | global._51degrees.pool = fiftyoneDegreesWorksetPoolCreate(&global._51degrees.data_set, NULL, 1); |
| 544 | #endif |
| 545 | _51d_init_http_headers(); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 546 | break; |
| 547 | case DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY: |
| 548 | chunk_printf(temp, "Insufficient memory."); |
| 549 | break; |
| 550 | case DATA_SET_INIT_STATUS_CORRUPT_DATA: |
| 551 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 552 | chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Pattern data format."); |
| 553 | #endif |
| 554 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 555 | chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Trie data format."); |
| 556 | #endif |
| 557 | break; |
| 558 | case DATA_SET_INIT_STATUS_INCORRECT_VERSION: |
| 559 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 560 | chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Pattern data format."); |
| 561 | #endif |
| 562 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 563 | chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Trie data format."); |
| 564 | #endif |
| 565 | break; |
| 566 | case DATA_SET_INIT_STATUS_FILE_NOT_FOUND: |
| 567 | chunk_printf(temp, "File not found."); |
| 568 | break; |
ben51degrees | 1f077eb | 2016-07-06 12:07:21 +0100 | [diff] [blame] | 569 | case DATA_SET_INIT_STATUS_NULL_POINTER: |
| 570 | chunk_printf(temp, "Null pointer to the existing dataset or memory location."); |
| 571 | break; |
| 572 | case DATA_SET_INIT_STATUS_POINTER_OUT_OF_BOUNDS: |
| 573 | chunk_printf(temp, "Allocated continuous memory containing 51Degrees data file appears to be smaller than expected. Most likely" |
| 574 | " because the data file was not fully loaded into the allocated memory."); |
| 575 | break; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 576 | case DATA_SET_INIT_STATUS_NOT_SET: |
| 577 | chunk_printf(temp, "Data set not initialised."); |
| 578 | break; |
| 579 | } |
| 580 | if (_51d_dataset_status != DATA_SET_INIT_STATUS_SUCCESS) { |
| 581 | if (temp->len) |
| 582 | Alert("51Degrees Setup - Error reading 51Degrees data file. %s\n", temp->str); |
| 583 | else |
| 584 | Alert("51Degrees Setup - Error reading 51Degrees data file.\n"); |
| 585 | exit(1); |
| 586 | } |
| 587 | free(_51d_property_list); |
| 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 | _51d_lru_seed = random(); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 591 | if (global._51degrees.cache_size) { |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 592 | _51d_lru_tree = lru64_new(global._51degrees.cache_size); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 593 | } |
| 594 | #endif |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 595 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | void deinit_51degrees(void) |
| 600 | { |
| 601 | struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb; |
| 602 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 603 | free(global._51degrees.header_names); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 604 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 605 | fiftyoneDegreesWorksetPoolFree(global._51degrees.pool); |
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 | #endif |
ben51degrees | 1f077eb | 2016-07-06 12:07:21 +0100 | [diff] [blame] | 611 | fiftyoneDegreesDataSetFree(&global._51degrees.data_set); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 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 | } |