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