Mailing list header changes (fuck you Google)
[citadel.git] / citadel / clientsocket.c
1 /*
2  * This module handles client-side sockets opened by the Citadel server (for
3  * the client side of Internet protocols, etc.)   It does _not_ handle client
4  * sockets for the Citadel client; for that you must look in ipc_c_tcp.c
5  * (which, uncoincidentally, bears a striking similarity to this file).
6  *
7  * Copyright (c) 1987-2017 by the citadel.org team
8  *
9  * This program is open source software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License, version 3.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <netdb.h>
21 #include <stdio.h>
22 #include <libcitadel.h>
23 #include "ctdl_module.h"
24 #include "clientsocket.h"
25
26 int sock_connect(char *host, char *service)
27 {
28         struct in6_addr serveraddr;
29         struct addrinfo hints;
30         struct addrinfo *res = NULL;
31         struct addrinfo *ai = NULL;
32         int rc = (-1);
33         int sock = (-1);
34
35         if ((host == NULL) || IsEmptyStr(host))
36                 return (-1);
37         if ((service == NULL) || IsEmptyStr(service))
38                 return (-1);
39
40         memset(&hints, 0x00, sizeof(hints));
41         hints.ai_flags = AI_NUMERICSERV;
42         hints.ai_family = AF_UNSPEC;
43         hints.ai_socktype = SOCK_STREAM;
44
45         /*
46          * Handle numeric IPv4 and IPv6 addresses
47          */
48         rc = inet_pton(AF_INET, host, &serveraddr);
49         if (rc == 1) {                                          /* dotted quad */
50                 hints.ai_family = AF_INET;
51                 hints.ai_flags |= AI_NUMERICHOST;
52         } else {
53                 rc = inet_pton(AF_INET6, host, &serveraddr);
54                 if (rc == 1) {                                  /* IPv6 address */
55                         hints.ai_family = AF_INET6;
56                         hints.ai_flags |= AI_NUMERICHOST;
57                 }
58         }
59
60         /* Begin the connection process */
61
62         rc = getaddrinfo(host, service, &hints, &res);
63         if (rc != 0) {
64                 syslog(LOG_ERR, "%s: %s", host, gai_strerror(rc));
65                 return(-1);
66         }
67
68         /*
69          * Try all available addresses until we connect to one or until we run out.
70          */
71         for (ai = res; ai != NULL; ai = ai->ai_next) {
72                 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
73                 if (sock < 0) {
74                         syslog(LOG_ERR, "%s: %m", host);
75                         freeaddrinfo(res);
76                         return(-1);
77                 }
78                 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
79                 if (rc >= 0) {
80                         freeaddrinfo(res);
81                         return(sock);
82                 }
83                 else {
84                         syslog(LOG_ERR, "%s: %m", host);
85                         close(sock);
86                 }
87         }
88         freeaddrinfo(res);
89         return(-1);
90 }
91
92
93
94 /*
95  * Read data from the client socket.
96  *
97  * sock         socket fd to read from
98  * buf          buffer to read into 
99  * bytes        number of bytes to read
100  * timeout      Number of seconds to wait before timing out
101  *
102  * Possible return values:
103  *      1       Requested number of bytes has been read.
104  *      0       Request timed out.
105  *      -1      Connection is broken, or other error.
106  */
107 int socket_read_blob(int *Socket, StrBuf *Target, int bytes, int timeout)
108 {
109         const char *Error;
110         int retval = 0;
111
112         retval = StrBufReadBLOBBuffered(Target, CC->SBuf.Buf, &CC->SBuf.ReadWritePointer, Socket, 1, bytes, O_TERM, &Error); 
113         if (retval < 0) {
114                 syslog(LOG_ERR, "clientsocket: socket_read_blob() failed: %s", Error);
115         }
116         return retval;
117 }
118
119
120 int CtdlSockGetLine(int *sock, StrBuf *Target, int nSec)
121 {
122         CitContext *CCC = MyContext();
123         const char *Error;
124         int rc;
125
126         FlushStrBuf(Target);
127         rc = StrBufTCP_read_buffered_line_fast(Target,
128                                                CCC->SBuf.Buf,
129                                                &CCC->SBuf.ReadWritePointer,
130                                                sock, nSec, 1, &Error);
131         if ((rc < 0) && (Error != NULL)) {
132                 syslog(LOG_ERR, "clientsocket: CtdlSockGetLine() failed: %s", Error);
133         }
134         return rc;
135 }
136
137
138 /*
139  * client_getln()   ...   Get a LF-terminated line of text from the client.
140  */
141 int sock_getln(int *sock, char *buf, int bufsize)
142 {
143         int i, retval;
144         CitContext *CCC = MyContext();
145         const char *pCh;
146
147         FlushStrBuf(CCC->sMigrateBuf);
148         retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
149
150         i = StrLength(CCC->sMigrateBuf);
151         pCh = ChrPtr(CCC->sMigrateBuf);
152
153         memcpy(buf, pCh, i + 1);
154
155         FlushStrBuf(CCC->sMigrateBuf);
156         if (retval < 0) {
157                 safestrncpy(&buf[i], "000", bufsize - i);
158                 i += 3;
159         }
160         return i;
161 }
162
163
164 /*
165  * sock_write() - send binary to server.
166  * Returns the number of bytes written, or -1 for error.
167  */
168 int sock_write(int *sock, const char *buf, int nbytes) 
169 { return sock_write_timeout(sock, buf, nbytes, 50); }
170 int sock_write_timeout(int *sock, const char *buf, int nbytes, int timeout)
171 {
172         int nSuccessLess = 0;
173         int bytes_written = 0;
174         int retval;
175         fd_set rfds;
176         int fdflags;
177         int IsNonBlock;
178         struct timeval tv;
179         int selectresolution = 100;
180
181         fdflags = fcntl(*sock, F_GETFL);
182         IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
183
184         while ((nSuccessLess < timeout) && 
185                (*sock != -1) && 
186                (bytes_written < nbytes)) 
187         {
188                 if (IsNonBlock){
189                         tv.tv_sec = selectresolution;
190                         tv.tv_usec = 0;
191                         
192                         FD_ZERO(&rfds);
193                         FD_SET(*sock, &rfds);
194                         if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
195                                 close (*sock);
196                                 *sock = -1;
197                                 return -1;
198                         }
199                 }
200                 if (IsNonBlock && !  FD_ISSET(*sock, &rfds)) {
201                         nSuccessLess ++;
202                         continue;
203                 }
204                 retval = write(*sock, &buf[bytes_written],
205                                nbytes - bytes_written);
206                 if (retval < 1) {
207                         sock_close(*sock);
208                         *sock = -1;
209                         return (-1);
210                 }
211                 bytes_written = bytes_written + retval;
212                 if (IsNonBlock && (bytes_written == nbytes)){
213                         tv.tv_sec = selectresolution;
214                         tv.tv_usec = 0;
215                         
216                         FD_ZERO(&rfds);
217                         FD_SET(*sock, &rfds);
218                         if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
219                                 close (*sock);
220                                 *sock = -1;
221                                 return -1;
222                         }
223                 }
224         }
225         return (bytes_written);
226 }
227
228
229 /*
230  * client_getln()   ...   Get a LF-terminated line of text from the client.
231  */
232 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
233 {
234         int i, retval;
235         CitContext *CCC = MyContext();
236         const char *pCh;
237
238         FlushStrBuf(CCC->sMigrateBuf);
239         *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
240
241         i = StrLength(CCC->sMigrateBuf);
242         pCh = ChrPtr(CCC->sMigrateBuf);
243
244         memcpy(buf, pCh, i + 1);
245
246         FlushStrBuf(CCC->sMigrateBuf);
247         if (retval < 0) {
248                 safestrncpy(&buf[i], "000", bufsize - i);
249                 i += 3;
250         }
251         return i;
252 }
253
254
255 /*
256  * Multiline version of sock_gets() ... this is a convenience function for
257  * client side protocol implementations.  It only returns the first line of
258  * a multiline response, discarding the rest.
259  */
260 int ml_sock_gets(int *sock, char *buf, int nSec)
261 {
262         int rc = 0;
263         char bigbuf[1024];
264         int g;
265
266         g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
267         if (rc < 0)
268                 return rc;
269         if (g < 4)
270                 return (g);
271         if (buf[3] != '-')
272                 return (g);
273
274         do {
275                 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
276                 if (rc < 0)
277                         return rc;
278                 if (g < 0)
279                         return (g);
280         } while ((g >= 4) && (bigbuf[3] == '-'));
281
282         return (strlen(buf));
283 }
284
285
286 /*
287  * sock_puts() - send line to server - implemented in terms of serv_write()
288  * Returns the number of bytes written, or -1 for error.
289  */
290 int sock_puts(int *sock, char *buf)
291 {
292         int i, j;
293
294         i = sock_write(sock, buf, strlen(buf));
295         if (i < 0)
296                 return (i);
297         j = sock_write(sock, "\n", 1);
298         if (j < 0)
299                 return (j);
300         return (i + j);
301 }