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;
110 std::string InboundServerName;
111 std::string InboundDescription;
115 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
116 : InspSocket(host, port, listening, maxtime)
118 Srv->Log(DEBUG,"Create new listening");
120 this->LinkState = LISTENER;
123 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
124 : InspSocket(host, port, listening, maxtime)
126 Srv->Log(DEBUG,"Create new outbound");
128 this->LinkState = CONNECTING;
131 TreeSocket(int newfd)
134 Srv->Log(DEBUG,"Associate new inbound");
135 this->LinkState = WAIT_AUTH_1;
138 virtual bool OnConnected()
140 if (this->LinkState == CONNECTING)
142 // we should send our details here.
143 // if the other side is satisfied, they send theirs.
144 // we do not need to change state here.
145 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
147 if (x->Name == this->myhost)
149 // found who we're supposed to be connecting to, send the neccessary gubbins.
150 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
155 log(DEBUG,"Outbound connection ERROR: Could not find the right link block!");
159 virtual void OnError(InspSocketError e)
163 virtual int OnDisconnect()
168 void DoBurst(TreeServer* s)
170 log(DEBUG,"Beginning network burst");
171 this->WriteLine("BURST");
172 this->WriteLine("ENDBURST");
175 virtual bool OnDataReady()
177 char* data = this->Read();
180 this->in_buffer += data;
181 while (in_buffer.find("\n") != std::string::npos)
183 char* line = (char*)in_buffer.c_str();
184 std::string ret = "";
185 while ((*line != '\n') && (strlen(line)))
190 if ((*line == '\n') || (*line == '\r'))
193 if (!this->ProcessLine(ret))
199 return (data != NULL);
202 int WriteLine(std::string line)
204 return this->Write(line + "\r\n");
207 bool Error(std::deque<std::string> params)
209 if (params.size() < 1)
211 std::string Errmsg = params[0];
212 std::string SName = myhost;
213 if (InboundServerName != "")
215 SName = InboundServerName;
217 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
218 // we will return false to cause the socket to close.
222 bool Outbound_Reply_Server(std::deque<std::string> params)
224 if (params.size() < 4)
226 std::string servername = params[0];
227 std::string password = params[1];
228 int hops = atoi(params[2].c_str());
231 this->WriteLine("ERROR :Server too far away for authentication");
234 std::string description = params[3];
235 Srv->SendToModeMask("o",WM_AND,"outbound-server-replied: name='"+servername+"' pass='"+password+"' description='"+description+"'");
236 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
238 if ((x->Name == servername) && (x->RecvPass == password))
240 // Begin the sync here. this kickstarts the
241 // other side, waiting in WAIT_AUTH_2 state,
242 // into starting their burst, as it shows
244 this->LinkState = CONNECTED;
245 // we should add the details of this server now
246 // to the servers tree, as a child of the root
248 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
249 TreeRoot->AddChild(Node);
254 this->WriteLine("ERROR :Invalid credentials");
258 bool Inbound_Server(std::deque<std::string> params)
260 if (params.size() < 4)
262 std::string servername = params[0];
263 std::string password = params[1];
264 int hops = atoi(params[2].c_str());
267 this->WriteLine("ERROR :Server too far away for authentication");
270 std::string description = params[3];
271 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
273 if ((x->Name == servername) && (x->RecvPass == password))
275 Srv->SendOpers("*** Verified incoming server connection from "+servername+"["+this->GetIP()+"] ("+description+")");
276 this->InboundServerName = servername;
277 this->InboundDescription = description;
278 // this is good. Send our details: Our server name and description and hopcount of 0,
279 // along with the sendpass from this block.
280 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
281 // move to the next state, we are now waiting for THEM.
282 this->LinkState = WAIT_AUTH_2;
286 this->WriteLine("ERROR :Invalid credentials");
290 std::deque<std::string> Split(std::string line)
292 std::deque<std::string> n;
293 std::stringstream s(line);
294 std::string param = "";
300 if ((param.c_str()[0] == ':') && (item))
302 char* str = (char*)param.c_str();
312 param = param + " " + append;
322 bool ProcessLine(std::string line)
324 Srv->SendToModeMask("o",WM_AND,"inbound-line: '"+line+"'");
326 std::deque<std::string> params = this->Split(line);
327 std::string command = "";
328 std::string prefix = "";
329 if ((params[0].c_str())[0] == ':')
343 switch (this->LinkState)
348 // Waiting for SERVER command from remote server. Server initiating
349 // the connection sends the first SERVER command, listening server
350 // replies with theirs if its happy, then if the initiator is happy,
351 // it starts to send its net sync, which starts the merge, otherwise
352 // it sends an ERROR.
353 if (command == "SERVER")
355 return this->Inbound_Server(params);
357 else if (command == "ERROR")
359 return this->Error(params);
363 // Waiting for start of other side's netmerge to say they liked our
365 if (command == "SERVER")
367 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
371 else if (command == "BURST")
373 this->LinkState = CONNECTED;
374 Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
375 TreeRoot->AddChild(Node);
378 else if (command == "ERROR")
380 return this->Error(params);
385 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
389 if (command == "SERVER")
391 // another server we connected to, which was in WAIT_AUTH_1 state,
392 // has just sent us their credentials. If we get this far, theyre
393 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
394 // if we're happy with this, we should send our netburst which
395 // kickstarts the merge.
396 return this->Outbound_Reply_Server(params);
400 // This is the 'authenticated' state, when all passwords
401 // have been exchanged and anything past this point is taken
409 virtual void OnTimeout()
411 if (this->LinkState = CONNECTING)
413 Srv->SendOpers("*** CONNECT: Connection to "+myhost+" timed out.");
417 virtual void OnClose()
421 virtual int OnIncomingConnection(int newsock, char* ip)
423 TreeSocket* s = new TreeSocket(newsock);
429 class ModuleSpanningTree : public Module
431 std::vector<TreeSocket*> Bindings;
435 void ReadConfiguration(bool rebind)
439 for (int j =0; j < Conf->Enumerate("bind"); j++)
441 std::string Type = Conf->ReadValue("bind","type",j);
442 std::string IP = Conf->ReadValue("bind","address",j);
443 long Port = Conf->ReadInteger("bind","port",j,true);
444 if (Type == "servers")
450 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
451 if (listener->GetState() == I_LISTENING)
453 Srv->AddSocket(listener);
454 Bindings.push_back(listener);
458 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
466 for (int j =0; j < Conf->Enumerate("link"); j++)
469 L.Name = Conf->ReadValue("link","name",j);
470 L.IPAddr = Conf->ReadValue("link","ipaddr",j);
471 L.Port = Conf->ReadInteger("link","port",j,true);
472 L.SendPass = Conf->ReadValue("link","sendpass",j);
473 L.RecvPass = Conf->ReadValue("link","recvpass",j);
474 LinkBlocks.push_back(L);
475 log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
482 Conf = new ConfigReader;
485 // Create the root of the tree
486 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
488 ReadConfiguration(true);
491 void HandleLinks(char** parameters, int pcnt, userrec* user)
496 void HandleLusers(char** parameters, int pcnt, userrec* user)
501 void HandleMap(char** parameters, int pcnt, userrec* user)
506 int HandleSquit(char** parameters, int pcnt, userrec* user)
511 int HandleConnect(char** parameters, int pcnt, userrec* user)
513 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
515 if (Srv->MatchText(x->Name.c_str(),parameters[0]))
517 WriteServ(user->fd,"NOTICE %s :*** CONNECT: Connecting to server: %s (%s:%d)",user->nick,x->Name.c_str(),x->IPAddr.c_str(),x->Port);
518 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
519 Srv->AddSocket(newsocket);
523 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No matching server could be found in the config file.",user->nick);
527 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
529 if (command == "CONNECT")
531 return this->HandleConnect(parameters,pcnt,user);
533 else if (command == "SQUIT")
535 return this->HandleSquit(parameters,pcnt,user);
537 else if (command == "MAP")
539 this->HandleMap(parameters,pcnt,user);
542 else if (command == "LUSERS")
544 this->HandleLusers(parameters,pcnt,user);
547 else if (command == "LINKS")
549 this->HandleLinks(parameters,pcnt,user);
555 virtual ~ModuleSpanningTree()
560 virtual Version GetVersion()
562 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
567 class ModuleSpanningTreeFactory : public ModuleFactory
570 ModuleSpanningTreeFactory()
574 ~ModuleSpanningTreeFactory()
578 virtual Module * CreateModule()
580 return new ModuleSpanningTree;
586 extern "C" void * init_module( void )
588 return new ModuleSpanningTreeFactory;