summaryrefslogtreecommitdiff
path: root/src/connection.cpp
blob: 48127086e795a3884ba92da40cf377518e0a7183 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <connection.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <vector>
#include <string>
#include <deque>
#include "inspircd.h"
#include "modules.h"

using namespace std;

extern std::vector<Module*> modules;
extern std::vector<ircd_module*> factory;

extern int MODCOUNT;


packet::packet()
{
	srand(time(NULL));
	id = random();
}

packet::~packet()
{
}

connection::connection()
{
	key = GenKey();
	fd = 0;
}


bool connection::CreateListener(char* host, int p)
{
	sockaddr_in host_address;
	int flags;
	in_addr addy;
	int on = 0;
	struct linger linger = { 0 };
	
	fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (fd <= 0)
	{
		return false;
	}

	memset((void*)&host_address, 0, sizeof(host_address));

	host_address.sin_family = AF_INET;

	if (!strcmp(host,""))
	{
		host_address.sin_addr.s_addr = htonl(INADDR_ANY);
	}
	else
	{
  		inet_aton(host,&addy);
		host_address.sin_addr = addy;
	}

	host_address.sin_port = htons(p);

	if (bind(fd,(sockaddr*)&host_address,sizeof(host_address))<0)
	{
		return false;
	}

	// make the socket non-blocking
	flags = fcntl(fd, F_GETFL, 0);
	fcntl(fd, F_SETFL, flags | O_NONBLOCK);

	this->port = p;

	setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(const char*)&on,sizeof(on));
	linger.l_onoff = 1;
	linger.l_linger = 0;
	setsockopt(fd,SOL_SOCKET,SO_LINGER,(const char*)&linger,sizeof(linger));
	
	// attempt to increase socket sendq and recvq as high as its possible
	// to get them on linux.
	int sendbuf = 32768;
	int recvbuf = 32768;
	setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
	setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
	
	listen(this->fd,5);

	return true;
}

char* ircd_connector::GetServerIP()
{
	return this->host;
}

int ircd_connector::GetServerPort()
{
	return this->port;
}

bool ircd_connector::SetHostAndPort(char* host, int port)
{
	strncpy(this->host,host,160);
	this->port = port;
	return true;
}

bool ircd_connector::SetHostAddress(char* host, int port)
{
	strncpy(this->host,host,160);
	this->port = port;
	memset((void*)&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	inet_aton(host,&addr.sin_addr);
	addr.sin_port = htons(port);
	return true;
}

bool ircd_connector::MakeOutboundConnection(char* host, int port)
{
	hostent* hoste = gethostbyname(host);
	if (!hoste)
	{
		WriteOpers("Failed to look up hostname for %s, using as an ip address",host);
		this->SetHostAddress(host,port);
	}
	else
	{
		WriteOpers("Found hostname for %s",host);
		this->SetHostAddress(hoste->h_addr,port);
	}

	this->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (this->fd >= 0)
	{
		if(connect(this->fd, (sockaddr*)&addr,sizeof(addr)))
		{
			WriteOpers("connect() failed for %s",host);
			return false;
		}
		int flags = fcntl(this->fd, F_GETFL, 0);
		fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
		int sendbuf = 32768;
		int recvbuf = 32768;
		setsockopt(this->fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
		setsockopt(this->fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
		return true;
	}
	else
	{
		WriteOpers("socket() failed!");
	}

	return false;
}


bool connection::BeginLink(char* targethost, int port, char* password, char* servername)
{
	char connect[MAXBUF];
	
	ircd_connector connector;
	
	if (this->fd)
	{
		if (connector.MakeOutboundConnection(targethost,port))
		{
			// targethost has been turned into an ip...
			// we dont want this as the server name.
			connector.SetServerName(servername);
			sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
			connector.SetState(STATE_NOAUTH_OUTBOUND);
			this->connectors.push_back(connector);
			return this->SendPacket(connect, servername);
		}
		else
		{
			WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
		}
	}
	return false;
}

bool connection::MeshCookie(char* targethost, int port, long cookie, char* servername)
{
	char connect[MAXBUF];
	
	ircd_connector connector;
	
	WriteOpers("Establishing meshed link to %s:%d",targethost,port);

	if (this->fd)
	{
		if (connector.MakeOutboundConnection(targethost,port))
		{
			// targethost has been turned into an ip...
			// we dont want this as the server name.
			connector.SetServerName(servername);
			sprintf(connect,"- %d %s :%s",cookie,getservername().c_str(),getserverdesc().c_str());
			connector.SetState(STATE_NOAUTH_OUTBOUND);
			this->connectors.push_back(connector);
			return this->SendPacket(connect, servername);
		}
		else
		{
			WriteOpers("Could not create outbound connection to %s:%d",targethost,port);
		}
	}
	return false;
}

bool connection::AddIncoming(int fd,char* targethost, int sourceport)
{
	char connect[MAXBUF];
	
	ircd_connector connector;
	
	// targethost has been turned into an ip...
	// we dont want this as the server name.
	connector.SetHostAndPort(targethost, sourceport);
	connector.SetServerName(targethost);
	connector.SetDescriptor(fd);
	connector.SetState(STATE_NOAUTH_INBOUND);
	int flags = fcntl(fd, F_GETFL, 0);
	fcntl(fd, F_SETFL, flags | O_NONBLOCK);
	int sendbuf = 32768;
	int recvbuf = 32768;
	setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf)); 
	setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
	log(DEBUG,"connection::AddIncoming() Added connection: %s:%d",host,sourceport);
	this->connectors.push_back(connector);
	return true;
}

void connection::TerminateLink(char* targethost)
{
	// this locates the targethost in the connection::connectors vector of the class,
 	// and terminates it by sending it an SQUIT token and closing its descriptor.
	// TerminateLink with a null string causes a terminate of ALL links
}


// Returns a pointer to the connector for 'host'
ircd_connector* connection::FindHost(std::string host)
{
	for (int i = 0; i < this->connectors.size(); i++)
	{
		if (this->connectors[i].GetServerName() == host)
		{
			return &this->connectors[i];
		}
	}
	return NULL;
}

std::string ircd_connector::GetServerName()
{
	return this->servername;
}

void ircd_connector::SetServerName(std::string serv)
{
	this->servername = serv;
}


int ircd_connector::GetDescriptor()
{
	return this->fd;
}

int ircd_connector::GetState()
{
	return this->state;
}


void ircd_connector::SetState(int state)
{
	this->state = state;
}

void ircd_connector::SetDescriptor(int fd)
{
	this->fd = fd;
}

bool connection::SendPacket(char *message, const char* host)
{
	ircd_connector* cn = this->FindHost(host);
	
	if (cn)
	{
		log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s",message,cn->GetServerName().c_str());

		strncat(message,"\n",MAXBUF);
		// returns false if the packet could not be sent (e.g. target host down)
		if (send(cn->GetDescriptor(),message,strlen(message),0)<0)
		{
			log(DEBUG,"send() failed for Connection::SendPacket(): %s",strerror(errno));
			return false;
		}
		return true;
	}
}

// receives a packet from any where there is data waiting, first come, first served
// fills the message and host values with the host where the data came from.

bool connection::RecvPacket(std::deque<std::string> &messages, char* host)
{
	char data[32767];
	memset(data, 0, 32767);
	for (int i = 0; i < this->connectors.size(); i++)
	{
		// returns false if the packet could not be sent (e.g. target host down)
		int rcvsize = 0;
		if (rcvsize = recv(this->connectors[i].GetDescriptor(),data,32767,0))
		{
			if (rcvsize > 0)
			{
				char* l = strtok(data,"\n");
				while (l)
				{
					char sanitized[32767];
					memset(sanitized, 0, 32767);
					int ptt = 0;
					for (int pt = 0; pt < strlen(l); pt++)
					{
						if (l[pt] != '\r')
						{
							sanitized[ptt++] = l[pt];
						}
					}
					sanitized[ptt] = '\0';
					if (strlen(sanitized))
					{
						messages.push_back(sanitized);
						strncpy(host,this->connectors[i].GetServerName().c_str(),160);
						log(DEBUG,"main: Connection::RecvPacket() got '%s' from %s",sanitized,host);
						
					}
					l = strtok(NULL,"\n");
				}
				return true;
			}
		}
	}
	// nothing new yet -- message and host will be undefined
	return false;
}

long connection::GenKey()
{
	srand(time(NULL));
	return (random()*time(NULL));
}