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> |
| 4 | #include <import/xxhash.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 5 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 6 | #include <haproxy/arg.h> |
| 7 | #include <haproxy/buf-t.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 8 | #include <haproxy/cfgparse.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 9 | #include <haproxy/chunk.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 10 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 11 | #include <haproxy/global.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 12 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 13 | #include <haproxy/http_fetch.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 14 | #include <haproxy/http_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> |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 18 | #include <51Degrees.h> |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 19 | |
| 20 | struct _51d_property_names { |
| 21 | struct list list; |
| 22 | char *name; |
| 23 | }; |
| 24 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 25 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 26 | static struct lru64_head *_51d_lru_tree = NULL; |
| 27 | static unsigned long long _51d_lru_seed; |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 28 | |
| 29 | __decl_spinlock(_51d_lru_lock); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 30 | #endif |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 32 | static struct { |
| 33 | char property_separator; /* the separator to use in the response for the values. this is taken from 51degrees-property-separator from config. */ |
| 34 | struct list property_names; /* list of properties to load into the data set. this is taken from 51degrees-property-name-list from config. */ |
| 35 | char *data_file_path; |
| 36 | int header_count; /* number of HTTP headers related to device detection. */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 37 | struct buffer *header_names; /* array of HTTP header names. */ |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 38 | fiftyoneDegreesDataSet data_set; /* data set used with the pattern and trie detection methods. */ |
| 39 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 40 | fiftyoneDegreesWorksetPool *pool; /* pool of worksets to avoid creating a new one for each request. */ |
| 41 | #endif |
| 42 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 43 | int32_t *header_offsets; /* offsets to the HTTP header name string. */ |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 44 | #ifdef FIFTYONEDEGREES_NO_THREADING |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 45 | fiftyoneDegreesDeviceOffsets device_offsets; /* Memory used for device offsets. */ |
| 46 | #endif |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 47 | #endif |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 48 | int cache_size; |
| 49 | } global_51degrees = { |
| 50 | .property_separator = ',', |
| 51 | .property_names = LIST_HEAD_INIT(global_51degrees.property_names), |
| 52 | .data_file_path = NULL, |
| 53 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 54 | .data_set = { }, |
| 55 | #endif |
| 56 | .cache_size = 0, |
| 57 | }; |
| 58 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 59 | static int _51d_data_file(char **args, int section_type, struct proxy *curpx, |
| 60 | struct proxy *defpx, const char *file, int line, |
| 61 | char **err) |
| 62 | { |
| 63 | if (*(args[1]) == 0) { |
| 64 | memprintf(err, |
| 65 | "'%s' expects a filepath to a 51Degrees trie or pattern data file.", |
| 66 | args[0]); |
| 67 | return -1; |
| 68 | } |
| 69 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 70 | if (global_51degrees.data_file_path) |
| 71 | free(global_51degrees.data_file_path); |
| 72 | global_51degrees.data_file_path = strdup(args[1]); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int _51d_property_name_list(char **args, int section_type, struct proxy *curpx, |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 78 | struct proxy *defpx, const char *file, int line, |
| 79 | char **err) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 80 | { |
| 81 | int cur_arg = 1; |
| 82 | struct _51d_property_names *name; |
| 83 | |
| 84 | if (*(args[cur_arg]) == 0) { |
| 85 | memprintf(err, |
| 86 | "'%s' expects at least one 51Degrees property name.", |
| 87 | args[0]); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | while (*(args[cur_arg])) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 92 | name = calloc(1, sizeof(*name)); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 93 | name->name = strdup(args[cur_arg]); |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 94 | LIST_ADDQ(&global_51degrees.property_names, &name->list); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 95 | ++cur_arg; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int _51d_property_separator(char **args, int section_type, struct proxy *curpx, |
| 102 | struct proxy *defpx, const char *file, int line, |
| 103 | char **err) |
| 104 | { |
| 105 | if (*(args[1]) == 0) { |
| 106 | memprintf(err, |
| 107 | "'%s' expects a single character.", |
| 108 | args[0]); |
| 109 | return -1; |
| 110 | } |
| 111 | if (strlen(args[1]) > 1) { |
| 112 | memprintf(err, |
| 113 | "'%s' expects a single character, got '%s'.", |
| 114 | args[0], args[1]); |
| 115 | return -1; |
| 116 | } |
| 117 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 118 | global_51degrees.property_separator = *args[1]; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 123 | static int _51d_cache_size(char **args, int section_type, struct proxy *curpx, |
| 124 | struct proxy *defpx, const char *file, int line, |
| 125 | char **err) |
| 126 | { |
| 127 | if (*(args[1]) == 0) { |
| 128 | memprintf(err, |
| 129 | "'%s' expects a positive numeric value.", |
| 130 | args[0]); |
| 131 | return -1; |
| 132 | } |
| 133 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 134 | global_51degrees.cache_size = atoi(args[1]); |
| 135 | if (global_51degrees.cache_size < 0) { |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 136 | memprintf(err, |
| 137 | "'%s' expects a positive numeric value, got '%s'.", |
| 138 | args[0], args[1]); |
| 139 | return -1; |
| 140 | } |
| 141 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int _51d_fetch_check(struct arg *arg, char **err_msg) |
| 146 | { |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 147 | if (global_51degrees.data_file_path) |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 148 | return 1; |
| 149 | |
| 150 | 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] | 151 | return 0; |
| 152 | } |
| 153 | |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 154 | static int _51d_conv_check(struct arg *arg, struct sample_conv *conv, |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 155 | const char *file, int line, char **err_msg) |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 156 | { |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 157 | if (global_51degrees.data_file_path) |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 158 | return 1; |
| 159 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 160 | 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] | 161 | return 0; |
| 162 | } |
| 163 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 164 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 165 | static void _51d_lru_free(void *cache_entry) |
| 166 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 167 | struct buffer *ptr = cache_entry; |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 168 | |
| 169 | if (!ptr) |
| 170 | return; |
| 171 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 172 | free(ptr->area); |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 173 | free(ptr); |
| 174 | } |
| 175 | |
| 176 | /* Allocates memory freeing space in the cache if necessary. |
| 177 | */ |
| 178 | static void *_51d_malloc(int size) |
| 179 | { |
| 180 | void *ptr = malloc(size); |
| 181 | |
| 182 | if (!ptr) { |
| 183 | /* free the oldest 10 entries from lru to free up some memory |
| 184 | * then try allocating memory again */ |
| 185 | lru64_kill_oldest(_51d_lru_tree, 10); |
| 186 | ptr = malloc(size); |
| 187 | } |
| 188 | |
| 189 | return ptr; |
| 190 | } |
| 191 | |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 192 | /* Insert the data associated with the sample into the cache as a fresh item. |
| 193 | */ |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 194 | 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] | 195 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 196 | struct buffer *cache_entry = _51d_malloc(sizeof(*cache_entry)); |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 197 | |
| 198 | if (!cache_entry) |
| 199 | return; |
| 200 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 201 | cache_entry->area = _51d_malloc(smp->data.u.str.data + 1); |
| 202 | if (!cache_entry->area) { |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 203 | free(cache_entry); |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 204 | return; |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 205 | } |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 206 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 207 | memcpy(cache_entry->area, smp->data.u.str.area, smp->data.u.str.data); |
| 208 | cache_entry->area[smp->data.u.str.data] = 0; |
| 209 | cache_entry->data = smp->data.u.str.data; |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 210 | HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock); |
ben@51degrees.com | c9dfa24 | 2016-01-08 13:47:46 +0000 | [diff] [blame] | 211 | lru64_commit(lru, cache_entry, domain, 0, _51d_lru_free); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 212 | HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock); |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* Retrieves the data from the cache and sets the sample data to this string. |
| 216 | */ |
| 217 | static void _51d_retrieve_cache_entry(struct sample *smp, struct lru64 *lru) |
| 218 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 219 | struct buffer *cache_entry = lru->data; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 220 | smp->data.u.str.area = cache_entry->area; |
| 221 | smp->data.u.str.data = cache_entry->data; |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 222 | } |
| 223 | #endif |
| 224 | |
| 225 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 226 | /* Sets the important HTTP headers ahead of the detection |
| 227 | */ |
| 228 | static void _51d_set_headers(struct sample *smp, fiftyoneDegreesWorkset *ws) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 229 | { |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 230 | struct channel *chn; |
| 231 | struct htx *htx; |
| 232 | struct http_hdr_ctx ctx; |
| 233 | struct ist name; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 234 | int i; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 235 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 236 | ws->importantHeadersCount = 0; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 237 | chn = (smp->strm ? &smp->strm->req : NULL); |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 238 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 239 | // No need to null check as this has already been carried out in the |
| 240 | // calling method |
Christopher Faulet | f11b1fb | 2020-05-05 11:53:43 +0200 | [diff] [blame] | 241 | htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Dragan Dosen | d1ba552 | 2020-07-01 19:58:32 +0200 | [diff] [blame] | 242 | ALREADY_CHECKED(htx); |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 243 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 244 | for (i = 0; i < global_51degrees.header_count; i++) { |
| 245 | name.ptr = (global_51degrees.header_names + i)->area; |
| 246 | name.len = (global_51degrees.header_names + i)->data; |
| 247 | ctx.blk = NULL; |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 248 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 249 | if (http_find_header(htx, name, &ctx, 1)) { |
| 250 | ws->importantHeaders[ws->importantHeadersCount].header = ws->dataSet->httpHeaders + i; |
| 251 | ws->importantHeaders[ws->importantHeadersCount].headerValue = ctx.value.ptr; |
| 252 | ws->importantHeaders[ws->importantHeadersCount].headerValueLength = ctx.value.len; |
| 253 | ws->importantHeadersCount++; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 257 | #endif |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 258 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 259 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 260 | static void _51d_init_device_offsets(fiftyoneDegreesDeviceOffsets *offsets) { |
| 261 | int i; |
| 262 | for (i = 0; i < global_51degrees.data_set.uniqueHttpHeaders.count; i++) { |
| 263 | offsets->firstOffset[i].userAgent = NULL; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static void _51d_set_device_offsets(struct sample *smp, fiftyoneDegreesDeviceOffsets *offsets) |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 268 | { |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 269 | struct channel *chn; |
| 270 | struct htx *htx; |
| 271 | struct http_hdr_ctx ctx; |
| 272 | struct ist name; |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 273 | int i; |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 274 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 275 | offsets->size = 0; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 276 | chn = (smp->strm ? &smp->strm->req : NULL); |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 277 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 278 | // No need to null check as this has already been carried out in the |
| 279 | // calling method |
Christopher Faulet | f11b1fb | 2020-05-05 11:53:43 +0200 | [diff] [blame] | 280 | htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Dragan Dosen | d1ba552 | 2020-07-01 19:58:32 +0200 | [diff] [blame] | 281 | ALREADY_CHECKED(htx); |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 282 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 283 | for (i = 0; i < global_51degrees.header_count; i++) { |
| 284 | name.ptr = (global_51degrees.header_names + i)->area; |
| 285 | name.len = (global_51degrees.header_names + i)->data; |
| 286 | ctx.blk = NULL; |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 287 | |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 288 | if (http_find_header(htx, name, &ctx, 1)) { |
| 289 | (offsets->firstOffset + offsets->size)->httpHeaderOffset = *(global_51degrees.header_offsets + i); |
| 290 | (offsets->firstOffset + offsets->size)->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set, ctx.value.ptr); |
| 291 | offsets->size++; |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 292 | } |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 293 | } |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 294 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 295 | } |
| 296 | #endif |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 297 | |
| 298 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 299 | /* Provides a hash code for the important HTTP headers. |
| 300 | */ |
| 301 | unsigned long long _51d_req_hash(const struct arg *args, fiftyoneDegreesWorkset* ws) |
| 302 | { |
| 303 | unsigned long long seed = _51d_lru_seed ^ (long)args; |
| 304 | unsigned long long hash = 0; |
| 305 | int i; |
| 306 | for(i = 0; i < ws->importantHeadersCount; i++) { |
| 307 | hash ^= ws->importantHeaders[i].header->headerNameOffset; |
| 308 | hash ^= XXH64(ws->importantHeaders[i].headerValue, |
| 309 | ws->importantHeaders[i].headerValueLength, |
| 310 | seed); |
| 311 | } |
| 312 | return hash; |
| 313 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 314 | #endif |
| 315 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 316 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 317 | static void _51d_process_match(const struct arg *args, struct sample *smp, fiftyoneDegreesWorkset* ws) |
| 318 | { |
| 319 | char *methodName; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 320 | #endif |
| 321 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 322 | 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] | 323 | { |
| 324 | char valuesBuffer[1024]; |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 325 | const char **requiredProperties = fiftyoneDegreesGetRequiredPropertiesNames(&global_51degrees.data_set); |
| 326 | int requiredPropertiesCount = fiftyoneDegreesGetRequiredPropertiesCount(&global_51degrees.data_set); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 327 | #endif |
| 328 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 329 | char no_data[] = "NoData"; /* response when no data could be found */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 330 | struct buffer *temp = get_trash_chunk(); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 331 | int j, i = 0, found; |
| 332 | const char* property_name; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 333 | |
| 334 | /* 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] | 335 | while (args[i].data.str.area) { |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 336 | /* Try to find request property in dataset. */ |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 337 | found = 0; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 338 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 339 | if (strcmp("Method", args[i].data.str.area) == 0) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 340 | switch(ws->method) { |
| 341 | case EXACT: methodName = "Exact"; break; |
| 342 | case NUMERIC: methodName = "Numeric"; break; |
| 343 | case NEAREST: methodName = "Nearest"; break; |
| 344 | case CLOSEST: methodName = "Closest"; break; |
| 345 | default: |
| 346 | case NONE: methodName = "None"; break; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 347 | } |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 348 | chunk_appendf(temp, "%s", methodName); |
| 349 | found = 1; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 350 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 351 | else if (strcmp("Difference", args[i].data.str.area) == 0) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 352 | chunk_appendf(temp, "%d", ws->difference); |
| 353 | found = 1; |
| 354 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 355 | else if (strcmp("Rank", args[i].data.str.area) == 0) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 356 | chunk_appendf(temp, "%d", fiftyoneDegreesGetSignatureRank(ws)); |
| 357 | found = 1; |
| 358 | } |
| 359 | else { |
| 360 | for (j = 0; j < ws->dataSet->requiredPropertyCount; j++) { |
| 361 | property_name = fiftyoneDegreesGetPropertyName(ws->dataSet, ws->dataSet->requiredProperties[j]); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 362 | if (strcmp(property_name, args[i].data.str.area) == 0) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 363 | found = 1; |
| 364 | fiftyoneDegreesSetValues(ws, j); |
| 365 | chunk_appendf(temp, "%s", fiftyoneDegreesGetValueName(ws->dataSet, *ws->values)); |
| 366 | break; |
| 367 | } |
| 368 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 369 | } |
| 370 | #endif |
| 371 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 372 | found = 0; |
| 373 | for (j = 0; j < requiredPropertiesCount; j++) { |
| 374 | property_name = requiredProperties[j]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 375 | if (strcmp(property_name, args[i].data.str.area) == 0 && |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 376 | fiftyoneDegreesGetValueFromOffsets(&global_51degrees.data_set, offsets, j, valuesBuffer, 1024) > 0) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 377 | found = 1; |
| 378 | chunk_appendf(temp, "%s", valuesBuffer); |
| 379 | break; |
| 380 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 381 | } |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 382 | #endif |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 383 | if (!found) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 384 | chunk_appendf(temp, "%s", no_data); |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 385 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 386 | /* Add separator. */ |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 387 | chunk_appendf(temp, "%c", global_51degrees.property_separator); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 388 | ++i; |
| 389 | } |
| 390 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 391 | if (temp->data) { |
| 392 | --temp->data; |
| 393 | temp->area[temp->data] = '\0'; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 396 | smp->data.u.str.area = temp->area; |
| 397 | smp->data.u.str.data = temp->data; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Ben51Degrees | 6bf0672 | 2020-01-20 11:25:11 +0000 | [diff] [blame] | 400 | /* Sets the sample data as a constant string. This ensures that the |
| 401 | * string will be processed correctly. |
| 402 | */ |
| 403 | static void _51d_set_smp(struct sample *smp) |
| 404 | { |
| 405 | /* |
| 406 | * Data type has to be set to ensure the string output is processed |
| 407 | * correctly. |
| 408 | */ |
| 409 | smp->data.type = SMP_T_STR; |
| 410 | |
| 411 | /* Flags the sample to show it uses constant memory. */ |
| 412 | smp->flags |= SMP_F_CONST; |
| 413 | } |
| 414 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 415 | static int _51d_fetch(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 416 | { |
| 417 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 418 | fiftyoneDegreesWorkset* ws; /* workset for detection */ |
| 419 | struct lru64 *lru = NULL; |
| 420 | #endif |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 421 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 422 | fiftyoneDegreesDeviceOffsets *offsets; /* Offsets for detection */ |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 423 | |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 424 | #endif |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 425 | struct channel *chn; |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 426 | struct htx *htx; |
| 427 | |
Ben51Degrees | 31a51f2 | 2019-06-12 15:19:12 +0100 | [diff] [blame] | 428 | chn = (smp->strm ? &smp->strm->req : NULL); |
Christopher Faulet | f11b1fb | 2020-05-05 11:53:43 +0200 | [diff] [blame] | 429 | htx = smp_prefetch_htx(smp, chn, NULL, 1); |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 430 | if (!htx) |
| 431 | return 0; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 432 | |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 433 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 434 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 435 | |
| 436 | /* Get only the headers needed for device detection so they can be used |
| 437 | * with the cache to return previous results. Pattern is slower than |
| 438 | * Trie so caching will help improve performance. |
| 439 | */ |
| 440 | |
| 441 | /* Get a workset from the pool which will later contain detection results. */ |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 442 | ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 443 | if (!ws) |
| 444 | return 0; |
| 445 | |
| 446 | /* Set the important HTTP headers for this request in the workset. */ |
| 447 | _51d_set_headers(smp, ws); |
| 448 | |
| 449 | /* Check the cache to see if there's results for these headers already. */ |
| 450 | if (_51d_lru_tree) { |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 451 | HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock); |
| 452 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 453 | lru = lru64_get(_51d_req_hash(args, ws), |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 454 | _51d_lru_tree, (void*)args, 0); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 455 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 456 | if (lru && lru->domain) { |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 457 | fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws); |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 458 | _51d_retrieve_cache_entry(smp, lru); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 459 | HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock); |
Ben51Degrees | 6bf0672 | 2020-01-20 11:25:11 +0000 | [diff] [blame] | 460 | |
| 461 | _51d_set_smp(smp); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 462 | return 1; |
| 463 | } |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 464 | HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | fiftyoneDegreesMatchForHttpHeaders(ws); |
| 468 | |
| 469 | _51d_process_match(args, smp, ws); |
| 470 | |
| 471 | #endif |
| 472 | |
| 473 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 474 | #ifndef FIFTYONEDEGREES_NO_THREADING |
| 475 | offsets = fiftyoneDegreesCreateDeviceOffsets(&global_51degrees.data_set); |
| 476 | _51d_init_device_offsets(offsets); |
| 477 | #else |
| 478 | offsets = &global_51degrees.device_offsets; |
| 479 | #endif |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 480 | |
| 481 | /* Trie is very fast so all the headers can be passed in and the result |
| 482 | * returned faster than the hashing algorithm process. |
| 483 | */ |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 484 | _51d_set_device_offsets(smp, offsets); |
| 485 | _51d_process_match(args, smp, offsets); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 486 | |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 487 | #ifndef FIFTYONEDEGREES_NO_THREADING |
| 488 | fiftyoneDegreesFreeDeviceOffsets(offsets); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 489 | #endif |
| 490 | |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 491 | #endif |
| 492 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 493 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 494 | fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws); |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 495 | if (lru) |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 496 | _51d_insert_cache_entry(smp, lru, (void*)args); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 497 | #endif |
| 498 | |
Ben51Degrees | 6bf0672 | 2020-01-20 11:25:11 +0000 | [diff] [blame] | 499 | _51d_set_smp(smp); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 500 | return 1; |
| 501 | } |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 502 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 503 | static int _51d_conv(const struct arg *args, struct sample *smp, void *private) |
| 504 | { |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 505 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 506 | fiftyoneDegreesWorkset* ws; /* workset for detection */ |
| 507 | struct lru64 *lru = NULL; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 508 | #endif |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 509 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 510 | fiftyoneDegreesDeviceOffsets *offsets; /* Offsets for detection */ |
| 511 | #endif |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 512 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 513 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 514 | |
| 515 | /* Look in the list. */ |
| 516 | if (_51d_lru_tree) { |
| 517 | unsigned long long seed = _51d_lru_seed ^ (long)args; |
| 518 | |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 519 | HA_SPIN_LOCK(OTHER_LOCK, &_51d_lru_lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 520 | lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed), |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 521 | _51d_lru_tree, (void*)args, 0); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 522 | if (lru && lru->domain) { |
James Rosewell | 63426cb | 2015-09-18 19:53:05 +0100 | [diff] [blame] | 523 | _51d_retrieve_cache_entry(smp, lru); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 524 | HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 525 | return 1; |
| 526 | } |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 527 | HA_SPIN_UNLOCK(OTHER_LOCK, &_51d_lru_lock); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* Create workset. This will later contain detection results. */ |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 531 | ws = fiftyoneDegreesWorksetPoolGet(global_51degrees.pool); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 532 | if (!ws) |
| 533 | return 0; |
| 534 | #endif |
| 535 | |
| 536 | /* Duplicate the data and remove the "const" flag before device detection. */ |
| 537 | if (!smp_dup(smp)) |
| 538 | return 0; |
| 539 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 540 | smp->data.u.str.area[smp->data.u.str.data] = '\0'; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 541 | |
| 542 | /* Perform detection. */ |
| 543 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 544 | fiftyoneDegreesMatch(ws, smp->data.u.str.area); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 545 | _51d_process_match(args, smp, ws); |
| 546 | #endif |
| 547 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 548 | #ifndef FIFTYONEDEGREES_NO_THREADING |
| 549 | offsets = fiftyoneDegreesCreateDeviceOffsets(&global_51degrees.data_set); |
| 550 | _51d_init_device_offsets(offsets); |
| 551 | #else |
| 552 | offsets = &global_51degrees.device_offsets; |
| 553 | #endif |
| 554 | |
| 555 | offsets->firstOffset->deviceOffset = fiftyoneDegreesGetDeviceOffset(&global_51degrees.data_set, |
| 556 | smp->data.u.str.area); |
| 557 | offsets->size = 1; |
| 558 | _51d_process_match(args, smp, offsets); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 559 | #endif |
| 560 | |
| 561 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 562 | fiftyoneDegreesWorksetPoolRelease(global_51degrees.pool, ws); |
ben@51degrees.com | d384252 | 2016-01-08 13:52:32 +0000 | [diff] [blame] | 563 | if (lru) |
ben@51degrees.com | 82a9d76 | 2016-01-08 13:42:41 +0000 | [diff] [blame] | 564 | _51d_insert_cache_entry(smp, lru, (void*)args); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 565 | #endif |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 566 | |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 567 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 568 | #ifndef FIFTYONEDEGREES_NO_THREADING |
| 569 | fiftyoneDegreesFreeDeviceOffsets(offsets); |
| 570 | #endif |
| 571 | #endif |
| 572 | |
Ben51Degrees | 6bf0672 | 2020-01-20 11:25:11 +0000 | [diff] [blame] | 573 | _51d_set_smp(smp); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 574 | return 1; |
| 575 | } |
| 576 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 577 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 578 | void _51d_init_http_headers() |
| 579 | { |
| 580 | int index = 0; |
| 581 | const fiftyoneDegreesAsciiString *headerName; |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 582 | fiftyoneDegreesDataSet *ds = &global_51degrees.data_set; |
| 583 | global_51degrees.header_count = ds->httpHeadersCount; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 584 | global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer)); |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 585 | for (index = 0; index < global_51degrees.header_count; index++) { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 586 | headerName = fiftyoneDegreesGetString(ds, ds->httpHeaders[index].headerNameOffset); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 587 | (global_51degrees.header_names + index)->area = (char*)&headerName->firstByte; |
| 588 | (global_51degrees.header_names + index)->data = headerName->length - 1; |
| 589 | (global_51degrees.header_names + index)->size = (global_51degrees.header_names + index)->data; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | #endif |
| 593 | |
| 594 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 595 | void _51d_init_http_headers() |
| 596 | { |
| 597 | int index = 0; |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 598 | fiftyoneDegreesDataSet *ds = &global_51degrees.data_set; |
| 599 | global_51degrees.header_count = fiftyoneDegreesGetHttpHeaderCount(ds); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 600 | #ifdef FIFTYONEDEGREES_NO_THREADING |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 601 | global_51degrees.device_offsets.firstOffset = malloc( |
| 602 | global_51degrees.header_count * sizeof(fiftyoneDegreesDeviceOffset)); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 603 | _51d_init_device_offsets(&global_51degrees.device_offsets); |
| 604 | #endif |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 605 | global_51degrees.header_names = malloc(global_51degrees.header_count * sizeof(struct buffer)); |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 606 | global_51degrees.header_offsets = malloc(global_51degrees.header_count * sizeof(int32_t)); |
| 607 | for (index = 0; index < global_51degrees.header_count; index++) { |
| 608 | global_51degrees.header_offsets[index] = fiftyoneDegreesGetHttpHeaderNameOffset(ds, index); |
Ben51Degrees | e0f6a2a | 2019-02-05 13:23:06 +0000 | [diff] [blame] | 609 | global_51degrees.header_names[index].area = (char*)fiftyoneDegreesGetHttpHeaderNamePointer(ds, index); |
| 610 | 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] | 611 | global_51degrees.header_names[index].size = global_51degrees.header_names->data; |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | #endif |
| 615 | |
Willy Tarreau | 9f3f254 | 2016-12-21 20:30:05 +0100 | [diff] [blame] | 616 | /* |
| 617 | * module init / deinit functions. Returns 0 if OK, or a combination of ERR_*. |
| 618 | */ |
| 619 | static int init_51degrees(void) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 620 | { |
| 621 | int i = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 622 | struct buffer *temp; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 623 | struct _51d_property_names *name; |
| 624 | char **_51d_property_list = NULL; |
| 625 | fiftyoneDegreesDataSetInitStatus _51d_dataset_status = DATA_SET_INIT_STATUS_NOT_SET; |
| 626 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 627 | if (!global_51degrees.data_file_path) |
Willy Tarreau | 9f3f254 | 2016-12-21 20:30:05 +0100 | [diff] [blame] | 628 | return 0; |
Dragan Dosen | 9373fc5 | 2015-08-07 16:41:23 +0200 | [diff] [blame] | 629 | |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 630 | if (global.nbthread < 1) { |
| 631 | ha_alert("51Degrees: The thread count cannot be zero or negative.\n"); |
Christopher Faulet | e8ca434 | 2017-10-25 17:23:02 +0200 | [diff] [blame] | 632 | return (ERR_FATAL | ERR_ALERT); |
| 633 | } |
| 634 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 635 | if (!LIST_ISEMPTY(&global_51degrees.property_names)) { |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 636 | i = 0; |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 637 | list_for_each_entry(name, &global_51degrees.property_names, list) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 638 | ++i; |
| 639 | _51d_property_list = calloc(i, sizeof(char *)); |
| 640 | |
| 641 | i = 0; |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 642 | list_for_each_entry(name, &global_51degrees.property_names, list) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 643 | _51d_property_list[i++] = name->name; |
| 644 | } |
| 645 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 646 | _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] | 647 | |
| 648 | temp = get_trash_chunk(); |
| 649 | chunk_reset(temp); |
| 650 | |
| 651 | switch (_51d_dataset_status) { |
| 652 | case DATA_SET_INIT_STATUS_SUCCESS: |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 653 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 654 | global_51degrees.pool = fiftyoneDegreesWorksetPoolCreate(&global_51degrees.data_set, NULL, global.nbthread); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 655 | #endif |
| 656 | _51d_init_http_headers(); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 657 | break; |
| 658 | case DATA_SET_INIT_STATUS_INSUFFICIENT_MEMORY: |
| 659 | chunk_printf(temp, "Insufficient memory."); |
| 660 | break; |
| 661 | case DATA_SET_INIT_STATUS_CORRUPT_DATA: |
| 662 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 663 | chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Pattern data format."); |
| 664 | #endif |
| 665 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 666 | chunk_printf(temp, "Corrupt data file. Check that the data file provided is uncompressed and Trie data format."); |
| 667 | #endif |
| 668 | break; |
| 669 | case DATA_SET_INIT_STATUS_INCORRECT_VERSION: |
| 670 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
| 671 | chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Pattern data format."); |
| 672 | #endif |
| 673 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
| 674 | chunk_printf(temp, "Incorrect version. Check that the data file provided is uncompressed and Trie data format."); |
| 675 | #endif |
| 676 | break; |
| 677 | case DATA_SET_INIT_STATUS_FILE_NOT_FOUND: |
| 678 | chunk_printf(temp, "File not found."); |
| 679 | break; |
ben51degrees | 1f077eb | 2016-07-06 12:07:21 +0100 | [diff] [blame] | 680 | case DATA_SET_INIT_STATUS_NULL_POINTER: |
| 681 | chunk_printf(temp, "Null pointer to the existing dataset or memory location."); |
| 682 | break; |
| 683 | case DATA_SET_INIT_STATUS_POINTER_OUT_OF_BOUNDS: |
| 684 | chunk_printf(temp, "Allocated continuous memory containing 51Degrees data file appears to be smaller than expected. Most likely" |
| 685 | " because the data file was not fully loaded into the allocated memory."); |
| 686 | break; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 687 | case DATA_SET_INIT_STATUS_NOT_SET: |
| 688 | chunk_printf(temp, "Data set not initialised."); |
| 689 | break; |
Dragan Dosen | 483b93c | 2017-09-27 12:46:44 +0200 | [diff] [blame] | 690 | default: |
| 691 | chunk_printf(temp, "Other error."); |
| 692 | break; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 693 | } |
| 694 | if (_51d_dataset_status != DATA_SET_INIT_STATUS_SUCCESS) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 695 | if (temp->data) |
| 696 | ha_alert("51Degrees Setup - Error reading 51Degrees data file. %s\n", |
| 697 | temp->area); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 698 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 699 | ha_alert("51Degrees Setup - Error reading 51Degrees data file.\n"); |
Willy Tarreau | 9f3f254 | 2016-12-21 20:30:05 +0100 | [diff] [blame] | 700 | return ERR_ALERT | ERR_FATAL; |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 701 | } |
| 702 | free(_51d_property_list); |
| 703 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 704 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 705 | _51d_lru_seed = ha_random(); |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 706 | if (global_51degrees.cache_size) { |
| 707 | _51d_lru_tree = lru64_new(global_51degrees.cache_size); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 708 | } |
| 709 | #endif |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 710 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 711 | return 0; |
| 712 | } |
| 713 | |
Willy Tarreau | 7ac4c20 | 2016-12-21 20:59:01 +0100 | [diff] [blame] | 714 | static void deinit_51degrees(void) |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 715 | { |
| 716 | struct _51d_property_names *_51d_prop_name, *_51d_prop_nameb; |
| 717 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 718 | free(global_51degrees.header_names); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 719 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Dragan Dosen | bc6218e | 2019-03-07 15:24:23 +0100 | [diff] [blame] | 720 | if (global_51degrees.pool) |
| 721 | fiftyoneDegreesWorksetPoolFree(global_51degrees.pool); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 722 | #endif |
| 723 | #ifdef FIFTYONEDEGREES_H_TRIE_INCLUDED |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 724 | #ifdef FIFTYONEDEGREES_NO_THREADING |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 725 | free(global_51degrees.device_offsets.firstOffset); |
Ben51Degrees | 4ddf59d | 2019-02-05 13:24:00 +0000 | [diff] [blame] | 726 | #endif |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 727 | free(global_51degrees.header_offsets); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 728 | #endif |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 729 | fiftyoneDegreesDataSetFree(&global_51degrees.data_set); |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 730 | |
Willy Tarreau | b7a6714 | 2016-12-21 21:18:44 +0100 | [diff] [blame] | 731 | free(global_51degrees.data_file_path); global_51degrees.data_file_path = NULL; |
| 732 | list_for_each_entry_safe(_51d_prop_name, _51d_prop_nameb, &global_51degrees.property_names, list) { |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 733 | LIST_DEL(&_51d_prop_name->list); |
| 734 | free(_51d_prop_name); |
| 735 | } |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 736 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 737 | #ifdef FIFTYONEDEGREES_H_PATTERN_INCLUDED |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 738 | while (lru64_destroy(_51d_lru_tree)); |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 739 | #endif |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static struct cfg_kw_list _51dcfg_kws = {{ }, { |
| 743 | { CFG_GLOBAL, "51degrees-data-file", _51d_data_file }, |
| 744 | { CFG_GLOBAL, "51degrees-property-name-list", _51d_property_name_list }, |
| 745 | { CFG_GLOBAL, "51degrees-property-separator", _51d_property_separator }, |
Dragan Dosen | 105c8e6 | 2015-06-29 16:43:26 +0200 | [diff] [blame] | 746 | { CFG_GLOBAL, "51degrees-cache-size", _51d_cache_size }, |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 747 | { 0, NULL, NULL }, |
| 748 | }}; |
| 749 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 750 | INITCALL1(STG_REGISTER, cfg_register_keywords, &_51dcfg_kws); |
| 751 | |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 752 | /* Note: must not be declared <const> as its list will be overwritten */ |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 753 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
| 754 | { "51d.all", _51d_fetch, ARG5(1,STR,STR,STR,STR,STR), _51d_fetch_check, SMP_T_STR, SMP_USE_HRQHV }, |
| 755 | { NULL, NULL, 0, 0, 0 }, |
| 756 | }}; |
| 757 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 758 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 759 | |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 760 | /* Note: must not be declared <const> as its list will be overwritten */ |
Dragan Dosen | 93b38d9 | 2015-06-29 16:43:25 +0200 | [diff] [blame] | 761 | static struct sample_conv_kw_list conv_kws = {ILH, { |
James Rosewell | a28bbd5 | 2015-09-18 18:28:52 +0100 | [diff] [blame] | 762 | { "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] | 763 | { NULL, NULL, 0, 0, 0 }, |
| 764 | }}; |
| 765 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 766 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 767 | |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 768 | REGISTER_POST_CHECK(init_51degrees); |
| 769 | REGISTER_POST_DEINIT(deinit_51degrees); |
Ben51Degrees | f4a82fb | 2019-06-13 16:51:59 +0100 | [diff] [blame] | 770 | |
| 771 | #if defined(FIFTYONEDEGREES_H_PATTERN_INCLUDED) |
| 772 | #ifndef FIFTYONEDEGREES_DUMMY_LIB |
| 773 | REGISTER_BUILD_OPTS("Built with 51Degrees Pattern support."); |
| 774 | #else |
| 775 | REGISTER_BUILD_OPTS("Built with 51Degrees Pattern support (dummy library)."); |
| 776 | #endif |
| 777 | #elif defined(FIFTYONEDEGREES_H_TRIE_INCLUDED) |
| 778 | #ifndef FIFTYONEDEGREES_DUMMY_LIB |
| 779 | REGISTER_BUILD_OPTS("Built with 51Degrees Trie support."); |
| 780 | #else |
| 781 | REGISTER_BUILD_OPTS("Built with 51Degrees Trie support (dummy library)."); |
| 782 | #endif |
Christopher Faulet | 6d1dd46 | 2019-07-15 14:36:03 +0200 | [diff] [blame] | 783 | #endif |