4 * Client-side IPC functions
10 #define DEFAULT_HOST UDS
11 #define DEFAULT_PORT "citadel"
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
30 #include "citadel_decls.h"
33 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
34 #include "client_crypto.h"
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
51 int server_is_local = 0;
54 #define INADDR_NONE 0xffffffff
59 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
60 extern int ssl_is_connected;
64 void connection_died(void) {
66 "Your connection to this Citadel server is broken.\n"
67 "Please re-connect and log in again.\n");
72 void timeout(int signum)
74 printf("\rConnection timed out.\n");
79 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
84 struct sockaddr_in sin;
87 memset(&sin, 0, sizeof(sin));
88 sin.sin_family = AF_INET;
90 pse = getservbyname(service, protocol);
92 sin.sin_port = pse->s_port;
94 else if (atoi(service) > 0) {
95 sin.sin_port = htons(atoi(service));
98 sin.sin_port = htons(defaultPort);
100 phe = gethostbyname(host);
102 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
103 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
104 fprintf(stderr, "Can't get %s host entry: %s\n",
105 host, strerror(errno));
108 if ((ppe = getprotobyname(protocol)) == 0) {
109 fprintf(stderr, "Can't get %s protocol entry: %s\n",
110 protocol, strerror(errno));
113 if (!strcmp(protocol, "udp")) {
119 s = socket(PF_INET, type, ppe->p_proto);
121 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
124 signal(SIGALRM, timeout);
127 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
128 fprintf(stderr, "can't connect to %s.%s: %s\n",
129 host, service, strerror(errno));
133 signal(SIGALRM, SIG_IGN);
138 int uds_connectsock(char *sockpath)
140 struct sockaddr_un addr;
143 memset(&addr, 0, sizeof(addr));
144 addr.sun_family = AF_UNIX;
145 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
147 s = socket(AF_UNIX, SOCK_STREAM, 0);
149 fprintf(stderr, "Can't create socket: %s\n",
154 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
155 fprintf(stderr, "can't connect: %s\n",
166 * input binary data from socket
168 void serv_read(char *buf, int bytes)
172 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
173 if (ssl_is_connected) {
174 serv_read_ssl(buf, bytes);
179 while (len < bytes) {
180 rlen = read(serv_sock, &buf[len], bytes - len);
191 * send binary to server
193 void serv_write(char *buf, int nbytes)
195 int bytes_written = 0;
198 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
199 if (ssl_is_connected) {
200 serv_write_ssl(buf, nbytes);
204 while (bytes_written < nbytes) {
205 retval = write(serv_sock, &buf[bytes_written],
206 nbytes - bytes_written);
211 bytes_written += retval;
218 * input string from socket - implemented in terms of serv_read()
220 void serv_gets(char *buf)
224 /* Read one character at a time.
227 serv_read(&buf[i], 1);
228 if (buf[i] == '\n' || i == (SIZ-1))
232 /* If we got a long line, discard characters until the newline.
235 while (buf[i] != '\n')
236 serv_read(&buf[i], 1);
238 /* Strip the trailing newline.
245 * send line to server - implemented in terms of serv_write()
247 void serv_puts(char *buf)
249 /* printf("< %s\n", buf); */
250 serv_write(buf, strlen(buf));
258 void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
265 strcpy(cithost, DEFAULT_HOST); /* default host */
266 strcpy(citport, DEFAULT_PORT); /* default port */
268 for (a = 0; a < argc; ++a) {
272 strcpy(cithost, argv[a]);
274 strcpy(citport, argv[a]);
277 fprintf(stderr,"%s: usage: ",argv[0]);
278 fprintf(stderr,"%s [host] [port] ",argv[0]);
283 if ((!strcmp(cithost, "localhost"))
284 || (!strcmp(cithost, "127.0.0.1"))) {
288 /* If we're using a unix domain socket we can do a bunch of stuff */
289 if (!strcmp(cithost, UDS)) {
290 sprintf(sockpath, "citadel.socket");
291 serv_sock = uds_connectsock(sockpath);
292 if (hostbuf != NULL) strcpy(hostbuf, cithost);
293 if (portbuf != NULL) strcpy(portbuf, sockpath);
297 serv_sock = connectsock(cithost, citport, "tcp", 504);
298 if (hostbuf != NULL) strcpy(hostbuf, cithost);
299 if (portbuf != NULL) strcpy(portbuf, citport);
304 * return the file descriptor of the server socket so we can select() on it.
313 * return one character