summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2010-11-26 21:59:27 +0000
committerChristian Pointner <equinox@spreadspace.org>2010-11-26 21:59:27 +0000
commitb9fef29a9d97ad15514b7356cf4de82775f6bf30 (patch)
treecef23360c5443f8a1db3491843005a29c7d542e9
parentadded generic single linked list (diff)
added tcp listener list
git-svn-id: https://svn.spreadspace.org/tcpproxy/trunk@7 e61f0598-a718-4e21-a8f0-0aadfa62ad6b
-rw-r--r--src/Makefile2
-rw-r--r--src/listener.c118
-rw-r--r--src/listener.h51
-rw-r--r--src/slist.c2
-rw-r--r--src/tcp.c70
-rw-r--r--src/tcp.h38
-rw-r--r--src/tcpproxy.c4
7 files changed, 282 insertions, 3 deletions
diff --git a/src/Makefile b/src/Makefile
index 6e70e6b..2fb2065 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -36,6 +36,8 @@ C_OBJS := log.o \
slist.o \
string_list.o \
sig_handler.o \
+ tcp.o \
+ listener.o \
tcpproxy.o
C_SRCS := $(C_OBJS:%.o=%.c)
diff --git a/src/listener.c b/src/listener.c
new file mode 100644
index 0000000..4025b26
--- /dev/null
+++ b/src/listener.c
@@ -0,0 +1,118 @@
+/*
+ * tcpproxy
+ *
+ * tcpproxy is a simple tcp connection proxy which combines the
+ * features of rinetd and 6tunnel. tcpproxy supports IPv4 and
+ * IPv6 and also supports connections from IPv6 to IPv4
+ * endpoints and vice versa.
+ *
+ *
+ * Copyright (C) 2010-2011 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of tcpproxy.
+ *
+ * tcpproxy is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * tcpproxy is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with tcpproxy. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "listener.h"
+#include "tcp.h"
+
+void listener_delete_element(void* e)
+{
+ if(!e)
+ return;
+
+ listener_t* element = (listener_t*)e;
+ close(element->fd_);
+
+ free(e);
+}
+
+int listener_init(listeners_t* list)
+{
+ return slist_init(list, &listener_delete_element);
+}
+
+void listener_clear(listeners_t* list)
+{
+ slist_clear(list);
+}
+
+int listener_add(listeners_t* list, char* laddr, char* lport, char* raddr, char* rport)
+{
+ if(!list)
+ return -1;
+
+ listener_t* element = malloc(sizeof(listener_t));
+ if(!element)
+ return -2;
+
+// TODO: open listen socket and resolv local and remote address
+
+ static int fds = 6;
+
+ element->fd_ = fds++;
+ memset(&(element->local_end_), 0, sizeof(element->local_end_));
+ memset(&(element->remote_end_), 0, sizeof(element->remote_end_));
+
+ if(slist_add(list, element) == NULL)
+ return -2;
+
+ return 0;
+}
+
+void listener_remove(listeners_t* list, int fd)
+{
+ slist_remove(list, listener_find(list, fd));
+}
+
+listener_t* listener_find(listeners_t* list, int fd)
+{
+ if(!list)
+ return NULL;
+
+ slist_element_t* tmp = list->first_;
+ while(tmp) {
+ listener_t* l = (listener_t*)tmp->data_;
+ if(l && l->fd_ == fd)
+ return l;
+ tmp = tmp->next_;
+ }
+
+ return NULL;
+}
+
+void listener_print(listeners_t* list)
+{
+ if(!list)
+ return;
+
+ slist_element_t* tmp = list->first_;
+ while(tmp) {
+ listener_t* l = (listener_t*)tmp->data_;
+ if(l) {
+ char* ls = tcp_endpoint_to_string(l->local_end_);
+ char* rs = tcp_endpoint_to_string(l->remote_end_);
+ printf("listener #%d: %s -> %s\n", l->fd_, ls ? ls : "(null)", rs ? rs : "(null)");
+ if(ls) free(ls);
+ if(rs) free(rs);
+ }
+ tmp = tmp->next_;
+ }
+}
diff --git a/src/listener.h b/src/listener.h
new file mode 100644
index 0000000..c545bdc
--- /dev/null
+++ b/src/listener.h
@@ -0,0 +1,51 @@
+/*
+ * tcpproxy
+ *
+ * tcpproxy is a simple tcp connection proxy which combines the
+ * features of rinetd and 6tunnel. tcpproxy supports IPv4 and
+ * IPv6 and also supports connections from IPv6 to IPv4
+ * endpoints and vice versa.
+ *
+ *
+ * Copyright (C) 2010-2011 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of tcpproxy.
+ *
+ * tcpproxy is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * tcpproxy is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with tcpproxy. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TCPPROXY_listener_h_INCLUDED
+#define TCPPROXY_listener_h_INCLUDED
+
+#include "slist.h"
+#include "tcp.h"
+
+typedef struct {
+ int fd_;
+ tcp_endpoint_t local_end_;
+ tcp_endpoint_t remote_end_;
+} listener_t;
+
+void listener_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, char* laddr, char* lport, char* raddr, char* rport);
+void listener_remove(listeners_t* list, int fd);
+listener_t* listener_find(listeners_t* list, int fd);
+void listener_print(listeners_t* list);
+
+#endif
diff --git a/src/slist.c b/src/slist.c
index 58528bd..8dd1963 100644
--- a/src/slist.c
+++ b/src/slist.c
@@ -76,7 +76,7 @@ slist_element_t* slist_add(slist_t* lst, void* data)
void slist_remove(slist_t* lst, void* data)
{
- if(!lst || !lst->first_)
+ if(!lst || !lst->first_ || !data)
return;
slist_element_t* tmp = lst->first_->next_;
diff --git a/src/tcp.c b/src/tcp.c
new file mode 100644
index 0000000..42db969
--- /dev/null
+++ b/src/tcp.c
@@ -0,0 +1,70 @@
+/*
+ * tcpproxy
+ *
+ * tcpproxy is a simple tcp connection proxy which combines the
+ * features of rinetd and 6tunnel. tcpproxy supports IPv4 and
+ * IPv6 and also supports connections from IPv6 to IPv4
+ * endpoints and vice versa.
+ *
+ *
+ * Copyright (C) 2010-2011 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of tcpproxy.
+ *
+ * tcpproxy is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * tcpproxy is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with tcpproxy. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <arpa/inet.h>
+
+#include "datatypes.h"
+
+#include "tcp.h"
+
+char* tcp_endpoint_to_string(tcp_endpoint_t e)
+{
+ void* ptr;
+ u_int16_t port;
+ size_t addrstr_len = 0;
+ char* addrstr, *ret;
+ char addrport_sep = ':';
+
+ switch(((struct sockaddr *)&e)->sa_family)
+ {
+ case AF_INET:
+ ptr = &((struct sockaddr_in *)&e)->sin_addr;
+ port = ntohs(((struct sockaddr_in *)&e)->sin_port);
+ addrstr_len = INET_ADDRSTRLEN + 1;
+ addrport_sep = ':';
+ break;
+ case AF_INET6:
+ ptr = &((struct sockaddr_in6 *)&e)->sin6_addr;
+ port = ntohs(((struct sockaddr_in6 *)&e)->sin6_port);
+ addrstr_len = INET6_ADDRSTRLEN + 1;
+ addrport_sep = '.';
+ break;
+ default:
+ asprintf(&ret, "unknown address type");
+ return ret;
+ }
+ addrstr = malloc(addrstr_len);
+ if(!addrstr)
+ return NULL;
+ inet_ntop (((struct sockaddr *)&e)->sa_family, ptr, addrstr, addrstr_len);
+ asprintf(&ret, "%s%c%d", addrstr, addrport_sep ,port);
+ free(addrstr);
+ return ret;
+}
diff --git a/src/tcp.h b/src/tcp.h
new file mode 100644
index 0000000..a2c50d6
--- /dev/null
+++ b/src/tcp.h
@@ -0,0 +1,38 @@
+/*
+ * tcpproxy
+ *
+ * tcpproxy is a simple tcp connection proxy which combines the
+ * features of rinetd and 6tunnel. tcpproxy supports IPv4 and
+ * IPv6 and also supports connections from IPv6 to IPv4
+ * endpoints and vice versa.
+ *
+ *
+ * Copyright (C) 2010-2011 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of tcpproxy.
+ *
+ * tcpproxy is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * tcpproxy is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with tcpproxy. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TCPPROXY_tcp_h_INCLUDED
+#define TCPPROXY_tcp_h_INCLUDED
+
+#include <sys/types.h>
+#include <sys/socket.h>
+
+typedef struct sockaddr_storage tcp_endpoint_t;
+
+char* tcp_endpoint_to_string(tcp_endpoint_t e);
+
+#endif
diff --git a/src/tcpproxy.c b/src/tcpproxy.c
index b708f31..45402c7 100644
--- a/src/tcpproxy.c
+++ b/src/tcpproxy.c
@@ -36,6 +36,8 @@
#include "log.h"
#include "daemon.h"
+#include "listener.h"
+
int main_loop(options_t* opt)
{
log_printf(INFO, "entering main loop");
@@ -121,8 +123,6 @@ int main(int argc, char* argv[])
log_printf(NOTICE, "just started...");
options_parse_post(&opt);
- options_print(&opt);
-
priv_info_t priv;
if(opt.username_)
if(priv_init(&priv, opt.username_, opt.groupname_)) {