From 147af10b619e1e2ba249b474522000eee6603387 Mon Sep 17 00:00:00 2001 From: bol-van Date: Fri, 26 Apr 2024 17:23:03 +0300 Subject: [PATCH] tpws: move portfilter code --- tpws/helpers.c | 32 ++++++++++++++++++++++++++++++++ tpws/helpers.h | 8 ++++++++ tpws/params.h | 7 +------ tpws/tpws_conn.c | 34 ---------------------------------- tpws/tpws_conn.h | 3 --- 5 files changed, 41 insertions(+), 43 deletions(-) diff --git a/tpws/helpers.c b/tpws/helpers.c index feb85b9..5ef0261 100644 --- a/tpws/helpers.c +++ b/tpws/helpers.c @@ -229,3 +229,35 @@ time_t file_mod_time(const char *filename) struct stat st; return stat(filename,&st)==-1 ? 0 : st.st_mtime; } + +bool pf_in_range(uint16_t port, const port_filter *pf) +{ + return port && ((!pf->from && !pf->to || port>=pf->from && port<=pf->to) ^ pf->neg); +} +bool pf_parse(const char *s, port_filter *pf) +{ + unsigned int v1,v2; + + if (!s) return false; + if (*s=='~') + { + pf->neg=true; + s++; + } + else + pf->neg=false; + if (sscanf(s,"%u-%u",&v1,&v2)==2) + { + if (!v1 || v1>65535 || v2>65535 || v1>v2) return false; + pf->from=(uint16_t)v1; + pf->to=(uint16_t)v2; + } + else if (sscanf(s,"%u",&v1)==1) + { + if (!v1 || v1>65535) return false; + pf->to=pf->from=(uint16_t)v1; + } + else + return false; + return true; +} diff --git a/tpws/helpers.h b/tpws/helpers.h index 00a1bc4..14cc220 100644 --- a/tpws/helpers.h +++ b/tpws/helpers.h @@ -47,3 +47,11 @@ static inline void phton16(uint8_t *p, uint16_t v) { int fprint_localtime(FILE *F); time_t file_mod_time(const char *filename); + +typedef struct +{ + uint16_t from,to; + bool neg; +} port_filter; +bool pf_in_range(uint16_t port, const port_filter *pf); +bool pf_parse(const char *s, port_filter *pf); diff --git a/tpws/params.h b/tpws/params.h index 8b608e8..13bc36a 100644 --- a/tpws/params.h +++ b/tpws/params.h @@ -8,6 +8,7 @@ #include #include "tpws.h" #include "pools.h" +#include "helpers.h" #define HOSTLIST_AUTO_FAIL_THRESHOLD_DEFAULT 3 #define HOSTLIST_AUTO_FAIL_TIME_DEFAULT 60 @@ -25,12 +26,6 @@ struct bind_s int bind_wait_ifup,bind_wait_ip,bind_wait_ip_ll; }; -typedef struct -{ - uint16_t from,to; - bool neg; -} port_filter; - struct params_s { struct bind_s binds[MAX_BINDS]; diff --git a/tpws/tpws_conn.c b/tpws/tpws_conn.c index 1fcd593..0e2dec9 100644 --- a/tpws/tpws_conn.c +++ b/tpws/tpws_conn.c @@ -1530,37 +1530,3 @@ ex: if (resolve_pipe[1]) close(resolve_pipe[1]); return retval; } - - -bool pf_in_range(uint16_t port, const port_filter *pf) -{ - return port && ((!pf->from && !pf->to || port>=pf->from && port<=pf->to) ^ pf->neg); -} -bool pf_parse(const char *s, port_filter *pf) -{ - unsigned int v1,v2; - - if (!s) return false; - if (*s=='~') - { - pf->neg=true; - s++; - } - else - pf->neg=false; - if (sscanf(s,"%u-%u",&v1,&v2)==2) - { - if (!v1 || v1>65535 || v2>65535 || v1>v2) return false; - pf->from=(uint16_t)v1; - pf->to=(uint16_t)v2; - } - else if (sscanf(s,"%u",&v1)==1) - { - if (!v1 || v1>65535) return false; - pf->to=pf->from=(uint16_t)v1; - } - else - return false; - return true; -} - diff --git a/tpws/tpws_conn.h b/tpws/tpws_conn.h index c15b4b8..b45afb8 100644 --- a/tpws/tpws_conn.h +++ b/tpws/tpws_conn.h @@ -103,6 +103,3 @@ TAILQ_HEAD(tailhead, tproxy_conn); bool set_socket_buffers(int fd, int rcvbuf, int sndbuf); - -bool pf_in_range(uint16_t port, const port_filter *pf); -bool pf_parse(const char *s, port_filter *pf);