From f9f17c0df333970b6d126df817f63acac01d58b9 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 4 Jun 2015 20:14:54 +0200 Subject: moved test tools to seperate directory --- .gitignore | 4 +- contrib/Makefile | 37 ---------- contrib/testclient.c | 185 ----------------------------------------------- contrib/testserver.c | 197 --------------------------------------------------- test/Makefile | 37 ++++++++++ test/test.conf | 4 ++ test/testclient.c | 185 +++++++++++++++++++++++++++++++++++++++++++++++ test/testserver.c | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 425 insertions(+), 421 deletions(-) delete mode 100644 contrib/Makefile delete mode 100644 contrib/testclient.c delete mode 100644 contrib/testserver.c create mode 100644 test/Makefile create mode 100644 test/test.conf create mode 100644 test/testclient.c create mode 100644 test/testserver.c diff --git a/.gitignore b/.gitignore index d1539c4..4b84d3f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ -contrib/testserver -contrib/testclient doc/tcpproxy.8.xml src/include.mk src/tcpproxy @@ -10,3 +8,5 @@ src/config.h src/cfg_parser.c src/cfg_parser.dot src/cfg_parser.png +test/testserver +test/testclient diff --git a/contrib/Makefile b/contrib/Makefile deleted file mode 100644 index 6e9bd6d..0000000 --- a/contrib/Makefile +++ /dev/null @@ -1,37 +0,0 @@ -## -## 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 -## -## 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 . -## - -.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 deleted file mode 100644 index bc93336..0000000 --- a/contrib/testclient.c +++ /dev/null @@ -1,185 +0,0 @@ -/* - * 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 - * - * 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 . - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -int main(int argc, char* argv[]) -{ - if(argc < 4) { - fprintf(stderr, "Usage: %s (A|B)\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; - } - - switch(toupper(argv[3][0])) { -/*************** MODE A ***************/ - case 'A': { - printf("MODE: A\n"); - - char buf[1234567]; - unsigned int i; - for(i = 0; i= sizeof(buf)) { - break; - } - } - - shutdown(c, SHUT_WR); - - int rtot = 0; - for(;;) { - int nbread = recv(c, &(buf[rtot]), sizeof(buf) - rtot, 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; - } - - rtot += nbread; - printf("%d bytes received, total = %d, left = %d", nbread, rtot, rtot - wtot); - if(rtot == wtot) { - printf(" .. finished\n"); - for(i = 0; i wtot) { - fprintf(stderr, "got too many bytes back???\n"); - return -1; - } - - printf("\n"); - } - - while(recv(c, buf, 1, 0) > 0); - break; - } -/*************** MODE A ***************/ - case 'B': { - printf("MODE: B\n"); - - char request[] = "hello world\n"; - int nbwritten = send(c, request, sizeof(request), 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(request)) { - fprintf(stderr, "to few bytes sent ... aborting!"); - return -1; - } - - char buf[1000]; - int rtot = 0; - for(;;) { - int nbread = recv(c, buf, sizeof(buf), 0); - if(nbread <= 0) { - if(!nbread) { - fprintf(stderr, "connection closed ... finished\n"); - return 0; - } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { - perror("recv()"); - return -1; - } - continue; - } - - rtot += nbread; - printf("%d bytes received, total = %d\n", nbread, rtot); - } - break; - } -/**************************************/ - } - - return 0; -} diff --git a/contrib/testserver.c b/contrib/testserver.c deleted file mode 100644 index 00e0ac4..0000000 --- a/contrib/testserver.c +++ /dev/null @@ -1,197 +0,0 @@ -/* - * 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 - * - * 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 . - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -int main(int argc, char* argv[]) -{ - if(argc < 3) { - fprintf(stderr, "Usage: %s (A|B)\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; - } - - printf("MODE: %c, listening...\n", toupper(argv[2][0])); - - 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); - - - switch(toupper(argv[2][0])) { -/*************** MODE A ***************/ - case 'A': { - char buf[10000]; - int rtot = 0; - for(;;) { - int nbread = recv(c, buf, sizeof(buf), 0); - if(nbread <= 0) { - if(!nbread) { - fprintf(stderr, "connection closed\n"); - sleep(1); - return 0; - } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { - perror("recv()"); - return -1; - } - continue; - } - - rtot += nbread; - printf("%d bytes received, total = %d\n", nbread, rtot); - - 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; - } - } - } - break; - } -/*************** MODE A ***************/ - case 'B': { - char buf[1234567]; - int rtot = 0; - for(;;) { - int nbread = recv(c, &(buf[rtot]), sizeof(buf) - rtot, 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; - } - - rtot += nbread; - printf("%d bytes received, total = %d\n", nbread, rtot); - unsigned char* end = memchr(buf, '\n', rtot); - if(!end) { - if(rtot >= sizeof(buf)) { - printf("Request too big...\n"); - return -2; - } - } else { - *end = 0; - printf("Got Request: '%s'\n", buf); - break; - } - } - - unsigned int i; - for(i = 0; i= sizeof(buf)) { - break; - } - } - shutdown(c, SHUT_WR); - while(recv(c, buf, 1, 0) > 0); - - break; - } -/**************************************/ - } - - return 0; -} diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..6e9bd6d --- /dev/null +++ b/test/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 +## +## 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 . +## + +.PHONY: clean + +all: testclient testserver + +%: %.c + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $< + +clean: + rm -f testclient + rm -f testserver diff --git a/test/test.conf b/test/test.conf new file mode 100644 index 0000000..979e5d0 --- /dev/null +++ b/test/test.conf @@ -0,0 +1,4 @@ +listen * 8000 +{ + remote: 127.0.0.1 8001; +}; diff --git a/test/testclient.c b/test/testclient.c new file mode 100644 index 0000000..bc93336 --- /dev/null +++ b/test/testclient.c @@ -0,0 +1,185 @@ +/* + * 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 + * + * 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 . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + if(argc < 4) { + fprintf(stderr, "Usage: %s (A|B)\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; + } + + switch(toupper(argv[3][0])) { +/*************** MODE A ***************/ + case 'A': { + printf("MODE: A\n"); + + char buf[1234567]; + unsigned int i; + for(i = 0; i= sizeof(buf)) { + break; + } + } + + shutdown(c, SHUT_WR); + + int rtot = 0; + for(;;) { + int nbread = recv(c, &(buf[rtot]), sizeof(buf) - rtot, 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; + } + + rtot += nbread; + printf("%d bytes received, total = %d, left = %d", nbread, rtot, rtot - wtot); + if(rtot == wtot) { + printf(" .. finished\n"); + for(i = 0; i wtot) { + fprintf(stderr, "got too many bytes back???\n"); + return -1; + } + + printf("\n"); + } + + while(recv(c, buf, 1, 0) > 0); + break; + } +/*************** MODE A ***************/ + case 'B': { + printf("MODE: B\n"); + + char request[] = "hello world\n"; + int nbwritten = send(c, request, sizeof(request), 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(request)) { + fprintf(stderr, "to few bytes sent ... aborting!"); + return -1; + } + + char buf[1000]; + int rtot = 0; + for(;;) { + int nbread = recv(c, buf, sizeof(buf), 0); + if(nbread <= 0) { + if(!nbread) { + fprintf(stderr, "connection closed ... finished\n"); + return 0; + } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { + perror("recv()"); + return -1; + } + continue; + } + + rtot += nbread; + printf("%d bytes received, total = %d\n", nbread, rtot); + } + break; + } +/**************************************/ + } + + return 0; +} diff --git a/test/testserver.c b/test/testserver.c new file mode 100644 index 0000000..00e0ac4 --- /dev/null +++ b/test/testserver.c @@ -0,0 +1,197 @@ +/* + * 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 + * + * 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 . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + if(argc < 3) { + fprintf(stderr, "Usage: %s (A|B)\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; + } + + printf("MODE: %c, listening...\n", toupper(argv[2][0])); + + 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); + + + switch(toupper(argv[2][0])) { +/*************** MODE A ***************/ + case 'A': { + char buf[10000]; + int rtot = 0; + for(;;) { + int nbread = recv(c, buf, sizeof(buf), 0); + if(nbread <= 0) { + if(!nbread) { + fprintf(stderr, "connection closed\n"); + sleep(1); + return 0; + } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { + perror("recv()"); + return -1; + } + continue; + } + + rtot += nbread; + printf("%d bytes received, total = %d\n", nbread, rtot); + + 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; + } + } + } + break; + } +/*************** MODE A ***************/ + case 'B': { + char buf[1234567]; + int rtot = 0; + for(;;) { + int nbread = recv(c, &(buf[rtot]), sizeof(buf) - rtot, 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; + } + + rtot += nbread; + printf("%d bytes received, total = %d\n", nbread, rtot); + unsigned char* end = memchr(buf, '\n', rtot); + if(!end) { + if(rtot >= sizeof(buf)) { + printf("Request too big...\n"); + return -2; + } + } else { + *end = 0; + printf("Got Request: '%s'\n", buf); + break; + } + } + + unsigned int i; + for(i = 0; i= sizeof(buf)) { + break; + } + } + shutdown(c, SHUT_WR); + while(recv(c, buf, 1, 0) > 0); + + break; + } +/**************************************/ + } + + return 0; +} -- cgit v1.2.3