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"
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
48 int server_is_local = 0;
51 #define INADDR_NONE 0xffffffff
56 void connection_died(void) {
58 "Your connection to this Citadel server is broken.\n"
59 "Please re-connect and log in again.\n");
64 void timeout(int signum)
66 printf("\rConnection timed out.\n");
71 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
76 struct sockaddr_in sin;
79 memset(&sin, 0, sizeof(sin));
80 sin.sin_family = AF_INET;
82 pse = getservbyname(service, protocol);
84 sin.sin_port = pse->s_port;
86 else if (atoi(service) > 0) {
87 sin.sin_port = htons(atoi(service));
90 sin.sin_port = htons(defaultPort);
92 phe = gethostbyname(host);
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));
100 if ((ppe = getprotobyname(protocol)) == 0) {
101 fprintf(stderr, "Can't get %s protocol entry: %s\n",
102 protocol, strerror(errno));
105 if (!strcmp(protocol, "udp")) {
111 s = socket(PF_INET, type, ppe->p_proto);
113 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
116 signal(SIGALRM, timeout);
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));
125 signal(SIGALRM, SIG_IGN);
130 int uds_connectsock(char *sockpath)
132 struct sockaddr_un addr;
135 memset(&addr, 0, sizeof(addr));
136 addr.sun_family = AF_UNIX;
137 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
139 s = socket(AF_UNIX, SOCK_STREAM, 0);
141 fprintf(stderr, "Can't create socket: %s\n",
146 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
147 fprintf(stderr, "can't connect: %s\n",
158 * input binary data from socket
160 void serv_read(char *buf, int bytes)
165 while (len < bytes) {
166 rlen = read(serv_sock, &buf[len], bytes - len);
177 * send binary to server
179 void serv_write(char *buf, int nbytes)
181 int bytes_written = 0;
183 while (bytes_written < nbytes) {
184 retval = write(serv_sock, &buf[bytes_written],
185 nbytes - bytes_written);
190 bytes_written = bytes_written + retval;
197 * input string from socket - implemented in terms of serv_read()
199 void serv_gets(char *buf)
203 /* Read one character at a time.
206 serv_read(&buf[i], 1);
207 if (buf[i] == '\n' || i == (SIZ-1))
211 /* If we got a long line, discard characters until the newline.
214 while (buf[i] != '\n')
215 serv_read(&buf[i], 1);
217 /* Strip the trailing newline.
224 * send line to server - implemented in terms of serv_write()
226 void serv_puts(char *buf)
228 /* printf("< %s\n", buf); */
229 serv_write(buf, strlen(buf));
237 void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
244 strcpy(cithost, DEFAULT_HOST); /* default host */
245 strcpy(citport, DEFAULT_PORT); /* default port */
247 for (a = 0; a < argc; ++a) {
251 strcpy(cithost, argv[a]);
253 strcpy(citport, argv[a]);
256 fprintf(stderr,"%s: usage: ",argv[0]);
257 fprintf(stderr,"%s [host] [port] ",argv[0]);
262 if ((!strcmp(cithost, "localhost"))
263 || (!strcmp(cithost, "127.0.0.1"))) {
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);
276 serv_sock = connectsock(cithost, citport, "tcp", 504);
277 if (hostbuf != NULL) strcpy(hostbuf, cithost);
278 if (portbuf != NULL) strcpy(portbuf, citport);
283 * return the file descriptor of the server socket so we can select() on it.
292 * return one character