Tue, 19 Dec 2000 10:08:29 +0000
[gaim-migrate @ 1319]
Yay, new icqlib
| 1152 | 1 | /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
3 | $Id: tcp.c 1319 2000-12-19 10:08:29Z warmenhoven $ |
| 1152 | 4 | $Log$ |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
5 | Revision 1.2 2000/12/19 10:08:29 warmenhoven |
|
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
6 | Yay, new icqlib |
|
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
7 | |
|
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
8 | Revision 1.37 2000/12/19 06:00:07 bills |
|
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
9 | moved members from ICQLINK to ICQLINK_private struct |
| 1152 | 10 | |
| 11 | Revision 1.36 2000/07/09 22:19:35 bills | |
| 12 | added new *Close functions, use *Close functions instead of *Delete | |
| 13 | where correct, and misc cleanup | |
| 14 | ||
| 15 | Revision 1.35 2000/06/15 01:52:16 bills | |
| 16 | added Cancel and Refuse functions for chat and file reqs, changed packet | |
| 17 | sending code to use new icq_TCPLinkSendSeq function to elimitane duplicate | |
| 18 | code, removed *Seq functions, renamed chat req functions | |
| 19 | ||
| 20 | Revision 1.34 2000/05/04 15:57:20 bills | |
| 21 | Reworked file transfer notification, small bugfixes, and cleanups. | |
| 22 | ||
| 23 | Revision 1.33 2000/04/10 18:11:45 denis | |
| 24 | ANSI cleanups. | |
| 25 | ||
| 26 | Revision 1.32 2000/04/10 16:36:04 denis | |
| 27 | Some more Win32 compatibility from Guillaume Rosanis <grs@mail.com> | |
| 28 | ||
| 29 | Revision 1.31 2000/04/06 16:38:04 denis | |
| 30 | icq_*Send*Seq() functions with specified sequence number were added. | |
| 31 | ||
| 32 | Revision 1.30 2000/04/05 14:37:02 denis | |
| 33 | Applied patch from "Guillaume R." <grs@mail.com> for basic Win32 | |
| 34 | compatibility. | |
| 35 | ||
| 36 | Revision 1.29 2000/02/15 04:02:41 bills | |
| 37 | warning cleanup | |
| 38 | ||
| 39 | Revision 1.28 2000/02/15 03:58:20 bills | |
| 40 | use new icq_ChatRusConv_n function in icq_TCPSendChatData, | |
| 41 | new icq_TCPSendChatData_n function | |
| 42 | ||
| 43 | Revision 1.27 2000/02/07 02:40:23 bills | |
| 44 | new code for SOCKS connections, more cyrillic translations | |
| 45 | ||
| 46 | Revision 1.26 2000/01/20 19:59:15 bills | |
| 47 | first implementation of sending file requests | |
| 48 | ||
| 49 | Revision 1.25 2000/01/16 21:28:24 bills | |
| 50 | renamed icq_TCPAcceptFileReq to icq_AcceptFileRequest, moved file request | |
| 51 | functions to new file session code | |
| 52 | ||
| 53 | Revision 1.24 2000/01/16 03:59:10 bills | |
| 54 | reworked list code so list_nodes don't need to be inside item structures, | |
| 55 | removed strlist code and replaced with generic list calls | |
| 56 | ||
| 57 | Revision 1.23 1999/12/27 16:10:04 bills | |
| 58 | fixed buy in icq_TCPAcceptFileReq, added icq_TCPFileSetSpeed | |
| 59 | ||
| 60 | Revision 1.22 1999/12/21 00:29:59 bills | |
| 61 | moved _process_packet logic into tcplink::icq_TCPLinkProcessReceived, | |
| 62 | removed unnecessary icq_TCPSendFile??Packet functions | |
| 63 | ||
| 64 | Revision 1.21 1999/12/14 03:31:48 bills | |
| 65 | fixed double delete bug in _handle_ready_sockets, added code to implement | |
| 66 | connect timeout | |
| 67 | ||
| 68 | Revision 1.20 1999/11/30 09:44:31 bills | |
| 69 | added file session logic | |
| 70 | ||
| 71 | Revision 1.19 1999/09/29 20:07:12 bills | |
| 72 | cleanups, moved connect logic from _handle_ready_sockets to | |
| 73 | icq_TCPLinkOnConnect, tcp_link->icq_TCPLink | |
| 74 | ||
| 75 | Revision 1.18 1999/09/29 17:08:48 denis | |
| 76 | Cleanups. | |
| 77 | ||
| 78 | Revision 1.17 1999/07/18 20:19:56 bills | |
| 79 | added better log messages | |
| 80 | ||
| 81 | Revision 1.16 1999/07/16 15:45:56 denis | |
| 82 | Cleaned up. | |
| 83 | ||
| 84 | Revision 1.15 1999/07/16 12:14:13 denis | |
| 85 | tcp_packet* functions renamed to icq_Packet* | |
| 86 | Cleaned up. | |
| 87 | ||
| 88 | Revision 1.14 1999/07/12 15:13:34 cproch | |
| 89 | - added definition of ICQLINK to hold session-specific global variabled | |
| 90 | applications which have more than one connection are now possible | |
| 91 | - changed nearly every function defintion to support ICQLINK parameter | |
| 92 | ||
| 93 | Revision 1.13 1999/07/03 06:33:49 lord | |
| 94 | . byte order conversion macros added | |
| 95 | . some compilation warnings removed | |
| 96 | ||
| 97 | Revision 1.12 1999/06/30 13:52:22 bills | |
| 98 | implemented non-blocking connects | |
| 99 | ||
| 100 | Revision 1.11 1999/05/03 21:41:26 bills | |
| 101 | initial file xfer support added- untested | |
| 102 | ||
| 103 | Revision 1.10 1999/04/29 09:35:41 denis | |
| 104 | Cleanups, warning removed | |
| 105 | ||
| 106 | Revision 1.9 1999/04/17 19:30:50 bills | |
| 107 | _major_ restructuring. all tcp sockets (including listening sockets) are | |
| 108 | kept in global linked list, icq_TCPLinks. accept and listen functions | |
| 109 | moved to tcplink.c. changed return values of Send* functions to DWORD. | |
| 110 | ||
| 111 | Revision 1.8 1999/04/14 14:57:05 denis | |
| 112 | Cleanups for "strict" compiling (-ansi -pedantic) | |
| 113 | Parameter port added to function icq_TCPCreateListeningSocket() | |
| 114 | ||
| 115 | */ | |
| 116 | ||
| 117 | /* | |
| 118 | Peer-to-peer ICQ protocol implementation | |
| 119 | ||
| 120 | Uses version 2 of the ICQ protocol | |
| 121 | ||
| 122 | Thanks to Douglas F. McLaughlin and many others for | |
| 123 | packet details (see tcp02.txt) | |
| 124 | ||
| 125 | */ | |
| 126 | ||
| 127 | #include <stdlib.h> | |
| 128 | ||
| 129 | #ifndef _WIN32 | |
| 130 | #include <unistd.h> | |
| 131 | #endif | |
| 132 | ||
| 133 | #include <fcntl.h> | |
| 134 | #include <stdarg.h> | |
| 135 | #include <errno.h> | |
| 136 | ||
| 137 | #include <sys/types.h> | |
| 138 | ||
| 139 | #ifdef _WIN32 | |
| 140 | #include <winsock.h> | |
| 141 | #else | |
| 142 | #include <sys/socket.h> | |
| 143 | #endif | |
| 144 | ||
| 145 | #include <sys/stat.h> | |
| 146 | ||
| 147 | #ifndef _WIN32 | |
| 148 | #include <sys/time.h> | |
| 149 | #endif | |
| 150 | ||
| 151 | #include "icqtypes.h" | |
| 152 | #include "icqlib.h" | |
| 153 | ||
| 154 | #include "tcp.h" | |
| 155 | #include "stdpackets.h" | |
| 156 | #include "list.h" | |
| 157 | #include "tcplink.h" | |
| 158 | #include "chatsession.h" | |
| 159 | #include "filesession.h" | |
| 160 | ||
| 161 | /** | |
| 162 | Initializes structures necessary for TCP use. Not required by user | |
| 163 | programs. | |
| 164 | ||
| 165 | \return true on error | |
| 166 | */ | |
| 167 | ||
| 168 | int icq_TCPInit(ICQLINK *link) | |
| 169 | { | |
| 170 | icq_TCPLink *plink; | |
| 171 | ||
| 172 | /* allocate lists */ | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
173 | link->d->icq_TCPLinks=list_new(); |
|
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
174 | link->d->icq_ChatSessions=list_new(); |
|
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
175 | link->d->icq_FileSessions=list_new(); |
| 1152 | 176 | |
| 177 | /* only the main listening socket gets created upon initialization - | |
| 178 | * the other two are created when necessary */ | |
| 179 | plink=icq_TCPLinkNew( link ); | |
| 180 | icq_TCPLinkListen(plink); | |
| 181 | link->icq_TCPSrvPort=ntohs(plink->socket_address.sin_port); | |
| 182 | ||
| 183 | /* reset tcp sequence number */ | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
184 | link->d->icq_TCPSequence=0xfffffffe; |
| 1152 | 185 | |
| 186 | return 0; | |
| 187 | } | |
| 188 | ||
| 189 | void icq_TCPDone(ICQLINK *link) | |
| 190 | { | |
| 191 | /* close and deallocate all tcp links, this will also close any attached | |
| 192 | * file or chat sessions */ | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
193 | list_delete(link->d->icq_TCPLinks, icq_TCPLinkDelete); |
|
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
194 | list_delete(link->d->icq_ChatSessions, icq_ChatSessionDelete); |
|
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
195 | list_delete(link->d->icq_FileSessions, icq_FileSessionDelete); |
| 1152 | 196 | } |
| 197 | ||
| 198 | /* helper function for icq_TCPMain */ | |
| 199 | int _generate_fds(void *p, va_list data) | |
| 200 | { | |
| 201 | icq_TCPLink *plink=(icq_TCPLink *)p; | |
| 202 | ICQLINK *icqlink = plink->icqlink; | |
| 203 | ||
| 204 | (void)data; | |
| 205 | ||
| 206 | if(plink->socket>-1) | |
| 207 | { | |
| 208 | int socket=plink->socket; | |
| 209 | ||
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
210 | FD_SET(socket, &icqlink->d->TCP_readfds); |
| 1152 | 211 | |
| 212 | /* we only care about writing if socket is trying to connect */ | |
| 213 | if(plink->mode & TCP_LINK_MODE_CONNECTING) | |
| 214 | { | |
| 215 | if(plink->mode & (TCP_LINK_SOCKS_AUTHORIZATION | TCP_LINK_SOCKS_NOAUTHSTATUS | TCP_LINK_SOCKS_AUTHSTATUS | TCP_LINK_SOCKS_CONNSTATUS)) | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
216 | FD_SET(socket, &icqlink->d->TCP_readfds); |
| 1152 | 217 | else |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
218 | FD_SET(socket, &icqlink->d->TCP_writefds); |
| 1152 | 219 | } |
| 220 | ||
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
221 | if(socket+1>icqlink->d->TCP_maxfd) |
|
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
222 | icqlink->d->TCP_maxfd=socket+1; |
| 1152 | 223 | } |
| 224 | ||
| 225 | return 0; /* traverse the entire list */ | |
| 226 | } | |
| 227 | ||
| 228 | /* helper function for icq_TCPMain */ | |
| 229 | int _handle_ready_sockets(void *p, va_list data) | |
| 230 | { | |
| 231 | icq_TCPLink *plink=(icq_TCPLink *)p; | |
| 232 | ICQLINK *icqlink = plink->icqlink; | |
| 233 | int socket=plink->socket; | |
| 234 | ||
| 235 | (void)data; | |
| 236 | ||
| 237 | /* handle connecting sockets */ | |
| 238 | if (plink->mode & TCP_LINK_MODE_CONNECTING) | |
| 239 | { | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
240 | if(socket>-1 && (FD_ISSET(socket, &icqlink->d->TCP_writefds) || FD_ISSET(socket, &icqlink->d->TCP_readfds))) |
| 1152 | 241 | { |
| 242 | icq_TCPLinkOnConnect(plink); | |
| 243 | return 0; | |
| 244 | } | |
| 245 | ||
| 246 | if((time(0L) - plink->connect_time) > TCP_LINK_CONNECT_TIMEOUT) | |
| 247 | { | |
| 248 | icq_TCPLinkClose(plink); | |
| 249 | return 0; | |
| 250 | } | |
| 251 | } | |
| 252 | ||
| 253 | /* handle ready for read sockets- either a connection is waiting on * | |
| 254 | * the listen sockets or data is ready to be read */ | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
255 | if(socket>-1 && FD_ISSET(socket, &icqlink->d->TCP_readfds)) |
| 1152 | 256 | { |
| 257 | if(plink->mode & TCP_LINK_MODE_LISTEN) | |
| 258 | (void)icq_TCPLinkAccept(plink); | |
| 259 | else { | |
| 260 | ||
| 261 | int result=icq_TCPLinkOnDataReceived(plink); | |
| 262 | ||
| 263 | /* close the link if there was a receive error or if * | |
| 264 | * the remote end has closed the connection */ | |
| 265 | if (result < 1) | |
| 266 | icq_TCPLinkClose(plink); | |
| 267 | ||
| 268 | } | |
| 269 | } | |
| 270 | ||
| 271 | return 0; /* traverse the entire list */ | |
| 272 | } | |
| 273 | ||
| 274 | /* helper function for icq_TCPMain */ | |
| 275 | int _process_links(void *p, va_list data) | |
| 276 | { | |
| 277 | icq_TCPLink *plink=(icq_TCPLink *)p; | |
| 278 | ||
| 279 | (void)data; | |
| 280 | ||
| 281 | /* receive any packets watiting on the link */ | |
| 282 | icq_TCPLinkProcessReceived(plink); | |
| 283 | ||
| 284 | /* if this a currently sending file link, send data! */ | |
| 285 | if(plink->type==TCP_LINK_FILE) { | |
| 286 | icq_FileSession *psession=plink->session; | |
| 287 | if(psession && psession->status==FILE_STATUS_SENDING) | |
| 288 | icq_FileSessionSendData(psession); | |
| 289 | } | |
| 290 | ||
| 291 | return 0; /* traverse entire list */ | |
| 292 | } | |
| 293 | ||
| 294 | void icq_TCPMain(ICQLINK *link) | |
| 295 | { | |
| 296 | struct timeval tv; | |
| 297 | ||
| 298 | tv.tv_sec = 0; | |
| 299 | tv.tv_usec = 0; | |
| 300 | ||
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
301 | link->d->TCP_maxfd = 0; |
|
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
302 | FD_ZERO(&link->d->TCP_readfds); |
|
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
303 | FD_ZERO(&link->d->TCP_writefds); |
| 1152 | 304 | |
| 305 | /* generate the fd sets for all open tcp links */ | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
306 | (void)list_traverse(link->d->icq_TCPLinks, _generate_fds); |
| 1152 | 307 | |
| 308 | /* determine which sockets require maintenance */ | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
309 | select(link->d->TCP_maxfd, &link->d->TCP_readfds, &link->d->TCP_writefds, 0, &tv); |
| 1152 | 310 | |
| 311 | /* call icq_TCPLinkOnDataReceived for any sockets with ready data, | |
| 312 | * send all packets on send queue if socket has connected, and | |
| 313 | * accept() from any listening sockets with pending connections */ | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
314 | (void)list_traverse(link->d->icq_TCPLinks, _handle_ready_sockets, 0, 0); |
| 1152 | 315 | |
| 316 | /* process all packets waiting for each TCPLink */ | |
|
1309
dccfec27ffd4
[gaim-migrate @ 1319]
Eric Warmenhoven <warmenhoven@yahoo.com>
parents:
1152
diff
changeset
|
317 | (void)list_traverse(link->d->icq_TCPLinks, _process_links, 0, 0); |
| 1152 | 318 | } |
| 319 | ||
| 320 | icq_TCPLink *icq_TCPCheckLink(ICQLINK *link, DWORD uin, int type) | |
| 321 | { | |
| 322 | icq_TCPLink *plink=icq_FindTCPLink(link, uin, type); | |
| 323 | ||
| 324 | if(!plink) | |
| 325 | { | |
| 326 | plink=icq_TCPLinkNew( link ); | |
| 327 | if(type==TCP_LINK_MESSAGE) | |
| 328 | icq_TCPLinkConnect(plink, uin, 0); | |
| 329 | } | |
| 330 | ||
| 331 | return plink; | |
| 332 | ||
| 333 | } | |
| 334 | ||
| 335 | DWORD icq_TCPSendMessage(ICQLINK *link, DWORD uin, const char *message) | |
| 336 | { | |
| 337 | icq_TCPLink *plink; | |
| 338 | icq_Packet *p; | |
| 339 | DWORD sequence; | |
| 340 | char data[512] ; | |
| 341 | ||
| 342 | strncpy(data,message,512) ; | |
| 343 | icq_RusConv("kw", data) ; | |
| 344 | ||
| 345 | plink=icq_TCPCheckLink(link, uin, TCP_LINK_MESSAGE); | |
| 346 | ||
| 347 | /* create and send the message packet */ | |
| 348 | p=icq_TCPCreateMessagePacket(plink, (unsigned char *)data); | |
| 349 | sequence=icq_TCPLinkSendSeq(plink, p, 0); | |
| 350 | ||
| 351 | #ifdef TCP_PACKET_TRACE | |
| 352 | printf("message packet sent to uin %lu { sequence=%lx }\n", uin, p->id); | |
| 353 | #endif | |
| 354 | ||
| 355 | return sequence; | |
| 356 | } | |
| 357 | ||
| 358 | DWORD icq_TCPSendURL(ICQLINK *link, DWORD uin, const char *message, const char *url) | |
| 359 | { | |
| 360 | icq_TCPLink *plink; | |
| 361 | icq_Packet *p; | |
| 362 | DWORD sequence; | |
| 363 | char data[512]; | |
| 364 | ||
| 365 | plink=icq_TCPCheckLink(link, uin, TCP_LINK_MESSAGE); | |
| 366 | ||
| 367 | strncpy(data, message, 512); | |
| 368 | data[511] = '\0'; | |
| 369 | icq_RusConv("kw", data); | |
| 370 | ||
| 371 | /* create and send the url packet */ | |
| 372 | p=icq_TCPCreateURLPacket(plink, data, url); | |
| 373 | sequence=icq_TCPLinkSendSeq(plink, p, 0); | |
| 374 | ||
| 375 | #ifdef TCP_PACKET_TRACE | |
| 376 | printf("url packet queued for uin %lu { sequence=%lx }\n", uin, p->id); | |
| 377 | #endif | |
| 378 | ||
| 379 | return sequence; | |
| 380 | } | |
| 381 | ||
| 382 | DWORD icq_SendChatRequest(ICQLINK *link, DWORD uin, const char *message) | |
| 383 | { | |
| 384 | icq_TCPLink *plink; | |
| 385 | icq_Packet *p; | |
| 386 | DWORD sequence; | |
| 387 | char data[512]; | |
| 388 | ||
| 389 | plink=icq_TCPCheckLink(link, uin, TCP_LINK_MESSAGE); | |
| 390 | ||
| 391 | strncpy(data, message, 512); | |
| 392 | data[511] = '\0'; | |
| 393 | icq_RusConv("kw", data); | |
| 394 | ||
| 395 | /* create and send the url packet */ | |
| 396 | p=icq_TCPCreateChatReqPacket(plink, (unsigned char *)data); | |
| 397 | sequence=icq_TCPLinkSendSeq(plink, p, 0); | |
| 398 | ||
| 399 | #ifdef TCP_PACKET_TRACE | |
| 400 | printf("chat req packet sent to uin %lu { sequence=%lx }\n", uin, p->id); | |
| 401 | #endif | |
| 402 | ||
| 403 | return sequence; | |
| 404 | } | |
| 405 | ||
| 406 | unsigned long icq_SendFileRequest(ICQLINK *link, unsigned long uin, | |
| 407 | const char *message, char **files) | |
| 408 | { | |
| 409 | icq_TCPLink *plink; | |
| 410 | icq_FileSession *pfile; | |
| 411 | icq_Packet *p; | |
| 412 | unsigned long sequence; | |
| 413 | char filename[64]; | |
| 414 | char data[512]; | |
| 415 | ||
| 416 | plink=icq_TCPCheckLink(link, uin, TCP_LINK_MESSAGE); | |
| 417 | ||
| 418 | /* create the file session, this will be linked to the incoming icq_TCPLink | |
| 419 | * in icq_HandleFileAck */ | |
| 420 | pfile=icq_FileSessionNew(link); | |
| 421 | pfile->remote_uin=uin; | |
| 422 | pfile->files=files; | |
| 423 | pfile->direction=FILE_STATUS_SENDING; | |
| 424 | ||
| 425 | /* count the number and size of the files */ | |
| 426 | pfile->total_files=0; | |
| 427 | while(*files) { | |
| 428 | struct stat file_status; | |
| 429 | ||
| 430 | if(stat(*files, &file_status)==0) { | |
| 431 | pfile->total_files++; | |
| 432 | pfile->total_bytes+=file_status.st_size; | |
| 433 | } | |
| 434 | files++; | |
| 435 | } | |
| 436 | ||
| 437 | strncpy(filename, *(pfile->files), 64); | |
| 438 | ||
| 439 | strncpy(data, message, 512); | |
| 440 | data[511] = '\0'; | |
| 441 | icq_RusConv("kw", data); | |
| 442 | ||
| 443 | /* create and send the file req packet */ | |
| 444 | p=icq_TCPCreateFileReqPacket(plink, (char *)data, filename, | |
| 445 | pfile->total_bytes); | |
| 446 | sequence=icq_TCPLinkSendSeq(plink, p, 0); | |
| 447 | pfile->id=sequence; | |
| 448 | ||
| 449 | #ifdef TCP_PACKET_TRACE | |
| 450 | printf("file req packet sent to uin %lu { sequence=%lx }\n", uin, p->id); | |
| 451 | #endif | |
| 452 | ||
| 453 | return sequence; | |
| 454 | } | |
| 455 | ||
| 456 | void icq_AcceptChatRequest(ICQLINK *link, DWORD uin, unsigned long sequence) | |
| 457 | { | |
| 458 | icq_TCPLink *pmessage, *plisten; | |
| 459 | icq_ChatSession *pchat; | |
| 460 | icq_Packet *p; | |
| 461 | ||
| 462 | pmessage=icq_TCPCheckLink(link, uin, TCP_LINK_MESSAGE); | |
| 463 | ||
| 464 | /* create the chat listening socket if necessary */ | |
| 465 | if(!(plisten=icq_FindTCPLink(link, 0, TCP_LINK_CHAT))) | |
| 466 | { | |
| 467 | plisten=icq_TCPLinkNew( link ); | |
| 468 | plisten->type=TCP_LINK_CHAT; | |
| 469 | icq_TCPLinkListen(plisten); | |
| 470 | } | |
| 471 | ||
| 472 | /* create the chat session, this will be linked to the incoming icq_TCPLink | |
| 473 | * in TCPProcessHello */ | |
| 474 | pchat=icq_ChatSessionNew(link); | |
| 475 | pchat->id=sequence; | |
| 476 | pchat->remote_uin=uin; | |
| 477 | ||
| 478 | /* create and send the ack packet */ | |
| 479 | p=icq_TCPCreateChatReqAck(pmessage, | |
| 480 | ntohs(plisten->socket_address.sin_port)); | |
| 481 | (void)icq_TCPLinkSendSeq(pmessage, p, sequence); | |
| 482 | ||
| 483 | #ifdef TCP_PACKET_TRACE | |
| 484 | printf("chat req ack sent to uin %lu { sequence=%lx }\n", uin, sequence); | |
| 485 | #endif | |
| 486 | } | |
| 487 | ||
| 488 | void icq_TCPSendChatData(ICQLINK *link, DWORD uin, const char *data) | |
| 489 | { | |
| 490 | icq_TCPLink *plink=icq_FindTCPLink(link, uin, TCP_LINK_CHAT); | |
| 491 | char data1[512]; | |
| 492 | int data1_len; | |
| 493 | ||
| 494 | if(!plink) | |
| 495 | return; | |
| 496 | ||
| 497 | strncpy(data1,data,512) ; | |
| 498 | data1[511] = '\0'; | |
| 499 | data1_len = strlen(data); | |
| 500 | icq_ChatRusConv_n("kw", data1, data1_len); | |
| 501 | ||
| 502 | send(plink->socket, data1, data1_len, 0); | |
| 503 | ||
| 504 | } | |
| 505 | ||
| 506 | void icq_TCPSendChatData_n(ICQLINK *link, DWORD uin, const char *data, int len) | |
| 507 | { | |
| 508 | icq_TCPLink *plink=icq_FindTCPLink(link, uin, TCP_LINK_CHAT); | |
| 509 | char *data1; | |
| 510 | ||
| 511 | if(!plink) | |
| 512 | return; | |
| 513 | ||
| 514 | data1 = (char *)malloc(len); | |
| 515 | memcpy(data1, data, len); | |
| 516 | icq_ChatRusConv_n("kw", data1, len); | |
| 517 | ||
| 518 | send(plink->socket, data1, len, 0); | |
| 519 | ||
| 520 | } | |
| 521 | ||
| 522 | void icq_TCPCloseChat(ICQLINK *link, unsigned long uin) | |
| 523 | { | |
| 524 | icq_TCPLink *plink=icq_FindTCPLink(link, uin, TCP_LINK_CHAT); | |
| 525 | ||
| 526 | if(plink) | |
| 527 | icq_TCPLinkClose(plink); | |
| 528 | ||
| 529 | } | |
| 530 | ||
| 531 | icq_FileSession *icq_AcceptFileRequest(ICQLINK *link, DWORD uin, | |
| 532 | unsigned long sequence) | |
| 533 | { | |
| 534 | icq_TCPLink *pmessage, *plisten; | |
| 535 | icq_FileSession *pfile; | |
| 536 | icq_Packet *p; | |
| 537 | ||
| 538 | pmessage=icq_TCPCheckLink(link, uin, TCP_LINK_MESSAGE); | |
| 539 | ||
| 540 | /* create the file listening socket if necessary */ | |
| 541 | if(!(plisten=icq_FindTCPLink(link, 0, TCP_LINK_FILE))) | |
| 542 | { | |
| 543 | plisten=icq_TCPLinkNew( link ); | |
| 544 | plisten->type=TCP_LINK_FILE; | |
| 545 | icq_TCPLinkListen(plisten); | |
| 546 | } | |
| 547 | ||
| 548 | /* create the file session, this will be linked to the incoming icq_TCPLink | |
| 549 | * in TCPProcessHello */ | |
| 550 | pfile=icq_FileSessionNew(link); | |
| 551 | pfile->id=sequence; | |
| 552 | pfile->remote_uin=uin; | |
| 553 | pfile->direction=FILE_STATUS_RECEIVING; | |
| 554 | icq_FileSessionSetStatus(pfile, FILE_STATUS_LISTENING); | |
| 555 | ||
| 556 | /* create and send the ack packet */ | |
| 557 | p=icq_TCPCreateFileReqAck(pmessage, | |
| 558 | ntohs(plisten->socket_address.sin_port)); | |
| 559 | (void)icq_TCPLinkSendSeq(pmessage, p, sequence); | |
| 560 | ||
| 561 | #ifdef TCP_PACKET_TRACE | |
| 562 | printf("file req ack sent to uin %lu { sequence=%lx }\n", uin, sequence); | |
| 563 | #endif | |
| 564 | ||
| 565 | return pfile; | |
| 566 | ||
| 567 | } | |
| 568 | ||
| 569 | void icq_RefuseFileRequest(ICQLINK *link, DWORD uin, | |
| 570 | unsigned long sequence, const char *reason) | |
| 571 | { | |
| 572 | icq_TCPLink *pmessage=icq_TCPCheckLink(link, uin, TCP_LINK_MESSAGE); | |
| 573 | icq_Packet *p; | |
| 574 | ||
| 575 | /* create and send the refuse packet */ | |
| 576 | p=icq_TCPCreateFileReqRefuse(pmessage, | |
| 577 | ntohs(pmessage->socket_address.sin_port), reason); | |
| 578 | (void)icq_TCPLinkSendSeq(pmessage, p, sequence); | |
| 579 | ||
| 580 | #ifdef TCP_PACKET_TRACE | |
| 581 | printf("file req refuse sent to uin %lu { sequence=%lx, reason=\"%s\" }\n", | |
| 582 | uin, sequence, reason); | |
| 583 | #endif | |
| 584 | ||
| 585 | } | |
| 586 | ||
| 587 | void icq_CancelFileRequest(ICQLINK *link, DWORD uin, unsigned long sequence) | |
| 588 | { | |
| 589 | icq_TCPLink *pmessage=icq_TCPCheckLink(link, uin, TCP_LINK_MESSAGE); | |
| 590 | icq_FileSession *psession=icq_FindFileSession(link, uin, sequence); | |
| 591 | icq_Packet *p; | |
| 592 | ||
| 593 | if (psession) | |
| 594 | icq_FileSessionClose(psession); | |
| 595 | ||
| 596 | /* create and send the cancel packet */ | |
| 597 | p=icq_TCPCreateFileReqCancel(pmessage, | |
| 598 | ntohs(pmessage->socket_address.sin_port)); | |
| 599 | (void)icq_TCPLinkSendSeq(pmessage, p, sequence); | |
| 600 | #ifdef TCP_PACKET_TRACE | |
| 601 | printf("file req cancel sent to uin %lu { sequence=%lx }\n", uin, sequence); | |
| 602 | #endif | |
| 603 | ||
| 604 | } | |
| 605 | ||
| 606 | void icq_RefuseChatRequest(ICQLINK *link, DWORD uin, | |
| 607 | unsigned long sequence, const char *reason) | |
| 608 | { | |
| 609 | icq_TCPLink *pmessage=icq_TCPCheckLink(link, uin, TCP_LINK_MESSAGE); | |
| 610 | icq_Packet *p; | |
| 611 | ||
| 612 | /* create and send the refuse packet */ | |
| 613 | p=icq_TCPCreateChatReqRefuse(pmessage, | |
| 614 | ntohs(pmessage->socket_address.sin_port), reason); | |
| 615 | (void)icq_TCPLinkSendSeq(pmessage, p, sequence); | |
| 616 | ||
| 617 | #ifdef TCP_PACKET_TRACE | |
| 618 | printf("chat req refuse sent to uin %lu { sequence=%lx, reason=\"%s\" }\n", | |
| 619 | uin, sequence, reason); | |
| 620 | #endif | |
| 621 | ||
| 622 | } | |
| 623 | ||
| 624 | void icq_CancelChatRequest(ICQLINK *link, DWORD uin, unsigned long sequence) | |
| 625 | { | |
| 626 | icq_TCPLink *pmessage=icq_TCPCheckLink(link, uin, TCP_LINK_MESSAGE); | |
| 627 | icq_FileSession *psession=icq_FindFileSession(link, uin, sequence); | |
| 628 | icq_Packet *p; | |
| 629 | ||
| 630 | if (psession) | |
| 631 | icq_FileSessionClose(psession); | |
| 632 | ||
| 633 | /* create and send the cancel packet */ | |
| 634 | p=icq_TCPCreateChatReqCancel(pmessage, | |
| 635 | ntohs(pmessage->socket_address.sin_port)); | |
| 636 | (void)icq_TCPLinkSendSeq(pmessage, p, sequence); | |
| 637 | ||
| 638 | #ifdef TCP_PACKET_TRACE | |
| 639 | printf("chat req cancel sent to uin %lu { sequence=%lx }\n", uin, sequence); | |
| 640 | #endif | |
| 641 | ||
| 642 | } |