800
800
/****************************************************************************
801
Create an outgoing TCP socket to any of the addrs. This is for
802
simultaneous connects to port 445 and 139 of a host or even a variety
803
of DC's all of which are equivalent for our purposes.
804
**************************************************************************/
806
BOOL open_any_socket_out(struct sockaddr_in *addrs, int num_addrs,
807
int timeout, int *fd_index, int *fd)
809
int i, resulting_index, res;
813
fd_set r_fds, wr_fds;
817
int connect_loop = 10000; /* 10 milliseconds */
819
timeout *= 1000; /* convert to microseconds */
821
sockets = SMB_MALLOC_ARRAY(int, num_addrs);
826
resulting_index = -1;
828
for (i=0; i<num_addrs; i++)
831
for (i=0; i<num_addrs; i++) {
832
sockets[i] = socket(PF_INET, SOCK_STREAM, 0);
835
set_blocking(sockets[i], False);
839
good_connect = False;
841
for (i=0; i<num_addrs; i++) {
843
if (sockets[i] == -1)
846
if (connect(sockets[i], (struct sockaddr *)&(addrs[i]),
847
sizeof(*addrs)) == 0) {
848
/* Rather unlikely as we are non-blocking, but it
849
* might actually happen. */
854
if (errno == EINPROGRESS || errno == EALREADY ||
856
/* These are the error messages that something is
859
} else if (errno != 0) {
860
/* There was a direct error */
867
/* All of the connect's resulted in real error conditions */
871
/* Lets see if any of the connect attempts succeeded */
877
for (i=0; i<num_addrs; i++) {
878
if (sockets[i] == -1)
880
FD_SET(sockets[i], &wr_fds);
881
FD_SET(sockets[i], &r_fds);
882
if (sockets[i]>maxfd)
887
tv.tv_usec = connect_loop;
889
res = sys_select(maxfd+1, &r_fds, &wr_fds, NULL, &tv);
897
for (i=0; i<num_addrs; i++) {
899
if (sockets[i] == -1)
902
/* Stevens, Network Programming says that if there's a
903
* successful connect, the socket is only writable. Upon an
904
* error, it's both readable and writable. */
906
if (FD_ISSET(sockets[i], &r_fds) &&
907
FD_ISSET(sockets[i], &wr_fds)) {
908
/* readable and writable, so it's an error */
914
if (!FD_ISSET(sockets[i], &r_fds) &&
915
FD_ISSET(sockets[i], &wr_fds)) {
916
/* Only writable, so it's connected */
924
timeout -= connect_loop;
928
if (connect_loop > timeout)
929
connect_loop = timeout;
933
for (i=0; i<num_addrs; i++) {
934
if (i == resulting_index)
940
if (resulting_index >= 0) {
941
*fd_index = resulting_index;
942
*fd = sockets[*fd_index];
943
set_blocking(*fd, True);
948
return (resulting_index >= 0);
950
/****************************************************************************
801
951
Open a connected UDP socket to host on port
802
952
**************************************************************************/