2009-10-11 00:04:03 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <task.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
STACK = 32768
|
|
|
|
};
|
|
|
|
|
2012-08-01 05:00:00 +04:00
|
|
|
static int verbose;
|
|
|
|
|
2009-10-11 00:04:03 +04:00
|
|
|
char *server;
|
|
|
|
int port;
|
|
|
|
void proxytask(void*);
|
|
|
|
void rwtask(void*);
|
|
|
|
|
|
|
|
int*
|
|
|
|
mkfd2(int fd1, int fd2)
|
|
|
|
{
|
|
|
|
int *a;
|
2014-08-03 20:28:37 +04:00
|
|
|
|
2009-10-11 00:04:03 +04:00
|
|
|
a = malloc(2*sizeof a[0]);
|
|
|
|
if(a == 0){
|
|
|
|
fprintf(stderr, "out of memory\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
a[0] = fd1;
|
|
|
|
a[1] = fd2;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
taskmain(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int cfd, fd;
|
|
|
|
int rport;
|
2012-08-01 06:00:00 +04:00
|
|
|
char remote[46];
|
2014-08-03 20:28:37 +04:00
|
|
|
|
2009-10-11 00:04:03 +04:00
|
|
|
if(argc != 4){
|
|
|
|
fprintf(stderr, "usage: tcpproxy localport server remoteport\n");
|
|
|
|
taskexitall(1);
|
|
|
|
}
|
|
|
|
server = argv[2];
|
|
|
|
port = atoi(argv[3]);
|
|
|
|
|
|
|
|
if((fd = netannounce(TCP, 0, atoi(argv[1]))) < 0){
|
|
|
|
fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno));
|
|
|
|
taskexitall(1);
|
|
|
|
}
|
fix warnings reported by Coverity
fd.c:230 negative_returns: fd is passed to a parameter that cannot be negative.
net.c:65 check_return: Calling setsockopt(fd, 1, 2, (char *)&n, 4U) without checking return value.
net.c:77 check_return: Calling fdnoblock(fd) without checking return value.
net.c:112 check_return: Calling fdnoblock(cfd) without checking return value.
net.c:114 check_return: Calling setsockopt(cfd, IPPROTO_TCP, 1, (char *)&one, 4U) without checking return value.
net.c:144 leaked_storage: Variable result going out of scope leaks the storage it points to.
net.c:168 check_return: Calling fdnoblock(fd) without checking return value.
net.c:173 check_return: Calling setsockopt(fd, 1, 6, &n, 4U) without checking return value.
net.c:206 check_return: Calling getsockopt(fd, 1, 4, (void *)&n, &sn) without checking return value.
task.c:59 negative_returns: fd is passed to a parameter that cannot be negative.
tcpload.c:50 Calling fdread without checking return value (as is done elsewhere 4 out of 5 times).
tcpproxy.c:51 check_return: Calling fdnoblock(fd) without checking return value.
2015-06-25 00:16:58 +03:00
|
|
|
if(fdnoblock(fd) < 0){
|
|
|
|
fprintf(stderr, "fdnoblock\n");
|
|
|
|
taskexitall(1);
|
|
|
|
}
|
2009-10-11 00:04:03 +04:00
|
|
|
while((cfd = netaccept(fd, remote, &rport)) >= 0){
|
2012-08-01 05:00:00 +04:00
|
|
|
if(verbose)
|
|
|
|
fprintf(stderr, "connection from %s:%d\n", remote, rport);
|
2012-08-01 03:00:00 +04:00
|
|
|
taskcreate(proxytask, (void*)(uintptr_t)cfd, STACK);
|
2009-10-11 00:04:03 +04:00
|
|
|
}
|
2015-06-18 22:31:54 +03:00
|
|
|
close(fd);
|
2009-10-11 00:04:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proxytask(void *v)
|
|
|
|
{
|
|
|
|
int fd, remotefd;
|
|
|
|
|
2012-08-01 03:00:00 +04:00
|
|
|
fd = (int)(uintptr_t)v;
|
2009-10-11 00:04:03 +04:00
|
|
|
if((remotefd = netdial(TCP, server, port)) < 0){
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-01 05:00:00 +04:00
|
|
|
|
|
|
|
if(verbose)
|
|
|
|
fprintf(stderr, "connected to %s:%d\n", server, port);
|
2009-10-11 00:04:03 +04:00
|
|
|
|
|
|
|
taskcreate(rwtask, mkfd2(fd, remotefd), STACK);
|
|
|
|
taskcreate(rwtask, mkfd2(remotefd, fd), STACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rwtask(void *v)
|
|
|
|
{
|
|
|
|
int *a, rfd, wfd, n;
|
|
|
|
char buf[2048];
|
|
|
|
|
|
|
|
a = v;
|
|
|
|
rfd = a[0];
|
|
|
|
wfd = a[1];
|
|
|
|
free(a);
|
2014-08-03 20:28:37 +04:00
|
|
|
|
2009-10-11 00:04:03 +04:00
|
|
|
while((n = fdread(rfd, buf, sizeof buf)) > 0)
|
|
|
|
fdwrite(wfd, buf, n);
|
|
|
|
shutdown(wfd, SHUT_WR);
|
|
|
|
close(rfd);
|
|
|
|
}
|
|
|
|
|