w32utils.c

00001 /* *
00002  * This file is part of libnemesi
00003  *
00004  * Copyright (C) 2007 by LScube team <team@streaming.polito.it>
00005  * See AUTHORS for more details
00006  *
00007  * libnemesi is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * libnemesi is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with libnemesi; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  *
00021  * */
00022 
00023 #include <winsock2.h>
00024 #include <ws2tcpip.h>
00025 #include <time.h>
00026 
00027 #ifndef __GNUC__
00028 #define EPOCHFILETIME (116444736000000000i64)
00029 #else
00030 #define EPOCHFILETIME (116444736000000000LL)
00031 #endif
00032 
00033 struct timezone {
00034     int tz_minuteswest; /* minutes W of Greenwich */
00035     int tz_dsttime;     /* type of dst correction */
00036 };
00037 
00038 int gettimeofday(struct timeval *tv, struct timezone *tz)
00039 {
00040     FILETIME        ft;
00041     LARGE_INTEGER   li;
00042     __int64         t;
00043     static int      tzflag;
00044 
00045     if (tv)
00046     {
00047         GetSystemTimeAsFileTime(&ft);
00048         li.LowPart  = ft.dwLowDateTime;
00049         li.HighPart = ft.dwHighDateTime;
00050         t  = li.QuadPart;       /* In 100-nanosecond intervals */
00051         t -= EPOCHFILETIME;     /* Offset to the Epoch time */
00052         t /= 10;                /* In microseconds */
00053         tv->tv_sec  = (long)(t / 1000000);
00054         tv->tv_usec = (long)(t % 1000000);
00055     }
00056 
00057     if (tz)
00058     {
00059         if (!tzflag)
00060         {
00061             _tzset();
00062             tzflag++;
00063         }
00064         tz->tz_minuteswest = _timezone / 60;
00065         tz->tz_dsttime = _daylight;
00066     }
00067 
00068     return 0;
00069 }
00070 
00071 int usleep(unsigned t)
00072 {
00073     Sleep((t) / 1000);
00074     return 0;
00075 }
00076 
00077 int socketpair(int domain, int type, int protocol, int socks[2])
00078 {
00079     struct sockaddr_in addr;
00080     SOCKET listener;
00081     int e;
00082     int addrlen = sizeof(addr);
00083     DWORD flags = 0; //maybe set WSA_FLAG_OVERLAPPED?
00084 
00085     if (socks == 0) {
00086       WSASetLastError(WSAEINVAL);
00087       return SOCKET_ERROR;
00088     }
00089 
00090     socks[0] = socks[1] = INVALID_SOCKET;
00091     if ((listener = socket(domain, type, 0)) == INVALID_SOCKET)
00092         return SOCKET_ERROR;
00093 
00094     memset(&addr, 0, sizeof(addr));
00095     addr.sin_family = domain;
00096     addr.sin_addr.s_addr = htonl(0x7f000001);
00097     addr.sin_port = 0;
00098 
00099     e = bind(listener, (const struct sockaddr*) &addr, sizeof(addr));
00100     if (e == SOCKET_ERROR) {
00101         e = WSAGetLastError();
00102         closesocket(listener);
00103         WSASetLastError(e);
00104         return SOCKET_ERROR;
00105     }
00106     e = getsockname(listener, (struct sockaddr*) &addr, &addrlen);
00107     if (e == SOCKET_ERROR) {
00108         e = WSAGetLastError();
00109         closesocket(listener);
00110         WSASetLastError(e);
00111         return SOCKET_ERROR;
00112     }
00113 
00114     do {
00115         if (listen(listener, 1) == SOCKET_ERROR)                      break;
00116         if ((socks[0] = WSASocket(domain, type, 0, NULL, 0, flags))
00117                 == INVALID_SOCKET)                                    break;
00118         if (connect(socks[0], (const struct sockaddr*) &addr,
00119                     sizeof(addr)) == SOCKET_ERROR)                    break;
00120         if ((socks[1] = accept(listener, NULL, NULL))
00121                 == INVALID_SOCKET)                                    break;
00122         closesocket(listener);
00123         return 0;
00124     } while (0);
00125     e = WSAGetLastError();
00126     closesocket(listener);
00127     closesocket(socks[0]);
00128     closesocket(socks[1]);
00129     WSASetLastError(e);
00130     return SOCKET_ERROR;
00131 }
00132 
00133 //strtok is supposed to be reetrant on win32
00134 char * strtok_r(char * str, const char *delim, char **saveptr)
00135 {
00136  return strtok(str, delim);
00137 }
00138 
00139 //mingw misses the real implementation...
00140 char * WSAAPI gai_strerrorA(int eid)
00141 {
00142      return "Unknown";
00143 }
00144 
00145 double drand48(void)
00146 {
00147  return ((double)rand())/RAND_MAX;
00148 }

Generated on Tue Feb 3 03:10:02 2009 for libnemesi by  doxygen 1.5.4