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