]> code.citadel.org Git - citadel.git/blobdiff - citadel/ipc_c_tcp.c
* Allow multiple simultaneous IPC connections. All changes necessary for
[citadel.git] / citadel / ipc_c_tcp.c
index 1680bcbf51eca57d4703fe30672aaa1e27e09a20..dd0994e4ce3d5bd2cf56bead806f5aa991b9263b 100644 (file)
@@ -12,6 +12,8 @@
 
 
 #include "sysdep.h"
+#undef NDEBUG
+#include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -27,8 +29,8 @@
 #include <errno.h>
 #include <stdarg.h>
 #include "citadel.h"
-#include "citadel_decls.h"
 #include "ipc.h"
+#include "citadel_decls.h"
 #include "tools.h"
 #if defined(HAVE_OPENSSL)
 #include "client_crypto.h"
@@ -38,9 +40,9 @@
 #endif
 
 /*
- * If server_is_local is set to nonzero, the client assumes that it is running
- * on the same computer as the server.  Several things happen when this is
- * the case, including the ability to map a specific tty to a particular login
+ * If ipc->isLocal is set to nonzero, the client assumes that it is running on
+ * the same computer as the server.  Several things happen when this is the
+ * case, including the ability to map a specific tty to a particular login
  * session in the "<W>ho is online" listing, the ability to run external
  * programs, and the ability to download files directly off the disk without
  * having to first fetch them from the server.
  * network session to the local machine) or 0 if the server is executing on
  * a remote computer.
  */
-int server_is_local = 0;
 
 #ifndef INADDR_NONE
 #define INADDR_NONE 0xffffffff
 #endif
 
-int serv_sock;
+/*
+ * FIXME: rewrite all of Ford's stuff here, it won't work with multiple
+ * instances
+ */
 
 static void (*deathHook)(void) = NULL;
 int (*error_printf)(char *s, ...) = (int (*)(char *, ...))printf;
@@ -67,7 +71,7 @@ void setIPCErrorPrintf(int (*func)(char *s, ...)) {
        error_printf = func;
 }
 
-void connection_died(void) {
+void connection_died(CtdlIPC *ipc) {
        if (deathHook != NULL)
                deathHook();
 
@@ -75,15 +79,23 @@ void connection_died(void) {
                        "Last error: %s\n"
                        "Please re-connect and log in again.\n",
                        strerror(errno));
-       logoff(3);
+#ifdef HAVE_OPENSSL
+       SSL_shutdown(ipc->ssl);
+       SSL_free(ipc->ssl);
+       ipc->ssl = NULL;
+#endif
+       shutdown(ipc->sock, 2);
+       ipc->sock = -1;
 }
 
 
+/*
 static void ipc_timeout(int signum)
 {
        error_printf("\rConnection timed out.\n");
-       logoff(3);
+       logoff(NULL, 3);
 }
+*/
 
 
 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
@@ -113,12 +125,12 @@ static int connectsock(char *host, char *service, char *protocol, int defaultPor
        } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
                error_printf("Can't get %s host entry: %s\n",
                        host, strerror(errno));
-               logoff(3);
+               return -1;
        }
        if ((ppe = getprotobyname(protocol)) == 0) {
                error_printf("Can't get %s protocol entry: %s\n",
                        protocol, strerror(errno));
-               logoff(3);
+               return -1;
        }
        if (!strcmp(protocol, "udp")) {
                type = SOCK_DGRAM;
@@ -129,23 +141,27 @@ static int connectsock(char *host, char *service, char *protocol, int defaultPor
        s = socket(PF_INET, type, ppe->p_proto);
        if (s < 0) {
                error_printf("Can't create socket: %s\n", strerror(errno));
-               logoff(3);
+               return -1;
        }
+       /*
        signal(SIGALRM, ipc_timeout);
        alarm(30);
+       */
 
        if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-               error_printf("can't connect to %s:%s: %s\n",
+               error_printf("Can't connect to %s:%s: %s\n",
                        host, service, strerror(errno));
-               logoff(3);
+               return -1;
        }
+       /*
        alarm(0);
        signal(SIGALRM, SIG_IGN);
+       */
 
        return (s);
 }
 
-int uds_connectsock(char *sockpath)
+static int uds_connectsock(int *isLocal, char *sockpath)
 {
        struct sockaddr_un addr;
        int s;
@@ -157,15 +173,15 @@ int uds_connectsock(char *sockpath)
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
                error_printf("Can't create socket: %s\n", strerror(errno));
-               logoff(3);
+               return -1;
        }
 
        if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
                error_printf("can't connect: %s\n", strerror(errno));
-               logoff(3);
+               return -1;
        }
 
-       server_is_local = 1;
+       *isLocal = 1;
        return s;
 }
 
@@ -173,21 +189,21 @@ int uds_connectsock(char *sockpath)
 /*
  * input binary data from socket
  */
-void serv_read(char *buf, int bytes)
+void serv_read(CtdlIPC *ipc, char *buf, int bytes)
 {
        int len, rlen;
 
 #if defined(HAVE_OPENSSL)
-       if (ssl_is_connected) {
-               serv_read_ssl(buf, bytes);
+       if (ipc->ssl) {
+               serv_read_ssl(ipc, buf, bytes);
                return;
        }
 #endif
        len = 0;
        while (len < bytes) {
-               rlen = read(serv_sock, &buf[len], bytes - len);
+               rlen = read(ipc->sock, &buf[len], bytes - len);
                if (rlen < 1) {
-                       connection_died();
+                       connection_died(ipc);
                        return;
                }
                len += rlen;
@@ -198,22 +214,22 @@ void serv_read(char *buf, int bytes)
 /*
  * send binary to server
  */
-void serv_write(char *buf, int nbytes)
+void serv_write(CtdlIPC *ipc, const char *buf, int nbytes)
 {
        int bytes_written = 0;
        int retval;
 
 #if defined(HAVE_OPENSSL)
-       if (ssl_is_connected) {
-               serv_write_ssl(buf, nbytes);
+       if (ipc->ssl) {
+               serv_write_ssl(ipc, buf, nbytes);
                return;
        }
 #endif
        while (bytes_written < nbytes) {
-               retval = write(serv_sock, &buf[bytes_written],
+               retval = write(ipc->sock, &buf[bytes_written],
                               nbytes - bytes_written);
                if (retval < 1) {
-                       connection_died();
+                       connection_died(ipc);
                        return;
                }
                bytes_written += retval;
@@ -225,23 +241,21 @@ void serv_write(char *buf, int nbytes)
 /*
  * input string from socket - implemented in terms of serv_read()
  */
-void serv_gets(char *buf)
+void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
 {
        int i;
 
-       /* Read one character at a time.
-        */
+       /* Read one character at a time. */
        for (i = 0;; i++) {
-               serv_read(&buf[i], 1);
+               serv_read(ipc, &buf[i], 1);
                if (buf[i] == '\n' || i == (SIZ-1))
                        break;
        }
 
-       /* If we got a long line, discard characters until the newline.
-        */
+       /* If we got a long line, discard characters until the newline. */
        if (i == (SIZ-1))
                while (buf[i] != '\n')
-                       serv_read(&buf[i], 1);
+                       serv_read(ipc, &buf[i], 1);
 
        /* Strip the trailing newline.
         */
@@ -252,24 +266,43 @@ void serv_gets(char *buf)
 /*
  * send line to server - implemented in terms of serv_write()
  */
-void serv_puts(char *buf)
+void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
 {
        /* error_printf("< %s\n", buf); */
-       serv_write(buf, strlen(buf));
-       serv_write("\n", 1);
+       int watch_ssl = 0;
+       if (ipc->ssl) watch_ssl = 1;
+       assert(!watch_ssl || ipc->ssl);
+       serv_write(ipc, buf, strlen(buf));
+       assert(!watch_ssl || ipc->ssl);
+       serv_write(ipc, "\n", 1);
+       assert(!watch_ssl || ipc->ssl);
 }
 
 
 /*
  * attach to server
  */
-void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
+CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
 {
        int a;
        char cithost[SIZ];
        char citport[SIZ];
        char sockpath[SIZ];
 
+       CtdlIPC *ipc = ialloc(CtdlIPC);
+       if (!ipc) {
+               error_printf("Out of memory creating CtdlIPC!\n");
+               return 0;
+       }
+#if defined(HAVE_OPENSSL)
+       ipc->ssl = NULL;
+#endif
+#if defined(HAVE_PTHREAD_H)
+       pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
+#endif
+       ipc->sock = -1;                 /* Not connected */
+       ipc->isLocal = 0;               /* Not local, of course! */
+
        strcpy(cithost, DEFAULT_HOST);  /* default host */
        strcpy(citport, DEFAULT_PORT);  /* default port */
 
@@ -280,52 +313,66 @@ void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
                        strcpy(cithost, argv[a]);
                } else if (a == 2) {
                        strcpy(citport, argv[a]);
-               }
-               else {
+               } else {
                        error_printf("%s: usage: ",argv[0]);
                        error_printf("%s [host] [port] ",argv[0]);
-                       logoff(2);
+                       ifree(ipc);
+                       return 0;
                }
        }
 
        if ((!strcmp(cithost, "localhost"))
           || (!strcmp(cithost, "127.0.0.1"))) {
-               server_is_local = 1;
+               ipc->isLocal = 1;
        }
 
        /* If we're using a unix domain socket we can do a bunch of stuff */
        if (!strcmp(cithost, UDS)) {
                snprintf(sockpath, sizeof sockpath, "citadel.socket");
-               serv_sock = uds_connectsock(sockpath);
+               ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
+               if (ipc->sock == -1) {
+                       ifree(ipc);
+                       return 0;
+               }
                if (hostbuf != NULL) strcpy(hostbuf, cithost);
                if (portbuf != NULL) strcpy(portbuf, sockpath);
-               return;
+               return ipc;
        }
 
-       serv_sock = connectsock(cithost, citport, "tcp", 504);
+       ipc->sock = connectsock(cithost, citport, "tcp", 504);
+       if (ipc->sock == -1) {
+               ifree(ipc);
+               return 0;
+       }
        if (hostbuf != NULL) strcpy(hostbuf, cithost);
        if (portbuf != NULL) strcpy(portbuf, citport);
-       return;
+       return ipc;
 }
 
 /*
  * return the file descriptor of the server socket so we can select() on it.
+ *
+ * FIXME: This is only used in chat mode; eliminate it when chat mode gets
+ * rewritten...
  */
-int getsockfd(void)
+int CtdlIPC_getsockfd(CtdlIPC* ipc)
 {
-       return serv_sock;
+       return ipc->sock;
 }
 
 
 /*
  * return one character
+ *
+ * FIXME: This is only used in chat mode; eliminate it when chat mode gets
+ * rewritten...
  */
-char serv_getc(void)
+char CtdlIPC_get(CtdlIPC* ipc)
 {
        char buf[2];
        char ch;
 
-       serv_read(buf, 1);
+       serv_read(ipc, buf, 1);
        ch = (int) buf[0];
 
        return (ch);