annotate libpurple/protocols/qq/qq_network.c @ 23054:ebad75b719f5

Sun Jun 29 22:00:12 CST 2008 csyfek@gmail.com * 20080629-fixed-qun-processing Merged patches from moo, ccpaging and coly Tickets: References #6199 Sat Jun 28 13:25:40 CST 2008 csyfek@gmail.com * 20080628-get-friends Patches from moo and ccpaging. Tickets: Fixes #4956. Fixes #2998.
author SHiNE CsyFeK <csyfek@gmail.com>
date Sun, 29 Jun 2008 14:24:21 +0000
parents 55f986ccbb6a
children bdb38a8bf721
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1 /**
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
2 * @file qq_network.c
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
3 *
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
4 * purple
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
5 *
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
6 * Purple is the legal property of its developers, whose names are too numerous
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
7 * to list here. Please refer to the COPYRIGHT file distributed with this
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
8 * source distribution.
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
9 *
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
10 * This program is free software; you can redistribute it and/or modify
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
11 * it under the terms of the GNU General Public License as published by
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
12 * the Free Software Foundation; either version 2 of the License, or
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
13 * (at your option) any later version.
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
14 *
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
15 * This program is distributed in the hope that it will be useful,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
18 * GNU General Public License for more details.
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
19 *
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
20 * You should have received a copy of the GNU General Public License
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
21 * along with this program; if not, write to the Free Software
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
23 */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
24
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
25 #include "cipher.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
26 #include "debug.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
27 #include "internal.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
28
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
29 #ifdef _WIN32
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
30 #define random rand
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
31 #define srandom srand
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
32 #endif
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
33
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
34 #include "buddy_info.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
35 #include "buddy_list.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
36 #include "buddy_opt.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
37 #include "buddy_status.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
38 #include "group_free.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
39 #include "char_conv.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
40 #include "crypt.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
41 #include "group_network.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
42 #include "header_info.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
43 #include "keep_alive.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
44 #include "im.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
45 #include "login_logout.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
46 #include "packet_parse.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
47 #include "qq_network.h"
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
48 #include "qq_trans.h"
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
49 #include "sys_msg.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
50 #include "utils.h"
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
51
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
52 /* set QQ_RECONNECT_MAX to 1, when test reconnecting */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
53 #define QQ_RECONNECT_MAX 4
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
54 #define QQ_RECONNECT_INTERVAL 5000
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
55
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
56 static gboolean set_new_server(qq_data *qd)
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
57 {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
58 gint count;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
59 gint index;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
60 GList *it = NULL;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
61
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
62 g_return_val_if_fail(qd != NULL, FALSE);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
63
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
64 if (qd->servers == NULL) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
65 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Server list is NULL\n");
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
66 return FALSE;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
67 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
68
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
69 if (qd->real_hostname) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
70 purple_debug(PURPLE_DEBUG_INFO, "QQ", "free real_hostname\n");
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
71 g_free(qd->real_hostname);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
72 qd->real_hostname = NULL;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
73 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
74
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
75 /* remove server used before */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
76 if (qd->server_name != NULL) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
77 purple_debug(PURPLE_DEBUG_INFO, "QQ",
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
78 "Remove previous server [%s]\n", qd->server_name);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
79 qd->servers = g_list_remove(qd->servers, qd->server_name);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
80 qd->server_name = NULL;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
81 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
82
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
83 count = g_list_length(qd->servers);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
84 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Server list has %d\n", count);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
85 if (count <= 0) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
86 /* no server left, disconnect when result is false */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
87 qd->servers = NULL;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
88 return FALSE;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
89 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
90
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
91 /* get new server */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
92 index = random() % count;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
93 it = g_list_nth(qd->servers, index);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
94 qd->server_name = it->data; /* do not free server_name */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
95 if (qd->server_name == NULL || strlen(qd->server_name) <= 0 ) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
96 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Server name at %d is empty\n", index);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
97 return FALSE;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
98 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
99
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
100 qd->real_hostname = g_strdup(qd->server_name);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
101 qd->real_port = qd->user_port;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
102
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
103 qd->reconnect_times = QQ_RECONNECT_MAX;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
104
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
105 purple_debug(PURPLE_DEBUG_INFO, "QQ",
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
106 "set new server to %s:%d\n", qd->real_hostname, qd->real_port);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
107 return TRUE;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
108 }
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
109
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
110 /* QQ 2003iii uses double MD5 for the pwkey to get the session key */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
111 static guint8 *encrypt_account_password(const gchar *pwd)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
112 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
113 PurpleCipher *cipher;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
114 PurpleCipherContext *context;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
115
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
116 guchar pwkey_tmp[QQ_KEY_LENGTH];
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
117
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
118 cipher = purple_ciphers_find_cipher("md5");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
119 context = purple_cipher_context_new(cipher, NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
120 purple_cipher_context_append(context, (guchar *) pwd, strlen(pwd));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
121 purple_cipher_context_digest(context, sizeof(pwkey_tmp), pwkey_tmp, NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
122 purple_cipher_context_destroy(context);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
123 context = purple_cipher_context_new(cipher, NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
124 purple_cipher_context_append(context, pwkey_tmp, QQ_KEY_LENGTH);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
125 purple_cipher_context_digest(context, sizeof(pwkey_tmp), pwkey_tmp, NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
126 purple_cipher_context_destroy(context);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
127
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
128 return g_memdup(pwkey_tmp, QQ_KEY_LENGTH);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
129 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
130
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
131 /* default process, decrypt and dump */
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
132 static void process_cmd_unknow(PurpleConnection *gc, guint8 *buf, gint buf_len, guint16 cmd, guint16 seq)
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
133 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
134 qq_data *qd;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
135 guint8 *data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
136 gint data_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
137 gchar *msg_utf8 = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
138
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
139 g_return_if_fail(buf != NULL && buf_len != 0);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
140
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
141 qq_show_packet("Processing unknown packet", buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
142
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
143 qd = (qq_data *) gc->proto_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
144
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
145 data_len = buf_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
146 data = g_newa(guint8, data_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
147 memset(data, 0, data_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
148 if ( !qq_decrypt(buf, buf_len, qd->session_key, data, &data_len )) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
149 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Fail decrypt packet with default process\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
150 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
151 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
152
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
153 qq_hex_dump(PURPLE_DEBUG_WARNING, "QQ",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
154 data, data_len,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
155 ">>> [%d] %s -> [default] decrypt and dump",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
156 seq, qq_get_cmd_desc(cmd));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
157
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
158 msg_utf8 = try_dump_as_gbk(data, data_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
159 if (msg_utf8) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
160 g_free(msg_utf8);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
161 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
162 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
163
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
164 static gint packet_get_header(guint8 *header_tag, guint16 *source_tag,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
165 guint16 *cmd, guint16 *seq, guint8 *buf)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
166 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
167 gint bytes = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
168 bytes += qq_get8(header_tag, buf + bytes);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
169 bytes += qq_get16(source_tag, buf + bytes);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
170 bytes += qq_get16(cmd, buf + bytes);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
171 bytes += qq_get16(seq, buf + bytes);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
172 return bytes;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
173 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
174
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
175 /* check whether one sequence number is duplicated or not
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
176 * return TRUE if it is duplicated, otherwise FALSE */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
177 static gboolean packet_is_dup(qq_data *qd, guint16 seq)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
178 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
179 guint8 *byte, mask;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
180
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
181 g_return_val_if_fail(qd != NULL, FALSE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
182
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
183 byte = &(qd->rcv_window[seq / 8]);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
184 mask = (1 << (seq % 8));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
185
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
186 if ((*byte) & mask)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
187 return TRUE; /* check mask */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
188 (*byte) |= mask;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
189 return FALSE; /* set mask */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
190 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
191
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
192 static gboolean packet_check_ack(qq_data *qd, guint16 seq)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
193 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
194 gpointer trans;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
195
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
196 g_return_val_if_fail(qd != NULL, FALSE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
197
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
198 trans = qq_send_trans_find(qd, seq);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
199 if (trans == NULL) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
200 return FALSE;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
201 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
202
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
203 qq_send_trans_remove(qd, trans);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
204 return TRUE;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
205 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
206
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
207 static gboolean reconnect_later_cb(gpointer data)
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
208 {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
209 PurpleConnection *gc;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
210 qq_data *qd;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
211
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
212 gc = (PurpleConnection *) data;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
213 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, FALSE);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
214 qd = (qq_data *) gc->proto_data;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
215
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
216 qd->reconnect_timeout = 0;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
217
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
218 qq_connect(gc->account);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
219 return FALSE; /* timeout callback stops */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
220 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
221
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
222 static void reconnect_later(PurpleConnection *gc)
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
223 {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
224 qq_data *qd;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
225
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
226 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
227 qd = (qq_data *) gc->proto_data;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
228
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
229 qd->reconnect_times--;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
230 if (qd->reconnect_times < 0) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
231 if ( set_new_server(qd) != TRUE) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
232 purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
233 _("Failed to connect server"));
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
234 return;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
235 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
236 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
237
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
238 purple_debug(PURPLE_DEBUG_INFO, "QQ",
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
239 "Reconnect to server %s:%d next retries %d in %d ms\n",
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
240 qd->real_hostname, qd->real_port,
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
241 qd->reconnect_times, QQ_RECONNECT_INTERVAL);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
242
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
243 qd->reconnect_timeout = purple_timeout_add(QQ_RECONNECT_INTERVAL,
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
244 reconnect_later_cb, gc);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
245 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
246
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
247 static void process_cmd_server(
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
248 PurpleConnection *gc, guint16 cmd, guint16 seq, guint8 *data, gint data_len)
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
249 {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
250 /* now process the packet */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
251 switch (cmd) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
252 case QQ_CMD_RECV_IM:
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
253 qq_process_recv_im(data, data_len, seq, gc);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
254 break;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
255 case QQ_CMD_RECV_MSG_SYS:
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
256 qq_process_msg_sys(data, data_len, seq, gc);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
257 break;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
258 case QQ_CMD_RECV_MSG_FRIEND_CHANGE_STATUS:
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
259 qq_process_friend_change_status(data, data_len, gc);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
260 break;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
261 default:
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
262 process_cmd_unknow(gc, data, data_len, cmd, seq);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
263 break;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
264 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
265 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
266
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
267 static void process_cmd_reply(
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
268 PurpleConnection *gc, guint16 cmd, guint16 seq, guint8 *data, gint data_len)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
269 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
270 /* now process the packet */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
271 switch (cmd) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
272 case QQ_CMD_KEEP_ALIVE:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
273 qq_process_keep_alive_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
274 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
275 case QQ_CMD_UPDATE_INFO:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
276 qq_process_modify_info_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
277 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
278 case QQ_CMD_ADD_FRIEND_WO_AUTH:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
279 qq_process_add_buddy_reply(data, data_len, seq, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
280 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
281 case QQ_CMD_DEL_FRIEND:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
282 qq_process_remove_buddy_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
283 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
284 case QQ_CMD_REMOVE_SELF:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
285 qq_process_remove_self_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
286 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
287 case QQ_CMD_BUDDY_AUTH:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
288 qq_process_add_buddy_auth_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
289 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
290 case QQ_CMD_GET_USER_INFO:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
291 qq_process_get_info_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
292 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
293 case QQ_CMD_CHANGE_ONLINE_STATUS:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
294 qq_process_change_status_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
295 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
296 case QQ_CMD_SEND_IM:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
297 qq_process_send_im_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
298 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
299 case QQ_CMD_LOGIN:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
300 qq_process_login_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
301 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
302 case QQ_CMD_GET_FRIENDS_LIST:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
303 qq_process_get_buddies_list_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
304 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
305 case QQ_CMD_GET_FRIENDS_ONLINE:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
306 qq_process_get_buddies_online_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
307 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
308 case QQ_CMD_GROUP_CMD:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
309 qq_process_group_cmd_reply(data, data_len, seq, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
310 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
311 case QQ_CMD_GET_ALL_LIST_WITH_GROUP:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
312 qq_process_get_all_list_with_group_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
313 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
314 case QQ_CMD_GET_LEVEL:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
315 qq_process_get_level_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
316 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
317 case QQ_CMD_REQUEST_LOGIN_TOKEN:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
318 qq_process_request_login_token_reply(data, data_len, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
319 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
320 default:
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
321 process_cmd_unknow(gc, data, data_len, cmd, seq);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
322 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
323 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
324 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
325
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
326 /* process the incoming packet from qq_pending */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
327 static void packet_process(PurpleConnection *gc, guint8 *buf, gint buf_len)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
328 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
329 qq_data *qd;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
330 gint bytes, bytes_not_read;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
331
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
332 gboolean prev_login_status;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
333 guint8 *new_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
334 gint new_data_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
335
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
336 guint8 header_tag;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
337 guint16 source_tag;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
338 guint16 cmd;
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
339 guint16 seq; /* May be ack_seq or send_seq, depends on cmd */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
340
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
341 gboolean is_reply;
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
342
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
343 g_return_if_fail(buf != NULL && buf_len > 0);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
344
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
345 qd = (qq_data *) gc->proto_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
346
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
347 prev_login_status = qd->logged_in;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
348
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
349 /* Len, header and tail tag have been checked before */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
350 bytes = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
351 bytes += packet_get_header(&header_tag, &source_tag, &cmd, &seq, buf + bytes);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
352
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
353 if (QQ_DEBUG) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
354 purple_debug(PURPLE_DEBUG_INFO, "QQ",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
355 "==> [%05d] 0x%04X %s, from (0x%04X %s)\n",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
356 seq, cmd, qq_get_cmd_desc(cmd), source_tag, qq_get_source_str(source_tag));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
357 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
358
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
359 bytes_not_read = buf_len - bytes - 1;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
360
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
361 /* ack packet, we need to update send tranactions */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
362 /* we do not check duplication for server ack */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
363 is_reply = packet_check_ack(qd, seq);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
364 if ( !is_reply ) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
365 if ( !qd->logged_in ) {
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
366 /* packets before login */
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
367 qq_rcv_trans_push(qd, cmd, seq, buf + bytes, bytes_not_read);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
368 return; /* do not process it now */
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
369 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
370
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
371 /* server intiated packet, we need to send ack and check duplicaion
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
372 * this must be put after processing b4_packet
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
373 * as these packets will be passed in twice */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
374 if (packet_is_dup(qd, seq)) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
375 purple_debug(PURPLE_DEBUG_WARNING,
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
376 "QQ", "dup [%05d] %s, discard...\n", seq, qq_get_cmd_desc(cmd));
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
377 return;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
378 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
379 process_cmd_server(gc, cmd, seq, buf + bytes, bytes_not_read);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
380 return;
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
381 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
382
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
383 /* this is the length of all the encrypted data (also remove tail tag */
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
384 process_cmd_reply(gc, cmd, seq, buf + bytes, bytes_not_read);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
385
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
386 /* check is redirect or not, and do it now */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
387 if (qd->is_redirect) {
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
388 /* free resource except real_hostname and port */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
389 qq_disconnect(gc);
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
390 qd->reconnect_times = QQ_RECONNECT_MAX;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
391 reconnect_later(gc);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
392 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
393 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
394
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
395 if (prev_login_status != qd->logged_in && qd->logged_in == TRUE) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
396 /* logged_in, but we have packets before login */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
397 new_data = g_newa(guint8, MAX_PACKET_SIZE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
398 while (1) {
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
399 memset(new_data, 0, MAX_PACKET_SIZE);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
400 new_data_len = qq_rcv_trans_pop(qd, &cmd, &seq, new_data, MAX_PACKET_SIZE);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
401 if (new_data_len < 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
402 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
403 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
404 if (new_data_len == 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
405 continue;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
406 }
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
407 process_cmd_reply(gc, seq, cmd, new_data, new_data_len);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
408 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
409 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
410 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
411
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
412 static void tcp_pending(gpointer data, gint source, PurpleInputCondition cond)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
413 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
414 PurpleConnection *gc;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
415 qq_data *qd;
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
416 guint8 buf[1024]; /* set to 16 when test tcp_rxqueue */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
417 gint buf_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
418 gint bytes;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
419
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
420 guint8 *pkt;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
421 guint16 pkt_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
422
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
423 gchar *error_msg;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
424 guint8 *jump;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
425 gint jump_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
426
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
427 gc = (PurpleConnection *) data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
428 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
429
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
430 if(cond != PURPLE_INPUT_READ) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
431 purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
432 _("Socket error"));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
433 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
434 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
435
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
436 qd = (qq_data *) gc->proto_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
437
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
438 /* test code, not using tcp_rxqueue
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
439 memset(pkt,0, sizeof(pkt));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
440 buf_len = read(qd->fd, pkt, sizeof(pkt));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
441 if (buf_len > 2) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
442 packet_process(gc, pkt + 2, buf_len - 2);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
443 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
444 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
445 */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
446
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
447 buf_len = read(qd->fd, buf, sizeof(buf));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
448 if (buf_len < 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
449 if (errno == EAGAIN)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
450 /* No worries */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
451 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
452
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
453 error_msg = g_strdup_printf(_("Lost connection with server:\n%d, %s"), errno, g_strerror(errno));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
454 purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, error_msg);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
455 g_free(error_msg);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
456 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
457 } else if (buf_len == 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
458 purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
459 _("Server closed the connection."));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
460 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
461 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
462
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
463 gc->last_received = time(NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
464 purple_debug(PURPLE_DEBUG_INFO, "TCP_PENDING",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
465 "Read %d bytes from socket, rxlen is %d\n", buf_len, qd->tcp_rxlen);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
466 qd->tcp_rxqueue = g_realloc(qd->tcp_rxqueue, buf_len + qd->tcp_rxlen);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
467 memcpy(qd->tcp_rxqueue + qd->tcp_rxlen, buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
468 qd->tcp_rxlen += buf_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
469
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
470 pkt = g_newa(guint8, MAX_PACKET_SIZE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
471 while (1) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
472 if (qd->tcp_rxlen < QQ_TCP_HEADER_LENGTH) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
473 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
474 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
475
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
476 bytes = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
477 bytes += qq_get16(&pkt_len, qd->tcp_rxqueue + bytes);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
478 if (qd->tcp_rxlen < pkt_len) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
479 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
480 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
481
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
482 purple_debug(PURPLE_DEBUG_INFO, "TCP_PENDING",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
483 "Packet len is %d bytes, rxlen is %d\n", pkt_len, qd->tcp_rxlen);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
484
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
485 if ( pkt_len < QQ_TCP_HEADER_LENGTH
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
486 || *(qd->tcp_rxqueue + bytes) != QQ_PACKET_TAG
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
487 || *(qd->tcp_rxqueue + pkt_len - 1) != QQ_PACKET_TAIL) {
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
488 /* HEY! This isn't even a QQ. What are you trying to pull? */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
489
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
490 purple_debug(PURPLE_DEBUG_ERROR, "TCP_PENDING",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
491 "Packet error, failed to check header and tail tag\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
492
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
493 jump = memchr(qd->tcp_rxqueue + 1, QQ_PACKET_TAIL, qd->tcp_rxlen - 1);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
494 if ( !jump ) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
495 purple_debug(PURPLE_DEBUG_INFO, "TCP_PENDING",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
496 "Failed to find next QQ_PACKET_TAIL, clear receive buffer\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
497 g_free(qd->tcp_rxqueue);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
498 qd->tcp_rxqueue = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
499 qd->tcp_rxlen = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
500 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
501 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
502
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
503 /* jump and over QQ_PACKET_TAIL */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
504 jump_len = (jump - qd->tcp_rxqueue) + 1;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
505 purple_debug(PURPLE_DEBUG_INFO, "TCP_PENDING",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
506 "Find next QQ_PACKET_TAIL at %d, jump %d bytes\n", jump_len, jump_len + 1);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
507 g_memmove(qd->tcp_rxqueue, jump, qd->tcp_rxlen - jump_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
508 qd->tcp_rxlen -= jump_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
509 continue;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
510 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
511
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
512 memset(pkt, 0, MAX_PACKET_SIZE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
513 g_memmove(pkt, qd->tcp_rxqueue + bytes, pkt_len - bytes);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
514
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
515 /* jump to next packet */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
516 qd->tcp_rxlen -= pkt_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
517 if (qd->tcp_rxlen) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
518 purple_debug(PURPLE_DEBUG_ERROR, "TCP_PENDING",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
519 "shrink tcp_rxqueue to %d\n", qd->tcp_rxlen);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
520 jump = g_memdup(qd->tcp_rxqueue + pkt_len, qd->tcp_rxlen);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
521 g_free(qd->tcp_rxqueue);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
522 qd->tcp_rxqueue = jump;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
523 } else {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
524 purple_debug(PURPLE_DEBUG_ERROR, "TCP_PENDING",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
525 "free tcp_rxqueue\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
526 g_free(qd->tcp_rxqueue);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
527 qd->tcp_rxqueue = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
528 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
529
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
530 if (pkt == NULL) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
531 continue;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
532 }
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
533 /* do not call packet_process before jump
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
534 * packet_process may call disconnect and destory tcp_rxqueue */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
535 packet_process(gc, pkt, pkt_len - bytes);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
536 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
537 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
538
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
539 static void udp_pending(gpointer data, gint source, PurpleInputCondition cond)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
540 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
541 PurpleConnection *gc;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
542 qq_data *qd;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
543 guint8 *buf;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
544 gint buf_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
545
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
546 gc = (PurpleConnection *) data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
547 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
548
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
549 if(cond != PURPLE_INPUT_READ) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
550 purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
551 _("Socket error"));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
552 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
553 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
554
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
555 qd = (qq_data *) gc->proto_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
556 g_return_if_fail(qd->fd >= 0);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
557
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
558 buf = g_newa(guint8, MAX_PACKET_SIZE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
559
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
560 /* here we have UDP proxy suppport */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
561 buf_len = read(qd->fd, buf, MAX_PACKET_SIZE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
562 if (buf_len <= 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
563 purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
564 _("Unable to read from socket"));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
565 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
566 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
567
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
568 gc->last_received = time(NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
569
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
570 if (buf_len < QQ_UDP_HEADER_LENGTH) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
571 if (buf[0] != QQ_PACKET_TAG || buf[buf_len - 1] != QQ_PACKET_TAIL) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
572 qq_hex_dump(PURPLE_DEBUG_ERROR, "UDP_PENDING",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
573 buf, buf_len,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
574 "Received packet is too short, or no header and tail tag");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
575 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
576 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
577 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
578
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
579 packet_process(gc, buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
580 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
581
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
582 static gint udp_send_out(qq_data *qd, guint8 *data, gint data_len)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
583 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
584 gint ret;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
585
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
586 g_return_val_if_fail(qd != NULL && qd->fd >= 0 && data != NULL && data_len > 0, -1);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
587
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
588 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Send %d bytes to socket %d\n", data_len, qd->fd);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
589
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
590 errno = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
591 ret = send(qd->fd, data, data_len, 0);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
592 if (ret < 0 && errno == EAGAIN) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
593 return ret;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
594 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
595
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
596 if (ret < 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
597 /* TODO: what to do here - do we really have to disconnect? */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
598 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Send failed: %d, %s\n", errno, g_strerror(errno));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
599 purple_connection_error_reason(qd->gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, g_strerror(errno));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
600 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
601 return ret;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
602 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
603
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
604 static void tcp_can_write(gpointer data, gint source, PurpleInputCondition cond)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
605 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
606 qq_data *qd = data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
607 int ret, writelen;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
608
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
609 writelen = purple_circ_buffer_get_max_read(qd->tcp_txbuf);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
610 if (writelen == 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
611 purple_input_remove(qd->tx_handler);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
612 qd->tx_handler = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
613 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
614 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
615
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
616 ret = write(qd->fd, qd->tcp_txbuf->outptr, writelen);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
617 purple_debug(PURPLE_DEBUG_ERROR, "TCP_CAN_WRITE",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
618 "total %d bytes is sent %d\n", writelen, ret);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
619
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
620 if (ret < 0 && errno == EAGAIN)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
621 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
622 else if (ret < 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
623 /* TODO: what to do here - do we really have to disconnect? */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
624 purple_connection_error_reason(qd->gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
625 _("Write Error"));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
626 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
627 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
628
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
629 purple_circ_buffer_mark_read(qd->tcp_txbuf, ret);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
630 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
631
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
632 static gint tcp_send_out(qq_data *qd, guint8 *data, gint data_len)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
633 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
634 gint ret;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
635
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
636 g_return_val_if_fail(qd != NULL && qd->fd >= 0 && data != NULL && data_len > 0, -1);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
637
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
638 /*
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
639 * purple_debug(PURPLE_DEBUG_INFO, "TCP_SEND_OUT", "Send %d bytes to socket %d\n", data_len, qd->fd);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
640 */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
641
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
642 if (qd->tx_handler == 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
643 ret = write(qd->fd, data, data_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
644 } else {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
645 ret = -1;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
646 errno = EAGAIN;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
647 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
648
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
649 purple_debug(PURPLE_DEBUG_INFO, "TCP_SEND_OUT",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
650 "Socket %d, total %d bytes is sent %d\n", qd->fd, data_len, ret);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
651 if (ret < 0 && errno == EAGAIN) {
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
652 /* socket is busy, send later */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
653 /*
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
654 * purple_debug(PURPLE_DEBUG_INFO, "TCP_SEND_OUT", "Socket is busy and send later\n");
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
655 */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
656 ret = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
657 } else if (ret <= 0) {
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
658 /* TODO: what to do here - do we really have to disconnect? */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
659 purple_debug(PURPLE_DEBUG_ERROR, "TCP_SEND_OUT",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
660 "Send to socket %d failed: %d, %s\n", qd->fd, errno, g_strerror(errno));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
661 purple_connection_error_reason(qd->gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, g_strerror(errno));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
662 return ret;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
663 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
664
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
665 if (ret < data_len) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
666 purple_debug(PURPLE_DEBUG_INFO, "TCP_SEND_OUT",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
667 "Add %d bytes to buffer\n", data_len - ret);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
668 if (qd->tx_handler == 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
669 qd->tx_handler = purple_input_add(qd->fd, PURPLE_INPUT_WRITE, tcp_can_write, qd);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
670 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
671 purple_circ_buffer_append(qd->tcp_txbuf, data + ret, data_len - ret);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
672 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
673 return ret;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
674 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
675
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
676 static gboolean trans_timeout(gpointer data)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
677 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
678 PurpleConnection *gc;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
679 qq_data *qd;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
680 guint8 *buf;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
681 gint buf_len = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
682 guint16 cmd;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
683 gint retries = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
684 int index;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
685
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
686 gc = (PurpleConnection *) data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
687 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, TRUE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
688
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
689 qd = (qq_data *) gc->proto_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
690
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
691 index = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
692 buf = g_newa(guint8, MAX_PACKET_SIZE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
693
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
694 while (1) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
695 if (index < 0) {
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
696 /* next record is NULL */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
697 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
698 }
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
699 /* purple_debug(PURPLE_DEBUG_ERROR, "QQ", "scan begin %d\n", index); */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
700 memset(buf, 0, MAX_PACKET_SIZE);
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
701 buf_len = qq_send_trans_scan(qd, &index, buf, MAX_PACKET_SIZE, &cmd, &retries);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
702 if (buf_len <= 0) {
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
703 /* curr record is empty, whole trans is NULL */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
704 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
705 }
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
706 /* index = -1, when get last record of transactions */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
707
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
708 /* purple_debug(PURPLE_DEBUG_ERROR, "QQ", "retries %d next index %d\n", retries, index); */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
709 if (retries > 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
710 if (qd->use_tcp) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
711 tcp_send_out(qd, buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
712 } else {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
713 udp_send_out(qd, buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
714 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
715 continue;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
716 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
717
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
718 /* retries <= 0 */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
719 switch (cmd) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
720 case QQ_CMD_KEEP_ALIVE:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
721 if (qd->logged_in) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
722 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Connection lost!\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
723 purple_connection_error_reason(gc,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
724 PURPLE_CONNECTION_ERROR_NETWORK_ERROR, _("Connection lost"));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
725 qd->logged_in = FALSE;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
726 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
727 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
728 case QQ_CMD_LOGIN:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
729 case QQ_CMD_REQUEST_LOGIN_TOKEN:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
730 if (!qd->logged_in) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
731 /* cancel login progress */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
732 purple_connection_error_reason(gc,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
733 PURPLE_CONNECTION_ERROR_NETWORK_ERROR, _("Login failed, no reply"));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
734 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
735 break;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
736 default:
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
737 purple_debug(PURPLE_DEBUG_WARNING, "QQ",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
738 "%s packet lost.\n", qq_get_cmd_desc(cmd));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
739 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
740 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
741
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
742 return TRUE; /* if return FALSE, timeout callback stops */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
743 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
744
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
745 /* the callback function after socket is built
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
746 * we setup the qq protocol related configuration here */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
747 static void qq_connect_cb(gpointer data, gint source, const gchar *error_message)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
748 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
749 qq_data *qd;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
750 PurpleConnection *gc;
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
751 gchar *conn_msg;
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
752 const gchar *passwd;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
753
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
754 gc = (PurpleConnection *) data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
755
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
756 if (!PURPLE_CONNECTION_IS_VALID(gc)) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
757 purple_debug(PURPLE_DEBUG_INFO, "QQ_CONN", "Invalid connection\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
758 close(source);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
759 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
760 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
761
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
762 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
763
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
764 qd = (qq_data *) gc->proto_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
765
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
766 /* Connect is now complete; clear the PurpleProxyConnectData */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
767 qd->connect_data = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
768
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
769 if (source < 0) { /* socket returns -1 */
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
770 purple_debug(PURPLE_DEBUG_INFO, "QQ_CONN", "Invalid connection, source is < 0\n");
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
771 qq_disconnect(gc);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
772 reconnect_later(gc);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
773 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
774 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
775
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
776 /* _qq_show_socket("Got login socket", source); */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
777
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
778 /* QQ use random seq, to minimize duplicated packets */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
779 srandom(time(NULL));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
780 qd->send_seq = random() & 0x0000ffff;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
781 qd->fd = source;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
782 qd->logged_in = FALSE;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
783 qd->channel = 1;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
784 qd->uid = strtol(purple_account_get_username(purple_connection_get_account(gc)), NULL, 10);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
785
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
786 /* now generate md5 processed passwd */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
787 passwd = purple_account_get_password(purple_connection_get_account(gc));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
788 g_return_if_fail(qd->pwkey == NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
789 qd->pwkey = encrypt_account_password(passwd);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
790
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
791 g_return_if_fail(qd->resend_timeout == 0);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
792 /* call trans_timeout every 5 seconds */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
793 qd->resend_timeout = purple_timeout_add(5000, trans_timeout, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
794
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
795 if (qd->use_tcp)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
796 gc->inpa = purple_input_add(qd->fd, PURPLE_INPUT_READ, tcp_pending, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
797 else
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
798 gc->inpa = purple_input_add(qd->fd, PURPLE_INPUT_READ, udp_pending, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
799
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
800 /* Update the login progress status display */
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
801 conn_msg = g_strdup_printf("Login as %d", qd->uid);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
802 purple_connection_update_progress(gc, conn_msg, QQ_CONNECT_STEPS - 1, QQ_CONNECT_STEPS);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
803 g_free(conn_msg);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
804
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
805 qq_send_packet_request_login_token(gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
806 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
807
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
808 static void udp_can_write(gpointer data, gint source, PurpleInputCondition cond)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
809 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
810 PurpleConnection *gc;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
811 qq_data *qd;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
812 socklen_t len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
813 int error=0, ret;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
814
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
815 gc = (PurpleConnection *) data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
816 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
817
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
818 qd = (qq_data *) gc->proto_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
819
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
820
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
821 purple_debug_info("proxy", "Connected.\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
822
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
823 /*
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
824 * getsockopt after a non-blocking connect returns -1 if something is
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
825 * really messed up (bad descriptor, usually). Otherwise, it returns 0 and
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
826 * error holds what connect would have returned if it blocked until now.
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
827 * Thus, error == 0 is success, error == EINPROGRESS means "try again",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
828 * and anything else is a real error.
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
829 *
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
830 * (error == EINPROGRESS can happen after a select because the kernel can
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
831 * be overly optimistic sometimes. select is just a hint that you might be
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
832 * able to do something.)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
833 */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
834 len = sizeof(error);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
835 ret = getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
836 if (ret == 0 && error == EINPROGRESS)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
837 return; /* we'll be called again later */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
838
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
839 purple_input_remove(qd->tx_handler);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
840 qd->tx_handler = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
841 if (ret < 0 || error != 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
842 if(ret != 0)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
843 error = errno;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
844
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
845 close(source);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
846
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
847 purple_debug_error("proxy", "getsockopt SO_ERROR check: %s\n", g_strerror(error));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
848
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
849 qq_connect_cb(gc, -1, _("Unable to connect"));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
850 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
851 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
852
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
853 qq_connect_cb(gc, source, NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
854 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
855
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
856 static void udp_host_resolved(GSList *hosts, gpointer data, const char *error_message) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
857 PurpleConnection *gc;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
858 qq_data *qd;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
859 struct sockaddr server_addr;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
860 int addr_size;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
861 gint fd = -1;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
862 int flags;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
863
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
864 gc = (PurpleConnection *) data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
865 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
866
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
867 qd = (qq_data *) gc->proto_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
868
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
869 /* udp_query_data must be set as NULL.
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
870 * Otherwise purple_dnsquery_destroy in qq_disconnect cause glib double free error */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
871 qd->udp_query_data = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
872
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
873 if (!hosts || !hosts->data) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
874 purple_connection_error_reason(gc,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
875 PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
876 _("Couldn't resolve host"));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
877 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
878 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
879
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
880 addr_size = GPOINTER_TO_INT(hosts->data);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
881 hosts = g_slist_remove(hosts, hosts->data);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
882 memcpy(&server_addr, hosts->data, addr_size);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
883 g_free(hosts->data);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
884
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
885 hosts = g_slist_remove(hosts, hosts->data);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
886 while(hosts) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
887 hosts = g_slist_remove(hosts, hosts->data);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
888 g_free(hosts->data);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
889 hosts = g_slist_remove(hosts, hosts->data);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
890 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
891
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
892 fd = socket(PF_INET, SOCK_DGRAM, 0);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
893 if (fd < 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
894 purple_debug(PURPLE_DEBUG_ERROR, "QQ",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
895 "Unable to create socket: %s\n", g_strerror(errno));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
896 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
897 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
898
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
899 /* we use non-blocking mode to speed up connection */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
900 flags = fcntl(fd, F_GETFL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
901 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
902
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
903 /* From Unix-socket-FAQ: http://www.faqs.org/faqs/unix-faq/socket/
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
904 *
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
905 * If a UDP socket is unconnected, which is the normal state after a
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
906 * bind() call, then send() or write() are not allowed, since no
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
907 * destination is available; only sendto() can be used to send data.
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
908 *
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
909 * Calling connect() on the socket simply records the specified address
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
910 * and port number as being the desired communications partner. That
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
911 * means that send() or write() are now allowed; they use the destination
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
912 * address and port given on the connect call as the destination of packets.
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
913 */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
914 if (connect(fd, &server_addr, addr_size) >= 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
915 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Connected.\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
916 flags = fcntl(fd, F_GETFL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
917 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
918 qq_connect_cb(gc, fd, NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
919 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
920 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
921
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
922 /* [EINPROGRESS]
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
923 * The socket is marked as non-blocking and the connection cannot be
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
924 * completed immediately. It is possible to select for completion by
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
925 * selecting the socket for writing.
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
926 * [EINTR]
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
927 * A signal interrupted the call.
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
928 * The connection is established asynchronously.
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
929 */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
930 if ((errno == EINPROGRESS) || (errno == EINTR)) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
931 purple_debug(PURPLE_DEBUG_WARNING, "QQ", "Connect in asynchronous mode.\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
932 qd->tx_handler = purple_input_add(fd, PURPLE_INPUT_WRITE, udp_can_write, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
933 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
934 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
935
23054
ebad75b719f5 Sun Jun 29 22:00:12 CST 2008 csyfek@gmail.com
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23053
diff changeset
936 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Connection failed: %s\n", g_strerror(errno));
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
937 close(fd);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
938 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
939
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
940 /* establish a generic QQ connection
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
941 * TCP/UDP, and direct/redirected */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
942 void qq_connect(PurpleAccount *account)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
943 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
944 PurpleConnection *gc;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
945 qq_data *qd;
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
946 gchar *conn_msg;
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
947
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
948 gc = purple_account_get_connection(account);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
949 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
950
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
951 qd = (qq_data *) gc->proto_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
952
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
953
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
954 /* test set_new_server
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
955 while (set_new_server(qd)) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
956 purple_debug(PURPLE_DEBUG_INFO, "QQ_TEST",
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
957 "New server %s:%d Real server %s:%d\n",
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
958 qd->server_name, qd->user_port, qd->real_hostname, qd->real_port);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
959 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
960 purple_debug(PURPLE_DEBUG_INFO, "QQ_TEST", "qd->servers %lu\n",
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
961 qd->servers);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
962 exit(1);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
963 */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
964 if (qd->server_name == NULL) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
965 /* must be first call this function */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
966 if ( set_new_server(qd) != TRUE) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
967 purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
968 _("Failed to connect server"));
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
969 return;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
970 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
971 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
972
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
973 if (qd->real_hostname == NULL || qd->real_port == 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
974 purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
975 _("hostname is NULL or port is 0"));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
976 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
977 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
978
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
979 conn_msg = g_strdup_printf( _("Connecting server %s, retries %d"),
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
980 qd->real_hostname, qd->reconnect_times);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
981 purple_connection_update_progress(gc, conn_msg, 1, QQ_CONNECT_STEPS);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
982 g_free(conn_msg);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
983
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
984 if (qd->is_redirect) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
985 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Redirect to %s:%d\n",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
986 qd->real_hostname, qd->real_port);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
987 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
988 qd->is_redirect = FALSE;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
989
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
990 qd->fd = -1;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
991 qd->tx_handler = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
992
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
993 /* QQ connection via UDP/TCP.
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
994 * Now use Purple proxy function to provide TCP proxy support,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
995 * and qq_udp_proxy.c to add UDP proxy support (thanks henry) */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
996 if(qd->use_tcp) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
997 purple_debug(PURPLE_DEBUG_INFO, "QQ", "TCP Connect to %s:%d\n",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
998 qd->real_hostname, qd->real_port);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
999
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1000 /* TODO: is there a good default grow size? */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1001 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Create tcp_txbuf\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1002 qd->tcp_txbuf = purple_circ_buffer_new(0);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1003
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1004 qd->connect_data = purple_proxy_connect(NULL, account,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1005 qd->real_hostname, qd->real_port, qq_connect_cb, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1006 if (qd->connect_data == NULL) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1007 purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1008 _("Unable to connect."));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1009 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1010 return;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1011 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1012
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1013 purple_debug(PURPLE_DEBUG_INFO, "QQ", "UDP Connect to %s:%d\n",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1014 qd->real_hostname, qd->real_port);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1015
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1016 g_return_if_fail(qd->udp_query_data == NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1017 qd->udp_query_data = purple_dnsquery_a(qd->real_hostname, qd->real_port,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1018 udp_host_resolved, gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1019 if (qd->udp_query_data == NULL) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1020 purple_connection_error_reason(qd->gc,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1021 PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1022 _("Could not resolve hostname"));
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1023 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1024 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1025
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1026 /* clean up qq_data structure and all its components
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1027 * always used before a redirectly connection */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1028 void qq_disconnect(PurpleConnection *gc)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1029 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1030 qq_data *qd;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1031
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1032 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1033 qd = (qq_data *) gc->proto_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1034
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1035 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Disconnecting ...\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1036 /* finish all I/O */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1037 if (qd->fd >= 0 && qd->logged_in) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1038 qq_send_packet_logout(gc);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1039 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1040
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1041 if (qd->resend_timeout > 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1042 purple_timeout_remove(qd->resend_timeout);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1043 qd->resend_timeout = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1044 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1045
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1046 if (gc->inpa > 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1047 purple_input_remove(gc->inpa);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1048 gc->inpa = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1049 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1050
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1051 if (qd->fd >= 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1052 close(qd->fd);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1053 qd->fd = -1;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1054 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1055
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1056 if (qd->reconnect_timeout > 0) {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1057 purple_timeout_remove(qd->reconnect_timeout);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1058 qd->reconnect_timeout = 0;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1059 }
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1060
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1061 if (qd->connect_data != NULL) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1062 purple_debug(PURPLE_DEBUG_INFO, "QQ", "Cancel connect_data\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1063 purple_proxy_connect_cancel(qd->connect_data);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1064 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1065
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1066 if(qd->tcp_txbuf != NULL) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1067 purple_debug(PURPLE_DEBUG_INFO, "QQ", "destroy tcp_txbuf\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1068 purple_circ_buffer_destroy(qd->tcp_txbuf);
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1069 qd->tcp_txbuf = NULL;
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1070 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1071
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1072 if (qd->tx_handler) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1073 purple_input_remove(qd->tx_handler);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1074 qd->tx_handler = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1075 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1076 if (qd->tcp_rxqueue != NULL) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1077 purple_debug(PURPLE_DEBUG_INFO, "QQ", "destroy tcp_rxqueue\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1078 g_free(qd->tcp_rxqueue);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1079 qd->tcp_rxqueue = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1080 qd->tcp_rxlen = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1081 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1082
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1083 if (qd->udp_query_data != NULL) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1084 purple_debug(PURPLE_DEBUG_INFO, "QQ", "destroy udp_query_data\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1085 purple_dnsquery_destroy(qd->udp_query_data);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1086 qd->udp_query_data = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1087 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1088
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1089 memset(qd->rcv_window, 0, sizeof(qd->rcv_window));
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1090 qq_rcv_trans_remove_all(qd);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1091 qq_send_trans_remove_all(qd);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1092
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1093 if (qd->inikey) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1094 purple_debug(PURPLE_DEBUG_INFO, "QQ", "free inikey\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1095 g_free(qd->inikey);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1096 qd->inikey = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1097 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1098 if (qd->pwkey) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1099 purple_debug(PURPLE_DEBUG_INFO, "QQ", "free pwkey\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1100 g_free(qd->pwkey);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1101 qd->pwkey = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1102 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1103 if (qd->session_key) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1104 purple_debug(PURPLE_DEBUG_INFO, "QQ", "free session_key\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1105 g_free(qd->session_key);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1106 qd->session_key = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1107 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1108 if (qd->session_md5) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1109 purple_debug(PURPLE_DEBUG_INFO, "QQ", "free session_md5\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1110 g_free(qd->session_md5);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1111 qd->session_md5 = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1112 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1113 if (qd->my_ip) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1114 purple_debug(PURPLE_DEBUG_INFO, "QQ", "free my_ip\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1115 g_free(qd->my_ip);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1116 qd->my_ip = NULL;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1117 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1118
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1119 qq_group_packets_free(qd);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1120 qq_group_free_all(qd);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1121 qq_add_buddy_request_free(qd);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1122 qq_info_query_free(qd);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1123 qq_buddies_list_free(gc->account, qd);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1124 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1125
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1126 static gint encap(qq_data *qd, guint8 *buf, gint maxlen, guint16 cmd, guint16 seq,
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1127 guint8 *data, gint data_len)
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1128 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1129 gint bytes = 0;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1130 g_return_val_if_fail(qd != NULL && buf != NULL && maxlen > 0, -1);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1131
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1132 if (data == NULL) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1133 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Fail encap packet, data is NULL\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1134 return -1;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1135 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1136 if (data_len <= 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1137 purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Fail encap packet, data len <= 0\n");
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1138 return -1;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1139 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1140
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1141 /* QQ TCP packet has two bytes in the begining defines packet length
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1142 * so leave room here to store packet size */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1143 if (qd->use_tcp) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1144 bytes += qq_put16(buf + bytes, 0x0000);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1145 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1146 /* now comes the normal QQ packet as UDP */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1147 bytes += qq_put8(buf + bytes, QQ_PACKET_TAG);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1148 bytes += qq_put16(buf + bytes, QQ_CLIENT);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1149 bytes += qq_put16(buf + bytes, cmd);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1150
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1151 bytes += qq_put16(buf + bytes, seq);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1152
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1153 bytes += qq_put32(buf + bytes, qd->uid);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1154 bytes += qq_putdata(buf + bytes, data, data_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1155 bytes += qq_put8(buf + bytes, QQ_PACKET_TAIL);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1156
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1157 /* set TCP packet length at begin of the packet */
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1158 if (qd->use_tcp) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1159 qq_put16(buf, bytes);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1160 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1161
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1162 return bytes;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1163 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1164
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1165 gint qq_send_data(qq_data *qd, guint16 cmd, guint8 *data, gint data_len)
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1166 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1167 guint8 *buf;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1168 gint buf_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1169 gint bytes_sent;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1170 gint seq;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1171
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1172 g_return_val_if_fail(qd != NULL, -1);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1173 g_return_val_if_fail(data != NULL && data_len > 0, -1);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1174
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1175 buf = g_newa(guint8, MAX_PACKET_SIZE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1176 memset(buf, 0, MAX_PACKET_SIZE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1177 seq = ++(qd->send_seq);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1178 buf_len = encap(qd, buf, MAX_PACKET_SIZE, cmd, seq, data, data_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1179 if (buf_len <= 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1180 return -1;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1181 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1182
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1183 if (qd->use_tcp) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1184 bytes_sent = tcp_send_out(qd, buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1185 } else {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1186 bytes_sent = udp_send_out(qd, buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1187 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1188
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1189 /* always need ack */
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1190 qq_send_trans_append(qd, buf, buf_len, cmd, seq);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1191
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1192 if (QQ_DEBUG) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1193 qq_show_packet("QQ_SEND_DATA", buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1194 purple_debug(PURPLE_DEBUG_INFO, "QQ",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1195 "<== [%05d], %s, total %d bytes is sent %d\n",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1196 seq, qq_get_cmd_desc(cmd), buf_len, bytes_sent);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1197 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1198 return bytes_sent;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1199 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1200
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1201 /* send the packet generated with the given cmd and data
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1202 * return the number of bytes sent to socket if succeeds
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1203 * return -1 if there is any error */
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1204 gint qq_send_cmd_detail(qq_data *qd, guint16 cmd, guint16 seq, gboolean need_ack,
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1205 guint8 *data, gint data_len)
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1206 {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1207 guint8 *buf;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1208 gint buf_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1209 guint8 *encrypted_data;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1210 gint encrypted_len;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1211 gint bytes_sent;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1212
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1213 g_return_val_if_fail(qd != NULL && qd->session_key != NULL, -1);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1214 g_return_val_if_fail(data != NULL && data_len > 0, -1);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1215
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1216 encrypted_len = data_len + 16; /* at most 16 bytes more */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1217 encrypted_data = g_newa(guint8, encrypted_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1218
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1219 qq_encrypt(data, data_len, qd->session_key, encrypted_data, &encrypted_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1220
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1221 buf = g_newa(guint8, MAX_PACKET_SIZE);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1222 memset(buf, 0, MAX_PACKET_SIZE);
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1223 buf_len = encap(qd, buf, MAX_PACKET_SIZE, cmd, seq, encrypted_data, encrypted_len);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1224 if (buf_len <= 0) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1225 return -1;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1226 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1227
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1228 if (QQ_DEBUG) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1229 qq_show_packet("QQ_SEND_CMD", buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1230 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1231 if (qd->use_tcp) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1232 bytes_sent = tcp_send_out(qd, buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1233 } else {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1234 bytes_sent = udp_send_out(qd, buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1235 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1236
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1237 /* if it does not need ACK, we send ACK manually several times */
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1238 if (need_ack) {
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1239 qq_send_trans_append(qd, buf, buf_len, cmd, seq);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1240 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1241
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1242 if (QQ_DEBUG) {
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1243 qq_show_packet("QQ_SEND_CMD", buf, buf_len);
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1244 purple_debug(PURPLE_DEBUG_INFO, "QQ",
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1245 "<== [%05d], %s, total %d bytes is sent %d\n",
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1246 seq, qq_get_cmd_desc(cmd), buf_len, bytes_sent);
23052
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1247 }
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1248 return bytes_sent;
51dbe83ebbd3 patch-04-tcp-pending
SHiNE CsyFeK <csyfek@gmail.com>
parents:
diff changeset
1249 }
23053
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1250
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1251 gint qq_send_cmd(qq_data *qd, guint16 cmd, guint8 *data, gint data_len)
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1252 {
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1253 g_return_val_if_fail(qd != NULL, -1);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1254 g_return_val_if_fail(data != NULL && data_len > 0, -1);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1255
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1256 qd->send_seq++;
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1257 return qq_send_cmd_detail(qd, cmd, qd->send_seq, TRUE, data, data_len);
55f986ccbb6a patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents: 23052
diff changeset
1258 }