93edebcdb4a0e0be7850e819e6fc16dd8a27fd18
[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         deathHook = hook;
26 }
27
28 void setIPCErrorPrintf(int (*func)(char *s, ...)) {
29         error_printf = func;
30 }
31
32 void connection_died(CtdlIPC* ipc, int using_ssl) {
33         if (deathHook != NULL) {
34                 deathHook();
35         }
36
37         stty_ctdl(SB_RESTORE);
38         fprintf(stderr, "\r\n\n\n");
39         fprintf(stderr, "Your connection to %s is broken.\n", ipc->ServInfo.humannode);
40
41 #ifdef HAVE_OPENSSL
42         if (using_ssl) {
43                 fprintf(stderr, "Last error: %s\n", ERR_reason_error_string(ERR_get_error()));
44                 SSL_free(ipc->ssl);
45                 ipc->ssl = NULL;
46         } else
47 #endif
48                 fprintf(stderr, "Last error: %s\n", strerror(errno));
49
50         fprintf(stderr, "Please re-connect and log in again.\n");
51         fflush(stderr);
52         fflush(stdout);
53         shutdown(ipc->sock, 2);
54         ipc->sock = -1;
55         exit(1);
56 }