blob: a23b468d6ab18662d1933a7862f69a5dded166d1 [file] [log] [blame]
Dragan Dosen93b38d92015-06-29 16:43:25 +02001#include <stdio.h>
2
Willy Tarreaub2551052020-06-09 09:07:15 +02003#include <import/lru.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +02004#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +02005#include <haproxy/arg.h>
6#include <haproxy/buf-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +02007#include <haproxy/cfgparse.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +02008#include <haproxy/chunk.h>
Willy Tarreau8d366972020-05-27 16:10:29 +02009#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020010#include <haproxy/global.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020011#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020012#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020013#include <haproxy/http_htx.h>
Dragan Dosena9800a02022-02-14 13:05:45 +010014#include <haproxy/htx.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020015#include <haproxy/sample.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/thread.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020017#include <haproxy/tools.h>
Tim Duesterhusd5fc8fc2021-09-11 17:51:13 +020018#include <haproxy/xxhash.h>
Dragan Dosena9800a02022-02-14 13:05:45 +010019
20#ifdef USE_51DEGREES_V4
21#include <hash/hash.h>
22#undef MAP_TYPE
23#include <hash/fiftyone.h>
24#else
Willy Tarreaub7a67142016-12-21 21:18:44 +010025#include <51Degrees.h>
Dragan Dosena9800a02022-02-14 13:05:45 +010026#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +020027
28struct _51d_property_names {
29 struct list list;
30 char *name;
31};
32
James Rosewella28bbd52015-09-18 18:28:52 +010033#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +020034static struct lru64_head *_51d_lru_tree = NULL;
35static unsigned long long _51d_lru_seed;
Ben51Degrees4ddf59d2019-02-05 13:24:00 +000036
37__decl_spinlock(_51d_lru_lock);
James Rosewella28bbd52015-09-18 18:28:52 +010038#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +020039
Dragan Dosena9800a02022-02-14 13:05:45 +010040#ifdef FIFTYONE_DEGREES_HASH_INCLUDED
41#define _51D_HEADERS_BUFFER_SIZE BUFSIZE
42
43static THREAD_LOCAL struct {
44 char **buf;
45 int max;
46 int count;
47} _51d_headers;
48
49static THREAD_LOCAL fiftyoneDegreesResultsHash *_51d_results = NULL;
50#endif
51
Willy Tarreaub7a67142016-12-21 21:18:44 +010052static struct {
53 char property_separator; /* the separator to use in the response for the values. this is taken from 51degrees-property-separator from config. */
54 struct list property_names; /* list of properties to load into the data set. this is taken from 51degrees-property-name-list from config. */
55 char *data_file_path;
Dragan Dosena9800a02022-02-14 13:05:45 +010056#if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) || defined(FIFTYONEDEGREES_H_TRIE_INCLUDED)
Willy Tarreaub7a67142016-12-21 21:18:44 +010057 int header_count; /* number of HTTP headers related to device detection. */
Willy Tarreau83061a82018-07-13 11:56:34 +020058 struct buffer *header_names; /* array of HTTP header names. */
Willy Tarreaub7a67142016-12-21 21:18:44 +010059 fiftyoneDegreesDataSet data_set; /* data set used with the pattern and trie detection methods. */
60#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
61 fiftyoneDegreesWorksetPool *pool; /* pool of worksets to avoid creating a new one for each request. */
62#endif
63#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
64 int32_t *header_offsets; /* offsets to the HTTP header name string. */
Ben51Degrees4ddf59d2019-02-05 13:24:00 +000065#ifdef FIFTYONEDEGREES_NO_THREADING
Willy Tarreaub7a67142016-12-21 21:18:44 +010066 fiftyoneDegreesDeviceOffsets device_offsets; /* Memory used for device offsets. */
67#endif
Ben51Degrees4ddf59d2019-02-05 13:24:00 +000068#endif
Dragan Dosena9800a02022-02-14 13:05:45 +010069#elif defined(FIFTYONE_DEGREES_HASH_INCLUDED)
70 fiftyoneDegreesResourceManager manager;
71 int use_perf_graph;
72 int use_pred_graph;
73 int drift;
74 int difference;
75 int allow_unmatched;
76#endif
Willy Tarreaub7a67142016-12-21 21:18:44 +010077 int cache_size;
78} global_51degrees = {
79 .property_separator = ',',
80 .property_names = LIST_HEAD_INIT(global_51degrees.property_names),
81 .data_file_path = NULL,
82#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
83 .data_set = { },
84#endif
Dragan Dosena9800a02022-02-14 13:05:45 +010085#ifdef FIFTYONE_DEGREES_HASH_INCLUDED
86 .manager = { },
87 .use_perf_graph = -1,
88 .use_pred_graph = -1,
89 .drift = -1,
90 .difference = -1,
91 .allow_unmatched = -1,
92#endif
Willy Tarreaub7a67142016-12-21 21:18:44 +010093 .cache_size = 0,
94};
95
Dragan Dosen93b38d92015-06-29 16:43:25 +020096static int _51d_data_file(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +010097 const struct proxy *defpx, const char *file, int line,
Dragan Dosen93b38d92015-06-29 16:43:25 +020098 char **err)
99{
100 if (*(args[1]) == 0) {
101 memprintf(err,
102 "'%s' expects a filepath to a 51Degrees trie or pattern data file.",
103 args[0]);
104 return -1;
105 }
106
Willy Tarreaub7a67142016-12-21 21:18:44 +0100107 if (global_51degrees.data_file_path)
108 free(global_51degrees.data_file_path);
109 global_51degrees.data_file_path = strdup(args[1]);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200110
111 return 0;
112}
113
114static int _51d_property_name_list(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100115 const struct proxy *defpx, const char *file, int line,
James Rosewella28bbd52015-09-18 18:28:52 +0100116 char **err)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200117{
118 int cur_arg = 1;
119 struct _51d_property_names *name;
120
121 if (*(args[cur_arg]) == 0) {
122 memprintf(err,
123 "'%s' expects at least one 51Degrees property name.",
124 args[0]);
125 return -1;
126 }
127
128 while (*(args[cur_arg])) {
Vincent Bernat02779b62016-04-03 13:48:43 +0200129 name = calloc(1, sizeof(*name));
Dragan Dosen93b38d92015-06-29 16:43:25 +0200130 name->name = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +0200131 LIST_APPEND(&global_51degrees.property_names, &name->list);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200132 ++cur_arg;
133 }
134
135 return 0;
136}
137
138static int _51d_property_separator(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100139 const struct proxy *defpx, const char *file, int line,
Dragan Dosen93b38d92015-06-29 16:43:25 +0200140 char **err)
141{
142 if (*(args[1]) == 0) {
143 memprintf(err,
144 "'%s' expects a single character.",
145 args[0]);
146 return -1;
147 }
148 if (strlen(args[1]) > 1) {
149 memprintf(err,
150 "'%s' expects a single character, got '%s'.",
151 args[0], args[1]);
152 return -1;
153 }
154
Willy Tarreaub7a67142016-12-21 21:18:44 +0100155 global_51degrees.property_separator = *args[1];
Dragan Dosen93b38d92015-06-29 16:43:25 +0200156
157 return 0;
158}
159
Dragan Dosen105c8e62015-06-29 16:43:26 +0200160static int _51d_cache_size(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100161 const struct proxy *defpx, const char *file, int line,
Dragan Dosen105c8e62015-06-29 16:43:26 +0200162 char **err)
163{
164 if (*(args[1]) == 0) {
165 memprintf(err,
166 "'%s' expects a positive numeric value.",
167 args[0]);
168 return -1;
169 }
170
Willy Tarreaub7a67142016-12-21 21:18:44 +0100171 global_51degrees.cache_size = atoi(args[1]);
172 if (global_51degrees.cache_size < 0) {
Dragan Dosen105c8e62015-06-29 16:43:26 +0200173 memprintf(err,
174 "'%s' expects a positive numeric value, got '%s'.",
175 args[0], args[1]);
176 return -1;
177 }
178
James Rosewella28bbd52015-09-18 18:28:52 +0100179 return 0;
180}
181
182static int _51d_fetch_check(struct arg *arg, char **err_msg)
183{
Willy Tarreaub7a67142016-12-21 21:18:44 +0100184 if (global_51degrees.data_file_path)
James Rosewella28bbd52015-09-18 18:28:52 +0100185 return 1;
186
187 memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
Dragan Dosen105c8e62015-06-29 16:43:26 +0200188 return 0;
189}
190
Dragan Dosen9373fc52015-08-07 16:41:23 +0200191static int _51d_conv_check(struct arg *arg, struct sample_conv *conv,
James Rosewella28bbd52015-09-18 18:28:52 +0100192 const char *file, int line, char **err_msg)
Dragan Dosen9373fc52015-08-07 16:41:23 +0200193{
Willy Tarreaub7a67142016-12-21 21:18:44 +0100194 if (global_51degrees.data_file_path)
Dragan Dosen9373fc52015-08-07 16:41:23 +0200195 return 1;
196
James Rosewella28bbd52015-09-18 18:28:52 +0100197 memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
Dragan Dosen9373fc52015-08-07 16:41:23 +0200198 return 0;
199}
200
James Rosewella28bbd52015-09-18 18:28:52 +0100201#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000202static void _51d_lru_free(void *cache_entry)
203{
Willy Tarreau83061a82018-07-13 11:56:34 +0200204 struct buffer *ptr = cache_entry;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000205
206 if (!ptr)
207 return;
208
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200209 free(ptr->area);
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000210 free(ptr);
211}
212
213/* Allocates memory freeing space in the cache if necessary.
214*/
215static void *_51d_malloc(int size)
216{
217 void *ptr = malloc(size);
218
219 if (!ptr) {
220 /* free the oldest 10 entries from lru to free up some memory
221 * then try allocating memory again */
222 lru64_kill_oldest(_51d_lru_tree, 10);
223 ptr = malloc(size);
224 }
225
226 return ptr;
227}
228
James Rosewell63426cb2015-09-18 19:53:05 +0100229/* Insert the data associated with the sample into the cache as a fresh item.
230 */
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000231static void _51d_insert_cache_entry(struct sample *smp, struct lru64 *lru, void* domain)
James Rosewell63426cb2015-09-18 19:53:05 +0100232{
Willy Tarreau83061a82018-07-13 11:56:34 +0200233 struct buffer *cache_entry = _51d_malloc(sizeof(*cache_entry));
James Rosewell63426cb2015-09-18 19:53:05 +0100234
235 if (!cache_entry)
236 return;
237
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200238 cache_entry->area = _51d_malloc(smp->data.u.str.data + 1);
239 if (!cache_entry->area) {
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000240 free(cache_entry);
James Rosewell63426cb2015-09-18 19:53:05 +0100241 return;
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000242 }
James Rosewell63426cb2015-09-18 19:53:05 +0100243
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200244 memcpy(cache_entry->area, smp->data.u.str.area, smp->data.u.str.data);
245 cache_entry->area[smp->data.u.str.data] = 0;
246 cache_entry->data = smp->data.u.str.data;
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000247 HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock);
ben@51degrees.comc9dfa242016-01-08 13:47:46 +0000248 lru64_commit(lru, cache_entry, domain, 0, _51d_lru_free);
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000249 HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
James Rosewell63426cb2015-09-18 19:53:05 +0100250}
251
252/* Retrieves the data from the cache and sets the sample data to this string.
253 */
254static void _51d_retrieve_cache_entry(struct sample *smp, struct lru64 *lru)
255{
Willy Tarreau83061a82018-07-13 11:56:34 +0200256 struct buffer *cache_entry = lru->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200257 smp->data.u.str.area = cache_entry->area;
258 smp->data.u.str.data = cache_entry->data;
James Rosewell63426cb2015-09-18 19:53:05 +0100259}
260#endif
261
262#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100263/* Sets the important HTTP headers ahead of the detection
264 */
265static void _51d_set_headers(struct sample *smp, fiftyoneDegreesWorkset *ws)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200266{
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200267 struct channel *chn;
268 struct htx *htx;
269 struct http_hdr_ctx ctx;
270 struct ist name;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200271 int i;
James Rosewella28bbd52015-09-18 18:28:52 +0100272
James Rosewella28bbd52015-09-18 18:28:52 +0100273 ws->importantHeadersCount = 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200274 chn = (smp->strm ? &smp->strm->req : NULL);
Ben51Degrees31a51f22019-06-12 15:19:12 +0100275
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200276 // No need to null check as this has already been carried out in the
277 // calling method
Christopher Fauletf11b1fb2020-05-05 11:53:43 +0200278 htx = smp_prefetch_htx(smp, chn, NULL, 1);
Dragan Dosend1ba5522020-07-01 19:58:32 +0200279 ALREADY_CHECKED(htx);
Ben51Degrees31a51f22019-06-12 15:19:12 +0100280
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200281 for (i = 0; i < global_51degrees.header_count; i++) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100282 name = ist2((global_51degrees.header_names + i)->area,
283 (global_51degrees.header_names + i)->data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200284 ctx.blk = NULL;
Ben51Degrees31a51f22019-06-12 15:19:12 +0100285
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200286 if (http_find_header(htx, name, &ctx, 1)) {
287 ws->importantHeaders[ws->importantHeadersCount].header = ws->dataSet->httpHeaders + i;
288 ws->importantHeaders[ws->importantHeadersCount].headerValue = ctx.value.ptr;
289 ws->importantHeaders[ws->importantHeadersCount].headerValueLength = ctx.value.len;
290 ws->importantHeadersCount++;
James Rosewella28bbd52015-09-18 18:28:52 +0100291 }
292 }
293}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200294#endif
James Rosewella28bbd52015-09-18 18:28:52 +0100295
Dragan Dosen93b38d92015-06-29 16:43:25 +0200296#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000297static void _51d_init_device_offsets(fiftyoneDegreesDeviceOffsets *offsets) {
298 int i;
299 for (i = 0; i < global_51degrees.data_set.uniqueHttpHeaders.count; i++) {
300 offsets->firstOffset[i].userAgent = NULL;
301 }
302}
303
304static void _51d_set_device_offsets(struct sample *smp, fiftyoneDegreesDeviceOffsets *offsets)
James Rosewella28bbd52015-09-18 18:28:52 +0100305{
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200306 struct channel *chn;
307 struct htx *htx;
308 struct http_hdr_ctx ctx;
309 struct ist name;
Ben51Degrees31a51f22019-06-12 15:19:12 +0100310 int i;
Dragan Dosen105c8e62015-06-29 16:43:26 +0200311
James Rosewella28bbd52015-09-18 18:28:52 +0100312 offsets->size = 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200313 chn = (smp->strm ? &smp->strm->req : NULL);
Ben51Degrees31a51f22019-06-12 15:19:12 +0100314
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200315 // No need to null check as this has already been carried out in the
316 // calling method
Christopher Fauletf11b1fb2020-05-05 11:53:43 +0200317 htx = smp_prefetch_htx(smp, chn, NULL, 1);
Dragan Dosend1ba5522020-07-01 19:58:32 +0200318 ALREADY_CHECKED(htx);
Ben51Degrees31a51f22019-06-12 15:19:12 +0100319
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200320 for (i = 0; i < global_51degrees.header_count; i++) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100321 name = ist2((global_51degrees.header_names + i)->area,
322 (global_51degrees.header_names + i)->data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200323 ctx.blk = NULL;
Ben51Degrees31a51f22019-06-12 15:19:12 +0100324
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200325 if (http_find_header(htx, name, &ctx, 1)) {
326 (offsets->firstOffset + offsets->size)->httpHeaderOffset = *(global_51degrees.header_offsets + i);
327 (offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set, ctx.value.ptr);
328 offsets->size++;
Ben51Degrees31a51f22019-06-12 15:19:12 +0100329 }
Ben51Degrees31a51f22019-06-12 15:19:12 +0100330 }
Ben51Degrees31a51f22019-06-12 15:19:12 +0100331
James Rosewella28bbd52015-09-18 18:28:52 +0100332}
333#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200334
335#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100336/* Provides a hash code for the important HTTP headers.
337 */
338unsigned long long _51d_req_hash(const struct arg *args, fiftyoneDegreesWorkset* ws)
339{
340 unsigned long long seed = _51d_lru_seed ^ (long)args;
341 unsigned long long hash = 0;
342 int i;
343 for(i = 0; i < ws->importantHeadersCount; i++) {
344 hash ^= ws->importantHeaders[i].header->headerNameOffset;
Dragan Dosen967e7e72020-12-22 13:22:34 +0100345 hash ^= XXH3(ws->importantHeaders[i].headerValue,
346 ws->importantHeaders[i].headerValueLength,
347 seed);
James Rosewella28bbd52015-09-18 18:28:52 +0100348 }
349 return hash;
350}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200351#endif
352
Dragan Dosena9800a02022-02-14 13:05:45 +0100353#ifdef FIFTYONE_DEGREES_HASH_INCLUDED
354static int _51d_use_perf_graph(char **args, int section_type, struct proxy *curpx,
355 const struct proxy *defpx, const char *file, int line,
356 char **err)
357{
358 if (too_many_args(1, args, err, NULL))
359 return -1;
360
361 if (strcmp(args[1], "on") == 0)
362 global_51degrees.use_perf_graph = 1;
363 else if (strcmp(args[1], "off") == 0)
364 global_51degrees.use_perf_graph = 0;
365 else {
366 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
367 return -1;
368 }
369
370 return 0;
371}
372
373static int _51d_use_pred_graph(char **args, int section_type, struct proxy *curpx,
374 const struct proxy *defpx, const char *file, int line,
375 char **err)
376{
377 if (too_many_args(1, args, err, NULL))
378 return -1;
379
380 if (strcmp(args[1], "on") == 0)
381 global_51degrees.use_pred_graph = 1;
382 else if (strcmp(args[1], "off") == 0)
383 global_51degrees.use_pred_graph = 0;
384 else {
385 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
386 return -1;
387 }
388
389 return 0;
390}
391
392static int _51d_drift(char **args, int section_type, struct proxy *curpx,
393 const struct proxy *defpx, const char *file, int line,
394 char **err)
395{
396 if (*(args[1]) == 0) {
397 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
398 return -1;
399 }
400
401 global_51degrees.drift = atoi(args[1]);
402 if (global_51degrees.drift < 0) {
403 memprintf(err, "'%s' expects a positive numeric value, got '%s'.",
404 args[0], args[1]);
405 return -1;
406 }
407
408 return 0;
409}
410
411static int _51d_difference(char **args, int section_type, struct proxy *curpx,
412 const struct proxy *defpx, const char *file, int line,
413 char **err)
414{
415 if (*(args[1]) == 0) {
416 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
417 return -1;
418 }
419
420 global_51degrees.difference = atoi(args[1]);
421 if (global_51degrees.difference < 0) {
422 memprintf(err, "'%s' expects a positive numeric value, got '%s'.",
423 args[0], args[1]);
424 return -1;
425 }
426
427 return 0;
428}
429
430static int _51d_allow_unmatched(char **args, int section_type, struct proxy *curpx,
431 const struct proxy *defpx, const char *file, int line,
432 char **err)
433{
434 if (too_many_args(1, args, err, NULL))
435 return -1;
436
437 if (strcmp(args[1], "on") == 0)
438 global_51degrees.allow_unmatched = 1;
439 else if (strcmp(args[1], "off") == 0)
440 global_51degrees.allow_unmatched = 0;
441 else {
442 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
443 return -1;
444 }
445
446 return 0;
447}
448
449static int _51d_init_internal()
450{
451 fiftyoneDegreesDataSetHash *ds;
452 int hdr_count;
453 int i, ret = 0;
454
455 ds = (fiftyoneDegreesDataSetHash *)fiftyoneDegreesDataSetGet(&global_51degrees.manager);
456
457 hdr_count = ds->b.b.uniqueHeaders->count;
458 if (hdr_count > _51d_headers.max)
459 hdr_count = _51d_headers.max;
460
461 _51d_results = fiftyoneDegreesResultsHashCreate(&global_51degrees.manager, hdr_count, 0);
462 if (!_51d_results)
463 goto out;
464
465 for (i = 0; i < hdr_count; i++) {
466 _51d_headers.buf[i] = malloc(_51D_HEADERS_BUFFER_SIZE);
467 if (!_51d_headers.buf[i])
468 goto out;
469 _51d_headers.count++;
470 }
471
472 /* success */
473 ret = 1;
474
475out:
476 fiftyoneDegreesDataSetRelease((fiftyoneDegreesDataSetBase *)ds);
477 return ret;
478}
479
480static fiftyoneDegreesEvidenceKeyValuePairArray * _51d_get_evidence(struct sample *smp)
481{
482 fiftyoneDegreesEvidenceKeyValuePairArray *evidence;
483 fiftyoneDegreesDataSetHash *ds;
484 size_t size;
485 struct channel *chn;
486 struct htx *htx;
487 struct http_hdr_ctx ctx;
488 struct ist name;
489 int i;
490
491 chn = (smp->strm ? &smp->strm->req : NULL);
492
493 // No need to null check as this has already been carried out in the
494 // calling method
495 htx = smp_prefetch_htx(smp, chn, NULL, 1);
496 ALREADY_CHECKED(htx);
497
498 ds = (fiftyoneDegreesDataSetHash *)_51d_results->b.b.dataSet;
499 size = _51d_headers.count * 2;
500
501 evidence = fiftyoneDegreesEvidenceCreate(size);
502 if (!evidence)
503 return NULL;
504
505 for (i = 0; i < _51d_headers.count; i++) {
506 fiftyoneDegreesHeader *hdr = &ds->b.b.uniqueHeaders->items[i];
507 name = ist2(hdr->name, hdr->nameLength);
508 ctx.blk = NULL;
509
510 if (http_find_header(htx, name, &ctx, 1)) {
511 size_t len = ctx.value.len;
512
513 if (unlikely(len >= _51D_HEADERS_BUFFER_SIZE))
514 len = _51D_HEADERS_BUFFER_SIZE - 1;
515
516 memcpy(_51d_headers.buf[i], ctx.value.ptr, len);
517 _51d_headers.buf[i][len] = '\0';
518
519 fiftyoneDegreesEvidenceAddString(
520 evidence,
521 FIFTYONE_DEGREES_EVIDENCE_HTTP_HEADER_STRING,
522 name.ptr,
523 _51d_headers.buf[i]);
524 }
525 }
526
527 return evidence;
528}
529#endif
530
531#if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) || defined(FIFTYONEDEGREES_H_TRIE_INCLUDED)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200532#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100533static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesWorkset* ws)
534{
535 char *methodName;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200536#endif
537#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000538static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesDeviceOffsets *offsets)
James Rosewella28bbd52015-09-18 18:28:52 +0100539{
540 char valuesBuffer[1024];
Willy Tarreaub7a67142016-12-21 21:18:44 +0100541 const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(&global_51degrees.data_set);
542 int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(&global_51degrees.data_set);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200543#endif
Dragan Dosena9800a02022-02-14 13:05:45 +0100544 const char* property_name;
545 int j;
546
547#elif defined(FIFTYONE_DEGREES_HASH_INCLUDED)
548static void _51d_process_match(const struct arg *args, struct sample *smp)
549{
550 char valuesBuffer[1024];
551#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200552
James Rosewella28bbd52015-09-18 18:28:52 +0100553 char no_data[] = "NoData"; /* response when no data could be found */
Willy Tarreau83061a82018-07-13 11:56:34 +0200554 struct buffer *temp = get_trash_chunk();
Dragan Dosena9800a02022-02-14 13:05:45 +0100555 int i = 0, found;
556
557#if defined(FIFTYONE_DEGREES_HASH_INCLUDED)
558 FIFTYONE_DEGREES_EXCEPTION_CREATE;
559#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200560
561 /* Loop through property names passed to the filter and fetch them from the dataset. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200562 while (args[i].data.str.area) {
Dragan Dosen93b38d92015-06-29 16:43:25 +0200563 /* Try to find request property in dataset. */
Dragan Dosen93b38d92015-06-29 16:43:25 +0200564 found = 0;
James Rosewella28bbd52015-09-18 18:28:52 +0100565#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200566 if (strcmp("Method", args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100567 switch(ws->method) {
568 case EXACT: methodName = "Exact"; break;
569 case NUMERIC: methodName = "Numeric"; break;
570 case NEAREST: methodName = "Nearest"; break;
571 case CLOSEST: methodName = "Closest"; break;
572 default:
573 case NONE: methodName = "None"; break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200574 }
James Rosewella28bbd52015-09-18 18:28:52 +0100575 chunk_appendf(temp, "%s", methodName);
576 found = 1;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200577 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200578 else if (strcmp("Difference", args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100579 chunk_appendf(temp, "%d", ws->difference);
580 found = 1;
581 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200582 else if (strcmp("Rank", args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100583 chunk_appendf(temp, "%d", fiftyoneDegreesGetSignatureRank(ws));
584 found = 1;
585 }
586 else {
587 for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) {
588 property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200589 if (strcmp(property_name, args[i].data.str.area) == 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100590 found = 1;
591 fiftyoneDegreesSetValues(ws, j);
592 chunk_appendf(temp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values));
593 break;
594 }
595 }
Dragan Dosen93b38d92015-06-29 16:43:25 +0200596 }
597#endif
598#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100599 found = 0;
600 for (j = 0; j < requiredPropertiesCount; j++) {
601 property_name = requiredProperties[j];
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200602 if (strcmp(property_name, args[i].data.str.area) == 0 &&
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000603 fiftyoneDegreesGetValueFromOffsets(&global_51degrees.data_set, offsets, j, valuesBuffer, 1024) > 0) {
James Rosewella28bbd52015-09-18 18:28:52 +0100604 found = 1;
605 chunk_appendf(temp, "%s", valuesBuffer);
606 break;
607 }
Dragan Dosen93b38d92015-06-29 16:43:25 +0200608 }
James Rosewella28bbd52015-09-18 18:28:52 +0100609#endif
Dragan Dosena9800a02022-02-14 13:05:45 +0100610#ifdef FIFTYONE_DEGREES_HASH_INCLUDED
611 FIFTYONE_DEGREES_EXCEPTION_CLEAR;
612
613 found = fiftyoneDegreesResultsHashGetValuesString(
614 _51d_results, args[i].data.str.area,
615 valuesBuffer, 1024, "|",
616 exception);
617
618 if (FIFTYONE_DEGREES_EXCEPTION_FAILED || found <= 0)
619 found = 0;
620 else
621 chunk_appendf(temp, "%s", valuesBuffer);
622#endif
ben@51degrees.comd3842522016-01-08 13:52:32 +0000623 if (!found)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200624 chunk_appendf(temp, "%s", no_data);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000625
Dragan Dosen93b38d92015-06-29 16:43:25 +0200626 /* Add separator. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100627 chunk_appendf(temp, "%c", global_51degrees.property_separator);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200628 ++i;
629 }
630
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200631 if (temp->data) {
632 --temp->data;
633 temp->area[temp->data] = '\0';
Dragan Dosen93b38d92015-06-29 16:43:25 +0200634 }
635
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200636 smp->data.u.str.area = temp->area;
637 smp->data.u.str.data = temp->data;
James Rosewella28bbd52015-09-18 18:28:52 +0100638}
639
Ben51Degrees6bf06722020-01-20 11:25:11 +0000640/* Sets the sample data as a constant string. This ensures that the
641 * string will be processed correctly.
642 */
643static void _51d_set_smp(struct sample *smp)
644{
645 /*
646 * Data type has to be set to ensure the string output is processed
647 * correctly.
648 */
649 smp->data.type = SMP_T_STR;
650
651 /* Flags the sample to show it uses constant memory. */
652 smp->flags |= SMP_F_CONST;
653}
654
James Rosewella28bbd52015-09-18 18:28:52 +0100655static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
656{
Dragan Dosena9800a02022-02-14 13:05:45 +0100657 struct channel *chn;
658 struct htx *htx;
James Rosewella28bbd52015-09-18 18:28:52 +0100659#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
660 fiftyoneDegreesWorkset* ws; /* workset for detection */
661 struct lru64 *lru = NULL;
662#endif
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000663#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
664 fiftyoneDegreesDeviceOffsets *offsets; /* Offsets for detection */
665#endif
Dragan Dosena9800a02022-02-14 13:05:45 +0100666#ifdef FIFTYONE_DEGREES_HASH_INCLUDED
667 fiftyoneDegreesEvidenceKeyValuePairArray *evidence = NULL;
668 FIFTYONE_DEGREES_EXCEPTION_CREATE;
669#endif
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200670
Ben51Degrees31a51f22019-06-12 15:19:12 +0100671 chn = (smp->strm ? &smp->strm->req : NULL);
Christopher Fauletf11b1fb2020-05-05 11:53:43 +0200672 htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200673 if (!htx)
674 return 0;
James Rosewella28bbd52015-09-18 18:28:52 +0100675
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000676
James Rosewella28bbd52015-09-18 18:28:52 +0100677#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
678
679 /* Get only the headers needed for device detection so they can be used
680 * with the cache to return previous results. Pattern is slower than
681 * Trie so caching will help improve performance.
682 */
683
684 /* Get a workset from the pool which will later contain detection results. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100685 ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool);
James Rosewella28bbd52015-09-18 18:28:52 +0100686 if (!ws)
687 return 0;
688
689 /* Set the important HTTP headers for this request in the workset. */
690 _51d_set_headers(smp, ws);
691
692 /* Check the cache to see if there's results for these headers already. */
693 if (_51d_lru_tree) {
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000694 HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock);
695
James Rosewella28bbd52015-09-18 18:28:52 +0100696 lru = lru64_get(_51d_req_hash(args, ws),
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000697 _51d_lru_tree, (void*)args, 0);
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000698
James Rosewella28bbd52015-09-18 18:28:52 +0100699 if (lru && lru->domain) {
Willy Tarreaub7a67142016-12-21 21:18:44 +0100700 fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
James Rosewell63426cb2015-09-18 19:53:05 +0100701 _51d_retrieve_cache_entry(smp, lru);
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000702 HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
Ben51Degrees6bf06722020-01-20 11:25:11 +0000703
704 _51d_set_smp(smp);
James Rosewella28bbd52015-09-18 18:28:52 +0100705 return 1;
706 }
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000707 HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
James Rosewella28bbd52015-09-18 18:28:52 +0100708 }
709
710 fiftyoneDegreesMatchForHttpHeaders(ws);
711
712 _51d_process_match(args, smp, ws);
713
714#endif
715
716#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000717#ifndef FIFTYONEDEGREES_NO_THREADING
718 offsets = fiftyoneDegreesCreateDeviceOffsets(&global_51degrees.data_set);
719 _51d_init_device_offsets(offsets);
720#else
721 offsets = &global_51degrees.device_offsets;
722#endif
James Rosewella28bbd52015-09-18 18:28:52 +0100723
724 /* Trie is very fast so all the headers can be passed in and the result
725 * returned faster than the hashing algorithm process.
726 */
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000727 _51d_set_device_offsets(smp, offsets);
728 _51d_process_match(args, smp, offsets);
James Rosewella28bbd52015-09-18 18:28:52 +0100729
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000730#ifndef FIFTYONEDEGREES_NO_THREADING
731 fiftyoneDegreesFreeDeviceOffsets(offsets);
James Rosewella28bbd52015-09-18 18:28:52 +0100732#endif
733
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000734#endif
735
James Rosewella28bbd52015-09-18 18:28:52 +0100736#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100737 fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000738 if (lru)
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000739 _51d_insert_cache_entry(smp, lru, (void*)args);
James Rosewella28bbd52015-09-18 18:28:52 +0100740#endif
741
Dragan Dosena9800a02022-02-14 13:05:45 +0100742#ifdef FIFTYONE_DEGREES_HASH_INCLUDED
743 evidence = _51d_get_evidence(smp);
744 if (!evidence)
745 return 0;
746
747 fiftyoneDegreesResultsHashFromEvidence(
748 _51d_results, evidence, exception);
749 fiftyoneDegreesEvidenceFree(evidence);
750
751 if (FIFTYONE_DEGREES_EXCEPTION_FAILED)
752 return 0;
753
754 _51d_process_match(args, smp);
755#endif
756
Ben51Degrees6bf06722020-01-20 11:25:11 +0000757 _51d_set_smp(smp);
James Rosewella28bbd52015-09-18 18:28:52 +0100758 return 1;
759}
Dragan Dosen93b38d92015-06-29 16:43:25 +0200760
James Rosewella28bbd52015-09-18 18:28:52 +0100761static int _51d_conv(const struct arg *args, struct sample *smp, void *private)
762{
Dragan Dosen93b38d92015-06-29 16:43:25 +0200763#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
James Rosewella28bbd52015-09-18 18:28:52 +0100764 fiftyoneDegreesWorkset* ws; /* workset for detection */
765 struct lru64 *lru = NULL;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200766#endif
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000767#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
768 fiftyoneDegreesDeviceOffsets *offsets; /* Offsets for detection */
769#endif
Dragan Dosena9800a02022-02-14 13:05:45 +0100770#ifdef FIFTYONE_DEGREES_HASH_INCLUDED
771 FIFTYONE_DEGREES_EXCEPTION_CREATE;
772#endif
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000773
James Rosewella28bbd52015-09-18 18:28:52 +0100774#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
775
776 /* Look in the list. */
777 if (_51d_lru_tree) {
778 unsigned long long seed = _51d_lru_seed ^ (long)args;
779
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000780 HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock);
Dragan Dosen967e7e72020-12-22 13:22:34 +0100781 lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed),
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000782 _51d_lru_tree, (void*)args, 0);
James Rosewella28bbd52015-09-18 18:28:52 +0100783 if (lru && lru->domain) {
James Rosewell63426cb2015-09-18 19:53:05 +0100784 _51d_retrieve_cache_entry(smp, lru);
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000785 HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
James Rosewella28bbd52015-09-18 18:28:52 +0100786 return 1;
787 }
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000788 HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
James Rosewella28bbd52015-09-18 18:28:52 +0100789 }
790
791 /* Create workset. This will later contain detection results. */
Willy Tarreaub7a67142016-12-21 21:18:44 +0100792 ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool);
James Rosewella28bbd52015-09-18 18:28:52 +0100793 if (!ws)
794 return 0;
795#endif
796
797 /* Duplicate the data and remove the "const" flag before device detection. */
798 if (!smp_dup(smp))
799 return 0;
800
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200801 smp->data.u.str.area[smp->data.u.str.data] = '\0';
James Rosewella28bbd52015-09-18 18:28:52 +0100802
803 /* Perform detection. */
Dragan Dosena9800a02022-02-14 13:05:45 +0100804#if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) || defined(FIFTYONEDEGREES_H_TRIE_INCLUDED)
James Rosewella28bbd52015-09-18 18:28:52 +0100805#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200806 fiftyoneDegreesMatch(ws, smp->data.u.str.area);
James Rosewella28bbd52015-09-18 18:28:52 +0100807 _51d_process_match(args, smp, ws);
808#endif
809#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000810#ifndef FIFTYONEDEGREES_NO_THREADING
811 offsets = fiftyoneDegreesCreateDeviceOffsets(&global_51degrees.data_set);
812 _51d_init_device_offsets(offsets);
813#else
814 offsets = &global_51degrees.device_offsets;
815#endif
816
817 offsets->firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set,
818 smp->data.u.str.area);
819 offsets->size = 1;
820 _51d_process_match(args, smp, offsets);
James Rosewella28bbd52015-09-18 18:28:52 +0100821#endif
822
823#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreaub7a67142016-12-21 21:18:44 +0100824 fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
ben@51degrees.comd3842522016-01-08 13:52:32 +0000825 if (lru)
ben@51degrees.com82a9d762016-01-08 13:42:41 +0000826 _51d_insert_cache_entry(smp, lru, (void*)args);
James Rosewella28bbd52015-09-18 18:28:52 +0100827#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200828
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000829#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
830#ifndef FIFTYONEDEGREES_NO_THREADING
831 fiftyoneDegreesFreeDeviceOffsets(offsets);
832#endif
833#endif
834
Dragan Dosena9800a02022-02-14 13:05:45 +0100835#elif defined(FIFTYONE_DEGREES_HASH_INCLUDED)
836 fiftyoneDegreesResultsHashFromUserAgent(_51d_results, smp->data.u.str.area,
837 smp->data.u.str.data, exception);
838 if (FIFTYONE_DEGREES_EXCEPTION_FAILED)
839 return 0;
840
841 _51d_process_match(args, smp);
842#endif
843
Ben51Degrees6bf06722020-01-20 11:25:11 +0000844 _51d_set_smp(smp);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200845 return 1;
846}
847
James Rosewella28bbd52015-09-18 18:28:52 +0100848#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
849void _51d_init_http_headers()
850{
851 int index = 0;
852 const fiftyoneDegreesAsciiString *headerName;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100853 fiftyoneDegreesDataSet *ds = &global_51degrees.data_set;
854 global_51degrees.header_count = ds->httpHeadersCount;
Willy Tarreau83061a82018-07-13 11:56:34 +0200855 global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer));
Willy Tarreaub7a67142016-12-21 21:18:44 +0100856 for (index = 0; index < global_51degrees.header_count; index++) {
James Rosewella28bbd52015-09-18 18:28:52 +0100857 headerName = fiftyoneDegreesGetString(ds, ds->httpHeaders[index].headerNameOffset);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200858 (global_51degrees.header_names + index)->area = (char*)&headerName->firstByte;
859 (global_51degrees.header_names + index)->data = headerName->length - 1;
860 (global_51degrees.header_names + index)->size = (global_51degrees.header_names + index)->data;
James Rosewella28bbd52015-09-18 18:28:52 +0100861 }
862}
863#endif
864
865#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
866void _51d_init_http_headers()
867{
868 int index = 0;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100869 fiftyoneDegreesDataSet *ds = &global_51degrees.data_set;
870 global_51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(ds);
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000871#ifdef FIFTYONEDEGREES_NO_THREADING
Willy Tarreaub7a67142016-12-21 21:18:44 +0100872 global_51degrees.device_offsets.firstOffset = malloc(
873 global_51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset));
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000874 _51d_init_device_offsets(&global_51degrees.device_offsets);
875#endif
Willy Tarreau83061a82018-07-13 11:56:34 +0200876 global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer));
Willy Tarreaub7a67142016-12-21 21:18:44 +0100877 global_51degrees.header_offsets = malloc(global_51degrees.header_count * sizeof(int32_t));
878 for (index = 0; index < global_51degrees.header_count; index++) {
879 global_51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(ds, index);
Ben51Degreese0f6a2a2019-02-05 13:23:06 +0000880 global_51degrees.header_names[index].area = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(ds, index);
881 global_51degrees.header_names[index].data = strlen(global_51degrees.header_names[index].area);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200882 global_51degrees.header_names[index].size = global_51degrees.header_names->data;
James Rosewella28bbd52015-09-18 18:28:52 +0100883 }
884}
885#endif
886
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100887/*
888 * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*.
889 */
890static int init_51degrees(void)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200891{
892 int i = 0;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200893 struct _51d_property_names *name;
894 char **_51d_property_list = NULL;
Dragan Dosena9800a02022-02-14 13:05:45 +0100895#if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) || defined(FIFTYONEDEGREES_H_TRIE_INCLUDED)
896 struct buffer *temp;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200897 fiftyoneDegreesDataSetInitStatus _51d_dataset_status = DATA_SET_INIT_STATUS_NOT_SET;
Dragan Dosena9800a02022-02-14 13:05:45 +0100898#elif defined(FIFTYONE_DEGREES_HASH_INCLUDED)
899 fiftyoneDegreesConfigHash config = fiftyoneDegreesHashInMemoryConfig;
900 fiftyoneDegreesPropertiesRequired properties = fiftyoneDegreesPropertiesDefault;
901 fiftyoneDegreesMemoryReader reader;
902 fiftyoneDegreesStatusCode status;
903 FIFTYONE_DEGREES_EXCEPTION_CREATE;
904#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +0200905
Willy Tarreaub7a67142016-12-21 21:18:44 +0100906 if (!global_51degrees.data_file_path)
Christopher Fauletfc633b62020-11-06 15:24:23 +0100907 return ERR_NONE;
Dragan Dosen9373fc52015-08-07 16:41:23 +0200908
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000909 if (global.nbthread < 1) {
910 ha_alert("51Degrees: The thread count cannot be zero or negative.\n");
Christopher Faulete8ca4342017-10-25 17:23:02 +0200911 return (ERR_FATAL | ERR_ALERT);
912 }
913
Willy Tarreaub7a67142016-12-21 21:18:44 +0100914 if (!LIST_ISEMPTY(&global_51degrees.property_names)) {
Dragan Dosen93b38d92015-06-29 16:43:25 +0200915 i = 0;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100916 list_for_each_entry(name, &global_51degrees.property_names, list)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200917 ++i;
Tim Duesterhuse52b6e52020-09-12 20:26:43 +0200918 _51d_property_list = calloc(i, sizeof(*_51d_property_list));
Dragan Dosen93b38d92015-06-29 16:43:25 +0200919
920 i = 0;
Willy Tarreaub7a67142016-12-21 21:18:44 +0100921 list_for_each_entry(name, &global_51degrees.property_names, list)
Dragan Dosen93b38d92015-06-29 16:43:25 +0200922 _51d_property_list[i++] = name->name;
923 }
924
Dragan Dosena9800a02022-02-14 13:05:45 +0100925#if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) || defined(FIFTYONEDEGREES_H_TRIE_INCLUDED)
Willy Tarreaub7a67142016-12-21 21:18:44 +0100926 _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 +0200927
928 temp = get_trash_chunk();
929 chunk_reset(temp);
930
931 switch (_51d_dataset_status) {
932 case DATA_SET_INIT_STATUS_SUCCESS:
James Rosewella28bbd52015-09-18 18:28:52 +0100933#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000934 global_51degrees.pool = fiftyoneDegreesWorksetPoolCreate(&global_51degrees.data_set, NULL, global.nbthread);
James Rosewella28bbd52015-09-18 18:28:52 +0100935#endif
936 _51d_init_http_headers();
Dragan Dosen93b38d92015-06-29 16:43:25 +0200937 break;
938 case DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY:
939 chunk_printf(temp, "Insufficient memory.");
940 break;
941 case DATA_SET_INIT_STATUS_CORRUPT_DATA:
942#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
943 chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Pattern data format.");
944#endif
945#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
946 chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Trie data format.");
947#endif
948 break;
949 case DATA_SET_INIT_STATUS_INCORRECT_VERSION:
950#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
951 chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Pattern data format.");
952#endif
953#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
954 chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Trie data format.");
955#endif
956 break;
957 case DATA_SET_INIT_STATUS_FILE_NOT_FOUND:
958 chunk_printf(temp, "File not found.");
959 break;
ben51degrees1f077eb2016-07-06 12:07:21 +0100960 case DATA_SET_INIT_STATUS_NULL_POINTER:
961 chunk_printf(temp, "Null pointer to the existing dataset or memory location.");
962 break;
963 case DATA_SET_INIT_STATUS_POINTER_OUT_OF_BOUNDS:
964 chunk_printf(temp, "Allocated continuous memory containing 51Degrees data file appears to be smaller than expected. Most likely"
965 " because the data file was not fully loaded into the allocated memory.");
966 break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200967 case DATA_SET_INIT_STATUS_NOT_SET:
968 chunk_printf(temp, "Data set not initialised.");
969 break;
Dragan Dosen483b93c2017-09-27 12:46:44 +0200970 default:
971 chunk_printf(temp, "Other error.");
972 break;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200973 }
974 if (_51d_dataset_status != DATA_SET_INIT_STATUS_SUCCESS) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200975 if (temp->data)
976 ha_alert("51Degrees Setup - Error reading 51Degrees data file. %s\n",
977 temp->area);
Dragan Dosen93b38d92015-06-29 16:43:25 +0200978 else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100979 ha_alert("51Degrees Setup - Error reading 51Degrees data file.\n");
Willy Tarreau9f3f2542016-12-21 20:30:05 +0100980 return ERR_ALERT | ERR_FATAL;
Dragan Dosen93b38d92015-06-29 16:43:25 +0200981 }
982 free(_51d_property_list);
983
James Rosewella28bbd52015-09-18 18:28:52 +0100984#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Willy Tarreau52bf8392020-03-08 00:42:37 +0100985 _51d_lru_seed = ha_random();
Willy Tarreaub7a67142016-12-21 21:18:44 +0100986 if (global_51degrees.cache_size) {
987 _51d_lru_tree = lru64_new(global_51degrees.cache_size);
James Rosewella28bbd52015-09-18 18:28:52 +0100988 }
989#endif
Dragan Dosen105c8e62015-06-29 16:43:26 +0200990
Dragan Dosena9800a02022-02-14 13:05:45 +0100991#elif defined(FIFTYONE_DEGREES_HASH_INCLUDED)
992 config.b.b.freeData = true;
993
994 if (global_51degrees.use_perf_graph != -1)
995 config.usePerformanceGraph = global_51degrees.use_perf_graph;
996 if (global_51degrees.use_pred_graph != -1)
997 config.usePredictiveGraph = global_51degrees.use_pred_graph;
998
999 if (global_51degrees.drift > 0)
1000 config.drift = global_51degrees.drift;
1001 if (global_51degrees.difference > 0)
1002 config.difference = global_51degrees.difference;
1003
1004 if (global_51degrees.allow_unmatched != -1)
1005 config.b.allowUnmatched = global_51degrees.allow_unmatched;
1006
1007 config.strings.concurrency =
1008 config.properties.concurrency =
1009 config.values.concurrency =
1010 config.profiles.concurrency =
1011 config.nodes.concurrency =
1012 config.profileOffsets.concurrency =
1013 config.maps.concurrency =
1014 config.components.concurrency =
1015 config.rootNodes.concurrency = global.nbthread;
1016
1017 properties.array = (const char **)_51d_property_list;
1018 properties.count = i;
1019
1020 status = fiftyoneDegreesFileReadToByteArray(global_51degrees.data_file_path, &reader);
1021 if (status == FIFTYONE_DEGREES_STATUS_SUCCESS && !FIFTYONE_DEGREES_EXCEPTION_FAILED) {
1022 FIFTYONE_DEGREES_EXCEPTION_CLEAR;
1023
1024 status = fiftyoneDegreesHashInitManagerFromMemory(
1025 &global_51degrees.manager,
1026 &config,
1027 &properties,
1028 reader.startByte,
1029 reader.length,
1030 exception);
1031 }
1032
1033 free(_51d_property_list);
1034 _51d_property_list = NULL;
1035 i = 0;
1036
1037 if (status != FIFTYONE_DEGREES_STATUS_SUCCESS || FIFTYONE_DEGREES_EXCEPTION_FAILED) {
1038 const char *message = fiftyoneDegreesStatusGetMessage(status, global_51degrees.data_file_path);
1039 if (message)
1040 ha_alert("51Degrees Setup - Error reading 51Degrees data file. %s\n",
1041 message);
1042 else
1043 ha_alert("51Degrees Setup - Error reading 51Degrees data file.\n");
1044 return ERR_ALERT | ERR_FATAL;
1045 }
1046#endif
1047
Christopher Fauletfc633b62020-11-06 15:24:23 +01001048 return ERR_NONE;
Dragan Dosen93b38d92015-06-29 16:43:25 +02001049}
1050
Willy Tarreau7ac4c202016-12-21 20:59:01 +01001051static void deinit_51degrees(void)
Dragan Dosen93b38d92015-06-29 16:43:25 +02001052{
1053 struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb;
1054
Dragan Dosena9800a02022-02-14 13:05:45 +01001055#if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) || defined(FIFTYONEDEGREES_H_TRIE_INCLUDED)
Willy Tarreaub7a67142016-12-21 21:18:44 +01001056 free(global_51degrees.header_names);
Dragan Dosen93b38d92015-06-29 16:43:25 +02001057#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosenbc6218e2019-03-07 15:24:23 +01001058 if (global_51degrees.pool)
1059 fiftyoneDegreesWorksetPoolFree(global_51degrees.pool);
Dragan Dosen93b38d92015-06-29 16:43:25 +02001060#endif
1061#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
Ben51Degrees4ddf59d2019-02-05 13:24:00 +00001062#ifdef FIFTYONEDEGREES_NO_THREADING
Willy Tarreaub7a67142016-12-21 21:18:44 +01001063 free(global_51degrees.device_offsets.firstOffset);
Ben51Degrees4ddf59d2019-02-05 13:24:00 +00001064#endif
Willy Tarreaub7a67142016-12-21 21:18:44 +01001065 free(global_51degrees.header_offsets);
Dragan Dosen93b38d92015-06-29 16:43:25 +02001066#endif
Willy Tarreaub7a67142016-12-21 21:18:44 +01001067 fiftyoneDegreesDataSetFree(&global_51degrees.data_set);
Dragan Dosena9800a02022-02-14 13:05:45 +01001068#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +02001069
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001070 ha_free(&global_51degrees.data_file_path);
Willy Tarreaub7a67142016-12-21 21:18:44 +01001071 list_for_each_entry_safe(_51d_prop_name, _51d_prop_nameb, &global_51degrees.property_names, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001072 LIST_DELETE(&_51d_prop_name->list);
Dragan Dosen93b38d92015-06-29 16:43:25 +02001073 free(_51d_prop_name);
1074 }
Dragan Dosen105c8e62015-06-29 16:43:26 +02001075
James Rosewella28bbd52015-09-18 18:28:52 +01001076#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
Dragan Dosen105c8e62015-06-29 16:43:26 +02001077 while (lru64_destroy(_51d_lru_tree));
James Rosewella28bbd52015-09-18 18:28:52 +01001078#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +02001079}
Dragan Dosena9800a02022-02-14 13:05:45 +01001080
1081#ifdef FIFTYONE_DEGREES_HASH_INCLUDED
1082static int init_51degrees_per_thread()
1083{
1084 if (!global_51degrees.data_file_path) {
1085 /* noop */
1086 return 1;
1087 }
1088
1089 _51d_headers.max = global.tune.max_http_hdr;
1090 _51d_headers.buf = calloc(_51d_headers.max, sizeof(*_51d_headers.buf));
1091 _51d_headers.count = 0;
1092
1093 if (!_51d_headers.buf)
1094 return 0;
1095
1096 if (!_51d_init_internal())
1097 return 0;
1098
1099 return 1;
1100}
1101
1102static void deinit_51degrees_per_thread()
1103{
1104 int i;
1105
1106 if (_51d_results) {
1107 fiftyoneDegreesResultsHashFree(_51d_results);
1108 _51d_results = NULL;
1109 }
1110
1111 if (_51d_headers.buf) {
1112 for (i = 0; i < _51d_headers.max; i++)
1113 free(_51d_headers.buf[i]);
1114 free(_51d_headers.buf);
1115 _51d_headers.buf = NULL;
1116 }
1117
1118 _51d_headers.max = 0;
1119 _51d_headers.count = 0;
1120}
1121#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +02001122
1123static struct cfg_kw_list _51dcfg_kws = {{ }, {
1124 { CFG_GLOBAL, "51degrees-data-file", _51d_data_file },
1125 { CFG_GLOBAL, "51degrees-property-name-list", _51d_property_name_list },
1126 { CFG_GLOBAL, "51degrees-property-separator", _51d_property_separator },
Dragan Dosen105c8e62015-06-29 16:43:26 +02001127 { CFG_GLOBAL, "51degrees-cache-size", _51d_cache_size },
Dragan Dosena9800a02022-02-14 13:05:45 +01001128#ifdef FIFTYONE_DEGREES_HASH_INCLUDED
1129 { CFG_GLOBAL, "51degrees-use-performance-graph", _51d_use_perf_graph },
1130 { CFG_GLOBAL, "51degrees-use-predictive-graph", _51d_use_pred_graph },
1131 { CFG_GLOBAL, "51degrees-drift", _51d_drift },
1132 { CFG_GLOBAL, "51degrees-difference", _51d_difference },
1133 { CFG_GLOBAL, "51degrees-allow-unmatched", _51d_allow_unmatched },
1134#endif
Dragan Dosen93b38d92015-06-29 16:43:25 +02001135 { 0, NULL, NULL },
1136}};
1137
Willy Tarreau0108d902018-11-25 19:14:37 +01001138INITCALL1(STG_REGISTER, cfg_register_keywords, &_51dcfg_kws);
1139
Dragan Dosen93b38d92015-06-29 16:43:25 +02001140/* Note: must not be declared <const> as its list will be overwritten */
James Rosewella28bbd52015-09-18 18:28:52 +01001141static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1142 { "51d.all", _51d_fetch, ARG5(1,STR,STR,STR,STR,STR), _51d_fetch_check, SMP_T_STR, SMP_USE_HRQHV },
1143 { NULL, NULL, 0, 0, 0 },
1144}};
1145
Willy Tarreau0108d902018-11-25 19:14:37 +01001146INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
1147
James Rosewella28bbd52015-09-18 18:28:52 +01001148/* Note: must not be declared <const> as its list will be overwritten */
Dragan Dosen93b38d92015-06-29 16:43:25 +02001149static struct sample_conv_kw_list conv_kws = {ILH, {
James Rosewella28bbd52015-09-18 18:28:52 +01001150 { "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 +02001151 { NULL, NULL, 0, 0, 0 },
1152}};
1153
Willy Tarreau0108d902018-11-25 19:14:37 +01001154INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
1155
Willy Tarreau172f5ce2018-11-26 11:21:50 +01001156REGISTER_POST_CHECK(init_51degrees);
1157REGISTER_POST_DEINIT(deinit_51degrees);
Ben51Degreesf4a82fb2019-06-13 16:51:59 +01001158
1159#if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED)
1160#ifndef FIFTYONEDEGREES_DUMMY_LIB
1161 REGISTER_BUILD_OPTS("Built with 51Degrees Pattern support.");
1162#else
1163 REGISTER_BUILD_OPTS("Built with 51Degrees Pattern support (dummy library).");
1164#endif
1165#elif defined(FIFTYONEDEGREES_H_TRIE_INCLUDED)
1166#ifndef FIFTYONEDEGREES_DUMMY_LIB
1167 REGISTER_BUILD_OPTS("Built with 51Degrees Trie support.");
1168#else
1169 REGISTER_BUILD_OPTS("Built with 51Degrees Trie support (dummy library).");
1170#endif
Dragan Dosena9800a02022-02-14 13:05:45 +01001171#elif defined(FIFTYONE_DEGREES_HASH_INCLUDED)
1172 REGISTER_PER_THREAD_INIT(init_51degrees_per_thread);
1173 REGISTER_PER_THREAD_DEINIT(deinit_51degrees_per_thread);
1174#ifndef FIFTYONEDEGREES_DUMMY_LIB
1175 REGISTER_BUILD_OPTS("Built with 51Degrees V4 Hash support.");
1176#else
1177 REGISTER_BUILD_OPTS("Built with 51Degrees V4 Hash support (dummy library).");
1178#endif
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001179#endif