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