]> code.citadel.org Git - citadel.git/blob - citadel/ipc_c_tcp.c
* Allow multiple simultaneous IPC connections. All changes necessary for
[citadel.git] / citadel / ipc_c_tcp.c
1 /*
2  * $Id$
3  * 
4  * Client-side IPC functions
5  *
6  */
7
8 #define UDS                     "_UDS_"
9
10 #define DEFAULT_HOST            UDS
11 #define DEFAULT_PORT            "citadel"
12
13
14 #include "sysdep.h"
15 #undef NDEBUG
16 #include <assert.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <signal.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <sys/un.h>
26 #include <netdb.h>
27 #include <string.h>
28 #include <pwd.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include "citadel.h"
32 #include "ipc.h"
33 #include "citadel_decls.h"
34 #include "tools.h"
35 #if defined(HAVE_OPENSSL)
36 #include "client_crypto.h"
37 #endif
38 #ifndef HAVE_SNPRINTF
39 #include "snprintf.h"
40 #endif
41
42 /*
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
51  * a remote computer.
52  */
53
54 #ifndef INADDR_NONE
55 #define INADDR_NONE 0xffffffff
56 #endif
57
58 /*
59  * FIXME: rewrite all of Ford's stuff here, it won't work with multiple
60  * instances
61  */
62
63 static void (*deathHook)(void) = NULL;
64 int (*error_printf)(char *s, ...) = (int (*)(char *, ...))printf;
65
66 void setIPCDeathHook(void (*hook)(void)) {
67         deathHook = hook;
68 }
69
70 void setIPCErrorPrintf(int (*func)(char *s, ...)) {
71         error_printf = func;
72 }
73
74 void connection_died(CtdlIPC *ipc) {
75         if (deathHook != NULL)
76                 deathHook();
77
78         error_printf("\rYour connection to this Citadel server is broken.\n"
79                         "Last error: %s\n"
80                         "Please re-connect and log in again.\n",
81                         strerror(errno));
82 #ifdef HAVE_OPENSSL
83         SSL_shutdown(ipc->ssl);
84         SSL_free(ipc->ssl);
85         ipc->ssl = NULL;
86 #endif
87         shutdown(ipc->sock, 2);
88         ipc->sock = -1;
89 }
90
91
92 /*
93 static void ipc_timeout(int signum)
94 {
95         error_printf("\rConnection timed out.\n");
96         logoff(NULL, 3);
97 }
98 */
99
100
101 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
102 {
103         struct hostent *phe;
104         struct servent *pse;
105         struct protoent *ppe;
106         struct sockaddr_in sin;
107         int s, type;
108
109         memset(&sin, 0, sizeof(sin));
110         sin.sin_family = AF_INET;
111
112         pse = getservbyname(service, protocol);
113         if (pse != NULL) {
114                 sin.sin_port = pse->s_port;
115         }
116         else if (atoi(service) > 0) {
117                 sin.sin_port = htons(atoi(service));
118         }
119         else {
120                 sin.sin_port = htons(defaultPort);
121         }
122         phe = gethostbyname(host);
123         if (phe) {
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));
128                 return -1;
129         }
130         if ((ppe = getprotobyname(protocol)) == 0) {
131                 error_printf("Can't get %s protocol entry: %s\n",
132                         protocol, strerror(errno));
133                 return -1;
134         }
135         if (!strcmp(protocol, "udp")) {
136                 type = SOCK_DGRAM;
137         } else {
138                 type = SOCK_STREAM;
139         }
140
141         s = socket(PF_INET, type, ppe->p_proto);
142         if (s < 0) {
143                 error_printf("Can't create socket: %s\n", strerror(errno));
144                 return -1;
145         }
146         /*
147         signal(SIGALRM, ipc_timeout);
148         alarm(30);
149         */
150
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));
154                 return -1;
155         }
156         /*
157         alarm(0);
158         signal(SIGALRM, SIG_IGN);
159         */
160
161         return (s);
162 }
163
164 static int uds_connectsock(int *isLocal, char *sockpath)
165 {
166         struct sockaddr_un addr;
167         int s;
168
169         memset(&addr, 0, sizeof(addr));
170         addr.sun_family = AF_UNIX;
171         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
172
173         s = socket(AF_UNIX, SOCK_STREAM, 0);
174         if (s < 0) {
175                 error_printf("Can't create socket: %s\n", strerror(errno));
176                 return -1;
177         }
178
179         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
180                 error_printf("can't connect: %s\n", strerror(errno));
181                 return -1;
182         }
183
184         *isLocal = 1;
185         return s;
186 }
187
188
189 /*
190  * input binary data from socket
191  */
192 void serv_read(CtdlIPC *ipc, char *buf, int bytes)
193 {
194         int len, rlen;
195
196 #if defined(HAVE_OPENSSL)
197         if (ipc->ssl) {
198                 serv_read_ssl(ipc, buf, bytes);
199                 return;
200         }
201 #endif
202         len = 0;
203         while (len < bytes) {
204                 rlen = read(ipc->sock, &buf[len], bytes - len);
205                 if (rlen < 1) {
206                         connection_died(ipc);
207                         return;
208                 }
209                 len += rlen;
210         }
211 }
212
213
214 /*
215  * send binary to server
216  */
217 void serv_write(CtdlIPC *ipc, const char *buf, int nbytes)
218 {
219         int bytes_written = 0;
220         int retval;
221
222 #if defined(HAVE_OPENSSL)
223         if (ipc->ssl) {
224                 serv_write_ssl(ipc, buf, nbytes);
225                 return;
226         }
227 #endif
228         while (bytes_written < nbytes) {
229                 retval = write(ipc->sock, &buf[bytes_written],
230                                nbytes - bytes_written);
231                 if (retval < 1) {
232                         connection_died(ipc);
233                         return;
234                 }
235                 bytes_written += retval;
236         }
237 }
238
239
240
241 /*
242  * input string from socket - implemented in terms of serv_read()
243  */
244 void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
245 {
246         int i;
247
248         /* Read one character at a time. */
249         for (i = 0;; i++) {
250                 serv_read(ipc, &buf[i], 1);
251                 if (buf[i] == '\n' || i == (SIZ-1))
252                         break;
253         }
254
255         /* If we got a long line, discard characters until the newline. */
256         if (i == (SIZ-1))
257                 while (buf[i] != '\n')
258                         serv_read(ipc, &buf[i], 1);
259
260         /* Strip the trailing newline.
261          */
262         buf[i] = 0;
263 }
264
265
266 /*
267  * send line to server - implemented in terms of serv_write()
268  */
269 void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
270 {
271         /* error_printf("< %s\n", buf); */
272         int watch_ssl = 0;
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);
279 }
280
281
282 /*
283  * attach to server
284  */
285 CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
286 {
287         int a;
288         char cithost[SIZ];
289         char citport[SIZ];
290         char sockpath[SIZ];
291
292         CtdlIPC *ipc = ialloc(CtdlIPC);
293         if (!ipc) {
294                 error_printf("Out of memory creating CtdlIPC!\n");
295                 return 0;
296         }
297 #if defined(HAVE_OPENSSL)
298         ipc->ssl = NULL;
299 #endif
300 #if defined(HAVE_PTHREAD_H)
301         pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
302 #endif
303         ipc->sock = -1;                 /* Not connected */
304         ipc->isLocal = 0;               /* Not local, of course! */
305
306         strcpy(cithost, DEFAULT_HOST);  /* default host */
307         strcpy(citport, DEFAULT_PORT);  /* default port */
308
309         for (a = 0; a < argc; ++a) {
310                 if (a == 0) {
311                         /* do nothing */
312                 } else if (a == 1) {
313                         strcpy(cithost, argv[a]);
314                 } else if (a == 2) {
315                         strcpy(citport, argv[a]);
316                 } else {
317                         error_printf("%s: usage: ",argv[0]);
318                         error_printf("%s [host] [port] ",argv[0]);
319                         ifree(ipc);
320                         return 0;
321                 }
322         }
323
324         if ((!strcmp(cithost, "localhost"))
325            || (!strcmp(cithost, "127.0.0.1"))) {
326                 ipc->isLocal = 1;
327         }
328
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) {
334                         ifree(ipc);
335                         return 0;
336                 }
337                 if (hostbuf != NULL) strcpy(hostbuf, cithost);
338                 if (portbuf != NULL) strcpy(portbuf, sockpath);
339                 return ipc;
340         }
341
342         ipc->sock = connectsock(cithost, citport, "tcp", 504);
343         if (ipc->sock == -1) {
344                 ifree(ipc);
345                 return 0;
346         }
347         if (hostbuf != NULL) strcpy(hostbuf, cithost);
348         if (portbuf != NULL) strcpy(portbuf, citport);
349         return ipc;
350 }
351
352 /*
353  * return the file descriptor of the server socket so we can select() on it.
354  *
355  * FIXME: This is only used in chat mode; eliminate it when chat mode gets
356  * rewritten...
357  */
358 int CtdlIPC_getsockfd(CtdlIPC* ipc)
359 {
360         return ipc->sock;
361 }
362
363
364 /*
365  * return one character
366  *
367  * FIXME: This is only used in chat mode; eliminate it when chat mode gets
368  * rewritten...
369  */
370 char CtdlIPC_get(CtdlIPC* ipc)
371 {
372         char buf[2];
373         char ch;
374
375         serv_read(ipc, buf, 1);
376         ch = (int) buf[0];
377
378         return (ch);
379 }