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.
16 #include <sys/socket.h>
18 #include <libcitadel.h>
19 #include "ctdl_module.h"
20 #include "clientsocket.h"
22 int sock_connect(char *host, char *service) {
23 struct in6_addr serveraddr;
24 struct addrinfo hints;
25 struct addrinfo *res = NULL;
26 struct addrinfo *ai = NULL;
30 if ((host == NULL) || IsEmptyStr(host))
32 if ((service == NULL) || IsEmptyStr(service))
35 memset(&hints, 0x00, sizeof(hints));
36 hints.ai_flags = AI_NUMERICSERV;
37 hints.ai_family = AF_UNSPEC;
38 hints.ai_socktype = SOCK_STREAM;
40 // Handle numeric IPv4 and IPv6 addresses
41 rc = inet_pton(AF_INET, host, &serveraddr);
42 if (rc == 1) { // dotted quad
43 hints.ai_family = AF_INET;
44 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
55 rc = getaddrinfo(host, service, &hints, &res);
57 syslog(LOG_ERR, "%s: %s", host, gai_strerror(rc));
61 // Try all available addresses until we connect to one or until we run out.
62 for (ai = res; ai != NULL; ai = ai->ai_next) {
63 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
65 syslog(LOG_ERR, "%s: %m", host);
69 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
75 syslog(LOG_ERR, "%s: %m", host);
84 // Read data from the client socket.
86 // sock socket fd to read from
87 // buf buffer to read into
88 // bytes number of bytes to read
89 // timeout Number of seconds to wait before timing out
91 // Possible return values:
92 // 1 Requested number of bytes has been read.
93 // 0 Request timed out.
94 // -1 Connection is broken, or other error.
95 int socket_read_blob(int *Socket, StrBuf *Target, int bytes, int timeout) {
99 retval = StrBufReadBLOBBuffered(Target, CC->SBuf.Buf, &CC->SBuf.ReadWritePointer, Socket, 1, bytes, O_TERM, &Error);
101 syslog(LOG_ERR, "clientsocket: socket_read_blob() failed: %s", Error);
107 int CtdlSockGetLine(int *sock, StrBuf *Target, int nSec) {
112 rc = StrBufTCP_read_buffered_line_fast(Target,
114 &CC->SBuf.ReadWritePointer,
115 sock, nSec, 1, &Error);
116 if ((rc < 0) && (Error != NULL)) {
117 syslog(LOG_ERR, "clientsocket: CtdlSockGetLine() failed: %s", Error);
123 // client_getln() ... Get a LF-terminated line of text from the client.
124 int sock_getln(int *sock, char *buf, int bufsize) {
128 FlushStrBuf(CC->sMigrateBuf);
129 retval = CtdlSockGetLine(sock, CC->sMigrateBuf, 5);
131 i = StrLength(CC->sMigrateBuf);
132 pCh = ChrPtr(CC->sMigrateBuf);
134 memcpy(buf, pCh, i + 1);
136 FlushStrBuf(CC->sMigrateBuf);
138 safestrncpy(&buf[i], "000", bufsize - i);
145 // sock_write() - send binary to server.
146 // Returns the number of bytes written, or -1 for error.
147 int sock_write(int *sock, const char *buf, int nbytes) {
148 return sock_write_timeout(sock, buf, nbytes, 50);
152 int sock_write_timeout(int *sock, const char *buf, int nbytes, int timeout) {
153 int nSuccessLess = 0;
154 int bytes_written = 0;
160 int selectresolution = 100;
162 fdflags = fcntl(*sock, F_GETFL);
163 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
165 while ((nSuccessLess < timeout) && (*sock != -1) && (bytes_written < nbytes)) {
167 tv.tv_sec = selectresolution;
171 FD_SET(*sock, &rfds);
172 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
178 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
182 retval = write(*sock, &buf[bytes_written],
183 nbytes - bytes_written);
189 bytes_written = bytes_written + retval;
190 if (IsNonBlock && (bytes_written == nbytes)){
191 tv.tv_sec = selectresolution;
195 FD_SET(*sock, &rfds);
196 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
203 return (bytes_written);
207 // client_getln() ... Get a LF-terminated line of text from the client.
208 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec) {
212 FlushStrBuf(CC->sMigrateBuf);
213 *rc = retval = CtdlSockGetLine(sock, CC->sMigrateBuf, nSec);
215 i = StrLength(CC->sMigrateBuf);
216 pCh = ChrPtr(CC->sMigrateBuf);
218 memcpy(buf, pCh, i + 1);
220 FlushStrBuf(CC->sMigrateBuf);
222 safestrncpy(&buf[i], "000", bufsize - i);
229 // Multiline version of sock_gets() ... this is a convenience function for
230 // client side protocol implementations. It only returns the first line of
231 // a multiline response, discarding the rest.
232 int ml_sock_gets(int *sock, char *buf, int nSec) {
237 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
246 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
251 } while ((g >= 4) && (bigbuf[3] == '-'));
253 return (strlen(buf));
257 // sock_puts() - send line to server - implemented in terms of serv_write()
258 // Returns the number of bytes written, or -1 for error.
259 int sock_puts(int *sock, char *buf) {
262 i = sock_write(sock, buf, strlen(buf));
265 j = sock_write(sock, "\n", 1);