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