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) {
55 printf("\rConnection timed out.\n");
59 int connectsock(char *host, char *service, char *protocol)
64 struct sockaddr_in sin;
67 memset(&sin,0,sizeof(sin));
68 sin.sin_family = AF_INET;
70 pse=getservbyname(service,protocol);
72 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));
80 phe=gethostbyname(host);
82 memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
84 else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
85 fprintf(stderr,"Can't get %s host entry: %s\n",
86 host,strerror(errno));
90 if ((ppe=getprotobyname(protocol))==0) {
91 fprintf(stderr,"Can't get %s protocol entry: %s\n",
92 protocol,strerror(errno));
96 if (!strcmp(protocol,"udp")) {
103 s = socket(PF_INET,type,ppe->p_proto);
105 fprintf(stderr,"Can't create socket: %s\n",strerror(errno));
110 signal(SIGALRM,timeout);
113 if (connect(s,(struct sockaddr *)&sin,sizeof(sin))<0) {
114 fprintf(stderr,"can't connect to %s.%s: %s\n",
115 host,service,strerror(errno));
120 signal(SIGALRM,SIG_IGN);
126 * convert service and host entries into a six-byte numeric in the format
127 * expected by a SOCKS v4 server
129 void numericize(char *buf, char *host, char *service, char *protocol)
133 struct sockaddr_in sin;
135 memset(&sin,0,sizeof(sin));
136 sin.sin_family = AF_INET;
138 pse=getservbyname(service,protocol);
140 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));
148 buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
149 buf[0] = ((sin.sin_port) & 0x00FF);
151 phe=gethostbyname(host);
153 memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
155 else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
156 fprintf(stderr,"Can't get %s host entry: %s\n",
157 host,strerror(errno));
160 buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
161 buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
162 buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
163 buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF) ;
167 * input binary data from socket
169 void serv_read(char *buf, int bytes)
175 rlen = read(serv_sock,&buf[len],bytes-len);
177 /* printf("\rNetwork error - connection terminated.\n");
178 printf("%s\n", strerror(errno)); */
187 * send binary to server
189 void serv_write(char *buf, int nbytes)
191 int bytes_written = 0;
193 while (bytes_written < nbytes) {
194 retval = write(serv_sock, &buf[bytes_written],
195 nbytes - bytes_written);
198 printf("\rNetwork error - connection terminated.\n");
199 printf("%s\n", strerror(errno)); */
202 bytes_written = bytes_written + retval;
209 * input string from socket - implemented in terms of serv_read()
211 void serv_gets(char *buf)
215 /* Read one character at a time.
218 serv_read(&buf[i], 1);
219 if (buf[i] == '\n' || i == 255)
223 /* If we got a long line, discard characters until the newline.
226 while (buf[i] != '\n')
227 serv_read(&buf[i], 1);
229 /* Strip the trailing newline.
236 * send line to server - implemented in terms of serv_write()
238 void serv_puts(char *buf)
240 /* printf("< %s\n", buf); */
241 serv_write(buf, strlen(buf));
249 void attach_to_server(int argc, char **argv)
252 char cithost[256]; int host_copied = 0;
253 char citport[256]; int port_copied = 0;
258 strcpy(cithost,DEFAULT_HOST); /* default host */
259 strcpy(citport,DEFAULT_PORT); /* default port */
260 strcpy(socks4,""); /* SOCKS v4 server */
262 for (a = 0; a < argc; ++a) {
266 else if (!strcmp(argv[a],"-s")) {
267 strcpy(socks4,argv[++a]);
269 else if (host_copied == 0) {
271 strcpy(cithost,argv[a]);
273 else if (port_copied == 0) {
275 strcpy(citport,argv[a]);
280 fprintf(stderr,"%s: usage: ",argv[0]);
281 fprintf(stderr,"%s [host] [port] ",argv[0]);
282 fprintf(stderr,"[-s socks_server]\n");
289 if ( (!strcmp(cithost,"localhost"))
290 || (!strcmp(cithost,"127.0.0.1")) ) server_is_local = 1;
292 /* if not using a SOCKS proxy server, make the connection directly */
293 if (strlen(socks4)==0) {
294 serv_sock = connectsock(cithost,citport,"tcp");
298 /* if using SOCKS, connect first to the proxy... */
299 serv_sock = connectsock(socks4,"1080","tcp");
300 printf("Connected to SOCKS proxy at %s.\n",socks4);
301 printf("Attaching to server...\r");
304 snprintf(buf,sizeof buf,"%c%c",
306 1); /* method = connect */
309 numericize(buf,cithost,citport,"tcp");
310 serv_write(buf,6); /* port and address */
312 p = (struct passwd *) getpwuid(getuid());
313 serv_write(p->pw_name, strlen(p->pw_name)+1);
316 serv_read(buf,8); /* get response */
319 printf("SOCKS server denied this proxy request.\n");
326 * return the file descriptor of the server socket so we can select() on it.
328 int getsockfd(void) {
334 * return one character
336 char serv_getc(void) {