1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
26 #include "helperfuncs.h"
29 enum ServerState { LISTENER, CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
37 std::vector<TreeServer*> Children;
38 std::string ServerName;
39 std::string ServerDesc;
40 std::string VersionString;
43 TreeSocket* Socket; // for directly connected servers this points at the socket object
53 UserCount = OperCount = 0;
56 TreeServer(std::string Name, std::string Desc) : ServerName(Name), ServerDesc(Desc)
60 UserCount = OperCount = 0;
63 TreeServer(std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock) : Parent(Above), ServerName(Name), ServerDesc(Desc), Socket(Sock)
66 UserCount = OperCount = 0;
69 void AddChild(TreeServer* Child)
71 Children.push_back(Child);
74 bool DelChild(TreeServer* Child)
76 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
98 /* $ModDesc: Povides a spanning tree server link protocol */
102 TreeServer *TreeRoot;
103 std::vector<Link> LinkBlocks;
105 class TreeSocket : public InspSocket
108 std::string in_buffer;
109 ServerState LinkState;
113 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
114 : InspSocket(host, port, listening, maxtime)
116 Srv->Log(DEBUG,"Create new");
118 this->LinkState = LISTENER;
121 TreeSocket(int newfd)
124 this->LinkState = WAIT_AUTH_1;
127 virtual bool OnConnected()
132 virtual void OnError(InspSocketError e)
136 virtual int OnDisconnect()
141 virtual bool OnDataReady()
143 char* data = this->Read();
146 this->in_buffer += data;
147 while (in_buffer.find("\n") != std::string::npos)
149 char* line = (char*)in_buffer.c_str();
150 std::string ret = "";
151 while ((*line != '\n') && (strlen(line)))
156 if ((*line == '\n') || (*line == '\r'))
159 if (!this->ProcessLine(ret))
165 return (data != NULL);
168 int WriteLine(std::string line)
170 return this->Write(line + "\r\n");
173 bool Outbound_Reply_Server(std::deque<std::string> params)
175 if (params.size() < 4)
177 std::string servername = params[0];
178 std::string password = params[1];
179 int hops = atoi(params[2].c_str());
182 this->WriteLine("ERROR :Server too far away for authentication");
185 std::string description = params[3];
186 Srv->SendToModeMask("o",WM_AND,"outbound-server-replied: name='"+servername+"' pass='"+password+"' description='"+description+"'");
187 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
189 if ((x->Name == servername) && (x->RecvPass == password))
191 // Begin the sync here. this kickstarts the
192 // other side, waiting in WAIT_AUTH_2 state,
193 // into starting their burst, as it shows
195 this->LinkState = CONNECTED;
196 // we should add the details of this server now
197 // to the servers tree, as a child of the root
199 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
200 TreeRoot->AddChild(Node);
204 this->WriteLine("ERROR :Invalid credentials");
208 bool Inbound_Server(std::deque<std::string> params)
210 if (params.size() < 4)
212 std::string servername = params[0];
213 std::string password = params[1];
214 int hops = atoi(params[2].c_str());
217 this->WriteLine("ERROR :Server too far away for authentication");
220 std::string description = params[3];
221 Srv->SendToModeMask("o",WM_AND,"inbound-server: name='"+servername+"' pass='"+password+"' description='"+description+"'");
222 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
224 if ((x->Name == servername) && (x->RecvPass == password))
226 // this is good. Send our details: Our server name and description and hopcount of 0,
227 // along with the sendpass from this block.
228 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
229 // move to the next state, we are now waiting for THEM.
230 this->LinkState = WAIT_AUTH_2;
234 this->WriteLine("ERROR :Invalid credentials");
238 std::deque<std::string> Split(std::string line)
240 std::deque<std::string> n;
241 std::stringstream s(line);
242 std::string param = "";
248 if ((param.c_str()[0] == ':') && (item))
250 char* str = (char*)param.c_str();
259 param = param + " " + append;
269 bool ProcessLine(std::string line)
271 Srv->SendToModeMask("o",WM_AND,"inbound-line: '"+line+"'");
273 std::deque<std::string> params = this->Split(line);
274 std::string command = "";
275 std::string prefix = "";
276 if ((params[0].c_str())[0] == ':')
278 prefix = params.pop_front();
279 command = params.pop_front();
284 command = params.pop_front();
287 switch (this->LinkState)
290 // Waiting for SERVER command from remote server. Server initiating
291 // the connection sends the first SERVER command, listening server
292 // replies with theirs if its happy, then if the initiator is happy,
293 // it starts to send its net sync, which starts the merge, otherwise
294 // it sends an ERROR.
295 if (command == "SERVER")
297 return this->Inbound_Server(params);
301 // Waiting for start of other side's netmerge to say they liked our
303 if (command == "SERVER")
305 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
312 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
316 if (command == "SERVER")
318 // another server we connected to, which was in WAIT_AUTH_1 state,
319 // has just sent us their credentials. If we get this far, theyre
320 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
321 // if we're happy with this, we should send our netburst which
322 // kickstarts the merge.
323 return this->Outbound_Reply_Server(params);
327 // This is the 'authenticated' state, when all passwords
328 // have been exchanged and anything past this point is taken
336 virtual void OnTimeout()
340 virtual void OnClose()
344 virtual int OnIncomingConnection(int newsock, char* ip)
346 TreeSocket* s = new TreeSocket(newsock);
352 void tree_handle_connect(char **parameters, int pcnt, userrec *user)
354 std::string addr = parameters[0];
355 TreeSocket* sock = new TreeSocket(addr,80,false,10);
356 Srv->AddSocket(sock);
359 class ModuleSpanningTree : public Module
361 std::vector<TreeSocket*> Bindings;
365 void ReadConfiguration(bool rebind)
369 for (int j =0; j < Conf->Enumerate("bind"); j++)
371 std::string Type = Conf->ReadValue("bind","type",j);
372 std::string IP = Conf->ReadValue("bind","address",j);
373 long Port = Conf->ReadInteger("bind","port",j,true);
374 if (Type == "servers")
380 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
381 if (listener->GetState() == I_LISTENING)
383 Srv->AddSocket(listener);
384 Bindings.push_back(listener);
388 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
396 for (int j =0; j < Conf->Enumerate("link"); j++)
399 L.Name = Conf->ReadValue("link","name",j);
400 L.IPAddr = Conf->ReadValue("link","ipaddr",j);
401 L.Port = Conf->ReadInteger("link","port",j,true);
402 L.SendPass = Conf->ReadValue("link","sendpass",j);
403 L.RecvPass = Conf->ReadValue("link","recvpass",j);
404 LinkBlocks.push_back(L);
405 log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
412 Conf = new ConfigReader;
415 // Create the root of the tree
416 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
418 ReadConfiguration(true);
421 void HandleLinks(char** parameters, int pcnt, userrec* user)
426 void HandleLusers(char** parameters, int pcnt, userrec* user)
431 void HandleMap(char** parameters, int pcnt, userrec* user)
436 int HandleSquit(char** parameters, int pcnt, userrec* user)
441 int HandleConnect(char** parameters, int pcnt, userrec* user)
446 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
448 if (command == "CONNECT")
450 return this->HandleConnect(parameters,pcnt,user);
452 else if (command == "SQUIT")
454 return this->HandleSquit(parameters,pcnt,user);
456 else if (command == "MAP")
458 this->HandleMap(parameters,pcnt,user);
461 else if (command == "LUSERS")
463 this->HandleLusers(parameters,pcnt,user);
466 else if (command == "LINKS")
468 this->HandleLinks(parameters,pcnt,user);
474 virtual ~ModuleSpanningTree()
479 virtual Version GetVersion()
481 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
486 class ModuleSpanningTreeFactory : public ModuleFactory
489 ModuleSpanningTreeFactory()
493 ~ModuleSpanningTreeFactory()
497 virtual Module * CreateModule()
499 return new ModuleSpanningTree;
505 extern "C" void * init_module( void )
507 return new ModuleSpanningTreeFactory;