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: %s\n",
55 service,strerror(errno));
59 phe=gethostbyname(host);
61 bcopy(phe->h_addr,(char *)&sin.sin_addr,phe->h_length);
63 else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
64 fprintf(stderr,"can't get %s host entry: %s\n",
65 host,strerror(errno));
69 if ((ppe=getprotobyname(protocol))==0) {
70 fprintf(stderr,"can't get %s protocol entry: %s\n",
71 protocol,strerror(errno));
75 if (!strcmp(protocol,"udp"))
80 s = socket(PF_INET,type,ppe->p_proto);
82 fprintf(stderr,"can't create socket: %s\n",strerror(errno));
87 signal(SIGALRM,timeout);
90 if (connect(s,(struct sockaddr *)&sin,sizeof(sin))<0) {
91 fprintf(stderr,"can't connect to %s.%s: %s\n",
92 host,service,strerror(errno));
97 signal(SIGALRM,SIG_IGN);
106 * Input binary data from socket
108 void serv_read(char *buf, int bytes)
114 rlen = read(serv_sock,&buf[len],bytes-len);
116 fprintf(stderr, "Server connection broken: %s\n",
128 * input string from pipe
130 void serv_gets(char *strbuf)
138 serv_read(&buf[0], 1);
141 } while((ch!=10)&&(ch!=13)&&(ch!=0)&&(len<255));
143 /* fprintf(stderr, ">%s\n", strbuf); */
149 * Attach to a Citadel server
151 void attach_to_server(int argc, char **argv)
155 serv_sock = connectsock("localhost","citadel","tcp");
158 serv_sock = connectsock(argv[1],"citadel","tcp");
159 if ( (!strcmp(argv[1],"localhost"))
160 || (!strcmp(argv[1],"127.0.0.1")) ) server_is_local = 1;
162 if (argc>=3) serv_sock = connectsock(argv[1],argv[2],"tcp");
164 if (serv_sock < 0) exit(errno);
169 * send binary to server
171 void serv_write(char *buf, int nbytes)
173 int bytes_written = 0;
175 while (bytes_written < nbytes) {
176 retval = write(serv_sock, &buf[bytes_written],
177 nbytes - bytes_written);
179 fprintf(stderr, "Server connection broken: %s\n",
185 bytes_written = bytes_written + retval;
191 * send line to server
193 void serv_puts(char *string)
197 sprintf(buf,"%s\n",string);
198 serv_write(buf, strlen(buf));
203 * convenience function to send stuff to the server
205 void serv_printf(const char *format, ...) {
209 va_start(arg_ptr, format);
210 vsprintf(buf, format, arg_ptr);
214 serv_write(buf, strlen(buf));
215 /* fprintf(stderr, "<%s", buf); */