5 #include "inspircd_config.h"
9 #include <sys/socket.h>
10 #include <sys/types.h>
12 #include <netinet/in.h>
19 #ifndef __CONNECTION_H__
20 #define __CONNECTION_H__
22 #define STATE_DISCONNECTED 0
23 #define STATE_CONNECTED 1
25 #define STATE_NOAUTH_INBOUND 3
26 #define STATE_NOAUTH_OUTBOUND 4
27 #define STATE_SERVICES 5
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.
41 class ircd_connector : public classbase
44 /** Sockaddr of the outbound ip and port
48 /** File descriptor of the connection
54 std::string servername;
58 std::string description;
60 /** State. STATE_NOAUTH_INBOUND, STATE_NOAUTH_OUTBOUND
61 * STATE_SYNC, STATE_DISCONNECTED, STATE_CONNECTED
65 /** PRIVATE function to set the host address and port to connect to
67 bool SetHostAddress(char* host, int port);
71 /** When MakeOutboundConnection is called, these public members are
72 * filled with the details passed to the function, for future
77 /** When MakeOutboundConnection is called, these public members are
78 * filled with the details passed to the function, for future
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.
87 std::vector<std::string> routes;
90 /** Create an outbound connection to a listening socket
92 bool MakeOutboundConnection(char* host, int port);
94 /** Return the servername on this established connection
96 std::string GetServerName();
98 /** Set the server name of this connection
100 void SetServerName(std::string serv);
102 /** Get the file descriptor associated with this connection
106 /** Set the file descriptor for this connection
108 void SetDescriptor(int fd);
110 /** Get the state flags for this connection
114 /** Set the state flags for this connection
116 void SetState(int state);
118 /** Get the ip address (not servername) associated with this connection
122 /** Get the server description of this connection
124 std::string GetDescription();
126 /** Set the server description of this connection
128 void SetDescription(std::string desc);
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().
137 /** Set the port used by this connection
139 void SetServerPort(int p);
141 /** Set both the host and the port in one operation for this connection
143 bool SetHostAndPort(char* host, int port);
145 /** Close the connection by calling close() on its file descriptor
146 * This function call updates no other data.
148 void CloseConnection();
152 /** Class packet is deprecated.
153 * This declaration is preserved here to maintain documentation only.
155 class packet : public classbase
159 /** Please note: classes serverrec and userrec both inherit from class connection.
161 class connection : public classbase
164 /** File descriptor of the connection
168 /** Hostname of connection. Not used if this is a serverrec
172 /** IP of connection. Reserved for future use.
176 /** Inbuf of connection. Only used for userrec
180 /** Stats counter for bytes inbound
184 /** Stats counter for bytes outbound
188 /** Stats counter for commands inbound
192 /** Stats counter for commands outbound
196 /** True if server/user has authenticated, false if otherwise
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.
206 /** Used by userrec to indicate the registration status of the connection
210 /** Reserved for future use
214 /** Time the connection was last pinged
218 /** Time the connection was created, set in the constructor
222 /** Time that the connection last sent data, used to calculate idle time
226 /** Used by PING checks with clients
230 /** Unused, will be removed in a future alpha/beta
232 char internal_addr[MAXBUF];
234 /** Unused, will be removed in a future alpha/beta
238 /** With a serverrec, this is a list of all established server connections.
239 * With a userrec this is unused.
241 std::vector<ircd_connector> connectors;
243 /** Default constructor
247 /** Create a listening socket on 'host' using port number 'p'
249 bool CreateListener(char* host, int p);
251 /** Begin an outbound link to another ircd at targethost.
253 bool BeginLink(char* targethost, int port, char* password, char* servername, int myport);
255 /** Begin an outbound mesh link to another ircd on a network you are already an authenticated member of
257 bool MeshCookie(char* targethost, int port, long cookie, char* servername);
259 /** Terminate a link to 'targethost' by calling the ircd_connector::CloseConnection method.
261 void TerminateLink(char* targethost);
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.
266 bool SendPacket(char *message, const char* host);
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.
272 bool RecvPacket(std::deque<std::string> &messages, char* host);
274 /** Find the ircd_connector oject related to a certain servername given in 'host'
276 ircd_connector* FindHost(std::string host);
278 /** Add an incoming connection to the connection pool.
279 * (reserved for core use)
281 bool AddIncoming(int fd,char* targethost, int sourceport);
283 /** This function is deprecated and may be removed in a later alpha/beta