//------------------------------------------------------------------------------ void CaptureForm::showEvent(QShowEvent* ) { initCrypto(); }
int real_main(int argc, char* argv[], WinService& service) { #else int main(int argc, char* argv[]) { DaemonService service; #endif try { try { if(!gOpt.parse(argc, argv)) { exit(0); } StringList targets = gOpt.getLogTargets(); for(StringList::const_iterator it = targets.begin(); it != targets.end(); ++it) { cLog.addTarget(*it); } } catch(syntax_error& e) { std::cerr << e << std::endl; gOpt.printUsage(); exit(-1); } cLog.msg(Log::PRIO_NOTICE) << "anytun started..."; gOpt.parse_post(); // print warnings // daemonizing has to done before any thread gets started service.initPrivs(gOpt.getUsername(), gOpt.getGroupname()); if(gOpt.getDaemonize()) { service.daemonize(); } OptionNetwork net = gOpt.getIfconfigParam(); TunDevice dev(gOpt.getDevName(), gOpt.getDevType(), net.net_addr, net.prefix_length); cLog.msg(Log::PRIO_NOTICE) << "dev opened - name '" << dev.getActualName() << "', node '" << dev.getActualNode() << "'"; cLog.msg(Log::PRIO_NOTICE) << "dev type is '" << dev.getTypeString() << "'"; SysExec* postup_script = NULL; if(gOpt.getPostUpScript() != "") { cLog.msg(Log::PRIO_NOTICE) << "executing post-up script '" << gOpt.getPostUpScript() << "'"; StringVector args = boost::assign::list_of(dev.getActualName())(dev.getActualNode()); postup_script = new SysExec(gOpt.getPostUpScript(), args); } if(gOpt.getChrootDir() != "") { try { service.chroot(gOpt.getChrootDir()); } catch(const std::runtime_error& e) { cLog.msg(Log::PRIO_WARNING) << "ignoring chroot error: " << e.what(); } } service.dropPrivs(); // this has to be called before the first thread is started gSignalController.init(service); gResolver.init(); boost::thread(boost::bind(&TunDevice::waitUntilReady,&dev)); if(postup_script) { boost::thread(boost::bind(&SysExec::waitAndDestroy,postup_script)); } initCrypto(); PacketSource* src = new UDPPacketSource(gOpt.getLocalAddr(), gOpt.getLocalPort()); if(gOpt.getRemoteAddr() != "") { gResolver.resolveUdp(gOpt.getRemoteAddr(), gOpt.getRemotePort(), boost::bind(createConnectionResolver, _1, gOpt.getSeqWindowSize(), gOpt.getMux()), boost::bind(createConnectionError, _1), gOpt.getResolvAddrType()); } HostList connect_to = gOpt.getRemoteSyncHosts(); #ifndef NO_ROUTING NetworkList routes = gOpt.getRoutes(); NetworkList::const_iterator rit; for(rit = routes.begin(); rit != routes.end(); ++rit) { NetworkAddress addr(rit->net_addr); NetworkPrefix prefix(addr, static_cast<uint8_t>(rit->prefix_length)); gRoutingTable.addRoute(prefix, gOpt.getMux()); } if(connect_to.begin() == connect_to.end() || gOpt.getDevType()!="tun") { cLog.msg(Log::PRIO_NOTICE) << "No sync/control host defined or not a tun device. Disabling multi connection support (routing)"; disableRouting=true; } #endif #ifndef ANYTUN_NOSYNC boost::thread* syncListenerThread = NULL; if(gOpt.getLocalSyncPort() != "") { syncListenerThread = new boost::thread(boost::bind(syncListener)); if(syncListenerThread) syncListenerThread->detach(); } boost::thread_group connectThreads; for(HostList::const_iterator it = connect_to.begin() ; it != connect_to.end(); ++it) { connectThreads.create_thread(boost::bind(syncConnector, *it)); } #endif // wait for packet source to finish in a seperate thread in order // to be still able to process signals while waiting boost::thread(boost::bind(startSendRecvThreads, &dev, src)); int ret = gSignalController.run(); // TODO: stop all threads and cleanup // // if(src) // delete src; // if(connTo) // delete connTo; return ret; } catch(std::runtime_error& e) { cLog.msg(Log::PRIO_ERROR) << "uncaught runtime error, exiting: " << e.what(); if(!service.isDaemonized()) { std::cout << "uncaught runtime error, exiting: " << e.what() << std::endl; } } catch(std::exception& e) { cLog.msg(Log::PRIO_ERROR) << "uncaught exception, exiting: " << e.what(); if(!service.isDaemonized()) { std::cout << "uncaught exception, exiting: " << e.what() << std::endl; } } return -1; }