blob: a44b40be2544784ae6f1d7c95ccfc4ee3c1f6a5a [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);
ben51degrees1f077eb2016-07-06 12:07:21 +0100238 (offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global._51degrees.data_set, ctx.line + ctx.val);
James Rosewella28bbd52015-09-18 18:28:52 +0100239 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];
ben51degrees1f077eb2016-07-06 12:07:21 +0100272 const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(&global._51degrees.data_set);
273 int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(&global._51degrees.data_set);
James Rosewella28bbd52015-09-18 18:28:52 +0100274 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 &&
ben51degrees1f077eb2016-07-06 12:07:21 +0100325 fiftyoneDegreesGetValueFromOffsets(&global._51degrees.data_set, deviceOffsets, j, valuesBuffer, 1024) > 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100326 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
ben51degrees1f077eb2016-07-06 12:07:21 +0100460 global._51degrees.device_offsets.firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global._51degrees.data_set, smp->data.u.str.str);
James Rosewella28bbd52015-09-18 18:28:52 +0100461 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;
ben51degrees1f077eb2016-07-06 12:07:21 +0100495 fiftyoneDegreesDataSet *ds = &global._51degrees.data_set;
496 global._51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(ds);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200497 global._51degrees.device_offsets.firstOffset = malloc(
James Rosewella28bbd52015-09-18 18:28:52 +0100498 global._51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset));
ben@51degrees.comd3842522016-01-08 13:52:32 +0000499 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 Rosewella28bbd52015-09-18 18:28:52 +0100501 for (index = 0; index < global._51degrees.header_count; index++) {
ben51degrees1f077eb2016-07-06 12:07:21 +0100502 global._51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(ds, index);
503 global._51degrees.header_names[index].str = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(ds, index);
James Rosewella28bbd52015-09-18 18:28:52 +0100504 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 Dosen93b38d92015-06-29 16:43:25 +0200510int 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 Dosen9373fc52015-08-07 16:41:23 +0200518 if (!global._51degrees.data_file_path)
519 return -1;
520
Dragan Dosen93b38d92015-06-29 16:43:25 +0200521 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.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
534 temp = get_trash_chunk();
535 chunk_reset(temp);
536
537 switch (_51d_dataset_status) {
538 case DATA_SET_INIT_STATUS_SUCCESS:
James Rosewella28bbd52015-09-18 18:28:52 +0100539#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 Dosen93b38d92015-06-29 16:43:25 +0200546 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;
ben51degrees1f077eb2016-07-06 12:07:21 +0100569 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 Dosen93b38d92015-06-29 16:43:25 +0200576 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 Rosewella28bbd52015-09-18 18:28:52 +0100589#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200590 _51d_lru_seed = random();
James Rosewella28bbd52015-09-18 18:28:52 +0100591 if (global._51degrees.cache_size) {
Dragan Dosen105c8e62015-06-29 16:43:26 +0200592 _51d_lru_tree = lru64_new(global._51degrees.cache_size);
James Rosewella28bbd52015-09-18 18:28:52 +0100593 }
594#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200595
Dragan Dosen93b38d92015-06-29 16:43:25 +0200596 return 0;
597}
598
599void deinit_51degrees(void)
600{
601 struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb;
602
James Rosewella28bbd52015-09-18 18:28:52 +0100603 free(global._51degrees.header_names);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200604#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100605 fiftyoneDegreesWorksetPoolFree(global._51degrees.pool);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200606#endif
607#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100608 free(global._51degrees.device_offsets.firstOffset);
609 free(global._51degrees.header_offsets);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200610#endif
ben51degrees1f077eb2016-07-06 12:07:21 +0100611 fiftyoneDegreesDataSetFree(&global._51degrees.data_set);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200612
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 Dosen105c8e62015-06-29 16:43:26 +0200618
James Rosewella28bbd52015-09-18 18:28:52 +0100619#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200620 while (lru64_destroy(_51d_lru_tree));
James Rosewella28bbd52015-09-18 18:28:52 +0100621#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200622}
623
624static 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 Dosen105c8e62015-06-29 16:43:26 +0200628 { CFG_GLOBAL, "51degrees-cache-size", _51d_cache_size },
Dragan Dosen93b38d92015-06-29 16:43:25 +0200629 { 0, NULL, NULL },
630}};
631
632/* Note: must not be declared <const> as its list will be overwritten */
James Rosewella28bbd52015-09-18 18:28:52 +0100633static 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 Dosen93b38d92015-06-29 16:43:25 +0200639static struct sample_conv_kw_list conv_kws = {ILH, {
James Rosewella28bbd52015-09-18 18:28:52 +0100640 { "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 +0200641 { NULL, NULL, 0, 0, 0 },
642}};
643
644__attribute__((constructor))
645static void __51d_init(void)
646{
James Rosewella28bbd52015-09-18 18:28:52 +0100647 /* register sample fetch and conversion keywords */
648 sample_register_fetches(&sample_fetch_keywords);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200649 sample_register_convs(&conv_kws);
650 cfg_register_keywords(&_51dcfg_kws);
Willy Tarreaub5e58d62016-12-21 18:49:29 +0100651 hap_register_build_opts("Built with 51Degrees support.", 0);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200652}