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"
37 * If server_is_local is set to nonzero, the client assumes that it is running
38 * on the same computer as the server. Several things happen when this is
39 * the case, including the ability to map a specific tty to a particular login
40 * session in the "<W>ho is online" listing, the ability to run external
41 * programs, and the ability to download files directly off the disk without
42 * having to first fetch them from the server.
43 * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
44 * network session to the local machine) or 0 if the server is executing on
47 int server_is_local = 0;
50 #define INADDR_NONE 0xffffffff
55 void connection_died(void) {
57 "Your connection to this Citadel server is broken.\n"
58 "Please re-connect and log in again.\n");
63 void timeout(int signum)
65 printf("\rConnection timed out.\n");
70 int connectsock(char *host, char *service, char *protocol)
75 struct sockaddr_in sin;
78 memset(&sin, 0, sizeof(sin));
79 sin.sin_family = AF_INET;
81 pse = getservbyname(service, protocol);
83 sin.sin_port = pse->s_port;
84 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
85 fprintf(stderr, "Can't get %s service entry: %s\n",
86 service, strerror(errno));
89 phe = gethostbyname(host);
91 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
92 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
93 fprintf(stderr, "Can't get %s host entry: %s\n",
94 host, strerror(errno));
97 if ((ppe = getprotobyname(protocol)) == 0) {
98 fprintf(stderr, "Can't get %s protocol entry: %s\n",
99 protocol, strerror(errno));
102 if (!strcmp(protocol, "udp")) {
108 s = socket(PF_INET, type, ppe->p_proto);
110 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
113 signal(SIGALRM, timeout);
116 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
117 fprintf(stderr, "can't connect to %s.%s: %s\n",
118 host, service, strerror(errno));
122 signal(SIGALRM, SIG_IGN);
127 int uds_connectsock(char *sockpath)
129 struct sockaddr_un addr;
133 memset(&addr, 0, sizeof(addr));
134 addr.sun_family = AF_UNIX;
135 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
137 s = socket(AF_UNIX, SOCK_STREAM, 0);
139 fprintf(stderr, "Can't create socket: %s\n",
144 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
145 fprintf(stderr, "can't connect: %s\n",
155 * convert service and host entries into a six-byte numeric in the format
156 * expected by a SOCKS v4 server
158 void numericize(char *buf, char *host, char *service, char *protocol)
162 struct sockaddr_in sin;
164 memset(&sin, 0, sizeof(sin));
165 sin.sin_family = AF_INET;
167 pse = getservbyname(service, protocol);
169 sin.sin_port = pse->s_port;
170 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
171 fprintf(stderr, "Can't get %s service entry: %s\n",
172 service, strerror(errno));
175 buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
176 buf[0] = ((sin.sin_port) & 0x00FF);
178 phe = gethostbyname(host);
180 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
181 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
182 fprintf(stderr, "Can't get %s host entry: %s\n",
183 host, strerror(errno));
186 buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
187 buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
188 buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
189 buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF);
193 * input binary data from socket
195 void serv_read(char *buf, int bytes)
200 while (len < bytes) {
201 rlen = read(serv_sock, &buf[len], bytes - len);
212 * send binary to server
214 void serv_write(char *buf, int nbytes)
216 int bytes_written = 0;
218 while (bytes_written < nbytes) {
219 retval = write(serv_sock, &buf[bytes_written],
220 nbytes - bytes_written);
225 bytes_written = bytes_written + retval;
232 * input string from socket - implemented in terms of serv_read()
234 void serv_gets(char *buf)
238 /* Read one character at a time.
241 serv_read(&buf[i], 1);
242 if (buf[i] == '\n' || i == 255)
246 /* If we got a long line, discard characters until the newline.
249 while (buf[i] != '\n')
250 serv_read(&buf[i], 1);
252 /* Strip the trailing newline.
259 * send line to server - implemented in terms of serv_write()
261 void serv_puts(char *buf)
263 /* printf("< %s\n", buf); */
264 serv_write(buf, strlen(buf));
272 void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
284 strcpy(cithost, DEFAULT_HOST); /* default host */
285 strcpy(citport, DEFAULT_PORT); /* default port */
286 strcpy(socks4, ""); /* SOCKS v4 server */
289 for (a = 0; a < argc; ++a) {
292 } else if (!strcmp(argv[a], "-s")) {
293 strcpy(socks4, argv[++a]);
294 } else if (host_copied == 0) {
296 strcpy(cithost, argv[a]);
297 } else if (port_copied == 0) {
299 strcpy(citport, argv[a]);
303 fprintf(stderr,"%s: usage: ",argv[0]);
304 fprintf(stderr,"%s [host] [port] ",argv[0]);
305 fprintf(stderr,"[-s socks_server]\n");
311 if ((!strcmp(cithost, "localhost"))
312 || (!strcmp(cithost, "127.0.0.1")))
315 /* If we're using a unix domain socket we can do a bunch of stuff */
316 if (!strcmp(cithost, UDS)) {
317 sprintf(sockpath, "citadel.socket");
318 serv_sock = uds_connectsock(sockpath);
319 if (hostbuf != NULL) strcpy(hostbuf, cithost);
320 if (portbuf != NULL) strcpy(portbuf, sockpath);
324 /* if not using a SOCKS proxy server, make the connection directly */
325 if (strlen(socks4) == 0) {
326 serv_sock = connectsock(cithost, citport, "tcp");
327 if (hostbuf != NULL) strcpy(hostbuf, cithost);
328 if (portbuf != NULL) strcpy(portbuf, citport);
331 /* if using SOCKS, connect first to the proxy... */
332 serv_sock = connectsock(socks4, "1080", "tcp");
333 printf("Connected to SOCKS proxy at %s.\n", socks4);
334 printf("Attaching to server...\r");
337 snprintf(buf, sizeof buf, "%c%c",
339 1); /* method = connect */
342 numericize(buf, cithost, citport, "tcp");
343 serv_write(buf, 6); /* port and address */
345 p = (struct passwd *) getpwuid(getuid());
346 serv_write(p->pw_name, strlen(p->pw_name) + 1);
349 serv_read(buf, 8); /* get response */
352 printf("SOCKS server denied this proxy request.\n");
358 * return the file descriptor of the server socket so we can select() on it.
367 * return one character