4 * TCP socket module for WebCit
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
22 char server_is_local = 0;
25 #define INADDR_NONE 0xffffffff
32 #ifndef DONT_PROTO_HTONS
39 printf("\rConnection timed out.\n");
43 int connectsock(host,service,protocol)
50 struct sockaddr_in sin;
53 bzero((char *)&sin,sizeof(sin));
54 sin.sin_family = AF_INET;
56 pse=getservbyname(service,protocol);
58 sin.sin_port = pse->s_port;
60 else if ((sin.sin_port = htons((u_short)atoi(service))) == 0) {
61 fprintf(stderr,"can't get %s service entry: %s\n",
62 service,strerror(errno));
66 phe=gethostbyname(host);
68 bcopy(phe->h_addr,(char *)&sin.sin_addr,phe->h_length);
70 else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
71 fprintf(stderr,"can't get %s host entry: %s\n",
72 host,strerror(errno));
76 if ((ppe=getprotobyname(protocol))==0) {
77 fprintf(stderr,"can't get %s protocol entry: %s\n",
78 protocol,strerror(errno));
82 if (!strcmp(protocol,"udp"))
87 s = socket(PF_INET,type,ppe->p_proto);
89 fprintf(stderr,"can't create socket: %s\n",strerror(errno));
94 signal(SIGALRM,timeout);
97 if (connect(s,(struct sockaddr *)&sin,sizeof(sin))<0) {
98 fprintf(stderr,"can't connect to %s.%s: %s\n",
99 host,service,strerror(errno));
104 signal(SIGALRM,SIG_IGN);
113 * Input binary data from socket
115 void serv_read(buf,bytes)
122 rlen = read(serv_sock,&buf[len],bytes-len);
124 printf("Network error - connection terminated.\n");
125 printf("%s\n", strerror(errno));
134 * input string from pipe
136 void serv_gets(strbuf)
144 serv_read(&buf[0], 1);
147 } while((ch!=10)&&(ch!=13)&&(ch!=0)&&(len<255));
154 * Attach to a Citadel server
156 void attach_to_server(argc,argv)
161 serv_sock = connectsock("localhost","citadel","tcp");
164 serv_sock = connectsock(argv[1],"citadel","tcp");
165 if ( (!strcmp(argv[1],"localhost"))
166 || (!strcmp(argv[1],"127.0.0.1")) ) server_is_local = 1;
168 if (argc>=3) serv_sock = connectsock(argv[1],argv[2],"tcp");
170 if (serv_sock < 0) exit(errno);
175 * send binary to server
177 void serv_write(buf, nbytes)
180 int bytes_written = 0;
182 while (bytes_written < nbytes) {
183 retval = write(serv_sock, &buf[bytes_written],
184 nbytes - bytes_written);
186 printf("Network error - connection terminated.\n");
187 printf("%s\n", strerror(errno));
190 bytes_written = bytes_written + retval;
196 * send line to server
198 void serv_puts(string)
202 sprintf(buf,"%s\n",string);
203 serv_write(buf, strlen(buf));
208 * convenience function to send stuff to the server
210 void serv_printf(const char *format, ...) {
214 va_start(arg_ptr, format);
215 vsprintf(buf, format, arg_ptr);
219 serv_write(buf, strlen(buf));