]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/connection.h
Services speedups for stupid buffered i/o uplinks
[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
31 #ifndef __CONNECTION_H__
32 #define __CONNECTION_H__
33
34 #define STATE_DISCONNECTED      0
35 #define STATE_CONNECTED         1
36 #define STATE_SYNC              2
37 #define STATE_NOAUTH_INBOUND    3
38 #define STATE_NOAUTH_OUTBOUND   4
39 #define STATE_SERVICES          5
40
41 /** Each connection has one or more of these
42  * each represents ONE outbound connection to another ircd
43  * so each inbound has multiple outbounds. A listening socket
44  * that accepts server type connections is represented by one
45  * class serverrec. Class serverrec will instantiate several
46  * objects of type ircd_connector to represent each established
47  * connection, inbound or outbound. So, to determine all linked
48  * servers you must walk through all the serverrecs that the
49  * core defines, and in each one iterate through until you find
50  * connection(s) relating to the server you want information on.
51  * The core and module API provide functions for this.
52  */
53 class ircd_connector : public Extensible
54 {
55  private:
56         /** Sockaddr of the outbound ip and port
57          */
58         sockaddr_in addr;
59         
60         /** File descriptor of the connection
61          */
62         int fd;
63         
64         /** Server name
65          */
66         std::string servername;
67         
68         /** Server 'GECOS'
69          */
70         std::string description;
71         
72         /** State. STATE_NOAUTH_INBOUND, STATE_NOAUTH_OUTBOUND
73          * STATE_SYNC, STATE_DISCONNECTED, STATE_CONNECTED
74          */
75         int state;
76         
77         /** PRIVATE function to set the host address and port to connect to
78          */
79         bool SetHostAddress(char* host, int port);
80
81         /** IRCD Buffer for input characters, holds one line
82          */
83         std::string ircdbuffer;
84
85  public:
86  
87         /** When MakeOutboundConnection is called, these public members are
88          * filled with the details passed to the function, for future
89          * reference
90          */
91         char host[MAXBUF];
92
93         /** When MakeOutboundConnection is called, these public members are
94          * filled with the details passed to the function, for future
95          * reference
96          */
97         int port;
98         
99         /** Server names of servers that this server is linked to
100          * So for A->B->C, if this was the record for B it would contain A and C
101          * whilever both servers are connected to B.
102          */
103         std::vector<std::string> routes;
104         
105
106         /** Create an outbound connection to a listening socket
107          */ 
108         bool MakeOutboundConnection(char* host, int port);
109         
110         /** Return the servername on this established connection
111          */
112         std::string GetServerName();
113         
114         /** Set the server name of this connection
115          */
116         void SetServerName(std::string serv);
117         
118         /** Get the file descriptor associated with this connection
119          */
120         int GetDescriptor();
121         
122         /** Set the file descriptor for this connection
123          */
124         void SetDescriptor(int fd);
125         
126         /** Get the state flags for this connection
127          */
128         int GetState();
129         
130         /** Set the state flags for this connection
131          */
132         void SetState(int state);
133         
134         /** Get the ip address (not servername) associated with this connection
135          */
136         char* GetServerIP();
137         
138         /** Get the server description of this connection
139          */
140         std::string GetDescription();
141         
142         /** Set the server description of this connection
143          */
144         void SetDescription(std::string desc);
145         
146         /** Get the port number being used for this connection
147          * If the connection is outbound this will be the remote port
148          * otherwise it will be the local port, so it can always be
149          * gautanteed as open at the address given in GetServerIP().
150          */
151         int GetServerPort();
152         
153         /** Set the port used by this connection
154          */
155         void SetServerPort(int p);
156         
157         /** Set both the host and the port in one operation for this connection
158          */
159         bool SetHostAndPort(char* host, int port);
160         
161         /** Close the connection by calling close() on its file descriptor
162          * This function call updates no other data.
163          */
164         void CloseConnection();
165
166         void AddBuffer(std::string a);
167         bool BufferIsComplete();
168         void ClearBuffer();
169         std::string GetBuffer();
170 };
171
172
173 /** Please note: classes serverrec and userrec both inherit from class connection.
174  */
175 class connection : public Extensible
176 {
177  public:
178         /** File descriptor of the connection
179          */
180         int fd;
181         
182         /** Hostname of connection. Not used if this is a serverrec
183          */
184         char host[256];
185         
186         /** IP of connection. Reserved for future use.
187          */
188         char ip[32];
189         
190         /** Inbuf of connection. Only used for userrec
191          */
192         char inbuf[MAXBUF];
193         
194         /** Stats counter for bytes inbound
195          */
196         long bytes_in;
197
198         /** Stats counter for bytes outbound
199          */
200         long bytes_out;
201
202         /** Stats counter for commands inbound
203          */
204         long cmds_in;
205
206         /** Stats counter for commands outbound
207          */
208         long cmds_out;
209
210         /** True if server/user has authenticated, false if otherwise
211          */
212         bool haspassed;
213
214         /** Port number
215          * For a userrec, this is the port they connected to the network on.
216          * For a serverrec this is the current listening port of the serverrec object.
217          */
218         int port;
219         
220         /** Used by userrec to indicate the registration status of the connection
221          */
222         int registered;
223         
224         /** Reserved for future use
225          */
226         short int state;
227         
228         /** Time the connection was last pinged
229          */
230         time_t lastping;
231         
232         /** Time the connection was created, set in the constructor
233          */
234         time_t signon;
235         
236         /** Time that the connection last sent data, used to calculate idle time
237          */
238         time_t idle_lastmsg;
239         
240         /** Used by PING checks with clients
241          */
242         time_t nping;
243         
244         /** Unused, will be removed in a future alpha/beta
245          */
246         char internal_addr[MAXBUF];
247         
248         /** Unused, will be removed in a future alpha/beta
249          */
250         int internal_port;
251
252         /** With a serverrec, this is a list of all established server connections.
253          * With a userrec this is unused.
254          */
255         std::vector<ircd_connector> connectors;
256         
257         /** Default constructor
258          */
259         connection();
260         
261         /** Create a listening socket on 'host' using port number 'p'
262          */
263         bool CreateListener(char* host, int p);
264         
265         /** Begin an outbound link to another ircd at targethost.
266          */
267         bool BeginLink(char* targethost, int port, char* password, char* servername, int myport);
268         
269         /** Begin an outbound mesh link to another ircd on a network you are already an authenticated member of
270          */
271         bool MeshCookie(char* targethost, int port, long cookie, char* servername);
272         
273         /** Terminate a link to 'targethost' by calling the ircd_connector::CloseConnection method.
274          */
275         void TerminateLink(char* targethost);
276         
277         /** Send a message to a server by name, if the server is unavailable directly route the packet via another server
278          * If the server still cannot be reached after attempting to route the message remotely, returns false.
279          */
280         bool SendPacket(char *message, const char* host);
281         
282         /** Returns the next available packet and returns true if data is available. Writes the servername the data came from to 'host'.
283          * If no data is available this function returns false.
284          * This function will automatically close broken links and reroute pathways, generating split messages on the network.
285          */
286         bool RecvPacket(std::deque<std::string> &messages, char* host);
287         
288         /** Find the ircd_connector oject related to a certain servername given in 'host'
289          */
290         ircd_connector* FindHost(std::string host);
291         
292         /** Add an incoming connection to the connection pool.
293          * (reserved for core use)
294          */
295         bool AddIncoming(int fd,char* targethost, int sourceport);
296         
297         /** This function is deprecated and may be removed in a later alpha/beta
298          */
299         long GenKey();
300 };
301
302
303 #endif
304