#include "citadel_decls.h"
#include "ipc.h"
#include "tools.h"
-#if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
+#if defined(HAVE_OPENSSL)
#include "client_crypto.h"
#endif
#ifndef HAVE_SNPRINTF
int serv_sock;
-#if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
-extern int ssl_is_connected;
-#endif
+static void (*deathHook)(void) = NULL;
+int (*error_printf)(char *s, ...) = (int (*)(char *, ...))printf;
+
+void setIPCDeathHook(void (*hook)(void)) {
+ deathHook = hook;
+}
+void setIPCErrorPrintf(int (*func)(char *s, ...)) {
+ error_printf = func;
+}
void connection_died(void) {
- fprintf(stderr, "\r"
- "Your connection to this Citadel server is broken.\n"
- "Please re-connect and log in again.\n");
+ if (deathHook != NULL)
+ deathHook();
+
+ error_printf("\rYour connection to this Citadel server is broken.\n"
+ "Last error: %s\n"
+ "Please re-connect and log in again.\n",
+ strerror(errno));
logoff(3);
}
-void timeout(int signum)
+static void ipc_timeout(int signum)
{
- printf("\rConnection timed out.\n");
+ error_printf("\rConnection timed out.\n");
logoff(3);
}
if (phe) {
memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
} else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
- fprintf(stderr, "Can't get %s host entry: %s\n",
+ error_printf("Can't get %s host entry: %s\n",
host, strerror(errno));
logoff(3);
}
if ((ppe = getprotobyname(protocol)) == 0) {
- fprintf(stderr, "Can't get %s protocol entry: %s\n",
+ error_printf("Can't get %s protocol entry: %s\n",
protocol, strerror(errno));
logoff(3);
}
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0) {
- fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
+ error_printf("Can't create socket: %s\n", strerror(errno));
logoff(3);
}
- signal(SIGALRM, timeout);
+ signal(SIGALRM, ipc_timeout);
alarm(30);
if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
- fprintf(stderr, "can't connect to %s.%s: %s\n",
+ error_printf("can't connect to %s:%s: %s\n",
host, service, strerror(errno));
logoff(3);
}
s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0) {
- fprintf(stderr, "Can't create socket: %s\n",
- strerror(errno));
+ error_printf("Can't create socket: %s\n", strerror(errno));
logoff(3);
}
if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- fprintf(stderr, "can't connect: %s\n",
- strerror(errno));
+ error_printf("can't connect: %s\n", strerror(errno));
logoff(3);
}
*/
void serv_puts(char *buf)
{
- /* printf("< %s\n", buf); */
+ /* error_printf("< %s\n", buf); */
serv_write(buf, strlen(buf));
serv_write("\n", 1);
}
strcpy(citport, argv[a]);
}
else {
- fprintf(stderr,"%s: usage: ",argv[0]);
- fprintf(stderr,"%s [host] [port] ",argv[0]);
+ error_printf("%s: usage: ",argv[0]);
+ error_printf("%s [host] [port] ",argv[0]);
logoff(2);
}
}
/* If we're using a unix domain socket we can do a bunch of stuff */
if (!strcmp(cithost, UDS)) {
- sprintf(sockpath, "citadel.socket");
+ snprintf(sockpath, sizeof sockpath, "citadel.socket");
serv_sock = uds_connectsock(sockpath);
if (hostbuf != NULL) strcpy(hostbuf, cithost);
if (portbuf != NULL) strcpy(portbuf, sockpath);