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