blob: 37ee90cece71302fe6cd44a860bc3ae30220e6b4 [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>
Willy Tarreau9f3f2542016-12-21 20:30:05 +01006#include <common/errors.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +02007#include <proto/arg.h>
8#include <proto/log.h>
James Rosewella28bbd52015-09-18 18:28:52 +01009#include <proto/proto_http.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +020010#include <proto/sample.h>
11#include <import/xxhash.h>
Dragan Dosen105c8e62015-06-29 16:43:26 +020012#include <import/lru.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +020013#include <import/51d.h>
14
15struct _51d_property_names {
16 struct list list;
17 char *name;
18};
19
James Rosewella28bbd52015-09-18 18:28:52 +010020#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +020021static struct lru64_head *_51d_lru_tree = NULL;
22static unsigned long long _51d_lru_seed;
James Rosewella28bbd52015-09-18 18:28:52 +010023#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +020024
Dragan Dosen93b38d92015-06-29 16:43:25 +020025static int _51d_data_file(char **args, int section_type, struct proxy *curpx,
26 struct proxy *defpx, const char *file, int line,
27 char **err)
28{
29 if (*(args[1]) == 0) {
30 memprintf(err,
31 "'%s' expects a filepath to a 51Degrees trie or pattern data file.",
32 args[0]);
33 return -1;
34 }
35
36 if (global._51degrees.data_file_path)
37 free(global._51degrees.data_file_path);
38 global._51degrees.data_file_path = strdup(args[1]);
39
40 return 0;
41}
42
43static int _51d_property_name_list(char **args, int section_type, struct proxy *curpx,
James Rosewella28bbd52015-09-18 18:28:52 +010044 struct proxy *defpx, const char *file, int line,
45 char **err)
Dragan Dosen93b38d92015-06-29 16:43:25 +020046{
47 int cur_arg = 1;
48 struct _51d_property_names *name;
49
50 if (*(args[cur_arg]) == 0) {
51 memprintf(err,
52 "'%s' expects at least one 51Degrees property name.",
53 args[0]);
54 return -1;
55 }
56
57 while (*(args[cur_arg])) {
Vincent Bernat02779b62016-04-03 13:48:43 +020058 name = calloc(1, sizeof(*name));
Dragan Dosen93b38d92015-06-29 16:43:25 +020059 name->name = strdup(args[cur_arg]);
60 LIST_ADDQ(&global._51degrees.property_names, &name->list);
61 ++cur_arg;
62 }
63
64 return 0;
65}
66
67static int _51d_property_separator(char **args, int section_type, struct proxy *curpx,
68 struct proxy *defpx, const char *file, int line,
69 char **err)
70{
71 if (*(args[1]) == 0) {
72 memprintf(err,
73 "'%s' expects a single character.",
74 args[0]);
75 return -1;
76 }
77 if (strlen(args[1]) > 1) {
78 memprintf(err,
79 "'%s' expects a single character, got '%s'.",
80 args[0], args[1]);
81 return -1;
82 }
83
84 global._51degrees.property_separator = *args[1];
85
86 return 0;
87}
88
Dragan Dosen105c8e62015-06-29 16:43:26 +020089static int _51d_cache_size(char **args, int section_type, struct proxy *curpx,
90 struct proxy *defpx, const char *file, int line,
91 char **err)
92{
93 if (*(args[1]) == 0) {
94 memprintf(err,
95 "'%s' expects a positive numeric value.",
96 args[0]);
97 return -1;
98 }
99
100 global._51degrees.cache_size = atoi(args[1]);
101 if (global._51degrees.cache_size < 0) {
102 memprintf(err,
103 "'%s' expects a positive numeric value, got '%s'.",
104 args[0], args[1]);
105 return -1;
106 }
107
James Rosewella28bbd52015-09-18 18:28:52 +0100108 return 0;
109}
110
111static int _51d_fetch_check(struct arg *arg, char **err_msg)
112{
113 if (global._51degrees.data_file_path)
114 return 1;
115
116 memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
Dragan Dosen105c8e62015-06-29 16:43:26 +0200117 return 0;
118}
119
Dragan Dosen9373fc52015-08-07 16:41:23 +0200120static int _51d_conv_check(struct arg *arg, struct sample_conv *conv,
James Rosewella28bbd52015-09-18 18:28:52 +0100121 const char *file, int line, char **err_msg)
Dragan Dosen9373fc52015-08-07 16:41:23 +0200122{
123 if (global._51degrees.data_file_path)
124 return 1;
125
James Rosewella28bbd52015-09-18 18:28:52 +0100126 memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
Dragan Dosen9373fc52015-08-07 16:41:23 +0200127 return 0;
128}
129
James Rosewella28bbd52015-09-18 18:28:52 +0100130#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000131static void _51d_lru_free(void *cache_entry)
132{
133 struct chunk *ptr = cache_entry;
134
135 if (!ptr)
136 return;
137
138 free(ptr->str);
139 free(ptr);
140}
141
142/* Allocates memory freeing space in the cache if necessary.
143*/
144static void *_51d_malloc(int size)
145{
146 void *ptr = malloc(size);
147
148 if (!ptr) {
149 /* free the oldest 10 entries from lru to free up some memory
150 * then try allocating memory again */
151 lru64_kill_oldest(_51d_lru_tree, 10);
152 ptr = malloc(size);
153 }
154
155 return ptr;
156}
157
James Rosewell63426cb2015-09-18 19:53:05 +0100158/* Insert the data associated with the sample into the cache as a fresh item.
159 */
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000160static void _51d_insert_cache_entry(struct sample *smp, struct lru64 *lru, void* domain)
James Rosewell63426cb2015-09-18 19:53:05 +0100161{
Vincent Bernat02779b62016-04-03 13:48:43 +0200162 struct chunk *cache_entry = _51d_malloc(sizeof(*cache_entry));
James Rosewell63426cb2015-09-18 19:53:05 +0100163
164 if (!cache_entry)
165 return;
166
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000167 cache_entry->str = _51d_malloc(smp->data.u.str.len + 1);
168 if (!cache_entry->str) {
169 free(cache_entry);
James Rosewell63426cb2015-09-18 19:53:05 +0100170 return;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000171 }
James Rosewell63426cb2015-09-18 19:53:05 +0100172
173 memcpy(cache_entry->str, smp->data.u.str.str, smp->data.u.str.len);
174 cache_entry->str[smp->data.u.str.len] = 0;
175 cache_entry->len = smp->data.u.str.len;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000176 lru64_commit(lru, cache_entry, domain, 0, _51d_lru_free);
James Rosewell63426cb2015-09-18 19:53:05 +0100177}
178
179/* Retrieves the data from the cache and sets the sample data to this string.
180 */
181static void _51d_retrieve_cache_entry(struct sample *smp, struct lru64 *lru)
182{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200183 struct chunk *cache_entry = lru->data;
James Rosewell63426cb2015-09-18 19:53:05 +0100184 smp->data.u.str.str = cache_entry->str;
185 smp->data.u.str.len = cache_entry->len;
186}
187#endif
188
189#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100190/* Sets the important HTTP headers ahead of the detection
191 */
192static void _51d_set_headers(struct sample *smp, fiftyoneDegreesWorkset *ws)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200193{
James Rosewella28bbd52015-09-18 18:28:52 +0100194 struct hdr_idx *idx;
195 struct hdr_ctx ctx;
196 const struct http_msg *msg;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200197 int i;
James Rosewella28bbd52015-09-18 18:28:52 +0100198
199 idx = &smp->strm->txn->hdr_idx;
200 msg = &smp->strm->txn->req;
201
202 ws->importantHeadersCount = 0;
203
204 for (i = 0; i < global._51degrees.header_count; i++) {
205 ctx.idx = 0;
206 if (http_find_full_header2(
207 (global._51degrees.header_names + i)->str,
208 (global._51degrees.header_names + i)->len,
209 msg->chn->buf->p, idx, &ctx) == 1) {
210 ws->importantHeaders[ws->importantHeadersCount].header = ws->dataSet->httpHeaders + i;
211 ws->importantHeaders[ws->importantHeadersCount].headerValue = ctx.line + ctx.val;
212 ws->importantHeaders[ws->importantHeadersCount].headerValueLength = ctx.vlen;
213 ws->importantHeadersCount++;
214 }
215 }
216}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200217#endif
James Rosewella28bbd52015-09-18 18:28:52 +0100218
Dragan Dosen93b38d92015-06-29 16:43:25 +0200219#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100220static void _51d_set_device_offsets(struct sample *smp)
221{
222 struct hdr_idx *idx;
223 struct hdr_ctx ctx;
224 const struct http_msg *msg;
225 int index;
226 fiftyoneDegreesDeviceOffsets *offsets = &global._51degrees.device_offsets;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200227
James Rosewella28bbd52015-09-18 18:28:52 +0100228 idx = &smp->strm->txn->hdr_idx;
229 msg = &smp->strm->txn->req;
230 offsets->size = 0;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200231
James Rosewella28bbd52015-09-18 18:28:52 +0100232 for (index = 0; index < global._51degrees.header_count; index++) {
233 ctx.idx = 0;
234 if (http_find_full_header2(
235 (global._51degrees.header_names + index)->str,
236 (global._51degrees.header_names + index)->len,
237 msg->chn->buf->p, idx, &ctx) == 1) {
238 (offsets->firstOffset + offsets->size)->httpHeaderOffset = *(global._51degrees.header_offsets + index);
ben51degrees1f077eb2016-07-06 12:07:21 +0100239 (offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global._51degrees.data_set, ctx.line + ctx.val);
James Rosewella28bbd52015-09-18 18:28:52 +0100240 offsets->size++;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200241 }
242 }
James Rosewella28bbd52015-09-18 18:28:52 +0100243}
244#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200245
246#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100247/* Provides a hash code for the important HTTP headers.
248 */
249unsigned long long _51d_req_hash(const struct arg *args, fiftyoneDegreesWorkset* ws)
250{
251 unsigned long long seed = _51d_lru_seed ^ (long)args;
252 unsigned long long hash = 0;
253 int i;
254 for(i = 0; i < ws->importantHeadersCount; i++) {
255 hash ^= ws->importantHeaders[i].header->headerNameOffset;
256 hash ^= XXH64(ws->importantHeaders[i].headerValue,
257 ws->importantHeaders[i].headerValueLength,
258 seed);
259 }
260 return hash;
261}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200262#endif
263
Dragan Dosen93b38d92015-06-29 16:43:25 +0200264#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100265static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesWorkset* ws)
266{
267 char *methodName;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200268#endif
269#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100270static void _51d_process_match(const struct arg *args, struct sample *smp)
271{
272 char valuesBuffer[1024];
ben51degrees1f077eb2016-07-06 12:07:21 +0100273 const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(&global._51degrees.data_set);
274 int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(&global._51degrees.data_set);
James Rosewella28bbd52015-09-18 18:28:52 +0100275 fiftyoneDegreesDeviceOffsets *deviceOffsets = &global._51degrees.device_offsets;
276
Dragan Dosen93b38d92015-06-29 16:43:25 +0200277#endif
278
James Rosewella28bbd52015-09-18 18:28:52 +0100279 char no_data[] = "NoData"; /* response when no data could be found */
280 struct chunk *temp = get_trash_chunk();
281 int j, i = 0, found;
282 const char* property_name;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200283
284 /* Loop through property names passed to the filter and fetch them from the dataset. */
285 while (args[i].data.str.str) {
286 /* Try to find request property in dataset. */
Dragan Dosen93b38d92015-06-29 16:43:25 +0200287 found = 0;
James Rosewella28bbd52015-09-18 18:28:52 +0100288#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
289 if (strcmp("Method", args[i].data.str.str) == 0) {
290 switch(ws->method) {
291 case EXACT: methodName = "Exact"; break;
292 case NUMERIC: methodName = "Numeric"; break;
293 case NEAREST: methodName = "Nearest"; break;
294 case CLOSEST: methodName = "Closest"; break;
295 default:
296 case NONE: methodName = "None"; break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200297 }
James Rosewella28bbd52015-09-18 18:28:52 +0100298 chunk_appendf(temp, "%s", methodName);
299 found = 1;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200300 }
James Rosewella28bbd52015-09-18 18:28:52 +0100301 else if (strcmp("Difference", args[i].data.str.str) == 0) {
302 chunk_appendf(temp, "%d", ws->difference);
303 found = 1;
304 }
305 else if (strcmp("Rank", args[i].data.str.str) == 0) {
306 chunk_appendf(temp, "%d", fiftyoneDegreesGetSignatureRank(ws));
307 found = 1;
308 }
309 else {
310 for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) {
311 property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]);
312 if (strcmp(property_name, args[i].data.str.str) == 0) {
313 found = 1;
314 fiftyoneDegreesSetValues(ws, j);
315 chunk_appendf(temp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values));
316 break;
317 }
318 }
Dragan Dosen93b38d92015-06-29 16:43:25 +0200319 }
320#endif
321#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100322 found = 0;
323 for (j = 0; j < requiredPropertiesCount; j++) {
324 property_name = requiredProperties[j];
325 if (strcmp(property_name, args[i].data.str.str) == 0 &&
ben51degrees1f077eb2016-07-06 12:07:21 +0100326 fiftyoneDegreesGetValueFromOffsets(&global._51degrees.data_set, deviceOffsets, j, valuesBuffer, 1024) > 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100327 found = 1;
328 chunk_appendf(temp, "%s", valuesBuffer);
329 break;
330 }
Dragan Dosen93b38d92015-06-29 16:43:25 +0200331 }
James Rosewella28bbd52015-09-18 18:28:52 +0100332#endif
ben@51degrees.comd3842522016-01-08 13:52:32 +0000333 if (!found)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200334 chunk_appendf(temp, "%s", no_data);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000335
Dragan Dosen93b38d92015-06-29 16:43:25 +0200336 /* Add separator. */
337 chunk_appendf(temp, "%c", global._51degrees.property_separator);
338 ++i;
339 }
340
341 if (temp->len) {
342 --temp->len;
343 temp->str[temp->len] = '\0';
344 }
345
Thierry FOURNIER2046c462015-08-20 13:42:12 +0200346 smp->data.u.str.str = temp->str;
James Rosewell63426cb2015-09-18 19:53:05 +0100347 smp->data.u.str.len = temp->len;
James Rosewella28bbd52015-09-18 18:28:52 +0100348}
349
350static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
351{
352#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
353 fiftyoneDegreesWorkset* ws; /* workset for detection */
354 struct lru64 *lru = NULL;
355#endif
356
357 /* Needed to ensure that the HTTP message has been fully recieved when
358 * used with TCP operation. Not required for HTTP operation.
359 * Data type has to be reset to ensure the string output is processed
360 * correctly.
361 */
362 CHECK_HTTP_MESSAGE_FIRST();
363 smp->data.type = SMP_T_STR;
364
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000365 /* Flags the sample to show it uses constant memory*/
366 smp->flags |= SMP_F_CONST;
367
James Rosewella28bbd52015-09-18 18:28:52 +0100368#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
369
370 /* Get only the headers needed for device detection so they can be used
371 * with the cache to return previous results. Pattern is slower than
372 * Trie so caching will help improve performance.
373 */
374
375 /* Get a workset from the pool which will later contain detection results. */
376 ws = fiftyoneDegreesWorksetPoolGet(global._51degrees.pool);
377 if (!ws)
378 return 0;
379
380 /* Set the important HTTP headers for this request in the workset. */
381 _51d_set_headers(smp, ws);
382
383 /* Check the cache to see if there's results for these headers already. */
384 if (_51d_lru_tree) {
385 lru = lru64_get(_51d_req_hash(args, ws),
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000386 _51d_lru_tree, (void*)args, 0);
James Rosewella28bbd52015-09-18 18:28:52 +0100387 if (lru && lru->domain) {
ben@51degrees.com6baceb92016-01-08 13:48:37 +0000388 fiftyoneDegreesWorksetPoolRelease(global._51degrees.pool, ws);
James Rosewell63426cb2015-09-18 19:53:05 +0100389 _51d_retrieve_cache_entry(smp, lru);
James Rosewella28bbd52015-09-18 18:28:52 +0100390 return 1;
391 }
392 }
393
394 fiftyoneDegreesMatchForHttpHeaders(ws);
395
396 _51d_process_match(args, smp, ws);
397
398#endif
399
400#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
401
402 /* Trie is very fast so all the headers can be passed in and the result
403 * returned faster than the hashing algorithm process.
404 */
405 _51d_set_device_offsets(smp);
406 _51d_process_match(args, smp);
407
408#endif
409
410#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
411 fiftyoneDegreesWorksetPoolRelease(global._51degrees.pool, ws);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000412 if (lru)
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000413 _51d_insert_cache_entry(smp, lru, (void*)args);
James Rosewella28bbd52015-09-18 18:28:52 +0100414#endif
415
416 return 1;
417}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200418
James Rosewella28bbd52015-09-18 18:28:52 +0100419static int _51d_conv(const struct arg *args, struct sample *smp, void *private)
420{
Dragan Dosen93b38d92015-06-29 16:43:25 +0200421#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100422 fiftyoneDegreesWorkset* ws; /* workset for detection */
423 struct lru64 *lru = NULL;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200424#endif
425
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000426 /* Flags the sample to show it uses constant memory*/
427 smp->flags |= SMP_F_CONST;
428
James Rosewella28bbd52015-09-18 18:28:52 +0100429#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
430
431 /* Look in the list. */
432 if (_51d_lru_tree) {
433 unsigned long long seed = _51d_lru_seed ^ (long)args;
434
435 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000436 _51d_lru_tree, (void*)args, 0);
James Rosewella28bbd52015-09-18 18:28:52 +0100437 if (lru && lru->domain) {
James Rosewell63426cb2015-09-18 19:53:05 +0100438 _51d_retrieve_cache_entry(smp, lru);
James Rosewella28bbd52015-09-18 18:28:52 +0100439 return 1;
440 }
441 }
442
443 /* Create workset. This will later contain detection results. */
444 ws = fiftyoneDegreesWorksetPoolGet(global._51degrees.pool);
445 if (!ws)
446 return 0;
447#endif
448
449 /* Duplicate the data and remove the "const" flag before device detection. */
450 if (!smp_dup(smp))
451 return 0;
452
453 smp->data.u.str.str[smp->data.u.str.len] = '\0';
454
455 /* Perform detection. */
456#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
457 fiftyoneDegreesMatch(ws, smp->data.u.str.str);
458 _51d_process_match(args, smp, ws);
459#endif
460#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
ben51degrees1f077eb2016-07-06 12:07:21 +0100461 global._51degrees.device_offsets.firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global._51degrees.data_set, smp->data.u.str.str);
James Rosewella28bbd52015-09-18 18:28:52 +0100462 global._51degrees.device_offsets.size = 1;
463 _51d_process_match(args, smp);
464#endif
465
466#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
467 fiftyoneDegreesWorksetPoolRelease(global._51degrees.pool, ws);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000468 if (lru)
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000469 _51d_insert_cache_entry(smp, lru, (void*)args);
James Rosewella28bbd52015-09-18 18:28:52 +0100470#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200471
Dragan Dosen93b38d92015-06-29 16:43:25 +0200472 return 1;
473}
474
James Rosewella28bbd52015-09-18 18:28:52 +0100475#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
476void _51d_init_http_headers()
477{
478 int index = 0;
479 const fiftyoneDegreesAsciiString *headerName;
480 fiftyoneDegreesDataSet *ds = &global._51degrees.data_set;
481 global._51degrees.header_count = ds->httpHeadersCount;
ben@51degrees.comd3842522016-01-08 13:52:32 +0000482 global._51degrees.header_names = malloc(global._51degrees.header_count * sizeof(struct chunk));
James Rosewella28bbd52015-09-18 18:28:52 +0100483 for (index = 0; index < global._51degrees.header_count; index++) {
484 headerName = fiftyoneDegreesGetString(ds, ds->httpHeaders[index].headerNameOffset);
485 (global._51degrees.header_names + index)->str = (char*)&headerName->firstByte;
486 (global._51degrees.header_names + index)->len = headerName->length - 1;
487 (global._51degrees.header_names + index)->size = (global._51degrees.header_names + index)->len;
488 }
489}
490#endif
491
492#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
493void _51d_init_http_headers()
494{
495 int index = 0;
ben51degrees1f077eb2016-07-06 12:07:21 +0100496 fiftyoneDegreesDataSet *ds = &global._51degrees.data_set;
497 global._51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(ds);
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200498 global._51degrees.device_offsets.firstOffset = malloc(
James Rosewella28bbd52015-09-18 18:28:52 +0100499 global._51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset));
ben@51degrees.comd3842522016-01-08 13:52:32 +0000500 global._51degrees.header_names = malloc(global._51degrees.header_count * sizeof(struct chunk));
501 global._51degrees.header_offsets = malloc(global._51degrees.header_count * sizeof(int32_t));
James Rosewella28bbd52015-09-18 18:28:52 +0100502 for (index = 0; index < global._51degrees.header_count; index++) {
ben51degrees1f077eb2016-07-06 12:07:21 +0100503 global._51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(ds, index);
504 global._51degrees.header_names[index].str = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(ds, index);
James Rosewella28bbd52015-09-18 18:28:52 +0100505 global._51degrees.header_names[index].len = strlen(global._51degrees.header_names[index].str);
506 global._51degrees.header_names[index].size = global._51degrees.header_names[index].len;
507 }
508}
509#endif
510
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100511/*
512 * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*.
513 */
514static int init_51degrees(void)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200515{
516 int i = 0;
517 struct chunk *temp;
518 struct _51d_property_names *name;
519 char **_51d_property_list = NULL;
520 fiftyoneDegreesDataSetInitStatus _51d_dataset_status = DATA_SET_INIT_STATUS_NOT_SET;
521
Dragan Dosen9373fc52015-08-07 16:41:23 +0200522 if (!global._51degrees.data_file_path)
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100523 return 0;
Dragan Dosen9373fc52015-08-07 16:41:23 +0200524
Dragan Dosen93b38d92015-06-29 16:43:25 +0200525 if (!LIST_ISEMPTY(&global._51degrees.property_names)) {
526 i = 0;
527 list_for_each_entry(name, &global._51degrees.property_names, list)
528 ++i;
529 _51d_property_list = calloc(i, sizeof(char *));
530
531 i = 0;
532 list_for_each_entry(name, &global._51degrees.property_names, list)
533 _51d_property_list[i++] = name->name;
534 }
535
ben@51degrees.com496299a2016-01-08 13:49:32 +0000536 _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 +0200537
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;
ben51degrees1f077eb2016-07-06 12:07:21 +0100573 case DATA_SET_INIT_STATUS_NULL_POINTER:
574 chunk_printf(temp, "Null pointer to the existing dataset or memory location.");
575 break;
576 case DATA_SET_INIT_STATUS_POINTER_OUT_OF_BOUNDS:
577 chunk_printf(temp, "Allocated continuous memory containing 51Degrees data file appears to be smaller than expected. Most likely"
578 " because the data file was not fully loaded into the allocated memory.");
579 break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200580 case DATA_SET_INIT_STATUS_NOT_SET:
581 chunk_printf(temp, "Data set not initialised.");
582 break;
583 }
584 if (_51d_dataset_status != DATA_SET_INIT_STATUS_SUCCESS) {
585 if (temp->len)
586 Alert("51Degrees Setup - Error reading 51Degrees data file. %s\n", temp->str);
587 else
588 Alert("51Degrees Setup - Error reading 51Degrees data file.\n");
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100589 return ERR_ALERT | ERR_FATAL;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200590 }
591 free(_51d_property_list);
592
James Rosewella28bbd52015-09-18 18:28:52 +0100593#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200594 _51d_lru_seed = random();
James Rosewella28bbd52015-09-18 18:28:52 +0100595 if (global._51degrees.cache_size) {
Dragan Dosen105c8e62015-06-29 16:43:26 +0200596 _51d_lru_tree = lru64_new(global._51degrees.cache_size);
James Rosewella28bbd52015-09-18 18:28:52 +0100597 }
598#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200599
Dragan Dosen93b38d92015-06-29 16:43:25 +0200600 return 0;
601}
602
Willy Tarreau7ac4c202016-12-21 20:59:01 +0100603static void deinit_51degrees(void)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200604{
605 struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb;
606
James Rosewella28bbd52015-09-18 18:28:52 +0100607 free(global._51degrees.header_names);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200608#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100609 fiftyoneDegreesWorksetPoolFree(global._51degrees.pool);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200610#endif
611#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100612 free(global._51degrees.device_offsets.firstOffset);
613 free(global._51degrees.header_offsets);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200614#endif
ben51degrees1f077eb2016-07-06 12:07:21 +0100615 fiftyoneDegreesDataSetFree(&global._51degrees.data_set);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200616
617 free(global._51degrees.data_file_path); global._51degrees.data_file_path = NULL;
618 list_for_each_entry_safe(_51d_prop_name, _51d_prop_nameb, &global._51degrees.property_names, list) {
619 LIST_DEL(&_51d_prop_name->list);
620 free(_51d_prop_name);
621 }
Dragan Dosen105c8e62015-06-29 16:43:26 +0200622
James Rosewella28bbd52015-09-18 18:28:52 +0100623#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200624 while (lru64_destroy(_51d_lru_tree));
James Rosewella28bbd52015-09-18 18:28:52 +0100625#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200626}
627
628static struct cfg_kw_list _51dcfg_kws = {{ }, {
629 { CFG_GLOBAL, "51degrees-data-file", _51d_data_file },
630 { CFG_GLOBAL, "51degrees-property-name-list", _51d_property_name_list },
631 { CFG_GLOBAL, "51degrees-property-separator", _51d_property_separator },
Dragan Dosen105c8e62015-06-29 16:43:26 +0200632 { CFG_GLOBAL, "51degrees-cache-size", _51d_cache_size },
Dragan Dosen93b38d92015-06-29 16:43:25 +0200633 { 0, NULL, NULL },
634}};
635
636/* Note: must not be declared <const> as its list will be overwritten */
James Rosewella28bbd52015-09-18 18:28:52 +0100637static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
638 { "51d.all", _51d_fetch, ARG5(1,STR,STR,STR,STR,STR), _51d_fetch_check, SMP_T_STR, SMP_USE_HRQHV },
639 { NULL, NULL, 0, 0, 0 },
640}};
641
642/* Note: must not be declared <const> as its list will be overwritten */
Dragan Dosen93b38d92015-06-29 16:43:25 +0200643static struct sample_conv_kw_list conv_kws = {ILH, {
James Rosewella28bbd52015-09-18 18:28:52 +0100644 { "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 +0200645 { NULL, NULL, 0, 0, 0 },
646}};
647
648__attribute__((constructor))
649static void __51d_init(void)
650{
James Rosewella28bbd52015-09-18 18:28:52 +0100651 /* register sample fetch and conversion keywords */
652 sample_register_fetches(&sample_fetch_keywords);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200653 sample_register_convs(&conv_kws);
654 cfg_register_keywords(&_51dcfg_kws);
Willy Tarreaub5e58d62016-12-21 18:49:29 +0100655 hap_register_build_opts("Built with 51Degrees support.", 0);
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100656 hap_register_post_check(init_51degrees);
Willy Tarreau7ac4c202016-12-21 20:59:01 +0100657 hap_register_post_deinit(deinit_51degrees);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200658}