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>
28 #include "citadel_decls.h"
32 * If server_is_local is set to nonzero, the client assumes that it is running
33 * on the same computer as the server. Several things happen when this is
34 * the case, including the ability to map a specific tty to a particular login
35 * session in the "<W>ho is online" listing, the ability to run external
36 * programs, and the ability to download files directly off the disk without
37 * having to first fetch them from the server.
38 * Set the flag to 1 if this IPC is local (as is the case with pipes, or a
39 * network session to the local machine) or 0 if the server is executing on
42 char server_is_local = 0;
45 #define INADDR_NONE 0xffffffff
50 void timeout(int signum) {
51 printf("\rConnection timed out.\n");
55 int connectsock(char *host, char *service, char *protocol)
60 struct sockaddr_in sin;
63 bzero((char *)&sin,sizeof(sin));
64 sin.sin_family = AF_INET;
66 pse=getservbyname(service,protocol);
68 sin.sin_port = pse->s_port;
70 else if ((sin.sin_port = htons((u_short)atoi(service))) == 0) {
71 fprintf(stderr,"Can't get %s service entry: %s\n",
72 service,strerror(errno));
76 phe=gethostbyname(host);
78 memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
80 else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
81 fprintf(stderr,"Can't get %s host entry: %s\n",
82 host,strerror(errno));
86 if ((ppe=getprotobyname(protocol))==0) {
87 fprintf(stderr,"Can't get %s protocol entry: %s\n",
88 protocol,strerror(errno));
92 if (!strcmp(protocol,"udp")) {
99 s = socket(PF_INET,type,ppe->p_proto);
101 fprintf(stderr,"Can't create socket: %s\n",strerror(errno));
106 signal(SIGALRM,timeout);
109 if (connect(s,(struct sockaddr *)&sin,sizeof(sin))<0) {
110 fprintf(stderr,"can't connect to %s.%s: %s\n",
111 host,service,strerror(errno));
116 signal(SIGALRM,SIG_IGN);
122 * convert service and host entries into a six-byte numeric in the format
123 * expected by a SOCKS v4 server
125 void numericize(char *buf, char *host, char *service, char *protocol)
129 struct sockaddr_in sin;
131 bzero((char *)&sin,sizeof(sin));
132 sin.sin_family = AF_INET;
134 pse=getservbyname(service,protocol);
136 sin.sin_port = pse->s_port;
138 else if ((sin.sin_port = htons((u_short)atoi(service))) == 0) {
139 fprintf(stderr,"Can't get %s service entry: %s\n",
140 service,strerror(errno));
144 buf[1] = (((sin.sin_port) & 0xFF00) >> 8);
145 buf[0] = ((sin.sin_port) & 0x00FF);
147 phe=gethostbyname(host);
149 memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
151 else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
152 fprintf(stderr,"Can't get %s host entry: %s\n",
153 host,strerror(errno));
156 buf[5] = ((sin.sin_addr.s_addr) & 0xFF000000) >> 24;
157 buf[4] = ((sin.sin_addr.s_addr) & 0x00FF0000) >> 16;
158 buf[3] = ((sin.sin_addr.s_addr) & 0x0000FF00) >> 8;
159 buf[2] = ((sin.sin_addr.s_addr) & 0x000000FF) ;
163 * input binary data from socket
165 void serv_read(char *buf, int bytes)
171 rlen = read(serv_sock,&buf[len],bytes-len);
173 /* printf("\rNetwork error - connection terminated.\n");
174 printf("%s\n", strerror(errno)); */
183 * send binary to server
185 void serv_write(char *buf, int nbytes)
187 int bytes_written = 0;
189 while (bytes_written < nbytes) {
190 retval = write(serv_sock, &buf[bytes_written],
191 nbytes - bytes_written);
194 printf("\rNetwork error - connection terminated.\n");
195 printf("%s\n", strerror(errno)); */
198 bytes_written = bytes_written + retval;
205 * input string from socket - implemented in terms of serv_read()
207 void serv_gets(char *buf)
211 /* Read one character at a time.
214 serv_read(&buf[i], 1);
215 if (buf[i] == '\n' || i == 255)
219 /* If we got a long line, discard characters until the newline.
222 while (buf[i] != '\n')
223 serv_read(&buf[i], 1);
225 /* Strip the trailing newline.
232 * send line to server - implemented in terms of serv_write()
234 void serv_puts(char *buf)
236 /* printf("< %s\n", buf); */
237 serv_write(buf, strlen(buf));
245 void attach_to_server(int argc, char **argv)
248 char cithost[256]; int host_copied = 0;
249 char citport[256]; int port_copied = 0;
254 strcpy(cithost,DEFAULT_HOST); /* default host */
255 strcpy(citport,DEFAULT_PORT); /* default port */
256 strcpy(socks4,""); /* SOCKS v4 server */
258 for (a = 0; a < argc; ++a) {
262 else if (!strcmp(argv[a],"-s")) {
263 strcpy(socks4,argv[++a]);
265 else if (host_copied == 0) {
267 strcpy(cithost,argv[a]);
269 else if (port_copied == 0) {
271 strcpy(citport,argv[a]);
276 fprintf(stderr,"%s: usage: ",argv[0]);
277 fprintf(stderr,"%s [host] [port] ",argv[0]);
278 fprintf(stderr,"[-s socks_server]\n");
285 if ( (!strcmp(cithost,"localhost"))
286 || (!strcmp(cithost,"127.0.0.1")) ) server_is_local = 1;
288 /* if not using a SOCKS proxy server, make the connection directly */
289 if (strlen(socks4)==0) {
290 serv_sock = connectsock(cithost,citport,"tcp");
294 /* if using SOCKS, connect first to the proxy... */
295 serv_sock = connectsock(socks4,"1080","tcp");
296 printf("Connected to SOCKS proxy at %s.\n",socks4);
297 printf("Attaching to server...\r");
300 snprintf(buf,sizeof buf,"%c%c",
302 1); /* method = connect */
305 numericize(buf,cithost,citport,"tcp");
306 serv_write(buf,6); /* port and address */
308 p = (struct passwd *) getpwuid(getuid());
309 serv_write(p->pw_name, strlen(p->pw_name)+1);
312 serv_read(buf,8); /* get response */
315 printf("SOCKS server denied this proxy request.\n");
322 * return the file descriptor of the server socket so we can select() on it.
324 int getsockfd(void) {
330 * return one character
332 char serv_getc(void) {