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