blob: e092c5ba83294dade6a38f98916cb2045ad2e4ea [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>
Willy Tarreau79e57332018-10-02 16:01:16 +02008#include <proto/http_fetch.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +02009#include <proto/log.h>
James Rosewella28bbd52015-09-18 18:28:52 +010010#include <proto/proto_http.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +020011#include <proto/sample.h>
12#include <import/xxhash.h>
Dragan Dosen105c8e62015-06-29 16:43:26 +020013#include <import/lru.h>
Willy Tarreaub7a67142016-12-21 21:18:44 +010014#include <51Degrees.h>
Dragan Dosen93b38d92015-06-29 16:43:25 +020015
16struct _51d_property_names {
17 struct list list;
18 char *name;
19};
20
James Rosewella28bbd52015-09-18 18:28:52 +010021#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +020022static struct lru64_head *_51d_lru_tree = NULL;
23static unsigned long long _51d_lru_seed;
James Rosewella28bbd52015-09-18 18:28:52 +010024#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +020025
Willy Tarreaub7a67142016-12-21 21:18:44 +010026static struct {
27 char property_separator; /* the separator to use in the response for the values. this is taken from 51degrees-property-separator from config. */
28 struct list property_names; /* list of properties to load into the data set. this is taken from 51degrees-property-name-list from config. */
29 char *data_file_path;
30 int header_count; /* number of HTTP headers related to device detection. */
Willy Tarreau83061a82018-07-13 11:56:34 +020031 struct buffer *header_names; /* array of HTTP header names. */
Willy Tarreaub7a67142016-12-21 21:18:44 +010032 fiftyoneDegreesDataSet data_set; /* data set used with the pattern and trie detection methods. */
33#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
34 fiftyoneDegreesWorksetPool *pool; /* pool of worksets to avoid creating a new one for each request. */
35#endif
36#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
37 int32_t *header_offsets; /* offsets to the HTTP header name string. */
38 fiftyoneDegreesDeviceOffsets device_offsets; /* Memory used for device offsets. */
39#endif
40 int cache_size;
41} global_51degrees = {
42 .property_separator = ',',
43 .property_names = LIST_HEAD_INIT(global_51degrees.property_names),
44 .data_file_path = NULL,
45#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
46 .data_set = { },
47#endif
48 .cache_size = 0,
49};
50
Dragan Dosen93b38d92015-06-29 16:43:25 +020051static int _51d_data_file(char **args, int section_type, struct proxy *curpx,
52 struct proxy *defpx, const char *file, int line,
53 char **err)
54{
55 if (*(args[1]) == 0) {
56 memprintf(err,
57 "'%s' expects a filepath to a 51Degrees trie or pattern data file.",
58 args[0]);
59 return -1;
60 }
61
Willy Tarreaub7a67142016-12-21 21:18:44 +010062 if (global_51degrees.data_file_path)
63 free(global_51degrees.data_file_path);
64 global_51degrees.data_file_path = strdup(args[1]);
Dragan Dosen93b38d92015-06-29 16:43:25 +020065
66 return 0;
67}
68
69static int _51d_property_name_list(char **args, int section_type, struct proxy *curpx,
James Rosewella28bbd52015-09-18 18:28:52 +010070 struct proxy *defpx, const char *file, int line,
71 char **err)
Dragan Dosen93b38d92015-06-29 16:43:25 +020072{
73 int cur_arg = 1;
74 struct _51d_property_names *name;
75
76 if (*(args[cur_arg]) == 0) {
77 memprintf(err,
78 "'%s' expects at least one 51Degrees property name.",
79 args[0]);
80 return -1;
81 }
82
83 while (*(args[cur_arg])) {
Vincent Bernat02779b62016-04-03 13:48:43 +020084 name = calloc(1, sizeof(*name));
Dragan Dosen93b38d92015-06-29 16:43:25 +020085 name->name = strdup(args[cur_arg]);
Willy Tarreaub7a67142016-12-21 21:18:44 +010086 LIST_ADDQ(&global_51degrees.property_names, &name->list);
Dragan Dosen93b38d92015-06-29 16:43:25 +020087 ++cur_arg;
88 }
89
90 return 0;
91}
92
93static int _51d_property_separator(char **args, int section_type, struct proxy *curpx,
94 struct proxy *defpx, const char *file, int line,
95 char **err)
96{
97 if (*(args[1]) == 0) {
98 memprintf(err,
99 "'%s' expects a single character.",
100 args[0]);
101 return -1;
102 }
103 if (strlen(args[1]) > 1) {
104 memprintf(err,
105 "'%s' expects a single character, got '%s'.",
106 args[0], args[1]);
107 return -1;
108 }
109
Willy Tarreaub7a67142016-12-21 21:18:44 +0100110 global_51degrees.property_separator = *args[1];
Dragan Dosen93b38d92015-06-29 16:43:25 +0200111
112 return 0;
113}
114
Dragan Dosen105c8e62015-06-29 16:43:26 +0200115static int _51d_cache_size(char **args, int section_type, struct proxy *curpx,
116 struct proxy *defpx, const char *file, int line,
117 char **err)
118{
119 if (*(args[1]) == 0) {
120 memprintf(err,
121 "'%s' expects a positive numeric value.",
122 args[0]);
123 return -1;
124 }
125
Willy Tarreaub7a67142016-12-21 21:18:44 +0100126 global_51degrees.cache_size = atoi(args[1]);
127 if (global_51degrees.cache_size < 0) {
Dragan Dosen105c8e62015-06-29 16:43:26 +0200128 memprintf(err,
129 "'%s' expects a positive numeric value, got '%s'.",
130 args[0], args[1]);
131 return -1;
132 }
133
James Rosewella28bbd52015-09-18 18:28:52 +0100134 return 0;
135}
136
137static int _51d_fetch_check(struct arg *arg, char **err_msg)
138{
Willy Tarreaub7a67142016-12-21 21:18:44 +0100139 if (global_51degrees.data_file_path)
James Rosewella28bbd52015-09-18 18:28:52 +0100140 return 1;
141
142 memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
Dragan Dosen105c8e62015-06-29 16:43:26 +0200143 return 0;
144}
145
Dragan Dosen9373fc52015-08-07 16:41:23 +0200146static int _51d_conv_check(struct arg *arg, struct sample_conv *conv,
James Rosewella28bbd52015-09-18 18:28:52 +0100147 const char *file, int line, char **err_msg)
Dragan Dosen9373fc52015-08-07 16:41:23 +0200148{
Willy Tarreaub7a67142016-12-21 21:18:44 +0100149 if (global_51degrees.data_file_path)
Dragan Dosen9373fc52015-08-07 16:41:23 +0200150 return 1;
151
James Rosewella28bbd52015-09-18 18:28:52 +0100152 memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
Dragan Dosen9373fc52015-08-07 16:41:23 +0200153 return 0;
154}
155
James Rosewella28bbd52015-09-18 18:28:52 +0100156#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000157static void _51d_lru_free(void *cache_entry)
158{
Willy Tarreau83061a82018-07-13 11:56:34 +0200159 struct buffer *ptr = cache_entry;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000160
161 if (!ptr)
162 return;
163
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200164 free(ptr->area);
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000165 free(ptr);
166}
167
168/* Allocates memory freeing space in the cache if necessary.
169*/
170static void *_51d_malloc(int size)
171{
172 void *ptr = malloc(size);
173
174 if (!ptr) {
175 /* free the oldest 10 entries from lru to free up some memory
176 * then try allocating memory again */
177 lru64_kill_oldest(_51d_lru_tree, 10);
178 ptr = malloc(size);
179 }
180
181 return ptr;
182}
183
James Rosewell63426cb2015-09-18 19:53:05 +0100184/* Insert the data associated with the sample into the cache as a fresh item.
185 */
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000186static void _51d_insert_cache_entry(struct sample *smp, struct lru64 *lru, void* domain)
James Rosewell63426cb2015-09-18 19:53:05 +0100187{
Willy Tarreau83061a82018-07-13 11:56:34 +0200188 struct buffer *cache_entry = _51d_malloc(sizeof(*cache_entry));
James Rosewell63426cb2015-09-18 19:53:05 +0100189
190 if (!cache_entry)
191 return;
192
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200193 cache_entry->area = _51d_malloc(smp->data.u.str.data + 1);
194 if (!cache_entry->area) {
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000195 free(cache_entry);
James Rosewell63426cb2015-09-18 19:53:05 +0100196 return;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000197 }
James Rosewell63426cb2015-09-18 19:53:05 +0100198
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200199 memcpy(cache_entry->area, smp->data.u.str.area, smp->data.u.str.data);
200 cache_entry->area[smp->data.u.str.data] = 0;
201 cache_entry->data = smp->data.u.str.data;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000202 lru64_commit(lru, cache_entry, domain, 0, _51d_lru_free);
James Rosewell63426cb2015-09-18 19:53:05 +0100203}
204
205/* Retrieves the data from the cache and sets the sample data to this string.
206 */
207static void _51d_retrieve_cache_entry(struct sample *smp, struct lru64 *lru)
208{
Willy Tarreau83061a82018-07-13 11:56:34 +0200209 struct buffer *cache_entry = lru->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200210 smp->data.u.str.area = cache_entry->area;
211 smp->data.u.str.data = cache_entry->data;
James Rosewell63426cb2015-09-18 19:53:05 +0100212}
213#endif
214
215#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100216/* Sets the important HTTP headers ahead of the detection
217 */
218static void _51d_set_headers(struct sample *smp, fiftyoneDegreesWorkset *ws)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200219{
James Rosewella28bbd52015-09-18 18:28:52 +0100220 struct hdr_idx *idx;
221 struct hdr_ctx ctx;
222 const struct http_msg *msg;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200223 int i;
James Rosewella28bbd52015-09-18 18:28:52 +0100224
225 idx = &smp->strm->txn->hdr_idx;
226 msg = &smp->strm->txn->req;
227
228 ws->importantHeadersCount = 0;
229
Willy Tarreaub7a67142016-12-21 21:18:44 +0100230 for (i = 0; i < global_51degrees.header_count; i++) {
James Rosewella28bbd52015-09-18 18:28:52 +0100231 ctx.idx = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200232 if (http_find_full_header2((global_51degrees.header_names + i)->area,
233 (global_51degrees.header_names + i)->data,
234 msg->chn->buf->p, idx, &ctx) == 1) {
James Rosewella28bbd52015-09-18 18:28:52 +0100235 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;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200259 if (http_find_full_header2((global_51degrees.header_names + index)->area,
260 (global_51degrees.header_names + index)->data,
261 msg->chn->buf->p, idx, &ctx) == 1) {
Willy Tarreaub7a67142016-12-21 21:18:44 +0100262 (offsets->firstOffset + offsets->size)->httpHeaderOffset = *(global_51degrees.header_offsets + index);
263 (offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set, ctx.line + ctx.val);
James Rosewella28bbd52015-09-18 18:28:52 +0100264 offsets->size++;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200265 }
266 }
James Rosewella28bbd52015-09-18 18:28:52 +0100267}
268#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200269
270#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100271/* Provides a hash code for the important HTTP headers.
272 */
273unsigned long long _51d_req_hash(const struct arg *args, fiftyoneDegreesWorkset* ws)
274{
275 unsigned long long seed = _51d_lru_seed ^ (long)args;
276 unsigned long long hash = 0;
277 int i;
278 for(i = 0; i < ws->importantHeadersCount; i++) {
279 hash ^= ws->importantHeaders[i].header->headerNameOffset;
280 hash ^= XXH64(ws->importantHeaders[i].headerValue,
281 ws->importantHeaders[i].headerValueLength,
282 seed);
283 }
284 return hash;
285}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200286#endif
287
Dragan Dosen93b38d92015-06-29 16:43:25 +0200288#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100289static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesWorkset* ws)
290{
291 char *methodName;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200292#endif
293#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100294static void _51d_process_match(const struct arg *args, struct sample *smp)
295{
296 char valuesBuffer[1024];
Willy Tarreaub7a67142016-12-21 21:18:44 +0100297 const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(&global_51degrees.data_set);
298 int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(&global_51degrees.data_set);
299 fiftyoneDegreesDeviceOffsets *deviceOffsets = &global_51degrees.device_offsets;
James Rosewella28bbd52015-09-18 18:28:52 +0100300
Dragan Dosen93b38d92015-06-29 16:43:25 +0200301#endif
302
James Rosewella28bbd52015-09-18 18:28:52 +0100303 char no_data[] = "NoData"; /* response when no data could be found */
Willy Tarreau83061a82018-07-13 11:56:34 +0200304 struct buffer *temp = get_trash_chunk();
James Rosewella28bbd52015-09-18 18:28:52 +0100305 int j, i = 0, found;
306 const char* property_name;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200307
308 /* Loop through property names passed to the filter and fetch them from the dataset. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200309 while (args[i].data.str.area) {
Dragan Dosen93b38d92015-06-29 16:43:25 +0200310 /* Try to find request property in dataset. */
Dragan Dosen93b38d92015-06-29 16:43:25 +0200311 found = 0;
James Rosewella28bbd52015-09-18 18:28:52 +0100312#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200313 if (strcmp("Method", args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100314 switch(ws->method) {
315 case EXACT: methodName = "Exact"; break;
316 case NUMERIC: methodName = "Numeric"; break;
317 case NEAREST: methodName = "Nearest"; break;
318 case CLOSEST: methodName = "Closest"; break;
319 default:
320 case NONE: methodName = "None"; break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200321 }
James Rosewella28bbd52015-09-18 18:28:52 +0100322 chunk_appendf(temp, "%s", methodName);
323 found = 1;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200324 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200325 else if (strcmp("Difference", args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100326 chunk_appendf(temp, "%d", ws->difference);
327 found = 1;
328 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200329 else if (strcmp("Rank", args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100330 chunk_appendf(temp, "%d", fiftyoneDegreesGetSignatureRank(ws));
331 found = 1;
332 }
333 else {
334 for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) {
335 property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200336 if (strcmp(property_name, args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100337 found = 1;
338 fiftyoneDegreesSetValues(ws, j);
339 chunk_appendf(temp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values));
340 break;
341 }
342 }
Dragan Dosen93b38d92015-06-29 16:43:25 +0200343 }
344#endif
345#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100346 found = 0;
347 for (j = 0; j < requiredPropertiesCount; j++) {
348 property_name = requiredProperties[j];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200349 if (strcmp(property_name, args[i].data.str.area) == 0 &&
Willy Tarreaub7a67142016-12-21 21:18:44 +0100350 fiftyoneDegreesGetValueFromOffsets(&global_51degrees.data_set, deviceOffsets, j, valuesBuffer, 1024) > 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100351 found = 1;
352 chunk_appendf(temp, "%s", valuesBuffer);
353 break;
354 }
Dragan Dosen93b38d92015-06-29 16:43:25 +0200355 }
James Rosewella28bbd52015-09-18 18:28:52 +0100356#endif
ben@51degrees.comd3842522016-01-08 13:52:32 +0000357 if (!found)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200358 chunk_appendf(temp, "%s", no_data);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000359
Dragan Dosen93b38d92015-06-29 16:43:25 +0200360 /* Add separator. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100361 chunk_appendf(temp, "%c", global_51degrees.property_separator);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200362 ++i;
363 }
364
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200365 if (temp->data) {
366 --temp->data;
367 temp->area[temp->data] = '\0';
Dragan Dosen93b38d92015-06-29 16:43:25 +0200368 }
369
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200370 smp->data.u.str.area = temp->area;
371 smp->data.u.str.data = temp->data;
James Rosewella28bbd52015-09-18 18:28:52 +0100372}
373
374static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
375{
376#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
377 fiftyoneDegreesWorkset* ws; /* workset for detection */
378 struct lru64 *lru = NULL;
379#endif
380
381 /* Needed to ensure that the HTTP message has been fully recieved when
382 * used with TCP operation. Not required for HTTP operation.
383 * Data type has to be reset to ensure the string output is processed
384 * correctly.
385 */
386 CHECK_HTTP_MESSAGE_FIRST();
387 smp->data.type = SMP_T_STR;
388
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000389 /* Flags the sample to show it uses constant memory*/
390 smp->flags |= SMP_F_CONST;
391
James Rosewella28bbd52015-09-18 18:28:52 +0100392#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
393
394 /* Get only the headers needed for device detection so they can be used
395 * with the cache to return previous results. Pattern is slower than
396 * Trie so caching will help improve performance.
397 */
398
399 /* Get a workset from the pool which will later contain detection results. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100400 ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool);
James Rosewella28bbd52015-09-18 18:28:52 +0100401 if (!ws)
402 return 0;
403
404 /* Set the important HTTP headers for this request in the workset. */
405 _51d_set_headers(smp, ws);
406
407 /* Check the cache to see if there's results for these headers already. */
408 if (_51d_lru_tree) {
409 lru = lru64_get(_51d_req_hash(args, ws),
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000410 _51d_lru_tree, (void*)args, 0);
James Rosewella28bbd52015-09-18 18:28:52 +0100411 if (lru && lru->domain) {
Willy Tarreaub7a67142016-12-21 21:18:44 +0100412 fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
James Rosewell63426cb2015-09-18 19:53:05 +0100413 _51d_retrieve_cache_entry(smp, lru);
James Rosewella28bbd52015-09-18 18:28:52 +0100414 return 1;
415 }
416 }
417
418 fiftyoneDegreesMatchForHttpHeaders(ws);
419
420 _51d_process_match(args, smp, ws);
421
422#endif
423
424#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
425
426 /* Trie is very fast so all the headers can be passed in and the result
427 * returned faster than the hashing algorithm process.
428 */
429 _51d_set_device_offsets(smp);
430 _51d_process_match(args, smp);
431
432#endif
433
434#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100435 fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000436 if (lru)
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000437 _51d_insert_cache_entry(smp, lru, (void*)args);
James Rosewella28bbd52015-09-18 18:28:52 +0100438#endif
439
440 return 1;
441}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200442
James Rosewella28bbd52015-09-18 18:28:52 +0100443static int _51d_conv(const struct arg *args, struct sample *smp, void *private)
444{
Dragan Dosen93b38d92015-06-29 16:43:25 +0200445#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100446 fiftyoneDegreesWorkset* ws; /* workset for detection */
447 struct lru64 *lru = NULL;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200448#endif
449
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000450 /* Flags the sample to show it uses constant memory*/
451 smp->flags |= SMP_F_CONST;
452
James Rosewella28bbd52015-09-18 18:28:52 +0100453#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
454
455 /* Look in the list. */
456 if (_51d_lru_tree) {
457 unsigned long long seed = _51d_lru_seed ^ (long)args;
458
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200459 lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000460 _51d_lru_tree, (void*)args, 0);
James Rosewella28bbd52015-09-18 18:28:52 +0100461 if (lru && lru->domain) {
James Rosewell63426cb2015-09-18 19:53:05 +0100462 _51d_retrieve_cache_entry(smp, lru);
James Rosewella28bbd52015-09-18 18:28:52 +0100463 return 1;
464 }
465 }
466
467 /* Create workset. This will later contain detection results. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100468 ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool);
James Rosewella28bbd52015-09-18 18:28:52 +0100469 if (!ws)
470 return 0;
471#endif
472
473 /* Duplicate the data and remove the "const" flag before device detection. */
474 if (!smp_dup(smp))
475 return 0;
476
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200477 smp->data.u.str.area[smp->data.u.str.data] = '\0';
James Rosewella28bbd52015-09-18 18:28:52 +0100478
479 /* Perform detection. */
480#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200481 fiftyoneDegreesMatch(ws, smp->data.u.str.area);
James Rosewella28bbd52015-09-18 18:28:52 +0100482 _51d_process_match(args, smp, ws);
483#endif
484#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200485 global_51degrees.device_offsets.firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set,
486 smp->data.u.str.area);
Willy Tarreaub7a67142016-12-21 21:18:44 +0100487 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;
Willy Tarreau83061a82018-07-13 11:56:34 +0200507 global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer));
Willy Tarreaub7a67142016-12-21 21:18:44 +0100508 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 Tarreau843b7cb2018-07-13 10:54:26 +0200510 (global_51degrees.header_names + index)->area = (char*)&headerName->firstByte;
511 (global_51degrees.header_names + index)->data = headerName->length - 1;
512 (global_51degrees.header_names + index)->size = (global_51degrees.header_names + index)->data;
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));
Willy Tarreau83061a82018-07-13 11:56:34 +0200525 global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer));
Willy Tarreaub7a67142016-12-21 21:18:44 +0100526 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);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200529 global_51degrees.header_names->area = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(ds, index);
530 global_51degrees.header_names->data = strlen(global_51degrees.header_names->area);
531 global_51degrees.header_names[index].size = global_51degrees.header_names->data;
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;
Willy Tarreau83061a82018-07-13 11:56:34 +0200542 struct buffer *temp;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200543 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
Christopher Faulete8ca4342017-10-25 17:23:02 +0200550 if (global.nbthread > 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100551 ha_alert("51Degrees: multithreading is not supported for now.\n");
Christopher Faulete8ca4342017-10-25 17:23:02 +0200552 return (ERR_FATAL | ERR_ALERT);
553 }
554
Willy Tarreaub7a67142016-12-21 21:18:44 +0100555 if (!LIST_ISEMPTY(&global_51degrees.property_names)) {
Dragan Dosen93b38d92015-06-29 16:43:25 +0200556 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 ++i;
559 _51d_property_list = calloc(i, sizeof(char *));
560
561 i = 0;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100562 list_for_each_entry(name, &global_51degrees.property_names, list)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200563 _51d_property_list[i++] = name->name;
564 }
565
Willy Tarreaub7a67142016-12-21 21:18:44 +0100566 _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 +0200567
568 temp = get_trash_chunk();
569 chunk_reset(temp);
570
571 switch (_51d_dataset_status) {
572 case DATA_SET_INIT_STATUS_SUCCESS:
James Rosewella28bbd52015-09-18 18:28:52 +0100573#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
574 /* only 1 workset in the pool because HAProxy is currently single threaded
575 * this value should be set to the number of threads in future versions.
576 */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100577 global_51degrees.pool = fiftyoneDegreesWorksetPoolCreate(&global_51degrees.data_set, NULL, 1);
James Rosewella28bbd52015-09-18 18:28:52 +0100578#endif
579 _51d_init_http_headers();
Dragan Dosen93b38d92015-06-29 16:43:25 +0200580 break;
581 case DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY:
582 chunk_printf(temp, "Insufficient memory.");
583 break;
584 case DATA_SET_INIT_STATUS_CORRUPT_DATA:
585#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
586 chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Pattern data format.");
587#endif
588#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
589 chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Trie data format.");
590#endif
591 break;
592 case DATA_SET_INIT_STATUS_INCORRECT_VERSION:
593#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
594 chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Pattern data format.");
595#endif
596#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
597 chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Trie data format.");
598#endif
599 break;
600 case DATA_SET_INIT_STATUS_FILE_NOT_FOUND:
601 chunk_printf(temp, "File not found.");
602 break;
ben51degrees1f077eb2016-07-06 12:07:21 +0100603 case DATA_SET_INIT_STATUS_NULL_POINTER:
604 chunk_printf(temp, "Null pointer to the existing dataset or memory location.");
605 break;
606 case DATA_SET_INIT_STATUS_POINTER_OUT_OF_BOUNDS:
607 chunk_printf(temp, "Allocated continuous memory containing 51Degrees data file appears to be smaller than expected. Most likely"
608 " because the data file was not fully loaded into the allocated memory.");
609 break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200610 case DATA_SET_INIT_STATUS_NOT_SET:
611 chunk_printf(temp, "Data set not initialised.");
612 break;
Dragan Dosen483b93c2017-09-27 12:46:44 +0200613 default:
614 chunk_printf(temp, "Other error.");
615 break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200616 }
617 if (_51d_dataset_status != DATA_SET_INIT_STATUS_SUCCESS) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200618 if (temp->data)
619 ha_alert("51Degrees Setup - Error reading 51Degrees data file. %s\n",
620 temp->area);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200621 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100622 ha_alert("51Degrees Setup - Error reading 51Degrees data file.\n");
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100623 return ERR_ALERT | ERR_FATAL;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200624 }
625 free(_51d_property_list);
626
James Rosewella28bbd52015-09-18 18:28:52 +0100627#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200628 _51d_lru_seed = random();
Willy Tarreaub7a67142016-12-21 21:18:44 +0100629 if (global_51degrees.cache_size) {
630 _51d_lru_tree = lru64_new(global_51degrees.cache_size);
James Rosewella28bbd52015-09-18 18:28:52 +0100631 }
632#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200633
Dragan Dosen93b38d92015-06-29 16:43:25 +0200634 return 0;
635}
636
Willy Tarreau7ac4c202016-12-21 20:59:01 +0100637static void deinit_51degrees(void)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200638{
639 struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb;
640
Willy Tarreaub7a67142016-12-21 21:18:44 +0100641 free(global_51degrees.header_names);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200642#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100643 fiftyoneDegreesWorksetPoolFree(global_51degrees.pool);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200644#endif
645#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100646 free(global_51degrees.device_offsets.firstOffset);
647 free(global_51degrees.header_offsets);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200648#endif
Willy Tarreaub7a67142016-12-21 21:18:44 +0100649 fiftyoneDegreesDataSetFree(&global_51degrees.data_set);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200650
Willy Tarreaub7a67142016-12-21 21:18:44 +0100651 free(global_51degrees.data_file_path); global_51degrees.data_file_path = NULL;
652 list_for_each_entry_safe(_51d_prop_name, _51d_prop_nameb, &global_51degrees.property_names, list) {
Dragan Dosen93b38d92015-06-29 16:43:25 +0200653 LIST_DEL(&_51d_prop_name->list);
654 free(_51d_prop_name);
655 }
Dragan Dosen105c8e62015-06-29 16:43:26 +0200656
James Rosewella28bbd52015-09-18 18:28:52 +0100657#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +0200658 while (lru64_destroy(_51d_lru_tree));
James Rosewella28bbd52015-09-18 18:28:52 +0100659#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200660}
661
662static struct cfg_kw_list _51dcfg_kws = {{ }, {
663 { CFG_GLOBAL, "51degrees-data-file", _51d_data_file },
664 { CFG_GLOBAL, "51degrees-property-name-list", _51d_property_name_list },
665 { CFG_GLOBAL, "51degrees-property-separator", _51d_property_separator },
Dragan Dosen105c8e62015-06-29 16:43:26 +0200666 { CFG_GLOBAL, "51degrees-cache-size", _51d_cache_size },
Dragan Dosen93b38d92015-06-29 16:43:25 +0200667 { 0, NULL, NULL },
668}};
669
670/* Note: must not be declared <const> as its list will be overwritten */
James Rosewella28bbd52015-09-18 18:28:52 +0100671static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
672 { "51d.all", _51d_fetch, ARG5(1,STR,STR,STR,STR,STR), _51d_fetch_check, SMP_T_STR, SMP_USE_HRQHV },
673 { NULL, NULL, 0, 0, 0 },
674}};
675
676/* Note: must not be declared <const> as its list will be overwritten */
Dragan Dosen93b38d92015-06-29 16:43:25 +0200677static struct sample_conv_kw_list conv_kws = {ILH, {
James Rosewella28bbd52015-09-18 18:28:52 +0100678 { "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 +0200679 { NULL, NULL, 0, 0, 0 },
680}};
681
682__attribute__((constructor))
683static void __51d_init(void)
684{
James Rosewella28bbd52015-09-18 18:28:52 +0100685 /* register sample fetch and conversion keywords */
686 sample_register_fetches(&sample_fetch_keywords);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200687 sample_register_convs(&conv_kws);
688 cfg_register_keywords(&_51dcfg_kws);
Willy Tarreaub5e58d62016-12-21 18:49:29 +0100689 hap_register_build_opts("Built with 51Degrees support.", 0);
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100690 hap_register_post_check(init_51degrees);
Willy Tarreau7ac4c202016-12-21 20:59:01 +0100691 hap_register_post_deinit(deinit_51degrees);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200692}