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