summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2010-12-09 20:49:35 +0000
committerChristian Pointner <equinox@spreadspace.org>2010-12-09 20:49:35 +0000
commit78c841168b3e018f472fb55e7c2d65c8e152ef56 (patch)
treec91be01511f4e7b11ced6cbccdfa3db74d8b6ed0
parentintroduced listener states (diff)
listner_ vs listeners_
git-svn-id: https://svn.spreadspace.org/tcpproxy/trunk@42 e61f0598-a718-4e21-a8f0-0aadfa62ad6b
-rw-r--r--src/cfg_parser.rl2
-rw-r--r--src/listener.c125
-rw-r--r--src/listener.h22
-rw-r--r--src/tcpproxy.c24
4 files changed, 89 insertions, 84 deletions
diff --git a/src/cfg_parser.rl b/src/cfg_parser.rl
index ad11b08..c215a83 100644
--- a/src/cfg_parser.rl
+++ b/src/cfg_parser.rl
@@ -107,7 +107,7 @@ static int owrt_string(char** dest, char* start, char* end)
action set_remote_resolv6 { lst.rrt_ = IPV6_ONLY; }
action set_source_addr { ret = owrt_string(&(lst.sa_), cpy_start, fpc); cpy_start = NULL; }
action add_listener {
- ret = listener_add(listener, lst.la_, lst.lrt_, lst.lp_, lst.ra_, lst.rrt_, lst.rp_, lst.sa_);
+ ret = listeners_add(listener, lst.la_, lst.lrt_, lst.lp_, lst.ra_, lst.rrt_, lst.rp_, lst.sa_);
clear_listener_struct(&lst);
}
diff --git a/src/listener.c b/src/listener.c
index 19be646..80876c5 100644
--- a/src/listener.c
+++ b/src/listener.c
@@ -45,7 +45,7 @@
#include "clients.h"
-void listener_delete_element(void* e)
+void listeners_delete_element(void* e)
{
if(!e)
return;
@@ -57,17 +57,17 @@ void listener_delete_element(void* e)
free(e);
}
-int listener_init(listeners_t* list)
+int listeners_init(listeners_t* list)
{
- return slist_init(list, &listener_delete_element);
+ return slist_init(list, &listeners_delete_element);
}
-void listener_clear(listeners_t* list)
+void listeners_clear(listeners_t* list)
{
slist_clear(list);
}
-int listener_add(listeners_t* list, const char* laddr, resolv_type_t lrt, const char* lport, const char* raddr, resolv_type_t rrt, const char* rport, const char* saddr)
+int listeners_add(listeners_t* list, const char* laddr, resolv_type_t lrt, const char* lport, const char* raddr, resolv_type_t rrt, const char* rport, const char* saddr)
{
if(!list)
return -1;
@@ -138,7 +138,55 @@ int listener_add(listeners_t* list, const char* laddr, resolv_type_t lrt, const
return ret;
}
-int listener_activate(listeners_t* list)
+static int activate_listener(listener_t* l)
+{
+ l->fd_ = socket(l->local_end_.addr_.ss_family, SOCK_STREAM, 0);
+ if(l->fd_ < 0) {
+ log_printf(ERROR, "Error on opening tcp socket: %s", strerror(errno));
+ l->state_ = ZOMBIE;
+ return -1;
+ }
+
+ int on = 1;
+ int ret = setsockopt(l->fd_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+ if(ret) {
+ log_printf(ERROR, "Error on setsockopt(): %s", strerror(errno));
+ l->state_ = ZOMBIE;
+ return -1;
+ }
+ if(l->local_end_.addr_.ss_family == AF_INET6) {
+ if(setsockopt(l->fd_, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)))
+ log_printf(WARNING, "failed to set IPV6_V6ONLY socket option: %s", strerror(errno));
+ }
+
+ ret = bind(l->fd_, (struct sockaddr *)&(l->local_end_.addr_), l->local_end_.len_);
+ if(ret) {
+ log_printf(ERROR, "Error on bind(): %s", strerror(errno));
+ l->state_ = ZOMBIE;
+ return -1;
+ }
+
+ ret = listen(l->fd_, 0);
+ if(ret) {
+ log_printf(ERROR, "Error on listen(): %s", strerror(errno));
+ l->state_ = ZOMBIE;
+ return -1;
+ }
+
+ l->state_ = ACTIVE;
+
+ char* ls = tcp_endpoint_to_string(l->local_end_);
+ char* rs = tcp_endpoint_to_string(l->remote_end_);
+ char* ss = tcp_endpoint_to_string(l->source_end_);
+ log_printf(NOTICE, "listening on: %s (remote: %s%s%s)", ls ? ls:"(null)", rs ? rs:"(null)", ss ? " with source " : "", ss ? ss : "");
+ if(ls) free(ls);
+ if(rs) free(rs);
+ if(ss) free(ss);
+
+ return 0;
+}
+
+int listeners_activate(listeners_t* list)
{
if(!list)
return;
@@ -147,61 +195,18 @@ int listener_activate(listeners_t* list)
slist_element_t* tmp = list->first_;
while(tmp) {
listener_t* l = (listener_t*)tmp->data_;
- if(l && l->state_ == NEW) {
- l->fd_ = socket(l->local_end_.addr_.ss_family, SOCK_STREAM, 0);
- if(l->fd_ < 0) {
- log_printf(ERROR, "Error on opening tcp socket: %s", strerror(errno));
- l->state_ = ZOMBIE;
- ret = -1;
- break;
- }
-
- int on = 1;
- ret = setsockopt(l->fd_, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
- if(ret) {
- log_printf(ERROR, "Error on setsockopt(): %s", strerror(errno));
- l->state_ = ZOMBIE;
- ret = -1;
- break;
- }
- if(l->local_end_.addr_.ss_family == AF_INET6) {
- if(setsockopt(l->fd_, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)))
- log_printf(WARNING, "failed to set IPV6_V6ONLY socket option: %s", strerror(errno));
- }
-
- ret = bind(l->fd_, (struct sockaddr *)&(l->local_end_.addr_), l->local_end_.len_);
- if(ret) {
- log_printf(ERROR, "Error on bind(): %s", strerror(errno));
- l->state_ = ZOMBIE;
- ret = -1;
- break;
- }
-
- ret = listen(l->fd_, 0);
- if(ret) {
- log_printf(ERROR, "Error on listen(): %s", strerror(errno));
- l->state_ = ZOMBIE;
- ret = -1;
- break;
- }
+ if(l && l->state_ == NEW)
+ ret = activate_listener(l);
- l->state_ = ACTIVE;
-
- char* ls = tcp_endpoint_to_string(l->local_end_);
- char* rs = tcp_endpoint_to_string(l->remote_end_);
- char* ss = tcp_endpoint_to_string(l->source_end_);
- log_printf(NOTICE, "listening on: %s (remote: %s%s%s)", ls ? ls:"(null)", rs ? rs:"(null)", ss ? " with source " : "", ss ? ss : "");
- if(ls) free(ls);
- if(rs) free(rs);
- if(ss) free(ss);
- }
+ if(ret)
+ break;
tmp = tmp->next_;
}
return ret;
}
-void listener_cleanup(listeners_t* list)
+void listeners_cleanup(listeners_t* list)
{
if(!list)
return;
@@ -215,12 +220,12 @@ void listener_cleanup(listeners_t* list)
}
}
-void listener_remove(listeners_t* list, int fd)
+void listeners_remove(listeners_t* list, int fd)
{
- slist_remove(list, listener_find(list, fd));
+ slist_remove(list, listeners_find(list, fd));
}
-listener_t* listener_find(listeners_t* list, int fd)
+listener_t* listeners_find(listeners_t* list, int fd)
{
if(!list)
return NULL;
@@ -236,7 +241,7 @@ listener_t* listener_find(listeners_t* list, int fd)
return NULL;
}
-void listener_print(listeners_t* list)
+void listeners_print(listeners_t* list)
{
if(!list)
return;
@@ -263,7 +268,7 @@ void listener_print(listeners_t* list)
}
}
-void listener_read_fds(listeners_t* list, fd_set* set, int* max_fd)
+void listeners_read_fds(listeners_t* list, fd_set* set, int* max_fd)
{
if(!list)
return;
@@ -279,7 +284,7 @@ void listener_read_fds(listeners_t* list, fd_set* set, int* max_fd)
}
}
-int listener_handle_accept(listeners_t* list, clients_t* clients, fd_set* set)
+int listeners_handle_accept(listeners_t* list, clients_t* clients, fd_set* set)
{
if(!list)
return -1;
diff --git a/src/listener.h b/src/listener.h
index 2cb88d6..7638254 100644
--- a/src/listener.h
+++ b/src/listener.h
@@ -45,20 +45,20 @@ typedef struct {
listener_state_t state_;
} listener_t;
-void listener_delete_element(void* e);
+void listeners_delete_element(void* e);
typedef slist_t listeners_t;
-int listener_init(listeners_t* list);
-void listener_clear(listeners_t* list);
-int listener_add(listeners_t* list, const char* laddr, resolv_type_t lrt, const char* lport, const char* raddr, resolv_type_t rrt, const char* rport, const char* saddr);
-int listener_activate(listeners_t* list);
-void listener_cleanup(listeners_t* list);
-void listener_remove(listeners_t* list, int fd);
-listener_t* listener_find(listeners_t* list, int fd);
-void listener_print(listeners_t* list);
+int listeners_init(listeners_t* list);
+void listeners_clear(listeners_t* list);
+int listeners_add(listeners_t* list, const char* laddr, resolv_type_t lrt, const char* lport, const char* raddr, resolv_type_t rrt, const char* rport, const char* saddr);
+int listeners_activate(listeners_t* list);
+void listeners_cleanup(listeners_t* list);
+void listeners_remove(listeners_t* list, int fd);
+listener_t* listeners_find(listeners_t* list, int fd);
+void listeners_print(listeners_t* list);
-void listener_read_fds(listeners_t* list, fd_set* set, int* max_fd);
-int listener_handle_accept(listeners_t* list, clients_t* clients, fd_set* set);
+void listeners_read_fds(listeners_t* list, fd_set* set, int* max_fd);
+int listeners_handle_accept(listeners_t* list, clients_t* clients, fd_set* set);
#endif
diff --git a/src/tcpproxy.c b/src/tcpproxy.c
index 58f9a4c..fcc871f 100644
--- a/src/tcpproxy.c
+++ b/src/tcpproxy.c
@@ -58,7 +58,7 @@ int main_loop(options_t* opt, listeners_t* listeners)
FD_ZERO(&writefds);
FD_SET(sig_fd, &readfds);
int nfds = sig_fd;
- listener_read_fds(listeners, &readfds, &nfds);
+ listeners_read_fds(listeners, &readfds, &nfds);
clients_read_fds(&clients, &readfds, &nfds);
clients_write_fds(&clients, &writefds, &nfds);
int ret = select(nfds + 1, &readfds, &writefds, NULL, NULL);
@@ -77,7 +77,7 @@ int main_loop(options_t* opt, listeners_t* listeners)
}
}
- return_value = listener_handle_accept(listeners, &clients, &readfds);
+ return_value = listeners_handle_accept(listeners, &clients, &readfds);
if(return_value) break;
return_value = clients_write(&clients, &writefds);
@@ -140,7 +140,7 @@ int main(int argc, char* argv[])
options_parse_post(&opt);
listeners_t listeners;
- ret = listener_init(&listeners);
+ ret = listeners_init(&listeners);
if(ret) {
options_clear(&opt);
log_close();
@@ -148,9 +148,9 @@ int main(int argc, char* argv[])
}
if(opt.local_port_) {
- ret = listener_add(&listeners, opt.local_addr_, opt.lresolv_type_, opt.local_port_, opt.remote_addr_, opt.rresolv_type_, opt.remote_port_, opt.source_addr_);
+ ret = listeners_add(&listeners, opt.local_addr_, opt.lresolv_type_, opt.local_port_, opt.remote_addr_, opt.rresolv_type_, opt.remote_port_, opt.source_addr_);
if(ret) {
- listener_clear(&listeners);
+ listeners_clear(&listeners);
options_clear(&opt);
log_close();
exit(-1);
@@ -160,16 +160,16 @@ int main(int argc, char* argv[])
if(ret || !slist_length(&listeners)) {
if(!ret)
log_printf(ERROR, "no listeners defined in config file %s", opt.config_file_);
- listener_clear(&listeners);
+ listeners_clear(&listeners);
options_clear(&opt);
log_close();
exit(-1);
}
}
- ret = listener_activate(&listeners);
+ ret = listeners_activate(&listeners);
if(ret) {
- listener_clear(&listeners);
+ listeners_clear(&listeners);
options_clear(&opt);
log_close();
exit(-1);
@@ -178,7 +178,7 @@ int main(int argc, char* argv[])
priv_info_t priv;
if(opt.username_)
if(priv_init(&priv, opt.username_, opt.groupname_)) {
- listener_clear(&listeners);
+ listeners_clear(&listeners);
options_clear(&opt);
log_close();
exit(-1);
@@ -194,14 +194,14 @@ int main(int argc, char* argv[])
if(opt.chroot_dir_)
if(do_chroot(opt.chroot_dir_)) {
- listener_clear(&listeners);
+ listeners_clear(&listeners);
options_clear(&opt);
log_close();
exit(-1);
}
if(opt.username_)
if(priv_drop(&priv)) {
- listener_clear(&listeners);
+ listeners_clear(&listeners);
options_clear(&opt);
log_close();
exit(-1);
@@ -221,7 +221,7 @@ int main(int argc, char* argv[])
ret = main_loop(&opt, &listeners);
- listener_clear(&listeners);
+ listeners_clear(&listeners);
options_clear(&opt);
if(!ret)