summaryrefslogtreecommitdiff
path: root/src/connection.cpp
blob: 7a92afcd66293a1330f1008d39fc1c1b7aad3d9c (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
#include <connection.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/utsname.h>
#include <errno.h>
#include "inspircd.h"
#include "modules.h"

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

extern int MODCOUNT;

#define STATE_CLEAR 1
#define STATE_WAIT_FOR_ACK 2

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

packet::~packet()
{
}

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


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_DGRAM, IPPROTO_UDP);
	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));

	return true;
}

bool connection::BeginLink(char* targethost, int port, char* password)
{
	char connect[MAXBUF];
	
	if (this->fd)
	{
		sprintf(connect,"S %s %s :%s",getservername().c_str(),password,getserverdesc().c_str());
		this->haspassed = false;
		return this->SendPacket(connect, targethost, port, 0);
	}
	return false;
}

// targethost: in dot notation a.b.c.d
void connection::TerminateLink(char* targethost)
{
}

// host: in dot notation a.b.c.d
// port: host byte order
bool connection::SendPacket(char *message, char* host, int port, long ourkey)
{
	sockaddr_in host_address;
	in_addr addy;
	packet p;

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

	host_address.sin_family = AF_INET;
	inet_aton(host,&addy);
	host_address.sin_addr = addy;

	host_address.sin_port = htons(port);

	strcpy(p.data,message);
	p.type = PT_SYN_WITH_DATA;
	p.key = ourkey;


	FOREACH_MOD OnPacketTransmit(p.data);

	log(DEBUG,"main: Connection::SendPacket() sent '%s' to %s:%d",p.data,host,port);

	// returns false if the packet could not be sent (e.g. target host down)
	if (sendto(this->fd,&p,sizeof(p),0,(sockaddr*)&host_address,sizeof(host_address))<0)
	{
		log(DEBUG,"sendto() failed for Connection::SendPacket() with a packet of size %d: %s",sizeof(p),strerror(errno));
		return false;
	}
	this->state = STATE_WAIT_FOR_ACK;


	// host_address remains unchanged. we only want to receive from where we just sent the packet to.
	
	// retry the packet up to 5 times
	for (int retries = 0; retries < 5; retries++)
	{
		socklen_t host_address_size;
		host_address.sin_family=AF_INET;
		host_address_size=sizeof(host_address);
	
		// wait for ack, or timeout.
		// if reached a timeout, send again.
		// the packet id in the ack must match that in the original packet
		// this MUST operate in lock/step fashion!!!
		int cycles = 0;
		packet p2;
		do 
		{
			fd_set sfd;
			timeval tval;
			tval.tv_usec = 100;
			tval.tv_sec = 0;
			FD_ZERO(&sfd);
			FD_SET(fd,&sfd);
			int res = select(65535, &sfd, NULL, NULL, &tval);
			cycles++;
		}
		while ((recvfrom(fd,&p2,sizeof(p2),MSG_PEEK,(sockaddr*)&host_address,&host_address_size)<0) && (cycles < 10));
		
		if (cycles >= 10)
		{
			log(DEFAULT,"ERROR! connection::SendPacket() waited >10000 nanosecs for an ACK. Will resend up to 5 times");
		}
		else
		{
			if (p2.type != PT_ACK_ONLY)
			{
				log(DEFAULT,"ERROR! connection::SendPacket() received a data response and was expecting a syn!!!");
				this->state = STATE_CLEAR;
				return true;
			}

			if (p2.id != p.id)
			{
				recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size);
				log(DEFAULT,"ERROR! connection::SendPacket() received an ack for a packet it didnt send!");
				this->state = STATE_CLEAR;
				return false;
			}
			else
			{
				recvfrom(fd,&p2,sizeof(p2),0,(sockaddr*)&host_address,&host_address_size);
				log(DEFAULT,"Successfully received ACK");
				this->state = STATE_CLEAR;
				return true;
				break;
			}
		}
	}
	log(DEFAULT,"We never received an ack. Something fishy going on, host is dead.");
	this->state = STATE_CLEAR;
	return false;

}

bool connection::SendSYN(char* host, int port)
{
	sockaddr_in host_address;
	in_addr addy;
	packet p;

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

	host_address.sin_family = AF_INET;
	inet_aton(host,&addy);
	host_address.sin_addr = addy;

	host_address.sin_port = htons(port);

	p.type = PT_SYN_ONLY;
	p.key = key;
	strcpy(p.data,"");

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

}

bool connection::SendACK(char* host, int port, int reply_id)
{
	sockaddr_in host_address;
	in_addr addy;
	packet p;

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

	host_address.sin_family = AF_INET;
	inet_aton(host,&addy);
	host_address.sin_addr = addy;

	host_address.sin_port = htons(port);

	p.type = PT_ACK_ONLY;
	p.key = key;
	p.id = reply_id;
	strcpy(p.data,"");

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

}


// Generates a server key. This is pseudo-random.
// the server always uses the same server-key in all communications
// across the network. All other servers must remember the server key
// of servers in the network, e.g.:
//
// ServerA:  key=5555555555
// ServerB:  key=6666666666
// I am ServerC: key=77777777777
//
// If ServerC sees a packet from ServerA, and the key stored for ServerA
// is 0, then cache the key as the servers key.
// after this point, any packet from ServerA which does not contain its key,
// 555555555, will be silently dropped.
// This should prevent blind spoofing, as to fake a server you must know its
// assigned key, and to do that you must receive messages that are origintated
// from it or hack the running executable.
//
// During the AUTH phase (when server passwords are checked, the key in any
// packet MUST be 0). Only the initial SERVER/PASS packets may have a key
// of 0 (and any ACK responses to them).
//

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

// host: in dot notation a.b.c.d
// port: host byte order
bool connection::RecvPacket(char *message, char* host, int &prt, long &theirkey)
{
	// returns false if no packet waiting for receive, e.g. EAGAIN or ECONNRESET
	sockaddr_in host_address;
	socklen_t host_address_size;
	packet p;
	
	memset((void*)&host_address, 0, sizeof(host_address));

	host_address.sin_family=AF_INET;
	host_address_size=sizeof(host_address);

	//int recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
	if (recvfrom(fd,&p,sizeof(p),0,(sockaddr*)&host_address,&host_address_size)<0)
	{
		return false;
	}

	log(DEBUG,"connection::RecvPacket(): received packet type %d '%s' from '%s'",p.type,p.data,inet_ntoa(host_address.sin_addr));

	if (p.type == PT_SYN_ONLY)
	{
		strcpy(message,p.data);
		strcpy(host,inet_ntoa(host_address.sin_addr));
		prt = ntohs(host_address.sin_port);
		SendACK(host,this->port,p.id);
		return false;
	}

	if (p.type == PT_ACK_ONLY)
	{
		strcpy(message,p.data);
		strcpy(host,inet_ntoa(host_address.sin_addr));
		prt = ntohs(host_address.sin_port);
		this->state = STATE_CLEAR;
		return false;
	}

	if (p.type == PT_SYN_WITH_DATA)
	{
		strcpy(message,p.data);
		strcpy(host,inet_ntoa(host_address.sin_addr));
		theirkey = p.key;
		prt = ntohs(host_address.sin_port); // the port we received it on
		SendACK(host,prt,p.id);
		return true;
	}

	log(DEBUG,"connection::RecvPacket(): Invalid packet type %d (protocol error)",p.type);
	return true;
}