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 open source software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License, version 3.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
23 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
34 #include <libcitadel.h>
40 #include "sysdep_decls.h"
42 #include "clientsocket.h"
43 #include "ctdl_module.h"
45 int sock_connect(char *host, char *service)
47 struct in6_addr serveraddr;
48 struct addrinfo hints;
49 struct addrinfo *res = NULL;
50 struct addrinfo *ai = NULL;
54 if ((host == NULL) || IsEmptyStr(host))
56 if ((service == NULL) || IsEmptyStr(service))
59 memset(&hints, 0x00, sizeof(hints));
60 hints.ai_flags = AI_NUMERICSERV;
61 hints.ai_family = AF_UNSPEC;
62 hints.ai_socktype = SOCK_STREAM;
65 * Handle numeric IPv4 and IPv6 addresses
67 rc = inet_pton(AF_INET, host, &serveraddr);
68 if (rc == 1) { /* dotted quad */
69 hints.ai_family = AF_INET;
70 hints.ai_flags |= AI_NUMERICHOST;
72 rc = inet_pton(AF_INET6, host, &serveraddr);
73 if (rc == 1) { /* IPv6 address */
74 hints.ai_family = AF_INET6;
75 hints.ai_flags |= AI_NUMERICHOST;
79 /* Begin the connection process */
81 rc = getaddrinfo(host, service, &hints, &res);
83 syslog(LOG_ERR, "%s: %s", host, gai_strerror(rc));
88 * Try all available addresses until we connect to one or until we run out.
90 for (ai = res; ai != NULL; ai = ai->ai_next) {
91 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
93 syslog(LOG_ERR, "socket() failed: %s", strerror(errno));
97 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
103 syslog(LOG_ERR, "connect() failed: %s", strerror(errno));
114 * Read data from the client socket.
116 * sock socket fd to read from
117 * buf buffer to read into
118 * bytes number of bytes to read
119 * timeout Number of seconds to wait before timing out
121 * Possible return values:
122 * 1 Requested number of bytes has been read.
123 * 0 Request timed out.
124 * -1 Connection is broken, or other error.
126 int socket_read_blob(int *Socket, StrBuf * Target, int bytes, int timeout)
128 CitContext *CCC = MyContext();
133 retval = StrBufReadBLOBBuffered(Target,
135 &CCC->SBuf.ReadWritePointer,
136 Socket, 1, bytes, O_TERM, &Error);
139 syslog(LOG_CRIT, "socket_read_blob() failed: %s", Error);
145 int CtdlSockGetLine(int *sock, StrBuf * Target, int nSec)
147 CitContext *CCC = MyContext();
152 rc = StrBufTCP_read_buffered_line_fast(Target,
154 &CCC->SBuf.ReadWritePointer,
155 sock, nSec, 1, &Error);
156 if ((rc < 0) && (Error != NULL))
157 syslog(LOG_CRIT, "CtdlSockGetLine() failed: %s", Error);
163 * client_getln() ... Get a LF-terminated line of text from the client.
164 * (This is implemented in terms of client_read() and could be
165 * justifiably moved out of sysdep.c)
167 int sock_getln(int *sock, char *buf, int bufsize)
170 CitContext *CCC = MyContext();
173 FlushStrBuf(CCC->sMigrateBuf);
174 retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);
176 i = StrLength(CCC->sMigrateBuf);
177 pCh = ChrPtr(CCC->sMigrateBuf);
179 memcpy(buf, pCh, i + 1);
181 FlushStrBuf(CCC->sMigrateBuf);
183 safestrncpy(&buf[i], "000", bufsize - i);
191 * sock_write() - send binary to server.
192 * Returns the number of bytes written, or -1 for error.
194 int sock_write(int *sock, const char *buf, int nbytes)
195 { return sock_write_timeout(sock, buf, nbytes, 50); }
196 int sock_write_timeout(int *sock, const char *buf, int nbytes, int timeout)
198 int nSuccessLess = 0;
199 int bytes_written = 0;
205 int selectresolution = 100;
207 fdflags = fcntl(*sock, F_GETFL);
208 IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
210 while ((nSuccessLess < timeout) &&
212 (bytes_written < nbytes))
215 tv.tv_sec = selectresolution;
219 FD_SET(*sock, &rfds);
220 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
221 /// *Error = strerror(errno);
227 if (IsNonBlock && ! FD_ISSET(*sock, &rfds)) {
231 retval = write(*sock, &buf[bytes_written],
232 nbytes - bytes_written);
238 bytes_written = bytes_written + retval;
239 if (IsNonBlock && (bytes_written == nbytes)){
240 tv.tv_sec = selectresolution;
244 FD_SET(*sock, &rfds);
245 if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
246 /// *Error = strerror(errno);
253 return (bytes_written);
259 * client_getln() ... Get a LF-terminated line of text from the client.
260 * (This is implemented in terms of client_read() and could be
261 * justifiably moved out of sysdep.c)
263 int sock_getln_err(int *sock, char *buf, int bufsize, int *rc, int nSec)
266 CitContext *CCC = MyContext();
269 FlushStrBuf(CCC->sMigrateBuf);
270 *rc = retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, nSec);
272 i = StrLength(CCC->sMigrateBuf);
273 pCh = ChrPtr(CCC->sMigrateBuf);
275 memcpy(buf, pCh, i + 1);
277 FlushStrBuf(CCC->sMigrateBuf);
279 safestrncpy(&buf[i], "000", bufsize - i);
286 * Multiline version of sock_gets() ... this is a convenience function for
287 * client side protocol implementations. It only returns the first line of
288 * a multiline response, discarding the rest.
290 int ml_sock_gets(int *sock, char *buf, int nSec)
296 g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
305 g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
310 } while ((g >= 4) && (bigbuf[3] == '-'));
312 return (strlen(buf));
317 * sock_puts() - send line to server - implemented in terms of serv_write()
318 * Returns the number of bytes written, or -1 for error.
320 int sock_puts(int *sock, char *buf)
324 i = sock_write(sock, buf, strlen(buf));
327 j = sock_write(sock, "\n", 1);