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-2017 by the citadel.org team
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.
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.
22 #include <libcitadel.h>
23 #include "ctdl_module.h"
24 #include "clientsocket.h"
26 int sock_connect(char *host, char *service)
28 struct in6_addr serveraddr;
29 struct addrinfo hints;
30 struct addrinfo *res = NULL;
31 struct addrinfo *ai = NULL;
35 if ((host == NULL) || IsEmptyStr(host))
37 if ((service == NULL) || IsEmptyStr(service))
40 memset(&hints, 0x00, sizeof(hints));
41 hints.ai_flags = AI_NUMERICSERV;
42 hints.ai_family = AF_UNSPEC;
43 hints.ai_socktype = SOCK_STREAM;
46 * Handle numeric IPv4 and IPv6 addresses
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;
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;
60 /* Begin the connection process */
62 rc = getaddrinfo(host, service, &hints, &res);
64 syslog(LOG_ERR, "%s: %s", host, gai_strerror(rc));
69 * Try all available addresses until we connect to one or until we run out.
71 for (ai = res; ai != NULL; ai = ai->ai_next) {
72 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
74 syslog(LOG_ERR, "%s: %m", host);
78 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
84 syslog(LOG_ERR, "%s: %m", host);
95 * Read data from the client socket.
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
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.
107 int socket_read_blob(int *Socket, StrBuf *Target, int bytes, int timeout)
112 retval = StrBufReadBLOBBuffered(Target, CC->SBuf.Buf, &CC->SBuf.ReadWritePointer, Socket, 1, bytes, O_TERM, &Error);
114 syslog(LOG_ERR, "clientsocket: socket_read_blob() failed: %s", Error);
120 int CtdlSockGetLine(int *sock, StrBuf *Target, int nSec)
122 CitContext *CCC = MyContext();
127 rc = StrBufTCP_read_buffered_line_fast(Target,
129 &CCC->SBuf.ReadWritePointer,
130 sock, nSec, 1, &Error);
131 if ((rc < 0) && (Error != NULL)) {
132 syslog(LOG_ERR, "clientsocket: CtdlSockGetLine() failed: %s", Error);
139 * client_getln() ... Get a LF-terminated line of text from the client.
141 int sock_getln(int *sock, char *buf, int bufsize)
144 CitContext *CCC = MyContext();
147 FlushStrBuf(CCC->sMigrateBuf);
148 retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
150 i = StrLength(CCC->sMigrateBuf);
151 pCh = ChrPtr(CCC->sMigrateBuf);
153 memcpy(buf, pCh, i + 1);
155 FlushStrBuf(CCC->sMigrateBuf);
157 safestrncpy(&buf[i], "000", bufsize - i);
165 * sock_write() - send binary to server.
166 * Returns the number of bytes written, or -1 for error.
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)
172 int nSuccessLess = 0;
173 int bytes_written = 0;
179 int selectresolution = 100;
181 fdflags = fcntl(*sock, F_GETFL);
182 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
184 while ((nSuccessLess < timeout) &&
186 (bytes_written < nbytes))
189 tv.tv_sec = selectresolution;
193 FD_SET(*sock, &rfds);
194 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
200 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
204 retval = write(*sock, &buf[bytes_written],
205 nbytes - bytes_written);
211 bytes_written = bytes_written + retval;
212 if (IsNonBlock && (bytes_written == nbytes)){
213 tv.tv_sec = selectresolution;
217 FD_SET(*sock, &rfds);
218 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
225 return (bytes_written);
230 * client_getln() ... Get a LF-terminated line of text from the client.
232 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
235 CitContext *CCC = MyContext();
238 FlushStrBuf(CCC->sMigrateBuf);
239 *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
241 i = StrLength(CCC->sMigrateBuf);
242 pCh = ChrPtr(CCC->sMigrateBuf);
244 memcpy(buf, pCh, i + 1);
246 FlushStrBuf(CCC->sMigrateBuf);
248 safestrncpy(&buf[i], "000", bufsize - i);
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.
260 int ml_sock_gets(int *sock, char *buf, int nSec)
266 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
275 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
280 } while ((g >= 4) && (bigbuf[3] == '-'));
282 return (strlen(buf));
287 * sock_puts() - send line to server - implemented in terms of serv_write()
288 * Returns the number of bytes written, or -1 for error.
290 int sock_puts(int *sock, char *buf)
294 i = sock_write(sock, buf, strlen(buf));
297 j = sock_write(sock, "\n", 1);