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>
32 #ifndef __CONNECTION_H__
33 #define __CONNECTION_H__
35 #define STATE_DISCONNECTED 0
36 #define STATE_CONNECTED 1
38 #define STATE_NOAUTH_INBOUND 3
39 #define STATE_NOAUTH_OUTBOUND 4
40 #define STATE_SERVICES 5
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.
54 class ircd_connector : public Extensible
57 /** Sockaddr of the outbound ip and port
61 /** File descriptor of the connection
67 std::string servername;
71 std::string description;
73 /** State. STATE_NOAUTH_INBOUND, STATE_NOAUTH_OUTBOUND
74 * STATE_SYNC, STATE_DISCONNECTED, STATE_CONNECTED
78 /** PRIVATE function to set the host address and port to connect to
80 bool SetHostAddress(char* host, int port);
85 /** IRCD Buffer for input characters, holds one line
87 std::string ircdbuffer;
90 /** When MakeOutboundConnection is called, these public members are
91 * filled with the details passed to the function, for future
96 /** When MakeOutboundConnection is called, these public members are
97 * filled with the details passed to the function, for future
102 /** Server names of servers that this server is linked to
103 * So for A->B->C, if this was the record for B it would contain A and C
104 * whilever both servers are connected to B.
106 std::vector<std::string> routes;
109 /** Create an outbound connection to a listening socket
111 bool MakeOutboundConnection(char* newhost, int newport);
113 /** Return the servername on this established connection
115 std::string GetServerName();
117 /** Set the server name of this connection
119 void SetServerName(std::string serv);
121 /** Get the file descriptor associated with this connection
125 /** Set the file descriptor for this connection
127 void SetDescriptor(int fd);
129 /** Get the state flags for this connection
133 /** Set the state flags for this connection
135 void SetState(int state);
137 /** Get the ip address (not servername) associated with this connection
141 /** Get the server description of this connection
143 std::string GetDescription();
145 /** Set the server description of this connection
147 void SetDescription(std::string desc);
149 /** Get the port number being used for this connection
150 * If the connection is outbound this will be the remote port
151 * otherwise it will be the local port, so it can always be
152 * gautanteed as open at the address given in GetServerIP().
156 /** Set the port used by this connection
158 void SetServerPort(int p);
160 /** Set both the host and the port in one operation for this connection
162 bool SetHostAndPort(char* newhost, int newport);
164 /** Close the connection by calling close() on its file descriptor
165 * This function call updates no other data.
167 void CloseConnection();
169 void AddBuffer(std::string a);
170 bool BufferIsComplete();
172 std::string GetBuffer();
176 /** Please note: classes serverrec and userrec both inherit from class connection.
178 class connection : public Extensible
181 /** File descriptor of the connection
185 /** Hostname of connection. Not used if this is a serverrec
189 /** IP of connection. Reserved for future use.
193 /** Inbuf of connection. Only used for userrec
197 /** Stats counter for bytes inbound
201 /** Stats counter for bytes outbound
205 /** Stats counter for commands inbound
209 /** Stats counter for commands outbound
213 /** True if server/user has authenticated, false if otherwise
218 * For a userrec, this is the port they connected to the network on.
219 * For a serverrec this is the current listening port of the serverrec object.
223 /** Used by userrec to indicate the registration status of the connection
227 /** Reserved for future use
231 /** Time the connection was last pinged
235 /** Time the connection was created, set in the constructor
239 /** Time that the connection last sent data, used to calculate idle time
243 /** Used by PING checks with clients
247 /** Unused, will be removed in a future alpha/beta
249 char internal_addr[MAXBUF];
251 /** Unused, will be removed in a future alpha/beta
255 /** With a serverrec, this is a list of all established server connections.
256 * With a userrec this is unused.
258 std::vector<ircd_connector> connectors;
260 /** Default constructor
264 /** Create a listening socket on 'host' using port number 'p'
266 bool CreateListener(char* host, int p);
268 /** Begin an outbound link to another ircd at targethost.
270 bool BeginLink(char* targethost, int port, char* password, char* servername, int myport);
272 /** Begin an outbound mesh link to another ircd on a network you are already an authenticated member of
274 bool MeshCookie(char* targethost, int port, unsigned long cookie, char* servername);
276 /** Terminate a link to 'targethost' by calling the ircd_connector::CloseConnection method.
278 void TerminateLink(char* targethost);
280 /** Send a message to a server by name, if the server is unavailable directly route the packet via another server
281 * If the server still cannot be reached after attempting to route the message remotely, returns false.
283 bool SendPacket(char *message, const char* host);
285 /** Returns the next available packet and returns true if data is available. Writes the servername the data came from to 'host'.
286 * If no data is available this function returns false.
287 * This function will automatically close broken links and reroute pathways, generating split messages on the network.
289 bool RecvPacket(std::deque<std::string> &messages, char* host);
291 /** Find the ircd_connector oject related to a certain servername given in 'host'
293 ircd_connector* FindHost(std::string host);
295 /** Add an incoming connection to the connection pool.
296 * (reserved for core use)
298 bool AddIncoming(int fd,char* targethost, int sourceport);
300 /** This function is deprecated and may be removed in a later alpha/beta