summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2015-05-25 03:52:49 +0200
committerChristian Pointner <equinox@spreadspace.org>2015-05-25 03:52:49 +0200
commitfa242bb301de8bbfdef76ced13fb32a3b744f10c (patch)
tree78a0686b2e935da05abe9cc6313e0c377cdeaf8c
parenttry not to loose data when removing client... needs testing!!! (diff)
added testserver and testclient
-rw-r--r--.gitignore2
-rw-r--r--contrib/Makefile37
-rw-r--r--contrib/testclient.c130
-rw-r--r--contrib/testserver.c125
-rw-r--r--src/clients.c1
5 files changed, 294 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 04fd414..d1539c4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
+contrib/testserver
+contrib/testclient
doc/tcpproxy.8.xml
src/include.mk
src/tcpproxy
diff --git a/contrib/Makefile b/contrib/Makefile
new file mode 100644
index 0000000..6e9bd6d
--- /dev/null
+++ b/contrib/Makefile
@@ -0,0 +1,37 @@
+##
+## 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-2015 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/>.
+##
+
+.PHONY: clean
+
+all: testclient testserver
+
+%: %.c
+ $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $<
+
+clean:
+ rm -f testclient
+ rm -f testserver
diff --git a/contrib/testclient.c b/contrib/testclient.c
new file mode 100644
index 0000000..a8dbf2e
--- /dev/null
+++ b/contrib/testclient.c
@@ -0,0 +1,130 @@
+/*
+ * 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-2015 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 <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+int main(int argc, char* argv[])
+{
+ if(argc < 3) {
+ fprintf(stderr, "Usage: %s <addr> <port>\n", argv[0]);
+ return -1;
+ }
+
+ int c = socket(AF_INET, SOCK_STREAM, 0);
+ if(!c) {
+ perror("socket()");
+ return -1;
+ }
+
+ struct sockaddr_in addr;
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(atoi(argv[2]));
+ int ret = inet_pton(AF_INET, argv[1], &(addr.sin_addr));
+ if (ret <= 0) {
+ if (ret == 0)
+ fprintf(stderr, "inet_pton(): Not in presentation format\n");
+ else
+ perror("inet_pton()");
+ return -1;
+ }
+
+ ret = connect(c, (struct sockaddr*)(&addr), sizeof(struct sockaddr_in));
+ if(ret) {
+ perror("connect()");
+ return -1;
+ }
+
+ char buf[123456];
+ unsigned int i;
+ for(i = 0; i<sizeof(buf); ++i) {
+ buf[i] = 'A' + i%62;
+ }
+ buf[sizeof(buf)-1] = '\n';
+
+ int nbwritten = send(c, buf, sizeof(buf), 0);
+ if(nbwritten <= 0) {
+ if(nbwritten < 0)
+ perror("send()");
+ else
+ fprintf(stderr, "nothing sent... aborting\n");
+ return -1;
+ }
+ printf("%d bytes sent\n", nbwritten);
+ if(nbwritten != sizeof(buf)) {
+ fprintf(stderr, "to few bytes sent ... aborting!");
+ return -1;
+ }
+
+ shutdown(c, SHUT_WR);
+
+ int len = 0;
+ for(;;) {
+ int nbread = recv(c, &(buf[len]), sizeof(buf) - len, 0);
+ if(nbread <= 0) {
+ if(!nbread) {
+ fprintf(stderr, "connection closed\n");
+ } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
+ perror("recv()");
+ } else
+ continue;
+
+ return -1;
+ }
+
+ printf("%d bytes received", nbread);
+ len += nbread;
+ if(len == nbwritten) {
+ printf(" .. finished\n");
+ for(i = 0; i<nbwritten-1; ++i) {
+ if(buf[i] != ('A' + i%62)) {
+ fprintf(stderr, "error at index %d (expected 0x%02X, got 0x%02X)\n", i, 'A' + i%62, (unsigned int)(buf[i]));
+ return -1;
+ }
+ }
+ if(buf[nbwritten-1] != '\n') {
+ fprintf(stderr, "string doesn't end with new-line\n");
+ return -1;
+ }
+ break;
+ }
+
+ printf("\n");
+ }
+
+ while(recv(c, buf, 1, 0) > 0);
+
+ return 0;
+}
diff --git a/contrib/testserver.c b/contrib/testserver.c
new file mode 100644
index 0000000..b9e3f0a
--- /dev/null
+++ b/contrib/testserver.c
@@ -0,0 +1,125 @@
+/*
+ * 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-2015 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 <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+int main(int argc, char* argv[])
+{
+ if(argc < 2) {
+ fprintf(stderr, "Usage: %s <port>\n", argv[0]);
+ return -1;
+ }
+
+ int s = socket(AF_INET, SOCK_STREAM, 0);
+ if(!s) {
+ perror("socket()");
+ return -1;
+ }
+
+ int on = 1;
+ int ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+ if(ret) {
+ perror("setsockopt(SO_REUSEADDR)");
+ return -1;
+ }
+
+ struct sockaddr_in laddr;
+ laddr.sin_family = AF_INET;
+ laddr.sin_port = htons(atoi(argv[1]));
+ laddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ ret = bind(s, (struct sockaddr*)(&laddr), sizeof(struct sockaddr_in));
+ if(ret) {
+ perror("bind()");
+ return -1;
+ }
+
+ ret = listen(s, 1);
+ if(ret) {
+ perror("listen()");
+ return -1;
+ }
+
+ struct sockaddr_in caddr;
+ socklen_t len = sizeof(struct sockaddr_in);
+ int c = accept(s, (struct sockaddr*)(&caddr), &len);
+ if(c < 0) {
+ perror("accept()");
+ return -1;
+ }
+ char addr_str[64];
+ if(!inet_ntop(AF_INET, (&caddr.sin_addr), addr_str, sizeof(addr_str))) {
+ perror("inet_ntop");
+ return -1;
+ }
+ printf("connection from %s:%d\n", addr_str, caddr.sin_port);
+
+
+ char buf[10 * 1024];
+ for(;;) {
+ int nbread = recv(c, buf, sizeof(buf), 0);
+ if(nbread <= 0) {
+ if(!nbread) {
+ fprintf(stderr, "connection closed\n");
+ return 0;
+ } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
+ perror("recv()");
+ return -1;
+ }
+ continue;
+ }
+
+ printf("%d bytes received\n", nbread);
+
+ int len = 0;
+ for(;;) {
+ int nbwritten = send(c, &(buf[len]), nbread - len, 0);
+ if(nbwritten <= 0) {
+ if(nbwritten < 0 && (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) {
+ perror("send()");
+ return -1;
+ }
+ continue;
+ }
+ printf("%d bytes sent\n", nbwritten);
+ len += nbwritten;
+ if(len >= nbread) {
+ break;
+ }
+ }
+ }
+
+ return 0;
+}
diff --git a/src/clients.c b/src/clients.c
index f59d575..dfd00cc 100644
--- a/src/clients.c
+++ b/src/clients.c
@@ -408,7 +408,6 @@ int clients_write(clients_t* list, fd_set* set)
for(i=0; i<2; ++i) {
if(FD_ISSET(c->fd_[i], set)) {
log_printf(DEBUG, "calling send(%d)", c->fd_[i]);
-
int len = send(c->fd_[i], c->write_buf_[i].buf_, c->write_buf_offset_[i], 0);
if(len < 0) {
// TODO: the other socket might still have data pending....