2010-10-26 18:53:50 +04:00
|
|
|
/*
|
2014-07-27 00:17:09 +04:00
|
|
|
* Copyright (c) 2010 High Performance Computing Center Stuttgart,
|
2010-10-26 18:53:50 +04:00
|
|
|
* University of Stuttgart. All rights reserved.
|
|
|
|
* $COPYRIGHT$
|
2014-07-27 00:17:09 +04:00
|
|
|
*
|
2010-10-26 18:53:50 +04:00
|
|
|
* Additional copyrights may follow
|
2014-07-27 00:17:09 +04:00
|
|
|
*
|
2010-10-26 18:53:50 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opal_config.h"
|
|
|
|
|
2014-07-27 00:17:09 +04:00
|
|
|
#include "opal/win32/opal_socket.h"
|
2010-10-26 18:53:50 +04:00
|
|
|
#include "opal/util/output.h"
|
|
|
|
|
|
|
|
int
|
2010-10-27 15:52:57 +04:00
|
|
|
create_socketpair(int family, int type, int protocol, int fd[2])
|
2010-10-26 18:53:50 +04:00
|
|
|
{
|
2010-10-27 15:52:57 +04:00
|
|
|
/* This code is originally from Tor. Used with permission. */
|
2010-10-26 18:53:50 +04:00
|
|
|
|
2010-10-27 15:52:57 +04:00
|
|
|
/* This socketpair does not work when localhost is down. So
|
|
|
|
* it's really not the same thing at all. But it's close enough
|
|
|
|
* for now, and really, when localhost is down sometimes, we
|
|
|
|
* have other problems too.
|
|
|
|
*/
|
2010-10-26 18:53:50 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
#define ERR(e) WSA##e
|
|
|
|
#else
|
|
|
|
#define ERR(e) e
|
|
|
|
#endif
|
2010-10-27 15:52:57 +04:00
|
|
|
int listener = -1;
|
|
|
|
int connector = -1;
|
|
|
|
int acceptor = -1;
|
|
|
|
struct sockaddr_in listen_addr;
|
|
|
|
struct sockaddr_in connect_addr;
|
|
|
|
int size;
|
|
|
|
int saved_errno = -1;
|
2010-10-26 18:53:50 +04:00
|
|
|
|
2010-10-27 15:52:57 +04:00
|
|
|
if (protocol
|
|
|
|
|| (family != AF_INET
|
2010-10-26 18:53:50 +04:00
|
|
|
#ifdef AF_UNIX
|
2010-10-27 15:52:57 +04:00
|
|
|
&& family != AF_UNIX
|
2010-10-26 18:53:50 +04:00
|
|
|
#endif
|
2010-10-27 15:52:57 +04:00
|
|
|
)) {
|
2010-10-26 18:53:50 +04:00
|
|
|
opal_output(0, "Protocol not support: %d", (ERR(EAFNOSUPPORT)));
|
2010-10-27 15:52:57 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!fd) {
|
2010-10-26 18:53:50 +04:00
|
|
|
opal_output(0, "Invalid socked: %d", (ERR(EINVAL)));
|
2010-10-27 15:52:57 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-10-26 18:53:50 +04:00
|
|
|
|
2010-10-27 15:52:57 +04:00
|
|
|
listener = socket(AF_INET, type, 0);
|
|
|
|
if (listener < 0)
|
|
|
|
return -1;
|
|
|
|
memset(&listen_addr, 0, sizeof(listen_addr));
|
|
|
|
listen_addr.sin_family = AF_INET;
|
|
|
|
listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
listen_addr.sin_port = 0; /* kernel chooses port. */
|
|
|
|
if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr))
|
|
|
|
== -1)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
if (listen(listener, 1) == -1)
|
|
|
|
goto tidy_up_and_fail;
|
2010-10-26 18:53:50 +04:00
|
|
|
|
2010-10-27 15:52:57 +04:00
|
|
|
connector = socket(AF_INET, type, 0);
|
|
|
|
if (connector < 0)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
/* We want to find out the port number to connect to. */
|
|
|
|
size = sizeof(connect_addr);
|
|
|
|
if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
if (size != sizeof (connect_addr))
|
|
|
|
goto abort_tidy_up_and_fail;
|
|
|
|
if (connect(connector, (struct sockaddr *) &connect_addr,
|
|
|
|
sizeof(connect_addr)) == -1)
|
|
|
|
goto tidy_up_and_fail;
|
2010-10-26 18:53:50 +04:00
|
|
|
|
2010-10-27 15:52:57 +04:00
|
|
|
size = sizeof(listen_addr);
|
|
|
|
acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
|
|
|
|
if (acceptor < 0)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
if (size != sizeof(listen_addr))
|
|
|
|
goto abort_tidy_up_and_fail;
|
|
|
|
closesocket(listener);
|
|
|
|
/* Now check we are talking to ourself by matching port and host on the
|
|
|
|
two sockets. */
|
|
|
|
if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
|
|
|
|
goto tidy_up_and_fail;
|
|
|
|
if (size != sizeof (connect_addr)
|
|
|
|
|| listen_addr.sin_family != connect_addr.sin_family
|
|
|
|
|| listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
|
|
|
|
|| listen_addr.sin_port != connect_addr.sin_port)
|
|
|
|
goto abort_tidy_up_and_fail;
|
|
|
|
fd[0] = connector;
|
|
|
|
fd[1] = acceptor;
|
2010-10-26 18:53:50 +04:00
|
|
|
|
2010-10-27 15:52:57 +04:00
|
|
|
return 0;
|
2010-10-26 18:53:50 +04:00
|
|
|
|
|
|
|
abort_tidy_up_and_fail:
|
2010-10-27 15:52:57 +04:00
|
|
|
saved_errno = ERR(ECONNABORTED);
|
2010-10-26 18:53:50 +04:00
|
|
|
tidy_up_and_fail:
|
2010-10-27 15:52:57 +04:00
|
|
|
if (saved_errno < 0)
|
|
|
|
saved_errno = WSAGetLastError();
|
|
|
|
if (listener != -1)
|
|
|
|
closesocket(listener);
|
|
|
|
if (connector != -1)
|
|
|
|
closesocket(connector);
|
|
|
|
if (acceptor != -1)
|
|
|
|
closesocket(acceptor);
|
2010-10-26 18:53:50 +04:00
|
|
|
|
2010-10-27 15:52:57 +04:00
|
|
|
WSASetLastError(saved_errno);
|
|
|
|
return -1;
|
2010-10-26 18:53:50 +04:00
|
|
|
#undef ERR
|
2014-07-27 00:17:09 +04:00
|
|
|
}
|