4 * This module handles client-side sockets opened by the Citadel server (for
5 * the client side of Internet protocols, etc.) It does _not_ handle client
6 * sockets for the Citadel client; for that you must look in ipc_c_tcp.c
7 * (which, uncoincidentally, bears a striking similarity to this file).
9 * Copyright (c) 1987-2010 by the citadel.org team
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
41 #include <libcitadel.h>
47 #include "sysdep_decls.h"
49 #include "clientsocket.h"
50 #include "ctdl_module.h"
52 int sock_connect(char *host, char *service)
54 struct in6_addr serveraddr;
55 struct addrinfo hints, *res = NULL;
59 if ((host == NULL) || IsEmptyStr(host))
61 if ((service == NULL) || IsEmptyStr(service))
64 memset(&hints, 0x00, sizeof(hints));
65 hints.ai_flags = AI_NUMERICSERV;
66 hints.ai_family = AF_UNSPEC;
67 hints.ai_socktype = SOCK_STREAM;
70 * Handle numeric IPv4 and IPv6 addresses
72 rc = inet_pton(AF_INET, host, &serveraddr);
73 if (rc == 1) { /* dotted quad */
74 hints.ai_family = AF_INET;
75 hints.ai_flags |= AI_NUMERICHOST;
77 rc = inet_pton(AF_INET6, host, &serveraddr);
78 if (rc == 1) { /* IPv6 address */
79 hints.ai_family = AF_INET6;
80 hints.ai_flags |= AI_NUMERICHOST;
84 /* Begin the connection process */
86 rc = getaddrinfo(host, service, &hints, &res);
88 CtdlLogPrintf(CTDL_ERR, "%s: %s\n", host, gai_strerror(rc));
93 * 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 /* FIXME display the address to which we are trying to connect */
99 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
101 CtdlLogPrintf(CTDL_ERR, "socket() failed: %s\n", strerror(errno));
105 rc = connect(sock, res->ai_addr, res->ai_addrlen);
110 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
121 * Read data from the client socket.
123 * sock socket fd to read from
124 * buf buffer to read into
125 * bytes number of bytes to read
126 * timeout Number of seconds to wait before timing out
128 * Possible return values:
129 * 1 Requested number of bytes has been read.
130 * 0 Request timed out.
131 * -1 Connection is broken, or other error.
133 int socket_read_blob(int *Socket, StrBuf * Target, int bytes, int timeout)
135 CitContext *CCC = MyContext();
140 retval = StrBufReadBLOBBuffered(Target,
143 Socket, 1, bytes, O_TERM, &Error);
145 CtdlLogPrintf(CTDL_CRIT,
146 "%s failed: %s\n", __FUNCTION__, Error);
152 int sock_read_to(int *sock, char *buf, int bytes, int timeout,
153 int keep_reading_until_full)
155 CitContext *CCC = MyContext();
158 FlushStrBuf(CCC->MigrateBuf);
159 rc = socket_read_blob(sock, CCC->sMigrateBuf, bytes, timeout);
164 if (StrLength(CCC->MigrateBuf) < bytes)
165 bytes = StrLength(CCC->MigrateBuf);
166 memcpy(buf, ChrPtr(CCC->MigrateBuf), bytes);
168 FlushStrBuf(CCC->MigrateBuf);
174 int CtdlSockGetLine(int *sock, StrBuf * Target)
176 CitContext *CCC = MyContext();
181 rc = StrBufTCP_read_buffered_line_fast(Target,
185 if ((rc < 0) && (Error != NULL))
186 CtdlLogPrintf(CTDL_CRIT,
187 "%s failed: %s\n", __FUNCTION__, Error);
193 * client_getln() ... Get a LF-terminated line of text from the client.
194 * (This is implemented in terms of client_read() and could be
195 * justifiably moved out of sysdep.c)
197 int sock_getln(int *sock, char *buf, int bufsize)
200 CitContext *CCC = MyContext();
203 FlushStrBuf(CCC->sMigrateBuf);
204 retval = CtdlSockGetLine(sock, CCC->sMigrateBuf);
206 i = StrLength(CCC->sMigrateBuf);
207 pCh = ChrPtr(CCC->sMigrateBuf);
208 /* Strip the trailing LF, and the trailing CR if present.
213 && ((pCh[i - 1] == 13)
214 || (pCh[i - 1] == 10))) {
220 FlushStrBuf(CCC->sMigrateBuf);
222 safestrncpy(&buf[i], "000", bufsize - i);
230 * sock_read() - input binary data from socket.
231 * Returns the number of bytes read, or -1 for error.
233 INLINE int sock_read(int *sock, char *buf, int bytes,
234 int keep_reading_until_full)
236 return sock_read_to(sock, buf, bytes, CLIENT_TIMEOUT,
237 keep_reading_until_full);
242 * sock_write() - send binary to server.
243 * Returns the number of bytes written, or -1 for error.
245 int sock_write(int *sock, const char *buf, int nbytes)
247 int bytes_written = 0;
250 while ((*sock != -1) && (bytes_written < nbytes)) {
251 retval = write(*sock, &buf[bytes_written],
252 nbytes - bytes_written);
258 bytes_written = bytes_written + retval;
260 return (bytes_written);
265 * Multiline version of sock_gets() ... this is a convenience function for
266 * client side protocol implementations. It only returns the first line of
267 * a multiline response, discarding the rest.
269 int ml_sock_gets(int *sock, char *buf)
274 g = sock_getln(sock, buf, SIZ);
281 g = sock_getln(sock, bigbuf, SIZ);
284 } while ((g >= 4) && (bigbuf[3] == '-'));
286 return (strlen(buf));
291 * sock_puts() - send line to server - implemented in terms of serv_write()
292 * Returns the number of bytes written, or -1 for error.
294 int sock_puts(int *sock, char *buf)
298 i = sock_write(sock, buf, strlen(buf));
301 j = sock_write(sock, "\n", 1);