* Cleaned up the rcs/cvs Id tags and leading comments at the top of some files
[citadel.git] / webcit / tcp_sockets.c
1 /*
2  * $Id$
3  * 
4  * TCP client socket module for WebCit
5  *
6  */
7
8 /*
9  * Uncomment this to log all communications with the Citadel server
10 #define SERV_TRACE 1
11  */
12
13 #include <ctype.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <signal.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <sys/socket.h>
22 #include <sys/time.h>
23 #include <limits.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <sys/un.h>
27 #include <netdb.h>
28 #include <string.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <stdarg.h>
32 #include <pthread.h>
33 #include <signal.h>
34 #include "webcit.h"
35 #include "webserver.h"
36
37
38 #ifndef INADDR_NONE
39 #define INADDR_NONE 0xffffffff
40 #endif
41
42 RETSIGTYPE timeout(int signum)
43 {
44         lprintf(1, "Connection timed out.\n");
45         exit(3);
46 }
47
48
49
50 /*
51  * Connect a unix domain socket
52  */
53 int uds_connectsock(char *sockpath)
54 {
55         struct sockaddr_un addr;
56         int s;
57
58         memset(&addr, 0, sizeof(addr));
59         addr.sun_family = AF_UNIX;
60         strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
61
62         s = socket(AF_UNIX, SOCK_STREAM, 0);
63         if (s < 0) {
64                 lprintf(1, "Can't create socket: %s\n",
65                         strerror(errno));
66                 return(-1);
67         }
68
69         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
70                 lprintf(1, "Can't connect: %s\n",
71                         strerror(errno));
72                 close(s);
73                 return(-1);
74         }
75
76         return s;
77 }
78
79
80 /*
81  * Connect a TCP/IP socket
82  */
83 int tcp_connectsock(char *host, char *service)
84 {
85         struct hostent *phe;
86         struct servent *pse;
87         struct protoent *ppe;
88         struct sockaddr_in sin;
89         int s;
90
91         memset(&sin, 0, sizeof(sin));
92         sin.sin_family = AF_INET;
93
94         pse = getservbyname(service, "tcp");
95         if (pse) {
96                 sin.sin_port = pse->s_port;
97         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
98                 lprintf(1, "Can't get %s service entry\n", service);
99                 return (-1);
100         }
101         phe = gethostbyname(host);
102         if (phe) {
103                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
104         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
105                 lprintf(1, "Can't get %s host entry: %s\n",
106                         host, strerror(errno));
107                 return (-1);
108         }
109         if ((ppe = getprotobyname("tcp")) == 0) {
110                 lprintf(1, "Can't get TCP protocol entry: %s\n",
111                         strerror(errno));
112                 return (-1);
113         }
114
115         s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
116         if (s < 0) {
117                 lprintf(1, "Can't create socket: %s\n", strerror(errno));
118                 return (-1);
119         }
120         signal(SIGALRM, timeout);
121         alarm(30);
122
123         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
124                 lprintf(1, "Can't connect to %s.%s: %s\n",
125                         host, service, strerror(errno));
126                 close(s);
127                 return (-1);
128         }
129         alarm(0);
130         signal(SIGALRM, SIG_IGN);
131
132         return (s);
133 }
134
135
136
137
138 /*
139  * Input binary data from socket
140  */
141 void serv_read(char *buf, int bytes)
142 {
143         int len, rlen;
144
145         len = 0;
146         while (len < bytes) {
147                 rlen = read(WC->serv_sock, &buf[len], bytes - len);
148                 if (rlen < 1) {
149                         lprintf(1, "Server connection broken: %s\n",
150                                 strerror(errno));
151                         close(WC->serv_sock);
152                         WC->serv_sock = (-1);
153                         WC->connected = 0;
154                         WC->logged_in = 0;
155                         memset(buf, 0, bytes);
156                         return;
157                 }
158                 len = len + rlen;
159         }
160 }
161
162
163 /*
164  * input string from pipe
165  */
166 void serv_gets(char *strbuf)
167 {
168         int ch, len;
169         char buf[2];
170
171         len = 0;
172         strcpy(strbuf, "");
173         do {
174                 serv_read(&buf[0], 1);
175                 ch = buf[0];
176                 strbuf[len++] = ch;
177         } while ((ch != 10) && (ch != 0) && (len < (SIZ-1)));
178         if (strbuf[len-1] == 10) strbuf[--len] = 0;
179         if (strbuf[len-1] == 13) strbuf[--len] = 0;
180 #ifdef SERV_TRACE
181         lprintf(9, ">%s\n", strbuf);
182 #endif
183 }
184
185
186
187 /*
188  * send binary to server
189  */
190 void serv_write(char *buf, int nbytes)
191 {
192         int bytes_written = 0;
193         int retval;
194         while (bytes_written < nbytes) {
195                 retval = write(WC->serv_sock, &buf[bytes_written],
196                                nbytes - bytes_written);
197                 if (retval < 1) {
198                         lprintf(1, "Server connection broken: %s\n",
199                                 strerror(errno));
200                         close(WC->serv_sock);
201                         WC->serv_sock = (-1);
202                         WC->connected = 0;
203                         WC->logged_in = 0;
204                         return;
205                 }
206                 bytes_written = bytes_written + retval;
207         }
208 }
209
210
211 /*
212  * send line to server
213  */
214 void serv_puts(char *string)
215 {
216         char buf[SIZ];
217
218 #ifdef SERV_TRACE
219         lprintf(9, "<%s\n", string);
220 #endif
221         sprintf(buf, "%s\n", string);
222         serv_write(buf, strlen(buf));
223 }
224
225
226 /*
227  * convenience function to send stuff to the server
228  */
229 void serv_printf(const char *format,...)
230 {
231         va_list arg_ptr;
232         char buf[SIZ];
233
234         va_start(arg_ptr, format);
235         vsnprintf(buf, sizeof buf, format, arg_ptr);
236         va_end(arg_ptr);
237
238         strcat(buf, "\n");
239         serv_write(buf, strlen(buf));
240 #ifdef SERV_TRACE
241         lprintf(9, "<%s", buf);
242 #endif
243 }