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.
17 #include <sys/socket.h>
19 #include <libcitadel.h>
20 #include "ctdl_module.h"
21 #include "clientsocket.h"
23 int sock_connect(char *host, char *service) {
24 struct in6_addr serveraddr;
25 struct addrinfo hints;
26 struct addrinfo *res = NULL;
27 struct addrinfo *ai = NULL;
31 if ((host == NULL) || IsEmptyStr(host))
33 if ((service == NULL) || IsEmptyStr(service))
36 memset(&hints, 0x00, sizeof(hints));
37 hints.ai_flags = AI_NUMERICSERV;
38 hints.ai_family = AF_UNSPEC;
39 hints.ai_socktype = SOCK_STREAM;
41 // 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;
48 rc = inet_pton(AF_INET6, host, &serveraddr);
49 if (rc == 1) { // IPv6 address
50 hints.ai_family = AF_INET6;
51 hints.ai_flags |= AI_NUMERICHOST;
55 // Begin the connection process
56 rc = getaddrinfo(host, service, &hints, &res);
58 syslog(LOG_ERR, "%s: %s", host, gai_strerror(rc));
62 // Try all available addresses until we connect to one or until we run out.
63 for (ai = res; ai != NULL; ai = ai->ai_next) {
64 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
66 syslog(LOG_ERR, "%s: %m", host);
70 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
76 syslog(LOG_ERR, "%s: %m", host);
85 // Read data from the client socket.
87 // sock socket fd to read from
88 // buf buffer to read into
89 // bytes number of bytes to read
90 // timeout Number of seconds to wait before timing out
92 // Possible return values:
93 // 1 Requested number of bytes has been read.
94 // 0 Request timed out.
95 // -1 Connection is broken, or other error.
96 int socket_read_blob(int *Socket, StrBuf *Target, int bytes, int timeout) {
100 retval = StrBufReadBLOBBuffered(Target, CC->SBuf.Buf, &CC->SBuf.ReadWritePointer, Socket, 1, bytes, O_TERM, &Error);
102 syslog(LOG_ERR, "clientsocket: socket_read_blob() failed: %s", Error);
108 int CtdlSockGetLine(int *sock, StrBuf *Target, int nSec) {
113 rc = StrBufTCP_read_buffered_line_fast(Target,
115 &CC->SBuf.ReadWritePointer,
116 sock, nSec, 1, &Error);
117 if ((rc < 0) && (Error != NULL)) {
118 syslog(LOG_ERR, "clientsocket: CtdlSockGetLine() failed: %s", Error);
124 // client_getln() ... Get a LF-terminated line of text from the client.
125 int sock_getln(int *sock, char *buf, int bufsize) {
129 FlushStrBuf(CC->sMigrateBuf);
130 retval = CtdlSockGetLine(sock, CC->sMigrateBuf, 5);
132 i = StrLength(CC->sMigrateBuf);
133 pCh = ChrPtr(CC->sMigrateBuf);
135 memcpy(buf, pCh, i + 1);
137 FlushStrBuf(CC->sMigrateBuf);
139 safestrncpy(&buf[i], "000", bufsize - i);
146 // sock_write() - send binary to server.
147 // Returns the number of bytes written, or -1 for error.
148 int sock_write(int *sock, const char *buf, int nbytes) {
149 return sock_write_timeout(sock, buf, nbytes, 50);
153 int sock_write_timeout(int *sock, const char *buf, int nbytes, int timeout) {
154 int nSuccessLess = 0;
155 int bytes_written = 0;
161 int selectresolution = 100;
163 fdflags = fcntl(*sock, F_GETFL);
164 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
166 while ((nSuccessLess < timeout) && (*sock != -1) && (bytes_written < nbytes)) {
168 tv.tv_sec = selectresolution;
172 FD_SET(*sock, &rfds);
173 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
179 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
183 retval = write(*sock, &buf[bytes_written],
184 nbytes - bytes_written);
190 bytes_written = bytes_written + retval;
191 if (IsNonBlock && (bytes_written == nbytes)){
192 tv.tv_sec = selectresolution;
196 FD_SET(*sock, &rfds);
197 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
204 return (bytes_written);
208 // client_getln() ... Get a LF-terminated line of text from the client.
209 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec) {
213 FlushStrBuf(CC->sMigrateBuf);
214 *rc = retval = CtdlSockGetLine(sock, CC->sMigrateBuf, nSec);
216 i = StrLength(CC->sMigrateBuf);
217 pCh = ChrPtr(CC->sMigrateBuf);
219 memcpy(buf, pCh, i + 1);
221 FlushStrBuf(CC->sMigrateBuf);
223 safestrncpy(&buf[i], "000", bufsize - i);
230 // Multiline version of sock_gets() ... this is a convenience function for
231 // client side protocol implementations. It only returns the first line of
232 // a multiline response, discarding the rest.
233 int ml_sock_gets(int *sock, char *buf, int nSec) {
238 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
247 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
252 } while ((g >= 4) && (bigbuf[3] == '-'));
254 return (strlen(buf));
258 // sock_puts() - send line to server - implemented in terms of serv_write()
259 // Returns the number of bytes written, or -1 for error.
260 int sock_puts(int *sock, char *buf) {
263 i = sock_write(sock, buf, strlen(buf));
266 j = sock_write(sock, "\n", 1);