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