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"
42 extern int err_printf(char *fmt, ...);
46 * If server_is_local is set to nonzero, the client assumes that it is running
47 * on the same computer as the server. Several things happen when this is
48 * the case, including the ability to map a specific tty to a particular login
49 * session in the "<W>ho is online" listing, the ability to run external
50 * programs, and the ability to download files directly off the disk without
51 * having to first fetch them from the server.
52 * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
53 * network session to the local machine) or 0 if the server is executing on
56 int server_is_local = 0;
59 #define INADDR_NONE 0xffffffff
64 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
65 extern int ssl_is_connected;
69 void connection_died(void) {
73 err_printf("\rYour connection to this Citadel server is broken.\n"
75 "Please re-connect and log in again.\n",
81 static void ipc_timeout(int signum)
83 err_printf("\rConnection timed out.\n");
88 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
93 struct sockaddr_in sin;
96 memset(&sin, 0, sizeof(sin));
97 sin.sin_family = AF_INET;
99 pse = getservbyname(service, protocol);
101 sin.sin_port = pse->s_port;
103 else if (atoi(service) > 0) {
104 sin.sin_port = htons(atoi(service));
107 sin.sin_port = htons(defaultPort);
109 phe = gethostbyname(host);
111 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
112 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
113 err_printf("Can't get %s host entry: %s\n",
114 host, strerror(errno));
117 if ((ppe = getprotobyname(protocol)) == 0) {
118 err_printf("Can't get %s protocol entry: %s\n",
119 protocol, strerror(errno));
122 if (!strcmp(protocol, "udp")) {
128 s = socket(PF_INET, type, ppe->p_proto);
130 err_printf("Can't create socket: %s\n", strerror(errno));
133 signal(SIGALRM, ipc_timeout);
136 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
137 err_printf("can't connect to %s:%s: %s\n",
138 host, service, strerror(errno));
142 signal(SIGALRM, SIG_IGN);
147 int uds_connectsock(char *sockpath)
149 struct sockaddr_un addr;
152 memset(&addr, 0, sizeof(addr));
153 addr.sun_family = AF_UNIX;
154 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
156 s = socket(AF_UNIX, SOCK_STREAM, 0);
158 err_printf("Can't create socket: %s\n", strerror(errno));
162 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
163 err_printf("can't connect: %s\n", strerror(errno));
173 * input binary data from socket
175 void serv_read(char *buf, int bytes)
179 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
180 if (ssl_is_connected) {
181 serv_read_ssl(buf, bytes);
186 while (len < bytes) {
187 rlen = read(serv_sock, &buf[len], bytes - len);
198 * send binary to server
200 void serv_write(char *buf, int nbytes)
202 int bytes_written = 0;
205 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
206 if (ssl_is_connected) {
207 serv_write_ssl(buf, nbytes);
211 while (bytes_written < nbytes) {
212 retval = write(serv_sock, &buf[bytes_written],
213 nbytes - bytes_written);
218 bytes_written += retval;
225 * input string from socket - implemented in terms of serv_read()
227 void serv_gets(char *buf)
231 /* Read one character at a time.
234 serv_read(&buf[i], 1);
235 if (buf[i] == '\n' || i == (SIZ-1))
239 /* If we got a long line, discard characters until the newline.
242 while (buf[i] != '\n')
243 serv_read(&buf[i], 1);
245 /* Strip the trailing newline.
252 * send line to server - implemented in terms of serv_write()
254 void serv_puts(char *buf)
256 /* err_printf("< %s\n", buf); */
257 serv_write(buf, strlen(buf));
265 void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
272 strcpy(cithost, DEFAULT_HOST); /* default host */
273 strcpy(citport, DEFAULT_PORT); /* default port */
275 for (a = 0; a < argc; ++a) {
279 strcpy(cithost, argv[a]);
281 strcpy(citport, argv[a]);
284 err_printf("%s: usage: ",argv[0]);
285 err_printf("%s [host] [port] ",argv[0]);
290 if ((!strcmp(cithost, "localhost"))
291 || (!strcmp(cithost, "127.0.0.1"))) {
295 /* If we're using a unix domain socket we can do a bunch of stuff */
296 if (!strcmp(cithost, UDS)) {
297 snprintf(sockpath, sizeof sockpath, "citadel.socket");
298 serv_sock = uds_connectsock(sockpath);
299 if (hostbuf != NULL) strcpy(hostbuf, cithost);
300 if (portbuf != NULL) strcpy(portbuf, sockpath);
304 serv_sock = connectsock(cithost, citport, "tcp", 504);
305 if (hostbuf != NULL) strcpy(hostbuf, cithost);
306 if (portbuf != NULL) strcpy(portbuf, citport);
311 * return the file descriptor of the server socket so we can select() on it.
320 * return one character