blob: 3aa5b860b2f5689291b1ffed6ebdef1619171b1d [file] [log] [blame]
Dragan Dosen93b38d92015-06-29 16:43:25 +02001#include <stdio.h>
2
3#include <common/cfgparse.h>
4#include <common/chunk.h>
James Rosewella28bbd52015-09-18 18:28:52 +01005#include <common/buffer.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +02006#include <proto/arg.h>
7#include <proto/log.h>
James Rosewella28bbd52015-09-18 18:28:52 +01008#include <proto/proto_http.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +02009#include <proto/sample.h>
10#include <import/xxhash.h>
Dragan Dosen105c8e62015-06-29 16:43:26 +020011#include <import/lru.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +020012#include <import/51d.h>
13
14struct _51d_property_names {
15 struct list list;
16 char *name;
17};
18
James Rosewella28bbd52015-09-18 18:28:52 +010019#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +020020static struct lru64_head *_51d_lru_tree = NULL;
21static unsigned long long _51d_lru_seed;
James Rosewella28bbd52015-09-18 18:28:52 +010022#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +020023
Dragan Dosen93b38d92015-06-29 16:43:25 +020024static 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
42static int _51d_property_name_list(char **args, int section_type, struct proxy *curpx,
James Rosewella28bbd52015-09-18 18:28:52 +010043 struct proxy *defpx, const char *file, int line,
44 char **err)
Dragan Dosen93b38d92015-06-29 16:43:25 +020045{
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 Bernat02779b62016-04-03 13:48:43 +020057 name = calloc(1, sizeof(*name));
Dragan Dosen93b38d92015-06-29 16:43:25 +020058 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
66static 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 Dosen105c8e62015-06-29 16:43:26 +020088static 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 Rosewella28bbd52015-09-18 18:28:52 +0100107 return 0;
108}
109
110static 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 Dosen105c8e62015-06-29 16:43:26 +0200116 return 0;
117}
118
Dragan Dosen9373fc52015-08-07 16:41:23 +0200119static int _51d_conv_check(struct arg *arg, struct sample_conv *conv,
James Rosewella28bbd52015-09-18 18:28:52 +0100120 const char *file, int line, char **err_msg)
Dragan Dosen9373fc52015-08-07 16:41:23 +0200121{
122 if (global._51degrees.data_file_path)
123 return 1;
124
James Rosewella28bbd52015-09-18 18:28:52 +0100125 memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
Dragan Dosen9373fc52015-08-07 16:41:23 +0200126 return 0;
127}
128
James Rosewella28bbd52015-09-18 18:28:52 +0100129#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000130static 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*/
143static 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 Rosewell63426cb2015-09-18 19:53:05 +0100157/* Insert the data associated with the sample into the cache as a fresh item.
158 */
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000159static void _51d_insert_cache_entry(struct sample *smp, struct lru64 *lru, void* domain)
James Rosewell63426cb2015-09-18 19:53:05 +0100160{
Vincent Bernat02779b62016-04-03 13:48:43 +0200161 struct chunk *cache_entry = _51d_malloc(sizeof(*cache_entry));
James Rosewell63426cb2015-09-18 19:53:05 +0100162
163 if (!cache_entry)
164 return;
165
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000166 cache_entry->str = _51d_malloc(smp->data.u.str.len + 1);
167 if (!cache_entry->str) {
168 free(cache_entry);
James Rosewell63426cb2015-09-18 19:53:05 +0100169 return;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000170 }
James Rosewell63426cb2015-09-18 19:53:05 +0100171
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.comc9dfa242016-01-08 13:47:46 +0000175 lru64_commit(lru, cache_entry, domain, 0, _51d_lru_free);
James Rosewell63426cb2015-09-18 19:53:05 +0100176}
177
178/* Retrieves the data from the cache and sets the sample data to this string.
179 */
180static void _51d_retrieve_cache_entry(struct sample *smp, struct lru64 *lru)
181{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200182 struct chunk *cache_entry = lru->data;
James Rosewell63426cb2015-09-18 19:53:05 +0100183 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 Rosewella28bbd52015-09-18 18:28:52 +0100189/* Sets the important HTTP headers ahead of the detection
190 */
191static void _51d_set_headers(struct sample *smp, fiftyoneDegreesWorkset *ws)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200192{
James Rosewella28bbd52015-09-18 18:28:52 +0100193 struct hdr_idx *idx;
194 struct hdr_ctx ctx;
195 const struct http_msg *msg;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200196 int i;
James Rosewella28bbd52015-09-18 18:28:52 +0100197
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 Dosen93b38d92015-06-29 16:43:25 +0200216#endif
James Rosewella28bbd52015-09-18 18:28:52 +0100217
Dragan Dosen93b38d92015-06-29 16:43:25 +0200218#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100219static 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 Dosen105c8e62015-06-29 16:43:26 +0200226
James Rosewella28bbd52015-09-18 18:28:52 +0100227 idx = &smp->strm->txn->hdr_idx;
228 msg = &smp->strm->txn->req;
229 offsets->size = 0;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200230
James Rosewella28bbd52015-09-18 18:28:52 +0100231 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 Dosen105c8e62015-06-29 16:43:26 +0200240 }
241 }
James Rosewella28bbd52015-09-18 18:28:52 +0100242}
243#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200244
245#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100246/* Provides a hash code for the important HTTP headers.
247 */
248unsigned 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 Dosen93b38d92015-06-29 16:43:25 +0200261#endif
262
Dragan Dosen93b38d92015-06-29 16:43:25 +0200263#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100264static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesWorkset* ws)
265{
266 char *methodName;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200267#endif
268#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100269static void _51d_process_match(const struct arg *args, struct sample *smp)
270{
271 char valuesBuffer[1024];
ben@51degrees.com496299a2016-01-08 13:49:32 +0000272 const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames();
James Rosewella28bbd52015-09-18 18:28:52 +0100273 int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount();
274 fiftyoneDegreesDeviceOffsets *deviceOffsets = &global._51degrees.device_offsets;
275
Dragan Dosen93b38d92015-06-29 16:43:25 +0200276#endif
277
James Rosewella28bbd52015-09-18 18:28:52 +0100278 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 Dosen93b38d92015-06-29 16:43:25 +0200282
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 Dosen93b38d92015-06-29 16:43:25 +0200286 found = 0;
James Rosewella28bbd52015-09-18 18:28:52 +0100287#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 Dosen93b38d92015-06-29 16:43:25 +0200296 }
James Rosewella28bbd52015-09-18 18:28:52 +0100297 chunk_appendf(temp, "%s", methodName);
298 found = 1;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200299 }
James Rosewella28bbd52015-09-18 18:28:52 +0100300 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 Dosen93b38d92015-06-29 16:43:25 +0200318 }
319#endif
320#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100321 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 Dosen93b38d92015-06-29 16:43:25 +0200330 }
James Rosewella28bbd52015-09-18 18:28:52 +0100331#endif
ben@51degrees.comd3842522016-01-08 13:52:32 +0000332 if (!found)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200333 chunk_appendf(temp, "%s", no_data);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000334
Dragan Dosen93b38d92015-06-29 16:43:25 +0200335 /* 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 FOURNIER2046c462015-08-20 13:42:12 +0200345 smp->data.u.str.str = temp->str;
James Rosewell63426cb2015-09-18 19:53:05 +0100346 smp->data.u.str.len = temp->len;
James Rosewella28bbd52015-09-18 18:28:52 +0100347}
348
349static 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.com82a9d762016-01-08 13:42:41 +0000364 /* Flags the sample to show it uses constant memory*/
365 smp->flags |= SMP_F_CONST;
366
James Rosewella28bbd52015-09-18 18:28:52 +0100367#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.com82a9d762016-01-08 13:42:41 +0000385 _51d_lru_tree, (void*)args, 0);
James Rosewella28bbd52015-09-18 18:28:52 +0100386 if (lru && lru->domain) {
ben@51degrees.com6baceb92016-01-08 13:48:37 +0000387 fiftyoneDegreesWorksetPoolRelease(global._51degrees.pool, ws);
James Rosewell63426cb2015-09-18 19:53:05 +0100388 _51d_retrieve_cache_entry(smp, lru);
James Rosewella28bbd52015-09-18 18:28:52 +0100389 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.comd3842522016-01-08 13:52:32 +0000411 if (lru)
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000412 _51d_insert_cache_entry(smp, lru, (void*)args);
James Rosewella28bbd52015-09-18 18:28:52 +0100413#endif
414
415 return 1;
416}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200417
James Rosewella28bbd52015-09-18 18:28:52 +0100418static int _51d_conv(const struct arg *args, struct sample *smp, void *private)
419{
Dragan Dosen93b38d92015-06-29 16:43:25 +0200420#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100421 fiftyoneDegreesWorkset* ws; /* workset for detection */
422 struct lru64 *lru = NULL;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200423#endif
424
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000425 /* Flags the sample to show it uses constant memory*/
426 smp->flags |= SMP_F_CONST;
427
James Rosewella28bbd52015-09-18 18:28:52 +0100428#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.com82a9d762016-01-08 13:42:41 +0000435 _51d_lru_tree, (void*)args, 0);
James Rosewella28bbd52015-09-18 18:28:52 +0100436 if (lru && lru->domain) {
James Rosewell63426cb2015-09-18 19:53:05 +0100437 _51d_retrieve_cache_entry(smp, lru);
James Rosewella28bbd52015-09-18 18:28:52 +0100438 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
460 global._51degrees.device_offsets.firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(smp->data.u.str.str);
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.comd3842522016-01-08 13:52:32 +0000467 if (lru)
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000468 _51d_insert_cache_entry(smp, lru, (void*)args);
James Rosewella28bbd52015-09-18 18:28:52 +0100469#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200470
Dragan Dosen93b38d92015-06-29 16:43:25 +0200471 return 1;
472}
473
James Rosewella28bbd52015-09-18 18:28:52 +0100474#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
475void _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.comd3842522016-01-08 13:52:32 +0000481 global._51degrees.header_names = malloc(global._51degrees.header_count * sizeof(struct chunk));
James Rosewella28bbd52015-09-18 18:28:52 +0100482 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
492void _51d_init_http_headers()
493{
494 int index = 0;
495 global._51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount();
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200496 global._51degrees.device_offsets.firstOffset = malloc(
James Rosewella28bbd52015-09-18 18:28:52 +0100497 global._51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset));
ben@51degrees.comd3842522016-01-08 13:52:32 +0000498 global._51degrees.header_names = malloc(global._51degrees.header_count * sizeof(struct chunk));
499 global._51degrees.header_offsets = malloc(global._51degrees.header_count * sizeof(int32_t));
James Rosewella28bbd52015-09-18 18:28:52 +0100500 for (index = 0; index < global._51degrees.header_count; index++) {
501 global._51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(index);
ben@51degrees.com496299a2016-01-08 13:49:32 +0000502 global._51degrees.header_names[index].str = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(index);
James Rosewella28bbd52015-09-18 18:28:52 +0100503 global._51degrees.header_names[index].len = strlen(global._51degrees.header_names[index].str);
504 global._51degrees.header_names[index].size = global._51degrees.header_names[index].len;
505 }
506}
507#endif
508
Dragan Dosen93b38d92015-06-29 16:43:25 +0200509int init_51degrees(void)
510{
511 int i = 0;
512 struct chunk *temp;
513 struct _51d_property_names *name;
514 char **_51d_property_list = NULL;
515 fiftyoneDegreesDataSetInitStatus _51d_dataset_status = DATA_SET_INIT_STATUS_NOT_SET;
516
Dragan Dosen9373fc52015-08-07 16:41:23 +0200517 if (!global._51degrees.data_file_path)
518 return -1;
519
Dragan Dosen93b38d92015-06-29 16:43:25 +0200520 if (!LIST_ISEMPTY(&global._51degrees.property_names)) {
521 i = 0;
522 list_for_each_entry(name, &global._51degrees.property_names, list)
523 ++i;
524 _51d_property_list = calloc(i, sizeof(char *));
525
526 i = 0;
527 list_for_each_entry(name, &global._51degrees.property_names, list)
528 _51d_property_list[i++] = name->name;
529 }
530
531#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
ben@51degrees.com496299a2016-01-08 13:49:32 +0000532 _51d_dataset_status = fiftyoneDegreesInitWithPropertyArray(global._51degrees.data_file_path, &global._51degrees.data_set, (const char**)_51d_property_list, i);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200533#endif
534#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
ben@51degrees.com496299a2016-01-08 13:49:32 +0000535 _51d_dataset_status = fiftyoneDegreesInitWithPropertyArray(global._51degrees.data_file_path, (const char**)_51d_property_list, i);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200536#endif
537
538 temp = get_trash_chunk();
539 chunk_reset(temp);
540
541 switch (_51d_dataset_status) {
542 case DATA_SET_INIT_STATUS_SUCCESS:
James Rosewella28bbd52015-09-18 18:28:52 +0100543#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
544 /* only 1 workset in the pool because HAProxy is currently single threaded
545 * this value should be set to the number of threads in future versions.
546 */
547 global._51degrees.pool = fiftyoneDegreesWorksetPoolCreate(&global._51degrees.data_set, NULL, 1);
548#endif
549 _51d_init_http_headers();
Dragan Dosen93b38d92015-06-29 16:43:25 +0200550 break;
551 case DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY:
552 chunk_printf(temp, "Insufficient memory.");
553 break;
554 case DATA_SET_INIT_STATUS_CORRUPT_DATA:
555#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
556 chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Pattern data format.");
557#endif
558#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
559 chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Trie data format.");
560#endif
561 break;
562 case DATA_SET_INIT_STATUS_INCORRECT_VERSION:
563#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
564 chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Pattern data format.");
565#endif
566#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
567 chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Trie data format.");
568#endif
569 break;
570 case DATA_SET_INIT_STATUS_FILE_NOT_FOUND:
571 chunk_printf(temp, "File not found.");
572 break;
573 case DATA_SET_INIT_STATUS_NOT_SET:
574 chunk_printf(temp, "Data set not initialised.");
575 break;
576 }
577 if (_51d_dataset_status != DATA_SET_INIT_STATUS_SUCCESS) {
578 if (temp->len)
579 Alert("51Degrees Setup - Error reading 51Degrees data file. %s\n", temp->str);
580 else
581 Alert("51Degrees Setup - Error reading 51Degrees data file.\n");
582 exit(1);
583 }
584 free(_51d_property_list);
585
James Rosewella28bbd52015-09-18 18:28:52 +0100586#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200587 _51d_lru_seed = random();
James Rosewella28bbd52015-09-18 18:28:52 +0100588 if (global._51degrees.cache_size) {
Dragan Dosen105c8e62015-06-29 16:43:26 +0200589 _51d_lru_tree = lru64_new(global._51degrees.cache_size);
James Rosewella28bbd52015-09-18 18:28:52 +0100590 }
591#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200592
Dragan Dosen93b38d92015-06-29 16:43:25 +0200593 return 0;
594}
595
596void deinit_51degrees(void)
597{
598 struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb;
599
James Rosewella28bbd52015-09-18 18:28:52 +0100600 free(global._51degrees.header_names);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200601#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100602 fiftyoneDegreesWorksetPoolFree(global._51degrees.pool);
603 fiftyoneDegreesDataSetFree(&global._51degrees.data_set);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200604#endif
605#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100606 free(global._51degrees.device_offsets.firstOffset);
607 free(global._51degrees.header_offsets);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200608 fiftyoneDegreesDestroy();
609#endif
610
611 free(global._51degrees.data_file_path); global._51degrees.data_file_path = NULL;
612 list_for_each_entry_safe(_51d_prop_name, _51d_prop_nameb, &global._51degrees.property_names, list) {
613 LIST_DEL(&_51d_prop_name->list);
614 free(_51d_prop_name);
615 }
Dragan Dosen105c8e62015-06-29 16:43:26 +0200616
James Rosewella28bbd52015-09-18 18:28:52 +0100617#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200618 while (lru64_destroy(_51d_lru_tree));
James Rosewella28bbd52015-09-18 18:28:52 +0100619#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200620}
621
622static struct cfg_kw_list _51dcfg_kws = {{ }, {
623 { CFG_GLOBAL, "51degrees-data-file", _51d_data_file },
624 { CFG_GLOBAL, "51degrees-property-name-list", _51d_property_name_list },
625 { CFG_GLOBAL, "51degrees-property-separator", _51d_property_separator },
Dragan Dosen105c8e62015-06-29 16:43:26 +0200626 { CFG_GLOBAL, "51degrees-cache-size", _51d_cache_size },
Dragan Dosen93b38d92015-06-29 16:43:25 +0200627 { 0, NULL, NULL },
628}};
629
630/* Note: must not be declared <const> as its list will be overwritten */
James Rosewella28bbd52015-09-18 18:28:52 +0100631static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
632 { "51d.all", _51d_fetch, ARG5(1,STR,STR,STR,STR,STR), _51d_fetch_check, SMP_T_STR, SMP_USE_HRQHV },
633 { NULL, NULL, 0, 0, 0 },
634}};
635
636/* Note: must not be declared <const> as its list will be overwritten */
Dragan Dosen93b38d92015-06-29 16:43:25 +0200637static struct sample_conv_kw_list conv_kws = {ILH, {
James Rosewella28bbd52015-09-18 18:28:52 +0100638 { "51d.single", _51d_conv, ARG5(1,STR,STR,STR,STR,STR), _51d_conv_check, SMP_T_STR, SMP_T_STR },
Dragan Dosen93b38d92015-06-29 16:43:25 +0200639 { NULL, NULL, 0, 0, 0 },
640}};
641
642__attribute__((constructor))
643static void __51d_init(void)
644{
James Rosewella28bbd52015-09-18 18:28:52 +0100645 /* register sample fetch and conversion keywords */
646 sample_register_fetches(&sample_fetch_keywords);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200647 sample_register_convs(&conv_kws);
648 cfg_register_keywords(&_51dcfg_kws);
649}