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 int err_printf(char *fmt, ...);
43 int err_printf(char *fmt, ...)
48 retval = fprintf(stderr, fmt, ap);
55 * If server_is_local is set to nonzero, the client assumes that it is running
56 * on the same computer as the server. Several things happen when this is
57 * the case, including the ability to map a specific tty to a particular login
58 * session in the "<W>ho is online" listing, the ability to run external
59 * programs, and the ability to download files directly off the disk without
60 * having to first fetch them from the server.
61 * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
62 * network session to the local machine) or 0 if the server is executing on
65 int server_is_local = 0;
68 #define INADDR_NONE 0xffffffff
73 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
74 extern int ssl_is_connected;
78 void connection_died(void) {
79 err_printf("\rYour connection to this Citadel server is broken.\n"
80 "Please re-connect and log in again.\n");
85 void timeout(int signum)
87 err_printf("\rConnection timed out.\n");
92 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
97 struct sockaddr_in sin;
100 memset(&sin, 0, sizeof(sin));
101 sin.sin_family = AF_INET;
103 pse = getservbyname(service, protocol);
105 sin.sin_port = pse->s_port;
107 else if (atoi(service) > 0) {
108 sin.sin_port = htons(atoi(service));
111 sin.sin_port = htons(defaultPort);
113 phe = gethostbyname(host);
115 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
116 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
117 err_printf("Can't get %s host entry: %s\n",
118 host, strerror(errno));
121 if ((ppe = getprotobyname(protocol)) == 0) {
122 err_printf("Can't get %s protocol entry: %s\n",
123 protocol, strerror(errno));
126 if (!strcmp(protocol, "udp")) {
132 s = socket(PF_INET, type, ppe->p_proto);
134 err_printf("Can't create socket: %s\n", strerror(errno));
137 signal(SIGALRM, timeout);
140 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
141 err_printf("can't connect to %s.%s: %s\n",
142 host, service, strerror(errno));
146 signal(SIGALRM, SIG_IGN);
151 int uds_connectsock(char *sockpath)
153 struct sockaddr_un addr;
156 memset(&addr, 0, sizeof(addr));
157 addr.sun_family = AF_UNIX;
158 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
160 s = socket(AF_UNIX, SOCK_STREAM, 0);
162 err_printf("Can't create socket: %s\n", strerror(errno));
166 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
167 err_printf("can't connect: %s\n", strerror(errno));
177 * input binary data from socket
179 void serv_read(char *buf, int bytes)
183 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
184 if (ssl_is_connected) {
185 serv_read_ssl(buf, bytes);
190 while (len < bytes) {
191 rlen = read(serv_sock, &buf[len], bytes - len);
202 * send binary to server
204 void serv_write(char *buf, int nbytes)
206 int bytes_written = 0;
209 #if defined(HAVE_OPENSSL) && defined(CIT_CLIENT)
210 if (ssl_is_connected) {
211 serv_write_ssl(buf, nbytes);
215 while (bytes_written < nbytes) {
216 retval = write(serv_sock, &buf[bytes_written],
217 nbytes - bytes_written);
222 bytes_written += retval;
229 * input string from socket - implemented in terms of serv_read()
231 void serv_gets(char *buf)
235 /* Read one character at a time.
238 serv_read(&buf[i], 1);
239 if (buf[i] == '\n' || i == (SIZ-1))
243 /* If we got a long line, discard characters until the newline.
246 while (buf[i] != '\n')
247 serv_read(&buf[i], 1);
249 /* Strip the trailing newline.
256 * send line to server - implemented in terms of serv_write()
258 void serv_puts(char *buf)
260 /* err_printf("< %s\n", buf); */
261 serv_write(buf, strlen(buf));
269 void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
276 strcpy(cithost, DEFAULT_HOST); /* default host */
277 strcpy(citport, DEFAULT_PORT); /* default port */
279 for (a = 0; a < argc; ++a) {
283 strcpy(cithost, argv[a]);
285 strcpy(citport, argv[a]);
288 err_printf("%s: usage: ",argv[0]);
289 err_printf("%s [host] [port] ",argv[0]);
294 if ((!strcmp(cithost, "localhost"))
295 || (!strcmp(cithost, "127.0.0.1"))) {
299 /* If we're using a unix domain socket we can do a bunch of stuff */
300 if (!strcmp(cithost, UDS)) {
301 sprintf(sockpath, "citadel.socket");
302 serv_sock = uds_connectsock(sockpath);
303 if (hostbuf != NULL) strcpy(hostbuf, cithost);
304 if (portbuf != NULL) strcpy(portbuf, sockpath);
308 serv_sock = connectsock(cithost, citport, "tcp", 504);
309 if (hostbuf != NULL) strcpy(hostbuf, cithost);
310 if (portbuf != NULL) strcpy(portbuf, citport);
315 * return the file descriptor of the server socket so we can select() on it.
324 * return one character