2 * include/haproxy/connection.h
3 * This file contains connection function prototypes
5 * Copyright (C) 2000-2002 Willy Tarreau - w@1wt.eu
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef _HAPROXY_CONNECTION_H
23 #define _HAPROXY_CONNECTION_H
25 #include <import/ist.h>
27 #include <haproxy/api.h>
28 #include <haproxy/connection-t.h>
29 #include <haproxy/fd.h>
30 #include <haproxy/http_ana.h>
31 #include <haproxy/listener-t.h>
32 #include <haproxy/obj_type.h>
33 #include <haproxy/pool.h>
34 #include <haproxy/session.h>
35 #include <haproxy/task-t.h>
36 #include <haproxy/tcpcheck-t.h>
39 extern struct pool_head *pool_head_connection;
40 extern struct pool_head *pool_head_connstream;
41 extern struct pool_head *pool_head_sockaddr;
42 extern struct pool_head *pool_head_authority;
43 extern struct xprt_ops *registered_xprt[XPRT_ENTRIES];
44 extern struct mux_proto_list mux_proto_list;
46 #define IS_HTX_CONN(conn) ((conn)->mux && ((conn)->mux->flags & MX_FL_HTX))
47 #define IS_HTX_CS(cs) (IS_HTX_CONN((cs)->conn))
49 /* I/O callback for fd-based connections. It calls the read/write handlers
50 * provided by the connection's sock_ops.
52 void conn_fd_handler(int fd);
53 int conn_fd_check(struct connection *conn);
55 /* receive a PROXY protocol header over a connection */
56 int conn_recv_proxy(struct connection *conn, int flag);
57 int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm);
58 int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst);
59 int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm);
61 int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es);
62 int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es);
64 /* receive a NetScaler Client IP insertion header over a connection */
65 int conn_recv_netscaler_cip(struct connection *conn, int flag);
67 /* raw send() directly on the socket */
68 int conn_sock_send(struct connection *conn, const void *buf, int len, int flags);
70 /* drains any pending bytes from the socket */
71 int conn_sock_drain(struct connection *conn);
73 /* scoks4 proxy handshake */
74 int conn_send_socks4_proxy_request(struct connection *conn);
75 int conn_recv_socks4_proxy_response(struct connection *conn);
77 /* If we delayed the mux creation because we were waiting for the handshake, do it now */
78 int conn_create_mux(struct connection *conn);
80 extern struct idle_conns idle_conns[MAX_THREADS];
82 /* returns true is the transport layer is ready */
83 static inline int conn_xprt_ready(const struct connection *conn)
85 return (conn->flags & CO_FL_XPRT_READY);
88 /* returns true is the control layer is ready */
89 static inline int conn_ctrl_ready(const struct connection *conn)
91 return (conn->flags & CO_FL_CTRL_READY);
94 /* Calls the init() function of the transport layer if any and if not done yet,
95 * and sets the CO_FL_XPRT_READY flag to indicate it was properly initialized.
96 * Returns <0 in case of error.
98 static inline int conn_xprt_init(struct connection *conn)
102 if (!conn_xprt_ready(conn) && conn->xprt && conn->xprt->init)
103 ret = conn->xprt->init(conn, &conn->xprt_ctx);
106 conn->flags |= CO_FL_XPRT_READY;
111 /* Calls the close() function of the transport layer if any and if not done
112 * yet, and clears the CO_FL_XPRT_READY flag. However this is not done if the
113 * CO_FL_XPRT_TRACKED flag is set, which allows logs to take data from the
114 * transport layer very late if needed.
116 static inline void conn_xprt_close(struct connection *conn)
118 if ((conn->flags & (CO_FL_XPRT_READY|CO_FL_XPRT_TRACKED)) == CO_FL_XPRT_READY) {
119 if (conn->xprt->close)
120 conn->xprt->close(conn, conn->xprt_ctx);
121 conn->xprt_ctx = NULL;
122 conn->flags &= ~CO_FL_XPRT_READY;
126 /* Initializes the connection's control layer which essentially consists in
127 * registering the file descriptor for polling and setting the CO_FL_CTRL_READY
128 * flag. The caller is responsible for ensuring that the control layer is
129 * already assigned to the connection prior to the call.
131 static inline void conn_ctrl_init(struct connection *conn)
133 if (!conn_ctrl_ready(conn)) {
134 int fd = conn->handle.fd;
136 fd_insert(fd, conn, conn_fd_handler, tid_bit);
137 conn->flags |= CO_FL_CTRL_READY;
141 /* Deletes the FD if the transport layer is already gone. Once done,
142 * it then removes the CO_FL_CTRL_READY flag.
144 static inline void conn_ctrl_close(struct connection *conn)
146 if ((conn->flags & (CO_FL_XPRT_READY|CO_FL_CTRL_READY)) == CO_FL_CTRL_READY) {
147 fd_delete(conn->handle.fd);
148 conn->handle.fd = DEAD_FD_MAGIC;
149 conn->flags &= ~CO_FL_CTRL_READY;
153 /* If the connection still has a transport layer, then call its close() function
154 * if any, and delete the file descriptor if a control layer is set. This is
155 * used to close everything at once and atomically. However this is not done if
156 * the CO_FL_XPRT_TRACKED flag is set, which allows logs to take data from the
157 * transport layer very late if needed.
159 static inline void conn_full_close(struct connection *conn)
161 conn_xprt_close(conn);
162 conn_ctrl_close(conn);
165 /* stop tracking a connection, allowing conn_full_close() to always
168 static inline void conn_stop_tracking(struct connection *conn)
170 conn->flags &= ~CO_FL_XPRT_TRACKED;
173 /* Stop all polling on the fd. This might be used when an error is encountered
176 static inline void conn_stop_polling(struct connection *c)
178 if (conn_ctrl_ready(c))
179 fd_stop_both(c->handle.fd);
182 /* Stops polling in case of error on the connection. */
183 static inline void conn_cond_update_polling(struct connection *c)
185 if (unlikely(c->flags & CO_FL_ERROR))
186 conn_stop_polling(c);
189 /* read shutdown, called from the rcv_buf/rcv_pipe handlers when
190 * detecting an end of connection.
192 static inline void conn_sock_read0(struct connection *c)
194 c->flags |= CO_FL_SOCK_RD_SH;
195 if (conn_ctrl_ready(c)) {
196 fd_stop_recv(c->handle.fd);
197 /* we don't risk keeping ports unusable if we found the
198 * zero from the other side.
200 fdtab[c->handle.fd].linger_risk = 0;
204 /* write shutdown, indication that the upper layer is not willing to send
205 * anything anymore and wants to close after pending data are sent. The
206 * <clean> argument will allow not to perform the socket layer shutdown if
209 static inline void conn_sock_shutw(struct connection *c, int clean)
211 c->flags |= CO_FL_SOCK_WR_SH;
212 if (conn_ctrl_ready(c)) {
213 fd_stop_send(c->handle.fd);
214 /* don't perform a clean shutdown if we're going to reset or
215 * if the shutr was already received.
217 if (!(c->flags & CO_FL_SOCK_RD_SH) && clean)
218 shutdown(c->handle.fd, SHUT_WR);
222 static inline void conn_xprt_shutw(struct connection *c)
224 if (conn_ctrl_ready(c))
225 fd_stop_send(c->handle.fd);
227 /* clean data-layer shutdown */
228 if (c->xprt && c->xprt->shutw)
229 c->xprt->shutw(c, c->xprt_ctx, 1);
232 static inline void conn_xprt_shutw_hard(struct connection *c)
234 if (conn_ctrl_ready(c))
235 fd_stop_send(c->handle.fd);
237 /* unclean data-layer shutdown */
238 if (c->xprt && c->xprt->shutw)
239 c->xprt->shutw(c, c->xprt_ctx, 0);
243 static inline void cs_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
246 /* clean data-layer shutdown */
247 if (cs->conn->mux && cs->conn->mux->shutr)
248 cs->conn->mux->shutr(cs, mode);
249 cs->flags |= (mode == CS_SHR_DRAIN) ? CS_FL_SHRD : CS_FL_SHRR;
253 static inline void cs_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
256 /* clean data-layer shutdown */
257 if (cs->conn->mux && cs->conn->mux->shutw)
258 cs->conn->mux->shutw(cs, mode);
259 cs->flags |= (mode == CS_SHW_NORMAL) ? CS_FL_SHWN : CS_FL_SHWS;
262 /* completely close a conn_stream (but do not detach it) */
263 static inline void cs_close(struct conn_stream *cs)
265 cs_shutw(cs, CS_SHW_SILENT);
266 cs_shutr(cs, CS_SHR_RESET);
267 cs->flags = CS_FL_NONE;
270 /* sets CS_FL_ERROR or CS_FL_ERR_PENDING on the cs */
271 static inline void cs_set_error(struct conn_stream *cs)
273 if (cs->flags & CS_FL_EOS)
274 cs->flags |= CS_FL_ERROR;
276 cs->flags |= CS_FL_ERR_PENDING;
279 /* detect sock->data read0 transition */
280 static inline int conn_xprt_read0_pending(struct connection *c)
282 return (c->flags & CO_FL_SOCK_RD_SH) != 0;
285 /* prepares a connection to work with protocol <proto> and transport <xprt>.
286 * The transport's is initialized as well, and the mux and its context are
287 * cleared. The target is not reinitialized and it is recommended that it is
288 * set prior to calling this function so that the function may make use of it
289 * in the future to refine the mux choice if needed.
291 static inline void conn_prepare(struct connection *conn, const struct protocol *proto, const struct xprt_ops *xprt)
296 conn->xprt_ctx = NULL;
301 * Initializes all required fields for a new conn_strema.
303 static inline void cs_init(struct conn_stream *cs, struct connection *conn)
305 cs->obj_type = OBJ_TYPE_CS;
306 cs->flags = CS_FL_NONE;
310 /* Initializes all required fields for a new connection. Note that it does the
311 * minimum acceptable initialization for a connection that already exists and
312 * is about to be reused. It also leaves the addresses untouched, which makes
313 * it usable across connection retries to reset a connection to a known state.
315 static inline void conn_init(struct connection *conn)
317 conn->obj_type = OBJ_TYPE_CONN;
318 conn->flags = CO_FL_NONE;
322 conn->send_proxy_ofs = 0;
323 conn->handle.fd = DEAD_FD_MAGIC;
324 conn->err_code = CO_ER_NONE;
326 conn->destroy_cb = NULL;
327 conn->proxy_netns = NULL;
328 MT_LIST_INIT(&conn->list);
329 LIST_INIT(&conn->session_list);
334 conn->proxy_authority = NULL;
335 conn->proxy_unique_id = IST_NULL;
338 /* sets <owner> as the connection's owner */
339 static inline void conn_set_owner(struct connection *conn, void *owner, void (*cb)(struct connection *))
342 conn->destroy_cb = cb;
345 /* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and
346 * returns it. If <sap> is NULL, the address is always allocated and returned.
347 * if <sap> is non-null, an address will only be allocated if it points to a
348 * non-null pointer. In this case the allocated address will be assigned there.
349 * In both situations the new pointer is returned.
351 static inline struct sockaddr_storage *sockaddr_alloc(struct sockaddr_storage **sap)
353 struct sockaddr_storage *sa;
358 sa = pool_alloc(pool_head_sockaddr);
364 /* Releases the struct sockaddr potentially pointed to by <sap> to the pool. It
365 * may be NULL or may point to NULL. If <sap> is not NULL, a NULL is placed
368 static inline void sockaddr_free(struct sockaddr_storage **sap)
372 pool_free(pool_head_sockaddr, *sap);
376 /* Tries to allocate a new connection and initialized its main fields. The
377 * connection is returned on success, NULL on failure. The connection must
378 * be released using pool_free() or conn_free().
380 static inline struct connection *conn_new()
382 struct connection *conn;
384 conn = pool_alloc(pool_head_connection);
385 if (likely(conn != NULL))
390 /* Releases a conn_stream previously allocated by cs_new(), as well as any
391 * buffer it would still hold.
393 static inline void cs_free(struct conn_stream *cs)
396 pool_free(pool_head_connstream, cs);
399 /* Tries to allocate a new conn_stream and initialize its main fields. If
400 * <conn> is NULL, then a new connection is allocated on the fly, initialized,
401 * and assigned to cs->conn ; this connection will then have to be released
402 * using pool_free() or conn_free(). The conn_stream is initialized and added
403 * to the mux's stream list on success, then returned. On failure, nothing is
404 * allocated and NULL is returned.
406 static inline struct conn_stream *cs_new(struct connection *conn)
408 struct conn_stream *cs;
410 cs = pool_alloc(pool_head_connstream);
416 if (unlikely(!conn)) {
427 /* Retrieves any valid conn_stream from this connection, preferably the first
428 * valid one. The purpose is to be able to figure one other end of a private
429 * connection for purposes like source binding or proxy protocol header
430 * emission. In such cases, any conn_stream is expected to be valid so the
431 * mux is encouraged to return the first one it finds. If the connection has
432 * no mux or the mux has no get_first_cs() method or the mux has no valid
433 * conn_stream, NULL is returned. The output pointer is purposely marked
434 * const to discourage the caller from modifying anything there.
436 static inline const struct conn_stream *cs_get_first(const struct connection *conn)
438 if (!conn || !conn->mux || !conn->mux->get_first_cs)
440 return conn->mux->get_first_cs(conn);
443 static inline void conn_force_unsubscribe(struct connection *conn)
447 conn->subs->events = 0;
451 /* Releases a connection previously allocated by conn_new() */
452 static inline void conn_free(struct connection *conn)
454 /* Remove ourself from the session's connections list, if any. */
455 if (!LIST_ISEMPTY(&conn->session_list)) {
456 struct session *sess = conn->owner;
457 if (conn->flags & CO_FL_SESS_IDLE)
459 session_unown_conn(sess, conn);
462 sockaddr_free(&conn->src);
463 sockaddr_free(&conn->dst);
465 if (conn->proxy_authority != NULL) {
466 pool_free(pool_head_authority, conn->proxy_authority);
467 conn->proxy_authority = NULL;
469 if (isttest(conn->proxy_unique_id)) {
470 pool_free(pool_head_uniqueid, conn->proxy_unique_id.ptr);
471 conn->proxy_unique_id = IST_NULL;
474 /* By convention we always place a NULL where the ctx points to if the
475 * mux is null. It may have been used to store the connection as a
476 * stream_interface's end point for example.
478 if (conn->ctx != NULL && conn->mux == NULL)
479 *(void **)conn->ctx = NULL;
481 /* The connection is currently in the server's idle list, so tell it
482 * there's one less connection available in that list.
484 if (conn->idle_time > 0) {
485 struct server *srv = __objt_server(conn->target);
486 _HA_ATOMIC_SUB(&srv->curr_idle_conns, 1);
487 _HA_ATOMIC_SUB(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb, 1);
488 _HA_ATOMIC_SUB(&srv->curr_idle_thr[tid], 1);
490 struct server *srv = objt_server(conn->target);
493 _HA_ATOMIC_SUB(&srv->curr_used_conns, 1);
496 conn_force_unsubscribe(conn);
497 MT_LIST_DEL((struct mt_list *)&conn->list);
498 pool_free(pool_head_connection, conn);
501 /* Release a conn_stream */
502 static inline void cs_destroy(struct conn_stream *cs)
505 cs->conn->mux->detach(cs);
507 /* It's too early to have a mux, let's just destroy
510 struct connection *conn = cs->conn;
512 conn_stop_tracking(conn);
513 conn_full_close(conn);
514 if (conn->destroy_cb)
515 conn->destroy_cb(conn);
521 /* Returns the conn from a cs. If cs is NULL, returns NULL */
522 static inline struct connection *cs_conn(const struct conn_stream *cs)
524 return cs ? cs->conn : NULL;
527 /* Retrieves the connection's original source address. Returns non-zero on
528 * success or zero on failure. The operation is only performed once and the
529 * address is stored in the connection for future use.
531 static inline int conn_get_src(struct connection *conn)
533 if (conn->flags & CO_FL_ADDR_FROM_SET)
536 if (!conn_ctrl_ready(conn) || !conn->ctrl->get_src)
539 if (!sockaddr_alloc(&conn->src))
542 if (conn->ctrl->get_src(conn->handle.fd, (struct sockaddr *)conn->src,
544 obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
546 conn->flags |= CO_FL_ADDR_FROM_SET;
550 /* Retrieves the connection's original destination address. Returns non-zero on
551 * success or zero on failure. The operation is only performed once and the
552 * address is stored in the connection for future use.
554 static inline int conn_get_dst(struct connection *conn)
556 if (conn->flags & CO_FL_ADDR_TO_SET)
559 if (!conn_ctrl_ready(conn) || !conn->ctrl->get_dst)
562 if (!sockaddr_alloc(&conn->dst))
565 if (conn->ctrl->get_dst(conn->handle.fd, (struct sockaddr *)conn->dst,
567 obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
569 conn->flags |= CO_FL_ADDR_TO_SET;
573 /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
574 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). The connection is tested and if
575 * it is null, nothing is done.
577 static inline void conn_set_tos(const struct connection *conn, int tos)
579 if (!conn || !conn_ctrl_ready(conn))
583 if (conn->src->ss_family == AF_INET)
584 setsockopt(conn->handle.fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
587 if (conn->src->ss_family == AF_INET6) {
588 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)conn->src)->sin6_addr))
589 /* v4-mapped addresses need IP_TOS */
590 setsockopt(conn->handle.fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
592 setsockopt(conn->handle.fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
597 /* Sets the netfilter mark on the connection's socket. The connection is tested
598 * and if it is null, nothing is done.
600 static inline void conn_set_mark(const struct connection *conn, int mark)
602 if (!conn || !conn_ctrl_ready(conn))
606 setsockopt(conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
610 /* Sets adjust the TCP quick-ack feature on the connection's socket. The
611 * connection is tested and if it is null, nothing is done.
613 static inline void conn_set_quickack(const struct connection *conn, int value)
615 if (!conn || !conn_ctrl_ready(conn))
619 setsockopt(conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &value, sizeof(value));
623 /* Attaches a conn_stream to a data layer and sets the relevant callbacks */
624 static inline void cs_attach(struct conn_stream *cs, void *data, const struct data_cb *data_cb)
626 cs->data_cb = data_cb;
630 static inline struct wait_event *wl_set_waitcb(struct wait_event *wl, struct task *(*cb)(struct task *, void *, unsigned short), void *ctx)
632 if (!wl->tasklet->process) {
633 wl->tasklet->process = cb;
634 wl->tasklet->context = ctx;
639 /* Installs the connection's mux layer for upper context <ctx>.
640 * Returns < 0 on error.
642 static inline int conn_install_mux(struct connection *conn, const struct mux_ops *mux,
643 void *ctx, struct proxy *prx, struct session *sess)
649 ret = mux->init ? mux->init(conn, prx, sess, &BUF_NULL) : 0;
657 /* returns a human-readable error code for conn->err_code, or NULL if the code
660 static inline const char *conn_err_code_str(struct connection *c)
662 switch (c->err_code) {
663 case CO_ER_NONE: return "Success";
665 case CO_ER_CONF_FDLIM: return "Reached configured maxconn value";
666 case CO_ER_PROC_FDLIM: return "Too many sockets on the process";
667 case CO_ER_SYS_FDLIM: return "Too many sockets on the system";
668 case CO_ER_SYS_MEMLIM: return "Out of system buffers";
669 case CO_ER_NOPROTO: return "Protocol or address family not supported";
670 case CO_ER_SOCK_ERR: return "General socket error";
671 case CO_ER_PORT_RANGE: return "Source port range exhausted";
672 case CO_ER_CANT_BIND: return "Can't bind to source address";
673 case CO_ER_FREE_PORTS: return "Out of local source ports on the system";
674 case CO_ER_ADDR_INUSE: return "Local source address already in use";
676 case CO_ER_PRX_EMPTY: return "Connection closed while waiting for PROXY protocol header";
677 case CO_ER_PRX_ABORT: return "Connection error while waiting for PROXY protocol header";
678 case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header";
679 case CO_ER_PRX_TRUNCATED: return "Truncated PROXY protocol header received";
680 case CO_ER_PRX_NOT_HDR: return "Received something which does not look like a PROXY protocol header";
681 case CO_ER_PRX_BAD_HDR: return "Received an invalid PROXY protocol header";
682 case CO_ER_PRX_BAD_PROTO: return "Received an unhandled protocol in the PROXY protocol header";
684 case CO_ER_CIP_EMPTY: return "Connection closed while waiting for NetScaler Client IP header";
685 case CO_ER_CIP_ABORT: return "Connection error while waiting for NetScaler Client IP header";
686 case CO_ER_CIP_TRUNCATED: return "Truncated NetScaler Client IP header received";
687 case CO_ER_CIP_BAD_MAGIC: return "Received an invalid NetScaler Client IP magic number";
688 case CO_ER_CIP_BAD_PROTO: return "Received an unhandled protocol in the NetScaler Client IP header";
690 case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake";
691 case CO_ER_SSL_ABORT: return "Connection error during SSL handshake";
692 case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake";
693 case CO_ER_SSL_TOO_MANY: return "Too many SSL connections";
694 case CO_ER_SSL_NO_MEM: return "Out of memory when initializing an SSL connection";
695 case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegotiation attempt";
696 case CO_ER_SSL_CA_FAIL: return "SSL client CA chain cannot be verified";
697 case CO_ER_SSL_CRT_FAIL: return "SSL client certificate not trusted";
698 case CO_ER_SSL_MISMATCH: return "Server presented an SSL certificate different from the configured one";
699 case CO_ER_SSL_MISMATCH_SNI: return "Server presented an SSL certificate different from the expected one";
700 case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure";
701 case CO_ER_SSL_HANDSHAKE_HB: return "SSL handshake failure after heartbeat";
702 case CO_ER_SSL_KILLED_HB: return "Stopped a TLSv1 heartbeat attack (CVE-2014-0160)";
703 case CO_ER_SSL_NO_TARGET: return "Attempt to use SSL on an unknown target (internal error)";
705 case CO_ER_SOCKS4_SEND: return "SOCKS4 Proxy write error during handshake";
706 case CO_ER_SOCKS4_RECV: return "SOCKS4 Proxy read error during handshake";
707 case CO_ER_SOCKS4_DENY: return "SOCKS4 Proxy deny the request";
708 case CO_ER_SOCKS4_ABORT: return "SOCKS4 Proxy handshake aborted by server";
713 static inline const char *conn_get_ctrl_name(const struct connection *conn)
715 if (!conn || !conn_ctrl_ready(conn))
717 return conn->ctrl->name;
720 static inline const char *conn_get_xprt_name(const struct connection *conn)
722 if (!conn || !conn_xprt_ready(conn))
724 return conn->xprt->name;
727 static inline const char *conn_get_mux_name(const struct connection *conn)
729 if (!conn || !conn->mux)
731 return conn->mux->name;
734 static inline const char *cs_get_data_name(const struct conn_stream *cs)
736 if (!cs || !cs->data_cb)
738 return cs->data_cb->name;
741 /* registers pointer to transport layer <id> (XPRT_*) */
742 static inline void xprt_register(int id, struct xprt_ops *xprt)
744 if (id >= XPRT_ENTRIES)
746 registered_xprt[id] = xprt;
749 /* returns pointer to transport layer <id> (XPRT_*) or NULL if not registered */
750 static inline struct xprt_ops *xprt_get(int id)
752 if (id >= XPRT_ENTRIES)
754 return registered_xprt[id];
757 /* Try to add a handshake pseudo-XPRT. If the connection's first XPRT is
758 * raw_sock, then just use the new XPRT as the connection XPRT, otherwise
759 * call the xprt's add_xprt() method.
760 * Returns 0 on success, or non-zero on failure.
762 static inline int xprt_add_hs(struct connection *conn)
764 void *xprt_ctx = NULL;
765 const struct xprt_ops *ops = xprt_get(XPRT_HANDSHAKE);
766 void *nextxprt_ctx = NULL;
767 const struct xprt_ops *nextxprt_ops = NULL;
769 if (conn->flags & CO_FL_ERROR)
771 if (ops->init(conn, &xprt_ctx) < 0)
773 if (conn->xprt == xprt_get(XPRT_RAW)) {
774 nextxprt_ctx = conn->xprt_ctx;
775 nextxprt_ops = conn->xprt;
776 conn->xprt_ctx = xprt_ctx;
779 if (conn->xprt->add_xprt(conn, conn->xprt_ctx, xprt_ctx, ops,
780 &nextxprt_ctx, &nextxprt_ops) != 0) {
781 ops->close(conn, xprt_ctx);
785 if (ops->add_xprt(conn, xprt_ctx, nextxprt_ctx, nextxprt_ops, NULL, NULL) != 0) {
786 ops->close(conn, xprt_ctx);
792 static inline int conn_get_alpn(const struct connection *conn, const char **str, int *len)
794 if (!conn_xprt_ready(conn) || !conn->xprt->get_alpn)
796 return conn->xprt->get_alpn(conn, conn->xprt_ctx, str, len);
799 /* registers proto mux list <list>. Modifies the list element! */
800 static inline void register_mux_proto(struct mux_proto_list *list)
802 LIST_ADDQ(&mux_proto_list.list, &list->list);
805 /* unregisters proto mux list <list> */
806 static inline void unregister_mux_proto(struct mux_proto_list *list)
808 LIST_DEL(&list->list);
809 LIST_INIT(&list->list);
812 static inline struct mux_proto_list *get_mux_proto(const struct ist proto)
814 struct mux_proto_list *item;
816 list_for_each_entry(item, &mux_proto_list.list, list) {
817 if (isteq(proto, item->token))
823 /* Lists the known proto mux on <out> */
824 static inline void list_mux_proto(FILE *out)
826 struct mux_proto_list *item;
830 fprintf(out, "Available multiplexer protocols :\n"
831 "(protocols marked as <default> cannot be specified using 'proto' keyword)\n");
832 list_for_each_entry(item, &mux_proto_list.list, list) {
835 if (item->mode == PROTO_MODE_ANY)
837 else if (item->mode == PROTO_MODE_TCP)
839 else if (item->mode == PROTO_MODE_HTTP)
844 if (item->side == PROTO_SIDE_BOTH)
846 else if (item->side == PROTO_SIDE_FE)
848 else if (item->side == PROTO_SIDE_BE)
853 fprintf(out, " %15s : mode=%-10s side=%-8s mux=%s\n",
854 (proto.len ? proto.ptr : "<default>"), mode, side, item->mux->name);
858 /* returns the first mux entry in the list matching the exact same <mux_proto>
859 * and compatible with the <proto_side> (FE or BE) and the <proto_mode> (TCP or
860 * HTTP). <mux_proto> can be empty. Will fall back to the first compatible mux
861 * with exactly the same <proto_mode> or with an empty name. May return
862 * null if the code improperly registered the default mux to use as a fallback.
864 static inline const struct mux_proto_list *conn_get_best_mux_entry(
865 const struct ist mux_proto,
866 int proto_side, int proto_mode)
868 struct mux_proto_list *item;
869 struct mux_proto_list *fallback = NULL;
871 list_for_each_entry(item, &mux_proto_list.list, list) {
872 if (!(item->side & proto_side) || !(item->mode & proto_mode))
874 if (istlen(mux_proto) && isteq(mux_proto, item->token))
876 else if (!istlen(item->token)) {
877 if (!fallback || (item->mode == proto_mode && fallback->mode != proto_mode))
885 /* returns the first mux in the list matching the exact same <mux_proto> and
886 * compatible with the <proto_side> (FE or BE) and the <proto_mode> (TCP or
887 * HTTP). <mux_proto> can be empty. Will fall back to the first compatible mux
888 * with exactly the same <proto_mode> or with an empty name. May return
889 * null if the code improperly registered the default mux to use as a fallback.
891 static inline const struct mux_ops *conn_get_best_mux(struct connection *conn,
892 const struct ist mux_proto,
893 int proto_side, int proto_mode)
895 const struct mux_proto_list *item;
897 item = conn_get_best_mux_entry(mux_proto, proto_side, proto_mode);
899 return item ? item->mux : NULL;
902 /* returns 0 if the connection is valid and is a frontend connection, otherwise
903 * returns 1 indicating it's a backend connection. And uninitialized connection
904 * also returns 1 to better handle the usage in the middle of initialization.
906 static inline int conn_is_back(const struct connection *conn)
908 return !objt_listener(conn->target);
911 /* returns a pointer to the proxy associated with this connection. For a front
912 * connection it returns a pointer to the frontend ; for a back connection, it
913 * returns a pointer to the backend.
915 static inline struct proxy *conn_get_proxy(const struct connection *conn)
920 /* check if it's a frontend connection */
921 l = objt_listener(conn->target);
923 return l->bind_conf->frontend;
925 /* check if it's a backend connection */
926 s = objt_server(conn->target);
930 return objt_proxy(conn->target);
933 /* installs the best mux for incoming connection <conn> using the upper context
934 * <ctx>. If the mux protocol is forced, we use it to find the best
935 * mux. Otherwise we use the ALPN name, if any. Returns < 0 on error.
937 static inline int conn_install_mux_fe(struct connection *conn, void *ctx)
939 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
940 const struct mux_ops *mux_ops;
942 if (bind_conf->mux_proto)
943 mux_ops = bind_conf->mux_proto->mux;
945 struct ist mux_proto;
946 const char *alpn_str = NULL;
950 if (bind_conf->frontend->mode == PR_MODE_HTTP)
951 mode = PROTO_MODE_HTTP;
953 mode = PROTO_MODE_TCP;
955 conn_get_alpn(conn, &alpn_str, &alpn_len);
956 mux_proto = ist2(alpn_str, alpn_len);
957 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
961 return conn_install_mux(conn, mux_ops, ctx, bind_conf->frontend, conn->owner);
964 /* installs the best mux for outgoing connection <conn> using the upper context
965 * <ctx>. If the mux protocol is forced, we use it to find the best mux. Returns
968 static inline int conn_install_mux_be(struct connection *conn, void *ctx, struct session *sess)
970 struct server *srv = objt_server(conn->target);
971 struct proxy *prx = objt_proxy(conn->target);
972 const struct mux_ops *mux_ops;
977 if (!prx) // target must be either proxy or server
980 if (srv && srv->mux_proto)
981 mux_ops = srv->mux_proto->mux;
983 struct ist mux_proto;
984 const char *alpn_str = NULL;
988 if (prx->mode == PR_MODE_HTTP)
989 mode = PROTO_MODE_HTTP;
991 mode = PROTO_MODE_TCP;
993 conn_get_alpn(conn, &alpn_str, &alpn_len);
994 mux_proto = ist2(alpn_str, alpn_len);
996 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
1000 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
1003 /* installs the best mux for outgoing connection <conn> for a check using the
1004 * upper context <ctx>. If the mux protocol is forced by the check, we use it to
1005 * find the best mux. Returns < 0 on error.
1007 static inline int conn_install_mux_chk(struct connection *conn, void *ctx, struct session *sess)
1009 struct check *check = objt_check(sess->origin);
1010 struct server *srv = objt_server(conn->target);
1011 struct proxy *prx = objt_proxy(conn->target);
1012 const struct mux_ops *mux_ops;
1014 if (!check) // Check must be defined
1020 if (!prx) // target must be either proxy or server
1023 if (check->mux_proto)
1024 mux_ops = check->mux_proto->mux;
1026 struct ist mux_proto;
1027 const char *alpn_str = NULL;
1031 if ((check->tcpcheck_rules->flags & TCPCHK_RULES_PROTO_CHK) == TCPCHK_RULES_HTTP_CHK)
1032 mode = PROTO_MODE_HTTP;
1034 mode = PROTO_MODE_TCP;
1036 conn_get_alpn(conn, &alpn_str, &alpn_len);
1037 mux_proto = ist2(alpn_str, alpn_len);
1039 mux_ops = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_BE, mode);
1043 return conn_install_mux(conn, mux_ops, ctx, prx, sess);
1046 /* Change the mux for the connection.
1047 * The caller should make sure he's not subscribed to the underlying XPRT.
1049 static inline int conn_upgrade_mux_fe(struct connection *conn, void *ctx, struct buffer *buf,
1050 struct ist mux_proto, int mode)
1052 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
1053 const struct mux_ops *old_mux, *new_mux;
1055 const char *alpn_str = NULL;
1058 if (!mux_proto.len) {
1059 conn_get_alpn(conn, &alpn_str, &alpn_len);
1060 mux_proto = ist2(alpn_str, alpn_len);
1062 new_mux = conn_get_best_mux(conn, mux_proto, PROTO_SIDE_FE, mode);
1063 old_mux = conn->mux;
1069 /* Same mux, nothing to do */
1070 if (old_mux == new_mux)
1073 old_mux_ctx = conn->ctx;
1074 conn->mux = new_mux;
1076 if (new_mux->init(conn, bind_conf->frontend, conn->owner, buf) == -1) {
1077 /* The mux upgrade failed, so restore the old mux */
1078 conn->ctx = old_mux_ctx;
1079 conn->mux = old_mux;
1083 /* The mux was upgraded, destroy the old one */
1085 old_mux->destroy(old_mux_ctx);
1089 #endif /* _HAPROXY_CONNECTION_H */