Commit | Line | Data |
---|---|---|
7f799388 M |
1 | /* |
2 | Copyright (c) 2006 by Dan Kennedy. | |
3 | Copyright (c) 2006 by Juliusz Chroboczek. | |
4 | ||
5 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | of this software and associated documentation files (the "Software"), to deal | |
7 | in the Software without restriction, including without limitation the rights | |
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | copies of the Software, and to permit persons to whom the Software is | |
10 | furnished to do so, subject to the following conditions: | |
11 | ||
12 | The above copyright notice and this permission notice shall be included in | |
13 | all copies or substantial portions of the Software. | |
14 | ||
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 | THE SOFTWARE. | |
22 | */ | |
23 | ||
24 | #ifndef WIN32 | |
25 | ||
26 | static int dummy ATTRIBUTE((unused)); | |
27 | ||
28 | #else | |
29 | #include "win32_compat.h" | |
30 | #include <errno.h> | |
31 | #include <stdio.h> | |
20a96d42 M |
32 | #include < time.h > |
33 | ||
7f799388 M |
34 | #undef poll |
35 | #undef socket | |
36 | #undef connect | |
37 | #undef accept | |
38 | #undef shutdown | |
39 | #undef getpeername | |
40 | #undef sleep | |
41 | #undef inet_aton | |
42 | #undef gettimeofday | |
43 | #undef stat | |
7f799388 M |
44 | #define assert(a) |
45 | ||
46 | /* Windows needs this header file for the implementation of inet_aton() */ | |
47 | #include <ctype.h> | |
48 | ||
49 | int win32_inet_pton(int af, const char * src, void * dst) | |
50 | { | |
20a96d42 M |
51 | struct sockaddr_in sa; |
52 | int len = sizeof(SOCKADDR); | |
53 | int ret = -1; | |
54 | int strLen = strlen(src) + 1; | |
55 | #ifdef UNICODE | |
56 | wchar_t *srcNonConst = (wchar_t *)malloc(strLen*sizeof(wchar_t)); | |
57 | memset(srcNonConst, 0, strLen); | |
58 | MultiByteToWideChar(CP_ACP, 0, src, -1, srcNonConst, strLen); | |
59 | #else | |
60 | char *srcNonConst = (char *)malloc(strLen); | |
61 | memset(srcNonConst, 0, strLen); | |
62 | strncpy(srcNonConst, src, strLen); | |
63 | #endif | |
7f799388 | 64 | |
20a96d42 | 65 | if( WSAStringToAddress(srcNonConst,af,NULL,(LPSOCKADDR)&sa,&len) == 0 ) |
7f799388 | 66 | { |
20a96d42 | 67 | ret = 1; |
7f799388 | 68 | } |
20a96d42 | 69 | else |
7f799388 | 70 | { |
20a96d42 M |
71 | if( WSAGetLastError() == WSAEINVAL ) |
72 | { | |
73 | ret = -1; | |
74 | } | |
7f799388 | 75 | } |
20a96d42 M |
76 | free(srcNonConst); |
77 | memcpy(dst, &sa.sin_addr, sizeof(struct in_addr)); | |
7f799388 M |
78 | return ret; |
79 | } | |
80 | ||
20a96d42 | 81 | int win32_poll(struct pollfd *fds, unsigned int nfds, int timo) |
7f799388 | 82 | { |
20a96d42 M |
83 | struct timeval timeout, *toptr; |
84 | fd_set ifds, ofds, efds, *ip, *op; | |
85 | unsigned int i; | |
86 | int rc; | |
87 | ||
88 | // Set up the file-descriptor sets in ifds, ofds and efds. | |
89 | FD_ZERO(&ifds); | |
90 | FD_ZERO(&ofds); | |
91 | FD_ZERO(&efds); | |
92 | for (i = 0, op = ip = 0; i < nfds; ++i) | |
93 | { | |
94 | fds[i].revents = 0; | |
95 | if(fds[i].events & (POLLIN|POLLPRI)) | |
96 | { | |
97 | ip = &ifds; | |
98 | FD_SET(fds[i].fd, ip); | |
99 | } | |
100 | if(fds[i].events & POLLOUT) | |
101 | { | |
102 | op = &ofds; | |
103 | FD_SET(fds[i].fd, op); | |
7f799388 | 104 | } |
20a96d42 M |
105 | FD_SET(fds[i].fd, &efds); |
106 | } | |
107 | ||
108 | // Set up the timeval structure for the timeout parameter | |
109 | if(timo < 0) | |
110 | { | |
111 | toptr = 0; | |
112 | } | |
113 | else | |
114 | { | |
115 | toptr = &timeout; | |
116 | timeout.tv_sec = timo / 1000; | |
117 | timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000; | |
118 | } | |
7f799388 M |
119 | |
120 | #ifdef DEBUG_POLL | |
20a96d42 M |
121 | printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n", |
122 | (long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op); | |
7f799388 | 123 | #endif |
20a96d42 | 124 | rc = select(0, ip, op, &efds, toptr); |
7f799388 | 125 | #ifdef DEBUG_POLL |
20a96d42 | 126 | printf("Exiting select rc=%d\n", rc); |
7f799388 M |
127 | #endif |
128 | ||
20a96d42 M |
129 | if(rc <= 0) |
130 | return rc; | |
7f799388 | 131 | |
20a96d42 M |
132 | if(rc > 0) |
133 | { | |
134 | for (i = 0; i < nfds; ++i) | |
135 | { | |
136 | int fd = fds[i].fd; | |
137 | if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds)) | |
138 | fds[i].revents |= POLLIN; | |
139 | if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds)) | |
140 | fds[i].revents |= POLLOUT; | |
141 | if(FD_ISSET(fd, &efds)) // Some error was detected ... should be some way to know. | |
142 | fds[i].revents |= POLLHUP; | |
7f799388 | 143 | #ifdef DEBUG_POLL |
20a96d42 M |
144 | printf("%d %d %d revent = %x\n", |
145 | FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds), | |
146 | fds[i].revents | |
147 | ); | |
7f799388 | 148 | #endif |
7f799388 | 149 | } |
20a96d42 M |
150 | } |
151 | return rc; | |
7f799388 | 152 | } |
20a96d42 M |
153 | |
154 | #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) | |
155 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 | |
156 | #else | |
157 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL | |
158 | #endif | |
159 | ||
160 | struct timezone | |
161 | { | |
162 | int tz_minuteswest; /* minutes W of Greenwich */ | |
163 | int tz_dsttime; /* type of dst correction */ | |
164 | }; | |
165 | ||
166 | int win32_gettimeofday(struct timeval *tv, struct timezone *tz) | |
167 | { | |
168 | FILETIME ft; | |
169 | unsigned __int64 tmpres = 0; | |
170 | static int tzflag; | |
171 | ||
172 | if (NULL != tv) | |
173 | { | |
174 | GetSystemTimeAsFileTime(&ft); | |
175 | ||
176 | tmpres |= ft.dwHighDateTime; | |
177 | tmpres <<= 32; | |
178 | tmpres |= ft.dwLowDateTime; | |
179 | ||
180 | /*converting file time to unix epoch*/ | |
181 | tmpres -= DELTA_EPOCH_IN_MICROSECS; | |
182 | tmpres /= 10; /*convert into microseconds*/ | |
183 | tv->tv_sec = (long)(tmpres / 1000000UL); | |
184 | tv->tv_usec = (long)(tmpres % 1000000UL); | |
185 | } | |
186 | ||
187 | if (NULL != tz) | |
188 | { | |
189 | if (!tzflag) | |
190 | { | |
191 | _tzset(); | |
192 | tzflag++; | |
193 | } | |
194 | tz->tz_minuteswest = _timezone / 60; | |
195 | tz->tz_dsttime = _daylight; | |
196 | } | |
197 | ||
198 | return 0; | |
199 | } | |
200 | ||
7f799388 | 201 | #endif |