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 std::string CreateSum();
44 /** Each connection has one or more of these
45 * each represents ONE outbound connection to another ircd
46 * so each inbound has multiple outbounds. A listening socket
47 * that accepts server type connections is represented by one
48 * class serverrec. Class serverrec will instantiate several
49 * objects of type ircd_connector to represent each established
50 * connection, inbound or outbound. So, to determine all linked
51 * servers you must walk through all the serverrecs that the
52 * core defines, and in each one iterate through until you find
53 * connection(s) relating to the server you want information on.
54 * The core and module API provide functions for this.
56 class ircd_connector : public Extensible
59 /** Sockaddr of the outbound ip and port
63 /** File descriptor of the connection
69 std::string servername;
73 std::string description;
75 /** State. STATE_NOAUTH_INBOUND, STATE_NOAUTH_OUTBOUND
76 * STATE_SYNC, STATE_DISCONNECTED, STATE_CONNECTED
80 /** PRIVATE function to set the host address and port to connect to
82 bool SetHostAddress(char* host, int port);
84 /** This string holds the ircd's version response
88 /** SendQ of the outbound connector, does not have a limit
92 /** Write error of connection
94 std::string WriteError;
96 /** Time this connection was last pinged
100 /** Did this connection reply to its last ping?
106 /** IRCD Buffer for input characters, holds as many lines as are
107 * pending - Note that the final line may not be complete and should
108 * only be read when there is a \n seperator.
110 std::string ircdbuffer;
113 /** When MakeOutboundConnection is called, these public members are
114 * filled with the details passed to the function, for future
119 /** When MakeOutboundConnection is called, these public members are
120 * filled with the details passed to the function, for future
125 /** Server names of servers that this server is linked to
126 * So for A->B->C, if this was the record for B it would contain A and C
127 * whilever both servers are connected to B.
129 std::vector<std::string> routes;
131 /** Constructor clears the sendq and initialises the fd to -1
135 /** Create an outbound connection to a listening socket
137 bool MakeOutboundConnection(char* newhost, int newport);
139 /** Return the servername on this established connection
141 std::string GetServerName();
143 /** Set the server name of this connection
145 void SetServerName(std::string serv);
147 /** Get the file descriptor associated with this connection
151 /** Set the file descriptor for this connection
153 void SetDescriptor(int fd);
155 /** Get the state flags for this connection
159 /** Set the state flags for this connection
161 void SetState(int state);
163 /** Get the ip address (not servername) associated with this connection
167 /** Get the server description of this connection
169 std::string GetDescription();
171 /** Set the server description of this connection
173 void SetDescription(std::string desc);
175 /** Get the port number being used for this connection
176 * If the connection is outbound this will be the remote port
177 * otherwise it will be the local port, so it can always be
178 * gautanteed as open at the address given in GetServerIP().
182 /** Set the port used by this connection
184 void SetServerPort(int p);
186 /** Set both the host and the port in one operation for this connection
188 bool SetHostAndPort(char* newhost, int newport);
190 /** Close the connection by calling close() on its file descriptor
191 * This function call updates no other data.
193 void CloseConnection();
195 /** This method adds text to the ircd connection's buffer
196 * This buffer's maximum size is one megabyte, the method returning false
197 * if the buffer is full.
199 bool AddBuffer(std::string a);
201 /** This method returns true if the buffer contains at least one
202 * carriage return character, e.g. one line can be read from the
203 * buffer successfully.
205 bool BufferIsComplete();
207 /** This method clears the server's buffer by setting it to an empty string.
211 /** This method retrieves the first string from the tail end of the
212 * buffer and advances the tail end of the buffer past the returned
213 * string, in a similar manner to strtok().
215 std::string GetBuffer();
217 /** This method sets the version string of the remote server
219 void SetVersionString(std::string newversion);
221 /** This method returns the version string of the remote server.
222 * If the server has no version string an empty string is returned.
224 std::string GetVersionString();
226 /** Adds data to the connection's sendQ to be flushed later
227 * Fails if there is an error pending on the connection.
229 bool AddWriteBuf(std::string data);
231 /** Flushes as much of the data from the buffer as possible,
232 * and advances the queue pointer to what is left.
234 bool FlushWriteBuf();
236 /** Sets the error string for this connection
238 void SetWriteError(std::string error);
240 /** Gets the error string for this connection
242 std::string GetWriteError();
244 /** Returns true if there is data to be written that hasn't been sent yet
246 bool HasBufferedOutput();
248 /** Checks if the connection replied to its last ping, and if it did
249 * sends another and returns true, if not, returns false.
253 /** Resets the ping counter
259 /** Please note: classes serverrec and userrec both inherit from class connection.
261 class connection : public Extensible
264 /** File descriptor of the connection
268 /** Hostname of connection. Not used if this is a serverrec
272 /** IP of connection.
276 /** Stats counter for bytes inbound
280 /** Stats counter for bytes outbound
284 /** Stats counter for commands inbound
288 /** Stats counter for commands outbound
292 /** True if server/user has authenticated, false if otherwise
297 * For a userrec, this is the port they connected to the network on.
298 * For a serverrec this is the current listening port of the serverrec object.
302 /** Used by userrec to indicate the registration status of the connection
306 /** Time the connection was last pinged
310 /** Time the connection was created, set in the constructor
314 /** Time that the connection last sent data, used to calculate idle time
318 /** Used by PING checks with clients
322 /** Default constructor