]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/connection.h
420839b0dc21d2be5fd4318a66069c4d78e2492e
[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  public:
97
98         /** IRCD Buffer for input characters, holds as many lines as are
99          * pending - Note that the final line may not be complete and should
100          * only be read when there is a \n seperator.
101          */
102         std::string ircdbuffer;
103
104  
105         /** When MakeOutboundConnection is called, these public members are
106          * filled with the details passed to the function, for future
107          * reference
108          */
109         char host[MAXBUF];
110
111         /** When MakeOutboundConnection is called, these public members are
112          * filled with the details passed to the function, for future
113          * reference
114          */
115         int port;
116         
117         /** Server names of servers that this server is linked to
118          * So for A->B->C, if this was the record for B it would contain A and C
119          * whilever both servers are connected to B.
120          */
121         std::vector<std::string> routes;
122
123         /** Constructor clears the sendq and initialises the fd to -1
124          */
125         ircd_connector();       
126
127         /** Create an outbound connection to a listening socket
128          */ 
129         bool MakeOutboundConnection(char* newhost, int newport);
130         
131         /** Return the servername on this established connection
132          */
133         std::string GetServerName();
134         
135         /** Set the server name of this connection
136          */
137         void SetServerName(std::string serv);
138         
139         /** Get the file descriptor associated with this connection
140          */
141         int GetDescriptor();
142         
143         /** Set the file descriptor for this connection
144          */
145         void SetDescriptor(int fd);
146         
147         /** Get the state flags for this connection
148          */
149         int GetState();
150         
151         /** Set the state flags for this connection
152          */
153         void SetState(int state);
154         
155         /** Get the ip address (not servername) associated with this connection
156          */
157         char* GetServerIP();
158         
159         /** Get the server description of this connection
160          */
161         std::string GetDescription();
162         
163         /** Set the server description of this connection
164          */
165         void SetDescription(std::string desc);
166         
167         /** Get the port number being used for this connection
168          * If the connection is outbound this will be the remote port
169          * otherwise it will be the local port, so it can always be
170          * gautanteed as open at the address given in GetServerIP().
171          */
172         int GetServerPort();
173         
174         /** Set the port used by this connection
175          */
176         void SetServerPort(int p);
177         
178         /** Set both the host and the port in one operation for this connection
179          */
180         bool SetHostAndPort(char* newhost, int newport);
181         
182         /** Close the connection by calling close() on its file descriptor
183          * This function call updates no other data.
184          */
185         void CloseConnection();
186
187         /** This method adds text to the ircd connection's buffer
188          * There is no limitation on how much text of what line width may
189          * be added to this buffer. It is the sending server's responsibility
190          * to ensure sent data is kept within reasonable quanities.
191          */
192         void AddBuffer(std::string a);
193
194         /** This method returns true if the buffer contains at least one
195          * carriage return character, e.g. one line can be read from the
196          * buffer successfully.
197          */
198         bool BufferIsComplete();
199
200         /** This method clears the server's buffer by setting it to an empty string.
201          */
202         void ClearBuffer();
203
204         /** This method retrieves the first string from the tail end of the
205          * buffer and advances the tail end of the buffer past the returned
206          * string, in a similar manner to strtok().
207          */
208         std::string GetBuffer();
209
210         /** This method sets the version string of the remote server
211          */
212         void SetVersionString(std::string newversion);
213
214         /** This method returns the version string of the remote server.
215          * If the server has no version string an empty string is returned.
216          */
217         std::string GetVersionString();
218
219         /** Adds data to the connection's sendQ to be flushed later
220          * Fails if there is an error pending on the connection.
221          */
222         bool AddWriteBuf(std::string data);
223
224         /** Flushes as much of the data from the buffer as possible,
225          * and advances the queue pointer to what is left.
226          */
227         bool FlushWriteBuf();
228
229         /** Sets the error string for this connection
230          */
231         void SetWriteError(std::string error);
232
233         /** Gets the error string for this connection
234          */
235         std::string GetWriteError();
236
237         /** Returns true if there is data to be written that hasn't been sent yet
238          */
239         bool HasBufferedOutput();
240 };
241
242
243 /** Please note: classes serverrec and userrec both inherit from class connection.
244  */
245 class connection : public Extensible
246 {
247  public:
248         /** File descriptor of the connection
249          */
250         int fd;
251         
252         /** Hostname of connection. Not used if this is a serverrec
253          */
254         char host[160];
255         
256         /** IP of connection.
257          */
258         char ip[16];
259         
260         /** Stats counter for bytes inbound
261          */
262         int bytes_in;
263
264         /** Stats counter for bytes outbound
265          */
266         int bytes_out;
267
268         /** Stats counter for commands inbound
269          */
270         int cmds_in;
271
272         /** Stats counter for commands outbound
273          */
274         int cmds_out;
275
276         /** True if server/user has authenticated, false if otherwise
277          */
278         bool haspassed;
279
280         /** Port number
281          * For a userrec, this is the port they connected to the network on.
282          * For a serverrec this is the current listening port of the serverrec object.
283          */
284         int port;
285         
286         /** Used by userrec to indicate the registration status of the connection
287          */
288         char registered;
289         
290         /** Time the connection was last pinged
291          */
292         time_t lastping;
293         
294         /** Time the connection was created, set in the constructor
295          */
296         time_t signon;
297         
298         /** Time that the connection last sent data, used to calculate idle time
299          */
300         time_t idle_lastmsg;
301         
302         /** Used by PING checks with clients
303          */
304         time_t nping;
305         
306         /** Default constructor
307          */
308         connection();
309 };
310
311
312 #endif