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 fprintf(stderr, "Server connection broken: %s\n",
136 * input string from pipe
138 void serv_gets(strbuf)
146 serv_read(&buf[0], 1);
149 } while((ch!=10)&&(ch!=13)&&(ch!=0)&&(len<255));
151 /* fprintf(stderr, ">%s\n", strbuf); */
157 * Attach to a Citadel server
159 void attach_to_server(argc,argv)
164 serv_sock = connectsock("localhost","citadel","tcp");
167 serv_sock = connectsock(argv[1],"citadel","tcp");
168 if ( (!strcmp(argv[1],"localhost"))
169 || (!strcmp(argv[1],"127.0.0.1")) ) server_is_local = 1;
171 if (argc>=3) serv_sock = connectsock(argv[1],argv[2],"tcp");
173 if (serv_sock < 0) exit(errno);
178 * send binary to server
180 void serv_write(buf, nbytes)
183 int bytes_written = 0;
185 while (bytes_written < nbytes) {
186 retval = write(serv_sock, &buf[bytes_written],
187 nbytes - bytes_written);
189 fprintf(stderr, "Server connection broken: %s\n",
195 bytes_written = bytes_written + retval;
201 * send line to server
203 void serv_puts(string)
207 sprintf(buf,"%s\n",string);
208 serv_write(buf, strlen(buf));
213 * convenience function to send stuff to the server
215 void serv_printf(const char *format, ...) {
219 va_start(arg_ptr, format);
220 vsprintf(buf, format, arg_ptr);
224 serv_write(buf, strlen(buf));
225 /* fprintf(stderr, "<%s", buf); */