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