-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy.c
2675 lines (2213 loc) · 75 KB
/
proxy.c
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file proxy.c Proxy API
* @ingroup core
*/
/* purple
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 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
, 3rd draw women to it like flies to honey */
#define _PURPLE_PROXY_C_
#include "internal.h"
#include "cipher.h"
#include "debug.h"
#include "dnsquery.h"
#include "notify.h"
#include "ntlm.h"
#include "prefs.h"
#include "proxy.h"
#include "util.h"
struct _PurpleProxyConnectData {
void *handle;
PurpleProxyConnectFunction connect_cb;
gpointer data;
gchar *host;
int port;
int fd;
int socket_type;
guint inpa;
PurpleProxyInfo *gpi;
PurpleDnsQueryData *query_data;
/**
* This contains alternating length/char* values. The char*
* values need to be freed when removed from the linked list.
*/
GSList *hosts;
PurpleProxyConnectData *child;
/*
* All of the following variables are used when establishing a
* connection through a proxy.
*/
guchar *write_buffer;
gsize write_buf_len;
gsize written_len;
PurpleInputFunction read_cb;
guchar *read_buffer;
gsize read_buf_len;
gsize read_len;
PurpleAccount *account;
};
static const char * const socks5errors[] = {
"succeeded\n",
"general SOCKS server failure\n",
"connection not allowed by ruleset\n",
"Network unreachable\n",
"Host unreachable\n",
"Connection refused\n",
"TTL expired\n",
"Command not supported\n",
"Address type not supported\n"
};
static PurpleProxyInfo *global_proxy_info = NULL;
static GSList *handles = NULL;
static void try_connect(PurpleProxyConnectData *connect_data);
/*
* TODO: Eventually (GObjectification) this bad boy will be removed, because it is
* a gross fix for a crashy problem.
*/
#define PURPLE_PROXY_CONNECT_DATA_IS_VALID(connect_data) g_slist_find(handles, connect_data)
/**************************************************************************
* Proxy structure API
**************************************************************************/
PurpleProxyInfo *
purple_proxy_info_new(void)
{
return g_new0(PurpleProxyInfo, 1);
}
void
purple_proxy_info_destroy(PurpleProxyInfo *info)
{
g_return_if_fail(info != NULL);
g_free(info->host);
g_free(info->username);
g_free(info->password);
g_free(info);
}
void
purple_proxy_info_set_type(PurpleProxyInfo *info, PurpleProxyType type)
{
g_return_if_fail(info != NULL);
info->type = type;
}
void
purple_proxy_info_set_host(PurpleProxyInfo *info, const char *host)
{
g_return_if_fail(info != NULL);
g_free(info->host);
info->host = g_strdup(host);
}
void
purple_proxy_info_set_port(PurpleProxyInfo *info, int port)
{
g_return_if_fail(info != NULL);
info->port = port;
}
void
purple_proxy_info_set_username(PurpleProxyInfo *info, const char *username)
{
g_return_if_fail(info != NULL);
g_free(info->username);
info->username = g_strdup(username);
}
void
purple_proxy_info_set_password(PurpleProxyInfo *info, const char *password)
{
g_return_if_fail(info != NULL);
g_free(info->password);
info->password = g_strdup(password);
}
PurpleProxyType
purple_proxy_info_get_type(const PurpleProxyInfo *info)
{
g_return_val_if_fail(info != NULL, PURPLE_PROXY_NONE);
return info->type;
}
const char *
purple_proxy_info_get_host(const PurpleProxyInfo *info)
{
g_return_val_if_fail(info != NULL, NULL);
return info->host;
}
int
purple_proxy_info_get_port(const PurpleProxyInfo *info)
{
g_return_val_if_fail(info != NULL, 0);
return info->port;
}
const char *
purple_proxy_info_get_username(const PurpleProxyInfo *info)
{
g_return_val_if_fail(info != NULL, NULL);
return info->username;
}
const char *
purple_proxy_info_get_password(const PurpleProxyInfo *info)
{
g_return_val_if_fail(info != NULL, NULL);
return info->password;
}
/**************************************************************************
* Global Proxy API
**************************************************************************/
PurpleProxyInfo *
purple_global_proxy_get_info(void)
{
return global_proxy_info;
}
void
purple_global_proxy_set_info(PurpleProxyInfo *info)
{
g_return_if_fail(info != NULL);
purple_proxy_info_destroy(global_proxy_info);
global_proxy_info = info;
}
/* index in gproxycmds below, keep them in sync */
#define GNOME_PROXY_MODE 0
#define GNOME_PROXY_USE_SAME_PROXY 1
#define GNOME_PROXY_SOCKS_HOST 2
#define GNOME_PROXY_SOCKS_PORT 3
#define GNOME_PROXY_HTTP_HOST 4
#define GNOME_PROXY_HTTP_PORT 5
#define GNOME_PROXY_HTTP_USER 6
#define GNOME_PROXY_HTTP_PASS 7
#define GNOME2_CMDS 0
#define GNOME3_CMDS 1
/* detect proxy settings for gnome2/gnome3 */
static const char* gproxycmds[][2] = {
{ "gconftool-2 -g /system/proxy/mode" , "gsettings get org.gnome.system.proxy mode" },
{ "gconftool-2 -g /system/http_proxy/use_same_proxy", "gsettings get org.gnome.system.proxy use-same-proxy" },
{ "gconftool-2 -g /system/proxy/socks_host", "gsettings get org.gnome.system.proxy.socks host" },
{ "gconftool-2 -g /system/proxy/socks_port", "gsettings get org.gnome.system.proxy.socks port" },
{ "gconftool-2 -g /system/http_proxy/host", "gsettings get org.gnome.system.proxy.http host" },
{ "gconftool-2 -g /system/http_proxy/port", "gsettings get org.gnome.system.proxy.http port"},
{ "gconftool-2 -g /system/http_proxy/authentication_user", "gsettings get org.gnome.system.proxy.http authentication-user" },
{ "gconftool-2 -g /system/http_proxy/authentication_password", "gsettings get org.gnome.system.proxy.http authentication-password" },
};
/**
* This is a utility function used to retrieve proxy parameter values from
* GNOME 2/3 environment.
*
* @param parameter One of the GNOME_PROXY_x constants defined above
* @param gnome_version GNOME2_CMDS or GNOME3_CMDS
*
* @return The value of requested proxy parameter
*/
static char *
purple_gnome_proxy_get_parameter(guint8 parameter, guint8 gnome_version)
{
gchar *param, *err;
size_t param_len;
if (parameter > GNOME_PROXY_HTTP_PASS)
return NULL;
if (gnome_version > GNOME3_CMDS)
return NULL;
if (!g_spawn_command_line_sync(gproxycmds[parameter][gnome_version],
¶m, &err, NULL, NULL))
return NULL;
g_free(err);
g_strstrip(param);
if (param[0] == '\'' || param[0] == '\"') {
param_len = strlen(param);
memmove(param, param + 1, param_len); /* copy last \0 too */
--param_len;
if (param_len > 0 && (param[param_len - 1] == '\'' || param[param_len - 1] == '\"'))
param[param_len - 1] = '\0';
g_strstrip(param);
}
return param;
}
static PurpleProxyInfo *
purple_gnome_proxy_get_info(void)
{
static PurpleProxyInfo info = {0, NULL, 0, NULL, NULL};
gboolean use_same_proxy = FALSE;
gchar *tmp;
guint8 gnome_version = GNOME3_CMDS;
tmp = g_find_program_in_path("gsettings");
if (tmp == NULL) {
tmp = g_find_program_in_path("gconftool-2");
gnome_version = GNOME2_CMDS;
}
if (tmp == NULL)
return purple_global_proxy_get_info();
g_free(tmp);
/* Check whether to use a proxy. */
tmp = purple_gnome_proxy_get_parameter(GNOME_PROXY_MODE, gnome_version);
if (!tmp)
return purple_global_proxy_get_info();
if (purple_strequal(tmp, "none")) {
info.type = PURPLE_PROXY_NONE;
g_free(tmp);
return &info;
}
if (!purple_strequal(tmp, "manual")) {
/* Unknown setting. Fallback to using our global proxy settings. */
g_free(tmp);
return purple_global_proxy_get_info();
}
g_free(tmp);
/* Free the old fields */
if (info.host) {
g_free(info.host);
info.host = NULL;
}
if (info.username) {
g_free(info.username);
info.username = NULL;
}
if (info.password) {
g_free(info.password);
info.password = NULL;
}
tmp = purple_gnome_proxy_get_parameter(GNOME_PROXY_USE_SAME_PROXY, gnome_version);
if (!tmp)
return purple_global_proxy_get_info();
if (purple_strequal(tmp, "true"))
use_same_proxy = TRUE;
g_free(tmp);
if (!use_same_proxy) {
info.host = purple_gnome_proxy_get_parameter(GNOME_PROXY_SOCKS_HOST, gnome_version);
if (!info.host)
return purple_global_proxy_get_info();
}
if (!use_same_proxy && (info.host != NULL) && (*info.host != '\0')) {
info.type = PURPLE_PROXY_SOCKS5;
tmp = purple_gnome_proxy_get_parameter(GNOME_PROXY_SOCKS_PORT, gnome_version);
if (!tmp) {
g_free(info.host);
info.host = NULL;
return purple_global_proxy_get_info();
}
info.port = atoi(tmp);
g_free(tmp);
} else {
g_free(info.host);
info.host = purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_HOST, gnome_version);
if (!info.host)
return purple_global_proxy_get_info();
/* If we get this far then we know we're using an HTTP proxy */
info.type = PURPLE_PROXY_HTTP;
if (*info.host == '\0')
{
purple_debug_info("proxy", "Gnome proxy settings are set to "
"'manual' but no suitable proxy server is specified. Using "
"Pidgin's proxy settings instead.\n");
g_free(info.host);
info.host = NULL;
return purple_global_proxy_get_info();
}
info.username = purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_USER, gnome_version);
if (!info.username)
{
g_free(info.host);
info.host = NULL;
return purple_global_proxy_get_info();
}
info.password = purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_PASS, gnome_version);
if (!info.password)
{
g_free(info.host);
info.host = NULL;
g_free(info.username);
info.username = NULL;
return purple_global_proxy_get_info();
}
tmp = purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_PORT, gnome_version);
if (!tmp)
{
g_free(info.host);
info.host = NULL;
g_free(info.username);
info.username = NULL;
g_free(info.password);
info.password = NULL;
return purple_global_proxy_get_info();
}
info.port = atoi(tmp);
g_free(tmp);
}
return &info;
}
#ifdef _WIN32
typedef BOOL (CALLBACK* LPFNWINHTTPGETIEPROXYCONFIG)(/*IN OUT*/ WINHTTP_CURRENT_USER_IE_PROXY_CONFIG* pProxyConfig);
/* This modifies "host" in-place evilly */
static void
_proxy_fill_hostinfo(PurpleProxyInfo *info, char *host, int default_port)
{
int port = default_port;
char *d;
d = g_strrstr(host, ":");
if (d) {
*d = '\0';
d++;
if (*d)
sscanf(d, "%d", &port);
if (port == 0)
port = default_port;
}
purple_proxy_info_set_host(info, host);
purple_proxy_info_set_port(info, port);
}
static PurpleProxyInfo *
purple_win32_proxy_get_info(void)
{
static LPFNWINHTTPGETIEPROXYCONFIG MyWinHttpGetIEProxyConfig = NULL;
static gboolean loaded = FALSE;
static PurpleProxyInfo info = {0, NULL, 0, NULL, NULL};
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_proxy_config;
if (!loaded) {
loaded = TRUE;
MyWinHttpGetIEProxyConfig = (LPFNWINHTTPGETIEPROXYCONFIG)
wpurple_find_and_loadproc("winhttp.dll", "WinHttpGetIEProxyConfigForCurrentUser");
if (!MyWinHttpGetIEProxyConfig)
purple_debug_warning("proxy", "Unable to read Windows Proxy Settings.\n");
}
if (!MyWinHttpGetIEProxyConfig)
return NULL;
ZeroMemory(&ie_proxy_config, sizeof(ie_proxy_config));
if (!MyWinHttpGetIEProxyConfig(&ie_proxy_config)) {
purple_debug_error("proxy", "Error reading Windows Proxy Settings(%lu).\n", GetLastError());
return NULL;
}
/* We can't do much if it is autodetect*/
if (ie_proxy_config.fAutoDetect) {
purple_debug_error("proxy", "Windows Proxy Settings set to autodetect (not supported).\n");
/* TODO: For 3.0.0 we'll revisit this (maybe)*/
return NULL;
} else if (ie_proxy_config.lpszProxy) {
gchar *proxy_list = g_utf16_to_utf8(ie_proxy_config.lpszProxy, -1,
NULL, NULL, NULL);
/* We can't do anything about the bypass list, as we don't have the url */
/* TODO: For 3.0.0 we'll revisit this*/
/* There are proxy settings for several protocols */
if (proxy_list && *proxy_list) {
char *specific = NULL, *tmp;
/* If there is only a global proxy, which means "HTTP" */
if (!strchr(proxy_list, ';') || (specific = g_strstr_len(proxy_list, -1, "http=")) != NULL) {
if (specific) {
specific += strlen("http=");
tmp = strchr(specific, ';');
if (tmp)
*tmp = '\0';
/* specific now points the proxy server (and port) */
} else
specific = proxy_list;
purple_proxy_info_set_type(&info, PURPLE_PROXY_HTTP);
_proxy_fill_hostinfo(&info, specific, 80);
/* TODO: is there a way to set the username/password? */
purple_proxy_info_set_username(&info, NULL);
purple_proxy_info_set_password(&info, NULL);
purple_debug_info("proxy", "Windows Proxy Settings: HTTP proxy: '%s:%d'.\n",
purple_proxy_info_get_host(&info),
purple_proxy_info_get_port(&info));
} else if ((specific = g_strstr_len(proxy_list, -1, "socks=")) != NULL) {
specific += strlen("socks=");
tmp = strchr(specific, ';');
if (tmp)
*tmp = '\0';
/* specific now points the proxy server (and port) */
purple_proxy_info_set_type(&info, PURPLE_PROXY_SOCKS5);
_proxy_fill_hostinfo(&info, specific, 1080);
/* TODO: is there a way to set the username/password? */
purple_proxy_info_set_username(&info, NULL);
purple_proxy_info_set_password(&info, NULL);
purple_debug_info("proxy", "Windows Proxy Settings: SOCKS5 proxy: '%s:%d'.\n",
purple_proxy_info_get_host(&info),
purple_proxy_info_get_port(&info));
} else {
purple_debug_info("proxy", "Windows Proxy Settings: No supported proxy specified.\n");
purple_proxy_info_set_type(&info, PURPLE_PROXY_NONE);
}
}
/* TODO: Fix API to be able look at proxy bypass settings */
g_free(proxy_list);
} else {
purple_debug_info("proxy", "No Windows proxy set.\n");
purple_proxy_info_set_type(&info, PURPLE_PROXY_NONE);
}
if (ie_proxy_config.lpszAutoConfigUrl)
GlobalFree(ie_proxy_config.lpszAutoConfigUrl);
if (ie_proxy_config.lpszProxy)
GlobalFree(ie_proxy_config.lpszProxy);
if (ie_proxy_config.lpszProxyBypass)
GlobalFree(ie_proxy_config.lpszProxyBypass);
return &info;
}
#endif
/**************************************************************************
* Proxy API
**************************************************************************/
/**
* Whoever calls this needs to have called
* purple_proxy_connect_data_disconnect() beforehand.
*/
static void
purple_proxy_connect_data_destroy(PurpleProxyConnectData *connect_data)
{
handles = g_slist_remove(handles, connect_data);
if (connect_data->query_data != NULL)
purple_dnsquery_destroy(connect_data->query_data);
while (connect_data->hosts != NULL)
{
/* Discard the length... */
connect_data->hosts = g_slist_remove(connect_data->hosts, connect_data->hosts->data);
/* Free the address... */
g_free(connect_data->hosts->data);
connect_data->hosts = g_slist_remove(connect_data->hosts, connect_data->hosts->data);
}
g_free(connect_data->host);
g_free(connect_data);
}
/**
* Free all information dealing with a connection attempt and
* reset the connect_data to prepare for it to try to connect
* to another IP address.
*
* If an error message is passed in, then we know the connection
* attempt failed. If the connection attempt failed and
* connect_data->hosts is not empty then we try the next IP address.
* If the connection attempt failed and we have no more hosts
* try try then we call the callback with the given error message,
* then destroy the connect_data.
*
* @param error_message An error message explaining why the connection
* failed. This will be passed to the callback function
* specified in the call to purple_proxy_connect(). If the
* connection was successful then pass in null.
*/
static void
purple_proxy_connect_data_disconnect(PurpleProxyConnectData *connect_data, const gchar *error_message)
{
if (connect_data->child != NULL)
{
purple_proxy_connect_cancel(connect_data->child);
connect_data->child = NULL;
}
if (connect_data->inpa > 0)
{
purple_input_remove(connect_data->inpa);
connect_data->inpa = 0;
}
if (connect_data->fd >= 0)
{
close(connect_data->fd);
connect_data->fd = -1;
}
g_free(connect_data->write_buffer);
connect_data->write_buffer = NULL;
g_free(connect_data->read_buffer);
connect_data->read_buffer = NULL;
if (error_message != NULL)
{
purple_debug_error("proxy", "Connection attempt failed: %s\n",
error_message);
if (connect_data->hosts != NULL)
try_connect(connect_data);
else
{
/* Everything failed! Tell the originator of the request. */
connect_data->connect_cb(connect_data->data, -1, error_message);
purple_proxy_connect_data_destroy(connect_data);
}
}
}
/**
* This calls purple_proxy_connect_data_disconnect(), but it lets you
* specify the error_message using a printf()-like syntax.
*/
static void
purple_proxy_connect_data_disconnect_formatted(PurpleProxyConnectData *connect_data, const char *format, ...)
{
va_list args;
gchar *tmp;
va_start(args, format);
tmp = g_strdup_vprintf(format, args);
va_end(args);
purple_proxy_connect_data_disconnect(connect_data, tmp);
g_free(tmp);
}
static void
purple_proxy_connect_data_connected(PurpleProxyConnectData *connect_data)
{
purple_debug_info("proxy", "Connected to %s:%d.\n",
connect_data->host, connect_data->port);
connect_data->connect_cb(connect_data->data, connect_data->fd, NULL);
/*
* We've passed the file descriptor to the protocol, so it's no longer
* our responsibility, and we should be careful not to free it when
* we destroy the connect_data.
*/
connect_data->fd = -1;
purple_proxy_connect_data_disconnect(connect_data, NULL);
purple_proxy_connect_data_destroy(connect_data);
}
static void
socket_ready_cb(gpointer data, gint source, PurpleInputCondition cond)
{
PurpleProxyConnectData *connect_data = data;
int error = 0;
int ret;
/* If the socket-connected message had already been triggered when connect_data
* was destroyed via purple_proxy_connect_cancel(), we may get here with a freed connect_data.
*/
if (!PURPLE_PROXY_CONNECT_DATA_IS_VALID(connect_data))
return;
purple_debug_info("proxy", "Connecting to %s:%d.\n",
connect_data->host, connect_data->port);
/*
* purple_input_get_error after a non-blocking connect returns -1 if something is
* really messed up (bad descriptor, usually). Otherwise, it returns 0 and
* error holds what connect would have returned if it blocked until now.
* Thus, error == 0 is success, error == EINPROGRESS means "try again",
* and anything else is a real error.
*
* (error == EINPROGRESS can happen after a select because the kernel can
* be overly optimistic sometimes. select is just a hint that you might be
* able to do something.)
*/
ret = purple_input_get_error(connect_data->fd, &error);
if (ret == 0 && error == EINPROGRESS) {
/* No worries - we'll be called again later */
/* TODO: Does this ever happen? */
purple_debug_info("proxy", "(ret == 0 && error == EINPROGRESS)\n");
return;
}
if (ret != 0 || error != 0) {
if (ret != 0)
error = errno;
purple_debug_error("proxy", "Error connecting to %s:%d (%s).\n",
connect_data->host, connect_data->port, g_strerror(error));
purple_proxy_connect_data_disconnect(connect_data, g_strerror(error));
return;
}
purple_proxy_connect_data_connected(connect_data);
}
static gboolean
clean_connect(gpointer data)
{
purple_proxy_connect_data_connected(data);
return FALSE;
}
static void
proxy_connect_udp_none(PurpleProxyConnectData *connect_data, struct sockaddr *addr, socklen_t addrlen)
{
int flags;
purple_debug_info("proxy", "UDP Connecting to %s:%d with no proxy\n",
connect_data->host, connect_data->port);
connect_data->fd = socket(addr->sa_family, SOCK_DGRAM, 0);
if (connect_data->fd < 0)
{
purple_proxy_connect_data_disconnect_formatted(connect_data,
_("Unable to create socket: %s"), g_strerror(errno));
return;
}
flags = fcntl(connect_data->fd, F_GETFL);
fcntl(connect_data->fd, F_SETFL, flags | O_NONBLOCK);
#ifndef _WIN32
fcntl(connect_data->fd, F_SETFD, FD_CLOEXEC);
#endif
if (connect(connect_data->fd, addr, addrlen) != 0)
{
if ((errno == EINPROGRESS) || (errno == EINTR))
{
purple_debug_info("proxy", "UDP Connection in progress\n");
connect_data->inpa = purple_input_add(connect_data->fd,
PURPLE_INPUT_WRITE, socket_ready_cb, connect_data);
}
else
{
purple_proxy_connect_data_disconnect(connect_data, g_strerror(errno));
}
}
else
{
/*
* The connection happened IMMEDIATELY... strange, but whatever.
*/
int error = ETIMEDOUT;
int ret;
purple_debug_info("proxy", "UDP Connected immediately.\n");
ret = purple_input_get_error(connect_data->fd, &error);
if ((ret != 0) || (error != 0))
{
if (ret != 0)
error = errno;
purple_proxy_connect_data_disconnect(connect_data, g_strerror(error));
return;
}
/*
* We want to call the "connected" callback eventually, but we
* don't want to call it before we return, just in case.
*/
purple_timeout_add(10, clean_connect, connect_data);
}
}
static void
proxy_connect_none(PurpleProxyConnectData *connect_data, struct sockaddr *addr, socklen_t addrlen)
{
int flags;
purple_debug_info("proxy", "Connecting to %s:%d with no proxy\n",
connect_data->host, connect_data->port);
connect_data->fd = socket(addr->sa_family, SOCK_STREAM, 0);
if (connect_data->fd < 0)
{
purple_proxy_connect_data_disconnect_formatted(connect_data,
_("Unable to create socket: %s"), g_strerror(errno));
return;
}
flags = fcntl(connect_data->fd, F_GETFL);
fcntl(connect_data->fd, F_SETFL, flags | O_NONBLOCK);
#ifndef _WIN32
fcntl(connect_data->fd, F_SETFD, FD_CLOEXEC);
#endif
if (connect(connect_data->fd, addr, addrlen) != 0)
{
if ((errno == EINPROGRESS) || (errno == EINTR))
{
purple_debug_info("proxy", "Connection in progress\n");
connect_data->inpa = purple_input_add(connect_data->fd,
PURPLE_INPUT_WRITE, socket_ready_cb, connect_data);
}
else
{
purple_proxy_connect_data_disconnect(connect_data, g_strerror(errno));
}
}
else
{
/*
* The connection happened IMMEDIATELY... strange, but whatever.
*/
int error = ETIMEDOUT;
int ret;
purple_debug_info("proxy", "Connected immediately.\n");
ret = purple_input_get_error(connect_data->fd, &error);
if ((ret != 0) || (error != 0))
{
if (ret != 0)
error = errno;
purple_proxy_connect_data_disconnect(connect_data, g_strerror(error));
return;
}
/*
* We want to call the "connected" callback eventually, but we
* don't want to call it before we return, just in case.
*/
purple_timeout_add(10, clean_connect, connect_data);
}
}
/**
* This is a utility function used by the HTTP, SOCKS4 and SOCKS5
* connect functions. It writes data from a buffer to a socket.
* When all the data is written it sets up a watcher to read a
* response and call a specified function.
*/
static void
proxy_do_write(gpointer data, gint source, PurpleInputCondition cond)
{
PurpleProxyConnectData *connect_data;
const guchar *request;
gsize request_len;
int ret;
connect_data = data;
request = connect_data->write_buffer + connect_data->written_len;
request_len = connect_data->write_buf_len - connect_data->written_len;
ret = write(connect_data->fd, request, request_len);
if (ret <= 0)
{
if (errno == EAGAIN)
/* No worries */
return;
/* Error! */
purple_proxy_connect_data_disconnect(connect_data, g_strerror(errno));
return;
}
if (ret < request_len) {
connect_data->written_len += ret;
return;
}
/* We're done writing data! Wait for a response. */
g_free(connect_data->write_buffer);
connect_data->write_buffer = NULL;
purple_input_remove(connect_data->inpa);
connect_data->inpa = purple_input_add(connect_data->fd,
PURPLE_INPUT_READ, connect_data->read_cb, connect_data);
}
#define HTTP_GOODSTRING "HTTP/1.0 200"
#define HTTP_GOODSTRING2 "HTTP/1.1 200"
/**
* We're using an HTTP proxy for a non-port 80 tunnel. Read the
* response to the CONNECT request.
*/
static void
http_canread(gpointer data, gint source, PurpleInputCondition cond)
{
int len, headers_len, status = 0;
gboolean error;
PurpleProxyConnectData *connect_data = data;
char *p;
gsize max_read;
if (connect_data->read_buffer == NULL) {
connect_data->read_buf_len = 8192;
connect_data->read_buffer = g_malloc(connect_data->read_buf_len);
connect_data->read_len = 0;
}
p = (char *)connect_data->read_buffer + connect_data->read_len;
max_read = connect_data->read_buf_len - connect_data->read_len - 1;
len = read(connect_data->fd, p, max_read);
if (len == 0) {
purple_proxy_connect_data_disconnect(connect_data,
_("Server closed the connection"));
return;
}
if (len < 0) {
if (errno == EAGAIN)
/* No worries */
return;
/* Error! */
purple_proxy_connect_data_disconnect_formatted(connect_data,
_("Lost connection with server: %s"), g_strerror(errno));
return;
}
connect_data->read_len += len;
p[len] = '\0';
p = g_strstr_len((const gchar *)connect_data->read_buffer,
connect_data->read_len, "\r\n\r\n");
if (p != NULL) {
*p = '\0';
headers_len = (p - (char *)connect_data->read_buffer) + 4;
} else if(len == max_read)
headers_len = len;
else
return;
error = strncmp((const char *)connect_data->read_buffer, "HTTP/", 5) != 0;
if (!error) {
int major;
p = (char *)connect_data->read_buffer + 5;
major = strtol(p, &p, 10);
error = (major == 0) || (*p != '.');
if(!error) {
int minor;
p++;
minor = strtol(p, &p, 10);
error = (*p != ' ');
if(!error) {
p++;
status = strtol(p, &p, 10);
error = (*p != ' ');
}
}
}
/* Read the contents */
p = g_strrstr((const gchar *)connect_data->read_buffer, "Content-Length: ");
if (p != NULL) {
gchar *tmp;
int len = 0;
char tmpc;
p += strlen("Content-Length: ");
tmp = strchr(p, '\r');
if(tmp)
*tmp = '\0';
len = atoi(p);
if(tmp)
*tmp = '\r';
/* Compensate for what has already been read */
len -= connect_data->read_len - headers_len;
/* I'm assuming that we're doing this to prevent the server from