Mercurial > pidgin
annotate plugins/icq/tcphandle.c @ 1382:0ccf91652dac
[gaim-migrate @ 1392]
Feel the sting of my bononononono
committer: Tailor Script <tailor@pidgin.im>
author | Rob Flynn <gaim@robflynn.com> |
---|---|
date | Fri, 05 Jan 2001 01:52:57 +0000 |
parents | 0a766047b4fd |
children | 4c510ca3563f |
rev | line source |
---|---|
1152 | 1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 /* | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
3 $Id: tcphandle.c 1319 2000-12-19 10:08:29Z warmenhoven $ |
1152 | 4 $Log$ |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
5 Revision 1.2 2000/12/19 10:08:29 warmenhoven |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
6 Yay, new icqlib |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
7 |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
8 Revision 1.14 2000/12/06 05:15:45 denis |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
9 Handling for mass TCP messages has been added based on patch by |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
10 Konstantin Klyagin <konst@konst.org.ua> |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
11 |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
12 Revision 1.13 2000/08/13 19:44:41 denis |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
13 Cyrillic recoding on received URL description added. |
1152 | 14 |
15 Revision 1.12 2000/07/09 22:19:35 bills | |
16 added new *Close functions, use *Close functions instead of *Delete | |
17 where correct, and misc cleanup | |
18 | |
19 Revision 1.11 2000/06/25 16:36:16 denis | |
20 '\n' was added at the end of log messages. | |
21 | |
22 Revision 1.10 2000/05/04 15:57:20 bills | |
23 Reworked file transfer notification, small bugfixes, and cleanups. | |
24 | |
25 Revision 1.9 2000/05/03 18:29:15 denis | |
26 Callbacks have been moved to the ICQLINK structure. | |
27 | |
28 Revision 1.8 2000/04/05 14:37:02 denis | |
29 Applied patch from "Guillaume R." <grs@mail.com> for basic Win32 | |
30 compatibility. | |
31 | |
32 Revision 1.7 2000/01/20 20:06:00 bills | |
33 removed debugging printfs | |
34 | |
35 Revision 1.6 2000/01/20 19:59:15 bills | |
36 first implementation of sending file requests | |
37 | |
38 Revision 1.5 1999/11/30 09:51:42 bills | |
39 more file xfer logic added | |
40 | |
41 Revision 1.4 1999/11/11 15:10:30 guruz | |
42 - Added Base for Webpager Messages. Please type "make fixme" | |
43 - Removed Segfault when kicq is started the first time | |
44 | |
45 Revision 1.3 1999/10/01 02:28:51 bills | |
46 icq_TCPProcessHello returns something now :) | |
47 | |
48 Revision 1.2 1999/10/01 00:49:20 lord | |
49 some compilation problems are fixed. | |
50 | |
51 Revision 1.1 1999/09/29 19:47:21 bills | |
52 reworked chat/file handling. fixed chat. (it's been broke since I put | |
53 non-blocking connects in) | |
54 | |
55 Revision 1.15 1999/07/16 15:45:59 denis | |
56 Cleaned up. | |
57 | |
58 Revision 1.14 1999/07/16 12:10:10 denis | |
59 tcp_packet* functions renamed to icq_Packet* | |
60 Cleaned up. | |
61 | |
62 Revision 1.13 1999/07/12 15:13:41 cproch | |
63 - added definition of ICQLINK to hold session-specific global variabled | |
64 applications which have more than one connection are now possible | |
65 - changed nearly every function defintion to support ICQLINK parameter | |
66 | |
67 Revision 1.12 1999/06/30 13:51:25 bills | |
68 cleanups | |
69 | |
70 Revision 1.11 1999/05/03 21:41:30 bills | |
71 initial file xfer support added- untested | |
72 | |
73 Revision 1.10 1999/04/29 09:36:06 denis | |
74 Cleanups, warning removed | |
75 | |
76 Revision 1.9 1999/04/17 19:40:33 bills | |
77 reworked code to use icq_TCPLinks instead of icq_ContactItem entries. | |
78 modified ProcessChatPacket to negotiate both sending and receiving chat | |
79 requests properly. | |
80 | |
81 Revision 1.8 1999/04/14 15:12:02 denis | |
82 Cleanups for "strict" compiling (-ansi -pedantic) | |
83 icq_ContactItem parameter added to function icq_TCPOnMessageReceived() | |
84 Segfault fixed on spoofed messages. | |
85 | |
86 */ | |
87 | |
88 #include <time.h> | |
89 | |
90 #ifndef _WIN32 | |
91 #include <unistd.h> | |
92 #endif | |
93 | |
94 #include "icqtypes.h" | |
95 #include "icq.h" | |
96 #include "icqlib.h" | |
97 | |
98 #include "tcp.h" | |
99 #include "stdpackets.h" | |
100 #include "tcplink.h" | |
101 | |
102 void icq_TCPOnMessageReceived(ICQLINK *link, DWORD uin, const char *message, DWORD id, icq_TCPLink *plink); | |
103 void icq_TCPOnURLReceived(ICQLINK *link, DWORD uin, const char *message, DWORD id); | |
104 void icq_TCPOnChatReqReceived(ICQLINK *link, DWORD uin, const char *message, DWORD id); | |
105 void icq_TCPOnFileReqReceived(ICQLINK *link, DWORD uin, const char *message, | |
106 const char *filename, unsigned long filesize, DWORD id); | |
107 void icq_TCPProcessAck(ICQLINK *link, icq_Packet *p); | |
108 void icq_HandleChatAck(icq_TCPLink *plink, icq_Packet *p, int port); | |
109 void icq_HandleChatHello(icq_TCPLink *plink); | |
110 void icq_HandleFileHello(icq_TCPLink *plink); | |
111 void icq_HandleFileAck(icq_TCPLink *plink, icq_Packet *p, int port); | |
112 | |
113 void icq_TCPProcessPacket(icq_Packet *p, icq_TCPLink *plink) | |
114 { | |
115 DWORD uin; | |
116 WORD version; | |
117 WORD command; | |
118 WORD type; | |
119 WORD status; | |
120 DWORD command_type; | |
121 DWORD filesize = 0; | |
122 DWORD port = 0; | |
123 | |
124 const char *message; | |
125 const char *filename = 0; | |
126 | |
127 icq_PacketBegin(p); | |
128 (void)icq_PacketRead32(p); | |
129 version=icq_PacketRead16(p); | |
130 command=icq_PacketRead16(p); | |
131 (void)icq_PacketRead16(p); | |
132 | |
133 uin=icq_PacketRead32(p); | |
134 type=icq_PacketRead16(p); | |
135 message=icq_PacketReadString(p); | |
136 (void)icq_PacketRead32(p); | |
137 (void)icq_PacketRead32(p); | |
138 (void)icq_PacketRead32(p); | |
139 (void)icq_PacketRead8(p); | |
140 status=icq_PacketRead16(p); | |
141 command_type=icq_PacketRead16(p); | |
142 | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
143 switch(type & ~ICQ_TCP_MASS_MASK) |
1152 | 144 { |
145 case ICQ_TCP_MSG_MSG: | |
146 case ICQ_TCP_MSG_URL: | |
147 p->id=icq_PacketRead32(p); | |
148 break; | |
149 | |
150 case ICQ_TCP_MSG_CHAT: | |
151 (void)icq_PacketReadString(p); | |
152 (void)icq_PacketRead16(p); | |
153 (void)icq_PacketRead16(p); | |
154 port=icq_PacketRead32(p); | |
155 p->id=icq_PacketRead32(p); | |
156 break; | |
157 | |
158 case ICQ_TCP_MSG_FILE: | |
159 (void)icq_PacketRead16(p); | |
160 (void)icq_PacketRead16(p); | |
161 filename=icq_PacketReadString(p); | |
162 filesize=icq_PacketRead32(p); | |
163 port=icq_PacketRead32(p); | |
164 p->id=icq_PacketRead32(p); | |
165 break; | |
166 | |
167 default: | |
168 icq_FmtLog(plink->icqlink, ICQ_LOG_WARNING, "unknown message packet, type %x\n", type); | |
169 } | |
170 | |
171 #ifdef TCP_PROCESS_TRACE | |
172 printf("packet processed from uin: %lu:\n", uin); | |
173 printf(" command: %x\ttype: %x\n", command, type); | |
174 printf(" status: %x\tcommand_type: %x\n", status, (int)command_type); | |
175 printf(" message %s\n", message); | |
176 printf(" id: %x\n", (int)p->id); | |
177 #endif | |
178 | |
179 switch(command) | |
180 { | |
181 case ICQ_TCP_MESSAGE: | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
182 switch(type & ~ICQ_TCP_MASS_MASK) |
1152 | 183 { |
184 case ICQ_TCP_MSG_MSG: | |
185 icq_TCPOnMessageReceived(plink->icqlink, uin, message, p->id, plink); | |
186 break; | |
187 | |
188 case ICQ_TCP_MSG_URL: | |
189 icq_TCPOnURLReceived(plink->icqlink, uin, message, p->id); | |
190 break; | |
191 | |
192 case ICQ_TCP_MSG_CHAT: | |
193 icq_TCPOnChatReqReceived(plink->icqlink, uin, message, p->id); | |
194 break; | |
195 | |
196 case ICQ_TCP_MSG_FILE: | |
197 icq_TCPOnFileReqReceived(plink->icqlink, uin, message, filename, filesize, p->id); | |
198 break; | |
199 | |
200 default: | |
201 icq_FmtLog(plink->icqlink, ICQ_LOG_WARNING, "unknown message type %d!\n", type); | |
202 break; | |
203 } | |
204 break; | |
205 | |
206 case ICQ_TCP_ACK: | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
207 switch(type) |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
208 { |
1152 | 209 case ICQ_TCP_MSG_CHAT: |
210 icq_HandleChatAck(plink, p, port); | |
211 break; | |
212 | |
213 case ICQ_TCP_MSG_FILE: | |
214 icq_HandleFileAck(plink, p, port); | |
215 break; | |
216 | |
217 case ICQ_TCP_MSG_MSG: | |
218 case ICQ_TCP_MSG_URL: | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
219 if(plink->icqlink->icq_RequestNotify) |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
220 { |
1152 | 221 icq_FmtLog(plink->icqlink, ICQ_LOG_MESSAGE, "received ack %d\n", p->id); |
222 (*plink->icqlink->icq_RequestNotify)(plink->icqlink, p->id, ICQ_NOTIFY_ACK, status, | |
223 (void *)message); | |
224 (*plink->icqlink->icq_RequestNotify)(plink->icqlink, p->id, ICQ_NOTIFY_SUCCESS, 0, 0); | |
225 } | |
226 break; | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
227 } |
1152 | 228 break; |
229 | |
230 case ICQ_TCP_CANCEL: | |
231 /* icq_TCPProcessCancel(p); */ | |
232 break; | |
233 | |
234 default: | |
235 icq_FmtLog(plink->icqlink, ICQ_LOG_WARNING, | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
236 "unknown packet command %d!\n", command); |
1152 | 237 } |
238 } | |
239 | |
240 void icq_TCPProcessCancel(icq_Packet *p) | |
241 { | |
242 (void)p; | |
243 | |
244 /* | |
245 find packet in queue | |
246 call notification function | |
247 remove packet from queue | |
248 */ | |
249 } | |
250 | |
251 int icq_TCPProcessHello(icq_Packet *p, icq_TCPLink *plink) | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
252 { |
1152 | 253 /* TCP Hello packet */ |
254 BYTE code; /* 0xFF - init packet code */ | |
255 DWORD version; /* tcp version */ | |
256 DWORD remote_port; /* remote message listen port */ | |
257 DWORD remote_uin; /* remote uin */ | |
258 DWORD remote_ip; /* remote IP as seen by ICQ server */ | |
259 DWORD remote_real_ip; /* remote IP as seen by client */ | |
260 BYTE flags; /* tcp flags */ | |
261 DWORD remote_other_port; /* remote chat or file listen port */ | |
262 | |
263 icq_PacketBegin(p); | |
264 | |
265 code=icq_PacketRead8(p); | |
266 version=icq_PacketRead32(p); | |
267 | |
268 if (!(p->length>=26 && code==ICQ_TCP_HELLO)) | |
269 { | |
270 icq_FmtLog(plink->icqlink, ICQ_LOG_WARNING, | |
271 "malformed hello packet received from %s:%d, closing link\n", | |
272 inet_ntoa(*((struct in_addr *)(&(plink->remote_address.sin_addr)))), | |
273 ntohs(plink->remote_address.sin_port)); | |
274 | |
275 icq_TCPLinkClose(plink); | |
276 return 0; | |
277 } | |
278 remote_port=icq_PacketRead32(p); | |
279 remote_uin=icq_PacketRead32(p); | |
280 remote_ip=icq_PacketRead32(p); | |
281 remote_real_ip=icq_PacketRead32(p); | |
282 flags=icq_PacketRead8(p); | |
283 remote_other_port=icq_PacketRead32(p); | |
284 | |
285 icq_FmtLog(plink->icqlink, ICQ_LOG_MESSAGE, | |
286 "hello packet received from %lu { version=%d }\n", remote_uin, version); | |
287 | |
288 plink->remote_version=version; | |
289 plink->remote_uin=remote_uin; | |
290 plink->flags=flags; | |
291 plink->mode&=~TCP_LINK_MODE_HELLOWAIT; | |
292 | |
293 /* file and chat sessions require additional handling */ | |
294 if(plink->type==TCP_LINK_CHAT) icq_HandleChatHello(plink); | |
295 if(plink->type==TCP_LINK_FILE) icq_HandleFileHello(plink); | |
296 | |
297 return 1; | |
298 } | |
299 | |
300 void icq_TCPOnMessageReceived(ICQLINK *link, DWORD uin, const char *message, DWORD id, icq_TCPLink *plink) | |
301 { | |
302 char data[512] ; | |
303 #ifdef TCP_PACKET_TRACE | |
304 printf("tcp message packet received from %lu { sequence=%x }\n", | |
305 uin, (int)id); | |
306 #endif | |
307 | |
308 if(link->icq_RecvMessage) | |
309 { | |
310 /* use the current system time for time received */ | |
311 time_t t=time(0); | |
312 struct tm *ptime=localtime(&t); | |
313 icq_Packet *pack; | |
314 icq_TCPLink *preallink=icq_FindTCPLink(link, uin, TCP_LINK_MESSAGE); | |
315 | |
316 strncpy(data,message,512) ; | |
317 icq_RusConv("wk",data) ; | |
318 | |
319 (*link->icq_RecvMessage)(link, uin, ptime->tm_hour, ptime->tm_min, | |
320 ptime->tm_mday, ptime->tm_mon+1, ptime->tm_year+1900, data); | |
321 | |
322 if(plink != preallink) | |
323 { | |
324 /* if(icq_SpoofedMessage) | |
325 (*icq_SpoofedMessage(uin, ...));*/ | |
326 } | |
327 | |
328 if(plink) | |
329 { | |
330 /* send an acknowledgement to the remote client */ | |
331 pack=icq_TCPCreateMessageAck(plink,0); | |
332 icq_PacketAppend32(pack, id); | |
333 icq_PacketSend(pack, plink->socket); | |
334 #ifdef TCP_PACKET_TRACE | |
335 printf("tcp message ack sent to uin %lu { sequence=%lx }\n", uin, id); | |
336 #endif | |
337 icq_PacketDelete(pack); | |
338 } | |
339 } | |
340 } | |
341 | |
342 void icq_TCPOnURLReceived(ICQLINK *link, DWORD uin, const char *message, DWORD id) | |
343 { | |
344 #ifdef TCP_PACKET_TRACE | |
345 printf("tcp url packet received from %lu { sequence=%lx }\n", | |
346 uin, id); | |
347 #endif /*TCP_PACKET_TRACE*/ | |
348 | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
349 if(link->icq_RecvURL) |
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
350 { |
1152 | 351 /* use the current system time for time received */ |
352 time_t t=time(0); | |
353 struct tm *ptime=localtime(&t); | |
354 icq_Packet *pack; | |
355 char *pfe; | |
356 icq_TCPLink *plink=icq_FindTCPLink(link, uin, TCP_LINK_MESSAGE); | |
357 | |
358 /* the URL is split from the description by 0xFE */ | |
359 pfe=strchr(message, '\xFE'); | |
360 *pfe=0; | |
1309
0a766047b4fd
[gaim-migrate @ 1319]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1152
diff
changeset
|
361 icq_RusConv("wk", (char*)message); |
1152 | 362 (*link->icq_RecvURL)(link, uin, ptime->tm_hour, ptime->tm_min, |
363 ptime->tm_mday, ptime->tm_mon+1, ptime->tm_year+1900, pfe+1, message); | |
364 | |
365 /* send an acknowledgement to the remote client */ | |
366 pack=icq_TCPCreateURLAck(plink,0); | |
367 icq_PacketAppend32(pack, id); | |
368 icq_PacketSend(pack, plink->socket); | |
369 #ifdef TCP_PACKET_TRACE | |
370 printf("tcp message ack sent to %lu { sequence=%lx }\n", uin, id); | |
371 #endif | |
372 icq_PacketDelete(pack); | |
373 } | |
374 } |