13870
|
1 /**
|
|
2 * The QQ2003C protocol plugin
|
|
3 *
|
|
4 * for gaim
|
|
5 *
|
|
6 * Copyright (C) 2004 Puzzlebird
|
|
7 * Henry Ou <henry@linux.net>
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU General Public License as published by
|
|
11 * the Free Software Foundation; either version 2 of the License, or
|
|
12 * (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 */
|
|
23
|
|
24 // START OF FILE
|
|
25 /*****************************************************************************/
|
|
26 #include "debug.h" // gaim_debug
|
|
27 #include "internal.h" // strlen, _("get_text")#include "md5.h" // md5 functions
|
|
28 //#include "md5.h"
|
|
29 #include "cipher.h" //gfhuang
|
|
30
|
|
31 #ifdef _WIN32
|
|
32 #define random rand
|
|
33 #define srandom srand
|
|
34 #endif
|
|
35
|
|
36 #include "utils.h" // qq_debug
|
|
37 #include "packet_parse.h" // MAX_PACKET_SIZE
|
|
38 #include "buddy_info.h" // qq_info_query_free
|
|
39 #include "buddy_opt.h" // qq_add_buddy_request_free
|
|
40 #include "group_admindlg.h" // qq_qun_info_window_free
|
|
41 #include "group_free.h" // qq_group_packets_free
|
|
42 #include "infodlg.h" // qq_contact_info_window_free
|
|
43 #include "login_logout.h" // qq_send_packet_login
|
|
44 #include "qq_proxy.h" //
|
|
45 #include "recv_core.h" // qq_pending, qq_b4_packets_free
|
|
46 #include "send_core.h" // qq_send_cmd
|
|
47 #include "sendqueue.h" // qq_sendqueue_timeout_callback
|
|
48 #include "udp_proxy_s5.h" // qq_proxy_sock5
|
|
49
|
|
50 /*****************************************************************************/
|
|
51
|
|
52 /* These functions are used only in development phased
|
|
53 *
|
|
54 static void _qq_show_socket(gchar *desc, gint fd) {
|
|
55 struct sockaddr_in sin;
|
|
56 gint len = sizeof(sin);
|
|
57 getsockname(fd, (struct sockaddr *)&sin, &len);
|
|
58 gaim_debug(GAIM_DEBUG_INFO, desc, "%s:%d\n",
|
|
59 inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
|
|
60 }
|
|
61 */
|
|
62
|
13871
|
63 void _qq_show_packet(gchar * desc, gchar * buf, gint len)
|
13870
|
64 {
|
|
65 char buf1[4096], buf2[10];
|
|
66 int i;
|
|
67 buf1[0] = 0;
|
|
68 for (i = 0; i < len; i++) {
|
|
69 sprintf(buf2, " %02x(%d)", buf[i] & 0xff, buf[i] & 0xff);
|
|
70 strcat(buf1, buf2);
|
|
71 }
|
|
72 strcat(buf1, "\n");
|
|
73 gaim_debug(GAIM_DEBUG_INFO, desc, buf1);
|
|
74 }
|
|
75
|
|
76 /*****************************************************************************/
|
|
77 // QQ 2003iii uses double MD5 for the pwkey to get the session key
|
|
78 static guint8 *_gen_pwkey(const gchar * pwd)
|
|
79 {
|
|
80 // md5_state_t ctx; //gfhuang
|
|
81 GaimCipher *cipher;
|
|
82 GaimCipherContext *context;
|
|
83
|
|
84 gchar pwkey_tmp[QQ_KEY_LENGTH];
|
|
85 /*
|
|
86 md5_init(&ctx);
|
|
87 md5_append(&ctx, pwd, strlen(pwd));
|
|
88 md5_finish(&ctx, pwkey_tmp);
|
|
89
|
|
90 md5_init(&ctx);
|
|
91 md5_append(&ctx, pwkey_tmp, QQ_KEY_LENGTH);
|
|
92 md5_finish(&ctx, pwkey_tmp);
|
|
93 */ //gfhuang
|
|
94
|
|
95 cipher = gaim_ciphers_find_cipher("md5");
|
|
96 context = gaim_cipher_context_new(cipher, NULL);
|
|
97 gaim_cipher_context_append(context, pwd, strlen(pwd));
|
|
98 gaim_cipher_context_digest(context, sizeof(pwkey_tmp), pwkey_tmp, NULL);
|
|
99 gaim_cipher_context_destroy(context);
|
|
100 context = gaim_cipher_context_new(cipher, NULL);
|
|
101 gaim_cipher_context_append(context, pwkey_tmp, QQ_KEY_LENGTH);
|
|
102 gaim_cipher_context_digest(context, sizeof(pwkey_tmp), pwkey_tmp, NULL);
|
|
103 gaim_cipher_context_destroy(context);
|
|
104
|
|
105
|
|
106 return g_memdup(pwkey_tmp, QQ_KEY_LENGTH);
|
|
107 } // _gen_pwkey
|
|
108
|
|
109
|
|
110 /*****************************************************************************/
|
13871
|
111 gint _qq_fill_host(struct sockaddr_in * addr, const gchar * host, guint16 port)
|
13870
|
112 {
|
|
113 if (!inet_aton(host, &(addr->sin_addr))) {
|
|
114 struct hostent *hp;
|
|
115 if (!(hp = gethostbyname(host))) {
|
|
116 return -1;
|
|
117 }
|
|
118 memset(addr, 0, sizeof(struct sockaddr_in));
|
|
119 memcpy(&(addr->sin_addr.s_addr), hp->h_addr, hp->h_length);
|
|
120 addr->sin_family = hp->h_addrtype;
|
|
121 } else
|
|
122 addr->sin_family = AF_INET;
|
|
123
|
|
124 addr->sin_port = htons(port);
|
|
125 return 0;
|
|
126 } // _qq_fill_host
|
|
127
|
|
128 /*****************************************************************************/
|
|
129 // the callback function after socket is built
|
|
130 // we setup the qq protocol related configuration here
|
|
131 static void _qq_got_login(gpointer data, gint source, GaimInputCondition cond)
|
|
132 {
|
|
133 qq_data *qd;
|
|
134 GaimConnection *gc;
|
|
135 gchar *buf;
|
|
136 const gchar *passwd;
|
|
137
|
|
138 gc = (GaimConnection *) data;
|
|
139 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
140
|
|
141 if (g_list_find(gaim_connections_get_all(), gc) == NULL) {
|
|
142 close(source);
|
|
143 return;
|
|
144 }
|
|
145
|
|
146 if (source < 0) { // socket returns -1
|
|
147 gaim_connection_error(gc, _("Unable to connect."));
|
|
148 return;
|
|
149 }
|
|
150
|
|
151 qd = (qq_data *) gc->proto_data;
|
|
152
|
|
153 // QQ use random seq, to minimize duplicated packets
|
|
154 srandom(time(NULL));
|
|
155 qd->send_seq = random() & 0x0000ffff;
|
|
156 qd->fd = source;
|
|
157 qd->logged_in = FALSE;
|
|
158 qd->channel = 1;
|
|
159 qd->uid = strtol(gaim_account_get_username(gaim_connection_get_account(gc)), NULL, 10);
|
|
160 qd->before_login_packets = g_queue_new();
|
|
161
|
|
162 // now generate md5 processed passwd
|
|
163 passwd = gaim_account_get_password(gaim_connection_get_account(gc));
|
|
164 qd->pwkey = _gen_pwkey(passwd);
|
|
165
|
|
166 qd->sendqueue_timeout = gaim_timeout_add(QQ_SENDQUEUE_TIMEOUT, qq_sendqueue_timeout_callback, gc);
|
|
167 gc->inpa = gaim_input_add(qd->fd, GAIM_INPUT_READ, qq_input_pending, gc);
|
|
168
|
|
169 // Update the login progress status display
|
|
170 buf = g_strdup_printf("Login as %d", qd->uid);
|
|
171 gaim_connection_update_progress(gc, buf, 1, QQ_CONNECT_STEPS);
|
|
172 g_free(buf);
|
|
173
|
|
174 // qq_send_packet_login(gc); // finally ready to fire
|
|
175 qq_send_packet_request_login_token(gc);
|
|
176 } // _qq_got_login
|
|
177
|
|
178 /*****************************************************************************/
|
|
179 // clean up qq_data structure and all its components
|
|
180 // always used before a redirectly connection
|
|
181 static void _qq_common_clean(GaimConnection * gc)
|
|
182 {
|
|
183 qq_data *qd;
|
|
184
|
|
185 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
186 qd = (qq_data *) gc->proto_data;
|
|
187
|
|
188 // finish all I/O
|
|
189 if (qd->fd >= 0 && qd->logged_in)
|
|
190 qq_send_packet_logout(gc);
|
|
191 close(qd->fd);
|
|
192
|
|
193 if (qd->sendqueue_timeout > 0) {
|
|
194 gaim_timeout_remove(qd->sendqueue_timeout);
|
|
195 qd->sendqueue_timeout = 0;
|
|
196 } // qd->sendqueue_timeout
|
|
197
|
|
198 if (gc->inpa > 0) {
|
|
199 gaim_input_remove(gc->inpa);
|
|
200 gc->inpa = 0;
|
|
201 } // gc->inpa
|
|
202
|
|
203 qq_b4_packets_free(qd);
|
|
204 qq_sendqueue_free(qd);
|
|
205 qq_group_packets_free(qd);
|
|
206 qq_group_free_all(qd);
|
|
207 qq_add_buddy_request_free(qd);
|
|
208 qq_info_query_free(qd);
|
|
209 qq_contact_info_window_free(qd);
|
|
210 qq_qun_info_window_free(qd);
|
|
211 qq_buddies_list_free(gc->account /* by gfhuang */, qd);
|
|
212
|
|
213 } // _qq_common_clean
|
|
214
|
|
215 /*****************************************************************************/
|
|
216 static gint _qq_proxy_none(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen)
|
|
217 {
|
|
218 gint fd = -1;
|
|
219
|
|
220 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Using UDP without proxy\n");
|
|
221 fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
222
|
|
223 if (fd < 0) {
|
|
224 gaim_debug(GAIM_DEBUG_ERROR, "QQ Redirect", "Unable to create socket: %s\n", strerror(errno));
|
|
225 return -1;
|
|
226 } // if fd
|
|
227
|
|
228 // we use non-blocking mode to speed up connection
|
|
229 fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
230
|
|
231 /* From Unix-socket-FAQ: http://www.faqs.org/faqs/unix-faq/socket/
|
|
232 *
|
|
233 * If a UDP socket is unconnected, which is the normal state after a
|
|
234 * bind() call, then send() or write() are not allowed, since no
|
|
235 * destination is available; only sendto() can be used to send data.
|
|
236 *
|
|
237 * Calling connect() on the socket simply records the specified address
|
|
238 * and port number as being the desired communications partner. That
|
|
239 * means that send() or write() are now allowed; they use the destination
|
|
240 * address and port given on the connect call as the destination of packets.
|
|
241 */
|
|
242 if (connect(fd, addr, addrlen) < 0) {
|
|
243 /* [EINPROGRESS]
|
|
244 * The socket is marked as non-blocking and the connection cannot be
|
|
245 * completed immediately. It is possible to select for completion by
|
|
246 * selecting the socket for writing.
|
|
247 * [EINTR]
|
|
248 * A signal interrupted the call.
|
|
249 * The connection is established asynchronously.
|
|
250 */
|
|
251 if ((errno == EINPROGRESS) || (errno == EINTR))
|
|
252 gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Connect in asynchronous mode.\n");
|
|
253 else {
|
|
254 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Faiil connection: %d\n", strerror(errno));
|
|
255 close(fd);
|
|
256 return -1;
|
|
257 } // if errno
|
|
258 } else { // connect returns 0
|
|
259 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Connected.\n");
|
|
260 fcntl(fd, F_SETFL, 0);
|
|
261 phb->func(phb->data, fd, GAIM_INPUT_READ);
|
|
262 } // if connect
|
|
263
|
|
264 return fd;
|
|
265 } // _qq_proxy_none
|
|
266
|
|
267 /*****************************************************************************/
|
|
268 // returns the socket handler, or -1 if there is any error
|
|
269 static gint _qq_udp_proxy_connect(GaimAccount * account,
|
|
270 const gchar * server,
|
|
271 guint16 port, void callback(gpointer, gint, GaimInputCondition), GaimConnection * gc)
|
|
272 {
|
|
273 struct sockaddr_in sin;
|
|
274 struct PHB *phb;
|
|
275 GaimProxyInfo *info;
|
|
276 qq_data *qd;
|
|
277
|
|
278 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, -1);
|
|
279 qd = (qq_data *) gc->proto_data;
|
|
280
|
|
281 info = gaim_account_get_proxy_info(account);
|
|
282
|
|
283 phb = g_new0(struct PHB, 1);
|
|
284 phb->host = g_strdup(server);
|
|
285 phb->port = port;
|
|
286 phb->account = account;
|
|
287 phb->gpi = info;
|
|
288 phb->func = callback;
|
|
289 phb->data = gc;
|
|
290
|
|
291 if (_qq_fill_host(&sin, server, port) < 0) {
|
|
292 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
|
|
293 "gethostbyname(\"%s\", %d) failed: %s\n", server, port, hstrerror(h_errno));
|
|
294 return -1;
|
|
295 } // if _qq_fill_host
|
|
296
|
|
297 if (info == NULL) {
|
|
298 qd->proxy_type = GAIM_PROXY_NONE;
|
|
299 return _qq_proxy_none(phb, (struct sockaddr *) &sin, sizeof(sin));
|
|
300 } // if info
|
|
301
|
|
302 qd->proxy_type = info->type;
|
|
303 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Choosing proxy type %d\n", info->type);
|
|
304
|
|
305 switch (info->type) {
|
|
306 case GAIM_PROXY_NONE:
|
|
307 return _qq_proxy_none(phb, (struct sockaddr *) &sin, sizeof(sin));
|
|
308 case GAIM_PROXY_SOCKS5:
|
|
309 // as the destination is always QQ server during the session,
|
|
310 // we can set dest_sin here, instead of _qq_s5_canread_again
|
|
311 _qq_fill_host(&qd->dest_sin, phb->host, phb->port);
|
|
312 _qq_fill_host(&sin, phb->gpi->host, phb->gpi->port);
|
|
313 return qq_proxy_socks5(phb, (struct sockaddr *) &sin, sizeof(sin));
|
|
314 default:
|
|
315 return _qq_proxy_none(phb, (struct sockaddr *) &sin, sizeof(sin));
|
|
316 } // switch
|
|
317
|
|
318 return -1;
|
|
319 }
|
|
320
|
|
321 /*****************************************************************************/
|
|
322 // QQ connection via UDP/TCP.
|
|
323 // I use GAIM proxy function to provide TCP proxy support,
|
|
324 // and qq_udp_proxy.c to add UDP proxy support (thanks henry)
|
|
325 // return the socket handle, -1 means fail
|
|
326 static gint _proxy_connect_full
|
|
327 (GaimAccount * account, const gchar * host, guint16 port, GaimInputFunction func, gpointer data, gboolean use_tcp) {
|
|
328
|
|
329 GaimConnection *gc;
|
|
330 qq_data *qd;
|
|
331
|
|
332 gc = gaim_account_get_connection(account);
|
|
333 qd = (qq_data *) gc->proto_data;
|
|
334 qd->server_ip = g_strdup(host);
|
|
335 qd->server_port = port;
|
|
336
|
|
337 return use_tcp ? gaim_proxy_connect(account, host, port, func, data) : // TCP mode
|
|
338 _qq_udp_proxy_connect(account, host, port, func, data); // UDP mode
|
|
339
|
|
340 } // _gaim_proxy_connect_full
|
|
341
|
|
342 /*****************************************************************************/
|
|
343 // establish a generic QQ connection
|
|
344 // TCP/UDP, and direct/redirected
|
|
345 // return the socket handler, or -1 if there is any error
|
|
346 gint qq_connect(GaimAccount * account, const gchar * host, guint16 port, gboolean use_tcp, gboolean is_redirect) {
|
|
347
|
|
348 GaimConnection *gc;
|
|
349
|
|
350 g_return_val_if_fail(host != NULL, -1);
|
|
351 g_return_val_if_fail(port > 0, -1);
|
|
352
|
|
353 gc = gaim_account_get_connection(account);
|
|
354 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, -1);
|
|
355
|
|
356 if (is_redirect)
|
|
357 _qq_common_clean(gc);
|
|
358
|
|
359 return _proxy_connect_full(account, host, port, _qq_got_login, gc, use_tcp);
|
|
360 } // qq_connect
|
|
361
|
|
362 /*****************************************************************************/
|
|
363 // clean up the given QQ connection and free all resources
|
|
364 void qq_disconnect(GaimConnection * gc)
|
|
365 {
|
|
366 qq_data *qd;
|
|
367
|
|
368 g_return_if_fail(gc != NULL);
|
|
369
|
|
370 _qq_common_clean(gc);
|
|
371
|
|
372 qd = gc->proto_data;
|
|
373 g_free(qd->inikey);
|
|
374 g_free(qd->pwkey);
|
|
375 g_free(qd->session_key);
|
|
376 g_free(qd->my_ip);
|
|
377 g_free(qd);
|
|
378
|
|
379 gc->proto_data = NULL;
|
|
380 } // qq_disconnect
|
|
381
|
|
382 /*****************************************************************************/
|
|
383 // send packet with proxy support
|
|
384 gint qq_proxy_write(qq_data * qd, guint8 * data, gint len)
|
|
385 {
|
|
386 guint8 *buf;
|
|
387 gint ret;
|
|
388
|
|
389 g_return_val_if_fail(qd != NULL && qd->fd >= 0 && data != NULL && len > 0, -1);
|
|
390
|
|
391 // TCP sock5 may be processed twice
|
|
392 // so we need to check qd->use_tcp as well
|
|
393 if ((!qd->use_tcp) && qd->proxy_type == GAIM_PROXY_SOCKS5) { // UDP sock5
|
|
394 buf = g_newa(guint8, len + 10);
|
|
395 buf[0] = 0x00;
|
|
396 buf[1] = 0x00; //reserved
|
|
397 buf[2] = 0x00; //frag
|
|
398 buf[3] = 0x01; //type
|
|
399 g_memmove(buf + 4, &(qd->dest_sin.sin_addr.s_addr), 4);
|
|
400 g_memmove(buf + 8, &(qd->dest_sin.sin_port), 2);
|
|
401 g_memmove(buf + 10, data, len);
|
|
402 ret = send(qd->fd, buf, len + 10, 0);
|
|
403 } else
|
|
404 ret = send(qd->fd, data, len, 0);
|
|
405
|
|
406 return ret;
|
|
407 }
|
|
408
|
|
409 /*****************************************************************************/
|
|
410 // read packet input with proxy support
|
|
411 gint qq_proxy_read(qq_data * qd, guint8 * data, gint len)
|
|
412 {
|
|
413 guint8 *buf;
|
|
414 gint bytes;
|
|
415 buf = g_newa(guint8, MAX_PACKET_SIZE + 10);
|
|
416
|
|
417 g_return_val_if_fail(qd != NULL && data != NULL && len > 0, -1);
|
|
418 g_return_val_if_fail(qd->fd > 0, -1);
|
|
419
|
|
420 bytes = read(qd->fd, buf, len + 10);
|
|
421 if (bytes < 0)
|
|
422 return -1;
|
|
423
|
|
424 if ((!qd->use_tcp) && qd->proxy_type == GAIM_PROXY_SOCKS5) { // UDP sock5
|
|
425 if (bytes < 10)
|
|
426 return -1;
|
|
427 bytes -= 10;
|
|
428 g_memmove(data, buf + 10, bytes); //cut off the header
|
|
429 } else
|
|
430 g_memmove(data, buf, bytes);
|
|
431
|
|
432 return bytes;
|
|
433 } // qq_proxy_read
|
|
434
|
|
435 /*****************************************************************************/
|
|
436 // END of FILE
|