/*=========================================================================*\ * Socket compatibilization module for Win32 * LuaSocket toolkit * * The penalty of calling select to avoid busy-wait is only paid when * the I/O call fail in the first place. \*=========================================================================*/ #include "luasocket.h" #include #include "socket.h" #include "pierror.h" /* WinSock doesn't have a strerror... */ static const char *wstrerror(int err); /*-------------------------------------------------------------------------*\ * Initializes module \*-------------------------------------------------------------------------*/ int socket_open(void) { WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 0); int err = WSAStartup(wVersionRequested, &wsaData ); if (err != 0) return 0; if ((LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) && (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)) { WSACleanup(); return 0; } return 1; } /*-------------------------------------------------------------------------*\ * Close module \*-------------------------------------------------------------------------*/ int socket_close(void) { WSACleanup(); return 1; } /*-------------------------------------------------------------------------*\ * Wait for readable/writable/connected socket with timeout \*-------------------------------------------------------------------------*/ #define WAITFD_R 1 #define WAITFD_W 2 #define WAITFD_E 4 #define WAITFD_C (WAITFD_E|WAITFD_W) int socket_waitfd(p_socket ps, int sw, p_timeout tm) { int ret; fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; struct timeval tv, *tp = NULL; double t; if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(*ps, &rfds); rp = &rfds; } if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } if (sw & WAITFD_C) { FD_ZERO(&efds); FD_SET(*ps, &efds); ep = &efds; } if ((t = timeout_get(tm)) >= 0.0) { tv.tv_sec = (int) t; tv.tv_usec = (int) ((t-tv.tv_sec)*1.0e6); tp = &tv; } ret = select(0, rp, wp, ep, tp); if (ret == -1) return WSAGetLastError(); if (ret == 0) return IO_TIMEOUT; if (sw == WAITFD_C && FD_ISSET(*ps, &efds)) return IO_CLOSED; return IO_DONE; } /*-------------------------------------------------------------------------*\ * Select with int timeout in ms \*-------------------------------------------------------------------------*/ int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_timeout tm) { struct timeval tv; double t = timeout_get(tm); tv.tv_sec = (int) t; tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6); if (n <= 0) { Sleep((DWORD) (1000*t)); return 0; } else return select(0, rfds, wfds, efds, t >= 0.0? &tv: NULL); } /*-------------------------------------------------------------------------*\ * Close and inutilize socket \*-------------------------------------------------------------------------*/ void socket_destroy(p_socket ps) { if (*ps != SOCKET_INVALID) { socket_setblocking(ps); /* close can take a long time on WIN32 */ closesocket(*ps); *ps = SOCKET_INVALID; } } /*-------------------------------------------------------------------------*\ * \*-------------------------------------------------------------------------*/ void socket_shutdown(p_socket ps, int how) { socket_setblocking(ps); shutdown(*ps, how); socket_setnonblocking(ps); } /*-------------------------------------------------------------------------*\ * Creates and sets up a socket \*-------------------------------------------------------------------------*/ int socket_create(p_socket ps, int domain, int type, int protocol) { *ps = socket(domain, type, protocol); if (*ps != SOCKET_INVALID) return IO_DONE; else return WSAGetLastError(); } /*-------------------------------------------------------------------------*\ * Connects or returns error message \*-------------------------------------------------------------------------*/ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { int err; /* don't call on closed socket */ if (*ps == SOCKET_INVALID) return IO_CLOSED; /* ask system to connect */ if (connect(*ps, addr, len) == 0) return IO_DONE; /* make sure the system is trying to connect */ err = WSAGetLastError(); if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err; /* zero timeout case optimization */ if (timeout_iszero(tm)) return IO_TIMEOUT; /* we wait until something happens */ err = socket_waitfd(ps, WAITFD_C, tm); if (err == IO_CLOSED) { int elen = sizeof(err); /* give windows time to se
Installation
============

Installation on Linux/UNIX
--------------------------

The GNU C Compiler is fully supported, other compilers may work. The C compiler
should be in your ``$PATH`` (most likely the case). Note that some few Linux
distributions do not ship with a GCC compiler preinstalled - then you have to
install it.

Install Nim by downloading the appropriate ``.zip`` file and extracting it 
to a directory of your choice. The Nim Compiler will stay in this
directory (unless you copy it somewhere else). The compiler does not need 
write access to its directory, so copying the nim folder to ``/opt`` 
works.

Then run the following command::

  sh build.sh

Unlike other software, Nim does not distribute its files over the whole file
hierarchy. This has the advantage that you can deinstall it by just deleting
its folder. The disadvantage is that you have to add it to your ``PATH``
manually. An alternative is to create a symbolic link in ``/usr/bin``::

  [sudo] ln -s $your_install_dir/bin/nim  /usr/bin/nim

There are also ``install.sh`` and ``deinstall.sh`` scripts for distributing 
the files over the UNIX hierarchy. However, updating your Nim installation
is more cumbersome then.


Installation on the Macintosh
-----------------------------

Only MacOS X is supported.
Since MacOS X is UNIX based too, it works like the installation on Linux. 
However, for unknown reasons the symbolic link method does not work on MacOS X. 
You need to install Apple's developer's tools for the GNU Compiler Collection.


Installation on Windows
-----------------------

Install Nim by downloading and running the ``nim_$version.exe`` file.
As default, the ``GCC`` compiler is used that is bundled with this installer.
You can change the configuration file ``config/nim.cfg`` to use
another C compiler or change the path to GCC.

Currently, the following C compilers are supported under Windows:

- | Microsoft's Visual C++
  | http://msdn.microsoft.com/visualc
  | (You need the SDK too - but not the full one: Only
    the win32api header files and import libraries are essential.)
- | Gnu C Compiler (the mingw version; the cygwin version has not been tested!)
  | http://www.mingw.org/download.shtml
- | LLVM with Clang or GNU C/C++ frontend
  | http://llvm.org/releases/download.html

However, most testing is done with GCC.

Bootstrapping from Github
-------------------------

Take a look at the readme file on github `here <https://github.com/Araq/Nim#readme>`_
for instructions.
{ switch (err) { case WSAEINTR: return "Interrupted function call"; case WSAEACCES: return PIE_ACCESS; // "Permission denied"; case WSAEFAULT: return "Bad address"; case WSAEINVAL: return "Invalid argument"; case WSAEMFILE: return "Too many open files"; case WSAEWOULDBLOCK: return "Resource temporarily unavailable"; case WSAEINPROGRESS: return "Operation now in progress"; case WSAEALREADY: return "Operation already in progress"; case WSAENOTSOCK: return "Socket operation on nonsocket"; case WSAEDESTADDRREQ: return "Destination address required"; case WSAEMSGSIZE: return "Message too long"; case WSAEPROTOTYPE: return "Protocol wrong type for socket"; case WSAENOPROTOOPT: return "Bad protocol option"; case WSAEPROTONOSUPPORT: return "Protocol not supported"; case WSAESOCKTNOSUPPORT: return PIE_SOCKTYPE; // "Socket type not supported"; case WSAEOPNOTSUPP: return "Operation not supported"; case WSAEPFNOSUPPORT: return "Protocol family not supported"; case WSAEAFNOSUPPORT: return PIE_FAMILY; // "Address family not supported by protocol family"; case WSAEADDRINUSE: return PIE_ADDRINUSE; // "Address already in use"; case WSAEADDRNOTAVAIL: return "Cannot assign requested address"; case WSAENETDOWN: return "Network is down"; case WSAENETUNREACH: return "Network is unreachable"; case WSAENETRESET: return "Network dropped connection on reset"; case WSAECONNABORTED: return "Software caused connection abort"; case WSAECONNRESET: return PIE_CONNRESET; // "Connection reset by peer"; case WSAENOBUFS: return "No buffer space available"; case WSAEISCONN: return PIE_ISCONN; // "Socket is already connected"; case WSAENOTCONN: return "Socket is not connected"; case WSAESHUTDOWN: return "Cannot send after socket shutdown"; case WSAETIMEDOUT: return PIE_TIMEDOUT; // "Connection timed out"; case WSAECONNREFUSED: return PIE_CONNREFUSED; // "Connection refused"; case WSAEHOSTDOWN: return "Host is down"; case WSAEHOSTUNREACH: return "No route to host"; case WSAEPROCLIM: return "Too many processes"; case WSASYSNOTREADY: return "Network subsystem is unavailable"; case WSAVERNOTSUPPORTED: return "Winsock.dll version out of range"; case WSANOTINITIALISED: return "Successful WSAStartup not yet performed"; case WSAEDISCON: return "Graceful shutdown in progress"; case WSAHOST_NOT_FOUND: return PIE_HOST_NOT_FOUND; // "Host not found"; case WSATRY_AGAIN: return "Nonauthoritative host not found"; case WSANO_RECOVERY: return PIE_FAIL; // "Nonrecoverable name lookup error"; case WSANO_DATA: return "Valid name, no data record of requested type"; default: return "Unknown error"; } } const char *socket_gaistrerror(int err) { if (err == 0) return NULL; switch (err) { case EAI_AGAIN: return PIE_AGAIN; case EAI_BADFLAGS: return PIE_BADFLAGS; #ifdef EAI_BADHINTS case EAI_BADHINTS: return PIE_BADHINTS; #endif case EAI_FAIL: return PIE_FAIL; case EAI_FAMILY: return PIE_FAMILY; case EAI_MEMORY: return PIE_MEMORY; case EAI_NONAME: return PIE_NONAME; #ifdef EAI_OVERFLOW case EAI_OVERFLOW: return PIE_OVERFLOW; #endif #ifdef EAI_PROTOCOL case EAI_PROTOCOL: return PIE_PROTOCOL; #endif case EAI_SERVICE: return PIE_SERVICE; case EAI_SOCKTYPE: return PIE_SOCKTYPE; #ifdef EAI_SYSTEM case EAI_SYSTEM: return strerror(errno); #endif default: return gai_strerror(err); } }