diff --git a/include/haproxy/connection.h b/include/haproxy/connection.h index 5914796d8..84c6645a7 100644 --- a/include/haproxy/connection.h +++ b/include/haproxy/connection.h @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include #include @@ -86,6 +86,12 @@ void conn_delete_from_tree(struct ebmb_node *node); void conn_init(struct connection *conn, void *target); struct connection *conn_new(void *target); void conn_free(struct connection *conn); +struct conn_hash_node *conn_alloc_hash_node(struct connection *conn); +struct sockaddr_storage *sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len); +void sockaddr_free(struct sockaddr_storage **sap); +void cs_free(struct conn_stream *cs); +struct conn_stream *cs_new(struct connection *conn, void *target); + /* connection hash stuff */ uint64_t conn_calculate_hash(const struct conn_hash_params *params); @@ -332,19 +338,6 @@ static inline int conn_is_back(const struct connection *conn) return !objt_listener(conn->target); } -static inline struct conn_hash_node *conn_alloc_hash_node(struct connection *conn) -{ - struct conn_hash_node *hash_node = NULL; - - hash_node = pool_zalloc(pool_head_conn_hash_node); - if (unlikely(!hash_node)) - return NULL; - - hash_node->conn = conn; - - return hash_node; -} - /* sets as the connection's owner */ static inline void conn_set_owner(struct connection *conn, void *owner, void (*cb)(struct connection *)) { @@ -364,77 +357,6 @@ static inline void conn_set_private(struct connection *conn) } } -/* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and - * returns it. If is NULL, the address is always allocated and returned. - * if is non-null, an address will only be allocated if it points to a - * non-null pointer. In this case the allocated address will be assigned there. - * If is non-null and positive, the address in will be copied - * into the allocated address. In both situations the new pointer is returned. - */ -static inline struct sockaddr_storage * -sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len) -{ - struct sockaddr_storage *sa; - - if (sap && *sap) - return *sap; - - sa = pool_alloc(pool_head_sockaddr); - if (sa && orig && len > 0) - memcpy(sa, orig, len); - if (sap) - *sap = sa; - return sa; -} - -/* Releases the struct sockaddr potentially pointed to by to the pool. It - * may be NULL or may point to NULL. If is not NULL, a NULL is placed - * there. - */ -static inline void sockaddr_free(struct sockaddr_storage **sap) -{ - if (!sap) - return; - pool_free(pool_head_sockaddr, *sap); - *sap = NULL; -} - -/* Releases a conn_stream previously allocated by cs_new(), as well as any - * buffer it would still hold. - */ -static inline void cs_free(struct conn_stream *cs) -{ - - pool_free(pool_head_connstream, cs); -} - -/* Tries to allocate a new conn_stream and initialize its main fields. If - * is NULL, then a new connection is allocated on the fly, initialized, - * and assigned to cs->conn ; this connection will then have to be released - * using pool_free() or conn_free(). The conn_stream is initialized and added - * to the mux's stream list on success, then returned. On failure, nothing is - * allocated and NULL is returned. - */ -static inline struct conn_stream *cs_new(struct connection *conn, void *target) -{ - struct conn_stream *cs; - - cs = pool_alloc(pool_head_connstream); - if (unlikely(!cs)) - return NULL; - - if (!conn) { - conn = conn_new(target); - if (unlikely(!conn)) { - cs_free(cs); - return NULL; - } - } - - cs_init(cs, conn); - return cs; -} - /* Retrieves any valid conn_stream from this connection, preferably the first * valid one. The purpose is to be able to figure one other end of a private * connection for purposes like source binding or proxy protocol header diff --git a/src/connection.c b/src/connection.c index 32950e04d..f8b704b67 100644 --- a/src/connection.c +++ b/src/connection.c @@ -437,6 +437,89 @@ void conn_free(struct connection *conn) pool_free(pool_head_connection, conn); } +struct conn_hash_node *conn_alloc_hash_node(struct connection *conn) +{ + struct conn_hash_node *hash_node = NULL; + + hash_node = pool_zalloc(pool_head_conn_hash_node); + if (unlikely(!hash_node)) + return NULL; + + hash_node->conn = conn; + + return hash_node; +} + +/* Allocates a struct sockaddr from the pool if needed, assigns it to *sap and + * returns it. If is NULL, the address is always allocated and returned. + * if is non-null, an address will only be allocated if it points to a + * non-null pointer. In this case the allocated address will be assigned there. + * If is non-null and positive, the address in will be copied + * into the allocated address. In both situations the new pointer is returned. + */ +struct sockaddr_storage *sockaddr_alloc(struct sockaddr_storage **sap, const struct sockaddr_storage *orig, socklen_t len) +{ + struct sockaddr_storage *sa; + + if (sap && *sap) + return *sap; + + sa = pool_alloc(pool_head_sockaddr); + if (sa && orig && len > 0) + memcpy(sa, orig, len); + if (sap) + *sap = sa; + return sa; +} + +/* Releases the struct sockaddr potentially pointed to by to the pool. It + * may be NULL or may point to NULL. If is not NULL, a NULL is placed + * there. + */ +void sockaddr_free(struct sockaddr_storage **sap) +{ + if (!sap) + return; + pool_free(pool_head_sockaddr, *sap); + *sap = NULL; +} + +/* Releases a conn_stream previously allocated by cs_new(), as well as any + * buffer it would still hold. + */ +void cs_free(struct conn_stream *cs) +{ + + pool_free(pool_head_connstream, cs); +} + +/* Tries to allocate a new conn_stream and initialize its main fields. If + * is NULL, then a new connection is allocated on the fly, initialized, + * and assigned to cs->conn ; this connection will then have to be released + * using pool_free() or conn_free(). The conn_stream is initialized and added + * to the mux's stream list on success, then returned. On failure, nothing is + * allocated and NULL is returned. + */ +struct conn_stream *cs_new(struct connection *conn, void *target) +{ + struct conn_stream *cs; + + cs = pool_alloc(pool_head_connstream); + if (unlikely(!cs)) + return NULL; + + if (!conn) { + conn = conn_new(target); + if (unlikely(!conn)) { + cs_free(cs); + return NULL; + } + } + + cs_init(cs, conn); + return cs; +} + /* Try to add a handshake pseudo-XPRT. If the connection's first XPRT is * raw_sock, then just use the new XPRT as the connection XPRT, otherwise * call the xprt's add_xprt() method.