]> code.citadel.org Git - citadel.git/blobdiff - citadel/ipc_c_tcp.c
* support autoconf 2.53
[citadel.git] / citadel / ipc_c_tcp.c
index a5a086daaa0a35835a632b3f3c8204517a013aff..c47c7f4df840cfc3ba3d9cdca8677e6d2da483e7 100644 (file)
@@ -30,7 +30,7 @@
 #include "citadel_decls.h"
 #include "ipc.h"
 #include "tools.h"
-#if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
+#if defined(HAVE_OPENSSL)
 #include "client_crypto.h"
 #endif
 #ifndef HAVE_SNPRINTF
@@ -56,22 +56,32 @@ int server_is_local = 0;
 
 int serv_sock;
 
-#if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
-extern int ssl_is_connected;
-#endif
+static void (*deathHook)(void) = NULL;
+int (*error_printf)(char *s, ...) = (int (*)(char *, ...))printf;
+
+void setIPCDeathHook(void (*hook)(void)) {
+       deathHook = hook;
+}
 
+void setIPCErrorPrintf(int (*func)(char *s, ...)) {
+       error_printf = func;
+}
 
 void connection_died(void) {
-       fprintf(stderr, "\r"
-                       "Your connection to this Citadel server is broken.\n"
-                       "Please re-connect and log in again.\n");
+       if (deathHook != NULL)
+               deathHook();
+
+       error_printf("\rYour connection to this Citadel server is broken.\n"
+                       "Last error: %s\n"
+                       "Please re-connect and log in again.\n",
+                       strerror(errno));
        logoff(3);
 }
 
 
-void timeout(int signum)
+static void ipc_timeout(int signum)
 {
-       printf("\rConnection timed out.\n");
+       error_printf("\rConnection timed out.\n");
        logoff(3);
 }
 
@@ -101,12 +111,12 @@ static int connectsock(char *host, char *service, char *protocol, int defaultPor
        if (phe) {
                memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
        } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
-               fprintf(stderr, "Can't get %s host entry: %s\n",
+               error_printf("Can't get %s host entry: %s\n",
                        host, strerror(errno));
                logoff(3);
        }
        if ((ppe = getprotobyname(protocol)) == 0) {
-               fprintf(stderr, "Can't get %s protocol entry: %s\n",
+               error_printf("Can't get %s protocol entry: %s\n",
                        protocol, strerror(errno));
                logoff(3);
        }
@@ -118,14 +128,14 @@ static int connectsock(char *host, char *service, char *protocol, int defaultPor
 
        s = socket(PF_INET, type, ppe->p_proto);
        if (s < 0) {
-               fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
+               error_printf("Can't create socket: %s\n", strerror(errno));
                logoff(3);
        }
-       signal(SIGALRM, timeout);
+       signal(SIGALRM, ipc_timeout);
        alarm(30);
 
        if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-               fprintf(stderr, "can't connect to %s.%s: %s\n",
+               error_printf("can't connect to %s:%s: %s\n",
                        host, service, strerror(errno));
                logoff(3);
        }
@@ -146,14 +156,12 @@ int uds_connectsock(char *sockpath)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               fprintf(stderr, "Can't create socket: %s\n",
-                       strerror(errno));
+               error_printf("Can't create socket: %s\n", strerror(errno));
                logoff(3);
        }
 
        if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-               fprintf(stderr, "can't connect: %s\n",
-                       strerror(errno));
+               error_printf("can't connect: %s\n", strerror(errno));
                logoff(3);
        }
 
@@ -246,7 +254,7 @@ void serv_gets(char *buf)
  */
 void serv_puts(char *buf)
 {
-       /* printf("< %s\n", buf); */
+       /* error_printf("< %s\n", buf); */
        serv_write(buf, strlen(buf));
        serv_write("\n", 1);
 }
@@ -274,8 +282,8 @@ void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
                        strcpy(citport, argv[a]);
                }
                else {
-                       fprintf(stderr,"%s: usage: ",argv[0]);
-                       fprintf(stderr,"%s [host] [port] ",argv[0]);
+                       error_printf("%s: usage: ",argv[0]);
+                       error_printf("%s [host] [port] ",argv[0]);
                        logoff(2);
                }
        }
@@ -287,7 +295,7 @@ void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
 
        /* If we're using a unix domain socket we can do a bunch of stuff */
        if (!strcmp(cithost, UDS)) {
-               sprintf(sockpath, "citadel.socket");
+               snprintf(sockpath, sizeof sockpath, "citadel.socket");
                serv_sock = uds_connectsock(sockpath);
                if (hostbuf != NULL) strcpy(hostbuf, cithost);
                if (portbuf != NULL) strcpy(portbuf, sockpath);