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-2011 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.
19 #include <libcitadel.h>
20 #include "ctdl_module.h"
21 #include "clientsocket.h"
23 int sock_connect(char *host, char *service)
25 struct in6_addr serveraddr;
26 struct addrinfo hints;
27 struct addrinfo *res = NULL;
28 struct addrinfo *ai = NULL;
32 if ((host == NULL) || IsEmptyStr(host))
34 if ((service == NULL) || IsEmptyStr(service))
37 memset(&hints, 0x00, sizeof(hints));
38 hints.ai_flags = AI_NUMERICSERV;
39 hints.ai_family = AF_UNSPEC;
40 hints.ai_socktype = SOCK_STREAM;
43 * Handle numeric IPv4 and IPv6 addresses
45 rc = inet_pton(AF_INET, host, &serveraddr);
46 if (rc == 1) { /* dotted quad */
47 hints.ai_family = AF_INET;
48 hints.ai_flags |= AI_NUMERICHOST;
50 rc = inet_pton(AF_INET6, host, &serveraddr);
51 if (rc == 1) { /* IPv6 address */
52 hints.ai_family = AF_INET6;
53 hints.ai_flags |= AI_NUMERICHOST;
57 /* Begin the connection process */
59 rc = getaddrinfo(host, service, &hints, &res);
61 syslog(LOG_ERR, "%s: %s", host, gai_strerror(rc));
66 * Try all available addresses until we connect to one or until we run out.
68 for (ai = res; ai != NULL; ai = ai->ai_next) {
69 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
71 syslog(LOG_ERR, "socket() failed: %s", strerror(errno));
75 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
81 syslog(LOG_ERR, "connect() failed: %s", strerror(errno));
92 * Read data from the client socket.
94 * sock socket fd to read from
95 * buf buffer to read into
96 * bytes number of bytes to read
97 * timeout Number of seconds to wait before timing out
99 * Possible return values:
100 * 1 Requested number of bytes has been read.
101 * 0 Request timed out.
102 * -1 Connection is broken, or other error.
104 int socket_read_blob(int *Socket, StrBuf * Target, int bytes, int timeout)
106 CitContext *CCC = MyContext();
111 retval = StrBufReadBLOBBuffered(Target,
113 &CCC->SBuf.ReadWritePointer,
114 Socket, 1, bytes, O_TERM, &Error);
117 syslog(LOG_CRIT, "socket_read_blob() failed: %s", Error);
123 int CtdlSockGetLine(int *sock, StrBuf * Target, int nSec)
125 CitContext *CCC = MyContext();
130 rc = StrBufTCP_read_buffered_line_fast(Target,
132 &CCC->SBuf.ReadWritePointer,
133 sock, nSec, 1, &Error);
134 if ((rc < 0) && (Error != NULL))
135 syslog(LOG_CRIT, "CtdlSockGetLine() failed: %s", Error);
141 * client_getln() ... Get a LF-terminated line of text from the client.
142 * (This is implemented in terms of client_read() and could be
143 * justifiably moved out of sysdep.c)
145 int sock_getln(int *sock, char *buf, int bufsize)
148 CitContext *CCC = MyContext();
151 FlushStrBuf(CCC->sMigrateBuf);
152 retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
154 i = StrLength(CCC->sMigrateBuf);
155 pCh = ChrPtr(CCC->sMigrateBuf);
157 memcpy(buf, pCh, i + 1);
159 FlushStrBuf(CCC->sMigrateBuf);
161 safestrncpy(&buf[i], "000", bufsize - i);
169 * sock_write() - send binary to server.
170 * Returns the number of bytes written, or -1 for error.
172 int sock_write(int *sock, const char *buf, int nbytes)
173 { return sock_write_timeout(sock, buf, nbytes, 50); }
174 int sock_write_timeout(int *sock, const char *buf, int nbytes, int timeout)
176 int nSuccessLess = 0;
177 int bytes_written = 0;
183 int selectresolution = 100;
185 fdflags = fcntl(*sock, F_GETFL);
186 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
188 while ((nSuccessLess < timeout) &&
190 (bytes_written < nbytes))
193 tv.tv_sec = selectresolution;
197 FD_SET(*sock, &rfds);
198 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
199 /// *Error = strerror(errno);
205 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
209 retval = write(*sock, &buf[bytes_written],
210 nbytes - bytes_written);
216 bytes_written = bytes_written + retval;
217 if (IsNonBlock && (bytes_written == nbytes)){
218 tv.tv_sec = selectresolution;
222 FD_SET(*sock, &rfds);
223 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
224 /// *Error = strerror(errno);
231 return (bytes_written);
237 * client_getln() ... Get a LF-terminated line of text from the client.
238 * (This is implemented in terms of client_read() and could be
239 * justifiably moved out of sysdep.c)
241 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
244 CitContext *CCC = MyContext();
247 FlushStrBuf(CCC->sMigrateBuf);
248 *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
250 i = StrLength(CCC->sMigrateBuf);
251 pCh = ChrPtr(CCC->sMigrateBuf);
253 memcpy(buf, pCh, i + 1);
255 FlushStrBuf(CCC->sMigrateBuf);
257 safestrncpy(&buf[i], "000", bufsize - i);
264 * Multiline version of sock_gets() ... this is a convenience function for
265 * client side protocol implementations. It only returns the first line of
266 * a multiline response, discarding the rest.
268 int ml_sock_gets(int *sock, char *buf, int nSec)
274 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
283 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
288 } while ((g >= 4) && (bigbuf[3] == '-'));
290 return (strlen(buf));
295 * sock_puts() - send line to server - implemented in terms of serv_write()
296 * Returns the number of bytes written, or -1 for error.
298 int sock_puts(int *sock, char *buf)
302 i = sock_write(sock, buf, strlen(buf));
305 j = sock_write(sock, "\n", 1);