00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifdef _WIN32
00024 #pragma warning(disable:4786)
00025 #endif
00026 #include "sockets-config.h"
00027 #ifdef HAVE_OPENSSL
00028 #include "MinderHandler.h"
00029 #include "Utility.h"
00030 #include "MinderSocket.h"
00031
00032 #ifdef SOCKETS_NAMESPACE
00033 namespace SOCKETS_NAMESPACE {
00034 #endif
00035
00036 #ifdef _DEBUG
00037 #define DEB(x) x
00038 #else
00039 #define DEB(x)
00040 #endif
00041
00042
00043
00044 #ifdef _WIN32
00045 char MinderSocket::g_UpdateHost[256];
00046 port_t MinderSocket::g_UpdatePort = 0;
00047 char MinderSocket::g_UpdateUrl[256];
00048 #endif
00049
00050
00051 MinderSocket::MinderSocket(ISocketHandler& h,const std::string& app)
00052 :TcpSocket(h)
00053 ,m_app(app)
00054 ,my_ip(0)
00055 ,my_port(0)
00056 ,m_function("")
00057 ,m_extra_info(0)
00058 {
00059 SetLineProtocol();
00060 }
00061
00062
00063 MinderSocket::~MinderSocket()
00064 {
00065 }
00066
00067
00068 void MinderSocket::OnLine(const std::string& line)
00069 {
00070 std::string cmd;
00071 std::string id;
00072 std::string ipstr;
00073 port_t port;
00074 ipaddr_t ip;
00075 int max = GetMaxConnections();
00076 Parse pa(Utility::base64d(line),"_:");
00077
00078 pa.getword(cmd);
00079 static_cast<MinderHandler&>(Handler()).SetMinderTime(time(NULL));
00080
00081 if (cmd == "You")
00082 {
00083 pa.getword(id);
00084 pa.getword(ipstr);
00085 port = (port_t)pa.getvalue();
00086 unsigned long hostid = pa.getvalue();
00087
00088 Utility::u2ip(ipstr,ip);
00089
00090 DEB( fprintf(stderr, " received my id '%s' %s:%d - %lu\n",id.c_str(),ipstr.c_str(),port,hostid);)
00091
00092 my_ip = ip;
00093 my_port = port;
00094 static_cast<MinderHandler&>(Handler()).SetMyIpPort(my_ip, my_port);
00095 DEB( fprintf(stderr, "ignoring %s:%d\n",ipstr.c_str(),port);)
00096 static_cast<MinderHandler&>(Handler()).SetExternalAddress(ipstr);
00097 if (static_cast<MinderHandler&>(Handler()).GetHostId() == 0)
00098 {
00099 static_cast<MinderHandler&>(Handler()).SetHostId(hostid);
00100 }
00101 }
00102 else
00103 #ifdef _WIN32
00104 if (cmd == "Update")
00105 {
00106 std::string host = pa.getword();
00107 port_t port = (port_t)pa.getvalue();
00108 std::string url = pa.getword();
00109 strncpy(g_UpdateHost, host.c_str(), 255);
00110 g_UpdatePort = port;
00111 strncpy(g_UpdateUrl, url.c_str(), 255);
00112 }
00113 else
00114 #endif
00115 if (cmd == "Minion")
00116 {
00117 pa.getword(id);
00118 pa.getword(ipstr);
00119 port = (port_t)pa.getvalue();
00120 long remote_host_id = pa.getvalue();
00121
00122 Utility::u2ip(ipstr,ip);
00123
00124 if (ip == my_ip && port == my_port)
00125 {
00126 return;
00127 }
00128 max = (max == 0) ? 4 : max;
00129
00130 if (!static_cast<MinderHandler&>(Handler()).FindMinion(id) )
00131 {
00132 if (0 && static_cast<MinderHandler&>(Handler()).Count() < max)
00133 {
00134 DEB( fprintf(stderr, " connect to %s:%d\n",ipstr.c_str(),port);)
00135
00136 MinionSocket *tmp = CreateMinionSocket(id,ip,port);
00137 tmp -> SetMyIpPort(my_ip,my_port);
00138 if (tmp -> Open(ip,port))
00139 {
00140 tmp -> SetDeleteByHandler(true);
00141 Handler().Add(tmp);
00142
00143 tmp -> SendHello("Hello");
00144 }
00145 else
00146 if (tmp -> Connecting())
00147 {
00148 tmp -> SetDeleteByHandler(true);
00149
00150 Handler().Add(tmp);
00151 }
00152 else
00153 {
00154 delete tmp;
00155 }
00156 }
00157 else
00158 {
00159 static_cast<MinderHandler&>(Handler()).AddHost(ip,port,id,remote_host_id);
00160 }
00161 }
00162 else
00163 {
00164 DEB( fprintf(stderr, " id found\n");)
00165 }
00166 }
00167 else
00168 if (cmd == "End")
00169 {
00170 SetCloseAndDelete( true );
00171 }
00172 }
00173
00174
00175 void MinderSocket::SendHello()
00176 {
00177 std::string msg = m_function + "_";
00178
00179 if (!m_app.size())
00180 {
00181 SetCloseAndDelete();
00182 return;
00183 }
00184 msg += Utility::base64(m_app);
00185 msg += ":" + static_cast<MinderHandler&>(Handler()).GetID();
00186 msg += ":" + local_ip;
00187 msg += ":" + Utility::l2string(local_port);
00188 msg += ":" + Utility::l2string(static_cast<MinderHandler&>(Handler()).GetHostId());
00189 msg += ":" + Utility::l2string(m_extra_info);
00190 Send( Utility::base64(msg) + "\n" );
00191 }
00192
00193
00194 void MinderSocket::OnConnect()
00195 {
00196 SendHello();
00197 }
00198
00199
00200 #ifdef SOCKETS_NAMESPACE
00201 }
00202 #endif
00203
00204 #endif // HAVE_OPENSSL