]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/connection.h
3699c1abbfca55064177b72e6f4746c1ebedccc7
[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 #define STATE_COOKIE_OUTBOUND   6
42
43 std::string CreateSum();
44
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.
56  */
57 class ircd_connector : public Extensible
58 {
59  private:
60         /** Sockaddr of the outbound ip and port
61          */
62         sockaddr_in addr;
63         
64         /** File descriptor of the connection
65          */
66         int fd;
67         
68         /** Server name
69          */
70         std::string servername;
71         
72         /** Server 'GECOS'
73          */
74         std::string description;
75         
76         /** State. STATE_NOAUTH_INBOUND, STATE_NOAUTH_OUTBOUND
77          * STATE_SYNC, STATE_DISCONNECTED, STATE_CONNECTED
78          */
79         char state;
80         
81         /** PRIVATE function to set the host address and port to connect to
82          */
83         bool SetHostAddress(char* host, int port);
84
85         /** This string holds the ircd's version response
86          */
87         std::string version;
88
89         /** SendQ of the outbound connector, does not have a limit
90          */
91         std::string sendq;
92
93         /** Write error of connection
94          */
95         std::string WriteError;
96
97         /** Time this connection was last pinged
98          */
99         time_t nextping;
100
101         /** Did this connection reply to its last ping?
102          */
103         bool replied;
104
105  public:
106
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.
110          */
111         std::string ircdbuffer;
112
113  
114         /** When MakeOutboundConnection is called, these public members are
115          * filled with the details passed to the function, for future
116          * reference
117          */
118         char host[MAXBUF];
119
120         /** When MakeOutboundConnection is called, these public members are
121          * filled with the details passed to the function, for future
122          * reference
123          */
124         int port;
125         
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.
129          */
130         std::vector<std::string> routes;
131
132         /** Constructor clears the sendq and initialises the fd to -1
133          */
134         ircd_connector();       
135
136         /** Create an outbound connection to a listening socket
137          */ 
138         bool MakeOutboundConnection(char* newhost, int newport);
139         
140         /** Return the servername on this established connection
141          */
142         std::string GetServerName();
143         
144         /** Set the server name of this connection
145          * @param serv The server name to set
146          */
147         void SetServerName(std::string serv);
148         
149         /** Get the file descriptor associated with this connection
150          * @return The file descriptor associated with this connection
151          */
152         int GetDescriptor();
153         
154         /** Set the file descriptor for this connection
155          * @param fd The file descriptor to associate with the connection
156          */
157         void SetDescriptor(int fd);
158         
159         /** Get the state flags for this connection
160          * @return The state flags associated with this connection
161          */
162         int GetState();
163         
164         /** Set the state flags for this connection
165          * @param state The state flags to set for this connection
166          */
167         void SetState(int state);
168         
169         /** Get the ip address (not servername) associated with this connection
170          * @return The connections IP address in dotted decimal form
171          */
172         char* GetServerIP();
173         
174         /** Get the server description of this connection
175          * @return The description (GECOS) of this connection
176          */
177         std::string GetDescription();
178         
179         /** Set the server description of this connection
180          * @param desc The description (GECOS) of this connection to be set
181          */
182         void SetDescription(std::string desc);
183         
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().
188          *
189          * @return The port number of this connection
190          */
191         int GetServerPort();
192         
193         /** Set the port used by this connection
194          * @param p The port number to set for this connection
195          */
196         void SetServerPort(int p);
197         
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
202          */
203         bool SetHostAndPort(char* newhost, int newport);
204         
205         /** Close the connection by calling close() on its file descriptor
206          * This function call updates no other data.
207          */
208         void CloseConnection();
209
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
212          *
213          * This buffer's maximum size is one megabyte, the method returning false
214          * if the buffer is full.
215          *
216          * @return True on success, false if the buffer is full or the connection is down
217          */
218         bool AddBuffer(std::string a);
219
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.
223          *
224          * @return True if there is at least one complete line waiting to be processed
225          */
226         bool BufferIsComplete();
227
228         /** This method clears the server's buffer by setting it to an empty string.
229          */
230         void ClearBuffer();
231
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().
235          *
236          * @return The first line of the buffer up to a carriage return
237          */
238         std::string GetBuffer();
239
240         /** This method sets the version string of the remote server
241          * @param newversion The version string to set
242          */
243         void SetVersionString(std::string newversion);
244
245         /** This method returns the version string of the remote server.
246          * If the server has no version string an empty string is returned.
247          *
248          * @return The version text of this connection
249          */
250         std::string GetVersionString();
251
252         /** Adds data to the connection's sendQ to be flushed later.
253          * @param data The data to add to the write buffer
254          *
255          * Fails if there is an error pending on the connection.
256          *
257          * @return True on success, false if the connection is down or the buffer is full
258          */
259         bool AddWriteBuf(std::string data);
260
261         /** Flushes as much of the data from the buffer as possible,
262          * and advances the queue pointer to what is left.
263          *
264          * @return True if the flush succeeded, false if the connection is down
265          */
266         bool FlushWriteBuf();
267
268         /** Sets the error string for this connection
269          * @param error The error string to set
270          */
271         void SetWriteError(std::string error);
272
273         /** Gets the error string for this connection
274          * @return The last error to occur or an empty string
275          */
276         std::string GetWriteError();
277
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
280          */
281         bool HasBufferedOutput();
282
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
286          */
287         bool CheckPing();
288
289         /** Resets the ping counter
290          */
291         void ResetPing();
292 };
293
294
295 /** Please note: classes serverrec and userrec both inherit from class connection.
296  */
297 class connection : public Extensible
298 {
299  public:
300         /** File descriptor of the connection
301          */
302         int fd;
303         
304         /** Hostname of connection. Not used if this is a serverrec
305          */
306         char host[160];
307         
308         /** IP of connection.
309          */
310         char ip[16];
311         
312         /** Stats counter for bytes inbound
313          */
314         int bytes_in;
315
316         /** Stats counter for bytes outbound
317          */
318         int bytes_out;
319
320         /** Stats counter for commands inbound
321          */
322         int cmds_in;
323
324         /** Stats counter for commands outbound
325          */
326         int cmds_out;
327
328         /** True if server/user has authenticated, false if otherwise
329          */
330         bool haspassed;
331
332         /** Port number
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.
335          */
336         int port;
337         
338         /** Used by userrec to indicate the registration status of the connection
339          */
340         char registered;
341         
342         /** Time the connection was last pinged
343          */
344         time_t lastping;
345         
346         /** Time the connection was created, set in the constructor
347          */
348         time_t signon;
349         
350         /** Time that the connection last sent data, used to calculate idle time
351          */
352         time_t idle_lastmsg;
353         
354         /** Used by PING checks with clients
355          */
356         time_t nping;
357         
358         /** Default constructor
359          */
360         connection();
361 };
362
363
364 #endif