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 void logoff(int code);
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 CtdlInternalTCPtimeout(int signum) {
51 printf("\rConnection timed out.\n");
55 int CtdlInternalTCPConnectSock(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((char *)&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,CtdlInternalTCPtimeout);
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 CtdlInternalTCPnumericize(unsigned 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 bcopy(phe->h_addr,(char *)&sin.sin_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);
193 printf("\rNetwork error - connection terminated.\n");
194 printf("%s\n", strerror(errno));
197 bytes_written = bytes_written + retval;
204 * input string from socket - implemented in terms of serv_read()
206 void serv_gets(char *buf)
210 buf[strlen(buf) + 1] = 0;
211 if (strlen(buf) < 255) serv_read(&buf[strlen(buf)], 1);
212 } while (buf[strlen(buf)-1] != 10);
213 if (strlen(buf) > 0) buf[strlen(buf)-1] = 0;
214 /* printf("> %s\n", buf); */
219 * send line to server - implemented in terms of serv_write()
221 void serv_puts(char *buf)
223 /* printf("< %s\n", buf); */
224 serv_write(buf, strlen(buf));
232 void attach_to_server(int argc, char **argv)
235 char cithost[256]; int host_copied = 0;
236 char citport[256]; int port_copied = 0;
238 unsigned char buf[256];
241 strcpy(cithost,DEFAULT_HOST); /* default host */
242 strcpy(citport,DEFAULT_PORT); /* default port */
243 strcpy(socks4,""); /* SOCKS v4 server */
245 for (a = 0; a < argc; ++a) {
249 else if (!strcmp(argv[a],"-s")) {
250 strcpy(socks4,argv[++a]);
252 else if (host_copied == 0) {
254 strcpy(cithost,argv[a]);
256 else if (port_copied == 0) {
258 strcpy(citport,argv[a]);
263 fprintf(stderr,"%s: usage: ",argv[0]);
264 fprintf(stderr,"%s [host] [port] ",argv[0]);
265 fprintf(stderr,"[-s socks_server]\n");
272 if ( (!strcmp(cithost,"localhost"))
273 || (!strcmp(cithost,"127.0.0.1")) ) server_is_local = 1;
275 /* if not using a SOCKS proxy server, make the connection directly */
276 if (strlen(socks4)==0) {
277 serv_sock = CtdlInternalTCPConnectSock(cithost,citport,"tcp");
281 /* if using SOCKS, connect first to the proxy... */
282 serv_sock = CtdlInternalTCPConnectSock(socks4,"1080","tcp");
283 printf("Connected to SOCKS proxy at %s.\n",socks4);
284 printf("Attaching to server...\r");
289 1); /* method = connect */
292 CtdlInternalTCPnumericize(buf,cithost,citport,"tcp");
293 serv_write(buf,6); /* port and address */
295 p = (struct passwd *) getpwuid(getuid());
296 serv_write(p->pw_name, strlen(p->pw_name)+1);
299 serv_read(buf,8); /* get response */
302 printf("SOCKS server denied this proxy request.\n");
309 * return the file descriptor of the server socket so we can select() on it.
311 int getsockfd(void) {
317 * return one character
319 char serv_getc(void) {