4 * Client-side IPC functions
10 #define DEFAULT_HOST UDS
11 #define DEFAULT_PORT "citadel"
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
33 #include "citadel_decls.h"
35 #if defined(HAVE_OPENSSL)
36 #include "client_crypto.h"
43 * If ipc->isLocal is set to nonzero, the client assumes that it is running on
44 * the same computer as the server. Several things happen when this is the
45 * case, including the ability to map a specific tty to a particular login
46 * session in the "<W>ho is online" listing, the ability to run external
47 * programs, and the ability to download files directly off the disk without
48 * having to first fetch them from the server.
49 * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
50 * network session to the local machine) or 0 if the server is executing on
55 #define INADDR_NONE 0xffffffff
59 * FIXME: rewrite all of Ford's stuff here, it won't work with multiple
63 static void (*deathHook)(void) = NULL;
64 int (*error_printf)(char *s, ...) = (int (*)(char *, ...))printf;
66 void setIPCDeathHook(void (*hook)(void)) {
70 void setIPCErrorPrintf(int (*func)(char *s, ...)) {
74 void connection_died(CtdlIPC *ipc) {
75 if (deathHook != NULL)
78 error_printf("\rYour connection to this Citadel server is broken.\n"
80 "Please re-connect and log in again.\n",
83 SSL_shutdown(ipc->ssl);
87 shutdown(ipc->sock, 2);
93 static void ipc_timeout(int signum)
95 error_printf("\rConnection timed out.\n");
101 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
105 struct protoent *ppe;
106 struct sockaddr_in sin;
109 memset(&sin, 0, sizeof(sin));
110 sin.sin_family = AF_INET;
112 pse = getservbyname(service, protocol);
114 sin.sin_port = pse->s_port;
116 else if (atoi(service) > 0) {
117 sin.sin_port = htons(atoi(service));
120 sin.sin_port = htons(defaultPort);
122 phe = gethostbyname(host);
124 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
125 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
126 error_printf("Can't get %s host entry: %s\n",
127 host, strerror(errno));
130 if ((ppe = getprotobyname(protocol)) == 0) {
131 error_printf("Can't get %s protocol entry: %s\n",
132 protocol, strerror(errno));
135 if (!strcmp(protocol, "udp")) {
141 s = socket(PF_INET, type, ppe->p_proto);
143 error_printf("Can't create socket: %s\n", strerror(errno));
147 signal(SIGALRM, ipc_timeout);
151 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
152 error_printf("Can't connect to %s:%s: %s\n",
153 host, service, strerror(errno));
158 signal(SIGALRM, SIG_IGN);
164 static int uds_connectsock(int *isLocal, char *sockpath)
166 struct sockaddr_un addr;
169 memset(&addr, 0, sizeof(addr));
170 addr.sun_family = AF_UNIX;
171 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
173 s = socket(AF_UNIX, SOCK_STREAM, 0);
175 error_printf("Can't create socket: %s\n", strerror(errno));
179 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
180 error_printf("can't connect: %s\n", strerror(errno));
190 * input binary data from socket
192 void serv_read(CtdlIPC *ipc, char *buf, int bytes)
196 #if defined(HAVE_OPENSSL)
198 serv_read_ssl(ipc, buf, bytes);
203 while (len < bytes) {
204 rlen = read(ipc->sock, &buf[len], bytes - len);
206 connection_died(ipc);
215 * send binary to server
217 void serv_write(CtdlIPC *ipc, const char *buf, int nbytes)
219 int bytes_written = 0;
222 #if defined(HAVE_OPENSSL)
224 serv_write_ssl(ipc, buf, nbytes);
228 while (bytes_written < nbytes) {
229 retval = write(ipc->sock, &buf[bytes_written],
230 nbytes - bytes_written);
232 connection_died(ipc);
235 bytes_written += retval;
242 * input string from socket - implemented in terms of serv_read()
244 void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
248 /* Read one character at a time. */
250 serv_read(ipc, &buf[i], 1);
251 if (buf[i] == '\n' || i == (SIZ-1))
255 /* If we got a long line, discard characters until the newline. */
257 while (buf[i] != '\n')
258 serv_read(ipc, &buf[i], 1);
260 /* Strip the trailing newline.
267 * send line to server - implemented in terms of serv_write()
269 void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
271 /* error_printf("< %s\n", buf); */
273 if (ipc->ssl) watch_ssl = 1;
274 assert(!watch_ssl || ipc->ssl);
275 serv_write(ipc, buf, strlen(buf));
276 assert(!watch_ssl || ipc->ssl);
277 serv_write(ipc, "\n", 1);
278 assert(!watch_ssl || ipc->ssl);
285 CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
292 CtdlIPC *ipc = ialloc(CtdlIPC);
294 error_printf("Out of memory creating CtdlIPC!\n");
297 #if defined(HAVE_OPENSSL)
300 #if defined(HAVE_PTHREAD_H)
301 pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
303 ipc->sock = -1; /* Not connected */
304 ipc->isLocal = 0; /* Not local, of course! */
306 strcpy(cithost, DEFAULT_HOST); /* default host */
307 strcpy(citport, DEFAULT_PORT); /* default port */
309 for (a = 0; a < argc; ++a) {
313 strcpy(cithost, argv[a]);
315 strcpy(citport, argv[a]);
317 error_printf("%s: usage: ",argv[0]);
318 error_printf("%s [host] [port] ",argv[0]);
324 if ((!strcmp(cithost, "localhost"))
325 || (!strcmp(cithost, "127.0.0.1"))) {
329 /* If we're using a unix domain socket we can do a bunch of stuff */
330 if (!strcmp(cithost, UDS)) {
331 snprintf(sockpath, sizeof sockpath, "citadel.socket");
332 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
333 if (ipc->sock == -1) {
337 if (hostbuf != NULL) strcpy(hostbuf, cithost);
338 if (portbuf != NULL) strcpy(portbuf, sockpath);
342 ipc->sock = connectsock(cithost, citport, "tcp", 504);
343 if (ipc->sock == -1) {
347 if (hostbuf != NULL) strcpy(hostbuf, cithost);
348 if (portbuf != NULL) strcpy(portbuf, citport);
353 * return the file descriptor of the server socket so we can select() on it.
355 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
358 int CtdlIPC_getsockfd(CtdlIPC* ipc)
365 * return one character
367 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
370 char CtdlIPC_get(CtdlIPC* ipc)
375 serv_read(ipc, buf, 1);