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