]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/connection.h
2d473bde4f0443f1a04f2dc701c732e33de97128
[user/henk/code/inspircd.git] / include / connection.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "inspircd_config.h"
18 #include "base.h"
19 #include <string>
20 #include <map>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netdb.h>
24 #include <netinet/in.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <time.h>
28 #include <vector>
29 #include <deque>
30 #include <sstream>
31
32 #ifndef __CONNECTION_H__
33 #define __CONNECTION_H__
34
35 #define STATE_DISCONNECTED      0
36 #define STATE_CONNECTED         1
37 #define STATE_SYNC              2
38 #define STATE_NOAUTH_INBOUND    3
39 #define STATE_NOAUTH_OUTBOUND   4
40 #define STATE_SERVICES          5
41
42 std::string CreateSum();
43
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.
55  */
56 class ircd_connector : public Extensible
57 {
58  private:
59         /** Sockaddr of the outbound ip and port
60          */
61         sockaddr_in addr;
62         
63         /** File descriptor of the connection
64          */
65         int fd;
66         
67         /** Server name
68          */
69         std::string servername;
70         
71         /** Server 'GECOS'
72          */
73         std::string description;
74         
75         /** State. STATE_NOAUTH_INBOUND, STATE_NOAUTH_OUTBOUND
76          * STATE_SYNC, STATE_DISCONNECTED, STATE_CONNECTED
77          */
78         char state;
79         
80         /** PRIVATE function to set the host address and port to connect to
81          */
82         bool SetHostAddress(char* host, int port);
83
84         /** This string holds the ircd's version response
85          */
86         std::string version;
87
88         /** SendQ of the outbound connector, does not have a limit
89          */
90         std::string sendq;
91
92         /** Write error of connection
93          */
94         std::string WriteError;
95
96         /** Time this connection was last pinged
97          */
98         time_t nextping;
99
100         /** Did this connection reply to its last ping?
101          */
102         bool replied;
103
104  public:
105
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.
109          */
110         std::string ircdbuffer;
111
112  
113         /** When MakeOutboundConnection is called, these public members are
114          * filled with the details passed to the function, for future
115          * reference
116          */
117         char host[MAXBUF];
118
119         /** When MakeOutboundConnection is called, these public members are
120          * filled with the details passed to the function, for future
121          * reference
122          */
123         int port;
124         
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.
128          */
129         std::vector<std::string> routes;
130
131         /** Constructor clears the sendq and initialises the fd to -1
132          */
133         ircd_connector();       
134
135         /** Create an outbound connection to a listening socket
136          */ 
137         bool MakeOutboundConnection(char* newhost, int newport);
138         
139         /** Return the servername on this established connection
140          */
141         std::string GetServerName();
142         
143         /** Set the server name of this connection
144          * @param serv The server name to set
145          */
146         void SetServerName(std::string serv);
147         
148         /** Get the file descriptor associated with this connection
149          * @return The file descriptor associated with this connection
150          */
151         int GetDescriptor();
152         
153         /** Set the file descriptor for this connection
154          * @param fd The file descriptor to associate with the connection
155          */
156         void SetDescriptor(int fd);
157         
158         /** Get the state flags for this connection
159          * @return The state flags associated with this connection
160          */
161         int GetState();
162         
163         /** Set the state flags for this connection
164          * @param state The state flags to set for this connection
165          */
166         void SetState(int state);
167         
168         /** Get the ip address (not servername) associated with this connection
169          * @return The connections IP address in dotted decimal form
170          */
171         char* GetServerIP();
172         
173         /** Get the server description of this connection
174          * @return The description (GECOS) of this connection
175          */
176         std::string GetDescription();
177         
178         /** Set the server description of this connection
179          * @param desc The description (GECOS) of this connection to be set
180          */
181         void SetDescription(std::string desc);
182         
183         /** Get the port number being used for this connection
184          * If the connection is outbound this will be the remote port
185          * otherwise it will be the local port, so it can always be
186          * gautanteed as open at the address given in GetServerIP().
187          *
188          * @return The port number of this connection
189          */
190         int GetServerPort();
191         
192         /** Set the port used by this connection
193          * @param p The port number to set for this connection
194          */
195         void SetServerPort(int p);
196         
197         /** Set both the host and the port in one operation for this connection
198          * @param newhost The hostname to set for this connection
199          * @param newport The port number to set for this connection
200          * @return True on success, false on failure
201          */
202         bool SetHostAndPort(char* newhost, int newport);
203         
204         /** Close the connection by calling close() on its file descriptor
205          * This function call updates no other data.
206          */
207         void CloseConnection();
208
209         /** This method adds text to the ircd connection's buffer
210          * @param a The text to add to the buffer up to a maximum size of 1MB
211          *
212          * This buffer's maximum size is one megabyte, the method returning false
213          * if the buffer is full.
214          *
215          * @return True on success, false if the buffer is full or the connection is down
216          */
217         bool AddBuffer(std::string a);
218
219         /** This method returns true if the buffer contains at least one
220          * carriage return character, e.g. one line can be read from the
221          * buffer successfully.
222          *
223          * @return True if there is at least one complete line waiting to be processed
224          */
225         bool BufferIsComplete();
226
227         /** This method clears the server's buffer by setting it to an empty string.
228          */
229         void ClearBuffer();
230
231         /** This method retrieves the first string from the tail end of the
232          * buffer and advances the tail end of the buffer past the returned
233          * string, in a similar manner to strtok().
234          *
235          * @return The first line of the buffer up to a carriage return
236          */
237         std::string GetBuffer();
238
239         /** This method sets the version string of the remote server
240          * @param newversion The version string to set
241          */
242         void SetVersionString(std::string newversion);
243
244         /** This method returns the version string of the remote server.
245          * If the server has no version string an empty string is returned.
246          *
247          * @return The version text of this connection
248          */
249         std::string GetVersionString();
250
251         /** Adds data to the connection's sendQ to be flushed later.
252          * @param data The data to add to the write buffer
253          *
254          * Fails if there is an error pending on the connection.
255          *
256          * @return True on success, false if the connection is down or the buffer is full
257          */
258         bool AddWriteBuf(std::string data);
259
260         /** Flushes as much of the data from the buffer as possible,
261          * and advances the queue pointer to what is left.
262          *
263          * @return True if the flush succeeded, false if the connection is down
264          */
265         bool FlushWriteBuf();
266
267         /** Sets the error string for this connection
268          * @param error The error string to set
269          */
270         void SetWriteError(std::string error);
271
272         /** Gets the error string for this connection
273          * @return The last error to occur or an empty string
274          */
275         std::string GetWriteError();
276
277         /** Returns true if there is data to be written that hasn't been sent yet
278          * @return True if the buffer is not empty
279          */
280         bool HasBufferedOutput();
281
282         /** Checks if the connection replied to its last ping, and if it did
283          * sends another and returns true, if not, returns false.
284          * @return True if the server is still replying to pings
285          */
286         bool CheckPing();
287
288         /** Resets the ping counter
289          */
290         void ResetPing();
291 };
292
293
294 /** Please note: classes serverrec and userrec both inherit from class connection.
295  */
296 class connection : public Extensible
297 {
298  public:
299         /** File descriptor of the connection
300          */
301         int fd;
302         
303         /** Hostname of connection. Not used if this is a serverrec
304          */
305         char host[160];
306         
307         /** IP of connection.
308          */
309         char ip[16];
310         
311         /** Stats counter for bytes inbound
312          */
313         int bytes_in;
314
315         /** Stats counter for bytes outbound
316          */
317         int bytes_out;
318
319         /** Stats counter for commands inbound
320          */
321         int cmds_in;
322
323         /** Stats counter for commands outbound
324          */
325         int cmds_out;
326
327         /** True if server/user has authenticated, false if otherwise
328          */
329         bool haspassed;
330
331         /** Port number
332          * For a userrec, this is the port they connected to the network on.
333          * For a serverrec this is the current listening port of the serverrec object.
334          */
335         int port;
336         
337         /** Used by userrec to indicate the registration status of the connection
338          */
339         char registered;
340         
341         /** Time the connection was last pinged
342          */
343         time_t lastping;
344         
345         /** Time the connection was created, set in the constructor
346          */
347         time_t signon;
348         
349         /** Time that the connection last sent data, used to calculate idle time
350          */
351         time_t idle_lastmsg;
352         
353         /** Used by PING checks with clients
354          */
355         time_t nping;
356         
357         /** Default constructor
358          */
359         connection();
360 };
361
362
363 #endif