blob: 421c0de1284f01691960de8adea47ae010cdeeca [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>
Willy Tarreaub7a67142016-12-21 21:18:44 +010013#include <51Degrees.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +020014
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
Willy Tarreaub7a67142016-12-21 21:18:44 +010025static struct {
26 char property_separator; /* the separator to use in the response for the values. this is taken from 51degrees-property-separator from config. */
27 struct list property_names; /* list of properties to load into the data set. this is taken from 51degrees-property-name-list from config. */
28 char *data_file_path;
29 int header_count; /* number of HTTP headers related to device detection. */
30 struct chunk *header_names; /* array of HTTP header names. */
31 fiftyoneDegreesDataSet data_set; /* data set used with the pattern and trie detection methods. */
32#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
33 fiftyoneDegreesWorksetPool *pool; /* pool of worksets to avoid creating a new one for each request. */
34#endif
35#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
36 int32_t *header_offsets; /* offsets to the HTTP header name string. */
37 fiftyoneDegreesDeviceOffsets device_offsets; /* Memory used for device offsets. */
38#endif
39 int cache_size;
40} global_51degrees = {
41 .property_separator = ',',
42 .property_names = LIST_HEAD_INIT(global_51degrees.property_names),
43 .data_file_path = NULL,
44#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
45 .data_set = { },
46#endif
47 .cache_size = 0,
48};
49
Dragan Dosen93b38d92015-06-29 16:43:25 +020050static int _51d_data_file(char **args, int section_type, struct proxy *curpx,
51 struct proxy *defpx, const char *file, int line,
52 char **err)
53{
54 if (*(args[1]) == 0) {
55 memprintf(err,
56 "'%s' expects a filepath to a 51Degrees trie or pattern data file.",
57 args[0]);
58 return -1;
59 }
60
Willy Tarreaub7a67142016-12-21 21:18:44 +010061 if (global_51degrees.data_file_path)
62 free(global_51degrees.data_file_path);
63 global_51degrees.data_file_path = strdup(args[1]);
Dragan Dosen93b38d92015-06-29 16:43:25 +020064
65 return 0;
66}
67
68static int _51d_property_name_list(char **args, int section_type, struct proxy *curpx,
James Rosewella28bbd52015-09-18 18:28:52 +010069 struct proxy *defpx, const char *file, int line,
70 char **err)
Dragan Dosen93b38d92015-06-29 16:43:25 +020071{
72 int cur_arg = 1;
73 struct _51d_property_names *name;
74
75 if (*(args[cur_arg]) == 0) {
76 memprintf(err,
77 "'%s' expects at least one 51Degrees property name.",
78 args[0]);
79 return -1;
80 }
81
82 while (*(args[cur_arg])) {
Vincent Bernat02779b62016-04-03 13:48:43 +020083 name = calloc(1, sizeof(*name));
Dragan Dosen93b38d92015-06-29 16:43:25 +020084 name->name = strdup(args[cur_arg]);
Willy Tarreaub7a67142016-12-21 21:18:44 +010085 LIST_ADDQ(&global_51degrees.property_names, &name->list);
Dragan Dosen93b38d92015-06-29 16:43:25 +020086 ++cur_arg;
87 }
88
89 return 0;
90}
91
92static int _51d_property_separator(char **args, int section_type, struct proxy *curpx,
93 struct proxy *defpx, const char *file, int line,
94 char **err)
95{
96 if (*(args[1]) == 0) {
97 memprintf(err,
98 "'%s' expects a single character.",
99 args[0]);
100 return -1;
101 }
102 if (strlen(args[1]) > 1) {
103 memprintf(err,
104 "'%s' expects a single character, got '%s'.",
105 args[0], args[1]);
106 return -1;
107 }
108
Willy Tarreaub7a67142016-12-21 21:18:44 +0100109 global_51degrees.property_separator = *args[1];
Dragan Dosen93b38d92015-06-29 16:43:25 +0200110
111 return 0;
112}
113
Dragan Dosen105c8e62015-06-29 16:43:26 +0200114static int _51d_cache_size(char **args, int section_type, struct proxy *curpx,
115 struct proxy *defpx, const char *file, int line,
116 char **err)
117{
118 if (*(args[1]) == 0) {
119 memprintf(err,
120 "'%s' expects a positive numeric value.",
121 args[0]);
122 return -1;
123 }
124
Willy Tarreaub7a67142016-12-21 21:18:44 +0100125 global_51degrees.cache_size = atoi(args[1]);
126 if (global_51degrees.cache_size < 0) {
Dragan Dosen105c8e62015-06-29 16:43:26 +0200127 memprintf(err,
128 "'%s' expects a positive numeric value, got '%s'.",
129 args[0], args[1]);
130 return -1;
131 }
132
James Rosewella28bbd52015-09-18 18:28:52 +0100133 return 0;
134}
135
136static int _51d_fetch_check(struct arg *arg, char **err_msg)
137{
Willy Tarreaub7a67142016-12-21 21:18:44 +0100138 if (global_51degrees.data_file_path)
James Rosewella28bbd52015-09-18 18:28:52 +0100139 return 1;
140
141 memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
Dragan Dosen105c8e62015-06-29 16:43:26 +0200142 return 0;
143}
144
Dragan Dosen9373fc52015-08-07 16:41:23 +0200145static int _51d_conv_check(struct arg *arg, struct sample_conv *conv,
James Rosewella28bbd52015-09-18 18:28:52 +0100146 const char *file, int line, char **err_msg)
Dragan Dosen9373fc52015-08-07 16:41:23 +0200147{
Willy Tarreaub7a67142016-12-21 21:18:44 +0100148 if (global_51degrees.data_file_path)
Dragan Dosen9373fc52015-08-07 16:41:23 +0200149 return 1;
150
James Rosewella28bbd52015-09-18 18:28:52 +0100151 memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
Dragan Dosen9373fc52015-08-07 16:41:23 +0200152 return 0;
153}
154
James Rosewella28bbd52015-09-18 18:28:52 +0100155#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000156static void _51d_lru_free(void *cache_entry)
157{
158 struct chunk *ptr = cache_entry;
159
160 if (!ptr)
161 return;
162
163 free(ptr->str);
164 free(ptr);
165}
166
167/* Allocates memory freeing space in the cache if necessary.
168*/
169static void *_51d_malloc(int size)
170{
171 void *ptr = malloc(size);
172
173 if (!ptr) {
174 /* free the oldest 10 entries from lru to free up some memory
175 * then try allocating memory again */
176 lru64_kill_oldest(_51d_lru_tree, 10);
177 ptr = malloc(size);
178 }
179
180 return ptr;
181}
182
James Rosewell63426cb2015-09-18 19:53:05 +0100183/* Insert the data associated with the sample into the cache as a fresh item.
184 */
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000185static void _51d_insert_cache_entry(struct sample *smp, struct lru64 *lru, void* domain)
James Rosewell63426cb2015-09-18 19:53:05 +0100186{
Vincent Bernat02779b62016-04-03 13:48:43 +0200187 struct chunk *cache_entry = _51d_malloc(sizeof(*cache_entry));
James Rosewell63426cb2015-09-18 19:53:05 +0100188
189 if (!cache_entry)
190 return;
191
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000192 cache_entry->str = _51d_malloc(smp->data.u.str.len + 1);
193 if (!cache_entry->str) {
194 free(cache_entry);
James Rosewell63426cb2015-09-18 19:53:05 +0100195 return;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000196 }
James Rosewell63426cb2015-09-18 19:53:05 +0100197
198 memcpy(cache_entry->str, smp->data.u.str.str, smp->data.u.str.len);
199 cache_entry->str[smp->data.u.str.len] = 0;
200 cache_entry->len = smp->data.u.str.len;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000201 lru64_commit(lru, cache_entry, domain, 0, _51d_lru_free);
James Rosewell63426cb2015-09-18 19:53:05 +0100202}
203
204/* Retrieves the data from the cache and sets the sample data to this string.
205 */
206static void _51d_retrieve_cache_entry(struct sample *smp, struct lru64 *lru)
207{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200208 struct chunk *cache_entry = lru->data;
James Rosewell63426cb2015-09-18 19:53:05 +0100209 smp->data.u.str.str = cache_entry->str;
210 smp->data.u.str.len = cache_entry->len;
211}
212#endif
213
214#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100215/* Sets the important HTTP headers ahead of the detection
216 */
217static void _51d_set_headers(struct sample *smp, fiftyoneDegreesWorkset *ws)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200218{
James Rosewella28bbd52015-09-18 18:28:52 +0100219 struct hdr_idx *idx;
220 struct hdr_ctx ctx;
221 const struct http_msg *msg;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200222 int i;
James Rosewella28bbd52015-09-18 18:28:52 +0100223
224 idx = &smp->strm->txn->hdr_idx;
225 msg = &smp->strm->txn->req;
226
227 ws->importantHeadersCount = 0;
228
Willy Tarreaub7a67142016-12-21 21:18:44 +0100229 for (i = 0; i < global_51degrees.header_count; i++) {
James Rosewella28bbd52015-09-18 18:28:52 +0100230 ctx.idx = 0;
231 if (http_find_full_header2(
Willy Tarreaub7a67142016-12-21 21:18:44 +0100232 (global_51degrees.header_names + i)->str,
233 (global_51degrees.header_names + i)->len,
James Rosewella28bbd52015-09-18 18:28:52 +0100234 msg->chn->buf->p, idx, &ctx) == 1) {
235 ws->importantHeaders[ws->importantHeadersCount].header = ws->dataSet->httpHeaders + i;
236 ws->importantHeaders[ws->importantHeadersCount].headerValue = ctx.line + ctx.val;
237 ws->importantHeaders[ws->importantHeadersCount].headerValueLength = ctx.vlen;
238 ws->importantHeadersCount++;
239 }
240 }
241}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200242#endif
James Rosewella28bbd52015-09-18 18:28:52 +0100243
Dragan Dosen93b38d92015-06-29 16:43:25 +0200244#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100245static void _51d_set_device_offsets(struct sample *smp)
246{
247 struct hdr_idx *idx;
248 struct hdr_ctx ctx;
249 const struct http_msg *msg;
250 int index;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100251 fiftyoneDegreesDeviceOffsets *offsets = &global_51degrees.device_offsets;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200252
James Rosewella28bbd52015-09-18 18:28:52 +0100253 idx = &smp->strm->txn->hdr_idx;
254 msg = &smp->strm->txn->req;
255 offsets->size = 0;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200256
Willy Tarreaub7a67142016-12-21 21:18:44 +0100257 for (index = 0; index < global_51degrees.header_count; index++) {
James Rosewella28bbd52015-09-18 18:28:52 +0100258 ctx.idx = 0;
259 if (http_find_full_header2(
Willy Tarreaub7a67142016-12-21 21:18:44 +0100260 (global_51degrees.header_names + index)->str,
261 (global_51degrees.header_names + index)->len,
James Rosewella28bbd52015-09-18 18:28:52 +0100262 msg->chn->buf->p, idx, &ctx) == 1) {
Willy Tarreaub7a67142016-12-21 21:18:44 +0100263 (offsets->firstOffset + offsets->size)->httpHeaderOffset = *(global_51degrees.header_offsets + index);
264 (offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set, ctx.line + ctx.val);
James Rosewella28bbd52015-09-18 18:28:52 +0100265 offsets->size++;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200266 }
267 }
James Rosewella28bbd52015-09-18 18:28:52 +0100268}
269#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200270
271#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100272/* Provides a hash code for the important HTTP headers.
273 */
274unsigned long long _51d_req_hash(const struct arg *args, fiftyoneDegreesWorkset* ws)
275{
276 unsigned long long seed = _51d_lru_seed ^ (long)args;
277 unsigned long long hash = 0;
278 int i;
279 for(i = 0; i < ws->importantHeadersCount; i++) {
280 hash ^= ws->importantHeaders[i].header->headerNameOffset;
281 hash ^= XXH64(ws->importantHeaders[i].headerValue,
282 ws->importantHeaders[i].headerValueLength,
283 seed);
284 }
285 return hash;
286}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200287#endif
288
Dragan Dosen93b38d92015-06-29 16:43:25 +0200289#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100290static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesWorkset* ws)
291{
292 char *methodName;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200293#endif
294#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100295static void _51d_process_match(const struct arg *args, struct sample *smp)
296{
297 char valuesBuffer[1024];
Willy Tarreaub7a67142016-12-21 21:18:44 +0100298 const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(&global_51degrees.data_set);
299 int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(&global_51degrees.data_set);
300 fiftyoneDegreesDeviceOffsets *deviceOffsets = &global_51degrees.device_offsets;
James Rosewella28bbd52015-09-18 18:28:52 +0100301
Dragan Dosen93b38d92015-06-29 16:43:25 +0200302#endif
303
James Rosewella28bbd52015-09-18 18:28:52 +0100304 char no_data[] = "NoData"; /* response when no data could be found */
305 struct chunk *temp = get_trash_chunk();
306 int j, i = 0, found;
307 const char* property_name;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200308
309 /* Loop through property names passed to the filter and fetch them from the dataset. */
310 while (args[i].data.str.str) {
311 /* Try to find request property in dataset. */
Dragan Dosen93b38d92015-06-29 16:43:25 +0200312 found = 0;
James Rosewella28bbd52015-09-18 18:28:52 +0100313#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
314 if (strcmp("Method", args[i].data.str.str) == 0) {
315 switch(ws->method) {
316 case EXACT: methodName = "Exact"; break;
317 case NUMERIC: methodName = "Numeric"; break;
318 case NEAREST: methodName = "Nearest"; break;
319 case CLOSEST: methodName = "Closest"; break;
320 default:
321 case NONE: methodName = "None"; break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200322 }
James Rosewella28bbd52015-09-18 18:28:52 +0100323 chunk_appendf(temp, "%s", methodName);
324 found = 1;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200325 }
James Rosewella28bbd52015-09-18 18:28:52 +0100326 else if (strcmp("Difference", args[i].data.str.str) == 0) {
327 chunk_appendf(temp, "%d", ws->difference);
328 found = 1;
329 }
330 else if (strcmp("Rank", args[i].data.str.str) == 0) {
331 chunk_appendf(temp, "%d", fiftyoneDegreesGetSignatureRank(ws));
332 found = 1;
333 }
334 else {
335 for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) {
336 property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]);
337 if (strcmp(property_name, args[i].data.str.str) == 0) {
338 found = 1;
339 fiftyoneDegreesSetValues(ws, j);
340 chunk_appendf(temp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values));
341 break;
342 }
343 }
Dragan Dosen93b38d92015-06-29 16:43:25 +0200344 }
345#endif
346#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100347 found = 0;
348 for (j = 0; j < requiredPropertiesCount; j++) {
349 property_name = requiredProperties[j];
350 if (strcmp(property_name, args[i].data.str.str) == 0 &&
Willy Tarreaub7a67142016-12-21 21:18:44 +0100351 fiftyoneDegreesGetValueFromOffsets(&global_51degrees.data_set, deviceOffsets, j, valuesBuffer, 1024) > 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100352 found = 1;
353 chunk_appendf(temp, "%s", valuesBuffer);
354 break;
355 }
Dragan Dosen93b38d92015-06-29 16:43:25 +0200356 }
James Rosewella28bbd52015-09-18 18:28:52 +0100357#endif
ben@51degrees.comd3842522016-01-08 13:52:32 +0000358 if (!found)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200359 chunk_appendf(temp, "%s", no_data);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000360
Dragan Dosen93b38d92015-06-29 16:43:25 +0200361 /* Add separator. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100362 chunk_appendf(temp, "%c", global_51degrees.property_separator);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200363 ++i;
364 }
365
366 if (temp->len) {
367 --temp->len;
368 temp->str[temp->len] = '\0';
369 }
370
Thierry FOURNIER2046c462015-08-20 13:42:12 +0200371 smp->data.u.str.str = temp->str;
James Rosewell63426cb2015-09-18 19:53:05 +0100372 smp->data.u.str.len = temp->len;
James Rosewella28bbd52015-09-18 18:28:52 +0100373}
374
375static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
376{
377#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
378 fiftyoneDegreesWorkset* ws; /* workset for detection */
379 struct lru64 *lru = NULL;
380#endif
381
382 /* Needed to ensure that the HTTP message has been fully recieved when
383 * used with TCP operation. Not required for HTTP operation.
384 * Data type has to be reset to ensure the string output is processed
385 * correctly.
386 */
387 CHECK_HTTP_MESSAGE_FIRST();
388 smp->data.type = SMP_T_STR;
389
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000390 /* Flags the sample to show it uses constant memory*/
391 smp->flags |= SMP_F_CONST;
392
James Rosewella28bbd52015-09-18 18:28:52 +0100393#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
394
395 /* Get only the headers needed for device detection so they can be used
396 * with the cache to return previous results. Pattern is slower than
397 * Trie so caching will help improve performance.
398 */
399
400 /* Get a workset from the pool which will later contain detection results. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100401 ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool);
James Rosewella28bbd52015-09-18 18:28:52 +0100402 if (!ws)
403 return 0;
404
405 /* Set the important HTTP headers for this request in the workset. */
406 _51d_set_headers(smp, ws);
407
408 /* Check the cache to see if there's results for these headers already. */
409 if (_51d_lru_tree) {
410 lru = lru64_get(_51d_req_hash(args, ws),
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000411 _51d_lru_tree, (void*)args, 0);
James Rosewella28bbd52015-09-18 18:28:52 +0100412 if (lru && lru->domain) {
Willy Tarreaub7a67142016-12-21 21:18:44 +0100413 fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
James Rosewell63426cb2015-09-18 19:53:05 +0100414 _51d_retrieve_cache_entry(smp, lru);
James Rosewella28bbd52015-09-18 18:28:52 +0100415 return 1;
416 }
417 }
418
419 fiftyoneDegreesMatchForHttpHeaders(ws);
420
421 _51d_process_match(args, smp, ws);
422
423#endif
424
425#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
426
427 /* Trie is very fast so all the headers can be passed in and the result
428 * returned faster than the hashing algorithm process.
429 */
430 _51d_set_device_offsets(smp);
431 _51d_process_match(args, smp);
432
433#endif
434
435#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100436 fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000437 if (lru)
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000438 _51d_insert_cache_entry(smp, lru, (void*)args);
James Rosewella28bbd52015-09-18 18:28:52 +0100439#endif
440
441 return 1;
442}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200443
James Rosewella28bbd52015-09-18 18:28:52 +0100444static int _51d_conv(const struct arg *args, struct sample *smp, void *private)
445{
Dragan Dosen93b38d92015-06-29 16:43:25 +0200446#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100447 fiftyoneDegreesWorkset* ws; /* workset for detection */
448 struct lru64 *lru = NULL;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200449#endif
450
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000451 /* Flags the sample to show it uses constant memory*/
452 smp->flags |= SMP_F_CONST;
453
James Rosewella28bbd52015-09-18 18:28:52 +0100454#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
455
456 /* Look in the list. */
457 if (_51d_lru_tree) {
458 unsigned long long seed = _51d_lru_seed ^ (long)args;
459
460 lru = lru64_get(XXH64(smp->data.u.str.str, smp->data.u.str.len, seed),
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000461 _51d_lru_tree, (void*)args, 0);
James Rosewella28bbd52015-09-18 18:28:52 +0100462 if (lru && lru->domain) {
James Rosewell63426cb2015-09-18 19:53:05 +0100463 _51d_retrieve_cache_entry(smp, lru);
James Rosewella28bbd52015-09-18 18:28:52 +0100464 return 1;
465 }
466 }
467
468 /* Create workset. This will later contain detection results. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100469 ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool);
James Rosewella28bbd52015-09-18 18:28:52 +0100470 if (!ws)
471 return 0;
472#endif
473
474 /* Duplicate the data and remove the "const" flag before device detection. */
475 if (!smp_dup(smp))
476 return 0;
477
478 smp->data.u.str.str[smp->data.u.str.len] = '\0';
479
480 /* Perform detection. */
481#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
482 fiftyoneDegreesMatch(ws, smp->data.u.str.str);
483 _51d_process_match(args, smp, ws);
484#endif
485#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100486 global_51degrees.device_offsets.firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set, smp->data.u.str.str);
487 global_51degrees.device_offsets.size = 1;
James Rosewella28bbd52015-09-18 18:28:52 +0100488 _51d_process_match(args, smp);
489#endif
490
491#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100492 fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000493 if (lru)
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000494 _51d_insert_cache_entry(smp, lru, (void*)args);
James Rosewella28bbd52015-09-18 18:28:52 +0100495#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200496
Dragan Dosen93b38d92015-06-29 16:43:25 +0200497 return 1;
498}
499
James Rosewella28bbd52015-09-18 18:28:52 +0100500#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
501void _51d_init_http_headers()
502{
503 int index = 0;
504 const fiftyoneDegreesAsciiString *headerName;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100505 fiftyoneDegreesDataSet *ds = &global_51degrees.data_set;
506 global_51degrees.header_count = ds->httpHeadersCount;
507 global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct chunk));
508 for (index = 0; index < global_51degrees.header_count; index++) {
James Rosewella28bbd52015-09-18 18:28:52 +0100509 headerName = fiftyoneDegreesGetString(ds, ds->httpHeaders[index].headerNameOffset);
Willy Tarreaub7a67142016-12-21 21:18:44 +0100510 (global_51degrees.header_names + index)->str = (char*)&headerName->firstByte;
511 (global_51degrees.header_names + index)->len = headerName->length - 1;
512 (global_51degrees.header_names + index)->size = (global_51degrees.header_names + index)->len;
James Rosewella28bbd52015-09-18 18:28:52 +0100513 }
514}
515#endif
516
517#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
518void _51d_init_http_headers()
519{
520 int index = 0;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100521 fiftyoneDegreesDataSet *ds = &global_51degrees.data_set;
522 global_51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(ds);
523 global_51degrees.device_offsets.firstOffset = malloc(
524 global_51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset));
525 global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct chunk));
526 global_51degrees.header_offsets = malloc(global_51degrees.header_count * sizeof(int32_t));
527 for (index = 0; index < global_51degrees.header_count; index++) {
528 global_51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(ds, index);
529 global_51degrees.header_names[index].str = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(ds, index);
530 global_51degrees.header_names[index].len = strlen(global_51degrees.header_names[index].str);
531 global_51degrees.header_names[index].size = global_51degrees.header_names[index].len;
James Rosewella28bbd52015-09-18 18:28:52 +0100532 }
533}
534#endif
535
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100536/*
537 * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*.
538 */
539static int init_51degrees(void)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200540{
541 int i = 0;
542 struct chunk *temp;
543 struct _51d_property_names *name;
544 char **_51d_property_list = NULL;
545 fiftyoneDegreesDataSetInitStatus _51d_dataset_status = DATA_SET_INIT_STATUS_NOT_SET;
546
Willy Tarreaub7a67142016-12-21 21:18:44 +0100547 if (!global_51degrees.data_file_path)
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100548 return 0;
Dragan Dosen9373fc52015-08-07 16:41:23 +0200549
Willy Tarreaub7a67142016-12-21 21:18:44 +0100550 if (!LIST_ISEMPTY(&global_51degrees.property_names)) {
Dragan Dosen93b38d92015-06-29 16:43:25 +0200551 i = 0;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100552 list_for_each_entry(name, &global_51degrees.property_names, list)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200553 ++i;
554 _51d_property_list = calloc(i, sizeof(char *));
555
556 i = 0;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100557 list_for_each_entry(name, &global_51degrees.property_names, list)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200558 _51d_property_list[i++] = name->name;
559 }
560
Willy Tarreaub7a67142016-12-21 21:18:44 +0100561 _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 +0200562
563 temp = get_trash_chunk();
564 chunk_reset(temp);
565
566 switch (_51d_dataset_status) {
567 case DATA_SET_INIT_STATUS_SUCCESS:
James Rosewella28bbd52015-09-18 18:28:52 +0100568#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
569 /* only 1 workset in the pool because HAProxy is currently single threaded
570 * this value should be set to the number of threads in future versions.
571 */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100572 global_51degrees.pool = fiftyoneDegreesWorksetPoolCreate(&global_51degrees.data_set, NULL, 1);
James Rosewella28bbd52015-09-18 18:28:52 +0100573#endif
574 _51d_init_http_headers();
Dragan Dosen93b38d92015-06-29 16:43:25 +0200575 break;
576 case DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY:
577 chunk_printf(temp, "Insufficient memory.");
578 break;
579 case DATA_SET_INIT_STATUS_CORRUPT_DATA:
580#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
581 chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Pattern data format.");
582#endif
583#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
584 chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Trie data format.");
585#endif
586 break;
587 case DATA_SET_INIT_STATUS_INCORRECT_VERSION:
588#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
589 chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Pattern data format.");
590#endif
591#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
592 chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Trie data format.");
593#endif
594 break;
595 case DATA_SET_INIT_STATUS_FILE_NOT_FOUND:
596 chunk_printf(temp, "File not found.");
597 break;
ben51degrees1f077eb2016-07-06 12:07:21 +0100598 case DATA_SET_INIT_STATUS_NULL_POINTER:
599 chunk_printf(temp, "Null pointer to the existing dataset or memory location.");
600 break;
601 case DATA_SET_INIT_STATUS_POINTER_OUT_OF_BOUNDS:
602 chunk_printf(temp, "Allocated continuous memory containing 51Degrees data file appears to be smaller than expected. Most likely"
603 " because the data file was not fully loaded into the allocated memory.");
604 break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200605 case DATA_SET_INIT_STATUS_NOT_SET:
606 chunk_printf(temp, "Data set not initialised.");
607 break;
608 }
609 if (_51d_dataset_status != DATA_SET_INIT_STATUS_SUCCESS) {
610 if (temp->len)
611 Alert("51Degrees Setup - Error reading 51Degrees data file. %s\n", temp->str);
612 else
613 Alert("51Degrees Setup - Error reading 51Degrees data file.\n");
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100614 return ERR_ALERT | ERR_FATAL;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200615 }
616 free(_51d_property_list);
617
James Rosewella28bbd52015-09-18 18:28:52 +0100618#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200619 _51d_lru_seed = random();
Willy Tarreaub7a67142016-12-21 21:18:44 +0100620 if (global_51degrees.cache_size) {
621 _51d_lru_tree = lru64_new(global_51degrees.cache_size);
James Rosewella28bbd52015-09-18 18:28:52 +0100622 }
623#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200624
Dragan Dosen93b38d92015-06-29 16:43:25 +0200625 return 0;
626}
627
Willy Tarreau7ac4c202016-12-21 20:59:01 +0100628static void deinit_51degrees(void)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200629{
630 struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb;
631
Willy Tarreaub7a67142016-12-21 21:18:44 +0100632 free(global_51degrees.header_names);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200633#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100634 fiftyoneDegreesWorksetPoolFree(global_51degrees.pool);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200635#endif
636#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100637 free(global_51degrees.device_offsets.firstOffset);
638 free(global_51degrees.header_offsets);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200639#endif
Willy Tarreaub7a67142016-12-21 21:18:44 +0100640 fiftyoneDegreesDataSetFree(&global_51degrees.data_set);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200641
Willy Tarreaub7a67142016-12-21 21:18:44 +0100642 free(global_51degrees.data_file_path); global_51degrees.data_file_path = NULL;
643 list_for_each_entry_safe(_51d_prop_name, _51d_prop_nameb, &global_51degrees.property_names, list) {
Dragan Dosen93b38d92015-06-29 16:43:25 +0200644 LIST_DEL(&_51d_prop_name->list);
645 free(_51d_prop_name);
646 }
Dragan Dosen105c8e62015-06-29 16:43:26 +0200647
James Rosewella28bbd52015-09-18 18:28:52 +0100648#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200649 while (lru64_destroy(_51d_lru_tree));
James Rosewella28bbd52015-09-18 18:28:52 +0100650#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200651}
652
653static struct cfg_kw_list _51dcfg_kws = {{ }, {
654 { CFG_GLOBAL, "51degrees-data-file", _51d_data_file },
655 { CFG_GLOBAL, "51degrees-property-name-list", _51d_property_name_list },
656 { CFG_GLOBAL, "51degrees-property-separator", _51d_property_separator },
Dragan Dosen105c8e62015-06-29 16:43:26 +0200657 { CFG_GLOBAL, "51degrees-cache-size", _51d_cache_size },
Dragan Dosen93b38d92015-06-29 16:43:25 +0200658 { 0, NULL, NULL },
659}};
660
661/* Note: must not be declared <const> as its list will be overwritten */
James Rosewella28bbd52015-09-18 18:28:52 +0100662static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
663 { "51d.all", _51d_fetch, ARG5(1,STR,STR,STR,STR,STR), _51d_fetch_check, SMP_T_STR, SMP_USE_HRQHV },
664 { NULL, NULL, 0, 0, 0 },
665}};
666
667/* Note: must not be declared <const> as its list will be overwritten */
Dragan Dosen93b38d92015-06-29 16:43:25 +0200668static struct sample_conv_kw_list conv_kws = {ILH, {
James Rosewella28bbd52015-09-18 18:28:52 +0100669 { "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 +0200670 { NULL, NULL, 0, 0, 0 },
671}};
672
673__attribute__((constructor))
674static void __51d_init(void)
675{
James Rosewella28bbd52015-09-18 18:28:52 +0100676 /* register sample fetch and conversion keywords */
677 sample_register_fetches(&sample_fetch_keywords);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200678 sample_register_convs(&conv_kws);
679 cfg_register_keywords(&_51dcfg_kws);
Willy Tarreaub5e58d62016-12-21 18:49:29 +0100680 hap_register_build_opts("Built with 51Degrees support.", 0);
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100681 hap_register_post_check(init_51degrees);
Willy Tarreau7ac4c202016-12-21 20:59:01 +0100682 hap_register_post_deinit(deinit_51degrees);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200683}