Welcome to mirror list, hosted at ThFree Co, Russian Federation.

06-revert-purple-socket.patch « patches - github.com/dequis/purple-facebook.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 230ccd09cd76b5be298641591454e826f3d22ada (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
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
diff --git a/libpurple/purple-socket.c b/libpurple/purple-socket.c
new file mode 100644
--- /dev/null
+++ b/libpurple/purple-socket.c
@@ -0,0 +1,410 @@
+/* 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
+ */
+
+#include "purple-socket.h"
+
+#include "internal.h"
+
+#include "debug.h"
+#include "proxy.h"
+#include "sslconn.h"
+
+typedef enum {
+	PURPLE_SOCKET_STATE_DISCONNECTED = 0,
+	PURPLE_SOCKET_STATE_CONNECTING,
+	PURPLE_SOCKET_STATE_CONNECTED,
+	PURPLE_SOCKET_STATE_ERROR
+} PurpleSocketState;
+
+struct _PurpleSocket
+{
+	PurpleConnection *gc;
+	gchar *host;
+	int port;
+	gboolean is_tls;
+	GHashTable *data;
+
+	PurpleSocketState state;
+
+	PurpleSslConnection *tls_connection;
+	PurpleProxyConnectData *raw_connection;
+	int fd;
+	guint inpa;
+
+	PurpleSocketConnectCb cb;
+	gpointer cb_data;
+};
+
+static GHashTable *handles = NULL;
+
+static void
+handle_add(PurpleSocket *ps)
+{
+	PurpleConnection *gc = ps->gc;
+	GSList *l;
+
+	l = g_hash_table_lookup(handles, gc);
+	l = g_slist_prepend(l, ps);
+	g_hash_table_insert(handles, gc, l);
+}
+
+static void
+handle_remove(PurpleSocket *ps)
+{
+	PurpleConnection *gc = ps->gc;
+	GSList *l;
+
+	l = g_hash_table_lookup(handles, gc);
+	l = g_slist_remove(l, ps);
+	g_hash_table_insert(handles, gc, l);
+}
+
+void
+_purple_socket_init(void)
+{
+	handles = g_hash_table_new(g_direct_hash, g_direct_equal);
+}
+
+void
+_purple_socket_uninit(void)
+{
+	g_hash_table_destroy(handles);
+	handles = NULL;
+}
+
+PurpleSocket *
+purple_socket_new(PurpleConnection *gc)
+{
+	PurpleSocket *ps = g_new0(PurpleSocket, 1);
+
+	ps->gc = gc;
+	ps->fd = -1;
+	ps->port = -1;
+	ps->data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+
+	handle_add(ps);
+
+	return ps;
+}
+
+PurpleConnection *
+purple_socket_get_connection(PurpleSocket *ps)
+{
+	g_return_val_if_fail(ps != NULL, NULL);
+
+	return ps->gc;
+}
+
+static gboolean
+purple_socket_check_state(PurpleSocket *ps, PurpleSocketState wanted_state)
+{
+	g_return_val_if_fail(ps != NULL, FALSE);
+
+	if (ps->state == wanted_state)
+		return TRUE;
+
+	purple_debug_error("socket", "invalid state: %d (should be: %d)",
+		ps->state, wanted_state);
+	ps->state = PURPLE_SOCKET_STATE_ERROR;
+	return FALSE;
+}
+
+void
+purple_socket_set_tls(PurpleSocket *ps, gboolean is_tls)
+{
+	g_return_if_fail(ps != NULL);
+
+	if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_DISCONNECTED))
+		return;
+
+	ps->is_tls = is_tls;
+}
+
+void
+purple_socket_set_host(PurpleSocket *ps, const gchar *host)
+{
+	g_return_if_fail(ps != NULL);
+
+	if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_DISCONNECTED))
+		return;
+
+	g_free(ps->host);
+	ps->host = g_strdup(host);
+}
+
+void
+purple_socket_set_port(PurpleSocket *ps, int port)
+{
+	g_return_if_fail(ps != NULL);
+	g_return_if_fail(port >= 0);
+	g_return_if_fail(port <= 65535);
+
+	if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_DISCONNECTED))
+		return;
+
+	ps->port = port;
+}
+
+static void
+_purple_socket_connected_raw(gpointer _ps, gint fd, const gchar *error_message)
+{
+	PurpleSocket *ps = _ps;
+
+	ps->raw_connection = NULL;
+
+	if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTING)) {
+		if (fd > 0)
+			close(fd);
+		ps->cb(ps, _("Invalid socket state"), ps->cb_data);
+		return;
+	}
+
+	if (fd <= 0 || error_message != NULL) {
+		if (error_message == NULL)
+			error_message = _("Unknown error");
+		ps->fd = -1;
+		ps->state = PURPLE_SOCKET_STATE_ERROR;
+		ps->cb(ps, error_message, ps->cb_data);
+		return;
+	}
+
+	ps->state = PURPLE_SOCKET_STATE_CONNECTED;
+	ps->fd = fd;
+	ps->cb(ps, NULL, ps->cb_data);
+}
+
+static void
+_purple_socket_connected_tls(gpointer _ps, PurpleSslConnection *tls_connection,
+	PurpleInputCondition cond)
+{
+	PurpleSocket *ps = _ps;
+
+	if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTING)) {
+		purple_ssl_close(tls_connection);
+		ps->tls_connection = NULL;
+		ps->cb(ps, _("Invalid socket state"), ps->cb_data);
+		return;
+	}
+
+	if (ps->tls_connection->fd <= 0) {
+		ps->state = PURPLE_SOCKET_STATE_ERROR;
+		purple_ssl_close(tls_connection);
+		ps->tls_connection = NULL;
+		ps->cb(ps, _("Invalid file descriptor"), ps->cb_data);
+		return;
+	}
+
+	ps->state = PURPLE_SOCKET_STATE_CONNECTED;
+	ps->fd = ps->tls_connection->fd;
+	ps->cb(ps, NULL, ps->cb_data);
+}
+
+static void
+_purple_socket_connected_tls_error(PurpleSslConnection *ssl_connection,
+	PurpleSslErrorType error, gpointer _ps)
+{
+	PurpleSocket *ps = _ps;
+
+	ps->state = PURPLE_SOCKET_STATE_ERROR;
+	ps->tls_connection = NULL;
+	ps->cb(ps, purple_ssl_strerror(error), ps->cb_data);
+}
+
+gboolean
+purple_socket_connect(PurpleSocket *ps, PurpleSocketConnectCb cb,
+	gpointer user_data)
+{
+	PurpleAccount *account = NULL;
+
+	g_return_val_if_fail(ps != NULL, FALSE);
+
+	if (ps->gc && purple_connection_is_disconnecting(ps->gc)) {
+		purple_debug_error("socket", "connection is being destroyed");
+		ps->state = PURPLE_SOCKET_STATE_ERROR;
+		return FALSE;
+	}
+
+	if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_DISCONNECTED))
+		return FALSE;
+	ps->state = PURPLE_SOCKET_STATE_CONNECTING;
+
+	if (ps->host == NULL || ps->port < 0) {
+		purple_debug_error("socket", "Host or port is not specified");
+		ps->state = PURPLE_SOCKET_STATE_ERROR;
+		return FALSE;
+	}
+
+	if (ps->gc != NULL)
+		account = purple_connection_get_account(ps->gc);
+
+	ps->cb = cb;
+	ps->cb_data = user_data;
+
+	if (ps->is_tls) {
+		ps->tls_connection = purple_ssl_connect(account, ps->host,
+			ps->port, _purple_socket_connected_tls,
+			_purple_socket_connected_tls_error, ps);
+	} else {
+		ps->raw_connection = purple_proxy_connect(ps->gc, account,
+			ps->host, ps->port, _purple_socket_connected_raw, ps);
+	}
+
+	if (ps->tls_connection == NULL &&
+		ps->raw_connection == NULL)
+	{
+		ps->state = PURPLE_SOCKET_STATE_ERROR;
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+gssize
+purple_socket_read(PurpleSocket *ps, guchar *buf, size_t len)
+{
+	g_return_val_if_fail(ps != NULL, -1);
+	g_return_val_if_fail(buf != NULL, -1);
+
+	if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTED))
+		return -1;
+
+	if (ps->is_tls)
+		return purple_ssl_read(ps->tls_connection, buf, len);
+	else
+		return read(ps->fd, buf, len);
+}
+
+gssize
+purple_socket_write(PurpleSocket *ps, const guchar *buf, size_t len)
+{
+	g_return_val_if_fail(ps != NULL, -1);
+	g_return_val_if_fail(buf != NULL, -1);
+
+	if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTED))
+		return -1;
+
+	if (ps->is_tls)
+		return purple_ssl_write(ps->tls_connection, buf, len);
+	else
+		return write(ps->fd, buf, len);
+}
+
+void
+purple_socket_watch(PurpleSocket *ps, PurpleInputCondition cond,
+	PurpleInputFunction func, gpointer user_data)
+{
+	g_return_if_fail(ps != NULL);
+
+	if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTED))
+		return;
+
+	if (ps->inpa > 0)
+		purple_input_remove(ps->inpa);
+	ps->inpa = 0;
+
+	g_return_if_fail(ps->fd > 0);
+
+	if (func != NULL)
+		ps->inpa = purple_input_add(ps->fd, cond, func, user_data);
+}
+
+int
+purple_socket_get_fd(PurpleSocket *ps)
+{
+	g_return_val_if_fail(ps != NULL, -1);
+
+	if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTED))
+		return -1;
+
+	g_return_val_if_fail(ps->fd > 0, -1);
+
+	return ps->fd;
+}
+
+void
+purple_socket_set_data(PurpleSocket *ps, const gchar *key, gpointer data)
+{
+	g_return_if_fail(ps != NULL);
+	g_return_if_fail(key != NULL);
+
+	if (data == NULL)
+		g_hash_table_remove(ps->data, key);
+	else
+		g_hash_table_insert(ps->data, g_strdup(key), data);
+}
+
+gpointer
+purple_socket_get_data(PurpleSocket *ps, const gchar *key)
+{
+	g_return_val_if_fail(ps != NULL, NULL);
+	g_return_val_if_fail(key != NULL, NULL);
+
+	return g_hash_table_lookup(ps->data, key);
+}
+
+static void
+purple_socket_cancel(PurpleSocket *ps)
+{
+	if (ps->inpa > 0)
+		purple_input_remove(ps->inpa);
+	ps->inpa = 0;
+
+	if (ps->tls_connection != NULL) {
+		purple_ssl_close(ps->tls_connection);
+		ps->fd = -1;
+	}
+	ps->tls_connection = NULL;
+
+	if (ps->raw_connection != NULL)
+		purple_proxy_connect_cancel(ps->raw_connection);
+	ps->raw_connection = NULL;
+
+	if (ps->fd > 0)
+		close(ps->fd);
+	ps->fd = 0;
+}
+
+void
+purple_socket_destroy(PurpleSocket *ps)
+{
+	if (ps == NULL)
+		return;
+
+	handle_remove(ps);
+
+	purple_socket_cancel(ps);
+
+	g_free(ps->host);
+	g_hash_table_destroy(ps->data);
+	g_free(ps);
+}
+
+void
+_purple_socket_cancel_with_connection(PurpleConnection *gc)
+{
+	GSList *it;
+
+	it = g_hash_table_lookup(handles, gc);
+	for (; it; it = g_slist_next(it)) {
+		PurpleSocket *ps = it->data;
+		purple_socket_cancel(ps);
+	}
+}
diff --git a/libpurple/purple-socket.h b/libpurple/purple-socket.h
new file mode 100644
--- /dev/null
+++ b/libpurple/purple-socket.h
@@ -0,0 +1,217 @@
+/* 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
+ */
+
+#ifndef _PURPLE_SOCKET_H_
+#define _PURPLE_SOCKET_H_
+/**
+ * SECTION:purple-socket
+ * @section_id: libpurple-purple-socket
+ * @short_description: <filename>purple-socket.h</filename>
+ * @title: Generic Sockets
+ */
+
+#include "connection.h"
+
+/**
+ * PurpleSocket:
+ *
+ * A structure holding all resources needed for the TCP connection.
+ */
+typedef struct _PurpleSocket PurpleSocket;
+
+/**
+ * PurpleSocketConnectCb:
+ * @ps:        The socket.
+ * @error:     Error message, or NULL if connection was successful.
+ * @user_data: The user data passed with callback function.
+ *
+ * A callback fired after (successfully or not) establishing a connection.
+ */
+typedef void (*PurpleSocketConnectCb)(PurpleSocket *ps, const gchar *error,
+	gpointer user_data);
+
+/**
+ * purple_socket_new:
+ * @gc: The connection for which the socket is needed, or NULL.
+ *
+ * Creates new, disconnected socket.
+ *
+ * Passing a PurpleConnection allows for proper proxy handling.
+ *
+ * Returns:   The new socket struct.
+ */
+PurpleSocket *
+purple_socket_new(PurpleConnection *gc);
+
+/**
+ * purple_socket_get_connection:
+ * @ps: The socket.
+ *
+ * Gets PurpleConnection tied with specified socket.
+ *
+ * Returns:   The PurpleConnection object.
+ */
+PurpleConnection *
+purple_socket_get_connection(PurpleSocket *ps);
+
+/**
+ * purple_socket_set_tls:
+ * @ps:     The socket.
+ * @is_tls: TRUE, if TLS should be handled transparently, FALSE otherwise.
+ *
+ * Determines, if socket should handle TLS.
+ */
+void
+purple_socket_set_tls(PurpleSocket *ps, gboolean is_tls);
+
+/**
+ * purple_socket_set_host:
+ * @ps:   The socket.
+ * @host: The connection host.
+ *
+ * Sets connection host.
+ */
+void
+purple_socket_set_host(PurpleSocket *ps, const gchar *host);
+
+/**
+ * purple_socket_set_port:
+ * @ps:   The socket.
+ * @port: The connection port.
+ *
+ * Sets connection port.
+ */
+void
+purple_socket_set_port(PurpleSocket *ps, int port);
+
+/**
+ * purple_socket_connect:
+ * @ps:        The socket.
+ * @cb:        The function to call after establishing a connection, or on
+ *                  error.
+ * @user_data: The user data to be passed to callback function.
+ *
+ * Establishes a connection.
+ *
+ * Returns: TRUE on success (this doesn't mean it's connected yet), FALSE
+ *         otherwise.
+ */
+gboolean
+purple_socket_connect(PurpleSocket *ps, PurpleSocketConnectCb cb,
+	gpointer user_data);
+
+/**
+ * purple_socket_read:
+ * @ps:  The socket.
+ * @buf: The buffer to write data to.
+ * @len: The buffer size.
+ *
+ * Reads incoming data from socket.
+ *
+ * This function deals with TLS, if the socket is configured to do it.
+ *
+ * Returns: Amount of data written, or -1 on error (errno will be also be set).
+ */
+gssize
+purple_socket_read(PurpleSocket *ps, guchar *buf, size_t len);
+
+/**
+ * purple_socket_write:
+ * @ps:  The socket.
+ * @buf: The buffer to read data from.
+ * @len: The amount of data to read and send.
+ *
+ * Sends data through socket.
+ *
+ * This function deals with TLS, if the socket is configured to do it.
+ *
+ * Returns: Amount of data sent, or -1 on error (errno will albo be set).
+ */
+gssize
+purple_socket_write(PurpleSocket *ps, const guchar *buf, size_t len);
+
+/**
+ * purple_socket_watch:
+ * @ps:        The socket.
+ * @cond:      The condition type.
+ * @func:      The callback function for data, or NULL to remove any
+ *                  existing callbacks.
+ * @user_data: The user data to be passed to callback function.
+ *
+ * Adds an input handler for the socket.
+ *
+ * If the specified socket had input handler already registered, it will be
+ * removed. To remove any input handlers, pass an NULL handler function.
+ */
+void
+purple_socket_watch(PurpleSocket *ps, PurpleInputCondition cond,
+	PurpleInputFunction func, gpointer user_data);
+
+/**
+ * purple_socket_get_fd:
+ * @ps: The socket
+ *
+ * Gets underlying file descriptor for socket.
+ *
+ * It's not meant to read/write data (use purple_socket_read/
+ * purple_socket_write), rather for watching for changes with select().
+ *
+ * Returns: The file descriptor, or -1 on error.
+ */
+int
+purple_socket_get_fd(PurpleSocket *ps);
+
+/**
+ * purple_socket_set_data:
+ * @ps:   The socket.
+ * @key:  The unique key.
+ * @data: The data to assign, or NULL to remove.
+ *
+ * Sets extra data for a socket.
+ */
+void
+purple_socket_set_data(PurpleSocket *ps, const gchar *key, gpointer data);
+
+/**
+ * purple_socket_get_data:
+ * @ps:  The socket.
+ * @key: The unqiue key.
+ *
+ * Returns extra data in a socket.
+ *
+ * Returns: The data associated with the key.
+ */
+gpointer
+purple_socket_get_data(PurpleSocket *ps, const gchar *key);
+
+/**
+ * purple_socket_destroy:
+ * @ps: The socket.
+ *
+ * Destroys the socket, closes connection and frees all resources.
+ *
+ * If file descriptor for the socket was extracted with purple_socket_get_fd and
+ * added to event loop, it have to be removed prior this.
+ */
+void
+purple_socket_destroy(PurpleSocket *ps);
+
+#endif /* _PURPLE_SOCKET_H_ */