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-2009 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>
39 #include <libcitadel.h>
45 #include "sysdep_decls.h"
47 #include "clientsocket.h"
49 #include "ctdl_module.h"
52 #define INADDR_NONE 0xffffffff
55 int sock_connect(char *host, char *service, char *protocol)
60 struct sockaddr_in sin;
61 struct sockaddr_in egress_sin;
64 if ((host == NULL) || IsEmptyStr(host))
66 if ((service == NULL) || IsEmptyStr(service))
68 if ((protocol == NULL) || IsEmptyStr(protocol))
71 memset(&sin, 0, sizeof(sin));
72 sin.sin_family = AF_INET;
74 pse = getservbyname(service, protocol);
76 sin.sin_port = pse->s_port;
77 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
78 CtdlLogPrintf(CTDL_CRIT, "Can't get %s service entry: %s\n",
79 service, strerror(errno));
82 phe = gethostbyname(host);
84 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
85 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
86 CtdlLogPrintf(CTDL_ERR, "Can't get %s host entry: %s\n",
87 host, strerror(errno));
90 if ((ppe = getprotobyname(protocol)) == 0) {
91 CtdlLogPrintf(CTDL_CRIT, "Can't get %s protocol entry: %s\n",
92 protocol, strerror(errno));
95 if (!strcmp(protocol, "udp")) {
101 s = socket(PF_INET, type, ppe->p_proto);
103 CtdlLogPrintf(CTDL_CRIT, "Can't create socket: %s\n", strerror(errno));
107 /* If citserver is bound to a specific IP address on the host, make
108 * sure we use that address for outbound connections.
110 memset(&egress_sin, 0, sizeof(egress_sin));
111 egress_sin.sin_family = AF_INET;
112 if (!IsEmptyStr(config.c_ip_addr)) {
113 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
114 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
115 egress_sin.sin_addr.s_addr = INADDR_ANY;
118 /* If this bind fails, no problem; we can still use INADDR_ANY */
119 bind(s, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
122 /* Now try to connect to the remote host. */
123 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
124 CtdlLogPrintf(CTDL_ERR, "Can't connect to %s:%s: %s\n",
125 host, service, strerror(errno));
136 * Read data from the client socket.
138 * sock socket fd to read from
139 * buf buffer to read into
140 * bytes number of bytes to read
141 * timeout Number of seconds to wait before timing out
143 * Possible return values:
144 * 1 Requested number of bytes has been read.
145 * 0 Request timed out.
146 * -1 Connection is broken, or other error.
148 int socket_read_blob(int *Socket,
158 retval = StrBufReadBLOBBuffered(Target,
167 CtdlLogPrintf(CTDL_CRIT,
176 int sock_read_to(int *sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
181 FlushStrBuf(CCC->MigrateBuf);
182 rc = socket_read_blob(sock,
193 if (StrLength(CCC->MigrateBuf) < bytes)
194 bytes = StrLength(CCC->MigrateBuf);
196 ChrPtr(CCC->MigrateBuf),
199 FlushStrBuf(CCC->MigrateBuf);
205 int CtdlSockGetLine(int *sock, StrBuf * Target, int nSec)
212 rc = StrBufTCP_read_buffered_line_fast(Target,
215 sock, nSec, 1, &Error);
216 if ((rc < 0) && (Error != NULL))
217 CtdlLogPrintf(CTDL_CRIT,
226 * client_getln() ... Get a LF-terminated line of text from the client.
227 * (This is implemented in terms of client_read() and could be
228 * justifiably moved out of sysdep.c)
230 int sock_getln(int *sock, char *buf, int bufsize)
236 FlushStrBuf(CCC->sMigrateBuf);
237 retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
239 i = StrLength(CCC->sMigrateBuf);
240 pCh = ChrPtr(CCC->sMigrateBuf);
242 memcpy(buf, pCh, i + 1);
244 FlushStrBuf(CCC->sMigrateBuf);
246 safestrncpy(&buf[i], "000", bufsize - i);
254 * sock_read() - input binary data from socket.
255 * Returns the number of bytes read, or -1 for error.
257 INLINE int sock_read(int *sock, char *buf, int bytes, int keep_reading_until_full)
259 return sock_read_to(sock, buf, bytes, CLIENT_TIMEOUT, keep_reading_until_full);
264 * sock_write() - send binary to server.
265 * Returns the number of bytes written, or -1 for error.
267 int sock_write(int *sock, const char *buf, int nbytes)
269 int nSuccessLess = 0;
270 int bytes_written = 0;
277 int selectresolution = 100;
279 fdflags = fcntl(*sock, F_GETFL);
280 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
282 while ((nSuccessLess < timeout) &&
284 (bytes_written < nbytes))
287 tv.tv_sec = selectresolution;
291 FD_SET(*sock, &rfds);
292 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
293 /// *Error = strerror(errno);
299 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
303 retval = write(*sock, &buf[bytes_written],
304 nbytes - bytes_written);
310 bytes_written = bytes_written + retval;
311 if (IsNonBlock && (bytes_written == nbytes)){
312 tv.tv_sec = selectresolution;
316 FD_SET(*sock, &rfds);
317 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
318 /// *Error = strerror(errno);
325 return (bytes_written);
331 * client_getln() ... Get a LF-terminated line of text from the client.
332 * (This is implemented in terms of client_read() and could be
333 * justifiably moved out of sysdep.c)
335 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
338 CitContext *CCC = MyContext();
341 FlushStrBuf(CCC->sMigrateBuf);
342 *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
344 i = StrLength(CCC->sMigrateBuf);
345 pCh = ChrPtr(CCC->sMigrateBuf);
347 memcpy(buf, pCh, i + 1);
349 FlushStrBuf(CCC->sMigrateBuf);
351 safestrncpy(&buf[i], "000", bufsize - i);
358 * Multiline version of sock_gets() ... this is a convenience function for
359 * client side protocol implementations. It only returns the first line of
360 * a multiline response, discarding the rest.
363 int ml_sock_gets(int *sock, char *buf, int nSec)
369 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
378 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
383 } while ((g >= 4) && (bigbuf[3] == '-'));
390 * sock_puts() - send line to server - implemented in terms of serv_write()
391 * Returns the number of bytes written, or -1 for error.
393 int sock_puts(int *sock, char *buf)
397 i = sock_write(sock, buf, strlen(buf));
399 j = sock_write(sock, "\n", 1);