1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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 * ---------------------------------------------------
17 #include "inspircd_config.h"
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <netinet/in.h>
32 #ifndef __CONNECTION_H__
33 #define __CONNECTION_H__
35 #define STATE_DISCONNECTED 0
36 #define STATE_CONNECTED 1
38 #define STATE_NOAUTH_INBOUND 3
39 #define STATE_NOAUTH_OUTBOUND 4
40 #define STATE_SERVICES 5
42 /** Each connection has one or more of these
43 * each represents ONE outbound connection to another ircd
44 * so each inbound has multiple outbounds. A listening socket
45 * that accepts server type connections is represented by one
46 * class serverrec. Class serverrec will instantiate several
47 * objects of type ircd_connector to represent each established
48 * connection, inbound or outbound. So, to determine all linked
49 * servers you must walk through all the serverrecs that the
50 * core defines, and in each one iterate through until you find
51 * connection(s) relating to the server you want information on.
52 * The core and module API provide functions for this.
54 class ircd_connector : public Extensible
57 /** Sockaddr of the outbound ip and port
61 /** File descriptor of the connection
67 std::string servername;
71 std::string description;
73 /** State. STATE_NOAUTH_INBOUND, STATE_NOAUTH_OUTBOUND
74 * STATE_SYNC, STATE_DISCONNECTED, STATE_CONNECTED
78 /** PRIVATE function to set the host address and port to connect to
80 bool SetHostAddress(char* host, int port);
82 /** This string holds the ircd's version response
88 /** IRCD Buffer for input characters, holds as many lines as are
89 * pending - Note that the final line may not be complete and should
90 * only be read when there is a \n seperator.
92 std::string ircdbuffer;
95 /** When MakeOutboundConnection is called, these public members are
96 * filled with the details passed to the function, for future
101 /** When MakeOutboundConnection is called, these public members are
102 * filled with the details passed to the function, for future
107 /** Server names of servers that this server is linked to
108 * So for A->B->C, if this was the record for B it would contain A and C
109 * whilever both servers are connected to B.
111 std::vector<std::string> routes;
114 /** Create an outbound connection to a listening socket
116 bool MakeOutboundConnection(char* newhost, int newport);
118 /** Return the servername on this established connection
120 std::string GetServerName();
122 /** Set the server name of this connection
124 void SetServerName(std::string serv);
126 /** Get the file descriptor associated with this connection
130 /** Set the file descriptor for this connection
132 void SetDescriptor(int fd);
134 /** Get the state flags for this connection
138 /** Set the state flags for this connection
140 void SetState(int state);
142 /** Get the ip address (not servername) associated with this connection
146 /** Get the server description of this connection
148 std::string GetDescription();
150 /** Set the server description of this connection
152 void SetDescription(std::string desc);
154 /** Get the port number being used for this connection
155 * If the connection is outbound this will be the remote port
156 * otherwise it will be the local port, so it can always be
157 * gautanteed as open at the address given in GetServerIP().
161 /** Set the port used by this connection
163 void SetServerPort(int p);
165 /** Set both the host and the port in one operation for this connection
167 bool SetHostAndPort(char* newhost, int newport);
169 /** Close the connection by calling close() on its file descriptor
170 * This function call updates no other data.
172 void CloseConnection();
174 /** This method adds text to the ircd connection's buffer
175 * There is no limitation on how much text of what line width may
176 * be added to this buffer. It is the sending server's responsibility
177 * to ensure sent data is kept within reasonable quanities.
179 void AddBuffer(std::string a);
181 /** This method returns true if the buffer contains at least one
182 * carriage return character, e.g. one line can be read from the
183 * buffer successfully.
185 bool BufferIsComplete();
187 /** This method clears the server's buffer by setting it to an empty string.
191 /** This method retrieves the first string from the tail end of the
192 * buffer and advances the tail end of the buffer past the returned
193 * string, in a similar manner to strtok().
195 std::string GetBuffer();
197 /** This method sets the version string of the remote server
199 void SetVersionString(std::string newversion);
201 /** This method returns the version string of the remote server.
202 * If the server has no version string an empty string is returned.
204 std::string GetVersionString();
208 /** Please note: classes serverrec and userrec both inherit from class connection.
210 class connection : public Extensible
213 /** File descriptor of the connection
217 /** Hostname of connection. Not used if this is a serverrec
221 /** IP of connection. Reserved for future use.
225 /** Inbuf of connection. Only used for userrec
229 /** Stats counter for bytes inbound
233 /** Stats counter for bytes outbound
237 /** Stats counter for commands inbound
241 /** Stats counter for commands outbound
245 /** True if server/user has authenticated, false if otherwise
250 * For a userrec, this is the port they connected to the network on.
251 * For a serverrec this is the current listening port of the serverrec object.
255 /** Used by userrec to indicate the registration status of the connection
259 /** Reserved for future use
263 /** Time the connection was last pinged
267 /** Time the connection was created, set in the constructor
271 /** Time that the connection last sent data, used to calculate idle time
275 /** Used by PING checks with clients
279 /** Unused, will be removed in a future alpha/beta
281 char internal_addr[MAXBUF];
283 /** Unused, will be removed in a future alpha/beta
287 /** With a serverrec, this is a list of all established server connections.
288 * With a userrec this is unused.
290 std::vector<ircd_connector> connectors;
292 /** Default constructor
296 /** Create a listening socket on 'host' using port number 'p'
298 bool CreateListener(char* host, int p);
300 /** Begin an outbound link to another ircd at targethost.
302 bool BeginLink(char* targethost, int port, char* password, char* servername, int myport);
304 /** Begin an outbound mesh link to another ircd on a network you are already an authenticated member of
306 bool MeshCookie(char* targethost, int port, unsigned long cookie, char* servername);
308 /** Terminate a link to 'targethost' by calling the ircd_connector::CloseConnection method.
310 void TerminateLink(char* targethost);
312 /** Send a message to a server by name, if the server is unavailable directly route the packet via another server
313 * If the server still cannot be reached after attempting to route the message remotely, returns false.
315 bool SendPacket(char *message, const char* host);
317 /** Returns the next available packet and returns true if data is available. Writes the servername the data came from to 'host'.
318 * If no data is available this function returns false.
319 * This function will automatically close broken links and reroute pathways, generating split messages on the network.
321 bool RecvPacket(std::deque<std::string> &messages, char* host);
323 /** Find the ircd_connector oject related to a certain servername given in 'host'
325 ircd_connector* FindHost(std::string host);
327 /** Add an incoming connection to the connection pool.
328 * (reserved for core use)
330 bool AddIncoming(int fd,char* targethost, int sourceport);