811aa876b6de1bb2ed7193bd2f5f6cecdda7adf3
[citadel.git] / citadel / utillib / ipc_c_tcp.c
1 /*
2  * $Id: ipc_c_tcp.c -1   $
3  * 
4  * Client-side IPC functions
5  *
6  */
7
8
9 #include "sysdep.h"
10 #undef NDEBUG
11 #include <assert.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <signal.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <sys/un.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <libcitadel.h>
27 #include "citadel.h"
28 #include "citadel_ipc.h"
29 #include "citadel_decls.h"
30 #ifndef HAVE_SNPRINTF
31 #include "snprintf.h"
32 #endif
33 #include "commands.h"
34
35 /* Note that some of these functions may not work with multiple instances. */
36
37 static void (*deathHook)(void) = NULL;
38 int (*error_printf)(char *s, ...) = (int (*)(char *, ...))printf;
39
40 void setIPCDeathHook(void (*hook)(void)) {
41         deathHook = hook;
42 }
43
44 void setIPCErrorPrintf(int (*func)(char *s, ...)) {
45         error_printf = func;
46 }
47
48 void connection_died(CtdlIPC* ipc, int using_ssl) {
49         if (deathHook != NULL) {
50                 deathHook();
51         }
52
53         stty_ctdl(SB_RESTORE);
54         fprintf(stderr, "\r\n\n\n");
55         fprintf(stderr, "Your connection to %s is broken.\n", ipc->ServInfo.humannode);
56
57 #ifdef HAVE_OPENSSL
58         if (using_ssl) {
59                 fprintf(stderr, "Last error: %s\n", ERR_reason_error_string(ERR_get_error()));
60                 SSL_free(ipc->ssl);
61                 ipc->ssl = NULL;
62         } else
63 #endif
64                 fprintf(stderr, "Last error: %s\n", strerror(errno));
65
66         fprintf(stderr, "Please re-connect and log in again.\n");
67         fflush(stderr);
68         fflush(stdout);
69         shutdown(ipc->sock, 2);
70         ipc->sock = -1;
71         exit(1);
72 }