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;
71 return this->ServerName;
76 return this->ServerDesc;
79 std::string GetVersion()
81 return this->VersionString;
86 return this->UserCount;
91 return this->OperCount;
94 TreeSocket* GetSocket()
99 TreeServer* GetParent()
104 void AddChild(TreeServer* Child)
106 Children.push_back(Child);
109 bool DelChild(TreeServer* Child)
111 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
129 std::string SendPass;
130 std::string RecvPass;
133 /* $ModDesc: Povides a spanning tree server link protocol */
137 TreeServer *TreeRoot;
138 std::vector<Link> LinkBlocks;
140 class TreeSocket : public InspSocket
143 std::string in_buffer;
144 ServerState LinkState;
145 std::string InboundServerName;
146 std::string InboundDescription;
150 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
151 : InspSocket(host, port, listening, maxtime)
153 Srv->Log(DEBUG,"Create new listening");
155 this->LinkState = LISTENER;
158 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
159 : InspSocket(host, port, listening, maxtime)
161 Srv->Log(DEBUG,"Create new outbound");
163 this->LinkState = CONNECTING;
166 TreeSocket(int newfd, char* ip)
167 : InspSocket(newfd, ip)
169 Srv->Log(DEBUG,"Associate new inbound");
170 this->LinkState = WAIT_AUTH_1;
173 virtual bool OnConnected()
175 if (this->LinkState == CONNECTING)
177 Srv->SendOpers("*** Connection to "+myhost+"["+this->GetIP()+"] established.");
178 // we should send our details here.
179 // if the other side is satisfied, they send theirs.
180 // we do not need to change state here.
181 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
183 if (x->Name == this->myhost)
185 // found who we're supposed to be connecting to, send the neccessary gubbins.
186 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
191 log(DEBUG,"Outbound connection ERROR: Could not find the right link block!");
195 virtual void OnError(InspSocketError e)
199 virtual int OnDisconnect()
204 void DoBurst(TreeServer* s)
206 log(DEBUG,"Beginning network burst");
207 Srv->SendOpers("*** Bursting to "+s->GetName()+".");
208 this->WriteLine("BURST");
209 this->WriteLine("ENDBURST");
212 virtual bool OnDataReady()
214 char* data = this->Read();
217 this->in_buffer += data;
218 while (in_buffer.find("\n") != std::string::npos)
220 char* line = (char*)in_buffer.c_str();
221 std::string ret = "";
222 while ((*line != '\n') && (strlen(line)))
227 if ((*line == '\n') || (*line == '\r'))
230 if (!this->ProcessLine(ret))
236 return (data != NULL);
239 int WriteLine(std::string line)
241 return this->Write(line + "\r\n");
244 bool Error(std::deque<std::string> params)
246 if (params.size() < 1)
248 std::string Errmsg = params[0];
249 std::string SName = myhost;
250 if (InboundServerName != "")
252 SName = InboundServerName;
254 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
255 // we will return false to cause the socket to close.
259 bool Outbound_Reply_Server(std::deque<std::string> params)
261 if (params.size() < 4)
263 std::string servername = params[0];
264 std::string password = params[1];
265 int hops = atoi(params[2].c_str());
268 this->WriteLine("ERROR :Server too far away for authentication");
271 std::string description = params[3];
272 Srv->SendToModeMask("o",WM_AND,"outbound-server-replied: name='"+servername+"' pass='"+password+"' description='"+description+"'");
273 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
275 if ((x->Name == servername) && (x->RecvPass == password))
277 // Begin the sync here. this kickstarts the
278 // other side, waiting in WAIT_AUTH_2 state,
279 // into starting their burst, as it shows
281 this->LinkState = CONNECTED;
282 // we should add the details of this server now
283 // to the servers tree, as a child of the root
285 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
286 TreeRoot->AddChild(Node);
291 this->WriteLine("ERROR :Invalid credentials");
295 bool Inbound_Server(std::deque<std::string> params)
297 if (params.size() < 4)
299 std::string servername = params[0];
300 std::string password = params[1];
301 int hops = atoi(params[2].c_str());
304 this->WriteLine("ERROR :Server too far away for authentication");
307 std::string description = params[3];
308 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
310 if ((x->Name == servername) && (x->RecvPass == password))
312 Srv->SendOpers("*** Verified incoming server connection from "+servername+"["+this->GetIP()+"] ("+description+")");
313 this->InboundServerName = servername;
314 this->InboundDescription = description;
315 // this is good. Send our details: Our server name and description and hopcount of 0,
316 // along with the sendpass from this block.
317 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
318 // move to the next state, we are now waiting for THEM.
319 this->LinkState = WAIT_AUTH_2;
323 this->WriteLine("ERROR :Invalid credentials");
327 std::deque<std::string> Split(std::string line)
329 std::deque<std::string> n;
330 std::stringstream s(line);
331 std::string param = "";
337 if ((param.c_str()[0] == ':') && (item))
339 char* str = (char*)param.c_str();
349 param = param + " " + append;
359 bool ProcessLine(std::string line)
361 Srv->SendToModeMask("o",WM_AND,"inbound-line: '"+line+"'");
363 std::deque<std::string> params = this->Split(line);
364 std::string command = "";
365 std::string prefix = "";
366 if ((params[0].c_str())[0] == ':')
380 switch (this->LinkState)
385 // Waiting for SERVER command from remote server. Server initiating
386 // the connection sends the first SERVER command, listening server
387 // replies with theirs if its happy, then if the initiator is happy,
388 // it starts to send its net sync, which starts the merge, otherwise
389 // it sends an ERROR.
390 if (command == "SERVER")
392 return this->Inbound_Server(params);
394 else if (command == "ERROR")
396 return this->Error(params);
400 // Waiting for start of other side's netmerge to say they liked our
402 if (command == "SERVER")
404 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
408 else if (command == "BURST")
410 this->LinkState = CONNECTED;
411 Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
412 TreeRoot->AddChild(Node);
415 else if (command == "ERROR")
417 return this->Error(params);
422 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
426 if (command == "SERVER")
428 // another server we connected to, which was in WAIT_AUTH_1 state,
429 // has just sent us their credentials. If we get this far, theyre
430 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
431 // if we're happy with this, we should send our netburst which
432 // kickstarts the merge.
433 return this->Outbound_Reply_Server(params);
437 // This is the 'authenticated' state, when all passwords
438 // have been exchanged and anything past this point is taken
446 virtual void OnTimeout()
448 if (this->LinkState = CONNECTING)
450 Srv->SendOpers("*** CONNECT: Connection to "+myhost+" timed out.");
454 virtual void OnClose()
458 virtual int OnIncomingConnection(int newsock, char* ip)
460 TreeSocket* s = new TreeSocket(newsock, ip);
466 class ModuleSpanningTree : public Module
468 std::vector<TreeSocket*> Bindings;
472 void ReadConfiguration(bool rebind)
476 for (int j =0; j < Conf->Enumerate("bind"); j++)
478 std::string Type = Conf->ReadValue("bind","type",j);
479 std::string IP = Conf->ReadValue("bind","address",j);
480 long Port = Conf->ReadInteger("bind","port",j,true);
481 if (Type == "servers")
487 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
488 if (listener->GetState() == I_LISTENING)
490 Srv->AddSocket(listener);
491 Bindings.push_back(listener);
495 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
503 for (int j =0; j < Conf->Enumerate("link"); j++)
506 L.Name = Conf->ReadValue("link","name",j);
507 L.IPAddr = Conf->ReadValue("link","ipaddr",j);
508 L.Port = Conf->ReadInteger("link","port",j,true);
509 L.SendPass = Conf->ReadValue("link","sendpass",j);
510 L.RecvPass = Conf->ReadValue("link","recvpass",j);
511 LinkBlocks.push_back(L);
512 log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
519 Conf = new ConfigReader;
522 // Create the root of the tree
523 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
525 ReadConfiguration(true);
528 void HandleLinks(char** parameters, int pcnt, userrec* user)
533 void HandleLusers(char** parameters, int pcnt, userrec* user)
538 void HandleMap(char** parameters, int pcnt, userrec* user)
543 int HandleSquit(char** parameters, int pcnt, userrec* user)
548 int HandleConnect(char** parameters, int pcnt, userrec* user)
550 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
552 if (Srv->MatchText(x->Name.c_str(),parameters[0]))
554 WriteServ(user->fd,"NOTICE %s :*** CONNECT: Connecting to server: %s (%s:%d)",user->nick,x->Name.c_str(),x->IPAddr.c_str(),x->Port);
555 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
556 Srv->AddSocket(newsocket);
560 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No matching server could be found in the config file.",user->nick);
564 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
566 if (command == "CONNECT")
568 return this->HandleConnect(parameters,pcnt,user);
570 else if (command == "SQUIT")
572 return this->HandleSquit(parameters,pcnt,user);
574 else if (command == "MAP")
576 this->HandleMap(parameters,pcnt,user);
579 else if (command == "LUSERS")
581 this->HandleLusers(parameters,pcnt,user);
584 else if (command == "LINKS")
586 this->HandleLinks(parameters,pcnt,user);
592 virtual ~ModuleSpanningTree()
597 virtual Version GetVersion()
599 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
604 class ModuleSpanningTreeFactory : public ModuleFactory
607 ModuleSpanningTreeFactory()
611 ~ModuleSpanningTreeFactory()
615 virtual Module * CreateModule()
617 return new ModuleSpanningTree;
623 extern "C" void * init_module( void )
625 return new ModuleSpanningTreeFactory;