Wed, 30 Jul 2008 03:58:21 +0000
Cleanup unnecessary casts and etc.
| 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 | ||
|
23212
a39a33c79a3f
2008.07.12 - ccpaging <ecc_hy(at)hotmail.com>
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23194
diff
changeset
|
192 | static gboolean packet_check_ack(qq_data *qd, guint16 cmd, guint16 seq) |
| 23192 | 193 | { |
| 194 | gpointer trans; | |
| 195 | ||
| 196 | g_return_val_if_fail(qd != NULL, FALSE); | |
| 197 | ||
|
23212
a39a33c79a3f
2008.07.12 - ccpaging <ecc_hy(at)hotmail.com>
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23194
diff
changeset
|
198 | trans = qq_send_trans_find(qd, cmd, 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 */ |
|
23212
a39a33c79a3f
2008.07.12 - ccpaging <ecc_hy(at)hotmail.com>
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23194
diff
changeset
|
363 | is_reply = packet_check_ack(qd, cmd, seq); |
|
23193
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 | ||
|
23212
a39a33c79a3f
2008.07.12 - ccpaging <ecc_hy(at)hotmail.com>
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23194
diff
changeset
|
463 | /* keep alive will be sent in 30 seconds since last_receive |
|
a39a33c79a3f
2008.07.12 - ccpaging <ecc_hy(at)hotmail.com>
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23194
diff
changeset
|
464 | * QQ need a keep alive packet in every 60 seconds |
|
a39a33c79a3f
2008.07.12 - ccpaging <ecc_hy(at)hotmail.com>
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23194
diff
changeset
|
465 | gc->last_received = time(NULL); |
|
a39a33c79a3f
2008.07.12 - ccpaging <ecc_hy(at)hotmail.com>
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23194
diff
changeset
|
466 | */ |
| 23192 | 467 | purple_debug(PURPLE_DEBUG_INFO, "TCP_PENDING", |
| 468 | "Read %d bytes from socket, rxlen is %d\n", buf_len, qd->tcp_rxlen); | |
| 469 | qd->tcp_rxqueue = g_realloc(qd->tcp_rxqueue, buf_len + qd->tcp_rxlen); | |
| 470 | memcpy(qd->tcp_rxqueue + qd->tcp_rxlen, buf, buf_len); | |
| 471 | qd->tcp_rxlen += buf_len; | |
| 472 | ||
| 473 | pkt = g_newa(guint8, MAX_PACKET_SIZE); | |
| 474 | while (1) { | |
| 475 | if (qd->tcp_rxlen < QQ_TCP_HEADER_LENGTH) { | |
| 476 | break; | |
| 477 | } | |
| 478 | ||
| 479 | bytes = 0; | |
| 480 | bytes += qq_get16(&pkt_len, qd->tcp_rxqueue + bytes); | |
| 481 | if (qd->tcp_rxlen < pkt_len) { | |
| 482 | break; | |
| 483 | } | |
| 484 | ||
| 485 | purple_debug(PURPLE_DEBUG_INFO, "TCP_PENDING", | |
| 486 | "Packet len is %d bytes, rxlen is %d\n", pkt_len, qd->tcp_rxlen); | |
| 487 | ||
| 488 | if ( pkt_len < QQ_TCP_HEADER_LENGTH | |
| 489 | || *(qd->tcp_rxqueue + bytes) != QQ_PACKET_TAG | |
| 490 | || *(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
|
491 | /* HEY! This isn't even a QQ. What are you trying to pull? */ |
| 23192 | 492 | |
| 493 | purple_debug(PURPLE_DEBUG_ERROR, "TCP_PENDING", | |
| 494 | "Packet error, failed to check header and tail tag\n"); | |
| 495 | ||
| 496 | jump = memchr(qd->tcp_rxqueue + 1, QQ_PACKET_TAIL, qd->tcp_rxlen - 1); | |
| 497 | if ( !jump ) { | |
| 498 | purple_debug(PURPLE_DEBUG_INFO, "TCP_PENDING", | |
| 499 | "Failed to find next QQ_PACKET_TAIL, clear receive buffer\n"); | |
| 500 | g_free(qd->tcp_rxqueue); | |
| 501 | qd->tcp_rxqueue = NULL; | |
| 502 | qd->tcp_rxlen = 0; | |
| 503 | return; | |
| 504 | } | |
| 505 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
506 | /* jump and over QQ_PACKET_TAIL */ |
| 23192 | 507 | jump_len = (jump - qd->tcp_rxqueue) + 1; |
| 508 | purple_debug(PURPLE_DEBUG_INFO, "TCP_PENDING", | |
| 509 | "Find next QQ_PACKET_TAIL at %d, jump %d bytes\n", jump_len, jump_len + 1); | |
| 510 | g_memmove(qd->tcp_rxqueue, jump, qd->tcp_rxlen - jump_len); | |
| 511 | qd->tcp_rxlen -= jump_len; | |
| 512 | continue; | |
| 513 | } | |
| 514 | ||
| 515 | memset(pkt, 0, MAX_PACKET_SIZE); | |
| 516 | g_memmove(pkt, qd->tcp_rxqueue + bytes, pkt_len - bytes); | |
| 517 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
518 | /* jump to next packet */ |
| 23192 | 519 | qd->tcp_rxlen -= pkt_len; |
| 520 | if (qd->tcp_rxlen) { | |
| 521 | purple_debug(PURPLE_DEBUG_ERROR, "TCP_PENDING", | |
| 522 | "shrink tcp_rxqueue to %d\n", qd->tcp_rxlen); | |
| 523 | jump = g_memdup(qd->tcp_rxqueue + pkt_len, qd->tcp_rxlen); | |
| 524 | g_free(qd->tcp_rxqueue); | |
| 525 | qd->tcp_rxqueue = jump; | |
| 526 | } else { | |
| 527 | purple_debug(PURPLE_DEBUG_ERROR, "TCP_PENDING", | |
| 528 | "free tcp_rxqueue\n"); | |
| 529 | g_free(qd->tcp_rxqueue); | |
| 530 | qd->tcp_rxqueue = NULL; | |
| 531 | } | |
| 532 | ||
| 533 | if (pkt == NULL) { | |
| 534 | continue; | |
| 535 | } | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
536 | /* do not call packet_process before jump |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
537 | * packet_process may call disconnect and destory tcp_rxqueue */ |
| 23192 | 538 | packet_process(gc, pkt, pkt_len - bytes); |
| 539 | } | |
| 540 | } | |
| 541 | ||
| 542 | static void udp_pending(gpointer data, gint source, PurpleInputCondition cond) | |
| 543 | { | |
| 544 | PurpleConnection *gc; | |
| 545 | qq_data *qd; | |
| 546 | guint8 *buf; | |
| 547 | gint buf_len; | |
| 548 | ||
| 549 | gc = (PurpleConnection *) data; | |
| 550 | g_return_if_fail(gc != NULL && gc->proto_data != NULL); | |
| 551 | ||
| 552 | if(cond != PURPLE_INPUT_READ) { | |
| 553 | purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, | |
| 554 | _("Socket error")); | |
| 555 | return; | |
| 556 | } | |
| 557 | ||
| 558 | qd = (qq_data *) gc->proto_data; | |
| 559 | g_return_if_fail(qd->fd >= 0); | |
| 560 | ||
| 561 | buf = g_newa(guint8, MAX_PACKET_SIZE); | |
| 562 | ||
| 563 | /* here we have UDP proxy suppport */ | |
| 564 | buf_len = read(qd->fd, buf, MAX_PACKET_SIZE); | |
| 565 | if (buf_len <= 0) { | |
| 566 | purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, | |
| 567 | _("Unable to read from socket")); | |
| 568 | return; | |
| 569 | } | |
| 570 | ||
|
23212
a39a33c79a3f
2008.07.12 - ccpaging <ecc_hy(at)hotmail.com>
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23194
diff
changeset
|
571 | /* keep alive will be sent in 30 seconds since last_receive |
|
a39a33c79a3f
2008.07.12 - ccpaging <ecc_hy(at)hotmail.com>
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23194
diff
changeset
|
572 | * QQ need a keep alive packet in every 60 seconds |
|
a39a33c79a3f
2008.07.12 - ccpaging <ecc_hy(at)hotmail.com>
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23194
diff
changeset
|
573 | gc->last_received = time(NULL); |
|
a39a33c79a3f
2008.07.12 - ccpaging <ecc_hy(at)hotmail.com>
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23194
diff
changeset
|
574 | */ |
| 23192 | 575 | |
| 576 | if (buf_len < QQ_UDP_HEADER_LENGTH) { | |
| 577 | if (buf[0] != QQ_PACKET_TAG || buf[buf_len - 1] != QQ_PACKET_TAIL) { | |
| 578 | qq_hex_dump(PURPLE_DEBUG_ERROR, "UDP_PENDING", | |
| 579 | buf, buf_len, | |
| 580 | "Received packet is too short, or no header and tail tag"); | |
| 581 | return; | |
| 582 | } | |
| 583 | } | |
| 584 | ||
| 585 | packet_process(gc, buf, buf_len); | |
| 586 | } | |
| 587 | ||
| 588 | static gint udp_send_out(qq_data *qd, guint8 *data, gint data_len) | |
| 589 | { | |
| 590 | gint ret; | |
| 591 | ||
| 592 | g_return_val_if_fail(qd != NULL && qd->fd >= 0 && data != NULL && data_len > 0, -1); | |
| 593 | ||
| 594 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "Send %d bytes to socket %d\n", data_len, qd->fd); | |
| 595 | ||
| 596 | errno = 0; | |
| 597 | ret = send(qd->fd, data, data_len, 0); | |
| 598 | if (ret < 0 && errno == EAGAIN) { | |
| 599 | return ret; | |
| 600 | } | |
| 601 | ||
| 602 | if (ret < 0) { | |
| 603 | /* TODO: what to do here - do we really have to disconnect? */ | |
| 604 | purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Send failed: %d, %s\n", errno, g_strerror(errno)); | |
| 605 | purple_connection_error_reason(qd->gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, g_strerror(errno)); | |
| 606 | } | |
| 607 | return ret; | |
| 608 | } | |
| 609 | ||
| 610 | static void tcp_can_write(gpointer data, gint source, PurpleInputCondition cond) | |
| 611 | { | |
| 612 | qq_data *qd = data; | |
| 613 | int ret, writelen; | |
| 614 | ||
| 615 | writelen = purple_circ_buffer_get_max_read(qd->tcp_txbuf); | |
| 616 | if (writelen == 0) { | |
| 617 | purple_input_remove(qd->tx_handler); | |
| 618 | qd->tx_handler = 0; | |
| 619 | return; | |
| 620 | } | |
| 621 | ||
| 622 | ret = write(qd->fd, qd->tcp_txbuf->outptr, writelen); | |
| 623 | purple_debug(PURPLE_DEBUG_ERROR, "TCP_CAN_WRITE", | |
| 624 | "total %d bytes is sent %d\n", writelen, ret); | |
| 625 | ||
| 626 | if (ret < 0 && errno == EAGAIN) | |
| 627 | return; | |
| 628 | else if (ret < 0) { | |
| 629 | /* TODO: what to do here - do we really have to disconnect? */ | |
| 630 | purple_connection_error_reason(qd->gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, | |
| 631 | _("Write Error")); | |
| 632 | return; | |
| 633 | } | |
| 634 | ||
| 635 | purple_circ_buffer_mark_read(qd->tcp_txbuf, ret); | |
| 636 | } | |
| 637 | ||
| 638 | static gint tcp_send_out(qq_data *qd, guint8 *data, gint data_len) | |
| 639 | { | |
| 640 | gint ret; | |
| 641 | ||
| 642 | g_return_val_if_fail(qd != NULL && qd->fd >= 0 && data != NULL && data_len > 0, -1); | |
| 643 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
644 | /* |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
645 | * 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
|
646 | */ |
| 23192 | 647 | |
| 648 | if (qd->tx_handler == 0) { | |
| 649 | ret = write(qd->fd, data, data_len); | |
| 650 | } else { | |
| 651 | ret = -1; | |
| 652 | errno = EAGAIN; | |
| 653 | } | |
| 654 | ||
| 655 | purple_debug(PURPLE_DEBUG_INFO, "TCP_SEND_OUT", | |
| 656 | "Socket %d, total %d bytes is sent %d\n", qd->fd, data_len, ret); | |
| 657 | if (ret < 0 && errno == EAGAIN) { | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
658 | /* socket is busy, send later */ |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
659 | /* |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
660 | * 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
|
661 | */ |
| 23192 | 662 | ret = 0; |
| 663 | } else if (ret <= 0) { | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
664 | /* TODO: what to do here - do we really have to disconnect? */ |
| 23192 | 665 | purple_debug(PURPLE_DEBUG_ERROR, "TCP_SEND_OUT", |
| 666 | "Send to socket %d failed: %d, %s\n", qd->fd, errno, g_strerror(errno)); | |
| 667 | purple_connection_error_reason(qd->gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, g_strerror(errno)); | |
| 668 | return ret; | |
| 669 | } | |
| 670 | ||
| 671 | if (ret < data_len) { | |
| 672 | purple_debug(PURPLE_DEBUG_INFO, "TCP_SEND_OUT", | |
| 673 | "Add %d bytes to buffer\n", data_len - ret); | |
| 674 | if (qd->tx_handler == 0) { | |
| 675 | qd->tx_handler = purple_input_add(qd->fd, PURPLE_INPUT_WRITE, tcp_can_write, qd); | |
| 676 | } | |
| 677 | purple_circ_buffer_append(qd->tcp_txbuf, data + ret, data_len - ret); | |
| 678 | } | |
| 679 | return ret; | |
| 680 | } | |
| 681 | ||
| 682 | static gboolean trans_timeout(gpointer data) | |
| 683 | { | |
| 684 | PurpleConnection *gc; | |
| 685 | qq_data *qd; | |
| 686 | guint8 *buf; | |
| 687 | gint buf_len = 0; | |
| 688 | guint16 cmd; | |
| 689 | gint retries = 0; | |
| 690 | int index; | |
| 691 | ||
| 692 | gc = (PurpleConnection *) data; | |
| 693 | g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, TRUE); | |
| 694 | ||
| 695 | qd = (qq_data *) gc->proto_data; | |
| 696 | ||
| 697 | index = 0; | |
| 698 | buf = g_newa(guint8, MAX_PACKET_SIZE); | |
| 699 | ||
| 700 | while (1) { | |
| 701 | if (index < 0) { | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
702 | /* next record is NULL */ |
| 23192 | 703 | break; |
| 704 | } | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
705 | /* purple_debug(PURPLE_DEBUG_ERROR, "QQ", "scan begin %d\n", index); */ |
| 23192 | 706 | memset(buf, 0, MAX_PACKET_SIZE); |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
707 | buf_len = qq_send_trans_scan(qd, &index, buf, MAX_PACKET_SIZE, &cmd, &retries); |
| 23192 | 708 | if (buf_len <= 0) { |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
709 | /* curr record is empty, whole trans is NULL */ |
| 23192 | 710 | break; |
| 711 | } | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
712 | /* index = -1, when get last record of transactions */ |
| 23192 | 713 | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
714 | /* purple_debug(PURPLE_DEBUG_ERROR, "QQ", "retries %d next index %d\n", retries, index); */ |
| 23192 | 715 | if (retries > 0) { |
| 716 | if (qd->use_tcp) { | |
| 717 | tcp_send_out(qd, buf, buf_len); | |
| 718 | } else { | |
| 719 | udp_send_out(qd, buf, buf_len); | |
| 720 | } | |
| 721 | continue; | |
| 722 | } | |
| 723 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
724 | /* retries <= 0 */ |
| 23192 | 725 | switch (cmd) { |
| 726 | case QQ_CMD_KEEP_ALIVE: | |
| 727 | if (qd->logged_in) { | |
| 728 | purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Connection lost!\n"); | |
| 729 | purple_connection_error_reason(gc, | |
| 730 | PURPLE_CONNECTION_ERROR_NETWORK_ERROR, _("Connection lost")); | |
| 731 | qd->logged_in = FALSE; | |
| 732 | } | |
| 733 | break; | |
| 734 | case QQ_CMD_LOGIN: | |
| 735 | case QQ_CMD_REQUEST_LOGIN_TOKEN: | |
| 736 | if (!qd->logged_in) { | |
| 737 | /* cancel login progress */ | |
| 738 | purple_connection_error_reason(gc, | |
| 739 | PURPLE_CONNECTION_ERROR_NETWORK_ERROR, _("Login failed, no reply")); | |
| 740 | } | |
| 741 | break; | |
| 742 | default: | |
| 743 | purple_debug(PURPLE_DEBUG_WARNING, "QQ", | |
| 744 | "%s packet lost.\n", qq_get_cmd_desc(cmd)); | |
| 745 | } | |
| 746 | } | |
| 747 | ||
| 748 | return TRUE; /* if return FALSE, timeout callback stops */ | |
| 749 | } | |
| 750 | ||
| 751 | /* the callback function after socket is built | |
| 752 | * we setup the qq protocol related configuration here */ | |
| 753 | static void qq_connect_cb(gpointer data, gint source, const gchar *error_message) | |
| 754 | { | |
| 755 | qq_data *qd; | |
| 756 | PurpleConnection *gc; | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
757 | gchar *conn_msg; |
| 23192 | 758 | const gchar *passwd; |
| 759 | ||
| 760 | gc = (PurpleConnection *) data; | |
| 761 | ||
| 762 | if (!PURPLE_CONNECTION_IS_VALID(gc)) { | |
| 763 | purple_debug(PURPLE_DEBUG_INFO, "QQ_CONN", "Invalid connection\n"); | |
| 764 | close(source); | |
| 765 | return; | |
| 766 | } | |
| 767 | ||
| 768 | g_return_if_fail(gc != NULL && gc->proto_data != NULL); | |
| 769 | ||
| 770 | qd = (qq_data *) gc->proto_data; | |
| 771 | ||
| 772 | /* Connect is now complete; clear the PurpleProxyConnectData */ | |
| 773 | qd->connect_data = NULL; | |
| 774 | ||
| 775 | if (source < 0) { /* socket returns -1 */ | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
776 | 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
|
777 | qq_disconnect(gc); |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
778 | reconnect_later(gc); |
| 23192 | 779 | return; |
| 780 | } | |
| 781 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
782 | /* _qq_show_socket("Got login socket", source); */ |
| 23192 | 783 | |
| 784 | /* QQ use random seq, to minimize duplicated packets */ | |
| 785 | srandom(time(NULL)); | |
| 786 | qd->send_seq = random() & 0x0000ffff; | |
| 787 | qd->fd = source; | |
| 788 | qd->logged_in = FALSE; | |
| 789 | qd->channel = 1; | |
| 790 | qd->uid = strtol(purple_account_get_username(purple_connection_get_account(gc)), NULL, 10); | |
| 791 | ||
| 792 | /* now generate md5 processed passwd */ | |
| 793 | passwd = purple_account_get_password(purple_connection_get_account(gc)); | |
| 794 | g_return_if_fail(qd->pwkey == NULL); | |
| 795 | qd->pwkey = encrypt_account_password(passwd); | |
| 796 | ||
| 797 | g_return_if_fail(qd->resend_timeout == 0); | |
| 798 | /* call trans_timeout every 5 seconds */ | |
| 799 | qd->resend_timeout = purple_timeout_add(5000, trans_timeout, gc); | |
| 800 | ||
| 801 | if (qd->use_tcp) | |
| 802 | gc->inpa = purple_input_add(qd->fd, PURPLE_INPUT_READ, tcp_pending, gc); | |
| 803 | else | |
| 804 | gc->inpa = purple_input_add(qd->fd, PURPLE_INPUT_READ, udp_pending, gc); | |
| 805 | ||
| 806 | /* Update the login progress status display */ | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
807 | 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
|
808 | 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
|
809 | g_free(conn_msg); |
| 23192 | 810 | |
| 811 | qq_send_packet_request_login_token(gc); | |
| 812 | } | |
| 813 | ||
| 814 | static void udp_can_write(gpointer data, gint source, PurpleInputCondition cond) | |
| 815 | { | |
| 816 | PurpleConnection *gc; | |
| 817 | qq_data *qd; | |
| 818 | socklen_t len; | |
| 819 | int error=0, ret; | |
| 820 | ||
| 821 | gc = (PurpleConnection *) data; | |
| 822 | g_return_if_fail(gc != NULL && gc->proto_data != NULL); | |
| 823 | ||
| 824 | qd = (qq_data *) gc->proto_data; | |
| 825 | ||
| 826 | ||
| 827 | purple_debug_info("proxy", "Connected.\n"); | |
| 828 | ||
| 829 | /* | |
| 830 | * getsockopt after a non-blocking connect returns -1 if something is | |
| 831 | * really messed up (bad descriptor, usually). Otherwise, it returns 0 and | |
| 832 | * error holds what connect would have returned if it blocked until now. | |
| 833 | * Thus, error == 0 is success, error == EINPROGRESS means "try again", | |
| 834 | * and anything else is a real error. | |
| 835 | * | |
| 836 | * (error == EINPROGRESS can happen after a select because the kernel can | |
| 837 | * be overly optimistic sometimes. select is just a hint that you might be | |
| 838 | * able to do something.) | |
| 839 | */ | |
| 840 | len = sizeof(error); | |
| 841 | ret = getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len); | |
| 842 | if (ret == 0 && error == EINPROGRESS) | |
| 843 | return; /* we'll be called again later */ | |
| 844 | ||
| 845 | purple_input_remove(qd->tx_handler); | |
| 846 | qd->tx_handler = 0; | |
| 847 | if (ret < 0 || error != 0) { | |
| 848 | if(ret != 0) | |
| 849 | error = errno; | |
| 850 | ||
| 851 | close(source); | |
| 852 | ||
| 853 | purple_debug_error("proxy", "getsockopt SO_ERROR check: %s\n", g_strerror(error)); | |
| 854 | ||
| 855 | qq_connect_cb(gc, -1, _("Unable to connect")); | |
| 856 | return; | |
| 857 | } | |
| 858 | ||
| 859 | qq_connect_cb(gc, source, NULL); | |
| 860 | } | |
| 861 | ||
| 862 | static void udp_host_resolved(GSList *hosts, gpointer data, const char *error_message) { | |
| 863 | PurpleConnection *gc; | |
| 864 | qq_data *qd; | |
| 865 | struct sockaddr server_addr; | |
| 866 | int addr_size; | |
| 867 | gint fd = -1; | |
| 868 | int flags; | |
| 869 | ||
| 870 | gc = (PurpleConnection *) data; | |
| 871 | g_return_if_fail(gc != NULL && gc->proto_data != NULL); | |
| 872 | ||
| 873 | qd = (qq_data *) gc->proto_data; | |
| 874 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
875 | /* udp_query_data must be set as NULL. |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
876 | * Otherwise purple_dnsquery_destroy in qq_disconnect cause glib double free error */ |
| 23192 | 877 | qd->udp_query_data = NULL; |
| 878 | ||
| 879 | if (!hosts || !hosts->data) { | |
| 880 | purple_connection_error_reason(gc, | |
| 881 | PURPLE_CONNECTION_ERROR_NETWORK_ERROR, | |
| 882 | _("Couldn't resolve host")); | |
| 883 | return; | |
| 884 | } | |
| 885 | ||
| 886 | addr_size = GPOINTER_TO_INT(hosts->data); | |
| 887 | hosts = g_slist_remove(hosts, hosts->data); | |
| 888 | memcpy(&server_addr, hosts->data, addr_size); | |
| 889 | g_free(hosts->data); | |
| 890 | ||
| 891 | hosts = g_slist_remove(hosts, hosts->data); | |
| 892 | while(hosts) { | |
| 893 | hosts = g_slist_remove(hosts, hosts->data); | |
| 894 | g_free(hosts->data); | |
| 895 | hosts = g_slist_remove(hosts, hosts->data); | |
| 896 | } | |
| 897 | ||
| 898 | fd = socket(PF_INET, SOCK_DGRAM, 0); | |
| 899 | if (fd < 0) { | |
| 900 | purple_debug(PURPLE_DEBUG_ERROR, "QQ", | |
| 901 | "Unable to create socket: %s\n", g_strerror(errno)); | |
| 902 | return; | |
| 903 | } | |
| 904 | ||
| 905 | /* we use non-blocking mode to speed up connection */ | |
| 906 | flags = fcntl(fd, F_GETFL); | |
| 907 | fcntl(fd, F_SETFL, flags | O_NONBLOCK); | |
| 908 | ||
| 909 | /* From Unix-socket-FAQ: http://www.faqs.org/faqs/unix-faq/socket/ | |
| 910 | * | |
| 911 | * If a UDP socket is unconnected, which is the normal state after a | |
| 912 | * bind() call, then send() or write() are not allowed, since no | |
| 913 | * destination is available; only sendto() can be used to send data. | |
| 914 | * | |
| 915 | * Calling connect() on the socket simply records the specified address | |
| 916 | * and port number as being the desired communications partner. That | |
| 917 | * means that send() or write() are now allowed; they use the destination | |
| 918 | * address and port given on the connect call as the destination of packets. | |
| 919 | */ | |
| 920 | if (connect(fd, &server_addr, addr_size) >= 0) { | |
| 921 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "Connected.\n"); | |
| 922 | flags = fcntl(fd, F_GETFL); | |
| 923 | fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); | |
| 924 | qq_connect_cb(gc, fd, NULL); | |
| 925 | return; | |
| 926 | } | |
| 927 | ||
| 928 | /* [EINPROGRESS] | |
| 929 | * The socket is marked as non-blocking and the connection cannot be | |
| 930 | * completed immediately. It is possible to select for completion by | |
| 931 | * selecting the socket for writing. | |
| 932 | * [EINTR] | |
| 933 | * A signal interrupted the call. | |
| 934 | * The connection is established asynchronously. | |
| 935 | */ | |
| 936 | if ((errno == EINPROGRESS) || (errno == EINTR)) { | |
| 937 | purple_debug(PURPLE_DEBUG_WARNING, "QQ", "Connect in asynchronous mode.\n"); | |
| 938 | qd->tx_handler = purple_input_add(fd, PURPLE_INPUT_WRITE, udp_can_write, gc); | |
| 939 | return; | |
| 940 | } | |
| 941 | ||
|
23194
e8c312705ee8
Sun Jun 29 22:00:12 CST 2008 csyfek@gmail.com
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23193
diff
changeset
|
942 | purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Connection failed: %s\n", g_strerror(errno)); |
| 23192 | 943 | close(fd); |
| 944 | } | |
| 945 | ||
| 946 | /* establish a generic QQ connection | |
| 947 | * TCP/UDP, and direct/redirected */ | |
| 948 | void qq_connect(PurpleAccount *account) | |
| 949 | { | |
| 950 | PurpleConnection *gc; | |
| 951 | qq_data *qd; | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
952 | gchar *conn_msg; |
| 23192 | 953 | |
| 954 | gc = purple_account_get_connection(account); | |
| 955 | g_return_if_fail(gc != NULL && gc->proto_data != NULL); | |
| 956 | ||
| 957 | qd = (qq_data *) gc->proto_data; | |
| 958 | ||
|
23193
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 | /* test set_new_server |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
961 | while (set_new_server(qd)) { |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
962 | purple_debug(PURPLE_DEBUG_INFO, "QQ_TEST", |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
963 | "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
|
964 | 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
|
965 | } |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
966 | 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
|
967 | qd->servers); |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
968 | exit(1); |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
969 | */ |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
970 | if (qd->server_name == NULL) { |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
971 | /* must be first call this function */ |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
972 | if ( set_new_server(qd) != TRUE) { |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
973 | 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
|
974 | _("Failed to connect server")); |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
975 | return; |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
976 | } |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
977 | } |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
978 | |
| 23192 | 979 | if (qd->real_hostname == NULL || qd->real_port == 0) { |
| 980 | purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, | |
| 981 | _("hostname is NULL or port is 0")); | |
| 982 | return; | |
| 983 | } | |
| 984 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
985 | 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
|
986 | qd->real_hostname, qd->reconnect_times); |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
987 | 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
|
988 | g_free(conn_msg); |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
989 | |
| 23192 | 990 | if (qd->is_redirect) { |
| 991 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "Redirect to %s:%d\n", | |
| 992 | qd->real_hostname, qd->real_port); | |
| 993 | } | |
| 994 | qd->is_redirect = FALSE; | |
| 995 | ||
| 996 | qd->fd = -1; | |
| 997 | qd->tx_handler = 0; | |
| 998 | ||
| 999 | /* QQ connection via UDP/TCP. | |
| 1000 | * Now use Purple proxy function to provide TCP proxy support, | |
| 1001 | * and qq_udp_proxy.c to add UDP proxy support (thanks henry) */ | |
| 1002 | if(qd->use_tcp) { | |
| 1003 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "TCP Connect to %s:%d\n", | |
| 1004 | qd->real_hostname, qd->real_port); | |
| 1005 | ||
| 1006 | /* TODO: is there a good default grow size? */ | |
| 1007 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "Create tcp_txbuf\n"); | |
| 1008 | qd->tcp_txbuf = purple_circ_buffer_new(0); | |
| 1009 | ||
| 1010 | qd->connect_data = purple_proxy_connect(NULL, account, | |
| 1011 | qd->real_hostname, qd->real_port, qq_connect_cb, gc); | |
| 1012 | if (qd->connect_data == NULL) { | |
| 1013 | purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, | |
| 1014 | _("Unable to connect.")); | |
| 1015 | } | |
| 1016 | return; | |
| 1017 | } | |
| 1018 | ||
| 1019 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "UDP Connect to %s:%d\n", | |
| 1020 | qd->real_hostname, qd->real_port); | |
| 1021 | ||
| 1022 | g_return_if_fail(qd->udp_query_data == NULL); | |
| 1023 | qd->udp_query_data = purple_dnsquery_a(qd->real_hostname, qd->real_port, | |
| 1024 | udp_host_resolved, gc); | |
| 1025 | if (qd->udp_query_data == NULL) { | |
| 1026 | purple_connection_error_reason(qd->gc, | |
| 1027 | PURPLE_CONNECTION_ERROR_NETWORK_ERROR, | |
| 1028 | _("Could not resolve hostname")); | |
| 1029 | } | |
| 1030 | } | |
| 1031 | ||
| 1032 | /* clean up qq_data structure and all its components | |
| 1033 | * always used before a redirectly connection */ | |
| 1034 | void qq_disconnect(PurpleConnection *gc) | |
| 1035 | { | |
| 1036 | qq_data *qd; | |
| 1037 | ||
| 1038 | g_return_if_fail(gc != NULL && gc->proto_data != NULL); | |
| 1039 | qd = (qq_data *) gc->proto_data; | |
| 1040 | ||
| 1041 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "Disconnecting ...\n"); | |
| 1042 | /* finish all I/O */ | |
| 1043 | if (qd->fd >= 0 && qd->logged_in) { | |
| 1044 | qq_send_packet_logout(gc); | |
| 1045 | } | |
| 1046 | ||
| 1047 | if (qd->resend_timeout > 0) { | |
| 1048 | purple_timeout_remove(qd->resend_timeout); | |
| 1049 | qd->resend_timeout = 0; | |
| 1050 | } | |
| 1051 | ||
| 1052 | if (gc->inpa > 0) { | |
| 1053 | purple_input_remove(gc->inpa); | |
| 1054 | gc->inpa = 0; | |
| 1055 | } | |
| 1056 | ||
| 1057 | if (qd->fd >= 0) { | |
| 1058 | close(qd->fd); | |
| 1059 | qd->fd = -1; | |
| 1060 | } | |
| 1061 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1062 | if (qd->reconnect_timeout > 0) { |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1063 | purple_timeout_remove(qd->reconnect_timeout); |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1064 | qd->reconnect_timeout = 0; |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1065 | } |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1066 | |
| 23192 | 1067 | if (qd->connect_data != NULL) { |
| 1068 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "Cancel connect_data\n"); | |
| 1069 | purple_proxy_connect_cancel(qd->connect_data); | |
| 1070 | } | |
| 1071 | ||
| 1072 | if(qd->tcp_txbuf != NULL) { | |
| 1073 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "destroy tcp_txbuf\n"); | |
| 1074 | purple_circ_buffer_destroy(qd->tcp_txbuf); | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1075 | qd->tcp_txbuf = NULL; |
| 23192 | 1076 | } |
| 1077 | ||
| 1078 | if (qd->tx_handler) { | |
| 1079 | purple_input_remove(qd->tx_handler); | |
| 1080 | qd->tx_handler = 0; | |
| 1081 | } | |
| 1082 | if (qd->tcp_rxqueue != NULL) { | |
| 1083 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "destroy tcp_rxqueue\n"); | |
| 1084 | g_free(qd->tcp_rxqueue); | |
| 1085 | qd->tcp_rxqueue = NULL; | |
| 1086 | qd->tcp_rxlen = 0; | |
| 1087 | } | |
| 1088 | ||
| 1089 | if (qd->udp_query_data != NULL) { | |
| 1090 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "destroy udp_query_data\n"); | |
| 1091 | purple_dnsquery_destroy(qd->udp_query_data); | |
| 1092 | qd->udp_query_data = NULL; | |
| 1093 | } | |
| 1094 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1095 | 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
|
1096 | qq_rcv_trans_remove_all(qd); |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1097 | qq_send_trans_remove_all(qd); |
| 23192 | 1098 | |
| 1099 | if (qd->inikey) { | |
| 1100 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "free inikey\n"); | |
| 1101 | g_free(qd->inikey); | |
| 1102 | qd->inikey = NULL; | |
| 1103 | } | |
| 1104 | if (qd->pwkey) { | |
| 1105 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "free pwkey\n"); | |
| 1106 | g_free(qd->pwkey); | |
| 1107 | qd->pwkey = NULL; | |
| 1108 | } | |
| 1109 | if (qd->session_key) { | |
| 1110 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "free session_key\n"); | |
| 1111 | g_free(qd->session_key); | |
| 1112 | qd->session_key = NULL; | |
| 1113 | } | |
| 1114 | if (qd->session_md5) { | |
| 1115 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "free session_md5\n"); | |
| 1116 | g_free(qd->session_md5); | |
| 1117 | qd->session_md5 = NULL; | |
| 1118 | } | |
| 1119 | if (qd->my_ip) { | |
| 1120 | purple_debug(PURPLE_DEBUG_INFO, "QQ", "free my_ip\n"); | |
| 1121 | g_free(qd->my_ip); | |
| 1122 | qd->my_ip = NULL; | |
| 1123 | } | |
| 1124 | ||
| 1125 | qq_group_packets_free(qd); | |
| 1126 | qq_group_free_all(qd); | |
| 1127 | qq_add_buddy_request_free(qd); | |
| 1128 | qq_info_query_free(qd); | |
| 1129 | qq_buddies_list_free(gc->account, qd); | |
| 1130 | } | |
| 1131 | ||
| 1132 | static gint encap(qq_data *qd, guint8 *buf, gint maxlen, guint16 cmd, guint16 seq, | |
| 1133 | guint8 *data, gint data_len) | |
| 1134 | { | |
| 1135 | gint bytes = 0; | |
| 1136 | g_return_val_if_fail(qd != NULL && buf != NULL && maxlen > 0, -1); | |
| 1137 | ||
| 1138 | if (data == NULL) { | |
| 1139 | purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Fail encap packet, data is NULL\n"); | |
| 1140 | return -1; | |
| 1141 | } | |
| 1142 | if (data_len <= 0) { | |
| 1143 | purple_debug(PURPLE_DEBUG_ERROR, "QQ", "Fail encap packet, data len <= 0\n"); | |
| 1144 | return -1; | |
| 1145 | } | |
| 1146 | ||
| 1147 | /* QQ TCP packet has two bytes in the begining defines packet length | |
| 1148 | * so leave room here to store packet size */ | |
| 1149 | if (qd->use_tcp) { | |
| 1150 | bytes += qq_put16(buf + bytes, 0x0000); | |
| 1151 | } | |
| 1152 | /* now comes the normal QQ packet as UDP */ | |
| 1153 | bytes += qq_put8(buf + bytes, QQ_PACKET_TAG); | |
| 1154 | bytes += qq_put16(buf + bytes, QQ_CLIENT); | |
| 1155 | bytes += qq_put16(buf + bytes, cmd); | |
| 1156 | ||
| 1157 | bytes += qq_put16(buf + bytes, seq); | |
| 1158 | ||
| 1159 | bytes += qq_put32(buf + bytes, qd->uid); | |
| 1160 | bytes += qq_putdata(buf + bytes, data, data_len); | |
| 1161 | bytes += qq_put8(buf + bytes, QQ_PACKET_TAIL); | |
| 1162 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1163 | /* set TCP packet length at begin of the packet */ |
| 23192 | 1164 | if (qd->use_tcp) { |
| 1165 | qq_put16(buf, bytes); | |
| 1166 | } | |
| 1167 | ||
| 1168 | return bytes; | |
| 1169 | } | |
| 1170 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1171 | gint qq_send_data(qq_data *qd, guint16 cmd, guint8 *data, gint data_len) |
| 23192 | 1172 | { |
| 1173 | guint8 *buf; | |
| 1174 | gint buf_len; | |
| 1175 | gint bytes_sent; | |
| 1176 | gint seq; | |
| 1177 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1178 | g_return_val_if_fail(qd != NULL, -1); |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1179 | 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
|
1180 | |
| 23192 | 1181 | buf = g_newa(guint8, MAX_PACKET_SIZE); |
| 1182 | memset(buf, 0, MAX_PACKET_SIZE); | |
| 1183 | seq = ++(qd->send_seq); | |
| 1184 | buf_len = encap(qd, buf, MAX_PACKET_SIZE, cmd, seq, data, data_len); | |
| 1185 | if (buf_len <= 0) { | |
| 1186 | return -1; | |
| 1187 | } | |
| 1188 | ||
| 1189 | if (qd->use_tcp) { | |
| 1190 | bytes_sent = tcp_send_out(qd, buf, buf_len); | |
| 1191 | } else { | |
| 1192 | bytes_sent = udp_send_out(qd, buf, buf_len); | |
| 1193 | } | |
| 1194 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1195 | /* always need ack */ |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1196 | qq_send_trans_append(qd, buf, buf_len, cmd, seq); |
| 23192 | 1197 | |
| 1198 | if (QQ_DEBUG) { | |
| 1199 | qq_show_packet("QQ_SEND_DATA", buf, buf_len); | |
| 1200 | purple_debug(PURPLE_DEBUG_INFO, "QQ", | |
| 1201 | "<== [%05d], %s, total %d bytes is sent %d\n", | |
| 1202 | seq, qq_get_cmd_desc(cmd), buf_len, bytes_sent); | |
| 1203 | } | |
| 1204 | return bytes_sent; | |
| 1205 | } | |
| 1206 | ||
| 1207 | /* send the packet generated with the given cmd and data | |
| 1208 | * return the number of bytes sent to socket if succeeds | |
| 1209 | * return -1 if there is any error */ | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1210 | 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
|
1211 | guint8 *data, gint data_len) |
| 23192 | 1212 | { |
| 1213 | guint8 *buf; | |
| 1214 | gint buf_len; | |
| 1215 | guint8 *encrypted_data; | |
| 1216 | gint encrypted_len; | |
| 1217 | gint bytes_sent; | |
| 1218 | ||
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1219 | 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
|
1220 | g_return_val_if_fail(data != NULL && data_len > 0, -1); |
| 23192 | 1221 | |
| 1222 | encrypted_len = data_len + 16; /* at most 16 bytes more */ | |
| 1223 | encrypted_data = g_newa(guint8, encrypted_len); | |
| 1224 | ||
| 1225 | qq_encrypt(data, data_len, qd->session_key, encrypted_data, &encrypted_len); | |
| 1226 | ||
| 1227 | buf = g_newa(guint8, MAX_PACKET_SIZE); | |
| 1228 | memset(buf, 0, MAX_PACKET_SIZE); | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1229 | buf_len = encap(qd, buf, MAX_PACKET_SIZE, cmd, seq, encrypted_data, encrypted_len); |
| 23192 | 1230 | if (buf_len <= 0) { |
| 1231 | return -1; | |
| 1232 | } | |
| 1233 | ||
| 1234 | if (QQ_DEBUG) { | |
| 1235 | qq_show_packet("QQ_SEND_CMD", buf, buf_len); | |
| 1236 | } | |
| 1237 | if (qd->use_tcp) { | |
| 1238 | bytes_sent = tcp_send_out(qd, buf, buf_len); | |
| 1239 | } else { | |
| 1240 | bytes_sent = udp_send_out(qd, buf, buf_len); | |
| 1241 | } | |
| 1242 | ||
| 1243 | /* if it does not need ACK, we send ACK manually several times */ | |
| 1244 | if (need_ack) { | |
|
23193
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1245 | qq_send_trans_append(qd, buf, buf_len, cmd, seq); |
| 23192 | 1246 | } |
| 1247 | ||
| 1248 | if (QQ_DEBUG) { | |
| 1249 | purple_debug(PURPLE_DEBUG_INFO, "QQ", | |
| 1250 | "<== [%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
|
1251 | seq, qq_get_cmd_desc(cmd), buf_len, bytes_sent); |
| 23192 | 1252 | } |
| 1253 | return bytes_sent; | |
| 1254 | } | |
|
23193
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 | 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
|
1257 | { |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1258 | g_return_val_if_fail(qd != NULL, -1); |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1259 | 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
|
1260 | |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1261 | qd->send_seq++; |
|
384ba35bfa8f
patch-05-reconnect-and-code-cleanup
SHiNE CsyFeK <csyfek@gmail.com>
parents:
23192
diff
changeset
|
1262 | 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
|
1263 | } |