From 15678efc45df458db0bf7a27dee5d4db4825ced3 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 31 Aug 2012 13:54:11 +0200 Subject: [PATCH] MEDIUM: connection: add an ->init function to data layer SSL need to initialize the data layer before proceeding with data. At the moment, this data layer is automatically initialized from itself, which will not be possible once we extract connection from sessions since we'll only create the data layer once the handshake is finished. So let's have the application layer initialize the data layer before using it. --- include/proto/connection.h | 10 ++++++++++ include/types/connection.h | 1 + src/proto_tcp.c | 4 ++++ src/session.c | 2 ++ 4 files changed, 17 insertions(+) diff --git a/include/proto/connection.h b/include/proto/connection.h index 7c8e2cb0d..007f9e263 100644 --- a/include/proto/connection.h +++ b/include/proto/connection.h @@ -30,6 +30,16 @@ */ int conn_fd_handler(int fd); +/* calls the init() function of the data layer if any. Returns <0 in case of + * error. + */ +static inline int conn_data_init(struct connection *conn) +{ + if (conn->data && conn->data->init) + return conn->data->init(conn); + return 0; +} + /* Calls the close() function of the data layer if any */ static inline void conn_data_close(struct connection *conn) { diff --git a/include/types/connection.h b/include/types/connection.h index 2fbd77930..0e12bb5d1 100644 --- a/include/types/connection.h +++ b/include/types/connection.h @@ -143,6 +143,7 @@ struct data_ops { void (*shutr)(struct connection *, int); /* shutr function */ void (*shutw)(struct connection *, int); /* shutw function */ void (*close)(struct connection *); /* close the data channel on the connection */ + int (*init)(struct connection *conn); /* initialize the data layer */ }; /* app_cb describes read and write callbacks which are called upon detected I/O diff --git a/src/proto_tcp.c b/src/proto_tcp.c index af9dbf95b..99741dc2d 100644 --- a/src/proto_tcp.c +++ b/src/proto_tcp.c @@ -458,6 +458,10 @@ int tcp_connect_server(struct connection *conn, int data) fdtab[fd].iocb = conn_fd_handler; fd_insert(fd); conn_sock_want_send(conn); /* for connect status */ + + if (conn_data_init(conn) < 0) + return SN_ERR_RESOURCE; + if (data) conn_data_want_send(conn); /* prepare to send data if any */ diff --git a/src/session.c b/src/session.c index db13fc632..6b784cfe3 100644 --- a/src/session.c +++ b/src/session.c @@ -278,6 +278,8 @@ int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr) fdtab[cfd].flags = 0; fdtab[cfd].iocb = conn_fd_handler; conn_data_want_recv(&s->si[0].conn); + if (conn_data_init(&s->si[0].conn) < 0) + goto out_free_rep; if (p->accept && (ret = p->accept(s)) <= 0) { /* Either we had an unrecoverable error (<0) or work is