blob: d709ec0e7c2db4ed050f78567940796cec18e062 [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>
Willy Tarreau0108d902018-11-25 19:14:37 +01007#include <common/initcall.h>
Willy Tarreau80713382018-11-26 10:19:54 +01008#include <types/global.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +02009#include <proto/arg.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020010#include <proto/http_fetch.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +020011#include <proto/log.h>
James Rosewella28bbd52015-09-18 18:28:52 +010012#include <proto/proto_http.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +020013#include <proto/sample.h>
14#include <import/xxhash.h>
Dragan Dosen105c8e62015-06-29 16:43:26 +020015#include <import/lru.h>
Willy Tarreaub7a67142016-12-21 21:18:44 +010016#include <51Degrees.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +020017
18struct _51d_property_names {
19 struct list list;
20 char *name;
21};
22
James Rosewella28bbd52015-09-18 18:28:52 +010023#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +020024static struct lru64_head *_51d_lru_tree = NULL;
25static unsigned long long _51d_lru_seed;
James Rosewella28bbd52015-09-18 18:28:52 +010026#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +020027
Willy Tarreaub7a67142016-12-21 21:18:44 +010028static struct {
29 char property_separator; /* the separator to use in the response for the values. this is taken from 51degrees-property-separator from config. */
30 struct list property_names; /* list of properties to load into the data set. this is taken from 51degrees-property-name-list from config. */
31 char *data_file_path;
32 int header_count; /* number of HTTP headers related to device detection. */
Willy Tarreau83061a82018-07-13 11:56:34 +020033 struct buffer *header_names; /* array of HTTP header names. */
Willy Tarreaub7a67142016-12-21 21:18:44 +010034 fiftyoneDegreesDataSet data_set; /* data set used with the pattern and trie detection methods. */
35#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
36 fiftyoneDegreesWorksetPool *pool; /* pool of worksets to avoid creating a new one for each request. */
37#endif
38#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
39 int32_t *header_offsets; /* offsets to the HTTP header name string. */
40 fiftyoneDegreesDeviceOffsets device_offsets; /* Memory used for device offsets. */
41#endif
42 int cache_size;
43} global_51degrees = {
44 .property_separator = ',',
45 .property_names = LIST_HEAD_INIT(global_51degrees.property_names),
46 .data_file_path = NULL,
47#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
48 .data_set = { },
49#endif
50 .cache_size = 0,
51};
52
Dragan Dosen93b38d92015-06-29 16:43:25 +020053static int _51d_data_file(char **args, int section_type, struct proxy *curpx,
54 struct proxy *defpx, const char *file, int line,
55 char **err)
56{
57 if (*(args[1]) == 0) {
58 memprintf(err,
59 "'%s' expects a filepath to a 51Degrees trie or pattern data file.",
60 args[0]);
61 return -1;
62 }
63
Willy Tarreaub7a67142016-12-21 21:18:44 +010064 if (global_51degrees.data_file_path)
65 free(global_51degrees.data_file_path);
66 global_51degrees.data_file_path = strdup(args[1]);
Dragan Dosen93b38d92015-06-29 16:43:25 +020067
68 return 0;
69}
70
71static int _51d_property_name_list(char **args, int section_type, struct proxy *curpx,
James Rosewella28bbd52015-09-18 18:28:52 +010072 struct proxy *defpx, const char *file, int line,
73 char **err)
Dragan Dosen93b38d92015-06-29 16:43:25 +020074{
75 int cur_arg = 1;
76 struct _51d_property_names *name;
77
78 if (*(args[cur_arg]) == 0) {
79 memprintf(err,
80 "'%s' expects at least one 51Degrees property name.",
81 args[0]);
82 return -1;
83 }
84
85 while (*(args[cur_arg])) {
Vincent Bernat02779b62016-04-03 13:48:43 +020086 name = calloc(1, sizeof(*name));
Dragan Dosen93b38d92015-06-29 16:43:25 +020087 name->name = strdup(args[cur_arg]);
Willy Tarreaub7a67142016-12-21 21:18:44 +010088 LIST_ADDQ(&global_51degrees.property_names, &name->list);
Dragan Dosen93b38d92015-06-29 16:43:25 +020089 ++cur_arg;
90 }
91
92 return 0;
93}
94
95static int _51d_property_separator(char **args, int section_type, struct proxy *curpx,
96 struct proxy *defpx, const char *file, int line,
97 char **err)
98{
99 if (*(args[1]) == 0) {
100 memprintf(err,
101 "'%s' expects a single character.",
102 args[0]);
103 return -1;
104 }
105 if (strlen(args[1]) > 1) {
106 memprintf(err,
107 "'%s' expects a single character, got '%s'.",
108 args[0], args[1]);
109 return -1;
110 }
111
Willy Tarreaub7a67142016-12-21 21:18:44 +0100112 global_51degrees.property_separator = *args[1];
Dragan Dosen93b38d92015-06-29 16:43:25 +0200113
114 return 0;
115}
116
Dragan Dosen105c8e62015-06-29 16:43:26 +0200117static int _51d_cache_size(char **args, int section_type, struct proxy *curpx,
118 struct proxy *defpx, const char *file, int line,
119 char **err)
120{
121 if (*(args[1]) == 0) {
122 memprintf(err,
123 "'%s' expects a positive numeric value.",
124 args[0]);
125 return -1;
126 }
127
Willy Tarreaub7a67142016-12-21 21:18:44 +0100128 global_51degrees.cache_size = atoi(args[1]);
129 if (global_51degrees.cache_size < 0) {
Dragan Dosen105c8e62015-06-29 16:43:26 +0200130 memprintf(err,
131 "'%s' expects a positive numeric value, got '%s'.",
132 args[0], args[1]);
133 return -1;
134 }
135
James Rosewella28bbd52015-09-18 18:28:52 +0100136 return 0;
137}
138
139static int _51d_fetch_check(struct arg *arg, char **err_msg)
140{
Willy Tarreaub7a67142016-12-21 21:18:44 +0100141 if (global_51degrees.data_file_path)
James Rosewella28bbd52015-09-18 18:28:52 +0100142 return 1;
143
144 memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
Dragan Dosen105c8e62015-06-29 16:43:26 +0200145 return 0;
146}
147
Dragan Dosen9373fc52015-08-07 16:41:23 +0200148static int _51d_conv_check(struct arg *arg, struct sample_conv *conv,
James Rosewella28bbd52015-09-18 18:28:52 +0100149 const char *file, int line, char **err_msg)
Dragan Dosen9373fc52015-08-07 16:41:23 +0200150{
Willy Tarreaub7a67142016-12-21 21:18:44 +0100151 if (global_51degrees.data_file_path)
Dragan Dosen9373fc52015-08-07 16:41:23 +0200152 return 1;
153
James Rosewella28bbd52015-09-18 18:28:52 +0100154 memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
Dragan Dosen9373fc52015-08-07 16:41:23 +0200155 return 0;
156}
157
James Rosewella28bbd52015-09-18 18:28:52 +0100158#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000159static void _51d_lru_free(void *cache_entry)
160{
Willy Tarreau83061a82018-07-13 11:56:34 +0200161 struct buffer *ptr = cache_entry;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000162
163 if (!ptr)
164 return;
165
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200166 free(ptr->area);
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000167 free(ptr);
168}
169
170/* Allocates memory freeing space in the cache if necessary.
171*/
172static void *_51d_malloc(int size)
173{
174 void *ptr = malloc(size);
175
176 if (!ptr) {
177 /* free the oldest 10 entries from lru to free up some memory
178 * then try allocating memory again */
179 lru64_kill_oldest(_51d_lru_tree, 10);
180 ptr = malloc(size);
181 }
182
183 return ptr;
184}
185
James Rosewell63426cb2015-09-18 19:53:05 +0100186/* Insert the data associated with the sample into the cache as a fresh item.
187 */
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000188static void _51d_insert_cache_entry(struct sample *smp, struct lru64 *lru, void* domain)
James Rosewell63426cb2015-09-18 19:53:05 +0100189{
Willy Tarreau83061a82018-07-13 11:56:34 +0200190 struct buffer *cache_entry = _51d_malloc(sizeof(*cache_entry));
James Rosewell63426cb2015-09-18 19:53:05 +0100191
192 if (!cache_entry)
193 return;
194
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200195 cache_entry->area = _51d_malloc(smp->data.u.str.data + 1);
196 if (!cache_entry->area) {
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000197 free(cache_entry);
James Rosewell63426cb2015-09-18 19:53:05 +0100198 return;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000199 }
James Rosewell63426cb2015-09-18 19:53:05 +0100200
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200201 memcpy(cache_entry->area, smp->data.u.str.area, smp->data.u.str.data);
202 cache_entry->area[smp->data.u.str.data] = 0;
203 cache_entry->data = smp->data.u.str.data;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000204 lru64_commit(lru, cache_entry, domain, 0, _51d_lru_free);
James Rosewell63426cb2015-09-18 19:53:05 +0100205}
206
207/* Retrieves the data from the cache and sets the sample data to this string.
208 */
209static void _51d_retrieve_cache_entry(struct sample *smp, struct lru64 *lru)
210{
Willy Tarreau83061a82018-07-13 11:56:34 +0200211 struct buffer *cache_entry = lru->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200212 smp->data.u.str.area = cache_entry->area;
213 smp->data.u.str.data = cache_entry->data;
James Rosewell63426cb2015-09-18 19:53:05 +0100214}
215#endif
216
217#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100218/* Sets the important HTTP headers ahead of the detection
219 */
220static void _51d_set_headers(struct sample *smp, fiftyoneDegreesWorkset *ws)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200221{
James Rosewella28bbd52015-09-18 18:28:52 +0100222 struct hdr_idx *idx;
223 struct hdr_ctx ctx;
224 const struct http_msg *msg;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200225 int i;
James Rosewella28bbd52015-09-18 18:28:52 +0100226
227 idx = &smp->strm->txn->hdr_idx;
228 msg = &smp->strm->txn->req;
229
230 ws->importantHeadersCount = 0;
231
Willy Tarreaub7a67142016-12-21 21:18:44 +0100232 for (i = 0; i < global_51degrees.header_count; i++) {
James Rosewella28bbd52015-09-18 18:28:52 +0100233 ctx.idx = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200234 if (http_find_full_header2((global_51degrees.header_names + i)->area,
235 (global_51degrees.header_names + i)->data,
236 msg->chn->buf->p, idx, &ctx) == 1) {
James Rosewella28bbd52015-09-18 18:28:52 +0100237 ws->importantHeaders[ws->importantHeadersCount].header = ws->dataSet->httpHeaders + i;
238 ws->importantHeaders[ws->importantHeadersCount].headerValue = ctx.line + ctx.val;
239 ws->importantHeaders[ws->importantHeadersCount].headerValueLength = ctx.vlen;
240 ws->importantHeadersCount++;
241 }
242 }
243}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200244#endif
James Rosewella28bbd52015-09-18 18:28:52 +0100245
Dragan Dosen93b38d92015-06-29 16:43:25 +0200246#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100247static void _51d_set_device_offsets(struct sample *smp)
248{
249 struct hdr_idx *idx;
250 struct hdr_ctx ctx;
251 const struct http_msg *msg;
252 int index;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100253 fiftyoneDegreesDeviceOffsets *offsets = &global_51degrees.device_offsets;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200254
James Rosewella28bbd52015-09-18 18:28:52 +0100255 idx = &smp->strm->txn->hdr_idx;
256 msg = &smp->strm->txn->req;
257 offsets->size = 0;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200258
Willy Tarreaub7a67142016-12-21 21:18:44 +0100259 for (index = 0; index < global_51degrees.header_count; index++) {
James Rosewella28bbd52015-09-18 18:28:52 +0100260 ctx.idx = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200261 if (http_find_full_header2((global_51degrees.header_names + index)->area,
262 (global_51degrees.header_names + index)->data,
263 msg->chn->buf->p, idx, &ctx) == 1) {
Willy Tarreaub7a67142016-12-21 21:18:44 +0100264 (offsets->firstOffset + offsets->size)->httpHeaderOffset = *(global_51degrees.header_offsets + index);
265 (offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set, ctx.line + ctx.val);
James Rosewella28bbd52015-09-18 18:28:52 +0100266 offsets->size++;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200267 }
268 }
James Rosewella28bbd52015-09-18 18:28:52 +0100269}
270#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200271
272#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100273/* Provides a hash code for the important HTTP headers.
274 */
275unsigned long long _51d_req_hash(const struct arg *args, fiftyoneDegreesWorkset* ws)
276{
277 unsigned long long seed = _51d_lru_seed ^ (long)args;
278 unsigned long long hash = 0;
279 int i;
280 for(i = 0; i < ws->importantHeadersCount; i++) {
281 hash ^= ws->importantHeaders[i].header->headerNameOffset;
282 hash ^= XXH64(ws->importantHeaders[i].headerValue,
283 ws->importantHeaders[i].headerValueLength,
284 seed);
285 }
286 return hash;
287}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200288#endif
289
Dragan Dosen93b38d92015-06-29 16:43:25 +0200290#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100291static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesWorkset* ws)
292{
293 char *methodName;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200294#endif
295#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100296static void _51d_process_match(const struct arg *args, struct sample *smp)
297{
298 char valuesBuffer[1024];
Willy Tarreaub7a67142016-12-21 21:18:44 +0100299 const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(&global_51degrees.data_set);
300 int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(&global_51degrees.data_set);
301 fiftyoneDegreesDeviceOffsets *deviceOffsets = &global_51degrees.device_offsets;
James Rosewella28bbd52015-09-18 18:28:52 +0100302
Dragan Dosen93b38d92015-06-29 16:43:25 +0200303#endif
304
James Rosewella28bbd52015-09-18 18:28:52 +0100305 char no_data[] = "NoData"; /* response when no data could be found */
Willy Tarreau83061a82018-07-13 11:56:34 +0200306 struct buffer *temp = get_trash_chunk();
James Rosewella28bbd52015-09-18 18:28:52 +0100307 int j, i = 0, found;
308 const char* property_name;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200309
310 /* Loop through property names passed to the filter and fetch them from the dataset. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200311 while (args[i].data.str.area) {
Dragan Dosen93b38d92015-06-29 16:43:25 +0200312 /* Try to find request property in dataset. */
Dragan Dosen93b38d92015-06-29 16:43:25 +0200313 found = 0;
James Rosewella28bbd52015-09-18 18:28:52 +0100314#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200315 if (strcmp("Method", args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100316 switch(ws->method) {
317 case EXACT: methodName = "Exact"; break;
318 case NUMERIC: methodName = "Numeric"; break;
319 case NEAREST: methodName = "Nearest"; break;
320 case CLOSEST: methodName = "Closest"; break;
321 default:
322 case NONE: methodName = "None"; break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200323 }
James Rosewella28bbd52015-09-18 18:28:52 +0100324 chunk_appendf(temp, "%s", methodName);
325 found = 1;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200326 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200327 else if (strcmp("Difference", args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100328 chunk_appendf(temp, "%d", ws->difference);
329 found = 1;
330 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200331 else if (strcmp("Rank", args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100332 chunk_appendf(temp, "%d", fiftyoneDegreesGetSignatureRank(ws));
333 found = 1;
334 }
335 else {
336 for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) {
337 property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200338 if (strcmp(property_name, args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100339 found = 1;
340 fiftyoneDegreesSetValues(ws, j);
341 chunk_appendf(temp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values));
342 break;
343 }
344 }
Dragan Dosen93b38d92015-06-29 16:43:25 +0200345 }
346#endif
347#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100348 found = 0;
349 for (j = 0; j < requiredPropertiesCount; j++) {
350 property_name = requiredProperties[j];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200351 if (strcmp(property_name, args[i].data.str.area) == 0 &&
Willy Tarreaub7a67142016-12-21 21:18:44 +0100352 fiftyoneDegreesGetValueFromOffsets(&global_51degrees.data_set, deviceOffsets, j, valuesBuffer, 1024) > 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100353 found = 1;
354 chunk_appendf(temp, "%s", valuesBuffer);
355 break;
356 }
Dragan Dosen93b38d92015-06-29 16:43:25 +0200357 }
James Rosewella28bbd52015-09-18 18:28:52 +0100358#endif
ben@51degrees.comd3842522016-01-08 13:52:32 +0000359 if (!found)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200360 chunk_appendf(temp, "%s", no_data);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000361
Dragan Dosen93b38d92015-06-29 16:43:25 +0200362 /* Add separator. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100363 chunk_appendf(temp, "%c", global_51degrees.property_separator);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200364 ++i;
365 }
366
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200367 if (temp->data) {
368 --temp->data;
369 temp->area[temp->data] = '\0';
Dragan Dosen93b38d92015-06-29 16:43:25 +0200370 }
371
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200372 smp->data.u.str.area = temp->area;
373 smp->data.u.str.data = temp->data;
James Rosewella28bbd52015-09-18 18:28:52 +0100374}
375
376static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
377{
378#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
379 fiftyoneDegreesWorkset* ws; /* workset for detection */
380 struct lru64 *lru = NULL;
381#endif
382
383 /* Needed to ensure that the HTTP message has been fully recieved when
384 * used with TCP operation. Not required for HTTP operation.
385 * Data type has to be reset to ensure the string output is processed
386 * correctly.
387 */
388 CHECK_HTTP_MESSAGE_FIRST();
389 smp->data.type = SMP_T_STR;
390
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000391 /* Flags the sample to show it uses constant memory*/
392 smp->flags |= SMP_F_CONST;
393
James Rosewella28bbd52015-09-18 18:28:52 +0100394#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
395
396 /* Get only the headers needed for device detection so they can be used
397 * with the cache to return previous results. Pattern is slower than
398 * Trie so caching will help improve performance.
399 */
400
401 /* Get a workset from the pool which will later contain detection results. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100402 ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool);
James Rosewella28bbd52015-09-18 18:28:52 +0100403 if (!ws)
404 return 0;
405
406 /* Set the important HTTP headers for this request in the workset. */
407 _51d_set_headers(smp, ws);
408
409 /* Check the cache to see if there's results for these headers already. */
410 if (_51d_lru_tree) {
411 lru = lru64_get(_51d_req_hash(args, ws),
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000412 _51d_lru_tree, (void*)args, 0);
James Rosewella28bbd52015-09-18 18:28:52 +0100413 if (lru && lru->domain) {
Willy Tarreaub7a67142016-12-21 21:18:44 +0100414 fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
James Rosewell63426cb2015-09-18 19:53:05 +0100415 _51d_retrieve_cache_entry(smp, lru);
James Rosewella28bbd52015-09-18 18:28:52 +0100416 return 1;
417 }
418 }
419
420 fiftyoneDegreesMatchForHttpHeaders(ws);
421
422 _51d_process_match(args, smp, ws);
423
424#endif
425
426#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
427
428 /* Trie is very fast so all the headers can be passed in and the result
429 * returned faster than the hashing algorithm process.
430 */
431 _51d_set_device_offsets(smp);
432 _51d_process_match(args, smp);
433
434#endif
435
436#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100437 fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000438 if (lru)
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000439 _51d_insert_cache_entry(smp, lru, (void*)args);
James Rosewella28bbd52015-09-18 18:28:52 +0100440#endif
441
442 return 1;
443}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200444
James Rosewella28bbd52015-09-18 18:28:52 +0100445static int _51d_conv(const struct arg *args, struct sample *smp, void *private)
446{
Dragan Dosen93b38d92015-06-29 16:43:25 +0200447#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100448 fiftyoneDegreesWorkset* ws; /* workset for detection */
449 struct lru64 *lru = NULL;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200450#endif
451
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000452 /* Flags the sample to show it uses constant memory*/
453 smp->flags |= SMP_F_CONST;
454
James Rosewella28bbd52015-09-18 18:28:52 +0100455#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
456
457 /* Look in the list. */
458 if (_51d_lru_tree) {
459 unsigned long long seed = _51d_lru_seed ^ (long)args;
460
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200461 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000462 _51d_lru_tree, (void*)args, 0);
James Rosewella28bbd52015-09-18 18:28:52 +0100463 if (lru && lru->domain) {
James Rosewell63426cb2015-09-18 19:53:05 +0100464 _51d_retrieve_cache_entry(smp, lru);
James Rosewella28bbd52015-09-18 18:28:52 +0100465 return 1;
466 }
467 }
468
469 /* Create workset. This will later contain detection results. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100470 ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool);
James Rosewella28bbd52015-09-18 18:28:52 +0100471 if (!ws)
472 return 0;
473#endif
474
475 /* Duplicate the data and remove the "const" flag before device detection. */
476 if (!smp_dup(smp))
477 return 0;
478
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200479 smp->data.u.str.area[smp->data.u.str.data] = '\0';
James Rosewella28bbd52015-09-18 18:28:52 +0100480
481 /* Perform detection. */
482#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200483 fiftyoneDegreesMatch(ws, smp->data.u.str.area);
James Rosewella28bbd52015-09-18 18:28:52 +0100484 _51d_process_match(args, smp, ws);
485#endif
486#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200487 global_51degrees.device_offsets.firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set,
488 smp->data.u.str.area);
Willy Tarreaub7a67142016-12-21 21:18:44 +0100489 global_51degrees.device_offsets.size = 1;
James Rosewella28bbd52015-09-18 18:28:52 +0100490 _51d_process_match(args, smp);
491#endif
492
493#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100494 fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000495 if (lru)
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000496 _51d_insert_cache_entry(smp, lru, (void*)args);
James Rosewella28bbd52015-09-18 18:28:52 +0100497#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200498
Dragan Dosen93b38d92015-06-29 16:43:25 +0200499 return 1;
500}
501
James Rosewella28bbd52015-09-18 18:28:52 +0100502#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
503void _51d_init_http_headers()
504{
505 int index = 0;
506 const fiftyoneDegreesAsciiString *headerName;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100507 fiftyoneDegreesDataSet *ds = &global_51degrees.data_set;
508 global_51degrees.header_count = ds->httpHeadersCount;
Willy Tarreau83061a82018-07-13 11:56:34 +0200509 global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer));
Willy Tarreaub7a67142016-12-21 21:18:44 +0100510 for (index = 0; index < global_51degrees.header_count; index++) {
James Rosewella28bbd52015-09-18 18:28:52 +0100511 headerName = fiftyoneDegreesGetString(ds, ds->httpHeaders[index].headerNameOffset);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200512 (global_51degrees.header_names + index)->area = (char*)&headerName->firstByte;
513 (global_51degrees.header_names + index)->data = headerName->length - 1;
514 (global_51degrees.header_names + index)->size = (global_51degrees.header_names + index)->data;
James Rosewella28bbd52015-09-18 18:28:52 +0100515 }
516}
517#endif
518
519#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
520void _51d_init_http_headers()
521{
522 int index = 0;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100523 fiftyoneDegreesDataSet *ds = &global_51degrees.data_set;
524 global_51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(ds);
525 global_51degrees.device_offsets.firstOffset = malloc(
526 global_51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset));
Willy Tarreau83061a82018-07-13 11:56:34 +0200527 global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer));
Willy Tarreaub7a67142016-12-21 21:18:44 +0100528 global_51degrees.header_offsets = malloc(global_51degrees.header_count * sizeof(int32_t));
529 for (index = 0; index < global_51degrees.header_count; index++) {
530 global_51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(ds, index);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200531 global_51degrees.header_names->area = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(ds, index);
532 global_51degrees.header_names->data = strlen(global_51degrees.header_names->area);
533 global_51degrees.header_names[index].size = global_51degrees.header_names->data;
James Rosewella28bbd52015-09-18 18:28:52 +0100534 }
535}
536#endif
537
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100538/*
539 * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*.
540 */
541static int init_51degrees(void)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200542{
543 int i = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200544 struct buffer *temp;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200545 struct _51d_property_names *name;
546 char **_51d_property_list = NULL;
547 fiftyoneDegreesDataSetInitStatus _51d_dataset_status = DATA_SET_INIT_STATUS_NOT_SET;
548
Willy Tarreaub7a67142016-12-21 21:18:44 +0100549 if (!global_51degrees.data_file_path)
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100550 return 0;
Dragan Dosen9373fc52015-08-07 16:41:23 +0200551
Christopher Faulete8ca4342017-10-25 17:23:02 +0200552 if (global.nbthread > 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100553 ha_alert("51Degrees: multithreading is not supported for now.\n");
Christopher Faulete8ca4342017-10-25 17:23:02 +0200554 return (ERR_FATAL | ERR_ALERT);
555 }
556
Willy Tarreaub7a67142016-12-21 21:18:44 +0100557 if (!LIST_ISEMPTY(&global_51degrees.property_names)) {
Dragan Dosen93b38d92015-06-29 16:43:25 +0200558 i = 0;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100559 list_for_each_entry(name, &global_51degrees.property_names, list)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200560 ++i;
561 _51d_property_list = calloc(i, sizeof(char *));
562
563 i = 0;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100564 list_for_each_entry(name, &global_51degrees.property_names, list)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200565 _51d_property_list[i++] = name->name;
566 }
567
Willy Tarreaub7a67142016-12-21 21:18:44 +0100568 _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 +0200569
570 temp = get_trash_chunk();
571 chunk_reset(temp);
572
573 switch (_51d_dataset_status) {
574 case DATA_SET_INIT_STATUS_SUCCESS:
James Rosewella28bbd52015-09-18 18:28:52 +0100575#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
576 /* only 1 workset in the pool because HAProxy is currently single threaded
577 * this value should be set to the number of threads in future versions.
578 */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100579 global_51degrees.pool = fiftyoneDegreesWorksetPoolCreate(&global_51degrees.data_set, NULL, 1);
James Rosewella28bbd52015-09-18 18:28:52 +0100580#endif
581 _51d_init_http_headers();
Dragan Dosen93b38d92015-06-29 16:43:25 +0200582 break;
583 case DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY:
584 chunk_printf(temp, "Insufficient memory.");
585 break;
586 case DATA_SET_INIT_STATUS_CORRUPT_DATA:
587#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
588 chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Pattern data format.");
589#endif
590#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
591 chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Trie data format.");
592#endif
593 break;
594 case DATA_SET_INIT_STATUS_INCORRECT_VERSION:
595#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
596 chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Pattern data format.");
597#endif
598#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
599 chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Trie data format.");
600#endif
601 break;
602 case DATA_SET_INIT_STATUS_FILE_NOT_FOUND:
603 chunk_printf(temp, "File not found.");
604 break;
ben51degrees1f077eb2016-07-06 12:07:21 +0100605 case DATA_SET_INIT_STATUS_NULL_POINTER:
606 chunk_printf(temp, "Null pointer to the existing dataset or memory location.");
607 break;
608 case DATA_SET_INIT_STATUS_POINTER_OUT_OF_BOUNDS:
609 chunk_printf(temp, "Allocated continuous memory containing 51Degrees data file appears to be smaller than expected. Most likely"
610 " because the data file was not fully loaded into the allocated memory.");
611 break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200612 case DATA_SET_INIT_STATUS_NOT_SET:
613 chunk_printf(temp, "Data set not initialised.");
614 break;
Dragan Dosen483b93c2017-09-27 12:46:44 +0200615 default:
616 chunk_printf(temp, "Other error.");
617 break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200618 }
619 if (_51d_dataset_status != DATA_SET_INIT_STATUS_SUCCESS) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200620 if (temp->data)
621 ha_alert("51Degrees Setup - Error reading 51Degrees data file. %s\n",
622 temp->area);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200623 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100624 ha_alert("51Degrees Setup - Error reading 51Degrees data file.\n");
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100625 return ERR_ALERT | ERR_FATAL;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200626 }
627 free(_51d_property_list);
628
James Rosewella28bbd52015-09-18 18:28:52 +0100629#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200630 _51d_lru_seed = random();
Willy Tarreaub7a67142016-12-21 21:18:44 +0100631 if (global_51degrees.cache_size) {
632 _51d_lru_tree = lru64_new(global_51degrees.cache_size);
James Rosewella28bbd52015-09-18 18:28:52 +0100633 }
634#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200635
Dragan Dosen93b38d92015-06-29 16:43:25 +0200636 return 0;
637}
638
Willy Tarreau7ac4c202016-12-21 20:59:01 +0100639static void deinit_51degrees(void)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200640{
641 struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb;
642
Willy Tarreaub7a67142016-12-21 21:18:44 +0100643 free(global_51degrees.header_names);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200644#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100645 fiftyoneDegreesWorksetPoolFree(global_51degrees.pool);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200646#endif
647#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100648 free(global_51degrees.device_offsets.firstOffset);
649 free(global_51degrees.header_offsets);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200650#endif
Willy Tarreaub7a67142016-12-21 21:18:44 +0100651 fiftyoneDegreesDataSetFree(&global_51degrees.data_set);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200652
Willy Tarreaub7a67142016-12-21 21:18:44 +0100653 free(global_51degrees.data_file_path); global_51degrees.data_file_path = NULL;
654 list_for_each_entry_safe(_51d_prop_name, _51d_prop_nameb, &global_51degrees.property_names, list) {
Dragan Dosen93b38d92015-06-29 16:43:25 +0200655 LIST_DEL(&_51d_prop_name->list);
656 free(_51d_prop_name);
657 }
Dragan Dosen105c8e62015-06-29 16:43:26 +0200658
James Rosewella28bbd52015-09-18 18:28:52 +0100659#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200660 while (lru64_destroy(_51d_lru_tree));
James Rosewella28bbd52015-09-18 18:28:52 +0100661#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200662}
663
664static struct cfg_kw_list _51dcfg_kws = {{ }, {
665 { CFG_GLOBAL, "51degrees-data-file", _51d_data_file },
666 { CFG_GLOBAL, "51degrees-property-name-list", _51d_property_name_list },
667 { CFG_GLOBAL, "51degrees-property-separator", _51d_property_separator },
Dragan Dosen105c8e62015-06-29 16:43:26 +0200668 { CFG_GLOBAL, "51degrees-cache-size", _51d_cache_size },
Dragan Dosen93b38d92015-06-29 16:43:25 +0200669 { 0, NULL, NULL },
670}};
671
Willy Tarreau0108d902018-11-25 19:14:37 +0100672INITCALL1(STG_REGISTER, cfg_register_keywords, &_51dcfg_kws);
673
Dragan Dosen93b38d92015-06-29 16:43:25 +0200674/* Note: must not be declared <const> as its list will be overwritten */
James Rosewella28bbd52015-09-18 18:28:52 +0100675static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
676 { "51d.all", _51d_fetch, ARG5(1,STR,STR,STR,STR,STR), _51d_fetch_check, SMP_T_STR, SMP_USE_HRQHV },
677 { NULL, NULL, 0, 0, 0 },
678}};
679
Willy Tarreau0108d902018-11-25 19:14:37 +0100680INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
681
James Rosewella28bbd52015-09-18 18:28:52 +0100682/* Note: must not be declared <const> as its list will be overwritten */
Dragan Dosen93b38d92015-06-29 16:43:25 +0200683static struct sample_conv_kw_list conv_kws = {ILH, {
James Rosewella28bbd52015-09-18 18:28:52 +0100684 { "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 +0200685 { NULL, NULL, 0, 0, 0 },
686}};
687
Willy Tarreau0108d902018-11-25 19:14:37 +0100688INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
689
Dragan Dosen93b38d92015-06-29 16:43:25 +0200690__attribute__((constructor))
691static void __51d_init(void)
692{
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100693 hap_register_post_check(init_51degrees);
Willy Tarreau7ac4c202016-12-21 20:59:01 +0100694 hap_register_post_deinit(deinit_51degrees);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200695}
Willy Tarreau80713382018-11-26 10:19:54 +0100696
697REGISTER_BUILD_OPTS("Built with 51Degrees support.");