diff --git a/build.sh b/build.sh index 21f9429..694a80b 100755 --- a/build.sh +++ b/build.sh @@ -11,5 +11,5 @@ else c++ -Wall -DNDEBUG -O3 *.cpp $OPTS -o icetunnel2 fi - +echo done diff --git a/common.cpp b/common.cpp index d45d69f..15ecd60 100644 --- a/common.cpp +++ b/common.cpp @@ -9,7 +9,7 @@ void Address::print() const - { printf("%hhu.%hhu.%hhu.%hhu:%hu", ip[0], ip[1], ip[2], ip[3], port); } + { logf("%hhu.%hhu.%hhu.%hhu:%hu", ip[0], ip[1], ip[2], ip[3], port); } Time monotonicTime() { diff --git a/common.h b/common.h index ae39f67..25165b7 100644 --- a/common.h +++ b/common.h @@ -5,16 +5,18 @@ #include +#define logf(...) do { fprintf(stdout, __VA_ARGS__); fflush(stdout); } while(0) + + #define HideDebugMSG(...) do {} while(0) #ifdef NDEBUG # define ShowDebugMSG(...) do {} while(0) #else # define ShowDebugMSG(...) do { \ - printf("DEBUG %16lld %s:%s:%d:", monotonicTime(), __FILE__, __func__, __LINE__); \ - printf(" " __VA_ARGS__); \ - printf("\n"); \ - fflush(stdout); \ + logf("DEBUG %16lld %s:%s:%d:", monotonicTime(), __FILE__, __func__, __LINE__); \ + logf(" " __VA_ARGS__); \ + logf("\n"); \ } while(0) #endif diff --git a/main.cpp b/main.cpp index 9ece38a..bfe0b63 100644 --- a/main.cpp +++ b/main.cpp @@ -6,44 +6,60 @@ #include "tunnel.h" -bool parseAddress(const char *str, Address &address) { +static bool parseAddress(const char *str, Address &address) { address = Address(); sscanf( str, "%hhu.%hhu.%hhu.%hhu:%hu", &address.ip[0], &address.ip[1], &address.ip[2], &address.ip[3], &address.port ); if (!address.port) { - printf("Wrong ip-address: [%s]\n", str); + logf("Wrong ip-address: [%s]\n", str); return false; } return true; } +static bool openLogFile(const char *logfile) { + if (0 != strcmp(logfile, "-")) { + bool eq = stderr == stdout; + stderr = stderr ? fopen(logfile, "a+") : freopen(logfile, "a+", stderr); + if (!stderr) + { logf("Cannot open log file: %s\n", logfile); return false; } + if (!eq && stdout && stdout != stderr) fclose(stdout); + stdout = stderr; + } + return true; +} + + + int main(int argc, char **argv) { // parse args unsigned long long bytesPerSecond = 0; Address localUdp, remoteUdp, localTcp, remoteTcp; - if (argc == 6) { + if (argc == 7) { + bool l = openLogFile(argv[6]); sscanf(argv[2], "%llu", &bytesPerSecond); if (!bytesPerSecond) - { printf("Wrong bytes per second: %llu\n", bytesPerSecond); return 1; } + { logf("Wrong bytes per second: %llu\n", bytesPerSecond); return 1; } parseAddress(argv[3], localUdp); parseAddress(argv[4], remoteUdp); parseAddress(argv[5], localTcp); - if (!localUdp.port || !remoteUdp.port || !localTcp.port) return 1; + if (!localUdp.port || !remoteUdp.port || !localTcp.port || !l) return 1; } else - if (argc == 5) { + if (argc == 6) { + bool l = openLogFile(argv[5]); sscanf(argv[2], "%llu", &bytesPerSecond); if (!bytesPerSecond) - { printf("Wrong bytes per second: %llu\n", bytesPerSecond); return 1; } + { logf("Wrong bytes per second: %llu\n", bytesPerSecond); return 1; } parseAddress(argv[3], localUdp); parseAddress(argv[4], remoteTcp); - if (!localUdp.port || !remoteTcp.port) return 1; + if (!localUdp.port || !remoteTcp.port || !l) return 1; } else { - printf("Usage: %s keyfile bytesPerSecond localUdp:port remoteUdp:port localTcp:port\n", argv[0]); - printf(" or: %s keyfile bytesPerSecond localUdp:port remoteTcp:port\n", argv[0]); + logf("Usage: %s keyfile bytesPerSecond localUdp:port remoteUdp:port localTcp:port logfile|-\n", argv[0]); + logf(" or: %s keyfile bytesPerSecond localUdp:port remoteTcp:port logfile|-\n", argv[0]); return 1; } @@ -51,7 +67,7 @@ int main(int argc, char **argv) { FILE *f = fopen(argv[1], "r"); if (!f) { - printf("Cannot open keyfile: [%s]\n", argv[1]); + logf("Cannot open keyfile: [%s]\n", argv[1]); return 1; } int maxSize = 1024*1024; @@ -60,7 +76,7 @@ int main(int argc, char **argv) { int i = 0; while((i = fgetc(f)) > 0 && c < e) *c++ = i; if (i != EOF || errno) { - printf("Cannot read keyfile: [%s]\n", argv[1]); + logf("Cannot read keyfile: [%s]\n", argv[1]); memset(key, 0, maxSize); free(key); fclose(f); diff --git a/tunnel.cpp b/tunnel.cpp index 51be451..e938942 100644 --- a/tunnel.cpp +++ b/tunnel.cpp @@ -44,19 +44,19 @@ void Tunnel::initSpeed(unsigned long long bytesPerSecond) { bool Tunnel::initUdpServer(const Address &address, const Address &remoteTcpAddress, const char *key) { closeUdpServer(); - printf("Init UDP server at address: "); address.print(); + logf("Init UDP server at address: "); address.print(); if (remoteTcpAddress.port) - { printf(", with remote TCP address: "); remoteTcpAddress.print(); } - printf("\n"); + { logf(", with remote TCP address: "); remoteTcpAddress.print(); } + logf("\n"); if (!crypt.setKey(key)) - { printf("bad key, cancelled\n"); return false; } + { logf("bad key, cancelled\n"); return false; } udpSockId = Socket::udpBind(address); if (udpSockId < 0) - { crypt.resetKey(); printf("cannot bind, cancelled\n"); return false; } + { crypt.resetKey(); logf("cannot bind, cancelled\n"); return false; } - printf("success\n"); + logf("success\n"); this->remoteTcpAddress = remoteTcpAddress; return true; } @@ -64,16 +64,16 @@ bool Tunnel::initUdpServer(const Address &address, const Address &remoteTcpAddre bool Tunnel::initTcpServer(const Address &address, const Address &remoteUdpAddress) { closeTcpServer(); - printf("Init TCP server at address: "); address.print(); printf("\n"); + logf("Init TCP server at address: "); address.print(); logf("\n"); if (udpSockId < 0) - { printf("UDP server was not initialized, cancelled\n"); return false; } + { logf("UDP server was not initialized, cancelled\n"); return false; } tcpSockId = Socket::tcpListen(address); if (tcpSockId < 0) - { printf("cannot listen, cancelled\n"); return false; } + { logf("cannot listen, cancelled\n"); return false; } - printf("success\n"); + logf("success\n"); this->remoteUdpAddress = remoteUdpAddress; return true; } @@ -83,7 +83,7 @@ void Tunnel::closeUdpServer() { if (udpSockId < 0) return; closeTcpServer(); - printf("Close UDP server\n"); + logf("Close UDP server\n"); for(ConnMap::iterator i = connections.begin(); i != connections.end(); ++i) Socket::close(i->second.tcpSockId); connections.clear(); @@ -97,7 +97,7 @@ void Tunnel::closeUdpServer() { void Tunnel::closeTcpServer() { if (tcpSockId < 0) return; - printf("Close TCP server\n"); + logf("Close TCP server\n"); Socket::close(tcpSockId); tcpSockId = -1; remoteUdpAddress = Address(); @@ -140,12 +140,12 @@ bool Tunnel::recvUdpPacket() { id.id = packet.connId; ConnMap::iterator i = connections.find(id); if (i == connections.end() && cmd == CMD_DATA && remoteTcpAddress.port && !closedConnections.count(id)) { - printf("Open incoming connection from UDP: "); + logf("Open incoming connection from UDP: "); id.address.print(); - printf(", id: %u\n", id.id); + logf(", id: %u\n", id.id); int sockId = Socket::tcpConnect(remoteTcpAddress); if (sockId < 0) { - printf("cannot init TCP connection, cancelled\n"); + logf("cannot init TCP connection, cancelled\n"); } else { i = connections.emplace(id, Connection::Args(*this, id, sockId)).first; } @@ -241,9 +241,9 @@ bool Tunnel::iteration() { if (poll.list[0].closed || (tcpSockId >= 0 && poll.list[1].closed)) { if (poll.list[0].closed) - printf("UDP socket closed\n"); + logf("UDP socket closed\n"); if (tcpSockId >= 0 && poll.list[1].closed) - printf("TCP server socket closed\n"); + logf("TCP server socket closed\n"); poll.list.clear(); close(); return false; @@ -261,9 +261,9 @@ bool Tunnel::iteration() { ConnId id; id.address = remoteUdpAddress; id.id = crypt.random(); - printf("Open incoming connection from TCP: "); + logf("Open incoming connection from TCP: "); address.print(); - printf(", id: %u\n", id.id); + logf(", id: %u\n", id.id); Connection &conn = connections.emplace(id, Connection::Args(*this, id, sockId)).first->second; conn.tcpRecvQueue.readFromTcp(); } @@ -324,7 +324,7 @@ bool Tunnel::iteration() { DebugMsg("%u, inactivate tcp", conn.id.id); Socket::close(conn.tcpSockId); conn.tcpSockId = -1; - printf("Close connection, id: %u\n", conn.id.id); + logf("Close connection, id: %u\n", conn.id.id); closedConnections[conn.id] = time + 2*udpSilenceDuration; connections.erase(j); continue; @@ -334,14 +334,14 @@ bool Tunnel::iteration() { DebugMsg("%u, inactivate udp, all data sent", conn.id.id); conn.udpActive = false; termQueue.insert(conn.id); - printf("Close connection, id: %u\n", conn.id.id); + logf("Close connection, id: %u\n", conn.id.id); closedConnections[conn.id] = time + 2*udpSilenceDuration; connections.erase(j); continue; } if (!conn.udpActive && conn.tcpSockId < 0) { - printf("Close connection, id: %u\n", conn.id.id); + logf("Close connection, id: %u\n", conn.id.id); closedConnections[conn.id] = time + 2*udpSilenceDuration; connections.erase(j); continue;