From 873e016772fe8d51e79aa13e9d80beaf33e78ec8 Mon Sep 17 00:00:00 2001 From: Alex Hermann Date: Tue, 9 Jun 2020 09:01:53 +0200 Subject: [PATCH 1/4] core/tcp: Convert RD_CONN_ flags to enum Easier to recognize the correct flags in the forest of flags. --- src/core/tcp_read.c | 14 +++++++------- src/core/tcp_read.h | 14 ++++++++------ src/core/tls_hooks.h | 3 ++- src/modules/tls/tls_server.c | 2 +- src/modules/tls/tls_server.h | 3 ++- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/core/tcp_read.c b/src/core/tcp_read.c index 6a005a9a323..f3eaadedfb9 100644 --- a/src/core/tcp_read.c +++ b/src/core/tcp_read.c @@ -218,7 +218,7 @@ static int tcp_emit_closed_event(struct tcp_connection *con, enum tcp_closed_rea * EOF checking should be done by checking the RD_CONN_EOF flag. */ int tcp_read_data(int fd, struct tcp_connection *c, - char* buf, int b_size, int* flags) + char* buf, int b_size, rd_conn_flags_t* flags) { int bytes_read; @@ -329,7 +329,7 @@ int tcp_read_data(int fd, struct tcp_connection *c, * (to distinguish from reads that would block which could return 0) * RD_CONN_SHORT_READ is also set in *flags for short reads. * sets also r->error */ -int tcp_read(struct tcp_connection *c, int* flags) +int tcp_read(struct tcp_connection *c, rd_conn_flags_t* flags) { int bytes_free, bytes_read; struct tcp_req *r; @@ -369,7 +369,7 @@ int tcp_read(struct tcp_connection *c, int* flags) * when either r->body!=0 or r->state==H_BODY => * all headers have been read. It should be called in a while loop. * returns < 0 if error or 0 if EOF */ -int tcp_read_headers(struct tcp_connection *c, int* read_flags) +int tcp_read_headers(struct tcp_connection *c, rd_conn_flags_t* read_flags) { int bytes, remaining; char *p; @@ -1082,7 +1082,7 @@ int msrp_process_msg(char* tcpbuf, unsigned int len, #endif #ifdef READ_WS -static int tcp_read_ws(struct tcp_connection *c, int* read_flags) +static int tcp_read_ws(struct tcp_connection *c, rd_conn_flags_t* read_flags) { int bytes; uint32_t size, pos, mask_present, len; @@ -1237,7 +1237,7 @@ static int ws_process_msg(char* tcpbuf, unsigned int len, } #endif -static int tcp_read_hep3(struct tcp_connection *c, int* read_flags) +static int tcp_read_hep3(struct tcp_connection *c, rd_conn_flags_t* read_flags) { int bytes; uint32_t size, len; @@ -1429,7 +1429,7 @@ int receive_tcp_msg(char* tcpbuf, unsigned int len, #endif /* TCP_CLONE_RCVBUF */ } -int tcp_read_req(struct tcp_connection* con, int* bytes_read, int* read_flags) +int tcp_read_req(struct tcp_connection* con, int* bytes_read, rd_conn_flags_t* read_flags) { int bytes; int total_bytes; @@ -1712,7 +1712,7 @@ inline static int handle_io(struct fd_map* fm, short events, int idx) { int ret; int n; - int read_flags; + rd_conn_flags_t read_flags; struct tcp_connection* con; int s; long resp; diff --git a/src/core/tcp_read.h b/src/core/tcp_read.h index 23c39eb7309..daf0bea54ed 100644 --- a/src/core/tcp_read.h +++ b/src/core/tcp_read.h @@ -25,14 +25,16 @@ #include "tcp_conn.h" -#define RD_CONN_SHORT_READ 1 -#define RD_CONN_EOF 2 -#define RD_CONN_REPEAT_READ 4 /* read should be repeated (more data) - (used so far only by tls) */ -#define RD_CONN_FORCE_EOF 65536 +typedef enum rd_conn_flags { + RD_CONN_SHORT_READ = (1<<0), + RD_CONN_EOF = (1<<1), + RD_CONN_REPEAT_READ = (1<<2), /* read should be repeated (more data) + (used so far only by tls) */ + RD_CONN_FORCE_EOF = (1<<16), +} rd_conn_flags_t; int tcp_read_data(int fd, struct tcp_connection *c, - char* buf, int b_size, int* flags); + char* buf, int b_size, rd_conn_flags_t* flags); #endif /*__tcp_read_h*/ diff --git a/src/core/tls_hooks.h b/src/core/tls_hooks.h index 528bf244ce9..fcc38d2038b 100644 --- a/src/core/tls_hooks.h +++ b/src/core/tls_hooks.h @@ -37,13 +37,14 @@ #endif #include "tcp_conn.h" +#include "tcp_read.h" struct tls_hooks{ /* read using tls (should use tcp internal read functions to get the data from the connection) */ - int (*read)(struct tcp_connection* c, int* flags); + int (*read)(struct tcp_connection* c, rd_conn_flags_t* flags); /* process data for sending. Should replace pbuf & plen with an internal buffer containing the tls records. If it was not able to process the whole pbuf, it should set (rest_buf, rest_len) to diff --git a/src/modules/tls/tls_server.c b/src/modules/tls/tls_server.c index 5ca11894f7e..2060b7b5502 100644 --- a/src/modules/tls/tls_server.c +++ b/src/modules/tls/tls_server.c @@ -991,7 +991,7 @@ int tls_h_encode_f(struct tcp_connection *c, * tcp connection flags and might set c->state and r->error on * EOF or error). */ -int tls_h_read_f(struct tcp_connection* c, int* flags) +int tls_h_read_f(struct tcp_connection* c, rd_conn_flags_t* flags) { struct tcp_req* r; int bytes_free, bytes_read, read_size, ssl_error, ssl_read; diff --git a/src/modules/tls/tls_server.h b/src/modules/tls/tls_server.h index 400c68160fc..fb86bc277e5 100644 --- a/src/modules/tls/tls_server.h +++ b/src/modules/tls/tls_server.h @@ -30,6 +30,7 @@ #include #include "../../core/tcp_conn.h" +#include "../../core/tcp_read.h" #include "tls_domain.h" #include "tls_ct_wrq.h" @@ -87,7 +88,7 @@ int tls_h_encode_f(struct tcp_connection *c, const char ** pbuf, unsigned int* plen, const char** rest_buf, unsigned int* rest_len, snd_flags_t* send_flags) ; -int tls_h_read_f(struct tcp_connection *c, int* flags); +int tls_h_read_f(struct tcp_connection *c, rd_conn_flags_t* flags); int tls_h_fix_read_conn(struct tcp_connection *c); From b5fe9c99ca219bb888009caa001a107b9c363b63 Mon Sep 17 00:00:00 2001 From: Alex Hermann Date: Tue, 9 Jun 2020 09:15:26 +0200 Subject: [PATCH 2/4] core/ip_addr: Reformat and redefine si_flags as bitshifts Make it a bit easier to read --- src/core/ip_addr.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/ip_addr.h b/src/core/ip_addr.h index 53880b0059d..ae649f83d75 100644 --- a/src/core/ip_addr.h +++ b/src/core/ip_addr.h @@ -77,8 +77,14 @@ union sockaddr_union{ }; -enum si_flags { SI_NONE=0, SI_IS_IP=1, SI_IS_LO=2, SI_IS_MCAST=4, - SI_IS_ANY=8, SI_IS_MHOMED=16 }; +enum si_flags { + SI_NONE = 0, + SI_IS_IP = (1<<0), + SI_IS_LO = (1<<1), + SI_IS_MCAST = (1<<2), + SI_IS_ANY = (1<<3), + SI_IS_MHOMED = (1<<4), +}; typedef struct addr_info { str name; /* name - eg.: foo.bar or 10.0.0.1 */ From 31ed62898cefdc3691cde21c9cd44b1072b461ca Mon Sep 17 00:00:00 2001 From: Alex Hermann Date: Tue, 9 Jun 2020 09:16:16 +0200 Subject: [PATCH 3/4] core/ip_addr: Convert SND_F_ flags to enum Easier to recognize the correct flags in the forest of flags. --- src/core/ip_addr.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/ip_addr.h b/src/core/ip_addr.h index ae649f83d75..58e91f0ac4e 100644 --- a/src/core/ip_addr.h +++ b/src/core/ip_addr.h @@ -134,9 +134,11 @@ typedef struct socket_info { /* send flags */ -#define SND_F_FORCE_CON_REUSE 1 /* reuse an existing connection or fail */ -#define SND_F_CON_CLOSE 2 /* close the connection after sending */ -#define SND_F_FORCE_SOCKET 4 /* send socket in dst is forced */ +enum send_flags { + SND_F_FORCE_CON_REUSE = (1 << 0), /* reuse an existing connection or fail */ + SND_F_CON_CLOSE = (1 << 1), /* close the connection after sending */ + SND_F_FORCE_SOCKET = (1 << 2), /* send socket in dst is forced */ +}; typedef struct snd_flags { unsigned short f; /* snd flags */ From 7e5dc0d7b1c2735becbf7944add9678dc72ba3a7 Mon Sep 17 00:00:00 2001 From: Alex Hermann Date: Tue, 9 Jun 2020 09:27:15 +0200 Subject: [PATCH 4/4] core/tcp: Convert F_TCP_REQ_ flags to enum Easier to recognize the correct flags in the forest of flags. --- src/core/tcp_conn.h | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/core/tcp_conn.h b/src/core/tcp_conn.h index d9a69b69290..ded595fdcd1 100644 --- a/src/core/tcp_conn.h +++ b/src/core/tcp_conn.h @@ -121,6 +121,27 @@ enum conn_cmds { /* CONN_RELEASE, EOF, ERROR, DESTROY can be used by "reader" processes * CONN_GET_FD, CONN_NEW*, CONN_QUEUED_WRITE only by writers */ +/* tcp_req flags */ +enum tcp_req_flags { + F_TCP_REQ_HAS_CLEN = (1<<0), + F_TCP_REQ_COMPLETE = (1<<1), +#ifdef READ_HTTP11 + F_TCP_REQ_BCHUNKED = (1<<2), +#endif +#ifdef READ_MSRP + F_TCP_REQ_MSRP_NO = (1<<3), + F_TCP_REQ_MSRP_FRAME = (1<<4), + F_TCP_REQ_MSRP_BODY = (1<<5), +#endif + F_TCP_REQ_HEP3 = (1<<6), +}; + +#define TCP_REQ_HAS_CLEN(tr) ((tr)->flags & F_TCP_REQ_HAS_CLEN) +#define TCP_REQ_COMPLETE(tr) ((tr)->flags & F_TCP_REQ_COMPLETE) +#ifdef READ_HTTP11 +#define TCP_REQ_BCHUNKED(tr) ((tr)->flags & F_TCP_REQ_BCHUNKED) +#endif + struct tcp_req{ struct tcp_req* next; /* sockaddr ? */ @@ -135,32 +156,12 @@ struct tcp_req{ #ifdef READ_HTTP11 int chunk_size; #endif - unsigned int flags; /* F_TCP_REQ_HAS_CLEN | F_TCP_REQ_COMPLETE */ + enum tcp_req_flags flags; /* F_TCP_REQ_HAS_CLEN | F_TCP_REQ_COMPLETE */ int bytes_to_go; /* how many bytes we have still to read from the body*/ enum tcp_req_errors error; enum tcp_req_states state; }; -/* tcp_req flags */ -#define F_TCP_REQ_HAS_CLEN 1 -#define F_TCP_REQ_COMPLETE 2 -#ifdef READ_HTTP11 -#define F_TCP_REQ_BCHUNKED 4 -#endif -#ifdef READ_MSRP -#define F_TCP_REQ_MSRP_NO 8 -#define F_TCP_REQ_MSRP_FRAME 16 -#define F_TCP_REQ_MSRP_BODY 32 -#endif -#define F_TCP_REQ_HEP3 64 - -#define TCP_REQ_HAS_CLEN(tr) ((tr)->flags & F_TCP_REQ_HAS_CLEN) -#define TCP_REQ_COMPLETE(tr) ((tr)->flags & F_TCP_REQ_COMPLETE) -#ifdef READ_HTTP11 -#define TCP_REQ_BCHUNKED(tr) ((tr)->flags & F_TCP_REQ_BCHUNKED) -#endif - - struct tcp_connection; /* tcp port alias structure */