1 #ifndef __TREESOCKET_H__
2 #define __TREESOCKET_H__
4 #include "configreader.h"
8 #include "commands/cmd_whois.h"
9 #include "commands/cmd_stats.h"
14 #include "transport.h"
16 #include "m_spanningtree/utils.h"
19 * The server list in InspIRCd is maintained as two structures
20 * which hold the data in different ways. Most of the time, we
21 * want to very quicky obtain three pieces of information:
23 * (1) The information on a server
24 * (2) The information on the server we must send data through
25 * to actually REACH the server we're after
26 * (3) Potentially, the child/parent objects of this server
28 * The InspIRCd spanning protocol provides easy access to these
29 * by storing the data firstly in a recursive structure, where
30 * each item references its parent item, and a dynamic list
31 * of child items, and another structure which stores the items
32 * hashed, linearly. This means that if we want to find a server
33 * by name quickly, we can look it up in the hash, avoiding
34 * any O(n) lookups. If however, during a split or sync, we want
35 * to apply an operation to a server, and any of its child objects
36 * we can resort to recursion to walk the tree structure.
37 * Any socket can have one of five states at any one time.
38 * The LISTENER state indicates a socket which is listening
39 * for connections. It cannot receive data itself, only incoming
41 * The CONNECTING state indicates an outbound socket which is
42 * waiting to be writeable.
43 * The WAIT_AUTH_1 state indicates the socket is outbound and
44 * has successfully connected, but has not yet sent and received
46 * The WAIT_AUTH_2 state indicates that the socket is inbound
47 * (allocated by a LISTENER) but has not yet sent and received
49 * The CONNECTED state represents a fully authorized, fully
52 enum ServerState { LISTENER, CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
54 /** Every SERVER connection inbound or outbound is represented by
55 * an object of type TreeSocket.
56 * TreeSockets, being inherited from InspSocket, can be tied into
57 * the core socket engine, and we cn therefore receive activity events
58 * for them, just like activex objects on speed. (yes really, that
59 * is a technical term!) Each of these which relates to a locally
60 * connected server is assocated with it, by hooking it onto a
61 * TreeSocket class using its constructor. In this way, we can
62 * maintain a list of servers, some of which are directly connected,
63 * some of which are not.
65 class TreeSocket : public InspSocket
67 SpanningTreeUtilities* Utils; /* Utility class */
68 std::string myhost; /* Canonical hostname */
69 std::string in_buffer; /* Input buffer */
70 ServerState LinkState; /* Link state */
71 std::string InboundServerName; /* Server name sent to us by other side */
72 std::string InboundDescription; /* Server description (GECOS) sent to us by the other side */
73 int num_lost_users; /* Users lost in split */
74 int num_lost_servers; /* Servers lost in split */
75 time_t NextPing; /* Time when we are due to ping this server */
76 bool LastPingWasGood; /* Responded to last ping we sent? */
77 bool bursting; /* True if not finished bursting yet */
78 unsigned int keylength; /* Is this still used? */
79 std::string ModuleList; /* Module list of other server from CAPAB */
80 std::map<std::string,std::string> CapKeys; /* CAPAB keys from other server */
81 Module* Hook; /* I/O hooking module that we're attached to for this socket */
85 /** Because most of the I/O gubbins are encapsulated within
86 * InspSocket, we just call the superclass constructor for
87 * most of the action, and append a few of our own values
90 TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, std::string host, int port, bool listening, unsigned long maxtime, Module* HookMod = NULL);
92 /** Because most of the I/O gubbins are encapsulated within
93 * InspSocket, we just call the superclass constructor for
94 * most of the action, and append a few of our own values
97 TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, std::string host, int port, bool listening, unsigned long maxtime, const std::string &ServerName, const std::string &bindto, Module* HookMod = NULL);
99 /** When a listening socket gives us a new file descriptor,
100 * we must associate it with a socket without creating a new
101 * connection. This constructor is used for this purpose.
103 TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, int newfd, char* ip, Module* HookMod = NULL);
107 ServerState GetLinkState();
109 /** Return the module which we are hooking to for I/O encapsulation
117 /** When an outbound connection finishes connecting, we receive
118 * this event, and must send our SERVER string to the other
119 * side. If the other side is happy, as outlined in the server
120 * to server docs on the inspircd.org site, the other side
121 * will then send back its own server string.
123 virtual bool OnConnected();
125 /** Handle socket error event
127 virtual void OnError(InspSocketError e);
129 /** Handle socket disconnect event
131 virtual int OnDisconnect();
133 /** Recursively send the server tree with distances as hops.
134 * This is used during network burst to inform the other server
135 * (and any of ITS servers too) of what servers we know about.
136 * If at any point any of these servers already exist on the other
137 * end, our connection may be terminated. The hopcounts given
138 * by this function are relative, this doesn't matter so long as
139 * they are all >1, as all the remote servers re-calculate them
140 * to be relative too, with themselves as hop 0.
142 void SendServers(TreeServer* Current, TreeServer* s, int hops);
144 /** Returns my capabilities as a string
146 std::string MyCapabilities();
148 /** Send my capabilities to the remote side
150 void SendCapabilities();
152 /* Check a comma seperated list for an item */
153 bool HasItem(const std::string &list, const std::string &item);
155 /* Isolate and return the elements that are different between two comma seperated lists */
156 std::string ListDifference(const std::string &one, const std::string &two);
158 bool Capab(const std::deque<std::string> ¶ms);
160 /** This function forces this server to quit, removing this server
161 * and any users on it (and servers and users below that, etc etc).
162 * It's very slow and pretty clunky, but luckily unless your network
163 * is having a REAL bad hair day, this function shouldnt be called
164 * too many times a month ;-)
166 void SquitServer(std::string &from, TreeServer* Current);
168 /** This is a wrapper function for SquitServer above, which
169 * does some validation first and passes on the SQUIT to all
170 * other remaining servers.
172 void Squit(TreeServer* Current, const std::string &reason);
174 /** FMODE command - server mode with timestamp checks */
175 bool ForceMode(const std::string &source, std::deque<std::string> ¶ms);
177 /** FTOPIC command */
178 bool ForceTopic(const std::string &source, std::deque<std::string> ¶ms);
180 /** FJOIN, similar to TS6 SJOIN, but not quite. */
181 bool ForceJoin(const std::string &source, std::deque<std::string> ¶ms);
184 bool IntroduceClient(const std::string &source, std::deque<std::string> ¶ms);
186 /** Send one or more FJOINs for a channel of users.
187 * If the length of a single line is more than 480-NICKMAX
188 * in length, it is split over multiple lines.
190 void SendFJoins(TreeServer* Current, chanrec* c);
192 /** Send G, Q, Z and E lines */
193 void SendXLines(TreeServer* Current);
195 /** Send channel modes and topics */
196 void SendChannelModes(TreeServer* Current);
198 /** send all users and their oper state/modes */
199 void SendUsers(TreeServer* Current);
201 /** This function is called when we want to send a netburst to a local
202 * server. There is a set order we must do this, because for example
203 * users require their servers to exist, and channels require their
204 * users to exist. You get the idea.
206 void DoBurst(TreeServer* s);
208 /** This function is called when we receive data from a remote
209 * server. We buffer the data in a std::string (it doesnt stay
210 * there for long), reading using InspSocket::Read() which can
211 * read up to 16 kilobytes in one operation.
213 * IF THIS FUNCTION RETURNS FALSE, THE CORE CLOSES AND DELETES
214 * THE SOCKET OBJECT FOR US.
216 virtual bool OnDataReady();
218 /** Send one or more complete lines down the socket
220 int WriteLine(std::string line);
222 /** Handle ERROR command */
223 bool Error(std::deque<std::string> ¶ms);
225 /** remote MOTD. leet, huh? */
226 bool Motd(const std::string &prefix, std::deque<std::string> ¶ms);
228 /** remote ADMIN. leet, huh? */
229 bool Admin(const std::string &prefix, std::deque<std::string> ¶ms);
231 /** Remote MODULES */
232 bool Modules(const std::string &prefix, std::deque<std::string> ¶ms);
234 bool Stats(const std::string &prefix, std::deque<std::string> ¶ms);
236 /** Because the core won't let users or even SERVERS set +o,
237 * we use the OPERTYPE command to do this.
239 bool OperType(const std::string &prefix, std::deque<std::string> ¶ms);
241 /** Because Andy insists that services-compatible servers must
242 * implement SVSNICK and SVSJOIN, that's exactly what we do :p
244 bool ForceNick(const std::string &prefix, std::deque<std::string> ¶ms);
246 bool OperQuit(const std::string &prefix, std::deque<std::string> ¶ms);
248 /** Remote SQUIT (RSQUIT). Routing works similar to SVSNICK: Route it to the server that the target is connected to locally,
249 * then let that server do the dirty work (squit it!). Example:
250 * A -> B -> C -> D: oper on A squits D, A routes to B, B routes to C, C notices D connected locally, kills it. -- w00t
252 bool RemoteSquit(const std::string &prefix, std::deque<std::string> ¶ms);
256 bool ServiceJoin(const std::string &prefix, std::deque<std::string> ¶ms);
260 bool RemoteRehash(const std::string &prefix, std::deque<std::string> ¶ms);
264 bool RemoteKill(const std::string &prefix, std::deque<std::string> ¶ms);
268 bool LocalPong(const std::string &prefix, std::deque<std::string> ¶ms);
272 bool MetaData(const std::string &prefix, std::deque<std::string> ¶ms);
276 bool ServerVersion(const std::string &prefix, std::deque<std::string> ¶ms);
280 bool ChangeHost(const std::string &prefix, std::deque<std::string> ¶ms);
284 bool AddLine(const std::string &prefix, std::deque<std::string> ¶ms);
288 bool ChangeName(const std::string &prefix, std::deque<std::string> ¶ms);
292 bool Whois(const std::string &prefix, std::deque<std::string> ¶ms);
296 bool Push(const std::string &prefix, std::deque<std::string> ¶ms);
300 bool HandleSetTime(const std::string &prefix, std::deque<std::string> ¶ms);
304 bool Time(const std::string &prefix, std::deque<std::string> ¶ms);
308 bool LocalPing(const std::string &prefix, std::deque<std::string> ¶ms);
310 /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
311 * This does not update the timestamp of the target channel, this must be done seperately.
313 bool RemoveStatus(const std::string &prefix, std::deque<std::string> ¶ms);
315 /** <- (remote) <- SERVER
317 bool RemoteServer(const std::string &prefix, std::deque<std::string> ¶ms);
319 /** (local) -> SERVER
321 bool Outbound_Reply_Server(std::deque<std::string> ¶ms);
323 /** (local) <- SERVER
325 bool Inbound_Server(std::deque<std::string> ¶ms);
329 void Split(const std::string &line, std::deque<std::string> &n);
331 /** Process complete line from buffer
333 bool ProcessLine(std::string &line);
335 /** Get this server's name
337 virtual std::string GetName();
339 /** Handle socket timeout from connect()
341 virtual void OnTimeout();
343 /** Handle socket close event
345 virtual void OnClose();
347 /** Handle incoming connection event
349 virtual int OnIncomingConnection(int newsock, char* ip);