Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 3 | #include <import/lru.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 4 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 5 | #include <haproxy/arg.h> |
| 6 | #include <haproxy/buf-t.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 7 | #include <haproxy/cfgparse.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 8 | #include <haproxy/chunk.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 9 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 10 | #include <haproxy/global.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 11 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 12 | #include <haproxy/http_fetch.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 13 | #include <haproxy/http_htx.h> |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 14 | #include <haproxy/htx.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 15 | #include <haproxy/sample.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 16 | #include <haproxy/thread.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 17 | #include <haproxy/tools.h> |
Tim Duesterhus | d5fc8fc | 2021-09-11 17:51:13 +0200 | [diff] [blame] | 18 | #include <haproxy/xxhash.h> |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 19 | |
| 20 | #ifdef USE_51DEGREES_V4 |
| 21 | #include <hash/hash.h> |
| 22 | #undef MAP_TYPE |
| 23 | #include <hash/fiftyone.h> |
| 24 | #else |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 25 | #include <51Degrees.h> |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 26 | #endif |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 27 | |
| 28 | struct _51d_property_names { |
| 29 | struct list list; |
| 30 | char *name; |
| 31 | }; |
| 32 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 33 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 34 | static struct lru64_head *_51d_lru_tree = NULL; |
| 35 | static unsigned long long _51d_lru_seed; |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 36 | |
| 37 | __decl_spinlock(_51d_lru_lock); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 38 | #endif |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 39 | |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 40 | #ifdef FIFTYONE_DEGREES_HASH_INCLUDED |
| 41 | #define _51D_HEADERS_BUFFER_SIZE BUFSIZE |
| 42 | |
| 43 | static THREAD_LOCAL struct { |
| 44 | char **buf; |
| 45 | int max; |
| 46 | int count; |
| 47 | } _51d_headers; |
| 48 | |
| 49 | static THREAD_LOCAL fiftyoneDegreesResultsHash *_51d_results = NULL; |
| 50 | #endif |
| 51 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 52 | static 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 Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 56 | #if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) || defined(FIFTYONEDEGREES_H_TRIE_INCLUDED) |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 57 | int header_count; /* number of HTTP headers related to device detection. */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 58 | struct buffer *header_names; /* array of HTTP header names. */ |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 59 | 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. */ |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 65 | #ifdef FIFTYONEDEGREES_NO_THREADING |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 66 | fiftyoneDegreesDeviceOffsets device_offsets; /* Memory used for device offsets. */ |
| 67 | #endif |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 68 | #endif |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 69 | #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 Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 77 | 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 Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 85 | #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 Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 93 | .cache_size = 0, |
| 94 | }; |
| 95 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 96 | static int _51d_data_file(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 97 | const struct proxy *defpx, const char *file, int line, |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 98 | 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 Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 107 | if (global_51degrees.data_file_path) |
| 108 | free(global_51degrees.data_file_path); |
| 109 | global_51degrees.data_file_path = strdup(args[1]); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int _51d_property_name_list(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 115 | const struct proxy *defpx, const char *file, int line, |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 116 | char **err) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 117 | { |
| 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 Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 129 | name = calloc(1, sizeof(*name)); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 130 | name->name = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 131 | LIST_APPEND(&global_51degrees.property_names, &name->list); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 132 | ++cur_arg; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int _51d_property_separator(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 139 | const struct proxy *defpx, const char *file, int line, |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 140 | 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 Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 155 | global_51degrees.property_separator = *args[1]; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 160 | static int _51d_cache_size(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 161 | const struct proxy *defpx, const char *file, int line, |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 162 | 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 Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 171 | global_51degrees.cache_size = atoi(args[1]); |
| 172 | if (global_51degrees.cache_size < 0) { |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 173 | memprintf(err, |
| 174 | "'%s' expects a positive numeric value, got '%s'.", |
| 175 | args[0], args[1]); |
| 176 | return -1; |
| 177 | } |
| 178 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int _51d_fetch_check(struct arg *arg, char **err_msg) |
| 183 | { |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 184 | if (global_51degrees.data_file_path) |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 185 | return 1; |
| 186 | |
| 187 | memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')"); |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 191 | static int _51d_conv_check(struct arg *arg, struct sample_conv *conv, |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 192 | const char *file, int line, char **err_msg) |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 193 | { |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 194 | if (global_51degrees.data_file_path) |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 195 | return 1; |
| 196 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 197 | memprintf(err_msg, "51Degrees data file is not specified (parameter '51degrees-data-file')"); |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 201 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 202 | static void _51d_lru_free(void *cache_entry) |
| 203 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 204 | struct buffer *ptr = cache_entry; |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 205 | |
| 206 | if (!ptr) |
| 207 | return; |
| 208 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 209 | free(ptr->area); |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 210 | free(ptr); |
| 211 | } |
| 212 | |
| 213 | /* Allocates memory freeing space in the cache if necessary. |
| 214 | */ |
| 215 | static 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 Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 229 | /* Insert the data associated with the sample into the cache as a fresh item. |
| 230 | */ |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 231 | static void _51d_insert_cache_entry(struct sample *smp, struct lru64 *lru, void* domain) |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 232 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 233 | struct buffer *cache_entry = _51d_malloc(sizeof(*cache_entry)); |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 234 | |
| 235 | if (!cache_entry) |
| 236 | return; |
| 237 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 238 | cache_entry->area = _51d_malloc(smp->data.u.str.data + 1); |
| 239 | if (!cache_entry->area) { |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 240 | free(cache_entry); |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 241 | return; |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 242 | } |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 243 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 244 | 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; |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 247 | HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock); |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 248 | lru64_commit(lru, cache_entry, domain, 0, _51d_lru_free); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 249 | HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock); |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* Retrieves the data from the cache and sets the sample data to this string. |
| 253 | */ |
| 254 | static void _51d_retrieve_cache_entry(struct sample *smp, struct lru64 *lru) |
| 255 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 256 | struct buffer *cache_entry = lru->data; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 257 | smp->data.u.str.area = cache_entry->area; |
| 258 | smp->data.u.str.data = cache_entry->data; |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 259 | } |
| 260 | #endif |
| 261 | |
| 262 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 263 | /* Sets the important HTTP headers ahead of the detection |
| 264 | */ |
| 265 | static void _51d_set_headers(struct sample *smp, fiftyoneDegreesWorkset *ws) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 266 | { |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 267 | struct channel *chn; |
| 268 | struct htx *htx; |
| 269 | struct http_hdr_ctx ctx; |
| 270 | struct ist name; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 271 | int i; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 272 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 273 | ws->importantHeadersCount = 0; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 274 | chn = (smp->strm ? &smp->strm->req : NULL); |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 275 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 276 | // No need to null check as this has already been carried out in the |
| 277 | // calling method |
Christopher Faulet | f11b1fb | 2020-05-05 11:53:43 +0200 | [diff] [blame] | 278 | htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Dragan Dosen | d1ba552 | 2020-07-01 19:58:32 +0200 | [diff] [blame] | 279 | ALREADY_CHECKED(htx); |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 280 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 281 | for (i = 0; i < global_51degrees.header_count; i++) { |
Tim Duesterhus | 92c696e | 2021-02-28 16:11:36 +0100 | [diff] [blame] | 282 | name = ist2((global_51degrees.header_names + i)->area, |
| 283 | (global_51degrees.header_names + i)->data); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 284 | ctx.blk = NULL; |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 285 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 286 | 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 Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 294 | #endif |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 295 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 296 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 297 | static 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 | |
| 304 | static void _51d_set_device_offsets(struct sample *smp, fiftyoneDegreesDeviceOffsets *offsets) |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 305 | { |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 306 | struct channel *chn; |
| 307 | struct htx *htx; |
| 308 | struct http_hdr_ctx ctx; |
| 309 | struct ist name; |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 310 | int i; |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 311 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 312 | offsets->size = 0; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 313 | chn = (smp->strm ? &smp->strm->req : NULL); |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 314 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 315 | // No need to null check as this has already been carried out in the |
| 316 | // calling method |
Christopher Faulet | f11b1fb | 2020-05-05 11:53:43 +0200 | [diff] [blame] | 317 | htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Dragan Dosen | d1ba552 | 2020-07-01 19:58:32 +0200 | [diff] [blame] | 318 | ALREADY_CHECKED(htx); |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 319 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 320 | for (i = 0; i < global_51degrees.header_count; i++) { |
Tim Duesterhus | 92c696e | 2021-02-28 16:11:36 +0100 | [diff] [blame] | 321 | name = ist2((global_51degrees.header_names + i)->area, |
| 322 | (global_51degrees.header_names + i)->data); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 323 | ctx.blk = NULL; |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 324 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 325 | 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++; |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 329 | } |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 330 | } |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 331 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 332 | } |
| 333 | #endif |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 334 | |
| 335 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 336 | /* Provides a hash code for the important HTTP headers. |
| 337 | */ |
| 338 | unsigned 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 Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 345 | hash ^= XXH3(ws->importantHeaders[i].headerValue, |
| 346 | ws->importantHeaders[i].headerValueLength, |
| 347 | seed); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 348 | } |
| 349 | return hash; |
| 350 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 351 | #endif |
| 352 | |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 353 | #ifdef FIFTYONE_DEGREES_HASH_INCLUDED |
| 354 | static 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 | |
| 373 | static 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 | |
| 392 | static 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 | |
| 411 | static 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 | |
| 430 | static 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 | |
| 449 | static 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 | |
| 475 | out: |
| 476 | fiftyoneDegreesDataSetRelease((fiftyoneDegreesDataSetBase *)ds); |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | static 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 Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 532 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 533 | static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesWorkset* ws) |
| 534 | { |
| 535 | char *methodName; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 536 | #endif |
| 537 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 538 | static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesDeviceOffsets *offsets) |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 539 | { |
| 540 | char valuesBuffer[1024]; |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 541 | const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(&global_51degrees.data_set); |
| 542 | int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(&global_51degrees.data_set); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 543 | #endif |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 544 | const char* property_name; |
| 545 | int j; |
| 546 | |
| 547 | #elif defined(FIFTYONE_DEGREES_HASH_INCLUDED) |
| 548 | static void _51d_process_match(const struct arg *args, struct sample *smp) |
| 549 | { |
| 550 | char valuesBuffer[1024]; |
| 551 | #endif |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 552 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 553 | char no_data[] = "NoData"; /* response when no data could be found */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 554 | struct buffer *temp = get_trash_chunk(); |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 555 | int i = 0, found; |
| 556 | |
| 557 | #if defined(FIFTYONE_DEGREES_HASH_INCLUDED) |
| 558 | FIFTYONE_DEGREES_EXCEPTION_CREATE; |
| 559 | #endif |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 560 | |
| 561 | /* Loop through property names passed to the filter and fetch them from the dataset. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 562 | while (args[i].data.str.area) { |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 563 | /* Try to find request property in dataset. */ |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 564 | found = 0; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 565 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 566 | if (strcmp("Method", args[i].data.str.area) == 0) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 567 | 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 Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 574 | } |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 575 | chunk_appendf(temp, "%s", methodName); |
| 576 | found = 1; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 577 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 578 | else if (strcmp("Difference", args[i].data.str.area) == 0) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 579 | chunk_appendf(temp, "%d", ws->difference); |
| 580 | found = 1; |
| 581 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 582 | else if (strcmp("Rank", args[i].data.str.area) == 0) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 583 | 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 Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 589 | if (strcmp(property_name, args[i].data.str.area) == 0) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 590 | found = 1; |
| 591 | fiftyoneDegreesSetValues(ws, j); |
| 592 | chunk_appendf(temp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values)); |
| 593 | break; |
| 594 | } |
| 595 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 596 | } |
| 597 | #endif |
| 598 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 599 | found = 0; |
| 600 | for (j = 0; j < requiredPropertiesCount; j++) { |
| 601 | property_name = requiredProperties[j]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 602 | if (strcmp(property_name, args[i].data.str.area) == 0 && |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 603 | fiftyoneDegreesGetValueFromOffsets(&global_51degrees.data_set, offsets, j, valuesBuffer, 1024) > 0) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 604 | found = 1; |
| 605 | chunk_appendf(temp, "%s", valuesBuffer); |
| 606 | break; |
| 607 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 608 | } |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 609 | #endif |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 610 | #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.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 623 | if (!found) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 624 | chunk_appendf(temp, "%s", no_data); |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 625 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 626 | /* Add separator. */ |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 627 | chunk_appendf(temp, "%c", global_51degrees.property_separator); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 628 | ++i; |
| 629 | } |
| 630 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 631 | if (temp->data) { |
| 632 | --temp->data; |
| 633 | temp->area[temp->data] = '\0'; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 634 | } |
| 635 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 636 | smp->data.u.str.area = temp->area; |
| 637 | smp->data.u.str.data = temp->data; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 638 | } |
| 639 | |
Ben51Degrees | 6bf0672 | 2020-01-20 11:25:11 +0000 | [diff] [blame] | 640 | /* Sets the sample data as a constant string. This ensures that the |
| 641 | * string will be processed correctly. |
| 642 | */ |
| 643 | static 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 Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 655 | static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 656 | { |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 657 | struct channel *chn; |
| 658 | struct htx *htx; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 659 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 660 | fiftyoneDegreesWorkset* ws; /* workset for detection */ |
| 661 | struct lru64 *lru = NULL; |
| 662 | #endif |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 663 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 664 | fiftyoneDegreesDeviceOffsets *offsets; /* Offsets for detection */ |
| 665 | #endif |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 666 | #ifdef FIFTYONE_DEGREES_HASH_INCLUDED |
| 667 | fiftyoneDegreesEvidenceKeyValuePairArray *evidence = NULL; |
| 668 | FIFTYONE_DEGREES_EXCEPTION_CREATE; |
| 669 | #endif |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 670 | |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 671 | chn = (smp->strm ? &smp->strm->req : NULL); |
Christopher Faulet | f11b1fb | 2020-05-05 11:53:43 +0200 | [diff] [blame] | 672 | htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 673 | if (!htx) |
| 674 | return 0; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 675 | |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 676 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 677 | #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 Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 685 | ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 686 | 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) { |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 694 | HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock); |
| 695 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 696 | lru = lru64_get(_51d_req_hash(args, ws), |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 697 | _51d_lru_tree, (void*)args, 0); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 698 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 699 | if (lru && lru->domain) { |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 700 | fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws); |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 701 | _51d_retrieve_cache_entry(smp, lru); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 702 | HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock); |
Ben51Degrees | 6bf0672 | 2020-01-20 11:25:11 +0000 | [diff] [blame] | 703 | |
| 704 | _51d_set_smp(smp); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 705 | return 1; |
| 706 | } |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 707 | HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | fiftyoneDegreesMatchForHttpHeaders(ws); |
| 711 | |
| 712 | _51d_process_match(args, smp, ws); |
| 713 | |
| 714 | #endif |
| 715 | |
| 716 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 717 | #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 Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 723 | |
| 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 | */ |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 727 | _51d_set_device_offsets(smp, offsets); |
| 728 | _51d_process_match(args, smp, offsets); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 729 | |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 730 | #ifndef FIFTYONEDEGREES_NO_THREADING |
| 731 | fiftyoneDegreesFreeDeviceOffsets(offsets); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 732 | #endif |
| 733 | |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 734 | #endif |
| 735 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 736 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 737 | fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws); |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 738 | if (lru) |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 739 | _51d_insert_cache_entry(smp, lru, (void*)args); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 740 | #endif |
| 741 | |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 742 | #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 | |
Ben51Degrees | 6bf0672 | 2020-01-20 11:25:11 +0000 | [diff] [blame] | 757 | _51d_set_smp(smp); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 758 | return 1; |
| 759 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 760 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 761 | static int _51d_conv(const struct arg *args, struct sample *smp, void *private) |
| 762 | { |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 763 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 764 | fiftyoneDegreesWorkset* ws; /* workset for detection */ |
| 765 | struct lru64 *lru = NULL; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 766 | #endif |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 767 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 768 | fiftyoneDegreesDeviceOffsets *offsets; /* Offsets for detection */ |
| 769 | #endif |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 770 | #ifdef FIFTYONE_DEGREES_HASH_INCLUDED |
| 771 | FIFTYONE_DEGREES_EXCEPTION_CREATE; |
| 772 | #endif |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 773 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 774 | #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 | |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 780 | HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock); |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 781 | lru = lru64_get(XXH3(smp->data.u.str.area, smp->data.u.str.data, seed), |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 782 | _51d_lru_tree, (void*)args, 0); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 783 | if (lru && lru->domain) { |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 784 | _51d_retrieve_cache_entry(smp, lru); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 785 | HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 786 | return 1; |
| 787 | } |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 788 | HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | /* Create workset. This will later contain detection results. */ |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 792 | ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 793 | 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 Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 801 | smp->data.u.str.area[smp->data.u.str.data] = '\0'; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 802 | |
| 803 | /* Perform detection. */ |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 804 | #if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) || defined(FIFTYONEDEGREES_H_TRIE_INCLUDED) |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 805 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 806 | fiftyoneDegreesMatch(ws, smp->data.u.str.area); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 807 | _51d_process_match(args, smp, ws); |
| 808 | #endif |
| 809 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 810 | #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 Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 821 | #endif |
| 822 | |
| 823 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 824 | fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws); |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 825 | if (lru) |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 826 | _51d_insert_cache_entry(smp, lru, (void*)args); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 827 | #endif |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 828 | |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 829 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 830 | #ifndef FIFTYONEDEGREES_NO_THREADING |
| 831 | fiftyoneDegreesFreeDeviceOffsets(offsets); |
| 832 | #endif |
| 833 | #endif |
| 834 | |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 835 | #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 | |
Ben51Degrees | 6bf0672 | 2020-01-20 11:25:11 +0000 | [diff] [blame] | 844 | _51d_set_smp(smp); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 845 | return 1; |
| 846 | } |
| 847 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 848 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 849 | void _51d_init_http_headers() |
| 850 | { |
| 851 | int index = 0; |
| 852 | const fiftyoneDegreesAsciiString *headerName; |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 853 | fiftyoneDegreesDataSet *ds = &global_51degrees.data_set; |
| 854 | global_51degrees.header_count = ds->httpHeadersCount; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 855 | global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer)); |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 856 | for (index = 0; index < global_51degrees.header_count; index++) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 857 | headerName = fiftyoneDegreesGetString(ds, ds->httpHeaders[index].headerNameOffset); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 858 | (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 Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | #endif |
| 864 | |
| 865 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 866 | void _51d_init_http_headers() |
| 867 | { |
| 868 | int index = 0; |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 869 | fiftyoneDegreesDataSet *ds = &global_51degrees.data_set; |
| 870 | global_51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(ds); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 871 | #ifdef FIFTYONEDEGREES_NO_THREADING |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 872 | global_51degrees.device_offsets.firstOffset = malloc( |
| 873 | global_51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset)); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 874 | _51d_init_device_offsets(&global_51degrees.device_offsets); |
| 875 | #endif |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 876 | global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer)); |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 877 | 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); |
Ben51Degrees | e0f6a2a | 2019-02-05 13:23:06 +0000 | [diff] [blame] | 880 | 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 Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 882 | global_51degrees.header_names[index].size = global_51degrees.header_names->data; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | #endif |
| 886 | |
Willy Tarreau | 9f3f254 | 2016-12-21 20:30:05 +0100 | [diff] [blame] | 887 | /* |
| 888 | * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*. |
| 889 | */ |
| 890 | static int init_51degrees(void) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 891 | { |
| 892 | int i = 0; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 893 | struct _51d_property_names *name; |
| 894 | char **_51d_property_list = NULL; |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 895 | #if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) || defined(FIFTYONEDEGREES_H_TRIE_INCLUDED) |
| 896 | struct buffer *temp; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 897 | fiftyoneDegreesDataSetInitStatus _51d_dataset_status = DATA_SET_INIT_STATUS_NOT_SET; |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 898 | #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 Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 905 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 906 | if (!global_51degrees.data_file_path) |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 907 | return ERR_NONE; |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 908 | |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 909 | if (global.nbthread < 1) { |
| 910 | ha_alert("51Degrees: The thread count cannot be zero or negative.\n"); |
Christopher Faulet | e8ca434 | 2017-10-25 17:23:02 +0200 | [diff] [blame] | 911 | return (ERR_FATAL | ERR_ALERT); |
| 912 | } |
| 913 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 914 | if (!LIST_ISEMPTY(&global_51degrees.property_names)) { |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 915 | i = 0; |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 916 | list_for_each_entry(name, &global_51degrees.property_names, list) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 917 | ++i; |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 918 | _51d_property_list = calloc(i, sizeof(*_51d_property_list)); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 919 | |
| 920 | i = 0; |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 921 | list_for_each_entry(name, &global_51degrees.property_names, list) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 922 | _51d_property_list[i++] = name->name; |
| 923 | } |
| 924 | |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 925 | #if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) || defined(FIFTYONEDEGREES_H_TRIE_INCLUDED) |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 926 | _51d_dataset_status = fiftyoneDegreesInitWithPropertyArray(global_51degrees.data_file_path, &global_51degrees.data_set, (const char**)_51d_property_list, i); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 927 | |
| 928 | temp = get_trash_chunk(); |
| 929 | chunk_reset(temp); |
| 930 | |
| 931 | switch (_51d_dataset_status) { |
| 932 | case DATA_SET_INIT_STATUS_SUCCESS: |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 933 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 934 | global_51degrees.pool = fiftyoneDegreesWorksetPoolCreate(&global_51degrees.data_set, NULL, global.nbthread); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 935 | #endif |
| 936 | _51d_init_http_headers(); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 937 | 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; |
ben51degrees | 1f077eb | 2016-07-06 12:07:21 +0100 | [diff] [blame] | 960 | 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 Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 967 | case DATA_SET_INIT_STATUS_NOT_SET: |
| 968 | chunk_printf(temp, "Data set not initialised."); |
| 969 | break; |
Dragan Dosen | 483b93c | 2017-09-27 12:46:44 +0200 | [diff] [blame] | 970 | default: |
| 971 | chunk_printf(temp, "Other error."); |
| 972 | break; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 973 | } |
| 974 | if (_51d_dataset_status != DATA_SET_INIT_STATUS_SUCCESS) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 975 | if (temp->data) |
| 976 | ha_alert("51Degrees Setup - Error reading 51Degrees data file. %s\n", |
| 977 | temp->area); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 978 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 979 | ha_alert("51Degrees Setup - Error reading 51Degrees data file.\n"); |
Willy Tarreau | 9f3f254 | 2016-12-21 20:30:05 +0100 | [diff] [blame] | 980 | return ERR_ALERT | ERR_FATAL; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 981 | } |
| 982 | free(_51d_property_list); |
| 983 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 984 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 985 | _51d_lru_seed = ha_random(); |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 986 | if (global_51degrees.cache_size) { |
| 987 | _51d_lru_tree = lru64_new(global_51degrees.cache_size); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 988 | } |
| 989 | #endif |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 990 | |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 991 | #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 Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1048 | return ERR_NONE; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
Willy Tarreau | 7ac4c20 | 2016-12-21 20:59:01 +0100 | [diff] [blame] | 1051 | static void deinit_51degrees(void) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1052 | { |
| 1053 | struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb; |
| 1054 | |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 1055 | #if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) || defined(FIFTYONEDEGREES_H_TRIE_INCLUDED) |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 1056 | free(global_51degrees.header_names); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1057 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Dragan Dosen | bc6218e | 2019-03-07 15:24:23 +0100 | [diff] [blame] | 1058 | if (global_51degrees.pool) |
| 1059 | fiftyoneDegreesWorksetPoolFree(global_51degrees.pool); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1060 | #endif |
| 1061 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 1062 | #ifdef FIFTYONEDEGREES_NO_THREADING |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 1063 | free(global_51degrees.device_offsets.firstOffset); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 1064 | #endif |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 1065 | free(global_51degrees.header_offsets); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1066 | #endif |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 1067 | fiftyoneDegreesDataSetFree(&global_51degrees.data_set); |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 1068 | #endif |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1069 | |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 1070 | ha_free(&global_51degrees.data_file_path); |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 1071 | list_for_each_entry_safe(_51d_prop_name, _51d_prop_nameb, &global_51degrees.property_names, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1072 | LIST_DELETE(&_51d_prop_name->list); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1073 | free(_51d_prop_name); |
| 1074 | } |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 1075 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 1076 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 1077 | while (lru64_destroy(_51d_lru_tree)); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 1078 | #endif |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1079 | } |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 1080 | |
| 1081 | #ifdef FIFTYONE_DEGREES_HASH_INCLUDED |
| 1082 | static 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 | |
| 1102 | static 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 Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1122 | |
| 1123 | static 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 Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 1127 | { CFG_GLOBAL, "51degrees-cache-size", _51d_cache_size }, |
Dragan Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 1128 | #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 Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1135 | { 0, NULL, NULL }, |
| 1136 | }}; |
| 1137 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1138 | INITCALL1(STG_REGISTER, cfg_register_keywords, &_51dcfg_kws); |
| 1139 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1140 | /* Note: must not be declared <const> as its list will be overwritten */ |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 1141 | static 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 Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1146 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 1147 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 1148 | /* Note: must not be declared <const> as its list will be overwritten */ |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1149 | static struct sample_conv_kw_list conv_kws = {ILH, { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 1150 | { "51d.single", _51d_conv, ARG5(1,STR,STR,STR,STR,STR), _51d_conv_check, SMP_T_STR, SMP_T_STR }, |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 1151 | { NULL, NULL, 0, 0, 0 }, |
| 1152 | }}; |
| 1153 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1154 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 1155 | |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 1156 | REGISTER_POST_CHECK(init_51degrees); |
| 1157 | REGISTER_POST_DEINIT(deinit_51degrees); |
Ben51Degrees | f4a82fb | 2019-06-13 16:51:59 +0100 | [diff] [blame] | 1158 | |
| 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 Dosen | a9800a0 | 2022-02-14 13:05:45 +0100 | [diff] [blame] | 1171 | #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 Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 1179 | #endif |