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-2009 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"
51 #include "ctdl_module.h"
54 #define INADDR_NONE 0xffffffff
57 int sock_connect(char *host, char *service, char *protocol)
62 struct sockaddr_in sin;
63 struct sockaddr_in egress_sin;
66 if ((host == NULL) || IsEmptyStr(host))
68 if ((service == NULL) || IsEmptyStr(service))
70 if ((protocol == NULL) || IsEmptyStr(protocol))
73 memset(&sin, 0, sizeof(sin));
74 sin.sin_family = AF_INET;
76 pse = getservbyname(service, protocol);
78 sin.sin_port = pse->s_port;
79 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
80 CtdlLogPrintf(CTDL_CRIT, "Can't get %s service entry: %s\n",
81 service, strerror(errno));
84 phe = gethostbyname(host);
86 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
87 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
88 CtdlLogPrintf(CTDL_ERR, "Can't get %s host entry: %s\n",
89 host, strerror(errno));
92 if ((ppe = getprotobyname(protocol)) == 0) {
93 CtdlLogPrintf(CTDL_CRIT, "Can't get %s protocol entry: %s\n",
94 protocol, strerror(errno));
97 if (!strcmp(protocol, "udp")) {
103 s = socket(PF_INET, type, ppe->p_proto);
105 CtdlLogPrintf(CTDL_CRIT, "Can't create socket: %s\n", strerror(errno));
109 /* If citserver is bound to a specific IP address on the host, make
110 * sure we use that address for outbound connections.
112 memset(&egress_sin, 0, sizeof(egress_sin));
113 egress_sin.sin_family = AF_INET;
114 if (!IsEmptyStr(config.c_ip_addr)) {
115 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
116 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
117 egress_sin.sin_addr.s_addr = INADDR_ANY;
120 /* If this bind fails, no problem; we can still use INADDR_ANY */
121 bind(s, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
124 /* Now try to connect to the remote host. */
125 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
126 CtdlLogPrintf(CTDL_ERR, "Can't connect to %s:%s: %s\n",
127 host, service, strerror(errno));
138 * sock_read_to() - input binary data from socket, with a settable timeout.
139 * Returns the number of bytes read, or -1 for error.
140 * If keep_reading_until_full is nonzero, we keep reading until we get the number of requested bytes
142 int sock_read_to(int sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
156 retval = select(sock+1, &rfds, NULL, NULL, &tv);
158 if (FD_ISSET(sock, &rfds) == 0) { /* timed out */
159 CtdlLogPrintf(CTDL_ERR, "sock_read() timed out.\n");
163 rlen = read(sock, &buf[len], bytes-len);
165 CtdlLogPrintf(CTDL_ERR, "sock_read() failed: %s\n",
170 if (!keep_reading_until_full) return(len);
177 * sock_read() - input binary data from socket.
178 * Returns the number of bytes read, or -1 for error.
180 INLINE int sock_read(int sock, char *buf, int bytes, int keep_reading_until_full)
182 return sock_read_to(sock, buf, bytes, CLIENT_TIMEOUT, keep_reading_until_full);
187 * sock_write() - send binary to server.
188 * Returns the number of bytes written, or -1 for error.
190 int sock_write(int sock, char *buf, int nbytes)
192 int bytes_written = 0;
194 while (bytes_written < nbytes) {
195 retval = write(sock, &buf[bytes_written],
196 nbytes - bytes_written);
200 bytes_written = bytes_written + retval;
202 return (bytes_written);
208 * Input string from socket - implemented in terms of sock_read()
211 int sock_getln(int sock, char *buf, int bufsize)
215 /* Read one character at a time.
218 if (sock_read(sock, &buf[i], 1, 1) < 0) return(-1);
219 if (buf[i] == '\n' || i == (bufsize-1))
223 /* If we got a long line, discard characters until the newline.
225 if (i == (bufsize-1))
226 while (buf[i] != '\n')
227 if (sock_read(sock, &buf[i], 1, 1) < 0) return(-1);
229 /* Strip any trailing CR and LF characters.
233 && ( (buf[i - 1]==13)
234 || ( buf[i - 1]==10)) ) {
242 * Multiline version of sock_gets() ... this is a convenience function for
243 * client side protocol implementations. It only returns the first line of
244 * a multiline response, discarding the rest.
246 int ml_sock_gets(int sock, char *buf) {
250 g = sock_getln(sock, buf, SIZ);
251 if (g < 4) return(g);
252 if (buf[3] != '-') return(g);
255 g = sock_getln(sock, bigbuf, SIZ);
256 if (g < 0) return(g);
257 } while ( (g >= 4) && (bigbuf[3] == '-') );
264 * sock_puts() - send line to server - implemented in terms of serv_write()
265 * Returns the number of bytes written, or -1 for error.
267 int sock_puts(int sock, char *buf)
271 i = sock_write(sock, buf, strlen(buf));
273 j = sock_write(sock, "\n", 1);