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>
34 #include <arpa/inet.h>
40 #include <libcitadel.h>
46 #include "sysdep_decls.h"
48 #include "clientsocket.h"
50 #include "ctdl_module.h"
53 #define INADDR_NONE 0xffffffff
57 int uds_sock_connect(char *sockpath)
59 struct sockaddr_un addr;
62 memset(&addr, 0, sizeof(addr));
63 addr.sun_family = AF_UNIX;
64 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
66 s = socket(AF_UNIX, SOCK_STREAM, 0);
71 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
80 int sock_connect(char *host, char *service, char *protocol)
85 struct sockaddr_in sin;
86 struct sockaddr_in egress_sin;
89 if ((host == NULL) || IsEmptyStr(host))
93 return uds_sock_connect(host);
96 if ((service == NULL) || IsEmptyStr(service))
98 if ((protocol == NULL) || IsEmptyStr(protocol))
101 memset(&sin, 0, sizeof(sin));
102 sin.sin_family = AF_INET;
104 pse = getservbyname(service, protocol);
106 sin.sin_port = pse->s_port;
107 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
108 CtdlLogPrintf(CTDL_CRIT, "Can't get %s service entry: %s\n",
109 service, strerror(errno));
112 phe = gethostbyname(host);
114 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
115 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
116 CtdlLogPrintf(CTDL_ERR, "Can't get %s host entry: %s\n",
117 host, strerror(errno));
120 if ((ppe = getprotobyname(protocol)) == 0) {
121 CtdlLogPrintf(CTDL_CRIT, "Can't get %s protocol entry: %s\n",
122 protocol, strerror(errno));
125 if (!strcmp(protocol, "udp")) {
131 s = socket(PF_INET, type, ppe->p_proto);
133 CtdlLogPrintf(CTDL_CRIT, "Can't create socket: %s\n", strerror(errno));
137 /* If citserver is bound to a specific IP address on the host, make
138 * sure we use that address for outbound connections.
140 memset(&egress_sin, 0, sizeof(egress_sin));
141 egress_sin.sin_family = AF_INET;
142 if (!IsEmptyStr(config.c_ip_addr)) {
143 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
144 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
145 egress_sin.sin_addr.s_addr = INADDR_ANY;
148 /* If this bind fails, no problem; we can still use INADDR_ANY */
149 bind(s, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
152 /* Now try to connect to the remote host. */
153 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
154 CtdlLogPrintf(CTDL_ERR, "Can't connect to %s:%s: %s\n",
155 host, service, strerror(errno));
166 * Read data from the client socket.
168 * sock socket fd to read from
169 * buf buffer to read into
170 * bytes number of bytes to read
171 * timeout Number of seconds to wait before timing out
173 * Possible return values:
174 * 1 Requested number of bytes has been read.
175 * 0 Request timed out.
176 * -1 Connection is broken, or other error.
178 int socket_read_blob(int *Socket,
188 retval = StrBufReadBLOBBuffered(Target,
197 CtdlLogPrintf(CTDL_CRIT,
206 int sock_read_to(int *sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
211 FlushStrBuf(CCC->MigrateBuf);
212 rc = socket_read_blob(sock,
223 if (StrLength(CCC->MigrateBuf) < bytes)
224 bytes = StrLength(CCC->MigrateBuf);
226 ChrPtr(CCC->MigrateBuf),
229 FlushStrBuf(CCC->MigrateBuf);
235 int CtdlSockGetLine(int *sock, StrBuf * Target, int nSec)
242 rc = StrBufTCP_read_buffered_line_fast(Target,
245 sock, nSec, 1, &Error);
246 if ((rc < 0) && (Error != NULL))
247 CtdlLogPrintf(CTDL_CRIT,
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(int *sock, char *buf, int bufsize)
266 FlushStrBuf(CCC->sMigrateBuf);
267 retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
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);
284 * sock_read() - input binary data from socket.
285 * Returns the number of bytes read, or -1 for error.
287 INLINE int sock_read(int *sock, char *buf, int bytes, int keep_reading_until_full)
289 return sock_read_to(sock, buf, bytes, CLIENT_TIMEOUT, keep_reading_until_full);
294 * sock_write() - send binary to server.
295 * Returns the number of bytes written, or -1 for error.
297 int sock_write(int *sock, const char *buf, int nbytes)
298 { return sock_write_timeout(sock, buf, nbytes, 50); }
299 int sock_write_timeout(int *sock, const char *buf, int nbytes, int timeout)
301 int nSuccessLess = 0;
302 int bytes_written = 0;
308 int selectresolution = 100;
310 fdflags = fcntl(*sock, F_GETFL);
311 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
313 while ((nSuccessLess < timeout) &&
315 (bytes_written < nbytes))
318 tv.tv_sec = selectresolution;
322 FD_SET(*sock, &rfds);
323 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
324 /// *Error = strerror(errno);
330 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
334 retval = write(*sock, &buf[bytes_written],
335 nbytes - bytes_written);
341 bytes_written = bytes_written + retval;
342 if (IsNonBlock && (bytes_written == nbytes)){
343 tv.tv_sec = selectresolution;
347 FD_SET(*sock, &rfds);
348 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
349 /// *Error = strerror(errno);
356 return (bytes_written);
362 * client_getln() ... Get a LF-terminated line of text from the client.
363 * (This is implemented in terms of client_read() and could be
364 * justifiably moved out of sysdep.c)
366 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
369 CitContext *CCC = MyContext();
372 FlushStrBuf(CCC->sMigrateBuf);
373 *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
375 i = StrLength(CCC->sMigrateBuf);
376 pCh = ChrPtr(CCC->sMigrateBuf);
378 memcpy(buf, pCh, i + 1);
380 FlushStrBuf(CCC->sMigrateBuf);
382 safestrncpy(&buf[i], "000", bufsize - i);
389 * Multiline version of sock_gets() ... this is a convenience function for
390 * client side protocol implementations. It only returns the first line of
391 * a multiline response, discarding the rest.
394 int ml_sock_gets(int *sock, char *buf, int nSec)
400 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
409 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
414 } while ((g >= 4) && (bigbuf[3] == '-'));
421 * sock_puts() - send line to server - implemented in terms of serv_write()
422 * Returns the number of bytes written, or -1 for error.
424 int sock_puts(int *sock, char *buf)
428 i = sock_write(sock, buf, strlen(buf));
430 j = sock_write(sock, "\n", 1);