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);