From 675adabaedd6e989ad1849727a9928695f30e0b4 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 11 Apr 2008 18:18:20 +0000 Subject: added control host option added post up script --- anytun.cpp | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'anytun.cpp') diff --git a/anytun.cpp b/anytun.cpp index 9156de9..a309f67 100644 --- a/anytun.cpp +++ b/anytun.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include // for ENOMEM @@ -394,21 +395,34 @@ void daemonize() umask(027); } -void writePid(string const& pidFilename) +int execScript(string const& script, string const& ifname) { + pid_t pid; + pid = fork(); + if(!pid) { + int fd; + for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors + close(fd); + fd=open("/dev/null",O_RDWR); // stdin + dup(fd); // stdout + dup(fd); // stderr + return execl("/bin/sh", "/bin/sh", script.c_str(), ifname.c_str(), NULL); + } + int status = 0; + waitpid(pid, &status, 0); + return status; } int main(int argc, char* argv[]) { // std::cout << "anytun - secure anycast tunneling protocol" << std::endl; - if(!gOpt.parse(argc, argv)) - { + if(!gOpt.parse(argc, argv)) { gOpt.printUsage(); exit(-1); } - + cLog.msg(Log::PRIO_NOTICE) << "anytun started..."; - + std::ofstream pidFile; if(gOpt.getPidFile() != "") { pidFile.open(gOpt.getPidFile().c_str()); @@ -416,7 +430,7 @@ int main(int argc, char* argv[]) std::cout << "can't open pid file" << std::endl; } } - + std::string dev_type(gOpt.getDevType()); TunDevice dev(gOpt.getDevName().c_str(), dev_type=="" ? NULL : dev_type.c_str(), gOpt.getIfconfigParamLocal() =="" ? NULL : gOpt.getIfconfigParamLocal().c_str(), @@ -424,6 +438,10 @@ int main(int argc, char* argv[]) cLog.msg(Log::PRIO_NOTICE) << "dev created (opened)"; cLog.msg(Log::PRIO_NOTICE) << "dev opened - actual name is '" << dev.getActualName() << "'"; cLog.msg(Log::PRIO_NOTICE) << "dev type is '" << dev.getTypeString() << "'"; + if(gOpt.getPostUpScript() != "") { + int postup_ret = execScript(gOpt.getPostUpScript(), dev.getActualName()); + cLog.msg(Log::PRIO_NOTICE) << "post up script '" << gOpt.getPostUpScript() << "' returned " << postup_ret; + } if(gOpt.getChroot()) chrootAndDrop(gOpt.getChrootDir(), gOpt.getUsername()); @@ -444,7 +462,7 @@ int main(int argc, char* argv[]) src = new UDPPacketSource(gOpt.getLocalPort()); else src = new UDPPacketSource(gOpt.getLocalAddr(), gOpt.getLocalPort()); - + ConnectionList cl; ConnectToList connect_to = gOpt.getConnectTo(); SyncQueue queue; @@ -468,11 +486,10 @@ int main(int argc, char* argv[]) pthread_create(&syncListenerThread, NULL, syncListener, &p); std::list connectThreads; - for(ConnectToList::iterator it = connect_to.begin() ;it != connect_to.end(); ++it) - { - connectThreads.push_back(pthread_t()); - ThreadParam * point = new ThreadParam(dev, *src, cl, queue,*it); - pthread_create(& connectThreads.back(), NULL, syncConnector, point); + for(ConnectToList::iterator it = connect_to.begin() ;it != connect_to.end(); ++it) { + connectThreads.push_back(pthread_t()); + ThreadParam * point = new ThreadParam(dev, *src, cl, queue,*it); + pthread_create(& connectThreads.back(), NULL, syncConnector, point); } int ret = sig.run(); -- cgit v1.2.3