1 // This module handles client-side sockets opened by the Citadel server (for
2 // the client side of Internet protocols, etc.) It does _not_ handle client
3 // sockets for the Citadel client; for that you must look in ipc_c_tcp.c
4 // (which, uncoincidentally, bears a striking similarity to this file).
6 // Copyright (c) 1987-2017 by the citadel.org team
8 // This program is open source software. Use, duplication, or disclosure
9 // is subject to the terms of the GNU General Public License, version 3.
10 // The program is distributed without any warranty, expressed or implied.
16 #include <libcitadel.h>
17 #include "ctdl_module.h"
18 #include "clientsocket.h"
20 int sock_connect(char *host, char *service)
22 struct in6_addr serveraddr;
23 struct addrinfo hints;
24 struct addrinfo *res = NULL;
25 struct addrinfo *ai = NULL;
29 if ((host == NULL) || IsEmptyStr(host))
31 if ((service == NULL) || IsEmptyStr(service))
34 memset(&hints, 0x00, sizeof(hints));
35 hints.ai_flags = AI_NUMERICSERV;
36 hints.ai_family = AF_UNSPEC;
37 hints.ai_socktype = SOCK_STREAM;
40 * Handle numeric IPv4 and IPv6 addresses
42 rc = inet_pton(AF_INET, host, &serveraddr);
43 if (rc == 1) { /* dotted quad */
44 hints.ai_family = AF_INET;
45 hints.ai_flags |= AI_NUMERICHOST;
47 rc = inet_pton(AF_INET6, host, &serveraddr);
48 if (rc == 1) { /* IPv6 address */
49 hints.ai_family = AF_INET6;
50 hints.ai_flags |= AI_NUMERICHOST;
54 /* Begin the connection process */
56 rc = getaddrinfo(host, service, &hints, &res);
58 syslog(LOG_ERR, "%s: %s", host, gai_strerror(rc));
63 * Try all available addresses until we connect to one or until we run out.
65 for (ai = res; ai != NULL; ai = ai->ai_next) {
66 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
68 syslog(LOG_ERR, "%s: %m", host);
72 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
78 syslog(LOG_ERR, "%s: %m", host);
89 * Read data from the client socket.
91 * sock socket fd to read from
92 * buf buffer to read into
93 * bytes number of bytes to read
94 * timeout Number of seconds to wait before timing out
96 * Possible return values:
97 * 1 Requested number of bytes has been read.
98 * 0 Request timed out.
99 * -1 Connection is broken, or other error.
101 int socket_read_blob(int *Socket, StrBuf *Target, int bytes, int timeout)
106 retval = StrBufReadBLOBBuffered(Target, CC->SBuf.Buf, &CC->SBuf.ReadWritePointer, Socket, 1, bytes, O_TERM, &Error);
108 syslog(LOG_ERR, "clientsocket: socket_read_blob() failed: %s", Error);
114 int CtdlSockGetLine(int *sock, StrBuf *Target, int nSec)
116 CitContext *CCC = MyContext();
121 rc = StrBufTCP_read_buffered_line_fast(Target,
123 &CCC->SBuf.ReadWritePointer,
124 sock, nSec, 1, &Error);
125 if ((rc < 0) && (Error != NULL)) {
126 syslog(LOG_ERR, "clientsocket: CtdlSockGetLine() failed: %s", Error);
133 * client_getln() ... Get a LF-terminated line of text from the client.
135 int sock_getln(int *sock, char *buf, int bufsize)
138 CitContext *CCC = MyContext();
141 FlushStrBuf(CCC->sMigrateBuf);
142 retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
144 i = StrLength(CCC->sMigrateBuf);
145 pCh = ChrPtr(CCC->sMigrateBuf);
147 memcpy(buf, pCh, i + 1);
149 FlushStrBuf(CCC->sMigrateBuf);
151 safestrncpy(&buf[i], "000", bufsize - i);
159 * sock_write() - send binary to server.
160 * Returns the number of bytes written, or -1 for error.
162 int sock_write(int *sock, const char *buf, int nbytes)
163 { return sock_write_timeout(sock, buf, nbytes, 50); }
164 int sock_write_timeout(int *sock, const char *buf, int nbytes, int timeout)
166 int nSuccessLess = 0;
167 int bytes_written = 0;
173 int selectresolution = 100;
175 fdflags = fcntl(*sock, F_GETFL);
176 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
178 while ((nSuccessLess < timeout) &&
180 (bytes_written < nbytes))
183 tv.tv_sec = selectresolution;
187 FD_SET(*sock, &rfds);
188 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
194 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
198 retval = write(*sock, &buf[bytes_written],
199 nbytes - bytes_written);
205 bytes_written = bytes_written + retval;
206 if (IsNonBlock && (bytes_written == nbytes)){
207 tv.tv_sec = selectresolution;
211 FD_SET(*sock, &rfds);
212 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
219 return (bytes_written);
224 * client_getln() ... Get a LF-terminated line of text from the client.
226 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
229 CitContext *CCC = MyContext();
232 FlushStrBuf(CCC->sMigrateBuf);
233 *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
235 i = StrLength(CCC->sMigrateBuf);
236 pCh = ChrPtr(CCC->sMigrateBuf);
238 memcpy(buf, pCh, i + 1);
240 FlushStrBuf(CCC->sMigrateBuf);
242 safestrncpy(&buf[i], "000", bufsize - i);
250 * Multiline version of sock_gets() ... this is a convenience function for
251 * client side protocol implementations. It only returns the first line of
252 * a multiline response, discarding the rest.
254 int ml_sock_gets(int *sock, char *buf, int nSec)
260 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
269 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
274 } while ((g >= 4) && (bigbuf[3] == '-'));
276 return (strlen(buf));
281 * sock_puts() - send line to server - implemented in terms of serv_write()
282 * Returns the number of bytes written, or -1 for error.
284 int sock_puts(int *sock, char *buf)
288 i = sock_write(sock, buf, strlen(buf));
291 j = sock_write(sock, "\n", 1);