From 24d3605d99513fd0ce11671a40ad3fca8a1caaf5 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 18 Nov 2004 11:57:49 +0000 Subject: r3843: If a connection to a DC is requested, open connections simultaeneously to all DCs found. The first one to reply wins. Volker (This used to be commit 84ac54aef2bd56b5c889d3b05b8828aceb8ae00e) --- source3/lib/util_sock.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) (limited to 'source3/lib') diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c index 8c16533bf9..27a7551e37 100644 --- a/source3/lib/util_sock.c +++ b/source3/lib/util_sock.c @@ -797,6 +797,142 @@ int open_socket_out(int type, struct in_addr *addr, int port ,int timeout) return res; } +/**************************************************************************** + Create an outgoing TCP socket to any of the addrs. This is for + simultaneous connects to port 445 and 139 of a host or even a variety + of DC's all of which are equivalent for our purposes. +**************************************************************************/ + +BOOL open_any_socket_out(struct sockaddr_in *addrs, int num_addrs, + int timeout, int *fd_index, int *fd) +{ + int i, resulting_index, res; + int *sockets; + BOOL good_connect; + + fd_set wr_fds; + struct timeval tv; + int maxfd; + + int connect_loop = 10000; /* 10 milliseconds */ + + timeout *= 1000; /* convert to microseconds */ + + sockets = malloc(num_addrs * sizeof(*sockets)); + + if (sockets == NULL) + return False; + + resulting_index = -1; + + for (i=0; imaxfd) + maxfd = sockets[i]; + } + + tv.tv_sec = 0; + tv.tv_usec = connect_loop; + + res = sys_select(maxfd+1, NULL, &wr_fds, NULL, &tv); + + if (res < 0) + goto done; + + if (res == 0) + goto next_round; + + for (i=0; i timeout) + connect_loop = timeout; + goto connect_again; + + done: + for (i=0; i= 0) + close(sockets[i]); + } + + if (resulting_index >= 0) { + *fd_index = resulting_index; + *fd = sockets[*fd_index]; + set_blocking(*fd, True); + } + + free(sockets); + + return (resulting_index >= 0); +} /**************************************************************************** Open a connected UDP socket to host on port **************************************************************************/ -- cgit