4 * Citadel/UX client/server IPC - client module using TCP/IP
12 #define DEFAULT_HOST UDS
13 #define DEFAULT_PORT "citadel"
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
32 #include "citadel_decls.h"
39 * If server_is_local is set to nonzero, the client assumes that it is running
40 * on the same computer as the server. Several things happen when this is
41 * the case, including the ability to map a specific tty to a particular login
42 * session in the "<W>ho is online" listing, the ability to run external
43 * programs, and the ability to download files directly off the disk without
44 * having to first fetch them from the server.
45 * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
46 * network session to the local machine) or 0 if the server is executing on
49 int server_is_local = 0;
52 #define INADDR_NONE 0xffffffff
57 void connection_died(void) {
59 "Your connection to this Citadel server is broken.\n"
60 "Please re-connect and log in again.\n");
65 void timeout(int signum)
67 printf("\rConnection timed out.\n");
72 int connectsock(char *host, char *service, char *protocol)
77 struct sockaddr_in sin;
80 memset(&sin, 0, sizeof(sin));
81 sin.sin_family = AF_INET;
83 pse = getservbyname(service, protocol);
85 sin.sin_port = pse->s_port;
86 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
87 fprintf(stderr, "Can't get %s service entry: %s\n",
88 service, strerror(errno));
91 phe = gethostbyname(host);
93 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
94 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
95 fprintf(stderr, "Can't get %s host entry: %s\n",
96 host, strerror(errno));
99 if ((ppe = getprotobyname(protocol)) == 0) {
100 fprintf(stderr, "Can't get %s protocol entry: %s\n",
101 protocol, strerror(errno));
104 if (!strcmp(protocol, "udp")) {
110 s = socket(PF_INET, type, ppe->p_proto);
112 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
115 signal(SIGALRM, timeout);
118 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
119 fprintf(stderr, "can't connect to %s.%s: %s\n",
120 host, service, strerror(errno));
124 signal(SIGALRM, SIG_IGN);
129 int uds_connectsock(char *sockpath)
131 struct sockaddr_un addr;
135 memset(&addr, 0, sizeof(addr));
136 addr.sun_family = AF_UNIX;
137 strncpy(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",
157 * convert service and host entries into a six-byte numeric in the format
158 * expected by a SOCKS v4 server
160 void numericize(char *buf, char *host, char *service, char *protocol)
164 struct sockaddr_in sin;
166 memset(&sin, 0, sizeof(sin));
167 sin.sin_family = AF_INET;
169 pse = getservbyname(service, protocol);
171 sin.sin_port = pse->s_port;
172 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
173 fprintf(stderr, "Can't get %s service entry: %s\n",
174 service, strerror(errno));
177 buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
178 buf[0] = ((sin.sin_port) & 0x00FF);
180 phe = gethostbyname(host);
182 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
183 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
184 fprintf(stderr, "Can't get %s host entry: %s\n",
185 host, strerror(errno));
188 buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
189 buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
190 buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
191 buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF);
195 * input binary data from socket
197 void serv_read(char *buf, int bytes)
202 while (len < bytes) {
203 rlen = read(serv_sock, &buf[len], bytes - len);
214 * send binary to server
216 void serv_write(char *buf, int nbytes)
218 int bytes_written = 0;
220 while (bytes_written < nbytes) {
221 retval = write(serv_sock, &buf[bytes_written],
222 nbytes - bytes_written);
227 bytes_written = bytes_written + retval;
234 * input string from socket - implemented in terms of serv_read()
236 void serv_gets(char *buf)
240 /* Read one character at a time.
243 serv_read(&buf[i], 1);
244 if (buf[i] == '\n' || i == 255)
248 /* If we got a long line, discard characters until the newline.
251 while (buf[i] != '\n')
252 serv_read(&buf[i], 1);
254 /* Strip the trailing newline.
261 * send line to server - implemented in terms of serv_write()
263 void serv_puts(char *buf)
265 /* printf("< %s\n", buf); */
266 serv_write(buf, strlen(buf));
274 void attach_to_server(int argc, char **argv, char *hostbuf, char *portbuf)
286 strcpy(cithost, DEFAULT_HOST); /* default host */
287 strcpy(citport, DEFAULT_PORT); /* default port */
288 strcpy(socks4, ""); /* SOCKS v4 server */
291 for (a = 0; a < argc; ++a) {
294 } else if (!strcmp(argv[a], "-s")) {
295 strcpy(socks4, argv[++a]);
296 } else if (host_copied == 0) {
298 strcpy(cithost, argv[a]);
299 } else if (port_copied == 0) {
301 strcpy(citport, argv[a]);
305 fprintf(stderr,"%s: usage: ",argv[0]);
306 fprintf(stderr,"%s [host] [port] ",argv[0]);
307 fprintf(stderr,"[-s socks_server]\n");
313 if ((!strcmp(cithost, "localhost"))
314 || (!strcmp(cithost, "127.0.0.1")))
317 /* If we're using a unix domain socket we can do a bunch of stuff */
318 if (!strcmp(cithost, UDS)) {
319 sprintf(sockpath, "%s/citadel.socket", BBSDIR);
320 serv_sock = uds_connectsock(sockpath);
321 if (hostbuf != NULL) strcpy(hostbuf, cithost);
322 if (portbuf != NULL) strcpy(portbuf, sockpath);
326 /* if not using a SOCKS proxy server, make the connection directly */
327 if (strlen(socks4) == 0) {
328 serv_sock = connectsock(cithost, citport, "tcp");
329 if (hostbuf != NULL) strcpy(hostbuf, cithost);
330 if (portbuf != NULL) strcpy(portbuf, citport);
333 /* if using SOCKS, connect first to the proxy... */
334 serv_sock = connectsock(socks4, "1080", "tcp");
335 printf("Connected to SOCKS proxy at %s.\n", socks4);
336 printf("Attaching to server...\r");
339 snprintf(buf, sizeof buf, "%c%c",
341 1); /* method = connect */
344 numericize(buf, cithost, citport, "tcp");
345 serv_write(buf, 6); /* port and address */
347 p = (struct passwd *) getpwuid(getuid());
348 serv_write(p->pw_name, strlen(p->pw_name) + 1);
351 serv_read(buf, 8); /* get response */
354 printf("SOCKS server denied this proxy request.\n");
360 * return the file descriptor of the server socket so we can select() on it.
369 * return one character