1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #include "inspircd_config.h"
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <netinet/in.h>
31 #ifndef __CONNECTION_H__
32 #define __CONNECTION_H__
34 #define STATE_DISCONNECTED 0
35 #define STATE_CONNECTED 1
37 #define STATE_NOAUTH_INBOUND 3
38 #define STATE_NOAUTH_OUTBOUND 4
39 #define STATE_SERVICES 5
41 /** Each connection has one or more of these
42 * each represents ONE outbound connection to another ircd
43 * so each inbound has multiple outbounds. A listening socket
44 * that accepts server type connections is represented by one
45 * class serverrec. Class serverrec will instantiate several
46 * objects of type ircd_connector to represent each established
47 * connection, inbound or outbound. So, to determine all linked
48 * servers you must walk through all the serverrecs that the
49 * core defines, and in each one iterate through until you find
50 * connection(s) relating to the server you want information on.
51 * The core and module API provide functions for this.
53 class ircd_connector : public Extensible
56 /** Sockaddr of the outbound ip and port
60 /** File descriptor of the connection
66 std::string servername;
70 std::string description;
72 /** State. STATE_NOAUTH_INBOUND, STATE_NOAUTH_OUTBOUND
73 * STATE_SYNC, STATE_DISCONNECTED, STATE_CONNECTED
77 /** PRIVATE function to set the host address and port to connect to
79 bool SetHostAddress(char* host, int port);
83 /** When MakeOutboundConnection is called, these public members are
84 * filled with the details passed to the function, for future
89 /** When MakeOutboundConnection is called, these public members are
90 * filled with the details passed to the function, for future
95 /** Server names of servers that this server is linked to
96 * So for A->B->C, if this was the record for B it would contain A and C
97 * whilever both servers are connected to B.
99 std::vector<std::string> routes;
102 /** Create an outbound connection to a listening socket
104 bool MakeOutboundConnection(char* host, int port);
106 /** Return the servername on this established connection
108 std::string GetServerName();
110 /** Set the server name of this connection
112 void SetServerName(std::string serv);
114 /** Get the file descriptor associated with this connection
118 /** Set the file descriptor for this connection
120 void SetDescriptor(int fd);
122 /** Get the state flags for this connection
126 /** Set the state flags for this connection
128 void SetState(int state);
130 /** Get the ip address (not servername) associated with this connection
134 /** Get the server description of this connection
136 std::string GetDescription();
138 /** Set the server description of this connection
140 void SetDescription(std::string desc);
142 /** Get the port number being used for this connection
143 * If the connection is outbound this will be the remote port
144 * otherwise it will be the local port, so it can always be
145 * gautanteed as open at the address given in GetServerIP().
149 /** Set the port used by this connection
151 void SetServerPort(int p);
153 /** Set both the host and the port in one operation for this connection
155 bool SetHostAndPort(char* host, int port);
157 /** Close the connection by calling close() on its file descriptor
158 * This function call updates no other data.
160 void CloseConnection();
164 /** Please note: classes serverrec and userrec both inherit from class connection.
166 class connection : public Extensible
169 /** File descriptor of the connection
173 /** Hostname of connection. Not used if this is a serverrec
177 /** IP of connection. Reserved for future use.
181 /** Inbuf of connection. Only used for userrec
185 /** Stats counter for bytes inbound
189 /** Stats counter for bytes outbound
193 /** Stats counter for commands inbound
197 /** Stats counter for commands outbound
201 /** True if server/user has authenticated, false if otherwise
206 * For a userrec, this is the port they connected to the network on.
207 * For a serverrec this is the current listening port of the serverrec object.
211 /** Used by userrec to indicate the registration status of the connection
215 /** Reserved for future use
219 /** Time the connection was last pinged
223 /** Time the connection was created, set in the constructor
227 /** Time that the connection last sent data, used to calculate idle time
231 /** Used by PING checks with clients
235 /** Unused, will be removed in a future alpha/beta
237 char internal_addr[MAXBUF];
239 /** Unused, will be removed in a future alpha/beta
243 /** With a serverrec, this is a list of all established server connections.
244 * With a userrec this is unused.
246 std::vector<ircd_connector> connectors;
248 /** Default constructor
252 /** Create a listening socket on 'host' using port number 'p'
254 bool CreateListener(char* host, int p);
256 /** Begin an outbound link to another ircd at targethost.
258 bool BeginLink(char* targethost, int port, char* password, char* servername, int myport);
260 /** Begin an outbound mesh link to another ircd on a network you are already an authenticated member of
262 bool MeshCookie(char* targethost, int port, long cookie, char* servername);
264 /** Terminate a link to 'targethost' by calling the ircd_connector::CloseConnection method.
266 void TerminateLink(char* targethost);
268 /** Send a message to a server by name, if the server is unavailable directly route the packet via another server
269 * If the server still cannot be reached after attempting to route the message remotely, returns false.
271 bool SendPacket(char *message, const char* host);
273 /** Returns the next available packet and returns true if data is available. Writes the servername the data came from to 'host'.
274 * If no data is available this function returns false.
275 * This function will automatically close broken links and reroute pathways, generating split messages on the network.
277 bool RecvPacket(std::deque<std::string> &messages, char* host);
279 /** Find the ircd_connector oject related to a certain servername given in 'host'
281 ircd_connector* FindHost(std::string host);
283 /** Add an incoming connection to the connection pool.
284 * (reserved for core use)
286 bool AddIncoming(int fd,char* targethost, int sourceport);
288 /** This function is deprecated and may be removed in a later alpha/beta