1 #define _WINSOCK_DEPRECATED_NO_WARNINGS
2 #include "private-libwebsockets.h"
7 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
9 ULARGE_INTEGER datetime;
12 GetCurrentFT(&filetime);
14 GetSystemTimeAsFileTime(&filetime);
18 * As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
19 * ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
20 * prevent alignment faults on 64-bit Windows).
22 memcpy(&datetime, &filetime, sizeof(datetime));
24 /* Windows file times are in 100s of nanoseconds. */
25 return (datetime.QuadPart - DELTA_EPOCH_IN_MICROSECS) / 10;
29 time_t time(time_t *t)
31 time_t ret = time_in_microseconds() / 1000000;
37 /* file descriptor hash management */
40 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd)
42 int h = LWS_FD_HASH(fd);
45 for (n = 0; n < context->fd_hashtable[h].length; n++)
46 if (context->fd_hashtable[h].wsi[n]->sock == fd)
47 return context->fd_hashtable[h].wsi[n];
53 insert_wsi(struct lws_context *context, struct lws *wsi)
55 int h = LWS_FD_HASH(wsi->sock);
57 if (context->fd_hashtable[h].length == (getdtablesize() - 1)) {
58 lwsl_err("hash table overflow\n");
62 context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi;
68 delete_from_fd(struct lws_context *context, lws_sockfd_type fd)
70 int h = LWS_FD_HASH(fd);
73 for (n = 0; n < context->fd_hashtable[h].length; n++)
74 if (context->fd_hashtable[h].wsi[n]->sock == fd) {
75 while (n < context->fd_hashtable[h].length) {
76 context->fd_hashtable[h].wsi[n] =
77 context->fd_hashtable[h].wsi[n + 1];
80 context->fd_hashtable[h].length--;
85 lwsl_err("Failed to find fd %d requested for "
86 "delete in hashtable\n", fd);
90 LWS_VISIBLE int lws_get_random(struct lws_context *context,
94 char *p = (char *)buf;
96 for (n = 0; n < len; n++)
97 p[n] = (unsigned char)rand();
102 LWS_VISIBLE int lws_send_pipe_choked(struct lws *wsi)
104 return (int)wsi->sock_send_blocking;
107 LWS_VISIBLE int lws_poll_listen_fd(struct lws_pollfd *fd)
110 struct timeval tv = { 0, 0 };
112 assert(fd->events == LWS_POLLIN);
115 FD_SET(fd->fd, &readfds);
117 return select(fd->fd + 1, &readfds, NULL, NULL, &tv);
121 * lws_cancel_service() - Cancel servicing of pending websocket activity
122 * @context: Websocket context
124 * This function let a call to lws_service() waiting for a timeout
125 * immediately return.
128 lws_cancel_service(struct lws_context *context)
130 struct lws_context_per_thread *pt = &context->pt[0];
131 int n = context->count_threads;
134 WSASetEvent(pt->events[0]);
140 lws_cancel_service_pt(struct lws *wsi)
142 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
143 WSASetEvent(pt->events[0]);
146 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
148 lwsl_emit_stderr(level, line);
152 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
157 WSANETWORKEVENTS networkevents;
158 struct lws_pollfd *pfd;
160 struct lws_context_per_thread *pt = &context->pt[tsi];
162 /* stay dead once we are dead */
167 if (!context->service_tid_detected) {
170 memset(&_lws, 0, sizeof(_lws));
171 _lws.context = context;
173 context->service_tid_detected = context->protocols[0].callback(
174 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
176 context->service_tid = context->service_tid_detected;
178 for (i = 0; i < pt->fds_count; ++i) {
180 if (pfd->fd == pt->lserv_fd)
183 if (pfd->events & LWS_POLLOUT) {
184 wsi = wsi_from_fd(context, pfd->fd);
185 if (!wsi || wsi->sock_send_blocking)
187 pfd->revents = LWS_POLLOUT;
188 n = lws_service_fd(context, pfd);
191 /* if something closed, retry this slot */
197 ev = WSAWaitForMultipleEvents(pt->fds_count + 1, pt->events,
198 FALSE, timeout_ms, FALSE);
199 context->service_tid = 0;
201 if (ev == WSA_WAIT_TIMEOUT) {
202 lws_service_fd(context, NULL);
206 if (ev == WSA_WAIT_EVENT_0) {
207 WSAResetEvent(pt->events[0]);
211 if (ev < WSA_WAIT_EVENT_0 || ev > WSA_WAIT_EVENT_0 + pt->fds_count)
214 pfd = &pt->fds[ev - WSA_WAIT_EVENT_0 - 1];
216 if (WSAEnumNetworkEvents(pfd->fd,
217 pt->events[ev - WSA_WAIT_EVENT_0],
218 &networkevents) == SOCKET_ERROR) {
219 lwsl_err("WSAEnumNetworkEvents() failed with error %d\n",
224 pfd->revents = (short)networkevents.lNetworkEvents;
226 if (pfd->revents & LWS_POLLOUT) {
227 wsi = wsi_from_fd(context, pfd->fd);
229 wsi->sock_send_blocking = 0;
232 return lws_service_fd(context, pfd);
236 lws_plat_service(struct lws_context *context, int timeout_ms)
238 return lws_plat_service_tsi(context, timeout_ms, 0);
242 lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd)
245 int optlen = sizeof(optval);
248 struct tcp_keepalive alive;
249 struct protoent *tcp_proto;
251 if (context->ka_time) {
252 /* enable keepalive on this socket */
254 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
255 (const char *)&optval, optlen) < 0)
259 alive.keepalivetime = context->ka_time;
260 alive.keepaliveinterval = context->ka_interval;
262 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
263 NULL, 0, &dwBytesRet, NULL, NULL))
269 tcp_proto = getprotobyname("TCP");
271 lwsl_err("getprotobyname() failed with error %d\n", LWS_ERRNO);
275 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, (const char *)&optval, optlen);
277 /* We are nonblocking... */
278 ioctlsocket(fd, FIONBIO, &optl);
284 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
289 lws_plat_context_early_init(void)
291 WORD wVersionRequested;
295 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
296 wVersionRequested = MAKEWORD(2, 2);
298 err = WSAStartup(wVersionRequested, &wsaData);
302 * Tell the user that we could not find a usable
305 lwsl_err("WSAStartup failed with error: %d\n", err);
311 lws_plat_context_early_destroy(struct lws_context *context)
313 struct lws_context_per_thread *pt = &context->pt[0];
314 int n = context->count_threads;
318 WSACloseEvent(pt->events[0]);
319 lws_free(pt->events);
326 lws_plat_context_late_destroy(struct lws_context *context)
330 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
331 if (context->fd_hashtable[n].wsi)
332 lws_free(context->fd_hashtable[n].wsi);
338 LWS_VISIBLE LWS_EXTERN int
339 lws_interface_to_sa(int ipv6,
340 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
342 long long address = inet_addr(ifname);
344 if (address == INADDR_NONE) {
345 struct hostent *entry = gethostbyname(ifname);
347 address = ((struct in_addr *)entry->h_addr_list[0])->s_addr;
350 if (address == INADDR_NONE)
353 addr->sin_addr.s_addr = (unsigned long)address;
359 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
361 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
363 pt->fds[pt->fds_count++].revents = 0;
364 pt->events[pt->fds_count] = WSACreateEvent();
365 WSAEventSelect(wsi->sock, pt->events[pt->fds_count], LWS_POLLIN);
369 lws_plat_delete_socket_from_fds(struct lws_context *context,
370 struct lws *wsi, int m)
372 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
374 WSACloseEvent(pt->events[m + 1]);
375 pt->events[m + 1] = pt->events[pt->fds_count--];
379 lws_plat_service_periodic(struct lws_context *context)
384 lws_plat_change_pollfd(struct lws_context *context,
385 struct lws *wsi, struct lws_pollfd *pfd)
387 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
388 long networkevents = LWS_POLLHUP;
390 if ((pfd->events & LWS_POLLIN))
391 networkevents |= LWS_POLLIN;
393 if ((pfd->events & LWS_POLLOUT))
394 networkevents |= LWS_POLLOUT;
396 if (WSAEventSelect(wsi->sock,
397 pt->events[wsi->position_in_fds_table + 1],
398 networkevents) != SOCKET_ERROR)
401 lwsl_err("WSAEventSelect() failed with error %d\n", LWS_ERRNO);
406 LWS_VISIBLE const char *
407 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
410 DWORD bufferlen = cnt;
413 buffer = lws_malloc(bufferlen);
415 lwsl_err("Out of memory\n");
420 struct sockaddr_in srcaddr;
421 bzero(&srcaddr, sizeof(srcaddr));
422 srcaddr.sin_family = AF_INET;
423 memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
425 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
428 } else if (af == AF_INET6) {
429 struct sockaddr_in6 srcaddr;
430 bzero(&srcaddr, sizeof(srcaddr));
431 srcaddr.sin6_family = AF_INET6;
432 memcpy(&(srcaddr.sin6_addr), src, sizeof(srcaddr.sin6_addr));
434 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
438 lwsl_err("Unsupported type\n");
441 int rv = WSAGetLastError();
442 lwsl_err("WSAAddressToString() : %d\n", rv);
444 if (WideCharToMultiByte(CP_ACP, 0, buffer, bufferlen, dst, cnt, 0, NULL) <= 0)
449 return ok ? dst : NULL;
452 static lws_filefd_type
453 _lws_plat_file_open(struct lws *wsi, const char *filename,
454 unsigned long *filelen, int flags)
460 MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, ARRAY_SIZE(buf));
461 if ((flags & 7) == _O_RDONLY) {
462 ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
463 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
465 lwsl_err("%s: open for write not implemented\n", __func__);
467 return LWS_INVALID_FILE;
470 if (ret != LWS_INVALID_FILE)
471 *filelen = GetFileSize(ret, NULL);
477 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
481 CloseHandle((HANDLE)fd);
487 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
491 return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT);
495 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
496 unsigned char* buf, unsigned long len)
501 if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) {
507 *amount = (unsigned long)_amount;
513 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
514 unsigned char* buf, unsigned long len)
522 lwsl_err("%s: not implemented yet on this platform\n", __func__);
528 lws_plat_init(struct lws_context *context,
529 struct lws_context_creation_info *info)
531 struct lws_context_per_thread *pt = &context->pt[0];
532 int i, n = context->count_threads;
534 for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
535 context->fd_hashtable[i].wsi =
536 lws_zalloc(sizeof(struct lws*) * context->max_fds);
538 if (!context->fd_hashtable[i].wsi)
543 pt->events = lws_malloc(sizeof(WSAEVENT) *
544 (context->fd_limit_per_thread + 1));
545 if (pt->events == NULL) {
546 lwsl_err("Unable to allocate events array for %d connections\n",
547 context->fd_limit_per_thread + 1);
552 pt->events[0] = WSACreateEvent();
557 context->fd_random = 0;
559 context->fops.open = _lws_plat_file_open;
560 context->fops.close = _lws_plat_file_close;
561 context->fops.seek_cur = _lws_plat_file_seek_cur;
562 context->fops.read = _lws_plat_file_read;
563 context->fops.write = _lws_plat_file_write;