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>
29 #include "citadel_decls.h"
36 * If server_is_local is set to nonzero, the client assumes that it is running
37 * on the same computer as the server. Several things happen when this is
38 * the case, including the ability to map a specific tty to a particular login
39 * session in the "<W>ho is online" listing, the ability to run external
40 * programs, and the ability to download files directly off the disk without
41 * having to first fetch them from the server.
42 * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
43 * network session to the local machine) or 0 if the server is executing on
46 char server_is_local = 0;
49 #define INADDR_NONE 0xffffffff
54 void connection_died(void) {
56 "Your connection to this Citadel server is broken.\n"
57 "Please re-connect and log in again.\n");
62 void timeout(int signum)
64 printf("\rConnection timed out.\n");
69 int connectsock(char *host, char *service, char *protocol)
74 struct sockaddr_in sin;
77 memset(&sin, 0, sizeof(sin));
78 sin.sin_family = AF_INET;
80 pse = getservbyname(service, protocol);
82 sin.sin_port = pse->s_port;
83 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
84 fprintf(stderr, "Can't get %s service entry: %s\n",
85 service, strerror(errno));
88 phe = gethostbyname(host);
90 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
91 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
92 fprintf(stderr, "Can't get %s host entry: %s\n",
93 host, strerror(errno));
96 if ((ppe = getprotobyname(protocol)) == 0) {
97 fprintf(stderr, "Can't get %s protocol entry: %s\n",
98 protocol, strerror(errno));
101 if (!strcmp(protocol, "udp")) {
107 s = socket(PF_INET, type, ppe->p_proto);
109 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
112 signal(SIGALRM, timeout);
115 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
116 fprintf(stderr, "can't connect to %s.%s: %s\n",
117 host, service, strerror(errno));
121 signal(SIGALRM, SIG_IGN);
127 * convert service and host entries into a six-byte numeric in the format
128 * expected by a SOCKS v4 server
130 void numericize(char *buf, char *host, char *service, char *protocol)
134 struct sockaddr_in sin;
136 memset(&sin, 0, sizeof(sin));
137 sin.sin_family = AF_INET;
139 pse = getservbyname(service, protocol);
141 sin.sin_port = pse->s_port;
142 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
143 fprintf(stderr, "Can't get %s service entry: %s\n",
144 service, strerror(errno));
147 buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
148 buf[0] = ((sin.sin_port) & 0x00FF);
150 phe = gethostbyname(host);
152 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
153 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
154 fprintf(stderr, "Can't get %s host entry: %s\n",
155 host, strerror(errno));
158 buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
159 buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
160 buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
161 buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF);
165 * input binary data from socket
167 void serv_read(char *buf, int bytes)
172 while (len < bytes) {
173 rlen = read(serv_sock, &buf[len], bytes - len);
184 * send binary to server
186 void serv_write(char *buf, int nbytes)
188 int bytes_written = 0;
190 while (bytes_written < nbytes) {
191 retval = write(serv_sock, &buf[bytes_written],
192 nbytes - bytes_written);
197 bytes_written = bytes_written + retval;
204 * input string from socket - implemented in terms of serv_read()
206 void serv_gets(char *buf)
210 /* Read one character at a time.
213 serv_read(&buf[i], 1);
214 if (buf[i] == '\n' || i == 255)
218 /* If we got a long line, discard characters until the newline.
221 while (buf[i] != '\n')
222 serv_read(&buf[i], 1);
224 /* Strip the trailing newline.
231 * send line to server - implemented in terms of serv_write()
233 void serv_puts(char *buf)
235 /* printf("< %s\n", buf); */
236 serv_write(buf, strlen(buf));
244 void attach_to_server(int argc, char **argv)
255 strcpy(cithost, DEFAULT_HOST); /* default host */
256 strcpy(citport, DEFAULT_PORT); /* default port */
257 strcpy(socks4, ""); /* SOCKS v4 server */
259 for (a = 0; a < argc; ++a) {
262 } else if (!strcmp(argv[a], "-s")) {
263 strcpy(socks4, argv[++a]);
264 } else if (host_copied == 0) {
266 strcpy(cithost, argv[a]);
267 } else if (port_copied == 0) {
269 strcpy(citport, argv[a]);
273 fprintf(stderr,"%s: usage: ",argv[0]);
274 fprintf(stderr,"%s [host] [port] ",argv[0]);
275 fprintf(stderr,"[-s socks_server]\n");
282 if ((!strcmp(cithost, "localhost"))
283 || (!strcmp(cithost, "127.0.0.1")))
286 /* if not using a SOCKS proxy server, make the connection directly */
287 if (strlen(socks4) == 0) {
288 serv_sock = connectsock(cithost, citport, "tcp");
291 /* if using SOCKS, connect first to the proxy... */
292 serv_sock = connectsock(socks4, "1080", "tcp");
293 printf("Connected to SOCKS proxy at %s.\n", socks4);
294 printf("Attaching to server...\r");
297 snprintf(buf, sizeof buf, "%c%c",
299 1); /* method = connect */
302 numericize(buf, cithost, citport, "tcp");
303 serv_write(buf, 6); /* port and address */
305 p = (struct passwd *) getpwuid(getuid());
306 serv_write(p->pw_name, strlen(p->pw_name) + 1);
309 serv_read(buf, 8); /* get response */
312 printf("SOCKS server denied this proxy request.\n");
318 * return the file descriptor of the server socket so we can select() on it.
327 * return one character