Mercurial > pidgin
comparison libgaim/protocols/qq/send_core.c @ 14192:60b1bc8dbf37
[gaim-migrate @ 16863]
Renamed 'core' to 'libgaim'
committer: Tailor Script <tailor@pidgin.im>
author | Evan Schoenberg <evan.s@dreskin.net> |
---|---|
date | Sat, 19 Aug 2006 01:50:10 +0000 |
parents | |
children | 902c3aa4950a |
comparison
equal
deleted
inserted
replaced
14191:009db0b357b5 | 14192:60b1bc8dbf37 |
---|---|
1 /** | |
2 * The QQ2003C protocol plugin | |
3 * | |
4 * for gaim | |
5 * | |
6 * Copyright (C) 2004 Puzzlebird | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 */ | |
22 | |
23 #include "debug.h" | |
24 #include "internal.h" | |
25 | |
26 #include "crypt.h" | |
27 #include "header_info.h" | |
28 #include "packet_parse.h" | |
29 #include "qq.h" | |
30 #include "qq_proxy.h" | |
31 #include "send_core.h" | |
32 #include "sendqueue.h" | |
33 | |
34 /* create qq packet header with given sequence | |
35 * return the number of bytes in header if succeeds | |
36 * return -1 if there is any error */ | |
37 gint _create_packet_head_seq(guint8 *buf, guint8 **cursor, | |
38 GaimConnection *gc, guint16 cmd, gboolean is_auto_seq, guint16 *seq) | |
39 { | |
40 qq_data *qd; | |
41 gint bytes_expected, bytes_written; | |
42 | |
43 g_return_val_if_fail(gc != NULL && | |
44 gc->proto_data != NULL && buf != NULL && cursor != NULL && *cursor != NULL, -1); | |
45 | |
46 qd = (qq_data *) gc->proto_data; | |
47 if (is_auto_seq) | |
48 *seq = ++(qd->send_seq); | |
49 | |
50 *cursor = buf; | |
51 bytes_written = 0; | |
52 bytes_expected = (qd->use_tcp) ? QQ_TCP_HEADER_LENGTH : QQ_UDP_HEADER_LENGTH; | |
53 | |
54 /* QQ TCP packet has two bytes in the begining defines packet length | |
55 * so I leave room here for size */ | |
56 if (qd->use_tcp) | |
57 bytes_written += create_packet_w(buf, cursor, 0x0000); | |
58 | |
59 /* now comes the normal QQ packet as UDP */ | |
60 bytes_written += create_packet_b(buf, cursor, QQ_PACKET_TAG); | |
61 bytes_written += create_packet_w(buf, cursor, QQ_CLIENT); | |
62 bytes_written += create_packet_w(buf, cursor, cmd); | |
63 bytes_written += create_packet_w(buf, cursor, *seq); | |
64 | |
65 if (bytes_written != bytes_expected) { | |
66 gaim_debug(GAIM_DEBUG_ERROR, "QQ", | |
67 "Fail create qq header, expect %d bytes, written %d bytes\n", bytes_expected, bytes_written); | |
68 bytes_written = -1; | |
69 } | |
70 return bytes_written; | |
71 } | |
72 | |
73 /* for those need ack and resend no ack feed back from server | |
74 * return number of bytes written to the socket, | |
75 * return -1 if there is any error */ | |
76 gint _qq_send_packet(GaimConnection * gc, guint8 *buf, gint len, guint16 cmd) | |
77 { | |
78 qq_data *qd; | |
79 qq_sendpacket *p; | |
80 gint bytes_sent; | |
81 guint8 *cursor; | |
82 | |
83 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, -1); | |
84 | |
85 qd = (qq_data *) gc->proto_data; | |
86 | |
87 if (qd->use_tcp) { | |
88 if (len > MAX_PACKET_SIZE) { | |
89 gaim_debug(GAIM_DEBUG_ERROR, "QQ", | |
90 "xxx [%05d] %s, %d bytes is too large, do not send\n", | |
91 qq_get_cmd_desc(cmd), qd->send_seq, len); | |
92 return -1; | |
93 } else { /* I update the len for TCP packet */ | |
94 cursor = buf; | |
95 create_packet_w(buf, &cursor, len); | |
96 } | |
97 } | |
98 | |
99 bytes_sent = qq_proxy_write(qd, buf, len); | |
100 | |
101 if (bytes_sent >= 0) { /* put to queue, for matching server ACK usage */ | |
102 p = g_new0(qq_sendpacket, 1); | |
103 p->fd = qd->fd; | |
104 p->cmd = cmd; | |
105 p->send_seq = qd->send_seq; | |
106 p->resend_times = 0; | |
107 p->sendtime = time(NULL); | |
108 p->buf = g_memdup(buf, len); /* don't use g_strdup, may have 0x00 */ | |
109 p->len = len; | |
110 qd->sendqueue = g_list_append(qd->sendqueue, p); | |
111 } | |
112 | |
113 return bytes_sent; | |
114 } | |
115 | |
116 /* send the packet generated with the given cmd and data | |
117 * return the number of bytes sent to socket if succeeds | |
118 * return -1 if there is any error */ | |
119 gint qq_send_cmd(GaimConnection *gc, guint16 cmd, | |
120 gboolean is_auto_seq, guint16 seq, gboolean need_ack, guint8 *data, gint len) | |
121 { | |
122 qq_data *qd; | |
123 guint8 *buf, *cursor, *encrypted_data; | |
124 guint16 seq_ret; | |
125 gint encrypted_len, bytes_written, bytes_expected, bytes_sent; | |
126 | |
127 g_return_val_if_fail(gc != NULL && gc->proto_data != NULL, -1); | |
128 | |
129 qd = (qq_data *) gc->proto_data; | |
130 g_return_val_if_fail(qd->session_key != NULL, -1); | |
131 | |
132 buf = g_newa(guint8, MAX_PACKET_SIZE); | |
133 encrypted_len = len + 16; /* at most 16 bytes more */ | |
134 encrypted_data = g_newa(guint8, encrypted_len); | |
135 cursor = buf; | |
136 bytes_written = 0; | |
137 | |
138 qq_crypt(ENCRYPT, data, len, qd->session_key, encrypted_data, &encrypted_len); | |
139 | |
140 seq_ret = seq; | |
141 if (_create_packet_head_seq(buf, &cursor, gc, cmd, is_auto_seq, &seq_ret) >= 0) { | |
142 bytes_expected = 4 + encrypted_len + 1; | |
143 bytes_written += create_packet_dw(buf, &cursor, (guint32) qd->uid); | |
144 bytes_written += create_packet_data(buf, &cursor, encrypted_data, encrypted_len); | |
145 bytes_written += create_packet_b(buf, &cursor, QQ_PACKET_TAIL); | |
146 if (bytes_written == bytes_expected) { /* packet OK */ | |
147 /* if it does not need ACK, we send ACK manually several times */ | |
148 if (need_ack) /* my request, send it */ | |
149 bytes_sent = _qq_send_packet(gc, buf, cursor - buf, cmd); | |
150 else /* server's request, send ACK */ | |
151 bytes_sent = qq_proxy_write(qd, buf, cursor - buf); | |
152 | |
153 if (QQ_DEBUG) | |
154 gaim_debug(GAIM_DEBUG_INFO, "QQ", | |
155 "<== [%05d] %s, %d bytes\n", seq_ret, qq_get_cmd_desc(cmd), bytes_sent); | |
156 return bytes_sent; | |
157 } else { /* bad packet */ | |
158 gaim_debug(GAIM_DEBUG_ERROR, "QQ", | |
159 "Fail creating packet, expect %d bytes, written %d bytes\n", | |
160 bytes_expected, bytes_written); | |
161 return -1; | |
162 } | |
163 } | |
164 | |
165 return -1; | |
166 } |