-
- bzero((char *)&sock,sizeof(sock));
- memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
- sock.sin_port = htons(port);
- sock.sin_family = hp->h_addrtype;
- sock.sin_addr.s_addr = INADDR_ANY;
- res = socket(hp->h_addrtype, type, 0);
- if (res == -1) {
- rprintf(FERROR,"socket failed\n");
- return -1;
+ return s;
+}
+
+
+/**
+ * Open an outgoing socket, but allow for it to be intercepted by
+ * $RSYNC_CONNECT_PROG, which will execute a program across a TCP
+ * socketpair rather than really opening a socket.
+ *
+ * We use this primarily in testing to detect TCP flow bugs, but not
+ * cause security problems by really opening remote connections.
+ *
+ * This is based on the Samba LIBSMB_PROG feature.
+ *
+ * @param bind_address Local address to use. Normally NULL to get the stack default.
+ **/
+int open_socket_out_wrapped(char *host, int port, const char *bind_address,
+ int af_hint)
+{
+ char *prog;
+
+ if ((prog = getenv("RSYNC_CONNECT_PROG")) != NULL)
+ return sock_exec(prog);
+ return open_socket_out(host, port, bind_address, af_hint);
+}
+
+
+
+/**
+ * Open a socket of the specified type, port and address for incoming data
+ *
+ * Try to be better about handling the results of getaddrinfo(): when
+ * opening an inbound socket, we might get several address results,
+ * e.g. for the machine's ipv4 and ipv6 name.
+ *
+ * If binding a wildcard, then any one of them should do. If an address
+ * was specified but it's insufficiently specific then that's not our
+ * fault.
+ *
+ * However, some of the advertized addresses may not work because e.g. we
+ * don't have IPv6 support in the kernel. In that case go on and try all
+ * addresses until one succeeds.
+ *
+ * @param bind_address Local address to bind, or NULL to allow it to
+ * default.
+ **/
+static int *open_socket_in(int type, int port, const char *bind_address,
+ int af_hint)
+{
+ int one=1;
+ int s, *sp, *socks, maxs;
+ struct addrinfo hints, *all_ai, *resp;
+ char portbuf[10];
+ int error;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = af_hint;
+ hints.ai_socktype = type;
+ hints.ai_flags = AI_PASSIVE;
+ snprintf(portbuf, sizeof(portbuf), "%d", port);
+ error = getaddrinfo(bind_address, portbuf, &hints, &all_ai);
+ if (error) {
+ rprintf(FERROR, RSYNC_NAME ": getaddrinfo: bind address %s: %s\n",
+ bind_address, gai_strerror(error));
+ return NULL;
+ }
+
+ /* Count max number of sockets we might open. */
+ for (maxs = 0, resp = all_ai; resp; resp = resp->ai_next, maxs++) {}
+ socks = new_array(int, maxs + 1);
+ if (!socks) {
+ rprintf(FERROR,
+ RSYNC_NAME "couldn't allocate memory for sockets");
+ return NULL;