File: networking.c

package info (click to toggle)
ayttm 0.6.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,368 kB
  • ctags: 8,851
  • sloc: ansic: 65,755; sh: 10,810; cpp: 3,092; makefile: 561; yacc: 294; lex: 53; sed: 16
file content (696 lines) | stat: -rw-r--r-- 15,933 bytes parent folder | download | duplicates (2)
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
 * Ayttm
 *
 * Copyright (C) 2009 the Ayttm team
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/* this is a little piece of code to handle proxy connection */
/* it is intended to : 1st handle http proxy, using the CONNECT command
 , 2nd provide an easy way to add socks support */

#include "intl.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

#ifdef __MINGW32__
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif

#include "networking.h"
#include "net_constants.h"
#include "proxy_private.h"
#include "common.h"
#include "ssl.h"

#include <glib.h>

#ifdef __MINGW32__
#define sleep(a)		Sleep(1000*a)

#define bcopy(a,b,c)	memcpy(b,a,c)
#define bzero(a,b)		memset(a,0,b)

#endif

/* The connection function for any proxy */
typedef int (*AyConnectionHandler) (int sockfd, const char *host, int port,
	gpointer con);

typedef struct {
	AyInputCallback callback;
	void *cb_data;
	AyConnection *connection;
	int tag;
} AyInputCallbackData;

static GSList *ay_input_callback_data_list = NULL;
static GSList *ay_active_attempts = NULL;

typedef struct {
	int sockfd;
	int listenfd;
#ifdef HAVE_OPENSSL
	SSL *ssl;
#endif
} AySource;

typedef struct {
	AyConnection *con;
	int id;
} ActiveAttempt;

/* Private structure of an active connection */
struct _AyConnectionPrivate {

	/* Hostname */
	char *host;

	/* Port */
	int port;

	/* Connection info. Socket family, IP address, etc. */
	struct sockaddr info;

	/* Proxy. NULL if the connection is direct */
	AyProxyData *proxy;

	/* Connection Type See AyConnectionType for valid values */
	AyConnectionType type;

	/* A Source representing the file descriptor or SSL socket */
	AySource fd;

	/* Status of connection. See the AyConnectionStatus enum */
	AyConnectionStatus status;
};

/* Used to establish a connection */
typedef struct {

	/* The Connection  I will eventually return */
	AyConnection *connection;

	/* What we should call when connection is completed */
	AyConnectCallback callback;

	/* Update status in the requestor */
	AyStatusCallback status_callback;

	/* Check if certificate is to be accepted or not */
	AyCertVerifyCallback verify_callback;

	/* Return value from verify_callback */
	int ret;

	/* Message for verify_callback */
	const char *message;

	/* Message Title for verify_callback */
	const char *title;

	/* Points to the appropriate connection function in case of proxy */
	AyConnectionHandler connect;

	/* What the requestor wants us to give back once we're done */
	gpointer cb_data;

	/* What the main thread uses to keep track of its thread */
	GAsyncQueue *leash;

	/* So that someone can talk back as well */
	GAsyncQueue *backleash;

	/* Tag for the polling idle function -- useful as a unique ID */
	guint source;

} _AyConnector;

static gboolean check_connect_complete(gpointer data)
{
	_AyConnector *con = data;

	if (g_async_queue_try_pop(con->leash)) {
		GSList *l = ay_active_attempts;

		if (con->connection->priv->status == AY_UI_REQUEST) {
			GAsyncQueue *backleash = con->backleash;

			con->ret =
				con->verify_callback(con->message, con->title);

			g_async_queue_ref(backleash);
			g_async_queue_push(backleash, con);
			g_async_queue_unref(backleash);

			return TRUE;
		}
		/* Clean up if there was an error or a cancellation */
		if (con->connection->priv->status != AY_NONE) {
			ay_connection_disconnect(con->connection);

		}

		con->callback(con->connection, con->connection->priv->status,
			con->cb_data);

		g_async_queue_unref(con->leash);

		g_free(con);

		/* Remove the connection attempt */
		while (l) {
			ActiveAttempt *a = l->data;

			if (a->con == con->connection) {
				ay_active_attempts =
					g_slist_remove(ay_active_attempts, a);
				g_free(a);
				break;
			}

			l = g_slist_next(l);
		}

		return FALSE;
	}

	return TRUE;
}

static gboolean ay_check_continue_connecting(_AyConnector *con,
	GAsyncQueue *leash)
{
	if (con->connection->priv->status == AY_CANCEL_CONNECT) {
		if (con->callback) {
			g_async_queue_push(leash, con);
			g_async_queue_unref(leash);
			g_async_queue_unref(con->backleash);
		}

		return FALSE;
	}

	return TRUE;
}

static gpointer _do_connect(gpointer data)
{
	int retval = 0;
	_AyConnector *con = (_AyConnector *)data;
	char message[255];
	GAsyncQueue *leash = NULL;

	/* Tell the main thread that I have begun */
	if (con->callback) {
		g_async_queue_ref(con->leash);
		g_async_queue_ref(con->backleash);
		leash = con->leash;
		g_async_queue_push(con->leash, con);

		if (!ay_check_continue_connecting(con, leash))
			return NULL;
	}

	if (con->status_callback != NULL) {
		snprintf(message, sizeof(message), _("Connecting to %s"),
			con->connection->priv->host);
		con->status_callback(message, con->cb_data);
	}

	/* If it's a server connection then just wait for a connection */
	if (con->connection->priv->type == AY_CONNECTION_TYPE_SERVER) {
		int newfd = 0;

		if ((newfd = accept(con->connection->priv->fd.listenfd, NULL,
					0)) < 0) {
			con->connection->priv->status = errno;
			con->connection->priv->fd.sockfd = -1;
		} else {
			con->connection->priv->fd.sockfd = newfd;
		}
	}
	/* Direct Connection */
	else if (!con->connect) {
		con->connection->priv->fd.sockfd =
			connect_address(con->connection->priv->host,
			con->connection->priv->port, &con->connection->priv->info);

		if (!ay_check_continue_connecting(con, leash))
			return NULL;

		if (con->connection->priv->fd.sockfd < 0) {
			con->connection->priv->status =
				con->connection->priv->fd.sockfd;
			con->connection->priv->fd.sockfd = -1;
		}
	}
	/* Proxy */
	else {
		int proxyfd = 0;

		if (con->status_callback != NULL) {
			snprintf(message, sizeof(message),
				_("Connecting to proxy %s"),
				con->connection->priv->proxy->host);
			con->status_callback(message, con->cb_data);
		}

		/* First get a connection to the proxy */
		proxyfd =
			connect_address(con->connection->priv->proxy->host,
			con->connection->priv->proxy->port, NULL);

		if (proxyfd >= 0) {
			if (!ay_check_continue_connecting(con, leash)) {
				close(proxyfd);
				return NULL;
			}

			if (con->status_callback != NULL) {
				snprintf(message, sizeof(message),
					_("Connecting to %s"),
					con->connection->priv->proxy->host);
				con->status_callback(message, con->cb_data);
			}

			retval = con->connect(proxyfd,
				con->connection->priv->host,
				con->connection->priv->port,
				con->connection->priv->proxy);

			if (!ay_check_continue_connecting(con, leash)) {
				close(proxyfd);
				return NULL;
			}

			if (retval < 0)
				con->connection->priv->status = retval;
			else
				con->connection->priv->fd.sockfd = proxyfd;
		} else {
			con->connection->priv->status = proxyfd;
			con->connection->priv->fd.sockfd = -1;
		}
	}

#ifdef HAVE_OPENSSL

	if (con->connection->priv->type == AY_CONNECTION_TYPE_SSL
		&& con->connection->priv->fd.sockfd >= 0) {
		con->connection->priv->fd.ssl =
			ssl_get_socket(con->connection->priv->fd.sockfd,
			con->connection->priv->host,
			con->connection->priv->port, con);

		if (con->connection->priv->fd.ssl == NULL)
			con->connection->priv->status = AY_SSL_CERT_REJECT;
	}
#endif

	if (con->callback) {
		fcntl(con->connection->priv->fd.sockfd, F_SETFL, O_NONBLOCK);

		g_async_queue_push(leash, con);
		g_async_queue_unref(leash);
		g_async_queue_unref(con->backleash);
	}

	return NULL;
}

gboolean ay_connection_verify(const char *message, const char *title,
	void *data)
{
	GAsyncQueue *leash;
	_AyConnector *con = (_AyConnector *)data;

	int status = con->connection->priv->status;

	con->message = message;
	con->title = title;

	g_async_queue_ref(con->leash);
	leash = con->leash;

	con->connection->priv->status = AY_UI_REQUEST;

	g_async_queue_push(leash, con);
	g_async_queue_pop(con->backleash);
	con->connection->priv->status = status;
	g_async_queue_unref(leash);

	return con->ret;
}

AyConnection *ay_connection_new(const char *host, int port,
	AyConnectionType type)
{
	AyConnection *con = g_new0(AyConnection, 1);

	con->priv = g_new0(struct _AyConnectionPrivate, 1);

	con->priv->host = strdup(host);
	con->priv->port = port;
	con->priv->type = type;

	con->priv->fd.sockfd = -1;
	con->priv->fd.listenfd = -1;

	con->priv->proxy = default_proxy;

	return con;
}

void ay_connection_free(AyConnection *con)
{
	ay_connection_disconnect(con);
	GSList *l = ay_active_attempts;

	if (!con || !con->priv) {
		fprintf(stderr,
			"Something is wrong... trying to free a NULL connection "
			"(con=%p, priv=%p)\n", con, (con ? con->priv : 0x0));
		return;
	}

	while (l) {
		ActiveAttempt *a = l->data;

		if (a->con == con) {
			a->con->priv->status = AY_CANCEL_CONNECT;
			ay_active_attempts =
				g_slist_remove(ay_active_attempts, a);
			g_free(a);
			return;
		}

		l = g_slist_next(l);
	}

	if (con->priv->host)
		free(con->priv->host);

	g_free(con->priv);
	free(con);
}

/* Cancel a connection. Lets the thread know that we're no longer interested in its connection */
void ay_connection_cancel_connect(int tag)
{
	GSList *l = ay_active_attempts;

	while (l) {
		ActiveAttempt *a = l->data;

		if (a->id == tag) {
			a->con->priv->status = AY_CANCEL_CONNECT;
			ay_active_attempts =
				g_slist_remove(ay_active_attempts, a);
			g_free(a);
			return;
		}

		l = g_slist_next(l);
	}

}

int ay_connection_listen(AyConnection *con)
{
	struct sockaddr_in addr;

	con->priv->fd.listenfd = socket(AF_INET, SOCK_STREAM, 0);

	if (con->priv->fd.listenfd == -1)
		return -1;

	memset(&addr, 0, sizeof(struct sockaddr_in));

	addr.sin_family = AF_INET;
	addr.sin_port = htons(con->priv->port);

	/* Bind and listen to the socket */
	if (bind(con->priv->fd.listenfd, (struct sockaddr *)(&addr),
			sizeof(addr)) < 0
		|| listen(con->priv->fd.listenfd, 1) < 0) {
		close(con->priv->fd.listenfd);
		return -1;
	}

	return 0;
}

int ay_connection_connect(AyConnection *con, AyConnectCallback cb,
	AyStatusCallback scb, AyCertVerifyCallback vfy_cb, void *data)
{
	_AyConnector *connector = g_new0(_AyConnector, 1);

	connector->connection = con;
	connector->callback = cb;
	connector->cb_data = data;
	connector->status_callback = scb;
	connector->verify_callback = vfy_cb;

	if (!default_proxy || default_proxy->type == PROXY_NONE)
		connector->connect = NULL;
	else if (default_proxy->type == PROXY_HTTP)
		connector->connect = (AyConnectionHandler) http_connect;
	else if (default_proxy->type == PROXY_SOCKS4)
		connector->connect = (AyConnectionHandler) socks4_connect;
	else if (default_proxy->type == PROXY_SOCKS5)
		connector->connect = (AyConnectionHandler) socks5_connect;
	else {
		printf("Invalid proxy\n");
		return AY_INVALID_CONNECT_DATA;
	}

	if (cb) {
		ActiveAttempt *attempt = g_new0(ActiveAttempt, 1);

		ay_active_attempts =
			g_slist_append(ay_active_attempts, attempt);

		connector->leash = g_async_queue_new();
		connector->backleash = g_async_queue_new();

		g_thread_create(_do_connect, connector, FALSE, NULL);

		/* Wait till the connector thread starts */
		g_async_queue_pop(connector->leash);

		attempt->con = con;
		attempt->id = connector->source =
			g_idle_add(check_connect_complete, connector);

		return connector->source;
	} else {
		_do_connect(connector);

		if (con->priv->status)
			return con->priv->status;
		else
			return 0;
	}
}

void ay_connection_disconnect(AyConnection *con)
{
	if (!con || !con->priv)
		return;

#ifdef HAVE_OPENSSL
	if (con->priv->fd.ssl) {
		ssl_done_socket(con->priv->fd.ssl);
		con->priv->fd.ssl = NULL;
	}
#endif

	if (con->priv->fd.sockfd >= 0) {
		close(con->priv->fd.sockfd);
		con->priv->fd.sockfd = -1;
	}

	if (con->priv->fd.listenfd >= 0) {
		close(con->priv->fd.listenfd);
		con->priv->fd.listenfd = -1;
	}
}

char *ay_connection_get_ip_addr(AyConnection *con)
{
	return g_strdup_printf("%u.%u.%u.%u", con->priv->info.sa_data[2] & 0xff,
		con->priv->info.sa_data[3] & 0xff,
		con->priv->info.sa_data[4] & 0xff,
		con->priv->info.sa_data[5] & 0xff);
}

int ay_connection_read(AyConnection *con, void *buf, int len)
{
	if (!con || con->priv->fd.sockfd < 0)
		return -1;

#ifdef HAVE_OPENSSL
	if (con->priv->type == AY_CONNECTION_TYPE_SSL)
		return ssl_read(con->priv->fd.ssl, buf, len);
	else
#endif
		return read(con->priv->fd.sockfd, buf, len);
}

int ay_connection_write(AyConnection *con, const void *buf, int len)
{
	if (!con || con->priv->fd.sockfd < 0)
		return -1;

#ifdef HAVE_OPENSSL
	if (con->priv->type == AY_CONNECTION_TYPE_SSL)
		return ssl_write(con->priv->fd.ssl, buf, len);
	else
#endif
		return write(con->priv->fd.sockfd, buf, len);
}

gboolean ay_connection_is_active(AyConnection *con)
{
	if (con && con->priv && con->priv->fd.sockfd >= 0)
		return TRUE;
	else
		return FALSE;
}

static void ay_connection_ready(void *data, int fd, eb_input_condition cond)
{
	AyInputCallbackData *cb_data = data;

	cb_data->callback(cb_data->connection, cond, cb_data->cb_data);
}

int ay_connection_input_add(AyConnection *con, eb_input_condition cond,
	AyInputCallback func, void *cb_data)
{
	AyInputCallbackData *data = g_new0(AyInputCallbackData, 1);

	if (!con || !con->priv || con->priv->fd.sockfd == -1)
		return -1;

	data->callback = func;
	data->connection = con;
	data->cb_data = cb_data;

	if (con->priv->type == AY_CONNECTION_TYPE_SERVER
		&& con->priv->fd.sockfd < 0)
		data->tag =
			eb_input_add(con->priv->fd.listenfd, cond,
			ay_connection_ready, data);
	else
		data->tag =
			eb_input_add(con->priv->fd.sockfd, cond,
			ay_connection_ready, data);

	ay_input_callback_data_list =
		g_slist_append(ay_input_callback_data_list, data);

	return data->tag;
}

void ay_connection_input_remove(int tag)
{
	GSList *input = ay_input_callback_data_list;

	while (input) {
		if (((AyInputCallbackData *)input->data)->tag == tag) {
			eb_input_remove(tag);
			void *tmp = input->data;
			ay_input_callback_data_list =
				g_slist_remove(ay_input_callback_data_list,
				tmp);
			g_free(tmp);

			return;
		}

		input = g_slist_next(input);
	}
}

/* Wow!! A new deprecated function! */
void ay_connection_free_no_close(AyConnection *con)
{
	if (!con || !con->priv) {
		fprintf(stderr,
			"Something is wrong... trying to free a NULL connection "
			"(con=%p, priv=%p)\n", con, (con ? con->priv : 0x0));
		return;
	}

	if (con->priv->host)
		free(con->priv->host);

	g_free(con->priv);
	free(con);
}

/* ... and another one! */

int ay_connection_get_fd(AyConnection *con)
{
	return con->priv->fd.sockfd;
}

AyConnectionType ay_connection_get_type(AyConnection *con)
{
	return con->priv->type;
}

static char *errors[] = {
	"Success",
	"Connection Refused",
	"Hostname Lookup failed",
	"Permission denied",
	"Proxy requires Authentication",
	"Socks4: Disconnected due to an unknown error",
	"Socks4: Authentication failed",
	"Socks4: Invalid username",
	"Socks4: Incompatible",
	"Socks5: Connection failed",
	"Invalid Connection information",
	"Connection cancelled",
	"Certificate Rejected",
	"Interrupt. You should not be getting this request. File a bug report",
	"Connection Refused"
};

const char *ay_connection_strerror(int error)
{
	if (error <= 0 && error > AY_NUM_ERRORS)
		return errors[0 - error];

	/* Assume this is an errno */
	printf("no return code found for %d\n", error);
	return strerror(error);
}