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>
27 #include "citadel_decls.h"
31 * If server_is_local is set to nonzero, the client assumes that it is running
32 * on the same computer as the server. Several things happen when this is
33 * the case, including the ability to map a specific tty to a particular login
34 * session in the "<W>ho is online" listing, the ability to run external
35 * programs, and the ability to download files directly off the disk without
36 * having to first fetch them from the server.
37 * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
38 * network session to the local machine) or 0 if the server is executing on
41 char server_is_local = 0;
44 #define INADDR_NONE 0xffffffff
49 void timeout(int signum) {
50 printf("\rConnection timed out.\n");
54 int connectsock(char *host, char *service, char *protocol)
59 struct sockaddr_in sin;
62 bzero((char *)&sin,sizeof(sin));
63 sin.sin_family = AF_INET;
65 pse=getservbyname(service,protocol);
67 sin.sin_port = pse->s_port;
69 else if ((sin.sin_port = htons((u_short)atoi(service))) == 0) {
70 fprintf(stderr,"Can't get %s service entry: %s\n",
71 service,strerror(errno));
75 phe=gethostbyname(host);
77 memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
79 else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
80 fprintf(stderr,"Can't get %s host entry: %s\n",
81 host,strerror(errno));
85 if ((ppe=getprotobyname(protocol))==0) {
86 fprintf(stderr,"Can't get %s protocol entry: %s\n",
87 protocol,strerror(errno));
91 if (!strcmp(protocol,"udp")) {
98 s = socket(PF_INET,type,ppe->p_proto);
100 fprintf(stderr,"Can't create socket: %s\n",strerror(errno));
105 signal(SIGALRM,timeout);
108 if (connect(s,(struct sockaddr *)&sin,sizeof(sin))<0) {
109 fprintf(stderr,"can't connect to %s.%s: %s\n",
110 host,service,strerror(errno));
115 signal(SIGALRM,SIG_IGN);
121 * convert service and host entries into a six-byte numeric in the format
122 * expected by a SOCKS v4 server
124 void numericize(char *buf, char *host, char *service, char *protocol)
128 struct sockaddr_in sin;
130 bzero((char *)&sin,sizeof(sin));
131 sin.sin_family = AF_INET;
133 pse=getservbyname(service,protocol);
135 sin.sin_port = pse->s_port;
137 else if ((sin.sin_port = htons((u_short)atoi(service))) == 0) {
138 fprintf(stderr,"Can't get %s service entry: %s\n",
139 service,strerror(errno));
143 buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
144 buf[0] = ((sin.sin_port) & 0x00FF);
146 phe=gethostbyname(host);
148 memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
150 else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
151 fprintf(stderr,"Can't get %s host entry: %s\n",
152 host,strerror(errno));
155 buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
156 buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
157 buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
158 buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF) ;
162 * input binary data from socket
164 void serv_read(char *buf, int bytes)
170 rlen = read(serv_sock,&buf[len],bytes-len);
172 printf("\rNetwork error - connection terminated.\n");
173 printf("%s\n", strerror(errno));
182 * send binary to server
184 void serv_write(char *buf, int nbytes)
186 int bytes_written = 0;
188 while (bytes_written < nbytes) {
189 retval = write(serv_sock, &buf[bytes_written],
190 nbytes - bytes_written);
192 printf("\rNetwork error - connection terminated.\n");
193 printf("%s\n", strerror(errno));
196 bytes_written = bytes_written + retval;
203 * input string from socket - implemented in terms of serv_read()
205 void serv_gets(char *buf)
209 /* Read one character at a time.
212 serv_read(&buf[i], 1);
213 if (buf[i] == '\n' || i == 255)
217 /* If we got a long line, discard characters until the newline.
220 while (buf[i] != '\n')
221 serv_read(&buf[i], 1);
223 /* Strip the trailing newline.
230 * send line to server - implemented in terms of serv_write()
232 void serv_puts(char *buf)
234 /* printf("< %s\n", buf); */
235 serv_write(buf, strlen(buf));
243 void attach_to_server(int argc, char **argv)
246 char cithost[256]; int host_copied = 0;
247 char citport[256]; int port_copied = 0;
252 strcpy(cithost,DEFAULT_HOST); /* default host */
253 strcpy(citport,DEFAULT_PORT); /* default port */
254 strcpy(socks4,""); /* SOCKS v4 server */
256 for (a = 0; a < argc; ++a) {
260 else if (!strcmp(argv[a],"-s")) {
261 strcpy(socks4,argv[++a]);
263 else if (host_copied == 0) {
265 strcpy(cithost,argv[a]);
267 else if (port_copied == 0) {
269 strcpy(citport,argv[a]);
274 fprintf(stderr,"%s: usage: ",argv[0]);
275 fprintf(stderr,"%s [host] [port] ",argv[0]);
276 fprintf(stderr,"[-s socks_server]\n");
283 if ( (!strcmp(cithost,"localhost"))
284 || (!strcmp(cithost,"127.0.0.1")) ) server_is_local = 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");
292 /* if using SOCKS, connect first to the proxy... */
293 serv_sock = connectsock(socks4,"1080","tcp");
294 printf("Connected to SOCKS proxy at %s.\n",socks4);
295 printf("Attaching to server...\r");
300 1); /* method = connect */
303 numericize(buf,cithost,citport,"tcp");
304 serv_write(buf,6); /* port and address */
306 p = (struct passwd *) getpwuid(getuid());
307 serv_write(p->pw_name, strlen(p->pw_name)+1);
310 serv_read(buf,8); /* get response */
313 printf("SOCKS server denied this proxy request.\n");
320 * return the file descriptor of the server socket so we can select() on it.
322 int getsockfd(void) {
328 * return one character
330 char serv_getc(void) {