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 | |
44 | #define bzero(a,b) memset((a),(0),(b)) | |
45 | #define assert(a) | |
46 | ||
47 | /* Windows needs this header file for the implementation of inet_aton() */ | |
48 | #include <ctype.h> | |
49 | ||
50 | int win32_inet_pton(int af, const char * src, void * dst) | |
51 | { | |
20a96d42 M |
52 | struct sockaddr_in sa; |
53 | int len = sizeof(SOCKADDR); | |
54 | int ret = -1; | |
55 | int strLen = strlen(src) + 1; | |
56 | #ifdef UNICODE | |
57 | wchar_t *srcNonConst = (wchar_t *)malloc(strLen*sizeof(wchar_t)); | |
58 | memset(srcNonConst, 0, strLen); | |
59 | MultiByteToWideChar(CP_ACP, 0, src, -1, srcNonConst, strLen); | |
60 | #else | |
61 | char *srcNonConst = (char *)malloc(strLen); | |
62 | memset(srcNonConst, 0, strLen); | |
63 | strncpy(srcNonConst, src, strLen); | |
64 | #endif | |
7f799388 | 65 | |
20a96d42 | 66 | if( WSAStringToAddress(srcNonConst,af,NULL,(LPSOCKADDR)&sa,&len) == 0 ) |
7f799388 | 67 | { |
20a96d42 | 68 | ret = 1; |
7f799388 | 69 | } |
20a96d42 | 70 | else |
7f799388 | 71 | { |
20a96d42 M |
72 | if( WSAGetLastError() == WSAEINVAL ) |
73 | { | |
74 | ret = -1; | |
75 | } | |
7f799388 | 76 | } |
20a96d42 M |
77 | free(srcNonConst); |
78 | memcpy(dst, &sa.sin_addr, sizeof(struct in_addr)); | |
7f799388 M |
79 | return ret; |
80 | } | |
81 | ||
20a96d42 | 82 | int win32_poll(struct pollfd *fds, unsigned int nfds, int timo) |
7f799388 | 83 | { |
20a96d42 M |
84 | struct timeval timeout, *toptr; |
85 | fd_set ifds, ofds, efds, *ip, *op; | |
86 | unsigned int i; | |
87 | int rc; | |
88 | ||
89 | // Set up the file-descriptor sets in ifds, ofds and efds. | |
90 | FD_ZERO(&ifds); | |
91 | FD_ZERO(&ofds); | |
92 | FD_ZERO(&efds); | |
93 | for (i = 0, op = ip = 0; i < nfds; ++i) | |
94 | { | |
95 | fds[i].revents = 0; | |
96 | if(fds[i].events & (POLLIN|POLLPRI)) | |
97 | { | |
98 | ip = &ifds; | |
99 | FD_SET(fds[i].fd, ip); | |
100 | } | |
101 | if(fds[i].events & POLLOUT) | |
102 | { | |
103 | op = &ofds; | |
104 | FD_SET(fds[i].fd, op); | |
7f799388 | 105 | } |
20a96d42 M |
106 | FD_SET(fds[i].fd, &efds); |
107 | } | |
108 | ||
109 | // Set up the timeval structure for the timeout parameter | |
110 | if(timo < 0) | |
111 | { | |
112 | toptr = 0; | |
113 | } | |
114 | else | |
115 | { | |
116 | toptr = &timeout; | |
117 | timeout.tv_sec = timo / 1000; | |
118 | timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000; | |
119 | } | |
7f799388 M |
120 | |
121 | #ifdef DEBUG_POLL | |
20a96d42 M |
122 | printf("Entering select() sec=%ld usec=%ld ip=%lx op=%lx\n", |
123 | (long)timeout.tv_sec, (long)timeout.tv_usec, (long)ip, (long)op); | |
7f799388 | 124 | #endif |
20a96d42 | 125 | rc = select(0, ip, op, &efds, toptr); |
7f799388 | 126 | #ifdef DEBUG_POLL |
20a96d42 | 127 | printf("Exiting select rc=%d\n", rc); |
7f799388 M |
128 | #endif |
129 | ||
20a96d42 M |
130 | if(rc <= 0) |
131 | return rc; | |
7f799388 | 132 | |
20a96d42 M |
133 | if(rc > 0) |
134 | { | |
135 | for (i = 0; i < nfds; ++i) | |
136 | { | |
137 | int fd = fds[i].fd; | |
138 | if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds)) | |
139 | fds[i].revents |= POLLIN; | |
140 | if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds)) | |
141 | fds[i].revents |= POLLOUT; | |
142 | if(FD_ISSET(fd, &efds)) // Some error was detected ... should be some way to know. | |
143 | fds[i].revents |= POLLHUP; | |
7f799388 | 144 | #ifdef DEBUG_POLL |
20a96d42 M |
145 | printf("%d %d %d revent = %x\n", |
146 | FD_ISSET(fd, &ifds), FD_ISSET(fd, &ofds), FD_ISSET(fd, &efds), | |
147 | fds[i].revents | |
148 | ); | |
7f799388 | 149 | #endif |
7f799388 | 150 | } |
20a96d42 M |
151 | } |
152 | return rc; | |
7f799388 | 153 | } |
20a96d42 M |
154 | |
155 | #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) | |
156 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 | |
157 | #else | |
158 | #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL | |
159 | #endif | |
160 | ||
161 | struct timezone | |
162 | { | |
163 | int tz_minuteswest; /* minutes W of Greenwich */ | |
164 | int tz_dsttime; /* type of dst correction */ | |
165 | }; | |
166 | ||
167 | int win32_gettimeofday(struct timeval *tv, struct timezone *tz) | |
168 | { | |
169 | FILETIME ft; | |
170 | unsigned __int64 tmpres = 0; | |
171 | static int tzflag; | |
172 | ||
173 | if (NULL != tv) | |
174 | { | |
175 | GetSystemTimeAsFileTime(&ft); | |
176 | ||
177 | tmpres |= ft.dwHighDateTime; | |
178 | tmpres <<= 32; | |
179 | tmpres |= ft.dwLowDateTime; | |
180 | ||
181 | /*converting file time to unix epoch*/ | |
182 | tmpres -= DELTA_EPOCH_IN_MICROSECS; | |
183 | tmpres /= 10; /*convert into microseconds*/ | |
184 | tv->tv_sec = (long)(tmpres / 1000000UL); | |
185 | tv->tv_usec = (long)(tmpres % 1000000UL); | |
186 | } | |
187 | ||
188 | if (NULL != tz) | |
189 | { | |
190 | if (!tzflag) | |
191 | { | |
192 | _tzset(); | |
193 | tzflag++; | |
194 | } | |
195 | tz->tz_minuteswest = _timezone / 60; | |
196 | tz->tz_dsttime = _daylight; | |
197 | } | |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
7f799388 | 202 | #endif |