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