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