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