573f872d3c98400a46b1ebc94f601a8e968fc569
[citadel.git] / textclient / ipc_c_tcp.c
1 // Client-side IPC functions
2 //
3 // Copyright (c) 1987-2018 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, and/or
6 // disclosure are subject to the GNU General Purpose License version 3.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12
13
14 #include "textclient.h"
15
16
17 /* Note that some of these functions may not work with multiple instances. */
18
19 static void (*deathHook)(void) = NULL;
20 int (*error_printf)(char *s, ...) = (int (*)(char *, ...)) printf;
21
22 void setIPCDeathHook(void (*hook)(void)) {
23         deathHook = hook;
24 }
25
26 void setIPCErrorPrintf(int (*func)(char *s, ...)) {
27         error_printf = func;
28 }
29
30 void connection_died(CtdlIPC * ipc, int using_ssl) {
31         if (deathHook != NULL) {
32                 deathHook();
33         }
34
35         stty_ctdl(SB_RESTORE);
36         fprintf(stderr, "\r\n\n\n");
37         fprintf(stderr, "Your connection to %s is broken.\n", ipc->ServInfo.humannode);
38
39 #ifdef HAVE_OPENSSL
40         if (using_ssl) {
41                 fprintf(stderr, "Last error: %s\n", ERR_reason_error_string(ERR_get_error()));
42                 SSL_free(ipc->ssl);
43                 ipc->ssl = NULL;
44         }
45         else
46 #endif
47                 fprintf(stderr, "Last error: %s\n", strerror(errno));
48
49         fprintf(stderr, "Please re-connect and log in again.\n");
50         fflush(stderr);
51         fflush(stdout);
52         shutdown(ipc->sock, 2);
53         ipc->sock = -1;
54         exit(1);
55 }