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)
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 static void (*deathHook)(void) = NULL;
60 int (*error_printf)(char *s, ...) = (int (*)(char *, ...))printf;
62 void setIPCDeathHook(void (*hook)(void)) {
66 void setIPCErrorPrintf(int (*func)(char *s, ...)) {
70 void connection_died(void) {
71 if (deathHook != NULL)
74 error_printf("\rYour connection to this Citadel server is broken.\n"
76 "Please re-connect and log in again.\n",
82 static void ipc_timeout(int signum)
84 error_printf("\rConnection timed out.\n");
89 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
94 struct sockaddr_in sin;
97 memset(&sin, 0, sizeof(sin));
98 sin.sin_family = AF_INET;
100 pse = getservbyname(service, protocol);
102 sin.sin_port = pse->s_port;
104 else if (atoi(service) > 0) {
105 sin.sin_port = htons(atoi(service));
108 sin.sin_port = htons(defaultPort);
110 phe = gethostbyname(host);
112 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
113 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
114 error_printf("Can't get %s host entry: %s\n",
115 host, strerror(errno));
118 if ((ppe = getprotobyname(protocol)) == 0) {
119 error_printf("Can't get %s protocol entry: %s\n",
120 protocol, strerror(errno));
123 if (!strcmp(protocol, "udp")) {
129 s = socket(PF_INET, type, ppe->p_proto);
131 error_printf("Can't create socket: %s\n", strerror(errno));
134 signal(SIGALRM, ipc_timeout);
137 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
138 error_printf("can't connect to %s:%s: %s\n",
139 host, service, strerror(errno));
143 signal(SIGALRM, SIG_IGN);
148 int uds_connectsock(char *sockpath)
150 struct sockaddr_un addr;
153 memset(&addr, 0, sizeof(addr));
154 addr.sun_family = AF_UNIX;
155 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
157 s = socket(AF_UNIX, SOCK_STREAM, 0);
159 error_printf("Can't create socket: %s\n", strerror(errno));
163 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
164 error_printf("can't connect: %s\n", strerror(errno));
174 * input binary data from socket
176 void serv_read(char *buf, int bytes)
180 #if defined(HAVE_OPENSSL)
181 if (ssl_is_connected) {
182 serv_read_ssl(buf, bytes);
187 while (len < bytes) {
188 rlen = read(serv_sock, &buf[len], bytes - len);
199 * send binary to server
201 void serv_write(char *buf, int nbytes)
203 int bytes_written = 0;
206 #if defined(HAVE_OPENSSL)
207 if (ssl_is_connected) {
208 serv_write_ssl(buf, nbytes);
212 while (bytes_written < nbytes) {
213 retval = write(serv_sock, &buf[bytes_written],
214 nbytes - bytes_written);
219 bytes_written += retval;
226 * input string from socket - implemented in terms of serv_read()
228 void serv_gets(char *buf)
232 /* Read one character at a time.
235 serv_read(&buf[i], 1);
236 if (buf[i] == '\n' || i == (SIZ-1))
240 /* If we got a long line, discard characters until the newline.
243 while (buf[i] != '\n')
244 serv_read(&buf[i], 1);
246 /* Strip the trailing newline.
253 * send line to server - implemented in terms of serv_write()
255 void serv_puts(char *buf)
257 /* error_printf("< %s\n", buf); */
258 serv_write(buf, strlen(buf));
266 void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
273 strcpy(cithost, DEFAULT_HOST); /* default host */
274 strcpy(citport, DEFAULT_PORT); /* default port */
276 for (a = 0; a < argc; ++a) {
280 strcpy(cithost, argv[a]);
282 strcpy(citport, argv[a]);
285 error_printf("%s: usage: ",argv[0]);
286 error_printf("%s [host] [port] ",argv[0]);
291 if ((!strcmp(cithost, "localhost"))
292 || (!strcmp(cithost, "127.0.0.1"))) {
296 /* If we're using a unix domain socket we can do a bunch of stuff */
297 if (!strcmp(cithost, UDS)) {
298 snprintf(sockpath, sizeof sockpath, "citadel.socket");
299 serv_sock = uds_connectsock(sockpath);
300 if (hostbuf != NULL) strcpy(hostbuf, cithost);
301 if (portbuf != NULL) strcpy(portbuf, sockpath);
305 serv_sock = connectsock(cithost, citport, "tcp", 504);
306 if (hostbuf != NULL) strcpy(hostbuf, cithost);
307 if (portbuf != NULL) strcpy(portbuf, citport);
312 * return the file descriptor of the server socket so we can select() on it.
321 * return one character