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