1 /*
2 * Frontend variables and functions.
3 *
4 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22
23 #include <netinet/tcp.h>
24
25 #include <common/chunk.h>
26 #include <common/compat.h>
27 #include <common/config.h>
28 #include <common/debug.h>
29 #include <common/initcall.h>
30 #include <common/standard.h>
31 #include <common/time.h>
32
33 #include <types/global.h>
34
35 #include <proto/acl.h>
36 #include <proto/arg.h>
37 #include <proto/channel.h>
38 #include <proto/fd.h>
39 #include <proto/frontend.h>
40 #include <proto/log.h>
41 #include <proto/proto_tcp.h>
42 #include <proto/http_ana.h>
43 #include <proto/proxy.h>
44 #include <proto/sample.h>
45 #include <proto/stream.h>
46 #include <proto/stream_interface.h>
47 #include <proto/task.h>
48
49 /* Finish a stream accept() for a proxy (TCP or HTTP). It returns a negative
50 * value in case of a critical failure which must cause the listener to be
51 * disabled, a positive or null value in case of success.
52 */
frontend_accept(struct stream * s)53 int frontend_accept(struct stream *s)
54 {
55 struct session *sess = s->sess;
56 struct connection *conn = objt_conn(sess->origin);
57 struct listener *l = sess->listener;
58 struct proxy *fe = sess->fe;
59
60 if ((fe->mode == PR_MODE_TCP || fe->mode == PR_MODE_HTTP)
61 && (!LIST_ISEMPTY(&fe->logsrvs))) {
62 if (likely(!LIST_ISEMPTY(&fe->logformat))) {
63 /* we have the client ip */
64 if (s->logs.logwait & LW_CLIP)
65 if (!(s->logs.logwait &= ~(LW_CLIP|LW_INIT)))
66 s->do_log(s);
67 }
68 else if (conn && !conn_get_src(conn)) {
69 send_log(fe, LOG_INFO, "Connect from unknown source to listener %d (%s/%s)\n",
70 l->luid, fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
71 }
72 else if (conn) {
73 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
74 int port;
75
76 switch (addr_to_str(conn->src, pn, sizeof(pn))) {
77 case AF_INET:
78 case AF_INET6:
79 if (conn_get_dst(conn)) {
80 addr_to_str(conn->dst, sn, sizeof(sn));
81 port = get_host_port(conn->dst);
82 } else {
83 strcpy(sn, "undetermined address");
84 port = 0;
85 }
86 send_log(fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
87 pn, get_host_port(conn->src),
88 sn, port,
89 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
90 break;
91 case AF_UNIX:
92 /* UNIX socket, only the destination is known */
93 send_log(fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
94 l->luid,
95 fe->id, (fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
96 break;
97 }
98 }
99 }
100
101 if (unlikely((global.mode & MODE_DEBUG) && conn &&
102 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
103 char pn[INET6_ADDRSTRLEN];
104 char alpn[16] = "<none>";
105 const char *alpn_str = NULL;
106 int alpn_len;
107
108 /* try to report the ALPN value when available (also works for NPN) */
109 if (conn == cs_conn(objt_cs(s->si[0].end))) {
110 if (conn_get_alpn(conn, &alpn_str, &alpn_len) && alpn_str) {
111 int len = MIN(alpn_len, sizeof(alpn) - 1);
112 memcpy(alpn, alpn_str, len);
113 alpn[len] = 0;
114 }
115 }
116
117 if (!conn_get_src(conn)) {
118 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [listener:%d] ALPN=%s\n",
119 s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->handle.fd,
120 l->luid, alpn);
121 }
122 else switch (addr_to_str(conn->src, pn, sizeof(pn))) {
123 case AF_INET:
124 case AF_INET6:
125 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [%s:%d] ALPN=%s\n",
126 s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->handle.fd,
127 pn, get_host_port(conn->src), alpn);
128 break;
129 case AF_UNIX:
130 /* UNIX socket, only the destination is known */
131 chunk_printf(&trash, "%08x:%s.accept(%04x)=%04x from [unix:%d] ALPN=%s\n",
132 s->uniq_id, fe->id, (unsigned short)l->fd, (unsigned short)conn->handle.fd,
133 l->luid, alpn);
134 break;
135 }
136
137 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
138 }
139
140 if (fe->mode == PR_MODE_HTTP)
141 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
142
143 if (unlikely(fe->nb_req_cap > 0)) {
144 if ((s->req_cap = pool_alloc(fe->req_cap_pool)) == NULL)
145 goto out_return; /* no memory */
146 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
147 }
148
149 if (unlikely(fe->nb_rsp_cap > 0)) {
150 if ((s->res_cap = pool_alloc(fe->rsp_cap_pool)) == NULL)
151 goto out_free_reqcap; /* no memory */
152 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
153 }
154
155 if (fe->http_needed) {
156 /* we have to allocate header indexes only if we know
157 * that we may make use of them. This of course includes
158 * (mode == PR_MODE_HTTP).
159 */
160 if (unlikely(!http_alloc_txn(s)))
161 goto out_free_rspcap; /* no memory */
162
163 /* and now initialize the HTTP transaction state */
164 http_init_txn(s);
165 }
166
167 /* everything's OK, let's go on */
168 return 1;
169
170 /* Error unrolling */
171 out_free_rspcap:
172 pool_free(fe->rsp_cap_pool, s->res_cap);
173 out_free_reqcap:
174 pool_free(fe->req_cap_pool, s->req_cap);
175 out_return:
176 return -1;
177 }
178
179 /************************************************************************/
180 /* All supported sample and ACL keywords must be declared here. */
181 /************************************************************************/
182
183 /* set temp integer to the id of the frontend */
184 static int
smp_fetch_fe_id(const struct arg * args,struct sample * smp,const char * kw,void * private)185 smp_fetch_fe_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
186 {
187 smp->flags = SMP_F_VOL_SESS;
188 smp->data.type = SMP_T_SINT;
189 smp->data.u.sint = smp->sess->fe->uuid;
190 return 1;
191 }
192
193 /* set string to the name of the frontend */
194 static int
smp_fetch_fe_name(const struct arg * args,struct sample * smp,const char * kw,void * private)195 smp_fetch_fe_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
196 {
197 smp->data.u.str.area = (char *)smp->sess->fe->id;
198 if (!smp->data.u.str.area)
199 return 0;
200
201 smp->data.type = SMP_T_STR;
202 smp->flags = SMP_F_CONST;
203 smp->data.u.str.data = strlen(smp->data.u.str.area);
204 return 1;
205 }
206
207 /* set string to the name of the default backend */
208 static int
smp_fetch_fe_defbe(const struct arg * args,struct sample * smp,const char * kw,void * private)209 smp_fetch_fe_defbe(const struct arg *args, struct sample *smp, const char *kw, void *private)
210 {
211 if (!smp->sess->fe->defbe.be)
212 return 0;
213 smp->data.u.str.area = (char *)smp->sess->fe->defbe.be->id;
214 if (!smp->data.u.str.area)
215 return 0;
216
217 smp->data.type = SMP_T_STR;
218 smp->flags = SMP_F_CONST;
219 smp->data.u.str.data = strlen(smp->data.u.str.area);
220 return 1;
221 }
222
223 /* set temp integer to the number of HTTP requests per second reaching the frontend.
224 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
225 * an undefined behaviour.
226 */
227 static int
smp_fetch_fe_req_rate(const struct arg * args,struct sample * smp,const char * kw,void * private)228 smp_fetch_fe_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
229 {
230 smp->flags = SMP_F_VOL_TEST;
231 smp->data.type = SMP_T_SINT;
232 smp->data.u.sint = read_freq_ctr(&args->data.prx->fe_req_per_sec);
233 return 1;
234 }
235
236 /* set temp integer to the number of connections per second reaching the frontend.
237 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
238 * an undefined behaviour.
239 */
240 static int
smp_fetch_fe_sess_rate(const struct arg * args,struct sample * smp,const char * kw,void * private)241 smp_fetch_fe_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
242 {
243 smp->flags = SMP_F_VOL_TEST;
244 smp->data.type = SMP_T_SINT;
245 smp->data.u.sint = read_freq_ctr(&args->data.prx->fe_sess_per_sec);
246 return 1;
247 }
248
249 /* set temp integer to the number of concurrent connections on the frontend
250 * Accepts exactly 1 argument. Argument is a frontend, other types will cause
251 * an undefined behaviour.
252 */
253 static int
smp_fetch_fe_conn(const struct arg * args,struct sample * smp,const char * kw,void * private)254 smp_fetch_fe_conn(const struct arg *args, struct sample *smp, const char *kw, void *private)
255 {
256 smp->flags = SMP_F_VOL_TEST;
257 smp->data.type = SMP_T_SINT;
258 smp->data.u.sint = args->data.prx->feconn;
259 return 1;
260 }
261
262
263 /* Note: must not be declared <const> as its list will be overwritten.
264 * Please take care of keeping this list alphabetically sorted.
265 */
266 static struct sample_fetch_kw_list smp_kws = {ILH, {
267 { "fe_conn", smp_fetch_fe_conn, ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
268 { "fe_defbe", smp_fetch_fe_defbe, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
269 { "fe_id", smp_fetch_fe_id, 0, NULL, SMP_T_SINT, SMP_USE_FTEND, },
270 { "fe_name", smp_fetch_fe_name, 0, NULL, SMP_T_STR, SMP_USE_FTEND, },
271 { "fe_req_rate", smp_fetch_fe_req_rate, ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
272 { "fe_sess_rate", smp_fetch_fe_sess_rate, ARG1(1,FE), NULL, SMP_T_SINT, SMP_USE_INTRN, },
273 { /* END */ },
274 }};
275
276 INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
277
278 /* Note: must not be declared <const> as its list will be overwritten.
279 * Please take care of keeping this list alphabetically sorted.
280 */
281 static struct acl_kw_list acl_kws = {ILH, {
282 { /* END */ },
283 }};
284
285 INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
286
287 /*
288 * Local variables:
289 * c-indent-level: 8
290 * c-basic-offset: 8
291 * End:
292 */
293