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