Skip to content

Commit

Permalink
Import dumb_socketpair() and enable command fd for MinGW
Browse files Browse the repository at this point in the history
https://github.com/ncm/selectable-socketpair/

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Feb 10, 2014
1 parent 7c7ad37 commit 950a24c
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 1 deletion.
128 changes: 128 additions & 0 deletions compat.c
Expand Up @@ -271,4 +271,132 @@ ssize_t openconnect__win32_sock_write(gnutls_transport_ptr_t ptr, const void *da
return send((long)ptr, data, size, 0);
}
#endif /* OPENCONNECT_GNUTLS */


/* https://github.com/ncm/selectable-socketpair
Copyright 2007, 2010 by Nathan C. Myers <ncm@cantrip.org>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
The name of the author must not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* Changes:
* 2013-07-18: Change to BSD 3-clause license
* 2010-03-31:
* set addr to 127.0.0.1 because win32 getsockname does not always set it.
* 2010-02-25:
* set SO_REUSEADDR option to avoid leaking some windows resource.
* Windows System Error 10049, "Event ID 4226 TCP/IP has reached
* the security limit imposed on the number of concurrent TCP connect
* attempts." Bleah.
* 2007-04-25:
* preserve value of WSAGetLastError() on all error returns.
* 2007-04-22: (Thanks to Matthew Gregan <kinetik@flim.org>)
* s/EINVAL/WSAEINVAL/ fix trivial compile failure
* s/socket/WSASocket/ enable creation of sockets suitable as stdin/stdout
* of a child process.
* add argument make_overlapped
*/

#include <string.h>

# include <winsock2.h>
# include <windows.h>
# include <io.h>

/* dumb_socketpair:
* If make_overlapped is nonzero, both sockets created will be usable for
* "overlapped" operations via WSASend etc. If make_overlapped is zero,
* socks[0] (only) will be usable with regular ReadFile etc., and thus
* suitable for use as stdin or stdout of a child process. Note that the
* sockets must be closed with closesocket() regardless.
*/

int dumb_socketpair(int socks[2], int make_overlapped)
{
union {
struct sockaddr_in inaddr;
struct sockaddr addr;
} a;
SOCKET listener;
int e;
socklen_t addrlen = sizeof(a.inaddr);
DWORD flags = (make_overlapped ? WSA_FLAG_OVERLAPPED : 0);
int reuse = 1;

if (socks == 0) {
WSASetLastError(WSAEINVAL);
return SOCKET_ERROR;
}

listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listener == INVALID_SOCKET)
return SOCKET_ERROR;

memset(&a, 0, sizeof(a));
a.inaddr.sin_family = AF_INET;
a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
a.inaddr.sin_port = 0;

socks[0] = socks[1] = INVALID_SOCKET;
do {
if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR,
(char*) &reuse, (socklen_t) sizeof(reuse)) == -1)
break;
if (bind(listener, &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
break;

memset(&a, 0, sizeof(a));
if (getsockname(listener, &a.addr, &addrlen) == SOCKET_ERROR)
break;
// win32 getsockname may only set the port number, p=0.0005.
// ( http://msdn.microsoft.com/library/ms738543.aspx ):
a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
a.inaddr.sin_family = AF_INET;

if (listen(listener, 1) == SOCKET_ERROR)
break;

socks[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags);
if (socks[0] == INVALID_SOCKET)
break;
if (connect(socks[0], &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
break;

socks[1] = accept(listener, NULL, NULL);
if (socks[1] == INVALID_SOCKET)
break;

closesocket(listener);
return 0;

} while (0);

e = WSAGetLastError();
closesocket(listener);
closesocket(socks[0]);
closesocket(socks[1]);
WSASetLastError(e);
return SOCKET_ERROR;
}
#endif /* _WIN32 */
5 changes: 5 additions & 0 deletions library.c
Expand Up @@ -370,8 +370,13 @@ int openconnect_setup_cmd_pipe(struct openconnect_info *vpninfo)
{
int pipefd[2];

#ifdef _WIN32
if (dumb_socketpair(pipefd, 0))
return -EIO;
#else
if (pipe(pipefd) < 0)
return -EIO;
#endif
if (set_sock_nonblock(pipefd[0]) || set_sock_nonblock(pipefd[1])) {
close(pipefd[0]);
close(pipefd[1]);
Expand Down
4 changes: 3 additions & 1 deletion main.c
Expand Up @@ -413,8 +413,8 @@ static void read_stdin(char **string, int hidden)
*c = 0;
}

#ifndef _WIN32
static int sig_cmd_fd;
#ifndef _WIN32
static int sig_caught;

static void handle_sigint(int sig)
Expand Down Expand Up @@ -921,13 +921,15 @@ int main(int argc, char **argv)
#endif
vpninfo->progress = syslog_progress;
}
#endif /* !_WIN32 */

sig_cmd_fd = openconnect_setup_cmd_pipe(vpninfo);
if (sig_cmd_fd < 0) {
fprintf(stderr, _("Error opening cmd pipe\n"));
exit(1);
}

#ifndef _WIN32
memset(&sa, 0, sizeof(sa));

sa.sa_handler = handle_sigusr;
Expand Down
1 change: 1 addition & 0 deletions openconnect-internal.h
Expand Up @@ -394,6 +394,7 @@ int openconnect__win32_sock_poll(gnutls_transport_ptr_t ptr, unsigned int ms);
ssize_t openconnect__win32_sock_read(gnutls_transport_ptr_t ptr, void *data, size_t size);
ssize_t openconnect__win32_sock_write(gnutls_transport_ptr_t ptr, const void *data, size_t size);
#endif
int dumb_socketpair(int socks[2], int make_overlapped);
#else
#define neterrno() errno
#define closesocket close
Expand Down

0 comments on commit 950a24c

Please sign in to comment.