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 as published
11 * by the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
34 #include <arpa/inet.h>
41 #include <libcitadel.h>
47 #include "sysdep_decls.h"
49 #include "clientsocket.h"
50 #include "ctdl_module.h"
54 * Connect to a unix domain socket (normally called by sock_connect() passthru)
56 int uds_sock_connect(char *sockpath)
58 struct sockaddr_un addr;
61 memset(&addr, 0, sizeof(addr));
62 addr.sun_family = AF_UNIX;
63 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
65 s = socket(AF_UNIX, SOCK_STREAM, 0);
67 syslog(LOG_ERR, "socket() failed: %s", strerror(errno));
71 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
72 syslog(LOG_ERR, "connect() failed: %s", strerror(errno));
82 * Connect to a service via a client socket. This supports both IPv4 and IPv6.
83 * If the first character of the host name/addr is "/" then we assume the caller
84 * is actually trying to connect to a unix domain socket and we do that instead.
86 int sock_connect(char *host, char *service)
88 struct in6_addr serveraddr;
89 struct addrinfo hints;
90 struct addrinfo *res = NULL;
91 struct addrinfo *ai = NULL;
95 if ((host == NULL) || IsEmptyStr(host))
99 return uds_sock_connect(host);
102 if ((service == NULL) || IsEmptyStr(service))
105 memset(&hints, 0x00, sizeof(hints));
106 hints.ai_flags = AI_NUMERICSERV;
107 hints.ai_family = AF_UNSPEC;
108 hints.ai_socktype = SOCK_STREAM;
111 * Handle numeric IPv4 and IPv6 addresses
113 rc = inet_pton(AF_INET, host, &serveraddr);
114 if (rc == 1) { /* dotted quad */
115 hints.ai_family = AF_INET;
116 hints.ai_flags |= AI_NUMERICHOST;
118 rc = inet_pton(AF_INET6, host, &serveraddr);
119 if (rc == 1) { /* IPv6 address */
120 hints.ai_family = AF_INET6;
121 hints.ai_flags |= AI_NUMERICHOST;
125 /* Begin the connection process */
127 rc = getaddrinfo(host, service, &hints, &res);
129 syslog(LOG_ERR, "%s: %s", host, gai_strerror(rc));
134 * Try all available addresses until we connect to one or until we run out.
136 for (ai = res; ai != NULL; ai = ai->ai_next) {
137 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
139 syslog(LOG_ERR, "socket() failed: %s", strerror(errno));
143 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
149 syslog(LOG_ERR, "connect() failed: %s", strerror(errno));
160 * Read data from the client socket.
162 * sock socket fd to read from
163 * buf buffer to read into
164 * bytes number of bytes to read
165 * timeout Number of seconds to wait before timing out
167 * Possible return values:
168 * 1 Requested number of bytes has been read.
169 * 0 Request timed out.
170 * -1 Connection is broken, or other error.
172 int socket_read_blob(int *Socket, StrBuf * Target, int bytes, int timeout)
174 CitContext *CCC = MyContext();
179 retval = StrBufReadBLOBBuffered(Target,
181 &CCC->SBuf.ReadWritePointer,
182 Socket, 1, bytes, O_TERM, &Error);
185 syslog(LOG_CRIT, "socket_read_blob() failed: %s", Error);
191 int CtdlSockGetLine(int *sock, StrBuf * Target, int nSec)
193 CitContext *CCC = MyContext();
198 rc = StrBufTCP_read_buffered_line_fast(Target,
200 &CCC->SBuf.ReadWritePointer,
201 sock, nSec, 1, &Error);
202 if ((rc < 0) && (Error != NULL))
203 syslog(LOG_CRIT, "CtdlSockGetLine() failed: %s", Error);
209 * client_getln() ... Get a LF-terminated line of text from the client.
210 * (This is implemented in terms of client_read() and could be
211 * justifiably moved out of sysdep.c)
213 int sock_getln(int *sock, char *buf, int bufsize)
216 CitContext *CCC = MyContext();
219 FlushStrBuf(CCC->sMigrateBuf);
220 retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
222 i = StrLength(CCC->sMigrateBuf);
223 pCh = ChrPtr(CCC->sMigrateBuf);
225 memcpy(buf, pCh, i + 1);
227 FlushStrBuf(CCC->sMigrateBuf);
229 safestrncpy(&buf[i], "000", bufsize - i);
237 * sock_write() - send binary to server.
238 * Returns the number of bytes written, or -1 for error.
240 int sock_write(int *sock, const char *buf, int nbytes)
241 { return sock_write_timeout(sock, buf, nbytes, 50); }
242 int sock_write_timeout(int *sock, const char *buf, int nbytes, int timeout)
244 int nSuccessLess = 0;
245 int bytes_written = 0;
251 int selectresolution = 100;
253 fdflags = fcntl(*sock, F_GETFL);
254 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
256 while ((nSuccessLess < timeout) &&
258 (bytes_written < nbytes))
261 tv.tv_sec = selectresolution;
265 FD_SET(*sock, &rfds);
266 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
267 /// *Error = strerror(errno);
273 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
277 retval = write(*sock, &buf[bytes_written],
278 nbytes - bytes_written);
284 bytes_written = bytes_written + retval;
285 if (IsNonBlock && (bytes_written == nbytes)){
286 tv.tv_sec = selectresolution;
290 FD_SET(*sock, &rfds);
291 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
292 /// *Error = strerror(errno);
299 return (bytes_written);
305 * client_getln() ... Get a LF-terminated line of text from the client.
306 * (This is implemented in terms of client_read() and could be
307 * justifiably moved out of sysdep.c)
309 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
312 CitContext *CCC = MyContext();
315 FlushStrBuf(CCC->sMigrateBuf);
316 *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
318 i = StrLength(CCC->sMigrateBuf);
319 pCh = ChrPtr(CCC->sMigrateBuf);
321 memcpy(buf, pCh, i + 1);
323 FlushStrBuf(CCC->sMigrateBuf);
325 safestrncpy(&buf[i], "000", bufsize - i);
332 * Multiline version of sock_gets() ... this is a convenience function for
333 * client side protocol implementations. It only returns the first line of
334 * a multiline response, discarding the rest.
336 int ml_sock_gets(int *sock, char *buf, int nSec)
342 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
351 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
356 } while ((g >= 4) && (bigbuf[3] == '-'));
358 return (strlen(buf));
363 * sock_puts() - send line to server - implemented in terms of serv_write()
364 * Returns the number of bytes written, or -1 for error.
366 int sock_puts(int *sock, char *buf)
370 i = sock_write(sock, buf, strlen(buf));
373 j = sock_write(sock, "\n", 1);