plugins/icq/tcplink.c

changeset 1912
77d18e17199b
parent 1531
a275218f3f4c
child 1977
a6bfa19d6884
--- a/plugins/icq/tcplink.c	Tue May 29 09:46:05 2001 +0000
+++ b/plugins/icq/tcplink.c	Tue May 29 10:32:53 2001 +0000
@@ -1,179 +1,29 @@
 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/*
-$Id: tcplink.c 1541 2001-03-04 02:26:32Z warmenhoven $
-$Log$
-Revision 1.5  2001/03/04 02:26:32  warmenhoven
-updates to icqlib; don't pay attention to exception; my patch for handling hangups. other minor details.
-
-Revision 1.45  2001/03/03 20:13:06  bills
-add compile fix for BeOS
-
-Revision 1.44  2001/02/22 05:40:04  bills
-port tcp connect timeout code and UDP queue to new timeout manager
-
-Revision 1.43  2001/01/27 22:48:01  bills
-fix bugs related to TCP and new socket manager: implemented accepting TCP
-sockets, fixed crashes when sending TCP messages.
-
-Revision 1.42  2001/01/17 01:29:17  bills
-Rework chat and file session interfaces; implement socket notifications.
-
-Revision 1.41  2001/01/15 06:19:12  denis
-Applied patch from Ilya Melamed <ilya@ort.org.il> which fixes random
-icq_TCPLinkAccept() fails.
-
-Revision 1.40  2000/12/19 06:00:07  bills
-moved members from ICQLINK to ICQLINK_private struct
-
-Revision 1.39  2000/12/03 21:57:15  bills
-fixed bug #105068
-
-Revision 1.38  2000/07/09 22:19:35  bills
-added new *Close functions, use *Close functions instead of *Delete
-where correct, and misc cleanup
-
-Revision 1.37  2000/06/15 01:53:17  bills
-added icq_TCPLinkSendSeq function
-
-Revision 1.36  2000/05/04 15:57:20  bills
-Reworked file transfer notification, small bugfixes, and cleanups.
-
-Revision 1.35  2000/05/03 18:29:15  denis
-Callbacks have been moved to the ICQLINK structure.
-
-Revision 1.34  2000/04/10 18:11:45  denis
-ANSI cleanups.
-
-Revision 1.33  2000/04/10 16:36:04  denis
-Some more Win32 compatibility from Guillaume Rosanis <grs@mail.com>
-
-Revision 1.32  2000/04/05 14:37:02  denis
-Applied patch from "Guillaume R." <grs@mail.com> for basic Win32
-compatibility.
-
-Revision 1.31  2000/02/15 04:00:16  bills
- new icq_ChatRusConv_n function
-
-Revision 1.30  2000/02/07 02:46:29  bills
-implemented non-blocking TCP connects over SOCKS, cyrillic translation for chat
-
-Revision 1.29  2000/01/20 19:59:35  bills
-fixed bug in icq_TCPLinkConnect that caused file/chat connection attempts
-to go to the wrong port
-
-Revision 1.28  2000/01/16 03:59:10  bills
-reworked list code so list_nodes don't need to be inside item structures,
-removed strlist code and replaced with generic list calls
-
-Revision 1.27  1999/12/27 16:13:29  bills
-fixed bug in icq_TCPOnDataReceived, removed flags variable ;)
-
-Revision 1.26  1999/12/27 10:56:10  denis
-Unused "flags" variable commented out.
-
-Revision 1.25  1999/12/21 00:30:53  bills
-added icq_TCPLinkProcessReceived to support processing receive queue
-before delete (packets used to get dropped in this instance, oops),
-reworked icq_TCPLinkOnDataReceived to handle quick, large streams of data,
-changed icq_TCPLinkOnConnect and *Accept to make all sockets non-blocking.
-
-Revision 1.24  1999/12/14 03:33:34  bills
-icq_TCPLinkConnect now uses real_ip when our IP is same as remote IP to make
-connection, added code to implement connect timeout
 
-Revision 1.23  1999/11/30 09:57:44  bills
-buffer overflow check added, tcplinks will now close if buffer overflows.
-increased icq_TCPLinkBufferSize to 4096 to support file transfer packets
-
-Revision 1.22  1999/11/29 17:15:51  denis
-Absence of socklen_t type fixed.
-
-Revision 1.21  1999/10/01 00:49:21  lord
-some compilation problems are fixed.
-
-Revision 1.20  1999/09/29 20:26:41  bills
-ack forgot the args :)
-
-Revision 1.19  1999/09/29 20:21:45  bills
-renamed denis' new function
-
-Revision 1.18  1999/09/29 20:11:29  bills
-renamed tcp_link* to icq_TCPLink*.  many cleanups, added icq_TCPLinkOnConnect
-
-Revision 1.17  1999/09/29 17:10:05  denis
-TCP code SOCKS-ification. Not finished.
-
-Revision 1.16  1999/07/18 20:21:34  bills
-fixed fail notification bug introduced during ICQLINK changes, changed to
-use new byte-order functions & contact list functions, added better log
-messages
-
-Revision 1.15  1999/07/16 15:45:57  denis
-Cleaned up.
-
-Revision 1.14  1999/07/16 12:02:58  denis
-tcp_packet* functions renamed to icq_Packet*
-Cleaned up.
-
-Revision 1.13  1999/07/12 15:13:36  cproch
-- added definition of ICQLINK to hold session-specific global variabled
-  applications which have more than one connection are now possible
-- changed nearly every function defintion to support ICQLINK parameter
-
-Revision 1.12  1999/07/03 06:33:51  lord
-. byte order conversion macros added
-. some compilation warnings removed
-
-Revision 1.11  1999/06/30 13:52:23  bills
-implemented non-blocking connects
-
-Revision 1.10  1999/05/03 21:39:41  bills
-removed exit calls
-
-Revision 1.9  1999/04/29 09:35:54  denis
-Cleanups, warning removed
-
-Revision 1.8  1999/04/17 19:34:49  bills
-fixed bug in icq_TCPLinkOnDataReceived, multiple packets that come in on
-one recv call are now handled correctly.  added icq_TCPLinkAccept and
-icq_TCPLinkListen.  started using mode and type structure entries.  added
-icq_TCPLinks list and icq_FindTCPLink function.  changed received_queue and
-send_queue to lists.
-
-Revision 1.7  1999/04/14 15:02:45  denis
-Cleanups for "strict" compiling (-ansi -pedantic)
-
-Revision 1.6  1999/04/05 18:47:17  bills
-initial chat support implemented
-
-Revision 1.5  1999/03/31 01:50:54  bills
-wrapped up many tcp details- tcp code now handles incoming and outgoing
-tcp messages and urls!
-
-Revision 1.4  1999/03/28 03:27:49  bills
-fixed icq_TCPLinkConnect so it really connects to remote ip instead of
-always my local test computer O:)
-
-Revision 1.3  1999/03/26 20:02:41  bills
-fixed C++ comments, cleaned up
-
-Revision 1.2  1999/03/25 22:21:59  bills
-added necessary includes
-
-Revision 1.1  1999/03/25 21:09:07  bills
-tcp link functions
-*/
+/*
+ * Copyright (C) 1998-2001, Denis V. Dmitrienko <denis@null.net> and
+ *                          Bill Soudan <soudan@kde.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
 
 #include <stdlib.h>
 
-#ifndef _WIN32
-#include <unistd.h>
-#endif
-
 #include <fcntl.h>
-#include <stdarg.h>
 #include <errno.h>
-#include <sys/types.h>
 
 #ifdef _WIN32
 #include <winsock.h>
@@ -185,33 +35,29 @@
 #define EAFNOSUPPORT  WSAEAFNOSUPPORT
 #define EWOULDBLOCK   WSAEWOULDBLOCK
 #else
-#include <sys/socket.h>
 #include <netdb.h>
 #endif
 
-#include "icqtypes.h"
-#include "icq.h"
 #include "icqlib.h"
-#include "tcplink.h"
 #include "stdpackets.h"
-#include "util.h"
 #include "tcp.h"
 #include "errno.h"
 #include "chatsession.h"
 #include "filesession.h"
+#include "contacts.h"
 
-icq_TCPLink *icq_TCPLinkNew(ICQLINK *link)
+icq_TCPLink *icq_TCPLinkNew(icq_Link *icqlink)
 {
   icq_TCPLink *p=(icq_TCPLink *)malloc(sizeof(icq_TCPLink));
 
   p->socket=-1;
-  p->icqlink=link;
+  p->icqlink=icqlink;
   p->mode=0;
   p->session=0L;
   p->type=TCP_LINK_MESSAGE;
   p->buffer_count=0;
-  p->send_queue=list_new();
-  p->received_queue=list_new();
+  p->send_queue=icq_ListNew();
+  p->received_queue=icq_ListNew();
   p->id=0;
   p->remote_uin=0;
   p->remote_version=0;
@@ -220,7 +66,7 @@
   p->connect_timeout = NULL;
 
   if(p)
-    list_enqueue(link->d->icq_TCPLinks, p);
+    icq_ListEnqueue(icqlink->d->icq_TCPLinks, p);
 
   return p;
 }
@@ -228,11 +74,12 @@
 int _icq_TCPLinkDelete(void *pv, va_list data)
 {
   icq_Packet *p=(icq_Packet *)pv;
-  ICQLINK *icqlink=va_arg(data, ICQLINK *);
+  icq_Link *icqlink=va_arg(data, icq_Link *);
 
   /* notify the app the packet didn't make it */
   if(p->id)
-    (*icqlink->icq_RequestNotify)(icqlink, p->id, ICQ_NOTIFY_FAILED, 0, 0);
+    invoke_callback(icqlink, icq_RequestNotify)(icqlink, p->id,
+      ICQ_NOTIFY_FAILED, 0, 0);
 
   return 0;
 }
@@ -245,11 +92,11 @@
   icq_TCPLinkProcessReceived(p);
 
   /* make sure we notify app that packets in send queue didn't make it */
-  (void)list_traverse(p->send_queue, _icq_TCPLinkDelete, p->icqlink);
+  (void)icq_ListTraverse(p->send_queue, _icq_TCPLinkDelete, p->icqlink);
 
   /* destruct all packets still waiting on queues */
-  list_delete(p->send_queue, icq_PacketDelete);
-  list_delete(p->received_queue, icq_PacketDelete);
+  icq_ListDelete(p->send_queue, icq_PacketDelete);
+  icq_ListDelete(p->received_queue, icq_PacketDelete);
 
   /* if this is a chat or file link, delete the associated session as
    * well, but make sure we unassociate ourself first so the session
@@ -286,7 +133,7 @@
 
 void icq_TCPLinkClose(icq_TCPLink *plink)
 {
-  list_remove(plink->icqlink->d->icq_TCPLinks, plink);
+  icq_ListRemove(plink->icqlink->d->icq_TCPLinks, plink);
   icq_TCPLinkDelete(plink);
 }
 
@@ -618,14 +465,14 @@
 #else
   int flags;
 #endif
-  int socket;
+  int socket_fd;
   size_t remote_length;
-  icq_TCPLink *pnewlink=icq_TCPLinkNew( plink->icqlink );
+  icq_TCPLink *pnewlink=icq_TCPLinkNew(plink->icqlink);
   
   if(pnewlink)
   {
     remote_length = sizeof(struct sockaddr_in);
-    socket=icq_SocketAccept(plink->socket,
+    socket_fd=icq_SocketAccept(plink->socket,
       (struct sockaddr *)&(plink->remote_address), &remote_length);
 
     icq_FmtLog(plink->icqlink, ICQ_LOG_MESSAGE,
@@ -636,14 +483,14 @@
     /* FIXME: make sure accept succeeded */
 
     pnewlink->type=plink->type;
-    pnewlink->socket=socket;
+    pnewlink->socket=socket_fd;
 
     /* first packet sent on an icq tcp link is always the hello packet */
     pnewlink->mode|=TCP_LINK_MODE_HELLOWAIT;
 
     /* install socket handler for new socket */
-    icq_SocketSetHandler(socket, ICQ_SOCKET_READ, icq_TCPLinkOnDataReceived,
-      pnewlink);
+    icq_SocketSetHandler(socket_fd, ICQ_SOCKET_READ, 
+      icq_TCPLinkOnDataReceived, pnewlink);
   }
 
   /* set the socket to non-blocking */
@@ -842,8 +689,8 @@
   icq_PacketDump(p);
 #endif
 
-  /* Stick packet on ready packet linked list */
-  list_enqueue(plink->received_queue, p);
+  /* Stick packet on ready packet linked icq_List */
+  icq_ListEnqueue(plink->received_queue, p);
 }
 
 void icq_TCPLinkOnConnect(icq_TCPLink *plink)
@@ -928,7 +775,7 @@
    * has been established and send pending data */
   while(plink->send_queue->count>0)
   {
-    icq_Packet *p=list_dequeue(plink->send_queue);
+    icq_Packet *p=icq_ListDequeue(plink->send_queue);
     if(p->id)
       if(plink->icqlink->icq_RequestNotify)
         (*plink->icqlink->icq_RequestNotify)(plink->icqlink, p->id, ICQ_NOTIFY_CONNECTED, 0, 0);
@@ -965,7 +812,7 @@
   /* if the link is currently connecting, queue the packets for
    * later, else send immediately */
   if(plink->mode & TCP_LINK_MODE_CONNECTING) {
-    list_insert(plink->send_queue, 0, p);
+    icq_ListInsert(plink->send_queue, 0, p);
     if(plink->icqlink->icq_RequestNotify)
       (*plink->icqlink->icq_RequestNotify)(plink->icqlink, p->id, ICQ_NOTIFY_CONNECTING, 0, 0);
   } else {
@@ -983,7 +830,7 @@
   /* if the link is currently connecting, queue the packets for
    * later, else send immediately */
   if(plink->mode & TCP_LINK_MODE_CONNECTING) {
-    list_insert(plink->send_queue, 0, p);
+    icq_ListInsert(plink->send_queue, 0, p);
     if(plink->icqlink->icq_RequestNotify)
       (*plink->icqlink->icq_RequestNotify)(plink->icqlink, p->id, ICQ_NOTIFY_CONNECTING, 0, 0);
   } else {
@@ -997,11 +844,11 @@
 
 void icq_TCPLinkProcessReceived(icq_TCPLink *plink)
 {
-  list *plist=plink->received_queue;
+  icq_List *plist=plink->received_queue;
   while(plist->count>0)
 
   {
-    icq_Packet *p=list_dequeue(plist);
+    icq_Packet *p=icq_ListDequeue(plist);
 
     if(plink->mode & TCP_LINK_MODE_HELLOWAIT)
     {
@@ -1041,7 +888,7 @@
   return ( (plink->remote_uin == uin ) && (plink->type == type) );
 }
 
-icq_TCPLink *icq_FindTCPLink(ICQLINK *link, unsigned long uin, int type)
+icq_TCPLink *icq_FindTCPLink(icq_Link *icqlink, unsigned long uin, int type)
 {
-  return list_traverse(link->d->icq_TCPLinks, _icq_FindTCPLink, uin, type);
+  return icq_ListTraverse(icqlink->d->icq_TCPLinks, _icq_FindTCPLink, uin, type);
 }

mercurial