comparison libpurple/protocols/qq/send_core.c @ 15373:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15372:f79e0f4df793 15373:5fe8042783c1
1 /**
2 * @file send_core.c
3 *
4 * gaim
5 *
6 * Gaim 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include "debug.h"
26 #include "internal.h"
27
28 #include "crypt.h"
29 #include "header_info.h"
30 #include "packet_parse.h"
31 #include "qq.h"
32 #include "qq_proxy.h"
33 #include "send_core.h"
34 #include "sendqueue.h"
35
36 /* create qq packet header with given sequence
37 * return the number of bytes in header if succeeds
38 * return -1 if there is any error */
39 gint _create_packet_head_seq(guint8 *buf, guint8 **cursor,
40 GaimConnection *gc, guint16 cmd, gboolean is_auto_seq, guint16 *seq)
41 {
42 qq_data *qd;
43 gint bytes_expected, bytes_written;
44
45 g_return_val_if_fail(buf != NULL && cursor != NULL && *cursor != NULL, -1);
46
47 qd = (qq_data *) gc->proto_data;
48 if (is_auto_seq)
49 *seq = ++(qd->send_seq);
50
51 *cursor = buf;
52 bytes_written = 0;
53 bytes_expected = (qd->use_tcp) ? QQ_TCP_HEADER_LENGTH : QQ_UDP_HEADER_LENGTH;
54
55 /* QQ TCP packet has two bytes in the begining defines packet length
56 * so I leave room here for size */
57 if (qd->use_tcp)
58 bytes_written += create_packet_w(buf, cursor, 0x0000);
59
60 /* now comes the normal QQ packet as UDP */
61 bytes_written += create_packet_b(buf, cursor, QQ_PACKET_TAG);
62 bytes_written += create_packet_w(buf, cursor, QQ_CLIENT);
63 bytes_written += create_packet_w(buf, cursor, cmd);
64 bytes_written += create_packet_w(buf, cursor, *seq);
65
66 if (bytes_written != bytes_expected) {
67 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
68 "Fail create qq header, expect %d bytes, written %d bytes\n", bytes_expected, bytes_written);
69 bytes_written = -1;
70 }
71 return bytes_written;
72 }
73
74 /* for those need ack and resend no ack feed back from server
75 * return number of bytes written to the socket,
76 * return -1 if there is any error */
77 gint _qq_send_packet(GaimConnection *gc, guint8 *buf, gint len, guint16 cmd)
78 {
79 qq_data *qd;
80 qq_sendpacket *p;
81 gint bytes_sent;
82 guint8 *cursor;
83
84 qd = (qq_data *) gc->proto_data;
85
86 if (qd->use_tcp) {
87 if (len > MAX_PACKET_SIZE) {
88 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
89 "xxx [%05d] %s, %d bytes is too large, do not send\n",
90 qq_get_cmd_desc(cmd), qd->send_seq, len);
91 return -1;
92 } else { /* I update the len for TCP packet */
93 cursor = buf;
94 create_packet_w(buf, &cursor, len);
95 }
96 }
97
98 bytes_sent = qq_proxy_write(qd, buf, len);
99
100 if (bytes_sent >= 0) { /* put to queue, for matching server ACK usage */
101 p = g_new0(qq_sendpacket, 1);
102 p->fd = qd->fd;
103 p->cmd = cmd;
104 p->send_seq = qd->send_seq;
105 p->resend_times = 0;
106 p->sendtime = time(NULL);
107 p->buf = g_memdup(buf, len); /* don't use g_strdup, may have 0x00 */
108 p->len = len;
109 qd->sendqueue = g_list_append(qd->sendqueue, p);
110 }
111
112 return bytes_sent;
113 }
114
115 /* send the packet generated with the given cmd and data
116 * return the number of bytes sent to socket if succeeds
117 * return -1 if there is any error */
118 gint qq_send_cmd(GaimConnection *gc, guint16 cmd,
119 gboolean is_auto_seq, guint16 seq, gboolean need_ack, guint8 *data, gint len)
120 {
121 qq_data *qd;
122 guint8 *buf, *cursor, *encrypted_data;
123 guint16 seq_ret;
124 gint encrypted_len, bytes_written, bytes_expected, bytes_sent;
125
126 qd = (qq_data *) gc->proto_data;
127 g_return_val_if_fail(qd->session_key != NULL, -1);
128
129 buf = g_newa(guint8, MAX_PACKET_SIZE);
130 encrypted_len = len + 16; /* at most 16 bytes more */
131 encrypted_data = g_newa(guint8, encrypted_len);
132 cursor = buf;
133 bytes_written = 0;
134
135 qq_crypt(ENCRYPT, data, len, qd->session_key, encrypted_data, &encrypted_len);
136
137 seq_ret = seq;
138 if (_create_packet_head_seq(buf, &cursor, gc, cmd, is_auto_seq, &seq_ret) >= 0) {
139 bytes_expected = 4 + encrypted_len + 1;
140 bytes_written += create_packet_dw(buf, &cursor, (guint32) qd->uid);
141 bytes_written += create_packet_data(buf, &cursor, encrypted_data, encrypted_len);
142 bytes_written += create_packet_b(buf, &cursor, QQ_PACKET_TAIL);
143 if (bytes_written == bytes_expected) { /* packet OK */
144 /* if it does not need ACK, we send ACK manually several times */
145 if (need_ack) /* my request, send it */
146 bytes_sent = _qq_send_packet(gc, buf, cursor - buf, cmd);
147 else /* server's request, send ACK */
148 bytes_sent = qq_proxy_write(qd, buf, cursor - buf);
149
150 if (QQ_DEBUG)
151 gaim_debug(GAIM_DEBUG_INFO, "QQ",
152 "<== [%05d] %s, %d bytes\n", seq_ret, qq_get_cmd_desc(cmd), bytes_sent);
153 return bytes_sent;
154 } else { /* bad packet */
155 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
156 "Fail creating packet, expect %d bytes, written %d bytes\n",
157 bytes_expected, bytes_written);
158 return -1;
159 }
160 }
161
162 return -1;
163 }