- if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
- lprintf(3, "can't connect to %s.%s: %s\n",
- host, service, strerror(errno));
- return(-1);
+ /*
+ * Try all available addresses until we connect to one or until we run out.
+ */
+ for (ai = res; ai != NULL; ai = ai->ai_next) {
+ sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (sock < 0) {
+ syslog(LOG_ERR, "socket() failed: %s", strerror(errno));
+ freeaddrinfo(res);
+ return(-1);
+ }
+ rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
+ if (rc >= 0) {
+ freeaddrinfo(res);
+ return(sock);
+ }
+ else {
+ syslog(LOG_ERR, "connect() failed: %s", strerror(errno));
+ close(sock);
+ }
+ }
+ freeaddrinfo(res);
+ return(-1);
+}
+
+
+
+/*
+ * Read data from the client socket.
+ *
+ * sock socket fd to read from
+ * buf buffer to read into
+ * bytes number of bytes to read
+ * timeout Number of seconds to wait before timing out
+ *
+ * Possible return values:
+ * 1 Requested number of bytes has been read.
+ * 0 Request timed out.
+ * -1 Connection is broken, or other error.
+ */
+int socket_read_blob(int *Socket, StrBuf * Target, int bytes, int timeout)
+{
+ CitContext *CCC = MyContext();
+ const char *Error;
+ int retval = 0;
+
+
+ retval = StrBufReadBLOBBuffered(Target,
+ CCC->SBuf.Buf,
+ &CCC->SBuf.ReadWritePointer,
+ Socket, 1, bytes, O_TERM, &Error);
+
+ if (retval < 0) {
+ syslog(LOG_CRIT, "socket_read_blob() failed: %s", Error);