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