]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/connection.h
Changed ordering of sys/socket.h and sys/types.h to fix compile error on gcc 2.95
[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  public:
82  
83         /** When MakeOutboundConnection is called, these public members are
84          * filled with the details passed to the function, for future
85          * reference
86          */
87         char host[MAXBUF];
88
89         /** When MakeOutboundConnection is called, these public members are
90          * filled with the details passed to the function, for future
91          * reference
92          */
93         int port;
94         
95         /** Server names of servers that this server is linked to
96          * So for A->B->C, if this was the record for B it would contain A and C
97          * whilever both servers are connected to B.
98          */
99         std::vector<std::string> routes;
100         
101
102         /** Create an outbound connection to a listening socket
103          */ 
104         bool MakeOutboundConnection(char* host, int port);
105         
106         /** Return the servername on this established connection
107          */
108         std::string GetServerName();
109         
110         /** Set the server name of this connection
111          */
112         void SetServerName(std::string serv);
113         
114         /** Get the file descriptor associated with this connection
115          */
116         int GetDescriptor();
117         
118         /** Set the file descriptor for this connection
119          */
120         void SetDescriptor(int fd);
121         
122         /** Get the state flags for this connection
123          */
124         int GetState();
125         
126         /** Set the state flags for this connection
127          */
128         void SetState(int state);
129         
130         /** Get the ip address (not servername) associated with this connection
131          */
132         char* GetServerIP();
133         
134         /** Get the server description of this connection
135          */
136         std::string GetDescription();
137         
138         /** Set the server description of this connection
139          */
140         void SetDescription(std::string desc);
141         
142         /** Get the port number being used for this connection
143          * If the connection is outbound this will be the remote port
144          * otherwise it will be the local port, so it can always be
145          * gautanteed as open at the address given in GetServerIP().
146          */
147         int GetServerPort();
148         
149         /** Set the port used by this connection
150          */
151         void SetServerPort(int p);
152         
153         /** Set both the host and the port in one operation for this connection
154          */
155         bool SetHostAndPort(char* host, int port);
156         
157         /** Close the connection by calling close() on its file descriptor
158          * This function call updates no other data.
159          */
160         void CloseConnection();
161 };
162
163
164 /** Class packet is deprecated.
165  * This declaration is preserved here to maintain documentation only.
166  */
167 class packet : public classbase
168 {
169 };
170
171 /** Please note: classes serverrec and userrec both inherit from class connection.
172  */
173 class connection : public Extensible
174 {
175  public:
176         /** File descriptor of the connection
177          */
178         int fd;
179         
180         /** Hostname of connection. Not used if this is a serverrec
181          */
182         char host[256];
183         
184         /** IP of connection. Reserved for future use.
185          */
186         char ip[32];
187         
188         /** Inbuf of connection. Only used for userrec
189          */
190         char inbuf[MAXBUF];
191         
192         /** Stats counter for bytes inbound
193          */
194         long bytes_in;
195
196         /** Stats counter for bytes outbound
197          */
198         long bytes_out;
199
200         /** Stats counter for commands inbound
201          */
202         long cmds_in;
203
204         /** Stats counter for commands outbound
205          */
206         long cmds_out;
207
208         /** True if server/user has authenticated, false if otherwise
209          */
210         bool haspassed;
211
212         /** Port number
213          * For a userrec, this is the port they connected to the network on.
214          * For a serverrec this is the current listening port of the serverrec object.
215          */
216         int port;
217         
218         /** Used by userrec to indicate the registration status of the connection
219          */
220         int registered;
221         
222         /** Reserved for future use
223          */
224         short int state;
225         
226         /** Time the connection was last pinged
227          */
228         time_t lastping;
229         
230         /** Time the connection was created, set in the constructor
231          */
232         time_t signon;
233         
234         /** Time that the connection last sent data, used to calculate idle time
235          */
236         time_t idle_lastmsg;
237         
238         /** Used by PING checks with clients
239          */
240         time_t nping;
241         
242         /** Unused, will be removed in a future alpha/beta
243          */
244         char internal_addr[MAXBUF];
245         
246         /** Unused, will be removed in a future alpha/beta
247          */
248         int internal_port;
249
250         /** With a serverrec, this is a list of all established server connections.
251          * With a userrec this is unused.
252          */
253         std::vector<ircd_connector> connectors;
254         
255         /** Default constructor
256          */
257         connection();
258         
259         /** Create a listening socket on 'host' using port number 'p'
260          */
261         bool CreateListener(char* host, int p);
262         
263         /** Begin an outbound link to another ircd at targethost.
264          */
265         bool BeginLink(char* targethost, int port, char* password, char* servername, int myport);
266         
267         /** Begin an outbound mesh link to another ircd on a network you are already an authenticated member of
268          */
269         bool MeshCookie(char* targethost, int port, long cookie, char* servername);
270         
271         /** Terminate a link to 'targethost' by calling the ircd_connector::CloseConnection method.
272          */
273         void TerminateLink(char* targethost);
274         
275         /** Send a message to a server by name, if the server is unavailable directly route the packet via another server
276          * If the server still cannot be reached after attempting to route the message remotely, returns false.
277          */
278         bool SendPacket(char *message, const char* host);
279         
280         /** Returns the next available packet and returns true if data is available. Writes the servername the data came from to 'host'.
281          * If no data is available this function returns false.
282          * This function will automatically close broken links and reroute pathways, generating split messages on the network.
283          */
284         bool RecvPacket(std::deque<std::string> &messages, char* host);
285         
286         /** Find the ircd_connector oject related to a certain servername given in 'host'
287          */
288         ircd_connector* FindHost(std::string host);
289         
290         /** Add an incoming connection to the connection pool.
291          * (reserved for core use)
292          */
293         bool AddIncoming(int fd,char* targethost, int sourceport);
294         
295         /** This function is deprecated and may be removed in a later alpha/beta
296          */
297         long GenKey();
298 };
299
300
301 #endif
302