-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathbackend.c
200 lines (174 loc) · 5.97 KB
/
backend.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (c) 2011 and 2012, Dustin Lundquist <dustin@null-ptr.net>
* Copyright (c) 2011 Manuel Kasper <mk@neon1.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
#include <assert.h>
#include "backend.h"
#include "address.h"
#include "logger.h"
static void free_backend(struct Backend *);
static const char *backend_config_options(const struct Backend *);
struct Backend *
new_backend() {
struct Backend *backend;
backend = calloc(1, sizeof(struct Backend));
if (backend == NULL) {
err("malloc");
return NULL;
}
return backend;
}
int
accept_backend_arg(struct Backend *backend, const char *arg) {
if (backend->pattern == NULL) {
backend->pattern = strdup(arg);
if (backend->pattern == NULL) {
err("strdup failed");
return -1;
}
} else if (backend->address == NULL) {
backend->address = new_address(arg);
if (backend->address == NULL) {
err("invalid address: %s", arg);
return -1;
}
#ifndef HAVE_LIBUDNS
if (!address_is_sockaddr(backend->address)) {
err("Only socket address backends are permitted when compiled without libudns");
return -1;
}
#endif
} else if (address_port(backend->address) == 0 && is_numeric(arg)) {
if (!address_set_port_str(backend->address, arg)) {
err("Invalid port: %s", arg);
return -1;
}
} else if (backend->use_proxy_header == 0 &&
strcasecmp(arg, "proxy_protocol") == 0) {
backend->use_proxy_header = 1;
} else {
err("Unexpected table backend argument: %s", arg);
return -1;
}
return 1;
}
void
add_backend(struct Backend_head *backends, struct Backend *backend) {
STAILQ_INSERT_TAIL(backends, backend, entries);
}
int
init_backend(struct Backend *backend) {
if (backend->pattern_re == NULL) {
#if defined(HAVE_LIBPCRE2_8)
int reerr;
size_t reerroffset;
backend->pattern_re =
pcre2_compile((const uint8_t *)backend->pattern, PCRE2_ZERO_TERMINATED, 0, &reerr, &reerroffset, NULL);
if (backend->pattern_re == NULL) {
err("Regex compilation of \"%s\" failed: %d, offset %zu",
backend->pattern, reerr, reerroffset);
return 0;
}
#elif defined(HAVE_LIBPCRE)
const char *reerr;
int reerroffset;
backend->pattern_re =
pcre_compile(backend->pattern, 0, &reerr, &reerroffset, NULL);
if (backend->pattern_re == NULL) {
err("Regex compilation of \"%s\" failed: %s, offset %d",
backend->pattern, reerr, reerroffset);
return 0;
}
#endif
char address[ADDRESS_BUFFER_SIZE];
debug("Parsed %s %s",
backend->pattern,
display_address(backend->address,
address, sizeof(address)));
}
return 1;
}
struct Backend *
lookup_backend(const struct Backend_head *head, const char *name, size_t name_len) {
struct Backend *iter;
if (name == NULL) {
name = "";
name_len = 0;
}
STAILQ_FOREACH(iter, head, entries) {
assert(iter->pattern_re != NULL);
#if defined(HAVE_LIBPCRE2_8)
pcre2_match_data *md = pcre2_match_data_create_from_pattern(iter->pattern_re, NULL);
int ret = pcre2_match(iter->pattern_re, (const uint8_t *)name, name_len, 0, 0, md, NULL);
pcre2_match_data_free(md);
if (ret >= 0)
return iter;
#elif defined(HAVE_LIBPCRE)
if (pcre_exec(iter->pattern_re, NULL,
name, name_len, 0, 0, NULL, 0) >= 0)
return iter;
#endif
}
return NULL;
}
void
print_backend_config(FILE *file, const struct Backend *backend) {
char address[ADDRESS_BUFFER_SIZE];
fprintf(file, "\t%s %s%s\n",
backend->pattern,
display_address(backend->address, address, sizeof(address)),
backend_config_options(backend));
}
static const char *
backend_config_options(const struct Backend *backend) {
if (backend->use_proxy_header)
return " proxy_protocol";
else
return "";
}
void
remove_backend(struct Backend_head *head, struct Backend *backend) {
STAILQ_REMOVE(head, backend, Backend, entries);
free_backend(backend);
}
static void
free_backend(struct Backend *backend) {
if (backend == NULL)
return;
free(backend->pattern);
free(backend->address);
#if defined(HAVE_LIBPCRE2_8)
if (backend->pattern_re != NULL)
pcre2_code_free(backend->pattern_re);
#elif defined(HAVE_LIBPCRE)
if (backend->pattern_re != NULL)
pcre_free(backend->pattern_re);
#endif
free(backend);
}