]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/connection.h
Removed deprecated class 'packet'
[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 /** Please note: classes serverrec and userrec both inherit from class connection.
165  */
166 class connection : public Extensible
167 {
168  public:
169         /** File descriptor of the connection
170          */
171         int fd;
172         
173         /** Hostname of connection. Not used if this is a serverrec
174          */
175         char host[256];
176         
177         /** IP of connection. Reserved for future use.
178          */
179         char ip[32];
180         
181         /** Inbuf of connection. Only used for userrec
182          */
183         char inbuf[MAXBUF];
184         
185         /** Stats counter for bytes inbound
186          */
187         long bytes_in;
188
189         /** Stats counter for bytes outbound
190          */
191         long bytes_out;
192
193         /** Stats counter for commands inbound
194          */
195         long cmds_in;
196
197         /** Stats counter for commands outbound
198          */
199         long cmds_out;
200
201         /** True if server/user has authenticated, false if otherwise
202          */
203         bool haspassed;
204
205         /** Port number
206          * For a userrec, this is the port they connected to the network on.
207          * For a serverrec this is the current listening port of the serverrec object.
208          */
209         int port;
210         
211         /** Used by userrec to indicate the registration status of the connection
212          */
213         int registered;
214         
215         /** Reserved for future use
216          */
217         short int state;
218         
219         /** Time the connection was last pinged
220          */
221         time_t lastping;
222         
223         /** Time the connection was created, set in the constructor
224          */
225         time_t signon;
226         
227         /** Time that the connection last sent data, used to calculate idle time
228          */
229         time_t idle_lastmsg;
230         
231         /** Used by PING checks with clients
232          */
233         time_t nping;
234         
235         /** Unused, will be removed in a future alpha/beta
236          */
237         char internal_addr[MAXBUF];
238         
239         /** Unused, will be removed in a future alpha/beta
240          */
241         int internal_port;
242
243         /** With a serverrec, this is a list of all established server connections.
244          * With a userrec this is unused.
245          */
246         std::vector<ircd_connector> connectors;
247         
248         /** Default constructor
249          */
250         connection();
251         
252         /** Create a listening socket on 'host' using port number 'p'
253          */
254         bool CreateListener(char* host, int p);
255         
256         /** Begin an outbound link to another ircd at targethost.
257          */
258         bool BeginLink(char* targethost, int port, char* password, char* servername, int myport);
259         
260         /** Begin an outbound mesh link to another ircd on a network you are already an authenticated member of
261          */
262         bool MeshCookie(char* targethost, int port, long cookie, char* servername);
263         
264         /** Terminate a link to 'targethost' by calling the ircd_connector::CloseConnection method.
265          */
266         void TerminateLink(char* targethost);
267         
268         /** Send a message to a server by name, if the server is unavailable directly route the packet via another server
269          * If the server still cannot be reached after attempting to route the message remotely, returns false.
270          */
271         bool SendPacket(char *message, const char* host);
272         
273         /** Returns the next available packet and returns true if data is available. Writes the servername the data came from to 'host'.
274          * If no data is available this function returns false.
275          * This function will automatically close broken links and reroute pathways, generating split messages on the network.
276          */
277         bool RecvPacket(std::deque<std::string> &messages, char* host);
278         
279         /** Find the ircd_connector oject related to a certain servername given in 'host'
280          */
281         ircd_connector* FindHost(std::string host);
282         
283         /** Add an incoming connection to the connection pool.
284          * (reserved for core use)
285          */
286         bool AddIncoming(int fd,char* targethost, int sourceport);
287         
288         /** This function is deprecated and may be removed in a later alpha/beta
289          */
290         long GenKey();
291 };
292
293
294 #endif
295