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