summaryrefslogtreecommitdiff
path: root/include/connection.h
blob: a18d201efbb960c7736debae7de85357e2c46b00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*

*/

#include "inspircd_config.h"
#include "base.h"
#include <string>
#include <map>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <vector>

#ifndef __CONNECTION_H__
#define __CONNECTION_H__

#define STATE_DISCONNECTED	0
#define STATE_CONNECTED		1
#define STATE_SYNC		2
#define STATE_NOAUTH_INBOUND	3
#define STATE_NOAUTH_OUTBOUND	4

/** Each connection has one or more of these
 * each represents ONE outbound connection to another ircd
 * so each inbound has multiple outbounds.
 */
class ircd_connector : public classbase
{
 private:
	/** Sockaddr of the outbound ip and port
	 */
	sockaddr_in addr;
	
	/** File descriptor of the outbound connection
	 */
	int fd;
	
	/** Server name
	 */
	std::string servername;
	
	/** Server names of servers that this server is linked to
	 * So for A->B->C, if this was the record for B it would contain A and C
	 * whilever both servers are connected to B.
	 */
	std::vector<std::string> routes;
	
	/** State. STATE_NOAUTH_INBOUND, STATE_NOAUTH_OUTBOUND
	 * STATE_SYNC, STATE_DISCONNECTED, STATE_CONNECTED
	 */
	int state;
	
	bool SetHostAddress(char* host, int port);

 public:
 
	bool MakeOutboundConnection(char* host, int port);
	std::string GetServerName();
	void SetServerName(std::string serv);
	int GetDescriptor();
	void SetDescriptor(int fd);
	int GetState();
	void SetState(int state);
};


class packet : public classbase
{
 public:
 	long key;
 	int id;
	short int type;
	char data[MAXBUF];

	packet();
	~packet();
};


class connection : public classbase
{
 public:
 	long key;
	int fd;			// file descriptor
	char host[256];		// hostname
	long ip;		// ipv4 address
	char inbuf[MAXBUF];	// recvQ
	long bytes_in;
	long bytes_out;
	long cmds_in;
	long cmds_out;
	bool haspassed;
	int port;
	int registered;
	short int state;
	time_t lastping;
	time_t signon;
	time_t idle_lastmsg;
	time_t nping;
	char internal_addr[1024];
	int internal_port;
	std::vector<ircd_connector> connectors;
	
	connection();
	bool CreateListener(char* host, int p);
	bool BeginLink(char* targethost, int port, char* password, char* servername);
	void TerminateLink(char* targethost);
	bool SendPacket(char *message, char* host);
	bool RecvPacket(char *message, char* host);
	ircd_connector* FindHost(std::string host);
	bool AddIncoming(int fd,char* targethost);
	long GenKey();
};


#endif