Path: blob/main/crypto/krb5/src/util/verto/ev_win32.c
34907 views
/*1* libev win32 compatibility cruft (_not_ a backend)2*3* Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without modifica-7* tion, are permitted provided that the following conditions are met:8*9* 1. Redistributions of source code must retain the above copyright notice,10* this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED17* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-18* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO19* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-20* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,21* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;22* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,23* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-24* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED25* OF THE POSSIBILITY OF SUCH DAMAGE.26*27* Alternatively, the contents of this file may be used under the terms of28* the GNU General Public License ("GPL") version 2 or any later version,29* in which case the provisions of the GPL are applicable instead of30* the above. If you wish to allow the use of your version of this file31* only under the terms of the GPL and not to allow others to use your32* version of this file under the BSD license, indicate your decision33* by deleting the provisions above and replace them with the notice34* and other provisions required by the GPL. If you do not delete the35* provisions above, a recipient may use your version of this file under36* either the BSD or the GPL.37*/3839#ifdef _WIN324041/* note: the comment below could not be substantiated, but what would I care */42/* MSDN says this is required to handle SIGFPE */43/* my wild guess would be that using something floating-pointy is required */44/* for the crt to do something about it */45volatile double SIGFPE_REQ = 0.0f;4647static SOCKET48ev_tcp_socket (void)49{50#if EV_USE_WSASOCKET51return WSASocket (AF_INET, SOCK_STREAM, 0, 0, 0, 0);52#else53return socket (AF_INET, SOCK_STREAM, 0);54#endif55}5657/* oh, the humanity! */58static int59ev_pipe (int filedes [2])60{61struct sockaddr_in addr = { 0 };62int addr_size = sizeof (addr);63struct sockaddr_in adr2;64int adr2_size = sizeof (adr2);65SOCKET listener;66SOCKET sock [2] = { -1, -1 };6768if ((listener = ev_tcp_socket ()) == INVALID_SOCKET)69return -1;7071addr.sin_family = AF_INET;72addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);73addr.sin_port = 0;7475if (bind (listener, (struct sockaddr *)&addr, addr_size))76goto fail;7778if (getsockname (listener, (struct sockaddr *)&addr, &addr_size))79goto fail;8081if (listen (listener, 1))82goto fail;8384if ((sock [0] = ev_tcp_socket ()) == INVALID_SOCKET)85goto fail;8687if (connect (sock [0], (struct sockaddr *)&addr, addr_size))88goto fail;8990/* TODO: returns INVALID_SOCKET on winsock accept, not < 0. fix it */91/* when convenient, probably by just removing error checking altogether? */92if ((sock [1] = accept (listener, 0, 0)) < 0)93goto fail;9495/* windows vista returns fantasy port numbers for sockets:96* example for two interconnected tcp sockets:97*98* (Socket::unpack_sockaddr_in getsockname $sock0)[0] == 5336499* (Socket::unpack_sockaddr_in getpeername $sock0)[0] == 53363100* (Socket::unpack_sockaddr_in getsockname $sock1)[0] == 53363101* (Socket::unpack_sockaddr_in getpeername $sock1)[0] == 53365102*103* wow! tridirectional sockets!104*105* this way of checking ports seems to work:106*/107if (getpeername (sock [0], (struct sockaddr *)&addr, &addr_size))108goto fail;109110if (getsockname (sock [1], (struct sockaddr *)&adr2, &adr2_size))111goto fail;112113errno = WSAEINVAL;114if (addr_size != adr2_size115|| addr.sin_addr.s_addr != adr2.sin_addr.s_addr /* just to be sure, I mean, it's windows */116|| addr.sin_port != adr2.sin_port)117goto fail;118119closesocket (listener);120121#if EV_SELECT_IS_WINSOCKET122filedes [0] = EV_WIN32_HANDLE_TO_FD (sock [0]);123filedes [1] = EV_WIN32_HANDLE_TO_FD (sock [1]);124#else125/* when select isn't winsocket, we also expect socket, connect, accept etc.126* to work on fds */127filedes [0] = sock [0];128filedes [1] = sock [1];129#endif130131return 0;132133fail:134closesocket (listener);135136if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);137if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);138139return -1;140}141142#undef pipe143#define pipe(filedes) ev_pipe (filedes)144145#define EV_HAVE_EV_TIME 1146ev_tstamp147ev_time (void)148{149FILETIME ft;150ULARGE_INTEGER ui;151152GetSystemTimeAsFileTime (&ft);153ui.u.LowPart = ft.dwLowDateTime;154ui.u.HighPart = ft.dwHighDateTime;155156/* msvc cannot convert ulonglong to double... yes, it is that sucky */157return (LONGLONG)(ui.QuadPart - 116444736000000000) * 1e-7;158}159160#endif161162163164