* Numerous memory management bugfixes.
[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                         close(WC->serv_sock);
149                         WC->serv_sock = (-1);
150                         WC->connected = 0;
151                         WC->logged_in = 0;
152                         memset(buf, 0, bytes);
153                         return;
154                 }
155                 len = len + rlen;
156         }
157 }
158
159
160 /*
161  * input string from pipe
162  */
163 void serv_gets(char *strbuf)
164 {
165         int ch, len;
166         char buf[2];
167
168         len = 0;
169         strcpy(strbuf, "");
170         do {
171                 serv_read(&buf[0], 1);
172                 ch = buf[0];
173                 strbuf[len++] = ch;
174         } while ((ch != 10) && (ch != 0) && (len < (SIZ-1)));
175         if (strbuf[len-1] == 10) strbuf[--len] = 0;
176         if (strbuf[len-1] == 13) strbuf[--len] = 0;
177         /* lprintf(9, ">%s\n", strbuf); */
178 }
179
180
181
182 /*
183  * send binary to server
184  */
185 void serv_write(char *buf, int nbytes)
186 {
187         int bytes_written = 0;
188         int retval;
189         while (bytes_written < nbytes) {
190                 retval = write(WC->serv_sock, &buf[bytes_written],
191                                nbytes - bytes_written);
192                 if (retval < 1) {
193                         lprintf(1, "Server connection broken: %s\n",
194                                 strerror(errno));
195                         close(WC->serv_sock);
196                         WC->serv_sock = (-1);
197                         WC->connected = 0;
198                         WC->logged_in = 0;
199                         return;
200                 }
201                 bytes_written = bytes_written + retval;
202         }
203 }
204
205
206 /*
207  * send line to server
208  */
209 void serv_puts(char *string)
210 {
211         char buf[SIZ];
212
213         sprintf(buf, "%s\n", string);
214         serv_write(buf, strlen(buf));
215 }
216
217
218 /*
219  * convenience function to send stuff to the server
220  */
221 void serv_printf(const char *format,...)
222 {
223         va_list arg_ptr;
224         char buf[SIZ];
225
226         va_start(arg_ptr, format);
227         vsnprintf(buf, sizeof buf, format, arg_ptr);
228         va_end(arg_ptr);
229
230         strcat(buf, "\n");
231         serv_write(buf, strlen(buf));
232         /* lprintf(9, "<%s", buf); */
233 }