4 * TCP socket module for WebCit
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
22 char server_is_local = 0;
25 #define INADDR_NONE 0xffffffff
33 void timeout(int signum) {
34 printf("\rConnection timed out.\n");
38 int connectsock(char *host, char *service, char *protocol)
43 struct sockaddr_in sin;
46 bzero((char *)&sin,sizeof(sin));
47 sin.sin_family = AF_INET;
49 pse=getservbyname(service,protocol);
51 sin.sin_port = pse->s_port;
53 else if ((sin.sin_port = htons((u_short)atoi(service))) == 0) {
54 fprintf(stderr,"can't get %s service entry\n", service);
58 phe=gethostbyname(host);
60 bcopy(phe->h_addr,(char *)&sin.sin_addr,phe->h_length);
62 else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
63 fprintf(stderr,"can't get %s host entry: %s\n",
64 host,strerror(errno));
68 if ((ppe=getprotobyname(protocol))==0) {
69 fprintf(stderr,"can't get %s protocol entry: %s\n",
70 protocol,strerror(errno));
74 if (!strcmp(protocol,"udp"))
79 s = socket(PF_INET,type,ppe->p_proto);
81 fprintf(stderr,"can't create socket: %s\n",strerror(errno));
86 signal(SIGALRM,timeout);
89 if (connect(s,(struct sockaddr *)&sin,sizeof(sin))<0) {
90 fprintf(stderr,"can't connect to %s.%s: %s\n",
91 host,service,strerror(errno));
96 signal(SIGALRM,SIG_IGN);
105 * Input binary data from socket
107 void serv_read(char *buf, int bytes)
113 rlen = read(serv_sock,&buf[len],bytes-len);
115 fprintf(stderr, "Server connection broken: %s\n",
127 * input string from pipe
129 void serv_gets(char *strbuf)
137 serv_read(&buf[0], 1);
140 } while((ch!=10)&&(ch!=13)&&(ch!=0)&&(len<255));
142 /* fprintf(stderr, ">%s\n", strbuf); */
148 * Attach to a Citadel server
150 void attach_to_server(int argc, char **argv)
154 serv_sock = connectsock("localhost","citadel","tcp");
157 serv_sock = connectsock(argv[1],"citadel","tcp");
158 if ( (!strcmp(argv[1],"localhost"))
159 || (!strcmp(argv[1],"127.0.0.1")) ) server_is_local = 1;
161 if (argc>=3) serv_sock = connectsock(argv[1],argv[2],"tcp");
163 if (serv_sock < 0) exit(errno);
168 * send binary to server
170 void serv_write(char *buf, int nbytes)
172 int bytes_written = 0;
174 while (bytes_written < nbytes) {
175 retval = write(serv_sock, &buf[bytes_written],
176 nbytes - bytes_written);
178 fprintf(stderr, "Server connection broken: %s\n",
184 bytes_written = bytes_written + retval;
190 * send line to server
192 void serv_puts(char *string)
196 sprintf(buf,"%s\n",string);
197 serv_write(buf, strlen(buf));
202 * convenience function to send stuff to the server
204 void serv_printf(const char *format, ...) {
208 va_start(arg_ptr, format);
209 vsprintf(buf, format, arg_ptr);
213 serv_write(buf, strlen(buf));
214 /* fprintf(stderr, "<%s", buf); */