#include "rsync.h"
-/* Establish a proxy connection on an open socket to a web roxy by
- * using the CONNECT method. */
+/**
+ * Establish a proxy connection on an open socket to a web proxy by
+ * using the HTTP CONNECT method.
+ **/
static int establish_proxy_connection(int fd, char *host, int port)
{
char buffer[1024];
buffer);
return -1;
}
- for (cp = &buffer[5]; isdigit(*cp) || (*cp == '.'); cp++)
+ for (cp = &buffer[5]; isdigit(* (unsigned char *) cp) || (*cp == '.'); cp++)
;
while (*cp == ' ')
cp++;
for (r = bres_all; r; r = r->ai_next) {
if (bind(s, r->ai_addr, r->ai_addrlen) == -1)
continue;
+ freeaddrinfo(bres_all);
return s;
}
/* no error message; there might be some problem that allows
* creation of the socket but not binding, perhaps if the
* machine has no ipv6 address of this name. */
+ freeaddrinfo(bres_all);
return -1;
}
*cp++ = '\0';
strcpy(portbuf, cp);
h = buffer;
+ if (verbose >= 2) {
+ rprintf(FINFO, "connection via http proxy %s port %s\n",
+ h, portbuf);
+ }
} else {
snprintf(portbuf, sizeof(portbuf), "%d", port);
h = host;
close(s);
continue;
}
-
+
+ freeaddrinfo(all_ai);
return s;
}
}
-void start_accept_loop(int port, int (*fn)(int ))
+void start_accept_loop(int port, int (*fn)(int, int))
{
int s;
extern char *bind_address;
for each incoming connection */
while (1) {
fd_set fds;
+ pid_t pid;
int fd;
struct sockaddr_storage addr;
socklen_t addrlen = sizeof addr;
while (waitpid(-1, NULL, WNOHANG) > 0);
#endif
- if (fork()==0) {
+ if ((pid = fork()) == 0) {
+ int ret;
close(s);
/* open log file in child before possibly giving
up privileges */
log_open();
- _exit(fn(fd));
+ ret = fn(fd, fd);
+ close_all();
+ _exit(ret);
+ } else if (pid < 0) {
+ rprintf(FERROR,
+ RSYNC_NAME
+ ": could not create child server process: %s\n",
+ strerror(errno));
+ close(fd);
+ /* This might have happened because we're
+ * overloaded. Sleep briefly before trying to
+ * accept again. */
+ sleep(2);
+ } else {
+ /* Parent doesn't need this fd anymore. */
+ close(fd);
}
-
- close(fd);
}
}
-/****************************************************************************
-set user socket options
-****************************************************************************/
+/**
+ * Set user socket options
+ **/
void set_socket_options(int fd, char *options)
{
char *tok;
free(options);
}
-/****************************************************************************
-become a daemon, discarding the controlling terminal
-****************************************************************************/
+/**
+ * Become a daemon, discarding the controlling terminal
+ **/
void become_daemon(void)
{
int i;
}
-/*******************************************************************
-this is like socketpair but uses tcp. It is used by the Samba
-regression test code
-The function guarantees that nobody else can attach to the socket,
-or if they do that this function fails and the socket gets closed
-returns 0 on success, -1 on failure
-the resulting file descriptors are symmetrical
- ******************************************************************/
+/**
+ * This is like socketpair but uses tcp. It is used by the Samba
+ * regression test code.
+ *
+ * The function guarantees that nobody else can attach to the socket,
+ * or if they do that this function fails and the socket gets closed
+ * returns 0 on success, -1 on failure the resulting file descriptors
+ * are symmetrical.
+ **/
static int socketpair_tcp(int fd[2])
{
int listener;
if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
memset(&sock2, 0, sizeof(sock2));
-#ifdef HAVE_SOCK_SIN_LEN
+#ifdef HAVE_SOCKADDR_LEN
sock2.sin_len = sizeof(sock2);
#endif
sock2.sin_family = PF_INET;