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