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).
7 * Copyright (c) 1987-2010 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
39 #include <libcitadel.h>
45 #include "sysdep_decls.h"
47 #include "clientsocket.h"
48 #include "ctdl_module.h"
50 int sock_connect(char *host, char *service)
52 struct in6_addr serveraddr;
53 struct addrinfo hints;
54 struct addrinfo *res = NULL;
55 struct addrinfo *ai = NULL;
59 if ((host == NULL) || IsEmptyStr(host))
61 if ((service == NULL) || IsEmptyStr(service))
64 memset(&hints, 0x00, sizeof(hints));
65 hints.ai_flags = AI_NUMERICSERV;
66 hints.ai_family = AF_UNSPEC;
67 hints.ai_socktype = SOCK_STREAM;
70 * Handle numeric IPv4 and IPv6 addresses
72 rc = inet_pton(AF_INET, host, &serveraddr);
73 if (rc == 1) { /* dotted quad */
74 hints.ai_family = AF_INET;
75 hints.ai_flags |= AI_NUMERICHOST;
77 rc = inet_pton(AF_INET6, host, &serveraddr);
78 if (rc == 1) { /* IPv6 address */
79 hints.ai_family = AF_INET6;
80 hints.ai_flags |= AI_NUMERICHOST;
84 /* Begin the connection process */
86 rc = getaddrinfo(host, service, &hints, &res);
88 CtdlLogPrintf(CTDL_ERR, "%s: %s\n", host, gai_strerror(rc));
93 * Try all available addresses until we connect to one or until we run out.
95 for (ai = res; ai != NULL; ai = ai->ai_next) {
96 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
98 CtdlLogPrintf(CTDL_ERR, "socket() failed: %s\n", strerror(errno));
101 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
106 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
117 * Read data from the client socket.
119 * sock socket fd to read from
120 * buf buffer to read into
121 * bytes number of bytes to read
122 * timeout Number of seconds to wait before timing out
124 * Possible return values:
125 * 1 Requested number of bytes has been read.
126 * 0 Request timed out.
127 * -1 Connection is broken, or other error.
129 int socket_read_blob(int *Socket, StrBuf * Target, int bytes, int timeout)
131 CitContext *CCC = MyContext();
136 retval = StrBufReadBLOBBuffered(Target,
139 Socket, 1, bytes, O_TERM, &Error);
141 CtdlLogPrintf(CTDL_CRIT,
142 "%s failed: %s\n", __FUNCTION__, Error);
148 int sock_read_to(int *sock, char *buf, int bytes, int timeout,
149 int keep_reading_until_full)
151 CitContext *CCC = MyContext();
154 FlushStrBuf(CCC->MigrateBuf);
155 rc = socket_read_blob(sock, CCC->sMigrateBuf, bytes, timeout);
160 if (StrLength(CCC->MigrateBuf) < bytes)
161 bytes = StrLength(CCC->MigrateBuf);
162 memcpy(buf, ChrPtr(CCC->MigrateBuf), bytes);
164 FlushStrBuf(CCC->MigrateBuf);
170 int CtdlSockGetLine(int *sock, StrBuf * Target, int nSec)
172 CitContext *CCC = MyContext();
177 rc = StrBufTCP_read_buffered_line_fast(Target,
180 sock, nSec, 1, &Error);
181 if ((rc < 0) && (Error != NULL))
182 CtdlLogPrintf(CTDL_CRIT,
183 "%s failed: %s\n", __FUNCTION__, Error);
189 * client_getln() ... Get a LF-terminated line of text from the client.
190 * (This is implemented in terms of client_read() and could be
191 * justifiably moved out of sysdep.c)
193 int sock_getln(int *sock, char *buf, int bufsize)
196 CitContext *CCC = MyContext();
199 FlushStrBuf(CCC->sMigrateBuf);
200 retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
202 i = StrLength(CCC->sMigrateBuf);
203 pCh = ChrPtr(CCC->sMigrateBuf);
205 memcpy(buf, pCh, i + 1);
207 FlushStrBuf(CCC->sMigrateBuf);
209 safestrncpy(&buf[i], "000", bufsize - i);
217 * sock_read() - input binary data from socket.
218 * Returns the number of bytes read, or -1 for error.
220 INLINE int sock_read(int *sock, char *buf, int bytes,
221 int keep_reading_until_full)
223 return sock_read_to(sock, buf, bytes, CLIENT_TIMEOUT,
224 keep_reading_until_full);
229 * sock_write() - send binary to server.
230 * Returns the number of bytes written, or -1 for error.
232 int sock_write(int *sock, const char *buf, int nbytes)
234 int nSuccessLess = 0;
235 int bytes_written = 0;
242 int selectresolution = 100;
244 fdflags = fcntl(*sock, F_GETFL);
245 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
247 while ((nSuccessLess < timeout) &&
249 (bytes_written < nbytes))
252 tv.tv_sec = selectresolution;
256 FD_SET(*sock, &rfds);
257 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
258 /// *Error = strerror(errno);
264 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
268 retval = write(*sock, &buf[bytes_written],
269 nbytes - bytes_written);
275 bytes_written = bytes_written + retval;
276 if (IsNonBlock && (bytes_written == nbytes)){
277 tv.tv_sec = selectresolution;
281 FD_SET(*sock, &rfds);
282 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
283 /// *Error = strerror(errno);
290 return (bytes_written);
296 * client_getln() ... Get a LF-terminated line of text from the client.
297 * (This is implemented in terms of client_read() and could be
298 * justifiably moved out of sysdep.c)
300 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
303 CitContext *CCC = MyContext();
306 FlushStrBuf(CCC->sMigrateBuf);
307 *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
309 i = StrLength(CCC->sMigrateBuf);
310 pCh = ChrPtr(CCC->sMigrateBuf);
312 memcpy(buf, pCh, i + 1);
314 FlushStrBuf(CCC->sMigrateBuf);
316 safestrncpy(&buf[i], "000", bufsize - i);
323 * Multiline version of sock_gets() ... this is a convenience function for
324 * client side protocol implementations. It only returns the first line of
325 * a multiline response, discarding the rest.
327 int ml_sock_gets(int *sock, char *buf, int nSec)
333 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
342 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
347 } while ((g >= 4) && (bigbuf[3] == '-'));
349 return (strlen(buf));
354 * sock_puts() - send line to server - implemented in terms of serv_write()
355 * Returns the number of bytes written, or -1 for error.
357 int sock_puts(int *sock, char *buf)
361 i = sock_write(sock, buf, strlen(buf));
364 j = sock_write(sock, "\n", 1);