]> code.citadel.org Git - citadel.git/blob - citadel/ipc_c_tcp.c
1680bcbf51eca57d4703fe30672aaa1e27e09a20
[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 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <signal.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <sys/un.h>
24 #include <netdb.h>
25 #include <string.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include "citadel.h"
30 #include "citadel_decls.h"
31 #include "ipc.h"
32 #include "tools.h"
33 #if defined(HAVE_OPENSSL)
34 #include "client_crypto.h"
35 #endif
36 #ifndef HAVE_SNPRINTF
37 #include "snprintf.h"
38 #endif
39
40 /*
41  * If server_is_local is set to nonzero, the client assumes that it is running
42  * on the same computer as the server.  Several things happen when this is
43  * the case, including the ability to map a specific tty to a particular login
44  * session in the "<W>ho is online" listing, the ability to run external
45  * programs, and the ability to download files directly off the disk without
46  * having to first fetch them from the server.
47  * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
48  * network session to the local machine) or 0 if the server is executing on
49  * a remote computer.
50  */
51 int server_is_local = 0;
52
53 #ifndef INADDR_NONE
54 #define INADDR_NONE 0xffffffff
55 #endif
56
57 int serv_sock;
58
59 static void (*deathHook)(void) = NULL;
60 int (*error_printf)(char *s, ...) = (int (*)(char *, ...))printf;
61
62 void setIPCDeathHook(void (*hook)(void)) {
63         deathHook = hook;
64 }
65
66 void setIPCErrorPrintf(int (*func)(char *s, ...)) {
67         error_printf = func;
68 }
69
70 void connection_died(void) {
71         if (deathHook != NULL)
72                 deathHook();
73
74         error_printf("\rYour connection to this Citadel server is broken.\n"
75                         "Last error: %s\n"
76                         "Please re-connect and log in again.\n",
77                         strerror(errno));
78         logoff(3);
79 }
80
81
82 static void ipc_timeout(int signum)
83 {
84         error_printf("\rConnection timed out.\n");
85         logoff(3);
86 }
87
88
89 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
90 {
91         struct hostent *phe;
92         struct servent *pse;
93         struct protoent *ppe;
94         struct sockaddr_in sin;
95         int s, type;
96
97         memset(&sin, 0, sizeof(sin));
98         sin.sin_family = AF_INET;
99
100         pse = getservbyname(service, protocol);
101         if (pse != NULL) {
102                 sin.sin_port = pse->s_port;
103         }
104         else if (atoi(service) > 0) {
105                 sin.sin_port = htons(atoi(service));
106         }
107         else {
108                 sin.sin_port = htons(defaultPort);
109         }
110         phe = gethostbyname(host);
111         if (phe) {
112                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
113         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
114                 error_printf("Can't get %s host entry: %s\n",
115                         host, strerror(errno));
116                 logoff(3);
117         }
118         if ((ppe = getprotobyname(protocol)) == 0) {
119                 error_printf("Can't get %s protocol entry: %s\n",
120                         protocol, strerror(errno));
121                 logoff(3);
122         }
123         if (!strcmp(protocol, "udp")) {
124                 type = SOCK_DGRAM;
125         } else {
126                 type = SOCK_STREAM;
127         }
128
129         s = socket(PF_INET, type, ppe->p_proto);
130         if (s < 0) {
131                 error_printf("Can't create socket: %s\n", strerror(errno));
132                 logoff(3);
133         }
134         signal(SIGALRM, ipc_timeout);
135         alarm(30);
136
137         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
138                 error_printf("can't connect to %s:%s: %s\n",
139                         host, service, strerror(errno));
140                 logoff(3);
141         }
142         alarm(0);
143         signal(SIGALRM, SIG_IGN);
144
145         return (s);
146 }
147
148 int uds_connectsock(char *sockpath)
149 {
150         struct sockaddr_un addr;
151         int s;
152
153         memset(&addr, 0, sizeof(addr));
154         addr.sun_family = AF_UNIX;
155         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
156
157         s = socket(AF_UNIX, SOCK_STREAM, 0);
158         if (s < 0) {
159                 error_printf("Can't create socket: %s\n", strerror(errno));
160                 logoff(3);
161         }
162
163         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
164                 error_printf("can't connect: %s\n", strerror(errno));
165                 logoff(3);
166         }
167
168         server_is_local = 1;
169         return s;
170 }
171
172
173 /*
174  * input binary data from socket
175  */
176 void serv_read(char *buf, int bytes)
177 {
178         int len, rlen;
179
180 #if defined(HAVE_OPENSSL)
181         if (ssl_is_connected) {
182                 serv_read_ssl(buf, bytes);
183                 return;
184         }
185 #endif
186         len = 0;
187         while (len < bytes) {
188                 rlen = read(serv_sock, &buf[len], bytes - len);
189                 if (rlen < 1) {
190                         connection_died();
191                         return;
192                 }
193                 len += rlen;
194         }
195 }
196
197
198 /*
199  * send binary to server
200  */
201 void serv_write(char *buf, int nbytes)
202 {
203         int bytes_written = 0;
204         int retval;
205
206 #if defined(HAVE_OPENSSL)
207         if (ssl_is_connected) {
208                 serv_write_ssl(buf, nbytes);
209                 return;
210         }
211 #endif
212         while (bytes_written < nbytes) {
213                 retval = write(serv_sock, &buf[bytes_written],
214                                nbytes - bytes_written);
215                 if (retval < 1) {
216                         connection_died();
217                         return;
218                 }
219                 bytes_written += retval;
220         }
221 }
222
223
224
225 /*
226  * input string from socket - implemented in terms of serv_read()
227  */
228 void serv_gets(char *buf)
229 {
230         int i;
231
232         /* Read one character at a time.
233          */
234         for (i = 0;; i++) {
235                 serv_read(&buf[i], 1);
236                 if (buf[i] == '\n' || i == (SIZ-1))
237                         break;
238         }
239
240         /* If we got a long line, discard characters until the newline.
241          */
242         if (i == (SIZ-1))
243                 while (buf[i] != '\n')
244                         serv_read(&buf[i], 1);
245
246         /* Strip the trailing newline.
247          */
248         buf[i] = 0;
249 }
250
251
252 /*
253  * send line to server - implemented in terms of serv_write()
254  */
255 void serv_puts(char *buf)
256 {
257         /* error_printf("< %s\n", buf); */
258         serv_write(buf, strlen(buf));
259         serv_write("\n", 1);
260 }
261
262
263 /*
264  * attach to server
265  */
266 void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
267 {
268         int a;
269         char cithost[SIZ];
270         char citport[SIZ];
271         char sockpath[SIZ];
272
273         strcpy(cithost, DEFAULT_HOST);  /* default host */
274         strcpy(citport, DEFAULT_PORT);  /* default port */
275
276         for (a = 0; a < argc; ++a) {
277                 if (a == 0) {
278                         /* do nothing */
279                 } else if (a == 1) {
280                         strcpy(cithost, argv[a]);
281                 } else if (a == 2) {
282                         strcpy(citport, argv[a]);
283                 }
284                 else {
285                         error_printf("%s: usage: ",argv[0]);
286                         error_printf("%s [host] [port] ",argv[0]);
287                         logoff(2);
288                 }
289         }
290
291         if ((!strcmp(cithost, "localhost"))
292            || (!strcmp(cithost, "127.0.0.1"))) {
293                 server_is_local = 1;
294         }
295
296         /* If we're using a unix domain socket we can do a bunch of stuff */
297         if (!strcmp(cithost, UDS)) {
298                 snprintf(sockpath, sizeof sockpath, "citadel.socket");
299                 serv_sock = uds_connectsock(sockpath);
300                 if (hostbuf != NULL) strcpy(hostbuf, cithost);
301                 if (portbuf != NULL) strcpy(portbuf, sockpath);
302                 return;
303         }
304
305         serv_sock = connectsock(cithost, citport, "tcp", 504);
306         if (hostbuf != NULL) strcpy(hostbuf, cithost);
307         if (portbuf != NULL) strcpy(portbuf, citport);
308         return;
309 }
310
311 /*
312  * return the file descriptor of the server socket so we can select() on it.
313  */
314 int getsockfd(void)
315 {
316         return serv_sock;
317 }
318
319
320 /*
321  * return one character
322  */
323 char serv_getc(void)
324 {
325         char buf[2];
326         char ch;
327
328         serv_read(buf, 1);
329         ch = (int) buf[0];
330
331         return (ch);
332 }