| |
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| |
2 /* |
| |
3 $Id: tcpfilehandle.c 1162 2000-11-28 02:22:42Z warmenhoven $ |
| |
4 $Log$ |
| |
5 Revision 1.1 2000/11/28 02:22:42 warmenhoven |
| |
6 icq. whoop de doo |
| |
7 |
| |
8 Revision 1.15 2000/07/24 03:10:08 bills |
| |
9 added support for real nickname during TCP transactions like file and |
| |
10 chat, instead of using Bill all the time (hmm, where'd I get that from? :) |
| |
11 |
| |
12 Revision 1.14 2000/07/09 22:19:35 bills |
| |
13 added new *Close functions, use *Close functions instead of *Delete |
| |
14 where correct, and misc cleanup |
| |
15 |
| |
16 Revision 1.13 2000/06/25 16:35:08 denis |
| |
17 '\n' was added at the end of log messages. |
| |
18 |
| |
19 Revision 1.12 2000/06/15 01:52:59 bills |
| |
20 fixed bug: sending file sessions would freeze if remote side changed speed |
| |
21 |
| |
22 Revision 1.11 2000/05/04 15:57:20 bills |
| |
23 Reworked file transfer notification, small bugfixes, and cleanups. |
| |
24 |
| |
25 Revision 1.10 2000/05/03 18:29:15 denis |
| |
26 Callbacks have been moved to the ICQLINK structure. |
| |
27 |
| |
28 Revision 1.9 2000/04/10 18:11:45 denis |
| |
29 ANSI cleanups. |
| |
30 |
| |
31 Revision 1.8 2000/04/10 16:36:04 denis |
| |
32 Some more Win32 compatibility from Guillaume Rosanis <grs@mail.com> |
| |
33 |
| |
34 Revision 1.7 2000/04/05 14:37:02 denis |
| |
35 Applied patch from "Guillaume R." <grs@mail.com> for basic Win32 |
| |
36 compatibility. |
| |
37 |
| |
38 Revision 1.6 2000/01/20 20:06:00 bills |
| |
39 removed debugging printfs |
| |
40 |
| |
41 Revision 1.5 2000/01/20 19:59:15 bills |
| |
42 first implementation of sending file requests |
| |
43 |
| |
44 Revision 1.4 2000/01/16 21:29:31 bills |
| |
45 added code so icq_FileSessions now keep track of the tcplink to which |
| |
46 they are attached |
| |
47 |
| |
48 Revision 1.3 1999/12/21 00:30:15 bills |
| |
49 added more file transfer logic to write file to disk |
| |
50 |
| |
51 Revision 1.2 1999/11/30 09:47:04 bills |
| |
52 added icq_HandleFileHello |
| |
53 |
| |
54 Revision 1.1 1999/09/29 19:47:21 bills |
| |
55 reworked chat/file handling. fixed chat. (it's been broke since I put |
| |
56 non-blocking connects in) |
| |
57 |
| |
58 */ |
| |
59 |
| |
60 #include <time.h> |
| |
61 |
| |
62 #ifndef _WIN32 |
| |
63 #include <unistd.h> |
| |
64 #endif |
| |
65 |
| |
66 #ifdef _MSVC_ |
| |
67 #include <io.h> |
| |
68 #define open _open |
| |
69 #define close _close |
| |
70 #define read _read |
| |
71 #define write _write |
| |
72 #endif |
| |
73 |
| |
74 #include <errno.h> |
| |
75 |
| |
76 #include "icqtypes.h" |
| |
77 #include "icq.h" |
| |
78 #include "icqlib.h" |
| |
79 |
| |
80 #include "tcp.h" |
| |
81 #include "icqpacket.h" |
| |
82 #include "stdpackets.h" |
| |
83 #include "tcplink.h" |
| |
84 #include "filesession.h" |
| |
85 |
| |
86 void icq_TCPOnFileReqReceived(ICQLINK *link, DWORD uin, const char *message, |
| |
87 const char *filename, unsigned long filesize, DWORD id) |
| |
88 { |
| |
89 #ifdef TCP_PACKET_TRACE |
| |
90 printf("file request packet received from %lu { sequence=%lx, message=%s }\n", |
| |
91 uin, id, message); |
| |
92 #endif |
| |
93 |
| |
94 if(link->icq_RecvFileReq) { |
| |
95 |
| |
96 /* use the current system time for time received */ |
| |
97 time_t t=time(0); |
| |
98 struct tm *ptime=localtime(&t); |
| |
99 |
| |
100 (*link->icq_RecvFileReq)(link, uin, ptime->tm_hour, ptime->tm_min, |
| |
101 ptime->tm_mday, ptime->tm_mon+1, ptime->tm_year+1900, message, |
| |
102 filename, filesize, id); |
| |
103 |
| |
104 /* don't send an acknowledgement to the remote client! |
| |
105 * GUI is responsible for sending acknowledgement once user accepts |
| |
106 * or denies using icq_TCPSendFileAck */ |
| |
107 } |
| |
108 } |
| |
109 |
| |
110 void icq_TCPProcessFilePacket(icq_Packet *p, icq_TCPLink *plink) |
| |
111 { |
| |
112 icq_FileSession *psession=(icq_FileSession *)plink->session; |
| |
113 BYTE type; |
| |
114 DWORD num_files; |
| |
115 DWORD total_bytes; |
| |
116 DWORD speed; |
| |
117 DWORD filesize; |
| |
118 const char *name; |
| |
119 int result; |
| |
120 |
| |
121 icq_Packet *presponse; |
| |
122 |
| |
123 icq_PacketBegin(p); |
| |
124 |
| |
125 type=icq_PacketRead8(p); |
| |
126 |
| |
127 switch(type) |
| |
128 { |
| |
129 case 0x00: |
| |
130 (void)icq_PacketRead32(p); |
| |
131 num_files=icq_PacketRead32(p); |
| |
132 total_bytes=icq_PacketRead32(p); |
| |
133 speed=icq_PacketRead32(p); |
| |
134 name=icq_PacketReadString(p); |
| |
135 psession->total_files=num_files; |
| |
136 psession->total_bytes=total_bytes; |
| |
137 psession->current_speed=speed; |
| |
138 icq_FileSessionSetHandle(psession, name); |
| |
139 icq_FileSessionSetStatus(psession, FILE_STATUS_INITIALIZED); |
| |
140 |
| |
141 /* respond */ |
| |
142 presponse=icq_TCPCreateFile01Packet(speed, plink->icqlink->icq_Nick); |
| |
143 |
| |
144 icq_TCPLinkSend(plink, presponse); |
| |
145 #ifdef TCP_PACKET_TRACE |
| |
146 printf("file 01 packet sent to uin %lu\n", plink->remote_uin); |
| |
147 #endif |
| |
148 |
| |
149 break; |
| |
150 |
| |
151 case 0x01: |
| |
152 speed=icq_PacketRead32(p); |
| |
153 name=icq_PacketReadString(p); |
| |
154 psession->current_speed=speed; |
| |
155 icq_FileSessionSetHandle(psession, name); |
| |
156 icq_FileSessionSetStatus(psession, FILE_STATUS_INITIALIZED); |
| |
157 |
| |
158 /* respond */ |
| |
159 icq_FileSessionPrepareNextFile(psession); |
| |
160 presponse=icq_TCPCreateFile02Packet(psession->current_file, |
| |
161 psession->current_file_size, psession->current_speed); |
| |
162 |
| |
163 icq_TCPLinkSend(plink, presponse); |
| |
164 #ifdef TCP_PACKET_TRACE |
| |
165 printf("file 02 packet sent to uin %lu\n", plink->remote_uin); |
| |
166 #endif |
| |
167 break; |
| |
168 |
| |
169 case 0x02: |
| |
170 /* when files are skipped |
| |
171 psession->total_transferred_bytes+= |
| |
172 (psession->current_file_size-psession->current_file_progress); |
| |
173 */ |
| |
174 |
| |
175 (void)icq_PacketRead8(p); |
| |
176 name=icq_PacketReadString(p); |
| |
177 (void)icq_PacketReadString(p); |
| |
178 filesize=icq_PacketRead32(p); |
| |
179 (void)icq_PacketRead32(p); |
| |
180 speed=icq_PacketRead32(p); |
| |
181 icq_FileSessionSetCurrentFile(psession, name); |
| |
182 psession->current_file_size=filesize; |
| |
183 psession->current_speed=speed; |
| |
184 psession->current_file_num++; |
| |
185 icq_FileSessionSetStatus(psession, FILE_STATUS_NEXT_FILE); |
| |
186 |
| |
187 /* respond */ |
| |
188 presponse=icq_TCPCreateFile03Packet(psession->current_file_progress, |
| |
189 speed); |
| |
190 |
| |
191 icq_TCPLinkSend(plink, presponse); |
| |
192 #ifdef TCP_PACKET_TRACE |
| |
193 printf("file 03 packet sent to uin %lu\n", plink->remote_uin); |
| |
194 #endif |
| |
195 break; |
| |
196 |
| |
197 case 0x03: |
| |
198 filesize=icq_PacketRead32(p); |
| |
199 (void)icq_PacketRead32(p); |
| |
200 speed=icq_PacketRead32(p); |
| |
201 psession->current_file_progress=filesize; |
| |
202 psession->total_transferred_bytes+=filesize; |
| |
203 psession->current_speed=speed; |
| |
204 |
| |
205 icq_FileSessionSetStatus(psession, FILE_STATUS_NEXT_FILE); |
| |
206 icq_FileSessionSetStatus(psession, FILE_STATUS_SENDING); |
| |
207 break; |
| |
208 |
| |
209 case 0x04: |
| |
210 (void)icq_PacketRead32(p); |
| |
211 icq_FileSessionSetStatus(psession, FILE_STATUS_STOP_FILE); |
| |
212 break; |
| |
213 |
| |
214 case 0x05: |
| |
215 speed=icq_PacketRead32(p); |
| |
216 psession->current_speed=speed; |
| |
217 if(plink->icqlink->icq_RequestNotify) |
| |
218 (*plink->icqlink->icq_RequestNotify)(plink->icqlink, plink->id, |
| |
219 ICQ_NOTIFY_FILE, FILE_STATUS_NEW_SPEED, 0); |
| |
220 break; |
| |
221 |
| |
222 case 0x06: |
| |
223 if(plink->icqlink->icq_RequestNotify) |
| |
224 (*plink->icqlink->icq_RequestNotify)(plink->icqlink, plink->id, ICQ_NOTIFY_FILEDATA, |
| |
225 p->length-sizeof(BYTE), p->data+sizeof(BYTE)); |
| |
226 icq_FileSessionSetStatus(psession, FILE_STATUS_RECEIVING); |
| |
227 result=write(psession->current_fd, p->data+sizeof(BYTE), p->length-sizeof(BYTE)); |
| |
228 psession->current_file_progress+=p->length-sizeof(BYTE); |
| |
229 psession->total_transferred_bytes+=p->length-sizeof(BYTE); |
| |
230 break; |
| |
231 |
| |
232 default: |
| |
233 icq_FmtLog(plink->icqlink, ICQ_LOG_WARNING, "unknown file packet type %d!\n", type); |
| |
234 |
| |
235 } |
| |
236 } |
| |
237 |
| |
238 void icq_HandleFileHello(icq_TCPLink *plink) |
| |
239 { |
| |
240 |
| |
241 /* once the hello packet has been processed and we know which uin this |
| |
242 * link is for, we can link up with a file session */ |
| |
243 icq_FileSession *pfile=icq_FindFileSession(plink->icqlink, |
| |
244 plink->remote_uin, 0); |
| |
245 |
| |
246 if(pfile) |
| |
247 { |
| |
248 plink->id=pfile->id; |
| |
249 plink->session=pfile; |
| |
250 pfile->tcplink=plink; |
| |
251 icq_FileSessionSetStatus(pfile, FILE_STATUS_CONNECTED); |
| |
252 |
| |
253 } else { |
| |
254 |
| |
255 icq_FmtLog(plink->icqlink, ICQ_LOG_WARNING, |
| |
256 "unexpected file hello received from %d, closing link\n", |
| |
257 plink->remote_uin); |
| |
258 icq_TCPLinkClose(plink); |
| |
259 } |
| |
260 |
| |
261 } |
| |
262 |
| |
263 void icq_HandleFileAck(icq_TCPLink *plink, icq_Packet *p, int port) |
| |
264 { |
| |
265 icq_TCPLink *pfilelink; |
| |
266 icq_FileSession *pfile; |
| |
267 icq_Packet *p2; |
| |
268 |
| |
269 if(plink->icqlink->icq_RequestNotify) |
| |
270 (*plink->icqlink->icq_RequestNotify)(plink->icqlink, p->id, ICQ_NOTIFY_ACK, 0, 0); |
| |
271 pfilelink=icq_TCPLinkNew(plink->icqlink); |
| |
272 pfilelink->type=TCP_LINK_FILE; |
| |
273 pfilelink->id=p->id; |
| |
274 |
| |
275 /* once the ack packet has been processed, link up with the file session */ |
| |
276 pfile=icq_FindFileSession(plink->icqlink, plink->remote_uin, 0); |
| |
277 |
| |
278 pfile->tcplink=pfilelink; |
| |
279 pfilelink->id=pfile->id; |
| |
280 |
| |
281 if (plink->icqlink->icq_RequestNotify) |
| |
282 (*plink->icqlink->icq_RequestNotify)(plink->icqlink, pfile->id, |
| |
283 ICQ_NOTIFY_FILESESSION, sizeof(icq_FileSession), pfile); |
| |
284 |
| |
285 icq_FileSessionSetStatus(pfile, FILE_STATUS_CONNECTING); |
| |
286 icq_TCPLinkConnect(pfilelink, plink->remote_uin, port); |
| |
287 |
| |
288 pfilelink->session=pfile; |
| |
289 |
| |
290 p2=icq_TCPCreateFile00Packet( pfile->total_files, |
| |
291 pfile->total_bytes, pfile->current_speed, plink->icqlink->icq_Nick); |
| |
292 icq_TCPLinkSend(pfilelink, p2); |
| |
293 |
| |
294 } |
| |
295 |