/** ** File ......... minder.cpp ** Published .... 2004-04-18 ** Author ....... grymse@alhem.net **/ /* Copyright (C) 2004 Anders Hedstrom This program 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 2 of the License, or (at your option) any later version. This program 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 this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef _WIN32 #pragma warning(disable:4786) #endif #include #include #include #include #include #include #include #include #include "config.h" #include "MyTcpSocket.h" #include "RedirWwwSocket.h" #include "MyHandler.h" #ifdef _DEBUG #define DEB(x) x #else #define DEB(x) #endif // this little program keeps tabs on all hosts in a game // adjustable keep-alive frequency #ifdef WIN32 WSADATA libmibWSAdata; class X { public: X() { SocketStartup(); } ~X() { SocketCleanup(); } }; X x; #endif static int quit = 0; #ifndef _WIN32 void sigint(int s) /* save+quit */ { DEB( printf("sigint\n");) quit++; } void sighup(int s) /* quit */ { DEB( printf("sighup\n");) quit++; } void sigusr1(int s) /* save */ { } void sigusr2(int s) /* reset all */ { } void sigpipe(int s) { } void siginit(void) { signal(SIGINT, sigint); signal(SIGHUP, sighup); signal(SIGUSR1, sigusr1); signal(SIGUSR2, sigusr2); signal(SIGPIPE, sigpipe); } #endif int main(int argc,char *argv[]) { // open listen socket // commands: // init (state = startup) // online (state = online) // keepalive // exit // route (traceroute from host to minder) // sent as: // http://minder.alhem.net:9696/init?ip&port // http://minder.alhem.net:9696/route?10.0.0.1&10.0.0.2&... time_t t = time(NULL); MyHandler h; ListenSocket l(h); ListenSocket l2(h); port_t redir_port = 0; #ifndef _WIN32 siginit(); #endif // default values config["minder_port"] = "9696"; config["next_host_id"] = "1"; // config["redir_port"] = "9666"; parse_config(argc,argv,"minder.cfg"); // config["progname"] = argv[0]; if (l.Bind(atoi(config["minder_port"].c_str()), 20) != 0) { DEB( printf("Bind() failed\n");) exit(-1); } redir_port = atoi(config["redir_port"].c_str()); if (redir_port > 0) { if (l2.Bind(redir_port, 20)) { DEB( perror("Bind() failed");) exit(-2); } } h.Add(&l); if (redir_port > 0) { h.Add(&l2); } while (!quit) { h.Select(1,0); if (time(NULL) - t > 60) { h.CheckTimeout(); } } return 0; }