/** ** File ......... MyTcpSocket.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 "config.h" #include "minder.h" #include "MyHandler.h" #include "TestSocket.h" #include "MyTcpSocket.h" using std::string; #ifdef _DEBUG #define DEB(x) x #else #define DEB(x) #endif MyTcpSocket::MyTcpSocket(SocketHandler& h) :SSLSocket(h) { SetLineProtocol(); // we want the OnLine callback } MyTcpSocket::~MyTcpSocket() { } void MyTcpSocket::OnLine(const std::string& line) { std::string cmd; std::string app; std::string id; std::string host; Parse pa(Utility::base64d(line),"_:"); pa.getword(cmd); // 'Hello' / 'Goodbye' app = Utility::base64d(pa.getword()); pa.getword(id); pa.getword(host); // host port_t port = pa.getvalue(); unsigned long hid_from_host = pa.getvalue(); long extra_info = pa.getvalue(); if (cmd != "Hello" && cmd != "Goodbye") { SetCloseAndDelete(true); return; } // get ip sent to us by the minion ipaddr_t ip; u2ip(host,ip); // we ignore whatever ip the minion sends us // we only use the ip from the connection endpoint ipaddr_t ip2 = GetRemoteIP4(); DEB( printf("Incoming: id '%s' %s:%d cmd '%s'\n",id.c_str(),GetRemoteAddress().c_str(),port,cmd.c_str());) if (cmd == "Goodbye") { static_cast(Handler()).unreg_host(app, id, ip2, port, ip); SetCloseAndDelete(true); return; } unsigned long host_id = 0; HOST *h = static_cast(Handler()).reg_host(app, id, ip2, port, ip, host_id, hid_from_host); h -> extra_information = extra_info; { std::string str; std::string msg; std::string ipstr; str = "You_" + id + ":" + GetRemoteAddress() + ":" + Utility::l2string(port) + ":" + Utility::l2string(host_id); msg = Utility::base64(str) + "\n"; if (config[app + "/dbname"].size()) { str = "Database_" + config[app + "/dbname"]; msg += Utility::base64(str) + "\n"; } if (config[app + "/update_host"].size() && config[app + "/update_port"].size() && config[app + "/update_url"].size() ) { str = "Update_" + config[app + "/update_host"]; str += ":" + config[app + "/update_port"]; str += ":" + config[app + "/update_url"]; msg += Utility::base64(str) + "\n"; } static_cast(Handler()).reset_sent(app); for (int i = 0; i < 20; i++) { HOST *p = static_cast(Handler()).GetRandomHOST(app); if (!p -> sent) { l2ip(p -> ip,ipstr); str = "Minion_" + p -> id + ":" + ipstr + ":" + Utility::l2string(p -> port); str += ":" + Utility::l2string(p -> host_id); msg += Utility::base64(str) + "\n"; p -> sent = true; } } msg += Utility::base64("End") + "\n"; Send(msg); } if (app == "viawww" && !h -> connectable) // try again { TestSocket *t = new TestSocket(Handler(),h -> connectable); t -> Open(ip2, h -> extra_information); t -> SetDeleteByHandler(); Handler().Add(t); } } void MyTcpSocket::InitAsServer() { InitializeContext("comb.pem",""); }