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 free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * 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>
33 #include <arpa/inet.h>
40 #include <libcitadel.h>
46 #include "sysdep_decls.h"
48 #include "clientsocket.h"
49 #include "ctdl_module.h"
51 int sock_connect(char *host, char *service)
53 struct in6_addr serveraddr;
54 struct addrinfo hints;
55 struct addrinfo *res = NULL;
56 struct addrinfo *ai = NULL;
60 if ((host == NULL) || IsEmptyStr(host))
62 if ((service == NULL) || IsEmptyStr(service))
65 memset(&hints, 0x00, sizeof(hints));
66 hints.ai_flags = AI_NUMERICSERV;
67 hints.ai_family = AF_UNSPEC;
68 hints.ai_socktype = SOCK_STREAM;
71 * Handle numeric IPv4 and IPv6 addresses
73 rc = inet_pton(AF_INET, host, &serveraddr);
74 if (rc == 1) { /* dotted quad */
75 hints.ai_family = AF_INET;
76 hints.ai_flags |= AI_NUMERICHOST;
78 rc = inet_pton(AF_INET6, host, &serveraddr);
79 if (rc == 1) { /* IPv6 address */
80 hints.ai_family = AF_INET6;
81 hints.ai_flags |= AI_NUMERICHOST;
85 /* Begin the connection process */
87 rc = getaddrinfo(host, service, &hints, &res);
89 syslog(LOG_ERR, "%s: %s\n", host, gai_strerror(rc));
94 * Try all available addresses until we connect to one or until we run out.
96 for (ai = res; ai != NULL; ai = ai->ai_next) {
97 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
99 syslog(LOG_ERR, "socket() failed: %s\n", strerror(errno));
103 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
109 syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
120 * Read data from the client socket.
122 * sock socket fd to read from
123 * buf buffer to read into
124 * bytes number of bytes to read
125 * timeout Number of seconds to wait before timing out
127 * Possible return values:
128 * 1 Requested number of bytes has been read.
129 * 0 Request timed out.
130 * -1 Connection is broken, or other error.
132 int socket_read_blob(int *Socket, StrBuf * Target, int bytes, int timeout)
134 CitContext *CCC = MyContext();
139 retval = StrBufReadBLOBBuffered(Target,
141 &CCC->SBuf.ReadWritePointer,
142 Socket, 1, bytes, O_TERM, &Error);
145 "%s failed: %s\n", __FUNCTION__, Error);
151 int CtdlSockGetLine(int *sock, StrBuf * Target, int nSec)
153 CitContext *CCC = MyContext();
158 rc = StrBufTCP_read_buffered_line_fast(Target,
160 &CCC->SBuf.ReadWritePointer,
161 sock, nSec, 1, &Error);
162 if ((rc < 0) && (Error != NULL))
164 "%s failed: %s\n", __FUNCTION__, Error);
170 * client_getln() ... Get a LF-terminated line of text from the client.
171 * (This is implemented in terms of client_read() and could be
172 * justifiably moved out of sysdep.c)
174 int sock_getln(int *sock, char *buf, int bufsize)
177 CitContext *CCC = MyContext();
180 FlushStrBuf(CCC->sMigrateBuf);
181 retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
183 i = StrLength(CCC->sMigrateBuf);
184 pCh = ChrPtr(CCC->sMigrateBuf);
186 memcpy(buf, pCh, i + 1);
188 FlushStrBuf(CCC->sMigrateBuf);
190 safestrncpy(&buf[i], "000", bufsize - i);
198 * sock_write() - send binary to server.
199 * Returns the number of bytes written, or -1 for error.
201 int sock_write(int *sock, const char *buf, int nbytes)
202 { return sock_write_timeout(sock, buf, nbytes, 50); }
203 int sock_write_timeout(int *sock, const char *buf, int nbytes, int timeout)
205 int nSuccessLess = 0;
206 int bytes_written = 0;
212 int selectresolution = 100;
214 fdflags = fcntl(*sock, F_GETFL);
215 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
217 while ((nSuccessLess < timeout) &&
219 (bytes_written < nbytes))
222 tv.tv_sec = selectresolution;
226 FD_SET(*sock, &rfds);
227 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
228 /// *Error = strerror(errno);
234 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
238 retval = write(*sock, &buf[bytes_written],
239 nbytes - bytes_written);
245 bytes_written = bytes_written + retval;
246 if (IsNonBlock && (bytes_written == nbytes)){
247 tv.tv_sec = selectresolution;
251 FD_SET(*sock, &rfds);
252 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
253 /// *Error = strerror(errno);
260 return (bytes_written);
266 * client_getln() ... Get a LF-terminated line of text from the client.
267 * (This is implemented in terms of client_read() and could be
268 * justifiably moved out of sysdep.c)
270 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
273 CitContext *CCC = MyContext();
276 FlushStrBuf(CCC->sMigrateBuf);
277 *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
279 i = StrLength(CCC->sMigrateBuf);
280 pCh = ChrPtr(CCC->sMigrateBuf);
282 memcpy(buf, pCh, i + 1);
284 FlushStrBuf(CCC->sMigrateBuf);
286 safestrncpy(&buf[i], "000", bufsize - i);
293 * Multiline version of sock_gets() ... this is a convenience function for
294 * client side protocol implementations. It only returns the first line of
295 * a multiline response, discarding the rest.
297 int ml_sock_gets(int *sock, char *buf, int nSec)
303 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
312 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
317 } while ((g >= 4) && (bigbuf[3] == '-'));
319 return (strlen(buf));
324 * sock_puts() - send line to server - implemented in terms of serv_write()
325 * Returns the number of bytes written, or -1 for error.
327 int sock_puts(int *sock, char *buf)
331 i = sock_write(sock, buf, strlen(buf));
334 j = sock_write(sock, "\n", 1);