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>
30 * If server_is_local is set to nonzero, the client assumes that it is running
31 * on the same computer as the server. Several things happen when this is
32 * the case, including the ability to map a specific tty to a particular login
33 * session in the "<W>ho is online" listing, the ability to run external
34 * programs, and the ability to download files directly off the disk without
35 * having to first fetch them from the server.
36 * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
37 * network session to the local machine) or 0 if the server is executing on
40 char server_is_local = 0;
43 #define INADDR_NONE 0xffffffff
51 printf("\rConnection timed out.\n");
55 int connectsock(host,service,protocol)
62 struct sockaddr_in sin;
65 bzero((char *)&sin,sizeof(sin));
66 sin.sin_family = AF_INET;
68 pse=getservbyname(service,protocol);
70 sin.sin_port = pse->s_port;
72 else if ((sin.sin_port = htons((u_short)atoi(service))) == 0) {
73 fprintf(stderr,"Can't get %s service entry: %s\n",
74 service,strerror(errno));
78 phe=gethostbyname(host);
80 memcpy(phe->h_addr,(char *)&sin.sin_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));
88 if ((ppe=getprotobyname(protocol))==0) {
89 fprintf(stderr,"Can't get %s protocol entry: %s\n",
90 protocol,strerror(errno));
94 if (!strcmp(protocol,"udp")) {
101 s = socket(PF_INET,type,ppe->p_proto);
103 fprintf(stderr,"Can't create socket: %s\n",strerror(errno));
108 signal(SIGALRM,timeout);
111 if (connect(s,(struct sockaddr *)&sin,sizeof(sin))<0) {
112 fprintf(stderr,"can't connect to %s.%s: %s\n",
113 host,service,strerror(errno));
118 signal(SIGALRM,SIG_IGN);
124 * convert service and host entries into a six-byte numeric in the format
125 * expected by a SOCKS v4 server
127 void numericize(buf,host,service,protocol)
134 struct sockaddr_in sin;
136 bzero((char *)&sin,sizeof(sin));
137 sin.sin_family = AF_INET;
139 pse=getservbyname(service,protocol);
141 sin.sin_port = pse->s_port;
143 else if ((sin.sin_port = htons((u_short)atoi(service))) == 0) {
144 fprintf(stderr,"Can't get %s service entry: %s\n",
145 service,strerror(errno));
149 buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
150 buf[0] = ((sin.sin_port) & 0x00FF);
152 phe=gethostbyname(host);
154 bcopy(phe->h_addr,(char *)&sin.sin_addr,phe->h_length);
156 else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
157 fprintf(stderr,"Can't get %s host entry: %s\n",
158 host,strerror(errno));
161 buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
162 buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
163 buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
164 buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF) ;
168 * input binary data from socket
170 void serv_read(buf,bytes)
177 rlen = read(serv_sock,&buf[len],bytes-len);
179 printf("\rNetwork error - connection terminated.\n");
180 printf("%s\n", strerror(errno));
189 * send binary to server
191 void serv_write(buf, nbytes)
194 int bytes_written = 0;
196 while (bytes_written < nbytes) {
197 retval = write(serv_sock, &buf[bytes_written],
198 nbytes - bytes_written);
200 printf("\rNetwork error - connection terminated.\n");
201 printf("%s\n", strerror(errno));
204 bytes_written = bytes_written + retval;
211 * input string from socket - implemented in terms of serv_read()
217 buf[strlen(buf) + 1] = 0;
218 if (strlen(buf) < 255) serv_read(&buf[strlen(buf)], 1);
219 } while (buf[strlen(buf)-1] != 10);
220 if (strlen(buf) > 0) buf[strlen(buf)-1] = 0;
221 /* printf("> %s\n", buf); */
226 * send line to server - implemented in terms of serv_write()
230 /* printf("< %s\n", buf); */
231 serv_write(buf, strlen(buf));
239 void attach_to_server(argc,argv)
243 char cithost[256]; int host_copied = 0;
244 char citport[256]; int port_copied = 0;
246 unsigned char buf[256];
249 strcpy(cithost,DEFAULT_HOST); /* default host */
250 strcpy(citport,DEFAULT_PORT); /* default port */
251 strcpy(socks4,""); /* SOCKS v4 server */
253 for (a = 0; a < argc; ++a) {
257 else if (!strcmp(argv[a],"-s")) {
258 strcpy(socks4,argv[++a]);
260 else if (host_copied == 0) {
262 strcpy(cithost,argv[a]);
264 else if (port_copied == 0) {
266 strcpy(citport,argv[a]);
271 fprintf(stderr,"%s: usage: ",argv[0]);
272 fprintf(stderr,"%s [host] [port] ",argv[0]);
273 fprintf(stderr,"[-s socks_server]\n");
280 if ( (!strcmp(cithost,"localhost"))
281 || (!strcmp(cithost,"127.0.0.1")) ) server_is_local = 1;
283 /* if not using a SOCKS proxy server, make the connection directly */
284 if (strlen(socks4)==0) {
285 serv_sock = connectsock(cithost,citport,"tcp");
289 /* if using SOCKS, connect first to the proxy... */
290 serv_sock = connectsock(socks4,"1080","tcp");
291 printf("Connected to SOCKS proxy at %s.\n",socks4);
292 printf("Attaching to server...\r");
297 1); /* method = connect */
300 numericize(buf,cithost,citport,"tcp");
301 serv_write(buf,6); /* port and address */
303 p = (struct passwd *) getpwuid(getuid());
304 serv_write(p->pw_name, strlen(p->pw_name)+1);
307 serv_read(buf,8); /* get response */
310 printf("SOCKS server denied this proxy request.\n");
317 * return the file descriptor of the server socket so we can select() on it.
325 * return one character