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
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.
40 class ircd_connector : public classbase
43 /** Sockaddr of the outbound ip and port
47 /** File descriptor of the connection
53 std::string servername;
57 std::string description;
59 /** State. STATE_NOAUTH_INBOUND, STATE_NOAUTH_OUTBOUND
60 * STATE_SYNC, STATE_DISCONNECTED, STATE_CONNECTED
64 /** PRIVATE function to set the host address and port to connect to
66 bool SetHostAddress(char* host, int port);
70 /** When MakeOutboundConnection is called, these public members are
71 * filled with the details passed to the function, for future
76 /** When MakeOutboundConnection is called, these public members are
77 * filled with the details passed to the function, for future
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.
86 std::vector<std::string> routes;
89 /** Create an outbound connection to a listening socket
91 bool MakeOutboundConnection(char* host, int port);
93 /** Return the servername on this established connection
95 std::string GetServerName();
97 /** Set the server name of this connection
99 void SetServerName(std::string serv);
101 /** Get the file descriptor associated with this connection
105 /** Set the file descriptor for this connection
107 void SetDescriptor(int fd);
109 /** Get the state flags for this connection
113 /** Set the state flags for this connection
115 void SetState(int state);
117 /** Get the ip address (not servername) associated with this connection
121 /** Get the server description of this connection
123 std::string GetDescription();
125 /** Set the server description of this connection
127 void SetDescription(std::string desc);
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().
136 /** Set the port used by this connection
138 void SetServerPort(int p);
140 /** Set both the host and the port in one operation for this connection
142 bool SetHostAndPort(char* host, int port);
144 /** Close the connection by calling close() on its file descriptor
145 * This function call updates no other data.
147 void CloseConnection();
151 /** Class packet is deprecated.
152 * This declaration is preserved here to maintain documentation only.
158 /** Please note: classes serverrec and userrec both inherit from class connection.
160 class connection : public classbase
163 /** File descriptor of the connection
167 /** Hostname of connection. Not used if this is a serverrec
171 /** IP of connection. Reserved for future use.
175 /** Inbuf of connection. Only used for userrec
179 /** Stats counter for bytes inbound
183 /** Stats counter for bytes outbound
187 /** Stats counter for commands inbound
191 /** Stats counter for commands outbound
195 /** True if server/user has authenticated, false if otherwise
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.
205 /** Used by userrec to indicate the registration status of the connection
209 /** Reserved for future use
213 /** Time the connection was last pinged
217 /** Time the connection was created, set in the constructor
221 /** Time that the connection last sent data, used to calculate idle time
225 /** Used by PING checks with clients
229 /** Unused, will be removed in a future alpha/beta
231 char internal_addr[1024];
233 /** Unused, will be removed in a future alpha/beta
237 /** With a serverrec, this is a list of all established server connections.
238 * With a userrec this is unused.
240 std::vector<ircd_connector> connectors;
242 /** Default constructor
246 /** Create a listening socket on 'host' using port number 'p'
248 bool CreateListener(char* host, int p);
250 /** Begin an outbound link to another ircd at targethost.
252 bool BeginLink(char* targethost, int port, char* password, char* servername, int myport);
254 /** Begin an outbound mesh link to another ircd on a network you are already an authenticated member of
256 bool MeshCookie(char* targethost, int port, long cookie, char* servername);
258 /** Terminate a link to 'targethost' by calling the ircd_connector::CloseConnection method.
260 void TerminateLink(char* targethost);
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.
265 bool SendPacket(char *message, const char* host);
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.
271 bool RecvPacket(std::deque<std::string> &messages, char* host);
273 /** Find the ircd_connector oject related to a certain servername given in 'host'
275 ircd_connector* FindHost(std::string host);
277 /** Add an incoming connection to the connection pool.
278 * (reserved for core use)
280 bool AddIncoming(int fd,char* targethost, int sourceport);
282 /** This function is deprecated and may be removed in a later alpha/beta