d87cdf5b7157dd9e9748c20b9f1d74d6e6e45252
[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 #include <stdlib.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <sys/types.h>
38
39 #if TIME_WITH_SYS_TIME
40 # include <sys/time.h>
41 # include <time.h>
42 #else
43 # if HAVE_SYS_TIME_H
44 #  include <sys/time.h>
45 # else
46 #  include <time.h>
47 # endif
48 #endif
49
50 #ifdef HAVE_TERMIOS_H
51 #include <termios.h>
52 #else
53 #include <sgtty.h>
54 #endif
55
56 #ifdef HAVE_SYS_SELECT_H
57 #include <sys/select.h>
58 #endif
59
60 /* Note that some of these functions may not work with multiple instances. */
61
62 static void (*deathHook)(void) = NULL;
63 int (*error_printf)(char *s, ...) = (int (*)(char *, ...))printf;
64
65 void setIPCDeathHook(void (*hook)(void)) {
66         deathHook = hook;
67 }
68
69 void setIPCErrorPrintf(int (*func)(char *s, ...)) {
70         error_printf = func;
71 }
72
73 void connection_died(CtdlIPC* ipc, int using_ssl) {
74         if (deathHook != NULL) {
75                 deathHook();
76         }
77
78         stty_ctdl(SB_RESTORE);
79         fprintf(stderr, "\r\n\n\n");
80         fprintf(stderr, "Your connection to %s is broken.\n", ipc->ServInfo.humannode);
81
82 #ifdef HAVE_OPENSSL
83         if (using_ssl) {
84                 fprintf(stderr, "Last error: %s\n", ERR_reason_error_string(ERR_get_error()));
85                 SSL_free(ipc->ssl);
86                 ipc->ssl = NULL;
87         } else
88 #endif
89                 fprintf(stderr, "Last error: %s\n", strerror(errno));
90
91         fprintf(stderr, "Please re-connect and log in again.\n");
92         fflush(stderr);
93         fflush(stdout);
94         shutdown(ipc->sock, 2);
95         ipc->sock = -1;
96         exit(1);
97 }