Back to index

citadel  8.12
ipc_c_tcp.c
Go to the documentation of this file.
00001 /*
00002  * Client-side IPC functions
00003  */
00004 
00005 
00006 #include "sysdep.h"
00007 #undef NDEBUG
00008 #include <assert.h>
00009 #include <stdlib.h>
00010 #include <unistd.h>
00011 #include <stdio.h>
00012 #include <signal.h>
00013 #include <sys/types.h>
00014 #include <sys/socket.h>
00015 #include <netinet/in.h>
00016 #include <arpa/inet.h>
00017 #include <sys/un.h>
00018 #include <netdb.h>
00019 #include <string.h>
00020 #include <pwd.h>
00021 #include <errno.h>
00022 #include <stdarg.h>
00023 #include <libcitadel.h>
00024 #include "citadel.h"
00025 #include "citadel_ipc.h"
00026 #include "citadel_decls.h"
00027 #ifndef HAVE_SNPRINTF
00028 #include "snprintf.h"
00029 #endif
00030 #include "commands.h"
00031 
00032 /* Note that some of these functions may not work with multiple instances. */
00033 
00034 static void (*deathHook)(void) = NULL;
00035 int (*error_printf)(char *s, ...) = (int (*)(char *, ...))printf;
00036 
00037 void setIPCDeathHook(void (*hook)(void)) {
00038        deathHook = hook;
00039 }
00040 
00041 void setIPCErrorPrintf(int (*func)(char *s, ...)) {
00042        error_printf = func;
00043 }
00044 
00045 void connection_died(CtdlIPC* ipc, int using_ssl) {
00046        if (deathHook != NULL) {
00047               deathHook();
00048        }
00049 
00050        stty_ctdl(SB_RESTORE);
00051        fprintf(stderr, "\r\n\n\n");
00052        fprintf(stderr, "Your connection to %s is broken.\n", ipc->ServInfo.humannode);
00053 
00054 #ifdef HAVE_OPENSSL
00055        if (using_ssl) {
00056               fprintf(stderr, "Last error: %s\n", ERR_reason_error_string(ERR_get_error()));
00057               SSL_free(ipc->ssl);
00058               ipc->ssl = NULL;
00059        } else
00060 #endif
00061               fprintf(stderr, "Last error: %s\n", strerror(errno));
00062 
00063        fprintf(stderr, "Please re-connect and log in again.\n");
00064        fflush(stderr);
00065        fflush(stdout);
00066        shutdown(ipc->sock, 2);
00067        ipc->sock = -1;
00068         exit(1);
00069 }