2015-06-29 14:43:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-06-09 07:07:15 +00:00
|
|
|
#include <import/lru.h>
|
2020-05-27 10:58:42 +00:00
|
|
|
#include <haproxy/api.h>
|
2020-06-09 07:07:15 +00:00
|
|
|
#include <haproxy/arg.h>
|
|
|
|
#include <haproxy/buf-t.h>
|
2020-06-04 22:00:29 +00:00
|
|
|
#include <haproxy/cfgparse.h>
|
2020-06-02 08:22:45 +00:00
|
|
|
#include <haproxy/chunk.h>
|
2020-05-27 14:10:29 +00:00
|
|
|
#include <haproxy/errors.h>
|
2020-06-09 07:07:15 +00:00
|
|
|
#include <haproxy/global.h>
|
2020-06-04 19:21:03 +00:00
|
|
|
#include <haproxy/http_ana.h>
|
2020-06-04 16:26:43 +00:00
|
|
|
#include <haproxy/http_fetch.h>
|
2020-06-04 07:08:41 +00:00
|
|
|
#include <haproxy/http_htx.h>
|
2020-06-04 13:33:47 +00:00
|
|
|
#include <haproxy/sample.h>
|
2020-06-09 07:07:15 +00:00
|
|
|
#include <haproxy/thread.h>
|
2020-06-05 15:27:29 +00:00
|
|
|
#include <haproxy/tools.h>
|
2021-09-11 15:51:13 +00:00
|
|
|
#include <haproxy/xxhash.h>
|
2016-12-21 20:18:44 +00:00
|
|
|
#include <51Degrees.h>
|
2015-06-29 14:43:25 +00:00
|
|
|
|
|
|
|
struct _51d_property_names {
|
|
|
|
struct list list;
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2015-06-29 14:43:26 +00:00
|
|
|
static struct lru64_head *_51d_lru_tree = NULL;
|
|
|
|
static unsigned long long _51d_lru_seed;
|
2019-02-05 13:24:00 +00:00
|
|
|
|
|
|
|
__decl_spinlock(_51d_lru_lock);
|
2015-09-18 17:28:52 +00:00
|
|
|
#endif
|
2015-06-29 14:43:26 +00:00
|
|
|
|
2016-12-21 20:18:44 +00:00
|
|
|
static struct {
|
|
|
|
char property_separator; /* the separator to use in the response for the values. this is taken from 51degrees-property-separator from config. */
|
|
|
|
struct list property_names; /* list of properties to load into the data set. this is taken from 51degrees-property-name-list from config. */
|
|
|
|
char *data_file_path;
|
|
|
|
int header_count; /* number of HTTP headers related to device detection. */
|
2018-07-13 09:56:34 +00:00
|
|
|
struct buffer *header_names; /* array of HTTP header names. */
|
2016-12-21 20:18:44 +00:00
|
|
|
fiftyoneDegreesDataSet data_set; /* data set used with the pattern and trie detection methods. */
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
|
|
|
fiftyoneDegreesWorksetPool *pool; /* pool of worksets to avoid creating a new one for each request. */
|
|
|
|
#endif
|
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
|
|
|
int32_t *header_offsets; /* offsets to the HTTP header name string. */
|
2019-02-05 13:24:00 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_NO_THREADING
|
2016-12-21 20:18:44 +00:00
|
|
|
fiftyoneDegreesDeviceOffsets device_offsets; /* Memory used for device offsets. */
|
2019-02-05 13:24:00 +00:00
|
|
|
#endif
|
2016-12-21 20:18:44 +00:00
|
|
|
#endif
|
|
|
|
int cache_size;
|
|
|
|
} global_51degrees = {
|
|
|
|
.property_separator = ',',
|
|
|
|
.property_names = LIST_HEAD_INIT(global_51degrees.property_names),
|
|
|
|
.data_file_path = NULL,
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
|
|
|
.data_set = { },
|
|
|
|
#endif
|
|
|
|
.cache_size = 0,
|
|
|
|
};
|
|
|
|
|
2015-06-29 14:43:25 +00:00
|
|
|
static int _51d_data_file(char **args, int section_type, struct proxy *curpx,
|
2021-03-09 08:53:46 +00:00
|
|
|
const struct proxy *defpx, const char *file, int line,
|
2015-06-29 14:43:25 +00:00
|
|
|
char **err)
|
|
|
|
{
|
|
|
|
if (*(args[1]) == 0) {
|
|
|
|
memprintf(err,
|
|
|
|
"'%s' expects a filepath to a 51Degrees trie or pattern data file.",
|
|
|
|
args[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-12-21 20:18:44 +00:00
|
|
|
if (global_51degrees.data_file_path)
|
|
|
|
free(global_51degrees.data_file_path);
|
|
|
|
global_51degrees.data_file_path = strdup(args[1]);
|
2015-06-29 14:43:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _51d_property_name_list(char **args, int section_type, struct proxy *curpx,
|
2021-03-09 08:53:46 +00:00
|
|
|
const struct proxy *defpx, const char *file, int line,
|
2015-09-18 17:28:52 +00:00
|
|
|
char **err)
|
2015-06-29 14:43:25 +00:00
|
|
|
{
|
|
|
|
int cur_arg = 1;
|
|
|
|
struct _51d_property_names *name;
|
|
|
|
|
|
|
|
if (*(args[cur_arg]) == 0) {
|
|
|
|
memprintf(err,
|
|
|
|
"'%s' expects at least one 51Degrees property name.",
|
|
|
|
args[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*(args[cur_arg])) {
|
2016-04-03 11:48:43 +00:00
|
|
|
name = calloc(1, sizeof(*name));
|
2015-06-29 14:43:25 +00:00
|
|
|
name->name = strdup(args[cur_arg]);
|
2021-04-21 05:32:39 +00:00
|
|
|
LIST_APPEND(&global_51degrees.property_names, &name->list);
|
2015-06-29 14:43:25 +00:00
|
|
|
++cur_arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _51d_property_separator(char **args, int section_type, struct proxy *curpx,
|
2021-03-09 08:53:46 +00:00
|
|
|
const struct proxy *defpx, const char *file, int line,
|
2015-06-29 14:43:25 +00:00
|
|
|
char **err)
|
|
|
|
{
|
|
|
|
if (*(args[1]) == 0) {
|
|
|
|
memprintf(err,
|
|
|
|
"'%s' expects a single character.",
|
|
|
|
args[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (strlen(args[1]) > 1) {
|
|
|
|
memprintf(err,
|
|
|
|
"'%s' expects a single character, got '%s'.",
|
|
|
|
args[0], args[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-12-21 20:18:44 +00:00
|
|
|
global_51degrees.property_separator = *args[1];
|
2015-06-29 14:43:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-29 14:43:26 +00:00
|
|
|
static int _51d_cache_size(char **args, int section_type, struct proxy *curpx,
|
2021-03-09 08:53:46 +00:00
|
|
|
const struct proxy *defpx, const char *file, int line,
|
2015-06-29 14:43:26 +00:00
|
|
|
char **err)
|
|
|
|
{
|
|
|
|
if (*(args[1]) == 0) {
|
|
|
|
memprintf(err,
|
|
|
|
"'%s' expects a positive numeric value.",
|
|
|
|
args[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-12-21 20:18:44 +00:00
|
|
|
global_51degrees.cache_size = atoi(args[1]);
|
|
|
|
if (global_51degrees.cache_size < 0) {
|
2015-06-29 14:43:26 +00:00
|
|
|
memprintf(err,
|
|
|
|
"'%s' expects a positive numeric value, got '%s'.",
|
|
|
|
args[0], args[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
static int _51d_fetch_check(struct arg *arg, char **err_msg)
|
|
|
|
{
|
2016-12-21 20:18:44 +00:00
|
|
|
if (global_51degrees.data_file_path)
|
2015-09-18 17:28:52 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-07 14:41:23 +00:00
|
|
|
static int _51d_conv_check(struct arg *arg, struct sample_conv *conv,
|
2015-09-18 17:28:52 +00:00
|
|
|
const char *file, int line, char **err_msg)
|
2015-08-07 14:41:23 +00:00
|
|
|
{
|
2016-12-21 20:18:44 +00:00
|
|
|
if (global_51degrees.data_file_path)
|
2015-08-07 14:41:23 +00:00
|
|
|
return 1;
|
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')");
|
2015-08-07 14:41:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-18 18:53:05 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2016-01-08 13:47:46 +00:00
|
|
|
static void _51d_lru_free(void *cache_entry)
|
|
|
|
{
|
2018-07-13 09:56:34 +00:00
|
|
|
struct buffer *ptr = cache_entry;
|
2016-01-08 13:47:46 +00:00
|
|
|
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
free(ptr->area);
|
2016-01-08 13:47:46 +00:00
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocates memory freeing space in the cache if necessary.
|
|
|
|
*/
|
|
|
|
static void *_51d_malloc(int size)
|
|
|
|
{
|
|
|
|
void *ptr = malloc(size);
|
|
|
|
|
|
|
|
if (!ptr) {
|
|
|
|
/* free the oldest 10 entries from lru to free up some memory
|
|
|
|
* then try allocating memory again */
|
|
|
|
lru64_kill_oldest(_51d_lru_tree, 10);
|
|
|
|
ptr = malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-09-18 18:53:05 +00:00
|
|
|
/* Insert the data associated with the sample into the cache as a fresh item.
|
|
|
|
*/
|
2016-01-08 13:42:41 +00:00
|
|
|
static void _51d_insert_cache_entry(struct sample *smp, struct lru64 *lru, void* domain)
|
2015-09-18 18:53:05 +00:00
|
|
|
{
|
2018-07-13 09:56:34 +00:00
|
|
|
struct buffer *cache_entry = _51d_malloc(sizeof(*cache_entry));
|
2015-09-18 18:53:05 +00:00
|
|
|
|
|
|
|
if (!cache_entry)
|
|
|
|
return;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
cache_entry->area = _51d_malloc(smp->data.u.str.data + 1);
|
|
|
|
if (!cache_entry->area) {
|
2016-01-08 13:47:46 +00:00
|
|
|
free(cache_entry);
|
2015-09-18 18:53:05 +00:00
|
|
|
return;
|
2016-01-08 13:47:46 +00:00
|
|
|
}
|
2015-09-18 18:53:05 +00:00
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
memcpy(cache_entry->area, smp->data.u.str.area, smp->data.u.str.data);
|
|
|
|
cache_entry->area[smp->data.u.str.data] = 0;
|
|
|
|
cache_entry->data = smp->data.u.str.data;
|
2019-02-05 13:24:00 +00:00
|
|
|
HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock);
|
2016-01-08 13:47:46 +00:00
|
|
|
lru64_commit(lru, cache_entry, domain, 0, _51d_lru_free);
|
2019-02-05 13:24:00 +00:00
|
|
|
HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
|
2015-09-18 18:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieves the data from the cache and sets the sample data to this string.
|
|
|
|
*/
|
|
|
|
static void _51d_retrieve_cache_entry(struct sample *smp, struct lru64 *lru)
|
|
|
|
{
|
2018-07-13 09:56:34 +00:00
|
|
|
struct buffer *cache_entry = lru->data;
|
2018-07-13 08:54:26 +00:00
|
|
|
smp->data.u.str.area = cache_entry->area;
|
|
|
|
smp->data.u.str.data = cache_entry->data;
|
2015-09-18 18:53:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
|
|
|
/* Sets the important HTTP headers ahead of the detection
|
|
|
|
*/
|
|
|
|
static void _51d_set_headers(struct sample *smp, fiftyoneDegreesWorkset *ws)
|
2015-06-29 14:43:25 +00:00
|
|
|
{
|
2019-07-15 12:36:03 +00:00
|
|
|
struct channel *chn;
|
|
|
|
struct htx *htx;
|
|
|
|
struct http_hdr_ctx ctx;
|
|
|
|
struct ist name;
|
2015-06-29 14:43:25 +00:00
|
|
|
int i;
|
2015-09-18 17:28:52 +00:00
|
|
|
|
|
|
|
ws->importantHeadersCount = 0;
|
2019-07-15 12:36:03 +00:00
|
|
|
chn = (smp->strm ? &smp->strm->req : NULL);
|
2015-09-18 17:28:52 +00:00
|
|
|
|
2019-07-15 12:36:03 +00:00
|
|
|
// No need to null check as this has already been carried out in the
|
|
|
|
// calling method
|
2020-05-05 09:53:43 +00:00
|
|
|
htx = smp_prefetch_htx(smp, chn, NULL, 1);
|
2020-07-01 17:58:32 +00:00
|
|
|
ALREADY_CHECKED(htx);
|
2019-06-12 14:19:12 +00:00
|
|
|
|
2019-07-15 12:36:03 +00:00
|
|
|
for (i = 0; i < global_51degrees.header_count; i++) {
|
2021-02-28 15:11:36 +00:00
|
|
|
name = ist2((global_51degrees.header_names + i)->area,
|
|
|
|
(global_51degrees.header_names + i)->data);
|
2019-07-15 12:36:03 +00:00
|
|
|
ctx.blk = NULL;
|
|
|
|
|
|
|
|
if (http_find_header(htx, name, &ctx, 1)) {
|
|
|
|
ws->importantHeaders[ws->importantHeadersCount].header = ws->dataSet->httpHeaders + i;
|
|
|
|
ws->importantHeaders[ws->importantHeadersCount].headerValue = ctx.value.ptr;
|
|
|
|
ws->importantHeaders[ws->importantHeadersCount].headerValueLength = ctx.value.len;
|
|
|
|
ws->importantHeadersCount++;
|
2015-09-18 17:28:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
2019-02-05 13:24:00 +00:00
|
|
|
static void _51d_init_device_offsets(fiftyoneDegreesDeviceOffsets *offsets) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < global_51degrees.data_set.uniqueHttpHeaders.count; i++) {
|
|
|
|
offsets->firstOffset[i].userAgent = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _51d_set_device_offsets(struct sample *smp, fiftyoneDegreesDeviceOffsets *offsets)
|
2015-09-18 17:28:52 +00:00
|
|
|
{
|
2019-07-15 12:36:03 +00:00
|
|
|
struct channel *chn;
|
|
|
|
struct htx *htx;
|
|
|
|
struct http_hdr_ctx ctx;
|
|
|
|
struct ist name;
|
2019-06-12 14:19:12 +00:00
|
|
|
int i;
|
2015-09-18 17:28:52 +00:00
|
|
|
|
|
|
|
offsets->size = 0;
|
2019-07-15 12:36:03 +00:00
|
|
|
chn = (smp->strm ? &smp->strm->req : NULL);
|
2015-09-18 17:28:52 +00:00
|
|
|
|
2019-07-15 12:36:03 +00:00
|
|
|
// No need to null check as this has already been carried out in the
|
|
|
|
// calling method
|
2020-05-05 09:53:43 +00:00
|
|
|
htx = smp_prefetch_htx(smp, chn, NULL, 1);
|
2020-07-01 17:58:32 +00:00
|
|
|
ALREADY_CHECKED(htx);
|
2019-06-12 14:19:12 +00:00
|
|
|
|
2019-07-15 12:36:03 +00:00
|
|
|
for (i = 0; i < global_51degrees.header_count; i++) {
|
2021-02-28 15:11:36 +00:00
|
|
|
name = ist2((global_51degrees.header_names + i)->area,
|
|
|
|
(global_51degrees.header_names + i)->data);
|
2019-07-15 12:36:03 +00:00
|
|
|
ctx.blk = NULL;
|
2019-06-12 14:19:12 +00:00
|
|
|
|
2019-07-15 12:36:03 +00:00
|
|
|
if (http_find_header(htx, name, &ctx, 1)) {
|
|
|
|
(offsets->firstOffset + offsets->size)->httpHeaderOffset = *(global_51degrees.header_offsets + i);
|
|
|
|
(offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set, ctx.value.ptr);
|
|
|
|
offsets->size++;
|
2015-09-18 17:28:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-15 12:36:03 +00:00
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
|
|
|
/* Provides a hash code for the important HTTP headers.
|
|
|
|
*/
|
|
|
|
unsigned long long _51d_req_hash(const struct arg *args, fiftyoneDegreesWorkset* ws)
|
|
|
|
{
|
|
|
|
unsigned long long seed = _51d_lru_seed ^ (long)args;
|
|
|
|
unsigned long long hash = 0;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < ws->importantHeadersCount; i++) {
|
|
|
|
hash ^= ws->importantHeaders[i].header->headerNameOffset;
|
2020-12-22 12:22:34 +00:00
|
|
|
hash ^= XXH3(ws->importantHeaders[i].headerValue,
|
|
|
|
ws->importantHeaders[i].headerValueLength,
|
|
|
|
seed);
|
2015-09-18 17:28:52 +00:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-06-29 14:43:25 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2015-09-18 17:28:52 +00:00
|
|
|
static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesWorkset* ws)
|
|
|
|
{
|
|
|
|
char *methodName;
|
|
|
|
#endif
|
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
2019-02-05 13:24:00 +00:00
|
|
|
static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesDeviceOffsets *offsets)
|
2015-09-18 17:28:52 +00:00
|
|
|
{
|
|
|
|
char valuesBuffer[1024];
|
2016-12-21 20:18:44 +00:00
|
|
|
const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(&global_51degrees.data_set);
|
|
|
|
int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(&global_51degrees.data_set);
|
2015-09-18 17:28:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
char no_data[] = "NoData"; /* response when no data could be found */
|
2018-07-13 09:56:34 +00:00
|
|
|
struct buffer *temp = get_trash_chunk();
|
2015-09-18 17:28:52 +00:00
|
|
|
int j, i = 0, found;
|
2015-06-29 14:43:25 +00:00
|
|
|
const char* property_name;
|
2015-09-18 17:28:52 +00:00
|
|
|
|
|
|
|
/* Loop through property names passed to the filter and fetch them from the dataset. */
|
2018-07-13 08:54:26 +00:00
|
|
|
while (args[i].data.str.area) {
|
2015-09-18 17:28:52 +00:00
|
|
|
/* Try to find request property in dataset. */
|
|
|
|
found = 0;
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2018-07-13 08:54:26 +00:00
|
|
|
if (strcmp("Method", args[i].data.str.area) == 0) {
|
2015-09-18 17:28:52 +00:00
|
|
|
switch(ws->method) {
|
|
|
|
case EXACT: methodName = "Exact"; break;
|
|
|
|
case NUMERIC: methodName = "Numeric"; break;
|
|
|
|
case NEAREST: methodName = "Nearest"; break;
|
|
|
|
case CLOSEST: methodName = "Closest"; break;
|
|
|
|
default:
|
|
|
|
case NONE: methodName = "None"; break;
|
|
|
|
}
|
|
|
|
chunk_appendf(temp, "%s", methodName);
|
|
|
|
found = 1;
|
|
|
|
}
|
2018-07-13 08:54:26 +00:00
|
|
|
else if (strcmp("Difference", args[i].data.str.area) == 0) {
|
2015-09-18 17:28:52 +00:00
|
|
|
chunk_appendf(temp, "%d", ws->difference);
|
|
|
|
found = 1;
|
|
|
|
}
|
2018-07-13 08:54:26 +00:00
|
|
|
else if (strcmp("Rank", args[i].data.str.area) == 0) {
|
2015-09-18 17:28:52 +00:00
|
|
|
chunk_appendf(temp, "%d", fiftyoneDegreesGetSignatureRank(ws));
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) {
|
|
|
|
property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]);
|
2018-07-13 08:54:26 +00:00
|
|
|
if (strcmp(property_name, args[i].data.str.area) == 0) {
|
2015-09-18 17:28:52 +00:00
|
|
|
found = 1;
|
|
|
|
fiftyoneDegreesSetValues(ws, j);
|
|
|
|
chunk_appendf(temp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
|
|
|
found = 0;
|
|
|
|
for (j = 0; j < requiredPropertiesCount; j++) {
|
|
|
|
property_name = requiredProperties[j];
|
2018-07-13 08:54:26 +00:00
|
|
|
if (strcmp(property_name, args[i].data.str.area) == 0 &&
|
2019-02-05 13:24:00 +00:00
|
|
|
fiftyoneDegreesGetValueFromOffsets(&global_51degrees.data_set, offsets, j, valuesBuffer, 1024) > 0) {
|
2015-09-18 17:28:52 +00:00
|
|
|
found = 1;
|
|
|
|
chunk_appendf(temp, "%s", valuesBuffer);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-01-08 13:52:32 +00:00
|
|
|
if (!found)
|
2015-09-18 17:28:52 +00:00
|
|
|
chunk_appendf(temp, "%s", no_data);
|
2016-01-08 13:52:32 +00:00
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
/* Add separator. */
|
2016-12-21 20:18:44 +00:00
|
|
|
chunk_appendf(temp, "%c", global_51degrees.property_separator);
|
2015-09-18 17:28:52 +00:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
if (temp->data) {
|
|
|
|
--temp->data;
|
|
|
|
temp->area[temp->data] = '\0';
|
2015-09-18 17:28:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
smp->data.u.str.area = temp->area;
|
|
|
|
smp->data.u.str.data = temp->data;
|
2015-09-18 17:28:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 11:25:11 +00:00
|
|
|
/* Sets the sample data as a constant string. This ensures that the
|
|
|
|
* string will be processed correctly.
|
|
|
|
*/
|
|
|
|
static void _51d_set_smp(struct sample *smp)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Data type has to be set to ensure the string output is processed
|
|
|
|
* correctly.
|
|
|
|
*/
|
|
|
|
smp->data.type = SMP_T_STR;
|
|
|
|
|
|
|
|
/* Flags the sample to show it uses constant memory. */
|
|
|
|
smp->flags |= SMP_F_CONST;
|
|
|
|
}
|
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private)
|
|
|
|
{
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2015-06-29 14:43:25 +00:00
|
|
|
fiftyoneDegreesWorkset* ws; /* workset for detection */
|
2015-09-18 17:28:52 +00:00
|
|
|
struct lru64 *lru = NULL;
|
|
|
|
#endif
|
2019-02-05 13:24:00 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
|
|
|
fiftyoneDegreesDeviceOffsets *offsets; /* Offsets for detection */
|
2015-09-18 17:28:52 +00:00
|
|
|
|
2019-06-12 14:19:12 +00:00
|
|
|
#endif
|
|
|
|
struct channel *chn;
|
2019-07-15 12:36:03 +00:00
|
|
|
struct htx *htx;
|
|
|
|
|
2019-06-12 14:19:12 +00:00
|
|
|
chn = (smp->strm ? &smp->strm->req : NULL);
|
2020-05-05 09:53:43 +00:00
|
|
|
htx = smp_prefetch_htx(smp, chn, NULL, 1);
|
2019-07-15 12:36:03 +00:00
|
|
|
if (!htx)
|
|
|
|
return 0;
|
2019-06-12 14:19:12 +00:00
|
|
|
|
2016-01-08 13:42:41 +00:00
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
|
|
|
|
|
|
|
/* Get only the headers needed for device detection so they can be used
|
|
|
|
* with the cache to return previous results. Pattern is slower than
|
|
|
|
* Trie so caching will help improve performance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Get a workset from the pool which will later contain detection results. */
|
2016-12-21 20:18:44 +00:00
|
|
|
ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool);
|
2015-09-18 17:28:52 +00:00
|
|
|
if (!ws)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Set the important HTTP headers for this request in the workset. */
|
|
|
|
_51d_set_headers(smp, ws);
|
|
|
|
|
|
|
|
/* Check the cache to see if there's results for these headers already. */
|
|
|
|
if (_51d_lru_tree) {
|
2019-02-05 13:24:00 +00:00
|
|
|
HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock);
|
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
lru = lru64_get(_51d_req_hash(args, ws),
|
2016-01-08 13:42:41 +00:00
|
|
|
_51d_lru_tree, (void*)args, 0);
|
2019-02-05 13:24:00 +00:00
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
if (lru && lru->domain) {
|
2016-12-21 20:18:44 +00:00
|
|
|
fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
|
2015-09-18 18:53:05 +00:00
|
|
|
_51d_retrieve_cache_entry(smp, lru);
|
2019-02-05 13:24:00 +00:00
|
|
|
HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
|
2020-01-20 11:25:11 +00:00
|
|
|
|
|
|
|
_51d_set_smp(smp);
|
2015-09-18 17:28:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-02-05 13:24:00 +00:00
|
|
|
HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
|
2015-09-18 17:28:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fiftyoneDegreesMatchForHttpHeaders(ws);
|
|
|
|
|
|
|
|
_51d_process_match(args, smp, ws);
|
|
|
|
|
2015-06-29 14:43:25 +00:00
|
|
|
#endif
|
2015-09-18 17:28:52 +00:00
|
|
|
|
2015-06-29 14:43:25 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
2019-02-05 13:24:00 +00:00
|
|
|
#ifndef FIFTYONEDEGREES_NO_THREADING
|
|
|
|
offsets = fiftyoneDegreesCreateDeviceOffsets(&global_51degrees.data_set);
|
|
|
|
_51d_init_device_offsets(offsets);
|
|
|
|
#else
|
|
|
|
offsets = &global_51degrees.device_offsets;
|
|
|
|
#endif
|
2015-09-18 17:28:52 +00:00
|
|
|
|
|
|
|
/* Trie is very fast so all the headers can be passed in and the result
|
|
|
|
* returned faster than the hashing algorithm process.
|
|
|
|
*/
|
2019-02-05 13:24:00 +00:00
|
|
|
_51d_set_device_offsets(smp, offsets);
|
|
|
|
_51d_process_match(args, smp, offsets);
|
|
|
|
|
|
|
|
#ifndef FIFTYONEDEGREES_NO_THREADING
|
|
|
|
fiftyoneDegreesFreeDeviceOffsets(offsets);
|
|
|
|
#endif
|
2015-09-18 17:28:52 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2016-12-21 20:18:44 +00:00
|
|
|
fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
|
2016-01-08 13:52:32 +00:00
|
|
|
if (lru)
|
2016-01-08 13:42:41 +00:00
|
|
|
_51d_insert_cache_entry(smp, lru, (void*)args);
|
2015-06-29 14:43:25 +00:00
|
|
|
#endif
|
2015-09-18 17:28:52 +00:00
|
|
|
|
2020-01-20 11:25:11 +00:00
|
|
|
_51d_set_smp(smp);
|
2015-09-18 17:28:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _51d_conv(const struct arg *args, struct sample *smp, void *private)
|
|
|
|
{
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
|
|
|
fiftyoneDegreesWorkset* ws; /* workset for detection */
|
2015-06-29 14:43:26 +00:00
|
|
|
struct lru64 *lru = NULL;
|
2015-09-18 17:28:52 +00:00
|
|
|
#endif
|
2019-02-05 13:24:00 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
|
|
|
fiftyoneDegreesDeviceOffsets *offsets; /* Offsets for detection */
|
|
|
|
#endif
|
2016-01-08 13:42:41 +00:00
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2015-06-29 14:43:26 +00:00
|
|
|
|
|
|
|
/* Look in the list. */
|
|
|
|
if (_51d_lru_tree) {
|
|
|
|
unsigned long long seed = _51d_lru_seed ^ (long)args;
|
|
|
|
|
2019-02-05 13:24:00 +00:00
|
|
|
HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock);
|
2020-12-22 12:22:34 +00:00
|
|
|
lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed),
|
2016-01-08 13:42:41 +00:00
|
|
|
_51d_lru_tree, (void*)args, 0);
|
2015-06-29 14:43:26 +00:00
|
|
|
if (lru && lru->domain) {
|
2015-09-18 18:53:05 +00:00
|
|
|
_51d_retrieve_cache_entry(smp, lru);
|
2019-02-05 13:24:00 +00:00
|
|
|
HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
|
2015-06-29 14:43:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-02-05 13:24:00 +00:00
|
|
|
HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock);
|
2015-06-29 14:43:26 +00:00
|
|
|
}
|
2015-06-29 14:43:25 +00:00
|
|
|
|
|
|
|
/* Create workset. This will later contain detection results. */
|
2016-12-21 20:18:44 +00:00
|
|
|
ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool);
|
2015-06-29 14:43:25 +00:00
|
|
|
if (!ws)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2015-07-07 14:10:43 +00:00
|
|
|
/* Duplicate the data and remove the "const" flag before device detection. */
|
|
|
|
if (!smp_dup(smp))
|
|
|
|
return 0;
|
|
|
|
|
2018-07-13 08:54:26 +00:00
|
|
|
smp->data.u.str.area[smp->data.u.str.data] = '\0';
|
2015-06-29 14:43:25 +00:00
|
|
|
|
|
|
|
/* Perform detection. */
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2018-07-13 08:54:26 +00:00
|
|
|
fiftyoneDegreesMatch(ws, smp->data.u.str.area);
|
2015-09-18 17:28:52 +00:00
|
|
|
_51d_process_match(args, smp, ws);
|
2015-06-29 14:43:25 +00:00
|
|
|
#endif
|
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
2019-02-05 13:24:00 +00:00
|
|
|
#ifndef FIFTYONEDEGREES_NO_THREADING
|
|
|
|
offsets = fiftyoneDegreesCreateDeviceOffsets(&global_51degrees.data_set);
|
|
|
|
_51d_init_device_offsets(offsets);
|
|
|
|
#else
|
|
|
|
offsets = &global_51degrees.device_offsets;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
offsets->firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set,
|
|
|
|
smp->data.u.str.area);
|
|
|
|
offsets->size = 1;
|
|
|
|
_51d_process_match(args, smp, offsets);
|
2015-06-29 14:43:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2016-12-21 20:18:44 +00:00
|
|
|
fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws);
|
2016-01-08 13:52:32 +00:00
|
|
|
if (lru)
|
2016-01-08 13:42:41 +00:00
|
|
|
_51d_insert_cache_entry(smp, lru, (void*)args);
|
2015-09-18 17:28:52 +00:00
|
|
|
#endif
|
2015-06-29 14:43:25 +00:00
|
|
|
|
2019-02-05 13:24:00 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
|
|
|
#ifndef FIFTYONEDEGREES_NO_THREADING
|
|
|
|
fiftyoneDegreesFreeDeviceOffsets(offsets);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2020-01-20 11:25:11 +00:00
|
|
|
_51d_set_smp(smp);
|
2015-09-18 17:28:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-06-29 14:43:25 +00:00
|
|
|
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2015-09-18 17:28:52 +00:00
|
|
|
void _51d_init_http_headers()
|
|
|
|
{
|
|
|
|
int index = 0;
|
|
|
|
const fiftyoneDegreesAsciiString *headerName;
|
2016-12-21 20:18:44 +00:00
|
|
|
fiftyoneDegreesDataSet *ds = &global_51degrees.data_set;
|
|
|
|
global_51degrees.header_count = ds->httpHeadersCount;
|
2018-07-13 09:56:34 +00:00
|
|
|
global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer));
|
2016-12-21 20:18:44 +00:00
|
|
|
for (index = 0; index < global_51degrees.header_count; index++) {
|
2015-09-18 17:28:52 +00:00
|
|
|
headerName = fiftyoneDegreesGetString(ds, ds->httpHeaders[index].headerNameOffset);
|
2018-07-13 08:54:26 +00:00
|
|
|
(global_51degrees.header_names + index)->area = (char*)&headerName->firstByte;
|
|
|
|
(global_51degrees.header_names + index)->data = headerName->length - 1;
|
|
|
|
(global_51degrees.header_names + index)->size = (global_51degrees.header_names + index)->data;
|
2015-09-18 17:28:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-29 14:43:25 +00:00
|
|
|
#endif
|
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
|
|
|
void _51d_init_http_headers()
|
|
|
|
{
|
|
|
|
int index = 0;
|
2016-12-21 20:18:44 +00:00
|
|
|
fiftyoneDegreesDataSet *ds = &global_51degrees.data_set;
|
|
|
|
global_51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(ds);
|
2019-02-05 13:24:00 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_NO_THREADING
|
2016-12-21 20:18:44 +00:00
|
|
|
global_51degrees.device_offsets.firstOffset = malloc(
|
|
|
|
global_51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset));
|
2019-02-05 13:24:00 +00:00
|
|
|
_51d_init_device_offsets(&global_51degrees.device_offsets);
|
|
|
|
#endif
|
2018-07-13 09:56:34 +00:00
|
|
|
global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer));
|
2016-12-21 20:18:44 +00:00
|
|
|
global_51degrees.header_offsets = malloc(global_51degrees.header_count * sizeof(int32_t));
|
|
|
|
for (index = 0; index < global_51degrees.header_count; index++) {
|
|
|
|
global_51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(ds, index);
|
2019-02-05 13:23:06 +00:00
|
|
|
global_51degrees.header_names[index].area = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(ds, index);
|
|
|
|
global_51degrees.header_names[index].data = strlen(global_51degrees.header_names[index].area);
|
2018-07-13 08:54:26 +00:00
|
|
|
global_51degrees.header_names[index].size = global_51degrees.header_names->data;
|
2015-07-07 14:10:43 +00:00
|
|
|
}
|
2015-06-29 14:43:25 +00:00
|
|
|
}
|
2015-09-18 17:28:52 +00:00
|
|
|
#endif
|
2015-06-29 14:43:25 +00:00
|
|
|
|
2016-12-21 19:30:05 +00:00
|
|
|
/*
|
|
|
|
* module init / deinit functions. Returns 0 if OK, or a combination of ERR_*.
|
|
|
|
*/
|
|
|
|
static int init_51degrees(void)
|
2015-06-29 14:43:25 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
2018-07-13 09:56:34 +00:00
|
|
|
struct buffer *temp;
|
2015-06-29 14:43:25 +00:00
|
|
|
struct _51d_property_names *name;
|
|
|
|
char **_51d_property_list = NULL;
|
|
|
|
fiftyoneDegreesDataSetInitStatus _51d_dataset_status = DATA_SET_INIT_STATUS_NOT_SET;
|
|
|
|
|
2016-12-21 20:18:44 +00:00
|
|
|
if (!global_51degrees.data_file_path)
|
2020-11-06 14:24:23 +00:00
|
|
|
return ERR_NONE;
|
2015-08-07 14:41:23 +00:00
|
|
|
|
2019-02-05 13:24:00 +00:00
|
|
|
if (global.nbthread < 1) {
|
|
|
|
ha_alert("51Degrees: The thread count cannot be zero or negative.\n");
|
2017-10-25 15:23:02 +00:00
|
|
|
return (ERR_FATAL | ERR_ALERT);
|
|
|
|
}
|
|
|
|
|
2016-12-21 20:18:44 +00:00
|
|
|
if (!LIST_ISEMPTY(&global_51degrees.property_names)) {
|
2015-06-29 14:43:25 +00:00
|
|
|
i = 0;
|
2016-12-21 20:18:44 +00:00
|
|
|
list_for_each_entry(name, &global_51degrees.property_names, list)
|
2015-06-29 14:43:25 +00:00
|
|
|
++i;
|
2020-09-12 18:26:43 +00:00
|
|
|
_51d_property_list = calloc(i, sizeof(*_51d_property_list));
|
2015-06-29 14:43:25 +00:00
|
|
|
|
|
|
|
i = 0;
|
2016-12-21 20:18:44 +00:00
|
|
|
list_for_each_entry(name, &global_51degrees.property_names, list)
|
2015-06-29 14:43:25 +00:00
|
|
|
_51d_property_list[i++] = name->name;
|
|
|
|
}
|
|
|
|
|
2016-12-21 20:18:44 +00:00
|
|
|
_51d_dataset_status = fiftyoneDegreesInitWithPropertyArray(global_51degrees.data_file_path, &global_51degrees.data_set, (const char**)_51d_property_list, i);
|
2015-06-29 14:43:25 +00:00
|
|
|
|
|
|
|
temp = get_trash_chunk();
|
|
|
|
chunk_reset(temp);
|
|
|
|
|
|
|
|
switch (_51d_dataset_status) {
|
|
|
|
case DATA_SET_INIT_STATUS_SUCCESS:
|
2015-09-18 17:28:52 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2019-02-05 13:24:00 +00:00
|
|
|
global_51degrees.pool = fiftyoneDegreesWorksetPoolCreate(&global_51degrees.data_set, NULL, global.nbthread);
|
2015-09-18 17:28:52 +00:00
|
|
|
#endif
|
|
|
|
_51d_init_http_headers();
|
2015-06-29 14:43:25 +00:00
|
|
|
break;
|
|
|
|
case DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY:
|
|
|
|
chunk_printf(temp, "Insufficient memory.");
|
|
|
|
break;
|
|
|
|
case DATA_SET_INIT_STATUS_CORRUPT_DATA:
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
|
|
|
chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Pattern data format.");
|
|
|
|
#endif
|
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
|
|
|
chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Trie data format.");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case DATA_SET_INIT_STATUS_INCORRECT_VERSION:
|
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
|
|
|
chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Pattern data format.");
|
|
|
|
#endif
|
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
|
|
|
chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Trie data format.");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case DATA_SET_INIT_STATUS_FILE_NOT_FOUND:
|
|
|
|
chunk_printf(temp, "File not found.");
|
|
|
|
break;
|
2016-07-06 11:07:21 +00:00
|
|
|
case DATA_SET_INIT_STATUS_NULL_POINTER:
|
|
|
|
chunk_printf(temp, "Null pointer to the existing dataset or memory location.");
|
|
|
|
break;
|
|
|
|
case DATA_SET_INIT_STATUS_POINTER_OUT_OF_BOUNDS:
|
|
|
|
chunk_printf(temp, "Allocated continuous memory containing 51Degrees data file appears to be smaller than expected. Most likely"
|
|
|
|
" because the data file was not fully loaded into the allocated memory.");
|
|
|
|
break;
|
2015-06-29 14:43:25 +00:00
|
|
|
case DATA_SET_INIT_STATUS_NOT_SET:
|
|
|
|
chunk_printf(temp, "Data set not initialised.");
|
|
|
|
break;
|
2017-09-27 10:46:44 +00:00
|
|
|
default:
|
|
|
|
chunk_printf(temp, "Other error.");
|
|
|
|
break;
|
2015-06-29 14:43:25 +00:00
|
|
|
}
|
|
|
|
if (_51d_dataset_status != DATA_SET_INIT_STATUS_SUCCESS) {
|
2018-07-13 08:54:26 +00:00
|
|
|
if (temp->data)
|
|
|
|
ha_alert("51Degrees Setup - Error reading 51Degrees data file. %s\n",
|
|
|
|
temp->area);
|
2015-06-29 14:43:25 +00:00
|
|
|
else
|
2017-11-24 15:50:31 +00:00
|
|
|
ha_alert("51Degrees Setup - Error reading 51Degrees data file.\n");
|
2016-12-21 19:30:05 +00:00
|
|
|
return ERR_ALERT | ERR_FATAL;
|
2015-06-29 14:43:25 +00:00
|
|
|
}
|
|
|
|
free(_51d_property_list);
|
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
BUG/MEDIUM: random: implement a thread-safe and process-safe PRNG
This is the replacement of failed attempt to add thread safety and
per-process sequences of random numbers initally tried with commit
1c306aa84d ("BUG/MEDIUM: random: implement per-thread and per-process
random sequences").
This new version takes a completely different approach and doesn't try
to work around the horrible OS-specific and non-portable random API
anymore. Instead it implements "xoroshiro128**", a reputedly high
quality random number generator, which is one of the many variants of
xorshift, which passes all quality tests and which is described here:
http://prng.di.unimi.it/
While not cryptographically secure, it is fast and features a 2^128-1
period. It supports fast jumps allowing to cut the period into smaller
non-overlapping sequences, which we use here to support up to 2^32
processes each having their own, non-overlapping sequence of 2^96
numbers (~7*10^28). This is enough to provide 1 billion randoms per
second and per process for 2200 billion years.
The implementation was made thread-safe either by using a double 64-bit
CAS on platforms supporting it (x86_64, aarch64) or by using a local
lock for the time needed to perform the shift operations. This ensures
that all threads pick numbers from the same pool so that it is not
needed to assign per-thread ranges. For processes we use the fast jump
method to advance the sequence by 2^96 for each process.
Before this patch, the following config:
global
nbproc 8
frontend f
bind :4445
mode http
log stdout format raw daemon
log-format "%[uuid] %pid"
redirect location /
Would produce this output:
a4d0ad64-2645-4b74-b894-48acce0669af 12987
a4d0ad64-2645-4b74-b894-48acce0669af 12992
a4d0ad64-2645-4b74-b894-48acce0669af 12986
a4d0ad64-2645-4b74-b894-48acce0669af 12988
a4d0ad64-2645-4b74-b894-48acce0669af 12991
a4d0ad64-2645-4b74-b894-48acce0669af 12989
a4d0ad64-2645-4b74-b894-48acce0669af 12990
82d5f6cd-f6c1-4f85-a89c-36ae85d26fb9 12987
82d5f6cd-f6c1-4f85-a89c-36ae85d26fb9 12992
82d5f6cd-f6c1-4f85-a89c-36ae85d26fb9 12986
(...)
And now produces:
f94b29b3-da74-4e03-a0c5-a532c635bad9 13011
47470c02-4862-4c33-80e7-a952899570e5 13014
86332123-539a-47bf-853f-8c8ea8b2a2b5 13013
8f9efa99-3143-47b2-83cf-d618c8dea711 13012
3cc0f5c7-d790-496b-8d39-bec77647af5b 13015
3ec64915-8f95-4374-9e66-e777dc8791e0 13009
0f9bf894-dcde-408c-b094-6e0bb3255452 13011
49c7bfde-3ffb-40e9-9a8d-8084d650ed8f 13014
e23f6f2e-35c5-4433-a294-b790ab902653 13012
There are multiple benefits to using this method. First, it doesn't
depend anymore on a non-portable API. Second it's thread safe. Third it
is fast and more proven than any hack we could attempt to try to work
around the deficiencies of the various implementations around.
This commit depends on previous patches "MINOR: tools: add 64-bit rotate
operators" and "BUG/MEDIUM: random: initialize the random pool a bit
better", all of which will need to be backported at least as far as
version 2.0. It doesn't require to backport the build fixes for circular
include files dependecy anymore.
2020-03-07 23:42:37 +00:00
|
|
|
_51d_lru_seed = ha_random();
|
2016-12-21 20:18:44 +00:00
|
|
|
if (global_51degrees.cache_size) {
|
|
|
|
_51d_lru_tree = lru64_new(global_51degrees.cache_size);
|
2015-09-18 17:28:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-06-29 14:43:26 +00:00
|
|
|
|
2020-11-06 14:24:23 +00:00
|
|
|
return ERR_NONE;
|
2015-06-29 14:43:25 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 19:59:01 +00:00
|
|
|
static void deinit_51degrees(void)
|
2015-06-29 14:43:25 +00:00
|
|
|
{
|
|
|
|
struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb;
|
|
|
|
|
2016-12-21 20:18:44 +00:00
|
|
|
free(global_51degrees.header_names);
|
2015-06-29 14:43:25 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2019-03-07 14:24:23 +00:00
|
|
|
if (global_51degrees.pool)
|
|
|
|
fiftyoneDegreesWorksetPoolFree(global_51degrees.pool);
|
2015-06-29 14:43:25 +00:00
|
|
|
#endif
|
|
|
|
#ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED
|
2019-02-05 13:24:00 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_NO_THREADING
|
2016-12-21 20:18:44 +00:00
|
|
|
free(global_51degrees.device_offsets.firstOffset);
|
2019-02-05 13:24:00 +00:00
|
|
|
#endif
|
2016-12-21 20:18:44 +00:00
|
|
|
free(global_51degrees.header_offsets);
|
2015-06-29 14:43:25 +00:00
|
|
|
#endif
|
2016-12-21 20:18:44 +00:00
|
|
|
fiftyoneDegreesDataSetFree(&global_51degrees.data_set);
|
2015-06-29 14:43:25 +00:00
|
|
|
|
2021-02-20 09:46:51 +00:00
|
|
|
ha_free(&global_51degrees.data_file_path);
|
2016-12-21 20:18:44 +00:00
|
|
|
list_for_each_entry_safe(_51d_prop_name, _51d_prop_nameb, &global_51degrees.property_names, list) {
|
2021-04-21 05:32:39 +00:00
|
|
|
LIST_DELETE(&_51d_prop_name->list);
|
2015-06-29 14:43:25 +00:00
|
|
|
free(_51d_prop_name);
|
|
|
|
}
|
2015-06-29 14:43:26 +00:00
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
#ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED
|
2015-06-29 14:43:26 +00:00
|
|
|
while (lru64_destroy(_51d_lru_tree));
|
2015-09-18 17:28:52 +00:00
|
|
|
#endif
|
2015-06-29 14:43:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct cfg_kw_list _51dcfg_kws = {{ }, {
|
|
|
|
{ CFG_GLOBAL, "51degrees-data-file", _51d_data_file },
|
|
|
|
{ CFG_GLOBAL, "51degrees-property-name-list", _51d_property_name_list },
|
|
|
|
{ CFG_GLOBAL, "51degrees-property-separator", _51d_property_separator },
|
2015-06-29 14:43:26 +00:00
|
|
|
{ CFG_GLOBAL, "51degrees-cache-size", _51d_cache_size },
|
2015-06-29 14:43:25 +00:00
|
|
|
{ 0, NULL, NULL },
|
|
|
|
}};
|
|
|
|
|
2018-11-25 18:14:37 +00:00
|
|
|
INITCALL1(STG_REGISTER, cfg_register_keywords, &_51dcfg_kws);
|
|
|
|
|
2015-09-18 17:28:52 +00:00
|
|
|
/* Note: must not be declared <const> as its list will be overwritten */
|
|
|
|
static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
|
|
|
|
{ "51d.all", _51d_fetch, ARG5(1,STR,STR,STR,STR,STR), _51d_fetch_check, SMP_T_STR, SMP_USE_HRQHV },
|
|
|
|
{ NULL, NULL, 0, 0, 0 },
|
|
|
|
}};
|
|
|
|
|
2018-11-25 18:14:37 +00:00
|
|
|
INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
|
|
|
|
|
2015-06-29 14:43:25 +00:00
|
|
|
/* Note: must not be declared <const> as its list will be overwritten */
|
|
|
|
static struct sample_conv_kw_list conv_kws = {ILH, {
|
2015-09-18 17:28:52 +00:00
|
|
|
{ "51d.single", _51d_conv, ARG5(1,STR,STR,STR,STR,STR), _51d_conv_check, SMP_T_STR, SMP_T_STR },
|
2015-06-29 14:43:25 +00:00
|
|
|
{ NULL, NULL, 0, 0, 0 },
|
|
|
|
}};
|
|
|
|
|
2018-11-25 18:14:37 +00:00
|
|
|
INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
|
|
|
|
|
2018-11-26 10:21:50 +00:00
|
|
|
REGISTER_POST_CHECK(init_51degrees);
|
|
|
|
REGISTER_POST_DEINIT(deinit_51degrees);
|
2019-06-13 15:51:59 +00:00
|
|
|
|
|
|
|
#if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED)
|
|
|
|
#ifndef FIFTYONEDEGREES_DUMMY_LIB
|
|
|
|
REGISTER_BUILD_OPTS("Built with 51Degrees Pattern support.");
|
|
|
|
#else
|
|
|
|
REGISTER_BUILD_OPTS("Built with 51Degrees Pattern support (dummy library).");
|
|
|
|
#endif
|
|
|
|
#elif defined(FIFTYONEDEGREES_H_TRIE_INCLUDED)
|
|
|
|
#ifndef FIFTYONEDEGREES_DUMMY_LIB
|
|
|
|
REGISTER_BUILD_OPTS("Built with 51Degrees Trie support.");
|
|
|
|
#else
|
|
|
|
REGISTER_BUILD_OPTS("Built with 51Degrees Trie support (dummy library).");
|
|
|
|
#endif
|
2019-07-15 12:36:03 +00:00
|
|
|
#endif
|