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) {
21 struct in6_addr serveraddr;
22 struct addrinfo hints;
23 struct addrinfo *res = NULL;
24 struct addrinfo *ai = NULL;
28 if ((host == NULL) || IsEmptyStr(host))
30 if ((service == NULL) || IsEmptyStr(service))
33 memset(&hints, 0x00, sizeof(hints));
34 hints.ai_flags = AI_NUMERICSERV;
35 hints.ai_family = AF_UNSPEC;
36 hints.ai_socktype = SOCK_STREAM;
38 // Handle numeric IPv4 and IPv6 addresses
39 rc = inet_pton(AF_INET, host, &serveraddr);
40 if (rc == 1) { // dotted quad
41 hints.ai_family = AF_INET;
42 hints.ai_flags |= AI_NUMERICHOST;
45 rc = inet_pton(AF_INET6, host, &serveraddr);
46 if (rc == 1) { // IPv6 address
47 hints.ai_family = AF_INET6;
48 hints.ai_flags |= AI_NUMERICHOST;
52 // Begin the connection process
53 rc = getaddrinfo(host, service, &hints, &res);
55 syslog(LOG_ERR, "%s: %s", host, gai_strerror(rc));
59 // Try all available addresses until we connect to one or until we run out.
60 for (ai = res; ai != NULL; ai = ai->ai_next) {
61 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
63 syslog(LOG_ERR, "%s: %m", host);
67 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
73 syslog(LOG_ERR, "%s: %m", host);
82 // Read data from the client socket.
84 // sock socket fd to read from
85 // buf buffer to read into
86 // bytes number of bytes to read
87 // timeout Number of seconds to wait before timing out
89 // Possible return values:
90 // 1 Requested number of bytes has been read.
91 // 0 Request timed out.
92 // -1 Connection is broken, or other error.
93 int socket_read_blob(int *Socket, StrBuf *Target, int bytes, int timeout) {
97 retval = StrBufReadBLOBBuffered(Target, CC->SBuf.Buf, &CC->SBuf.ReadWritePointer, Socket, 1, bytes, O_TERM, &Error);
99 syslog(LOG_ERR, "clientsocket: socket_read_blob() failed: %s", Error);
105 int CtdlSockGetLine(int *sock, StrBuf *Target, int nSec) {
110 rc = StrBufTCP_read_buffered_line_fast(Target,
112 &CC->SBuf.ReadWritePointer,
113 sock, nSec, 1, &Error);
114 if ((rc < 0) && (Error != NULL)) {
115 syslog(LOG_ERR, "clientsocket: CtdlSockGetLine() failed: %s", Error);
121 // client_getln() ... Get a LF-terminated line of text from the client.
122 int sock_getln(int *sock, char *buf, int bufsize) {
126 FlushStrBuf(CC->sMigrateBuf);
127 retval = CtdlSockGetLine(sock, CC->sMigrateBuf, 5);
129 i = StrLength(CC->sMigrateBuf);
130 pCh = ChrPtr(CC->sMigrateBuf);
132 memcpy(buf, pCh, i + 1);
134 FlushStrBuf(CC->sMigrateBuf);
136 safestrncpy(&buf[i], "000", bufsize - i);
143 // sock_write() - send binary to server.
144 // Returns the number of bytes written, or -1 for error.
145 int sock_write(int *sock, const char *buf, int nbytes) {
146 return sock_write_timeout(sock, buf, nbytes, 50);
150 int sock_write_timeout(int *sock, const char *buf, int nbytes, int timeout) {
151 int nSuccessLess = 0;
152 int bytes_written = 0;
158 int selectresolution = 100;
160 fdflags = fcntl(*sock, F_GETFL);
161 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
163 while ((nSuccessLess < timeout) && (*sock != -1) && (bytes_written < nbytes)) {
165 tv.tv_sec = selectresolution;
169 FD_SET(*sock, &rfds);
170 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
176 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
180 retval = write(*sock, &buf[bytes_written],
181 nbytes - bytes_written);
187 bytes_written = bytes_written + retval;
188 if (IsNonBlock && (bytes_written == nbytes)){
189 tv.tv_sec = selectresolution;
193 FD_SET(*sock, &rfds);
194 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
201 return (bytes_written);
205 // client_getln() ... Get a LF-terminated line of text from the client.
206 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec) {
210 FlushStrBuf(CC->sMigrateBuf);
211 *rc = retval = CtdlSockGetLine(sock, CC->sMigrateBuf, nSec);
213 i = StrLength(CC->sMigrateBuf);
214 pCh = ChrPtr(CC->sMigrateBuf);
216 memcpy(buf, pCh, i + 1);
218 FlushStrBuf(CC->sMigrateBuf);
220 safestrncpy(&buf[i], "000", bufsize - i);
227 // Multiline version of sock_gets() ... this is a convenience function for
228 // client side protocol implementations. It only returns the first line of
229 // a multiline response, discarding the rest.
230 int ml_sock_gets(int *sock, char *buf, int nSec) {
235 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
244 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
249 } while ((g >= 4) && (bigbuf[3] == '-'));
251 return (strlen(buf));
255 // sock_puts() - send line to server - implemented in terms of serv_write()
256 // Returns the number of bytes written, or -1 for error.
257 int sock_puts(int *sock, char *buf) {
260 i = sock_write(sock, buf, strlen(buf));
263 j = sock_write(sock, "\n", 1);