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