* Began (but did not finish) applying GPL3+ declarations to each source file. This...
[citadel.git] / citadel / clientsocket.c
1 /*
2  * $Id$
3  *
4  * This module handles client-side sockets opened by the Citadel server (for
5  * the client side of Internet protocols, etc.)   It does _not_ handle client
6  * sockets for the Citadel client; for that you must look in ipc_c_tcp.c
7  * (which, uncoincidentally, bears a striking similarity to this file).
8  *
9  * Copyright (c) 1987-2009 by the citadel.org team
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 3 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #include "sysdep.h"
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netdb.h>
37 #include <string.h>
38 #include <pwd.h>
39 #include <errno.h>
40 #include <stdarg.h>
41 #include <libcitadel.h>
42 #include "citadel.h"
43 #include "server.h"
44 #ifndef HAVE_SNPRINTF
45 #include "snprintf.h"
46 #endif
47 #include "sysdep_decls.h"
48 #include "config.h"
49 #include "clientsocket.h"
50
51 #ifndef INADDR_NONE
52 #define INADDR_NONE 0xffffffff
53 #endif
54
55 int sock_connect(char *host, char *service, char *protocol)
56 {
57         struct hostent *phe;
58         struct servent *pse;
59         struct protoent *ppe;
60         struct sockaddr_in sin;
61         struct sockaddr_in egress_sin;
62         int s, type;
63
64         if ((host == NULL) || IsEmptyStr(host)) 
65                 return(-1);
66         if ((service == NULL) || IsEmptyStr(service)) 
67                 return(-1);
68         if ((protocol == NULL) || IsEmptyStr(protocol)) 
69                 return(-1);
70
71         memset(&sin, 0, sizeof(sin));
72         sin.sin_family = AF_INET;
73
74         pse = getservbyname(service, protocol);
75         if (pse) {
76                 sin.sin_port = pse->s_port;
77         } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
78                 CtdlLogPrintf(CTDL_CRIT, "Can't get %s service entry: %s\n",
79                         service, strerror(errno));
80                 return(-1);
81         }
82         phe = gethostbyname(host);
83         if (phe) {
84                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
85         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
86                 CtdlLogPrintf(CTDL_ERR, "Can't get %s host entry: %s\n",
87                         host, strerror(errno));
88                 return(-1);
89         }
90         if ((ppe = getprotobyname(protocol)) == 0) {
91                 CtdlLogPrintf(CTDL_CRIT, "Can't get %s protocol entry: %s\n",
92                         protocol, strerror(errno));
93                 return(-1);
94         }
95         if (!strcmp(protocol, "udp")) {
96                 type = SOCK_DGRAM;
97         } else {
98                 type = SOCK_STREAM;
99         }
100
101         s = socket(PF_INET, type, ppe->p_proto);
102         if (s < 0) {
103                 CtdlLogPrintf(CTDL_CRIT, "Can't create socket: %s\n", strerror(errno));
104                 return(-1);
105         }
106
107         /* If citserver is bound to a specific IP address on the host, make
108          * sure we use that address for outbound connections.
109          */
110         memset(&egress_sin, 0, sizeof(egress_sin));
111         egress_sin.sin_family = AF_INET;
112         if (!IsEmptyStr(config.c_ip_addr)) {
113                 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
114                 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
115                         egress_sin.sin_addr.s_addr = INADDR_ANY;
116                 }
117
118                 /* If this bind fails, no problem; we can still use INADDR_ANY */
119                 bind(s, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
120         }
121
122         /* Now try to connect to the remote host. */
123         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
124                 CtdlLogPrintf(CTDL_ERR, "Can't connect to %s:%s: %s\n",
125                         host, service, strerror(errno));
126                 close(s);
127                 return(-1);
128         }
129
130         return (s);
131 }
132
133
134
135 /*
136  * sock_read_to() - input binary data from socket, with a settable timeout.
137  * Returns the number of bytes read, or -1 for error.
138  * If keep_reading_until_full is nonzero, we keep reading until we get the number of requested bytes
139  */
140 int sock_read_to(int sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
141 {
142         int len,rlen;
143         fd_set rfds;
144         struct timeval tv;
145         int retval;
146
147         len = 0;
148         while (len<bytes) {
149                 FD_ZERO(&rfds);
150                 FD_SET(sock, &rfds);
151                 tv.tv_sec = timeout;
152                 tv.tv_usec = 0;
153
154                 retval = select(sock+1, &rfds, NULL, NULL, &tv);
155
156                 if (FD_ISSET(sock, &rfds) == 0) {       /* timed out */
157                         CtdlLogPrintf(CTDL_ERR, "sock_read() timed out.\n");
158                         return(-1);
159                 }
160
161                 rlen = read(sock, &buf[len], bytes-len);
162                 if (rlen<1) {
163                         CtdlLogPrintf(CTDL_ERR, "sock_read() failed: %s\n",
164                                 strerror(errno));
165                         return(-1);
166                 }
167                 len = len + rlen;
168                 if (!keep_reading_until_full) return(len);
169         }
170         return(bytes);
171 }
172
173
174 /*
175  * sock_read() - input binary data from socket.
176  * Returns the number of bytes read, or -1 for error.
177  */
178 INLINE int sock_read(int sock, char *buf, int bytes, int keep_reading_until_full)
179 {
180         return sock_read_to(sock, buf, bytes, CLIENT_TIMEOUT, keep_reading_until_full);
181 }
182
183
184 /*
185  * sock_write() - send binary to server.
186  * Returns the number of bytes written, or -1 for error.
187  */
188 int sock_write(int sock, char *buf, int nbytes)
189 {
190         int bytes_written = 0;
191         int retval;
192         while (bytes_written < nbytes) {
193                 retval = write(sock, &buf[bytes_written],
194                                nbytes - bytes_written);
195                 if (retval < 1) {
196                         return (-1);
197                 }
198                 bytes_written = bytes_written + retval;
199         }
200         return (bytes_written);
201 }
202
203
204
205 /*
206  * Input string from socket - implemented in terms of sock_read()
207  * 
208  */
209 int sock_getln(int sock, char *buf, int bufsize)
210 {
211         int i;
212
213         /* Read one character at a time.
214          */
215         for (i = 0;; i++) {
216                 if (sock_read(sock, &buf[i], 1, 1) < 0) return(-1);
217                 if (buf[i] == '\n' || i == (bufsize-1))
218                         break;
219         }
220
221         /* If we got a long line, discard characters until the newline.
222          */
223         if (i == (bufsize-1))
224                 while (buf[i] != '\n')
225                         if (sock_read(sock, &buf[i], 1, 1) < 0) return(-1);
226
227         /* Strip any trailing CR and LF characters.
228          */
229         buf[i] = 0;
230         while ( (i > 0)
231                 && ( (buf[i - 1]==13)
232                      || ( buf[i - 1]==10)) ) {
233                 i--;
234                 buf[i] = 0;
235         }
236         return(i);
237 }
238
239 /*
240  * Multiline version of sock_gets() ... this is a convenience function for
241  * client side protocol implementations.  It only returns the first line of
242  * a multiline response, discarding the rest.
243  */
244 int ml_sock_gets(int sock, char *buf) {
245         char bigbuf[1024];
246         int g;
247
248         g = sock_getln(sock, buf, SIZ);
249         if (g < 4) return(g);
250         if (buf[3] != '-') return(g);
251
252         do {
253                 g = sock_getln(sock, bigbuf, SIZ);
254                 if (g < 0) return(g);
255         } while ( (g >= 4) && (bigbuf[3] == '-') );
256
257         return(strlen(buf));
258 }
259
260
261 /*
262  * sock_puts() - send line to server - implemented in terms of serv_write()
263  * Returns the number of bytes written, or -1 for error.
264  */
265 int sock_puts(int sock, char *buf)
266 {
267         int i, j;
268
269         i = sock_write(sock, buf, strlen(buf));
270         if (i<0) return(i);
271         j = sock_write(sock, "\n", 1);
272         if (j<0) return(j);
273         return(i+j);
274 }