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 timeout(int signum)
56 printf("\rConnection timed out.\n");
60 int connectsock(char *host, char *service, char *protocol)
65 struct sockaddr_in sin;
68 memset(&sin, 0, sizeof(sin));
69 sin.sin_family = AF_INET;
71 pse = getservbyname(service, protocol);
73 sin.sin_port = pse->s_port;
74 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
75 fprintf(stderr, "Can't get %s service entry: %s\n",
76 service, strerror(errno));
79 phe = gethostbyname(host);
81 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
82 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
83 fprintf(stderr, "Can't get %s host entry: %s\n",
84 host, strerror(errno));
87 if ((ppe = getprotobyname(protocol)) == 0) {
88 fprintf(stderr, "Can't get %s protocol entry: %s\n",
89 protocol, strerror(errno));
92 if (!strcmp(protocol, "udp")) {
98 s = socket(PF_INET, type, ppe->p_proto);
100 fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
103 signal(SIGALRM, timeout);
106 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
107 fprintf(stderr, "can't connect to %s.%s: %s\n",
108 host, service, strerror(errno));
112 signal(SIGALRM, SIG_IGN);
118 * convert service and host entries into a six-byte numeric in the format
119 * expected by a SOCKS v4 server
121 void numericize(char *buf, char *host, char *service, char *protocol)
125 struct sockaddr_in sin;
127 memset(&sin, 0, sizeof(sin));
128 sin.sin_family = AF_INET;
130 pse = getservbyname(service, protocol);
132 sin.sin_port = pse->s_port;
133 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
134 fprintf(stderr, "Can't get %s service entry: %s\n",
135 service, strerror(errno));
138 buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
139 buf[0] = ((sin.sin_port) & 0x00FF);
141 phe = gethostbyname(host);
143 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
144 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
145 fprintf(stderr, "Can't get %s host entry: %s\n",
146 host, strerror(errno));
149 buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
150 buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
151 buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
152 buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF);
156 * input binary data from socket
158 void serv_read(char *buf, int bytes)
163 while (len < bytes) {
164 rlen = read(serv_sock, &buf[len], bytes - len);
175 * send binary to server
177 void serv_write(char *buf, int nbytes)
179 int bytes_written = 0;
181 while (bytes_written < nbytes) {
182 retval = write(serv_sock, &buf[bytes_written],
183 nbytes - bytes_written);
188 bytes_written = bytes_written + retval;
195 * input string from socket - implemented in terms of serv_read()
197 void serv_gets(char *buf)
201 /* Read one character at a time.
204 serv_read(&buf[i], 1);
205 if (buf[i] == '\n' || i == 255)
209 /* If we got a long line, discard characters until the newline.
212 while (buf[i] != '\n')
213 serv_read(&buf[i], 1);
215 /* Strip the trailing newline.
222 * send line to server - implemented in terms of serv_write()
224 void serv_puts(char *buf)
226 /* printf("< %s\n", buf); */
227 serv_write(buf, strlen(buf));
235 void attach_to_server(int argc, char **argv)
246 strcpy(cithost, DEFAULT_HOST); /* default host */
247 strcpy(citport, DEFAULT_PORT); /* default port */
248 strcpy(socks4, ""); /* SOCKS v4 server */
250 for (a = 0; a < argc; ++a) {
253 } else if (!strcmp(argv[a], "-s")) {
254 strcpy(socks4, argv[++a]);
255 } else if (host_copied == 0) {
257 strcpy(cithost, argv[a]);
258 } else if (port_copied == 0) {
260 strcpy(citport, argv[a]);
264 fprintf(stderr,"%s: usage: ",argv[0]);
265 fprintf(stderr,"%s [host] [port] ",argv[0]);
266 fprintf(stderr,"[-s socks_server]\n");
273 if ((!strcmp(cithost, "localhost"))
274 || (!strcmp(cithost, "127.0.0.1")))
277 /* if not using a SOCKS proxy server, make the connection directly */
278 if (strlen(socks4) == 0) {
279 serv_sock = connectsock(cithost, citport, "tcp");
282 /* if using SOCKS, connect first to the proxy... */
283 serv_sock = connectsock(socks4, "1080", "tcp");
284 printf("Connected to SOCKS proxy at %s.\n", socks4);
285 printf("Attaching to server...\r");
288 snprintf(buf, sizeof buf, "%c%c",
290 1); /* method = connect */
293 numericize(buf, cithost, citport, "tcp");
294 serv_write(buf, 6); /* port and address */
296 p = (struct passwd *) getpwuid(getuid());
297 serv_write(p->pw_name, strlen(p->pw_name) + 1);
300 serv_read(buf, 8); /* get response */
303 printf("SOCKS server denied this proxy request.\n");
309 * return the file descriptor of the server socket so we can select() on it.
318 * return one character
334 * Are we still connected to a Citadel server?
336 int is_connected(void) {
337 return ( serv_sock >= 0 );