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.
23 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
34 #include <libcitadel.h>
37 #include "sysdep_decls.h"
39 #include "clientsocket.h"
40 #include "ctdl_module.h"
42 int sock_connect(char *host, char *service)
44 struct in6_addr serveraddr;
45 struct addrinfo hints;
46 struct addrinfo *res = NULL;
47 struct addrinfo *ai = NULL;
51 if ((host == NULL) || IsEmptyStr(host))
53 if ((service == NULL) || IsEmptyStr(service))
56 memset(&hints, 0x00, sizeof(hints));
57 hints.ai_flags = AI_NUMERICSERV;
58 hints.ai_family = AF_UNSPEC;
59 hints.ai_socktype = SOCK_STREAM;
62 * Handle numeric IPv4 and IPv6 addresses
64 rc = inet_pton(AF_INET, host, &serveraddr);
65 if (rc == 1) { /* dotted quad */
66 hints.ai_family = AF_INET;
67 hints.ai_flags |= AI_NUMERICHOST;
69 rc = inet_pton(AF_INET6, host, &serveraddr);
70 if (rc == 1) { /* IPv6 address */
71 hints.ai_family = AF_INET6;
72 hints.ai_flags |= AI_NUMERICHOST;
76 /* Begin the connection process */
78 rc = getaddrinfo(host, service, &hints, &res);
80 syslog(LOG_ERR, "%s: %s", host, gai_strerror(rc));
85 * Try all available addresses until we connect to one or until we run out.
87 for (ai = res; ai != NULL; ai = ai->ai_next) {
88 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
90 syslog(LOG_ERR, "socket() failed: %s", strerror(errno));
94 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
100 syslog(LOG_ERR, "connect() failed: %s", strerror(errno));
111 * Read data from the client socket.
113 * sock socket fd to read from
114 * buf buffer to read into
115 * bytes number of bytes to read
116 * timeout Number of seconds to wait before timing out
118 * Possible return values:
119 * 1 Requested number of bytes has been read.
120 * 0 Request timed out.
121 * -1 Connection is broken, or other error.
123 int socket_read_blob(int *Socket, StrBuf * Target, int bytes, int timeout)
125 CitContext *CCC = MyContext();
130 retval = StrBufReadBLOBBuffered(Target,
132 &CCC->SBuf.ReadWritePointer,
133 Socket, 1, bytes, O_TERM, &Error);
136 syslog(LOG_CRIT, "socket_read_blob() failed: %s", Error);
142 int CtdlSockGetLine(int *sock, StrBuf * Target, int nSec)
144 CitContext *CCC = MyContext();
149 rc = StrBufTCP_read_buffered_line_fast(Target,
151 &CCC->SBuf.ReadWritePointer,
152 sock, nSec, 1, &Error);
153 if ((rc < 0) && (Error != NULL))
154 syslog(LOG_CRIT, "CtdlSockGetLine() failed: %s", Error);
160 * client_getln() ... Get a LF-terminated line of text from the client.
161 * (This is implemented in terms of client_read() and could be
162 * justifiably moved out of sysdep.c)
164 int sock_getln(int *sock, char *buf, int bufsize)
167 CitContext *CCC = MyContext();
170 FlushStrBuf(CCC->sMigrateBuf);
171 retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
173 i = StrLength(CCC->sMigrateBuf);
174 pCh = ChrPtr(CCC->sMigrateBuf);
176 memcpy(buf, pCh, i + 1);
178 FlushStrBuf(CCC->sMigrateBuf);
180 safestrncpy(&buf[i], "000", bufsize - i);
188 * sock_write() - send binary to server.
189 * Returns the number of bytes written, or -1 for error.
191 int sock_write(int *sock, const char *buf, int nbytes)
192 { return sock_write_timeout(sock, buf, nbytes, 50); }
193 int sock_write_timeout(int *sock, const char *buf, int nbytes, int timeout)
195 int nSuccessLess = 0;
196 int bytes_written = 0;
202 int selectresolution = 100;
204 fdflags = fcntl(*sock, F_GETFL);
205 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
207 while ((nSuccessLess < timeout) &&
209 (bytes_written < nbytes))
212 tv.tv_sec = selectresolution;
216 FD_SET(*sock, &rfds);
217 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
218 /// *Error = strerror(errno);
224 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
228 retval = write(*sock, &buf[bytes_written],
229 nbytes - bytes_written);
235 bytes_written = bytes_written + retval;
236 if (IsNonBlock && (bytes_written == nbytes)){
237 tv.tv_sec = selectresolution;
241 FD_SET(*sock, &rfds);
242 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
243 /// *Error = strerror(errno);
250 return (bytes_written);
256 * client_getln() ... Get a LF-terminated line of text from the client.
257 * (This is implemented in terms of client_read() and could be
258 * justifiably moved out of sysdep.c)
260 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
263 CitContext *CCC = MyContext();
266 FlushStrBuf(CCC->sMigrateBuf);
267 *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
269 i = StrLength(CCC->sMigrateBuf);
270 pCh = ChrPtr(CCC->sMigrateBuf);
272 memcpy(buf, pCh, i + 1);
274 FlushStrBuf(CCC->sMigrateBuf);
276 safestrncpy(&buf[i], "000", bufsize - i);
283 * Multiline version of sock_gets() ... this is a convenience function for
284 * client side protocol implementations. It only returns the first line of
285 * a multiline response, discarding the rest.
287 int ml_sock_gets(int *sock, char *buf, int nSec)
293 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
302 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
307 } while ((g >= 4) && (bigbuf[3] == '-'));
309 return (strlen(buf));
314 * sock_puts() - send line to server - implemented in terms of serv_write()
315 * Returns the number of bytes written, or -1 for error.
317 int sock_puts(int *sock, char *buf)
321 i = sock_write(sock, buf, strlen(buf));
324 j = sock_write(sock, "\n", 1);