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