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