* converted to autoconf and began port to Digital UNIX
[citadel.git] / webcit / tcp_sockets.c
1 /*
2  * tcp_sockets.c
3  * 
4  * TCP socket module for WebCit
5  *
6  * $Id$
7  */
8
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <netdb.h>
18 #include <string.h>
19 #include <stdarg.h>
20 #include "webcit.h"
21
22 char server_is_local = 0;
23
24 #ifndef INADDR_NONE
25 #define INADDR_NONE 0xffffffff
26 #endif
27
28 extern int errno;
29
30 int serv_sock;
31
32
33 void timeout(int signum) {
34         printf("\rConnection timed out.\n");
35         exit(3);
36         }
37
38 int connectsock(char *host, char *service, char *protocol)
39 {
40         struct hostent *phe;
41         struct servent *pse;
42         struct protoent *ppe;
43         struct sockaddr_in sin;
44         int s,type;
45
46         bzero((char *)&sin,sizeof(sin));
47         sin.sin_family = AF_INET;
48
49         pse=getservbyname(service,protocol);
50         if (pse) {
51                 sin.sin_port = pse->s_port;
52                 }
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));
56                 return(-1);
57                 }
58         
59         phe=gethostbyname(host);
60         if (phe) {
61                 bcopy(phe->h_addr,(char *)&sin.sin_addr,phe->h_length);
62                 }
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));
66                 return(-1);
67                 }
68
69         if ((ppe=getprotobyname(protocol))==0) {
70                 fprintf(stderr,"can't get %s protocol entry: %s\n",
71                         protocol,strerror(errno));
72                 return(-1);
73                 }
74
75         if (!strcmp(protocol,"udp"))
76                 type = SOCK_DGRAM;
77         else
78                 type = SOCK_STREAM;
79
80         s = socket(PF_INET,type,ppe->p_proto);
81         if (s<0) {
82                 fprintf(stderr,"can't create socket: %s\n",strerror(errno));
83                 return(-1);
84                 }
85
86
87         signal(SIGALRM,timeout);
88         alarm(30);
89
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));
93                 return(-1);
94                 }
95
96         alarm(0);
97         signal(SIGALRM,SIG_IGN);
98
99         return(s);
100         }
101
102
103
104
105 /*
106  * Input binary data from socket
107  */
108 void serv_read(char *buf, int bytes)
109 {
110         int len,rlen;
111
112         len = 0;
113         while(len<bytes) {
114                 rlen = read(serv_sock,&buf[len],bytes-len);
115                 if (rlen<1) {
116                         fprintf(stderr, "Server connection broken: %s\n",
117                                 strerror(errno));
118                         connected = 0;
119                         logged_in = 0;
120                         return;
121                         }
122                 len = len + rlen;
123                 }
124         }
125
126
127 /*
128  * input string from pipe
129  */
130 void serv_gets(char *strbuf)
131 {
132         int ch,len;
133         char buf[2];
134
135         len = 0;
136         strcpy(strbuf,"");
137         do {
138                 serv_read(&buf[0], 1);
139                 ch = buf[0];
140                 strbuf[len++] = ch;
141                 } while((ch!=10)&&(ch!=13)&&(ch!=0)&&(len<255));
142         strbuf[len-1] = 0;
143         /* fprintf(stderr, ">%s\n", strbuf); */
144         }
145
146
147
148 /*
149  * Attach to a Citadel server
150  */
151 void attach_to_server(int argc, char **argv)
152 {
153         if (argc==1) {
154                 server_is_local = 1;
155                 serv_sock = connectsock("localhost","citadel","tcp");
156                 }
157         if (argc==2) {
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;
161                 }
162         if (argc>=3) serv_sock = connectsock(argv[1],argv[2],"tcp");
163
164         if (serv_sock < 0) exit(errno);
165         }
166
167
168 /*
169  * send binary to server
170  */
171 void serv_write(char *buf, int nbytes)
172 {
173         int bytes_written = 0;
174         int retval;
175         while (bytes_written < nbytes) {
176                 retval = write(serv_sock, &buf[bytes_written],
177                         nbytes - bytes_written);
178                 if (retval < 1) {
179                         fprintf(stderr, "Server connection broken: %s\n",
180                                 strerror(errno));
181                         connected = 0;
182                         logged_in = 0;
183                         return;
184                         }
185                 bytes_written = bytes_written + retval;
186                 }
187         }
188
189
190 /*
191  * send line to server
192  */
193 void serv_puts(char *string)
194 {
195         char buf[256];
196
197         sprintf(buf,"%s\n",string);
198         serv_write(buf, strlen(buf));
199         }
200
201
202 /*
203  * convenience function to send stuff to the server
204  */
205 void serv_printf(const char *format, ...) {   
206         va_list arg_ptr;   
207         char buf[256];
208
209         va_start(arg_ptr, format);   
210         vsprintf(buf, format, arg_ptr);   
211         va_end(arg_ptr);   
212
213         strcat(buf, "\n");
214         serv_write(buf, strlen(buf));
215         /* fprintf(stderr, "<%s", buf); */
216         }
217
218