4 * Citadel/UX client/server IPC - client module using TCP/IP
10 #define DEFAULT_HOST "127.0.0.1"
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 char 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;
76 struct sockaddr_un sun;
80 if ( (!strcmp(protocol, "unix")) || (atoi(service)<0) ) {
81 memset(&sun, 0, sizeof(sun));
82 sun.sun_family = AF_UNIX;
83 sprintf(sun.sun_path, USOCKPATH, 0-atoi(service) );
85 s = socket(AF_UNIX, SOCK_STREAM, 0);
87 fprintf(stderr, "Can't create socket: %s\n",
92 if (connect(s, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
93 fprintf(stderr, "can't connect: %s\n",
102 /* otherwise it's a network connection */
104 memset(&sin, 0, sizeof(sin));
105 sin.sin_family = AF_INET;
107 pse = getservbyname(service, protocol);
109 sin.sin_port = pse->s_port;
110 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
111 fprintf(stderr, "Can't get %s service entry: %s\n",
112 service, strerror(errno));
115 phe = gethostbyname(host);
117 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
118 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
119 fprintf(stderr, "Can't get %s host entry: %s\n",
120 host, strerror(errno));
123 if ((ppe = getprotobyname(protocol)) == 0) {
124 fprintf(stderr, "Can't get %s protocol entry: %s\n",
125 protocol, strerror(errno));
128 if (!strcmp(protocol, "udp")) {
134 s = socket(PF_INET, type, ppe->p_proto);
136 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
139 signal(SIGALRM, timeout);
142 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
143 fprintf(stderr, "can't connect to %s.%s: %s\n",
144 host, service, strerror(errno));
148 signal(SIGALRM, SIG_IGN);
154 * convert service and host entries into a six-byte numeric in the format
155 * expected by a SOCKS v4 server
157 void numericize(char *buf, char *host, char *service, char *protocol)
161 struct sockaddr_in sin;
163 memset(&sin, 0, sizeof(sin));
164 sin.sin_family = AF_INET;
166 pse = getservbyname(service, protocol);
168 sin.sin_port = pse->s_port;
169 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
170 fprintf(stderr, "Can't get %s service entry: %s\n",
171 service, strerror(errno));
174 buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
175 buf[0] = ((sin.sin_port) & 0x00FF);
177 phe = gethostbyname(host);
179 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
180 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
181 fprintf(stderr, "Can't get %s host entry: %s\n",
182 host, strerror(errno));
185 buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
186 buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
187 buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
188 buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF);
192 * input binary data from socket
194 void serv_read(char *buf, int bytes)
199 while (len < bytes) {
200 rlen = read(serv_sock, &buf[len], bytes - len);
211 * send binary to server
213 void serv_write(char *buf, int nbytes)
215 int bytes_written = 0;
217 while (bytes_written < nbytes) {
218 retval = write(serv_sock, &buf[bytes_written],
219 nbytes - bytes_written);
224 bytes_written = bytes_written + retval;
231 * input string from socket - implemented in terms of serv_read()
233 void serv_gets(char *buf)
237 /* Read one character at a time.
240 serv_read(&buf[i], 1);
241 if (buf[i] == '\n' || i == 255)
245 /* If we got a long line, discard characters until the newline.
248 while (buf[i] != '\n')
249 serv_read(&buf[i], 1);
251 /* Strip the trailing newline.
258 * send line to server - implemented in terms of serv_write()
260 void serv_puts(char *buf)
262 /* printf("< %s\n", buf); */
263 serv_write(buf, strlen(buf));
271 void attach_to_server(int argc, char **argv)
282 strcpy(cithost, DEFAULT_HOST); /* default host */
283 strcpy(citport, DEFAULT_PORT); /* default port */
284 strcpy(socks4, ""); /* SOCKS v4 server */
286 for (a = 0; a < argc; ++a) {
289 } else if (!strcmp(argv[a], "-s")) {
290 strcpy(socks4, argv[++a]);
291 } else if (host_copied == 0) {
293 strcpy(cithost, argv[a]);
294 } else if (port_copied == 0) {
296 strcpy(citport, argv[a]);
300 fprintf(stderr,"%s: usage: ",argv[0]);
301 fprintf(stderr,"%s [host] [port] ",argv[0]);
302 fprintf(stderr,"[-s socks_server]\n");
309 if ((!strcmp(cithost, "localhost"))
310 || (!strcmp(cithost, "127.0.0.1")))
313 /* if not using a SOCKS proxy server, make the connection directly */
314 if (strlen(socks4) == 0) {
315 serv_sock = connectsock(cithost, citport, "tcp");
318 /* if using SOCKS, connect first to the proxy... */
319 serv_sock = connectsock(socks4, "1080", "tcp");
320 printf("Connected to SOCKS proxy at %s.\n", socks4);
321 printf("Attaching to server...\r");
324 snprintf(buf, sizeof buf, "%c%c",
326 1); /* method = connect */
329 numericize(buf, cithost, citport, "tcp");
330 serv_write(buf, 6); /* port and address */
332 p = (struct passwd *) getpwuid(getuid());
333 serv_write(p->pw_name, strlen(p->pw_name) + 1);
336 serv_read(buf, 8); /* get response */
339 printf("SOCKS server denied this proxy request.\n");
345 * return the file descriptor of the server socket so we can select() on it.
354 * return one character