]> andersk Git - libfaim.git/blobdiff - aim_conn.c
Forgot this.
[libfaim.git] / aim_conn.c
index 322effdf317dde70038a8004e7d00c86e322f19b..061409f74b78a672d39eda164e2f76b21683c18f 100644 (file)
@@ -6,32 +6,85 @@
  *
  */
 
-#include "aim.h"
+#include <faim/aim.h> 
 
-void aim_connrst(void)
+/*
+ * Clears out connection list, killing remaining connections.
+ */
+void aim_connrst(struct aim_session_t *sess)
 {
-  int i;
-  for (i = 0; i < AIM_CONN_MAX; i++)
-    {
-      aim_conns[i].fd = -1;
-      aim_conns[i].type = -1;
-      aim_conns[i].status = 0;
-      aim_conns[i].seqnum = 0;
-      aim_conns[i].lastactivity = 0;
-      aim_conns[i].forcedlatency = 0;
-      aim_clearhandlers(&aim_conns[i]);
-      aim_conns[i].handlerlist = NULL;
+  faim_mutex_init(&sess->connlistlock, NULL);
+  if (sess->connlist) {
+    struct aim_conn_t *cur = sess->connlist, *tmp;
+
+    while(cur) {
+      tmp = cur->next;
+      aim_conn_close(cur);
+      free(cur);
+      cur = tmp;
     }
+  }
+  sess->connlist = NULL;
+  return;
+}
 
+/*
+ * Gets a new connection structure.
+ */
+struct aim_conn_t *aim_conn_getnext(struct aim_session_t *sess)
+{
+  struct aim_conn_t *newconn, *cur;
+
+  if (!(newconn = malloc(sizeof(struct aim_conn_t))))  
+    return NULL;
+
+  memset(newconn, 0, sizeof(struct aim_conn_t));
+  aim_conn_close(newconn);
+  newconn->next = NULL;
+
+  faim_mutex_lock(&sess->connlistlock);
+  if (sess->connlist == NULL)
+    sess->connlist = newconn;
+  else {
+    for (cur = sess->connlist; cur->next; cur = cur->next)
+      ;
+    cur->next = newconn;
+  }
+  faim_mutex_unlock(&sess->connlistlock);
+
+  return newconn;
 }
 
-struct aim_conn_t *aim_conn_getnext(void)
+void aim_conn_kill(struct aim_session_t *sess, struct aim_conn_t **deadconn)
 {
-  int i;
-  for (i=0;i<AIM_CONN_MAX;i++)
-    if (aim_conns[i].fd == -1)
-      return &(aim_conns[i]);
-  return NULL;
+  struct aim_conn_t *cur;
+
+  if (!deadconn || !*deadconn) 
+    return;
+
+  faim_mutex_lock(&sess->connlistlock);
+  if (sess->connlist == NULL)
+    ;
+  else if (sess->connlist->next == NULL) {
+    if (sess->connlist == *deadconn)
+      sess->connlist = NULL;
+  } else {
+    cur = sess->connlist;
+    while (cur->next) {
+      if (cur->next == *deadconn) {
+       cur->next = cur->next->next;
+       break;
+      }
+      cur = cur->next;
+    }
+  }
+  faim_mutex_unlock(&sess->connlistlock);
+
+  aim_conn_close(*deadconn);
+  free(*deadconn);
+  deadconn = NULL;
+
+  return;
 }
 
 void aim_conn_close(struct aim_conn_t *deadconn)
@@ -43,17 +96,28 @@ void aim_conn_close(struct aim_conn_t *deadconn)
   deadconn->seqnum = 0;
   deadconn->lastactivity = 0;
   deadconn->forcedlatency = 0;
-  aim_clearhandlers(deadconn);
+  if (deadconn->handlerlist)
+    aim_clearhandlers(deadconn);
   deadconn->handlerlist = NULL;
+  if (deadconn->priv)
+    free(deadconn->priv);
+  deadconn->priv = NULL;
+  faim_mutex_init(&deadconn->active, NULL);
+  faim_mutex_init(&deadconn->seqnum_lock, NULL);
 }
 
-struct aim_conn_t *aim_getconn_type(int type)
+struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess,
+                                   int type)
 {
-  int i;
-  for (i=0; i<AIM_CONN_MAX; i++)
-    if (aim_conns[i].type == type)
-      return &(aim_conns[i]);
-  return NULL;
+  struct aim_conn_t *cur;
+
+  faim_mutex_lock(&sess->connlistlock);
+  for (cur = sess->connlist; cur; cur = cur->next) {
+    if (cur->type == type)
+      break;
+  }
+  faim_mutex_unlock(&sess->connlistlock);
+  return cur;
 }
 
 /*
@@ -65,20 +129,31 @@ struct aim_conn_t *aim_getconn_type(int type)
  * FIXME: Return errors in a more sane way.
  *
  */
-struct aim_conn_t *aim_newconn(int type, char *dest)
+struct aim_conn_t *aim_newconn(struct aim_session_t *sess,
+                              int type, char *dest)
 {
   struct aim_conn_t *connstruct;
   int ret;
   struct sockaddr_in sa;
   struct hostent *hp;
   u_short port = FAIM_LOGIN_PORT;
+  char *host = NULL;
   int i=0;
-  
-  if (!dest || ((connstruct=aim_conn_getnext())==NULL))
+
+  if ((connstruct=aim_conn_getnext(sess))==NULL)
     return NULL;
 
+  faim_mutex_lock(&connstruct->active);
+  
   connstruct->type = type;
 
+  if (!dest) { /* just allocate a struct */
+    connstruct->fd = -1;
+    connstruct->status = 0;
+    faim_mutex_unlock(&connstruct->active);
+    return connstruct;
+  }
+
   /* 
    * As of 23 Jul 1999, AOL now sends the port number, preceded by a 
    * colon, in the BOS redirect.  This fatally breaks all previous 
@@ -88,19 +163,24 @@ struct aim_conn_t *aim_newconn(int type, char *dest)
    *
    */
 
-  for(i=0;(i<strlen(dest));i++)
+  for(i=0;i<strlen(dest);i++) {
     if (dest[i] == ':') {
       port = atoi(&(dest[i+1]));
-      dest[i]=0;
       break;
     }
+  }
+  host = (char *)malloc(i+1);
+  strncpy(host, dest, i);
+  host[i] = '\0';
 
-  hp = gethostbyname2(dest, AF_INET);
-  if (hp == NULL)
-    {
-      connstruct->status = (h_errno | AIM_CONN_STATUS_RESOLVERR);
-      return connstruct;
-    }
+  hp = gethostbyname(host);
+  free(host);
+
+  if (hp == NULL) {
+    connstruct->status = (h_errno | AIM_CONN_STATUS_RESOLVERR);
+    faim_mutex_unlock(&connstruct->active);
+    return connstruct;
+  }
 
   memset(&sa.sin_zero, 0, 8);
   sa.sin_port = htons(port);
@@ -109,33 +189,43 @@ struct aim_conn_t *aim_newconn(int type, char *dest)
   
   connstruct->fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
   ret = connect(connstruct->fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in));
-  if( ret < 0)
-    {
-      connstruct->fd = -1;
-      connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
-      return connstruct;
-    }
+  if(ret < 0) {
+    connstruct->fd = -1;
+    connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
+    faim_mutex_unlock(&connstruct->active);
+    return connstruct;
+  }
+  
+  faim_mutex_unlock(&connstruct->active);
 
   return connstruct;
 }
 
-int aim_conngetmaxfd(void)
+int aim_conngetmaxfd(struct aim_session_t *sess)
 {
-  int i,j;
-  j=0;
-  for (i=0;i<AIM_CONN_MAX;i++)
-    if(aim_conns[i].fd > j)
-      j = aim_conns[i].fd;
+  int j = 0;
+  struct aim_conn_t *cur;
+
+  faim_mutex_lock(&sess->connlistlock);
+  for (cur = sess->connlist; cur; cur = cur->next) {
+    if (cur->fd > j)
+      j = cur->fd;
+  }
+  faim_mutex_unlock(&sess->connlistlock);
+
   return j;
 }
 
-int aim_countconn(void)
+int aim_countconn(struct aim_session_t *sess)
 {
-  int i,cnt;
-  cnt = 0;
-  for (i=0;i<AIM_CONN_MAX;i++)
-    if (aim_conns[i].fd > -1)
-      cnt++;
+  int cnt = 0;
+  struct aim_conn_t *cur;
+
+  faim_mutex_lock(&sess->connlistlock);
+  for (cur = sess->connlist; cur; cur = cur->next)
+    cnt++;
+  faim_mutex_unlock(&sess->connlistlock);
+
   return cnt;
 }
 
@@ -145,80 +235,116 @@ int aim_countconn(void)
  * Waits for a socket with data or for timeout, whichever comes first.
  * See select(2).
  * 
+ * Return codes in *status:
+ *   -1  error in select() (NULL returned)
+ *    0  no events pending (NULL returned)
+ *    1  outgoing data pending (NULL returned)
+ *    2  incoming data pending (connection with pending data returned)
+ *
+ * XXX: we could probably stand to do a little courser locking here.
+ *
  */ 
-struct aim_conn_t *aim_select(struct timeval *timeout)
+struct aim_conn_t *aim_select(struct aim_session_t *sess,
+                             struct timeval *timeout, int *status)
 {
+  struct aim_conn_t *cur;
   fd_set fds;
-  fd_set errfds;
+  int maxfd = 0;
   int i;
 
-  if (aim_countconn() <= 0)
+  faim_mutex_lock(&sess->connlistlock);
+  if (sess->connlist == NULL) {
+    faim_mutex_unlock(&sess->connlistlock);
     return 0;
+  }
+  faim_mutex_unlock(&sess->connlistlock);
 
   /* 
    * If we have data waiting to be sent, return immediatly
    */
-  if (aim_queue_outgoing)
-    return (struct aim_conn_t *)1;
+  if (sess->queue_outgoing != NULL) {
+    *status = 1;
+    return NULL;
+  } 
 
   FD_ZERO(&fds);
-  FD_ZERO(&errfds);
-  
-  for(i=0;i<AIM_CONN_MAX;i++)
-    if (aim_conns[i].fd>-1)
-      {
-       FD_SET(aim_conns[i].fd, &fds);
-       FD_SET(aim_conns[i].fd, &errfds);
+  maxfd = 0;
+
+  faim_mutex_lock(&sess->connlistlock);
+  for (cur = sess->connlist; cur; cur = cur->next) {
+    FD_SET(cur->fd, &fds);
+    if (cur->fd > maxfd)
+      maxfd = cur->fd;
+  }
+  faim_mutex_unlock(&sess->connlistlock);
+
+  if ((i = select(maxfd+1, &fds, NULL, NULL, timeout))>=1) {
+    faim_mutex_lock(&sess->connlistlock);
+    for (cur = sess->connlist; cur; cur = cur->next) {
+      if (FD_ISSET(cur->fd, &fds)) {
+       *status = 2;
+       faim_mutex_unlock(&sess->connlistlock);
+       return cur;
       }
-  
-  i = select(aim_conngetmaxfd()+1, &fds, NULL, &errfds, timeout);
-  if (i>=1)
-    {
-      int j;
-      for (j=0;j<AIM_CONN_MAX;j++)
-       {
-         if (aim_conns[j].fd > -1)
-           {
-             if ((FD_ISSET(aim_conns[j].fd, &errfds)))
-               {
-                 /* got an exception; close whats left of it up */
-                 aim_conn_close(&(aim_conns[j]));
-                 return (struct aim_conn_t *)-1;
-               }
-             else if ((FD_ISSET(aim_conns[j].fd, &fds)))
-               return &(aim_conns[j]);  /* return the first waiting struct */
-           }
-       }
-      /* should never get here */
     }
-  else
-    return (struct aim_conn_t *)i;  /* no waiting or error, return -- FIXME: return type funnies */
-  return NULL; /* NO REACH */
+  }
+
+  faim_mutex_unlock(&sess->connlistlock);
+  *status = i; /* may be 0 or -1 */
+  return NULL;  /* no waiting or error, return */
 }
 
 int aim_conn_isready(struct aim_conn_t *conn)
 {
   if (conn)
     return (conn->status & 0x0001);
-  else
-    return -1;
+  return -1;
 }
 
 int aim_conn_setstatus(struct aim_conn_t *conn, int status)
 {
-  if (conn)
-    return (conn->status ^= status);
-  else
+  int val;
+
+  if (!conn)
     return -1;
+  
+  faim_mutex_lock(&conn->active);
+  val = conn->status ^= status;
+  faim_mutex_unlock(&conn->active);
+  return val;
 }
 
 int aim_conn_setlatency(struct aim_conn_t *conn, int newval)
 {
   if (!conn)
     return -1;
-  
+
+  faim_mutex_lock(&conn->active);
   conn->forcedlatency = newval;
   conn->lastactivity = 0; /* reset this just to make sure */
+  faim_mutex_unlock(&conn->active);
 
   return 0;
 }
+
+void aim_session_init(struct aim_session_t *sess)
+{
+  if (!sess)
+    return;
+
+  memset(sess, 0, sizeof(struct aim_session_t));
+  aim_connrst(sess);
+  sess->queue_outgoing = NULL;
+  sess->queue_incoming = NULL;
+  sess->pendingjoin = NULL;
+  sess->outstanding_snacs = NULL;
+  sess->snac_nextid = 0x00000001;
+
+  /*
+   * This must always be set.  Default to the queue-based
+   * version for back-compatibility.  
+   */
+  sess->tx_enqueue = &aim_tx_enqueue__queuebased;
+
+  return;
+}
This page took 0.108324 seconds and 4 git commands to generate.