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
41 #define STATE_COOKIE_OUTBOUND 6
43 std::string CreateSum();
45 /** Each connection has one or more of these
46 * each represents ONE outbound connection to another ircd
47 * so each inbound has multiple outbounds. A listening socket
48 * that accepts server type connections is represented by one
49 * class serverrec. Class serverrec will instantiate several
50 * objects of type ircd_connector to represent each established
51 * connection, inbound or outbound. So, to determine all linked
52 * servers you must walk through all the serverrecs that the
53 * core defines, and in each one iterate through until you find
54 * connection(s) relating to the server you want information on.
55 * The core and module API provide functions for this.
57 class ircd_connector : public Extensible
60 /** Sockaddr of the outbound ip and port
64 /** File descriptor of the connection
70 std::string servername;
74 std::string description;
76 /** State. STATE_NOAUTH_INBOUND, STATE_NOAUTH_OUTBOUND
77 * STATE_SYNC, STATE_DISCONNECTED, STATE_CONNECTED
81 /** PRIVATE function to set the host address and port to connect to
83 bool SetHostAddress(char* host, int port);
85 /** This string holds the ircd's version response
89 /** SendQ of the outbound connector, does not have a limit
93 /** Write error of connection
95 std::string WriteError;
97 /** Time this connection was last pinged
101 /** Did this connection reply to its last ping?
107 /** IRCD Buffer for input characters, holds as many lines as are
108 * pending - Note that the final line may not be complete and should
109 * only be read when there is a \n seperator.
111 std::string ircdbuffer;
114 /** When MakeOutboundConnection is called, these public members are
115 * filled with the details passed to the function, for future
120 /** When MakeOutboundConnection is called, these public members are
121 * filled with the details passed to the function, for future
126 /** Server names of servers that this server is linked to
127 * So for A->B->C, if this was the record for B it would contain A and C
128 * whilever both servers are connected to B.
130 std::vector<std::string> routes;
132 /** Constructor clears the sendq and initialises the fd to -1
136 /** Create an outbound connection to a listening socket
138 bool MakeOutboundConnection(char* newhost, int newport);
140 /** Return the servername on this established connection
142 std::string GetServerName();
144 /** Set the server name of this connection
145 * @param serv The server name to set
147 void SetServerName(std::string serv);
149 /** Get the file descriptor associated with this connection
150 * @return The file descriptor associated with this connection
154 /** Set the file descriptor for this connection
155 * @param fd The file descriptor to associate with the connection
157 void SetDescriptor(int fd);
159 /** Get the state flags for this connection
160 * @return The state flags associated with this connection
164 /** Set the state flags for this connection
165 * @param state The state flags to set for this connection
167 void SetState(int state);
169 /** Get the ip address (not servername) associated with this connection
170 * @return The connections IP address in dotted decimal form
174 /** Get the server description of this connection
175 * @return The description (GECOS) of this connection
177 std::string GetDescription();
179 /** Set the server description of this connection
180 * @param desc The description (GECOS) of this connection to be set
182 void SetDescription(std::string desc);
184 /** Get the port number being used for this connection
185 * If the connection is outbound this will be the remote port
186 * otherwise it will be the local port, so it can always be
187 * gautanteed as open at the address given in GetServerIP().
189 * @return The port number of this connection
193 /** Set the port used by this connection
194 * @param p The port number to set for this connection
196 void SetServerPort(int p);
198 /** Set both the host and the port in one operation for this connection
199 * @param newhost The hostname to set for this connection
200 * @param newport The port number to set for this connection
201 * @return True on success, false on failure
203 bool SetHostAndPort(char* newhost, int newport);
205 /** Close the connection by calling close() on its file descriptor
206 * This function call updates no other data.
208 void CloseConnection();
210 /** This method adds text to the ircd connection's buffer
211 * @param a The text to add to the buffer up to a maximum size of 1MB
213 * This buffer's maximum size is one megabyte, the method returning false
214 * if the buffer is full.
216 * @return True on success, false if the buffer is full or the connection is down
218 bool AddBuffer(std::string a);
220 /** This method returns true if the buffer contains at least one
221 * carriage return character, e.g. one line can be read from the
222 * buffer successfully.
224 * @return True if there is at least one complete line waiting to be processed
226 bool BufferIsComplete();
228 /** This method clears the server's buffer by setting it to an empty string.
232 /** This method retrieves the first string from the tail end of the
233 * buffer and advances the tail end of the buffer past the returned
234 * string, in a similar manner to strtok().
236 * @return The first line of the buffer up to a carriage return
238 std::string GetBuffer();
240 /** This method sets the version string of the remote server
241 * @param newversion The version string to set
243 void SetVersionString(std::string newversion);
245 /** This method returns the version string of the remote server.
246 * If the server has no version string an empty string is returned.
248 * @return The version text of this connection
250 std::string GetVersionString();
252 /** Adds data to the connection's sendQ to be flushed later.
253 * @param data The data to add to the write buffer
255 * Fails if there is an error pending on the connection.
257 * @return True on success, false if the connection is down or the buffer is full
259 bool AddWriteBuf(std::string data);
261 /** Flushes as much of the data from the buffer as possible,
262 * and advances the queue pointer to what is left.
264 * @return True if the flush succeeded, false if the connection is down
266 bool FlushWriteBuf();
268 /** Sets the error string for this connection
269 * @param error The error string to set
271 void SetWriteError(std::string error);
273 /** Gets the error string for this connection
274 * @return The last error to occur or an empty string
276 std::string GetWriteError();
278 /** Returns true if there is data to be written that hasn't been sent yet
279 * @return True if the buffer is not empty
281 bool HasBufferedOutput();
283 /** Checks if the connection replied to its last ping, and if it did
284 * sends another and returns true, if not, returns false.
285 * @return True if the server is still replying to pings
289 /** Resets the ping counter
295 /** Please note: classes serverrec and userrec both inherit from class connection.
297 class connection : public Extensible
300 /** File descriptor of the connection
304 /** Hostname of connection. Not used if this is a serverrec
308 /** IP of connection.
312 /** Stats counter for bytes inbound
316 /** Stats counter for bytes outbound
320 /** Stats counter for commands inbound
324 /** Stats counter for commands outbound
328 /** True if server/user has authenticated, false if otherwise
333 * For a userrec, this is the port they connected to the network on.
334 * For a serverrec this is the current listening port of the serverrec object.
338 /** Used by userrec to indicate the registration status of the connection
342 /** Time the connection was last pinged
346 /** Time the connection was created, set in the constructor
350 /** Time that the connection last sent data, used to calculate idle time
354 /** Used by PING checks with clients
358 /** Default constructor