Mercurial > pidgin
annotate src/protocols/icq/icqpacket.c @ 3063:22c84cbcd5a6
[gaim-migrate @ 3077]
A buddy window placement fix by Marc Deslauriers and a compile fix from Benjamin Miller. Thanks guys. Also, an irc fix.
committer: Tailor Script <tailor@pidgin.im>
author | Sean Egan <seanegan@gmail.com> |
---|---|
date | Sat, 16 Mar 2002 20:14:24 +0000 |
parents | 19b4cd08e458 |
children |
rev | line source |
---|---|
2086 | 1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | |
3 /* | |
2474
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
4 * $Id: icqpacket.c 2487 2001-10-10 19:58:11Z warmenhoven $ |
2086 | 5 * |
6 * Copyright (C) 1998-2001, Denis V. Dmitrienko <denis@null.net> and | |
7 * Bill Soudan <soudan@kde.org> | |
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., 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 * | |
23 */ | |
24 | |
25 /* | |
26 * ICQ packet abstraction | |
27 */ | |
28 | |
29 #include <stdlib.h> | |
30 | |
31 #ifdef _WIN32 | |
32 #include <winsock.h> | |
33 #else | |
34 #include <sys/types.h> | |
35 #include <sys/socket.h> | |
36 #endif | |
37 | |
38 #include "icqlib.h" | |
39 #include "tcp.h" | |
40 #include "icqbyteorder.h" | |
41 | |
42 icq_Packet *icq_PacketNew() | |
43 { | |
44 icq_Packet *p=(icq_Packet*)malloc(sizeof(icq_Packet)); | |
45 | |
46 p->length=0; | |
47 p->cursor=0; | |
48 p->id=0; | |
49 | |
50 return p; | |
51 } | |
52 | |
53 void icq_PacketDelete(void *p) | |
54 { | |
55 free(p); | |
56 } | |
57 | |
58 void icq_PacketAppend32(icq_Packet *p, DWORD i) | |
59 { | |
60 DWORD val=i; | |
61 | |
2474
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
62 val = htoicql(val); |
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
63 memcpy((p->data)+(p->cursor),&val,sizeof(DWORD)); |
2086 | 64 icq_PacketAdvance(p, sizeof(DWORD)); |
65 } | |
66 | |
67 void icq_PacketAppend32n(icq_Packet *p, DWORD i) | |
68 { | |
69 DWORD val=i; | |
70 | |
2474
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
71 memcpy((p->data)+(p->cursor),&val,sizeof(DWORD)); |
2086 | 72 icq_PacketAdvance(p, sizeof(DWORD)); |
73 } | |
74 | |
75 DWORD icq_PacketRead32(icq_Packet *p) | |
76 { | |
77 DWORD val; | |
78 | |
2474
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
79 memcpy(&val,(p->data)+(p->cursor),sizeof(DWORD)); |
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
80 val = icqtohl(val); |
2086 | 81 icq_PacketAdvance(p, sizeof(DWORD)); |
82 | |
83 return val; | |
84 } | |
85 | |
86 DWORD icq_PacketRead32n(icq_Packet *p) | |
87 { | |
88 DWORD val; | |
89 | |
2474
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
90 memcpy(&val,(p->data)+(p->cursor),sizeof(DWORD)); |
2086 | 91 icq_PacketAdvance(p, sizeof(DWORD)); |
92 | |
93 return val; | |
94 } | |
95 | |
96 void icq_PacketAppend16(icq_Packet *p, WORD i) | |
97 { | |
98 WORD val=i; | |
99 | |
2474
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
100 val = htoicqs(val); |
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
101 memcpy((p->data)+(p->cursor),&val,sizeof(WORD)); |
2086 | 102 icq_PacketAdvance(p, sizeof(WORD)); |
103 } | |
104 | |
105 void icq_PacketAppend16n(icq_Packet *p, WORD i) | |
106 { | |
107 WORD val=i; | |
108 | |
2474
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
109 memcpy((p->data)+(p->cursor),&val,sizeof(WORD)); |
2086 | 110 icq_PacketAdvance(p, sizeof(WORD)); |
111 } | |
112 | |
113 WORD icq_PacketRead16(icq_Packet *p) | |
114 { | |
115 WORD val; | |
116 | |
2474
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
117 memcpy(&val,(p->data)+(p->cursor),sizeof(WORD)); |
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
118 val = icqtohs(val); |
2086 | 119 icq_PacketAdvance(p, sizeof(WORD)); |
120 | |
121 return val; | |
122 } | |
123 | |
124 WORD icq_PacketRead16n(icq_Packet *p) | |
125 { | |
126 WORD val; | |
127 | |
2474
19b4cd08e458
[gaim-migrate @ 2487]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
128 memcpy(&val,(p->data)+(p->cursor),sizeof(WORD)); |
2086 | 129 icq_PacketAdvance(p, sizeof(WORD)); |
130 | |
131 return val; | |
132 } | |
133 | |
134 void icq_PacketAppend8(icq_Packet *p, BYTE i) | |
135 { | |
136 BYTE val=i; | |
137 | |
138 memcpy((p->data)+(p->cursor), &val, sizeof(BYTE)); | |
139 icq_PacketAdvance(p, sizeof(BYTE)); | |
140 } | |
141 | |
142 BYTE icq_PacketRead8(icq_Packet *p) | |
143 { | |
144 BYTE val; | |
145 | |
146 memcpy(&val, (p->data)+(p->cursor), sizeof(BYTE)); | |
147 icq_PacketAdvance(p, sizeof(BYTE)); | |
148 | |
149 return val; | |
150 } | |
151 | |
152 void icq_PacketAppendString(icq_Packet *p, const char *s) | |
153 { | |
154 if(s) | |
155 { | |
156 int length=strlen(s)+1; | |
157 | |
158 icq_PacketAppend16(p, length); | |
159 icq_PacketAppend(p, s, length); | |
160 } | |
161 else | |
162 { | |
163 icq_PacketAppend16(p, 1); | |
164 icq_PacketAppend8(p,0); | |
165 } | |
166 } | |
167 | |
168 const char *icq_PacketReadString(icq_Packet *p) | |
169 { | |
170 int length=icq_PacketRead16(p); | |
171 | |
172 return (const char *)icq_PacketRead(p, length); | |
173 } | |
174 | |
175 char *icq_PacketReadStringNew(icq_Packet *p) | |
176 { | |
177 char *ptr; | |
178 int length=icq_PacketRead16(p); | |
179 | |
180 ptr = (char*)malloc(length); | |
181 if(!ptr) | |
182 return 0L; | |
183 strncpy(ptr, icq_PacketRead(p, length), length); | |
184 return ptr; | |
185 } | |
186 | |
187 void icq_PacketAppendStringFE(icq_Packet *p, const char *s) | |
188 { | |
189 if(s) | |
190 { | |
191 int length=strlen(s); | |
192 icq_PacketAppend(p, s, length); | |
193 } | |
194 icq_PacketAppend8(p, 0xFE); | |
195 } | |
196 | |
197 void icq_PacketAppendString0(icq_Packet *p, const char *s) | |
198 { | |
199 if(s) | |
200 { | |
201 int length=strlen(s); | |
202 icq_PacketAppend(p, s, length); | |
203 } | |
204 icq_PacketAppend8(p, 0); | |
205 } | |
206 | |
207 WORD icq_PacketReadUDPOutVer(icq_Packet *p) | |
208 { | |
209 icq_PacketGoto(p, 0); | |
210 return icq_PacketRead16(p); | |
211 } | |
212 | |
213 WORD icq_PacketReadUDPOutCmd(icq_Packet *p) | |
214 { | |
215 icq_PacketGoto(p, 14 /*2*/); | |
216 return icq_PacketRead16(p); | |
217 } | |
218 | |
219 WORD icq_PacketReadUDPOutSeq1(icq_Packet *p) | |
220 { | |
221 icq_PacketGoto(p, 16); | |
222 return icq_PacketRead16(p); | |
223 } | |
224 | |
225 WORD icq_PacketReadUDPOutSeq2(icq_Packet *p) | |
226 { | |
227 icq_PacketGoto(p, 18 /*4*/); | |
228 return icq_PacketRead16(p); | |
229 } | |
230 | |
231 WORD icq_PacketReadUDPInVer(icq_Packet *p) | |
232 { | |
233 icq_PacketGoto(p, 0); | |
234 return icq_PacketRead16(p); | |
235 } | |
236 | |
237 WORD icq_PacketReadUDPInCmd(icq_Packet *p) | |
238 { | |
239 icq_PacketGoto(p, 7); | |
240 return icq_PacketRead16(p); | |
241 } | |
242 | |
243 WORD icq_PacketReadUDPInCmdV3(icq_Packet *p) | |
244 { | |
245 icq_PacketGoto(p, 2); | |
246 return icq_PacketRead16(p); | |
247 } | |
248 | |
249 WORD icq_PacketReadUDPInSeq1(icq_Packet *p) | |
250 { | |
251 icq_PacketGoto(p, 9); | |
252 return icq_PacketRead16(p); | |
253 } | |
254 | |
255 WORD icq_PacketReadUDPInSeq2(icq_Packet *p) | |
256 { | |
257 icq_PacketGoto(p, 11); | |
258 return icq_PacketRead16(p); | |
259 } | |
260 | |
261 DWORD icq_PacketReadUDPInUIN(icq_Packet *p) | |
262 { | |
263 icq_PacketGoto(p, 13); | |
264 return icq_PacketRead32(p); | |
265 } | |
266 | |
267 void icq_PacketAppend(icq_Packet *p, const void *data, int length) | |
268 { | |
269 memcpy((p->data)+(p->cursor), data, length); | |
270 icq_PacketAdvance(p, length); | |
271 } | |
272 | |
273 const void *icq_PacketRead(icq_Packet *p, int length) | |
274 { | |
275 const void *data=(p->data)+(p->cursor); | |
276 | |
277 icq_PacketAdvance(p, length); | |
278 | |
279 return data; | |
280 } | |
281 | |
282 void icq_PacketShortDump(icq_Packet *p) | |
283 { | |
284 printf("icq_Packet %lx { id=%d, cursor=%x, length=%d }\n", | |
285 (long)p, (int)p->id, p->cursor, p->length); | |
286 } | |
287 | |
288 void icq_PacketDump(icq_Packet *p) | |
289 { | |
290 icq_PacketShortDump(p); | |
291 hex_dump((char*)&(p->length), p->length+sizeof(WORD)); | |
292 } | |
293 | |
294 void icq_PacketUDPDump(icq_Packet *p) | |
295 { | |
296 icq_PacketShortDump(p); | |
297 hex_dump((char*)&(p->data), p->length); | |
298 } | |
299 | |
300 void icq_PacketBegin(icq_Packet *p) | |
301 { | |
302 p->cursor=0; | |
303 } | |
304 | |
305 void icq_PacketEnd(icq_Packet *p) | |
306 { | |
307 p->cursor=p->length; | |
308 } | |
309 | |
310 void icq_PacketAdvance(icq_Packet *p, int i) | |
311 { | |
312 p->cursor+=i; | |
313 | |
314 if(p->cursor > p->length) | |
315 p->length=p->cursor; | |
316 | |
317 /* Do nothing, because we don't have ICQLINK here */ | |
318 /* if(p->cursor > ICQ_PACKET_DATA_SIZE) */ | |
319 /* icq_FmtLog(0L, ICQ_LOG_WARNING, "icq_Packet data overflow\n"); */ | |
320 } | |
321 | |
322 void icq_PacketGoto(icq_Packet *p, int i) | |
323 { | |
324 icq_PacketBegin(p); | |
325 icq_PacketAdvance(p, i); | |
326 } | |
327 | |
328 void icq_PacketGotoUDPOutData(icq_Packet *p, int i) | |
329 { | |
330 /* Go to data in UDP _client_ packet. */ | |
331 icq_PacketGoto(p, 24+i); | |
332 } | |
333 | |
334 void icq_PacketGotoUDPInData(icq_Packet *p, int i) | |
335 { | |
336 /* Go to data in UDP _server_ packet. */ | |
337 icq_PacketGoto(p, 21+i); | |
338 } | |
339 | |
340 WORD icq_PacketPos(icq_Packet *p) | |
341 { | |
342 return p->cursor; | |
343 } | |
344 | |
345 int icq_PacketSend(icq_Packet *p, int socket_fd) | |
346 { | |
347 int result; | |
348 | |
349 result=send(socket_fd, (const char*)&(p->length), p->length+sizeof(WORD), 0); | |
350 | |
351 #ifdef TCP_RAW_TRACE | |
352 printf("%d bytes sent on socket %d, result %d\n", | |
353 p->length+1, socket, result); | |
354 icq_PacketDump(p); | |
355 #endif /* TCP_RAW_TRACE */ | |
356 | |
357 return result; | |
358 } |