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