1 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
2 #define _WINSOCK_DEPRECATED_NO_WARNINGS
4 #include "private-libwebsockets.h"
9 #ifndef DELTA_EPOCH_IN_MICROSECS
10 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
13 ULARGE_INTEGER datetime;
16 GetCurrentFT(&filetime);
18 GetSystemTimeAsFileTime(&filetime);
22 * As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
23 * ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
24 * prevent alignment faults on 64-bit Windows).
26 memcpy(&datetime, &filetime, sizeof(datetime));
28 /* Windows file times are in 100s of nanoseconds. */
29 return (datetime.QuadPart - DELTA_EPOCH_IN_MICROSECS) / 10;
33 time_t time(time_t *t)
35 time_t ret = time_in_microseconds() / 1000000;
44 /* file descriptor hash management */
47 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd)
49 int h = LWS_FD_HASH(fd);
52 for (n = 0; n < context->fd_hashtable[h].length; n++)
53 if (context->fd_hashtable[h].wsi[n]->sock == fd)
54 return context->fd_hashtable[h].wsi[n];
60 insert_wsi(struct lws_context *context, struct lws *wsi)
62 int h = LWS_FD_HASH(wsi->sock);
64 if (context->fd_hashtable[h].length == (getdtablesize() - 1)) {
65 lwsl_err("hash table overflow\n");
69 context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi;
75 delete_from_fd(struct lws_context *context, lws_sockfd_type fd)
77 int h = LWS_FD_HASH(fd);
80 for (n = 0; n < context->fd_hashtable[h].length; n++)
81 if (context->fd_hashtable[h].wsi[n]->sock == fd) {
82 while (n < context->fd_hashtable[h].length) {
83 context->fd_hashtable[h].wsi[n] =
84 context->fd_hashtable[h].wsi[n + 1];
87 context->fd_hashtable[h].length--;
92 lwsl_err("Failed to find fd %d requested for "
93 "delete in hashtable\n", fd);
97 LWS_VISIBLE int lws_get_random(struct lws_context *context,
101 char *p = (char *)buf;
103 for (n = 0; n < len; n++)
104 p[n] = (unsigned char)rand();
109 LWS_VISIBLE int lws_send_pipe_choked(struct lws *wsi)
111 return (int)wsi->sock_send_blocking;
114 LWS_VISIBLE int lws_poll_listen_fd(struct lws_pollfd *fd)
117 struct timeval tv = { 0, 0 };
119 assert((fd->events & LWS_POLLIN) == LWS_POLLIN);
122 FD_SET(fd->fd, &readfds);
124 return select(fd->fd + 1, &readfds, NULL, NULL, &tv);
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);
151 LWS_VISIBLE LWS_EXTERN int
152 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
154 struct lws_context_per_thread *pt;
155 WSANETWORKEVENTS networkevents;
156 struct lws_pollfd *pfd;
162 /* stay dead once we are dead */
166 pt = &context->pt[tsi];
168 if (!context->service_tid_detected) {
171 memset(&_lws, 0, sizeof(_lws));
172 _lws.context = context;
174 context->service_tid_detected = context->vhost_list->
175 protocols[0].callback(&_lws, LWS_CALLBACK_GET_THREAD_ID,
178 context->service_tid = context->service_tid_detected;
182 if (lws_service_flag_pending(context, tsi)) {
183 /* any socket with events to service? */
184 for (n = 0; n < (int)pt->fds_count; n++) {
185 if (!pt->fds[n].revents)
188 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
191 /* if something closed, retry this slot */
199 for (i = 0; i < pt->fds_count; ++i) {
202 if (!(pfd->events & LWS_POLLOUT))
205 wsi = wsi_from_fd(context, pfd->fd);
208 if (!wsi || wsi->sock_send_blocking)
210 pfd->revents = LWS_POLLOUT;
211 n = lws_service_fd(context, pfd);
214 /* if something closed, retry this slot */
220 * is there anybody with pending stuff that needs service forcing?
222 if (!lws_service_adjust_timeout(context, 1, tsi)) {
223 /* -1 timeout means just do forced service */
224 _lws_plat_service_tsi(context, -1, pt->tid);
225 /* still somebody left who wants forced service? */
226 if (!lws_service_adjust_timeout(context, 1, pt->tid))
227 /* yes... come back again quickly */
231 ev = WSAWaitForMultipleEvents( 1, pt->events , FALSE, timeout_ms, FALSE);
232 if (ev == WSA_WAIT_EVENT_0) {
235 WSAResetEvent(pt->events[0]);
237 for (eIdx = 0; eIdx < pt->fds_count; ++eIdx) {
238 if (WSAEnumNetworkEvents(pt->fds[eIdx].fd, 0, &networkevents) == SOCKET_ERROR) {
239 lwsl_err("WSAEnumNetworkEvents() failed with error %d\n", LWS_ERRNO);
243 pfd = &pt->fds[eIdx];
244 pfd->revents = (short)networkevents.lNetworkEvents;
246 if ((networkevents.lNetworkEvents & FD_CONNECT) &&
247 networkevents.iErrorCode[FD_CONNECT_BIT] &&
248 networkevents.iErrorCode[FD_CONNECT_BIT] != LWS_EALREADY &&
249 networkevents.iErrorCode[FD_CONNECT_BIT] != LWS_EINPROGRESS &&
250 networkevents.iErrorCode[FD_CONNECT_BIT] != LWS_EWOULDBLOCK &&
251 networkevents.iErrorCode[FD_CONNECT_BIT] != WSAEINVAL) {
252 lwsl_debug("Unable to connect errno=%d\n",
253 networkevents.iErrorCode[FD_CONNECT_BIT]);
254 pfd->revents = LWS_POLLHUP;
256 pfd->revents = (short)networkevents.lNetworkEvents;
258 if (pfd->revents & LWS_POLLOUT) {
259 wsi = wsi_from_fd(context, pfd->fd);
261 wsi->sock_send_blocking = 0;
263 /* if something closed, retry this slot */
264 if (pfd->revents & LWS_POLLHUP)
267 if( pfd->revents != 0 ) {
268 lws_service_fd_tsi(context, pfd, tsi);
274 context->service_tid = 0;
276 if (ev == WSA_WAIT_TIMEOUT) {
277 lws_service_fd(context, NULL);
283 lws_plat_service(struct lws_context *context, int timeout_ms)
285 return _lws_plat_service_tsi(context, timeout_ms, 0);
289 lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd)
292 int optlen = sizeof(optval);
295 struct tcp_keepalive alive;
298 struct protoent *tcp_proto;
301 if (vhost->ka_time) {
302 /* enable keepalive on this socket */
304 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
305 (const char *)&optval, optlen) < 0)
309 alive.keepalivetime = vhost->ka_time;
310 alive.keepaliveinterval = vhost->ka_interval;
312 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
313 NULL, 0, &dwBytesRet, NULL, NULL))
320 tcp_proto = getprotobyname("TCP");
322 lwsl_err("getprotobyname() failed with error %d\n", LWS_ERRNO);
325 protonbr = tcp_proto->p_proto;
330 setsockopt(fd, protonbr, TCP_NODELAY, (const char *)&optval, optlen);
332 /* We are nonblocking... */
333 ioctlsocket(fd, FIONBIO, &optl);
339 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
344 lws_plat_context_early_init(void)
346 WORD wVersionRequested;
350 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
351 wVersionRequested = MAKEWORD(2, 2);
353 err = WSAStartup(wVersionRequested, &wsaData);
357 * Tell the user that we could not find a usable
360 lwsl_err("WSAStartup failed with error: %d\n", err);
366 lws_plat_context_early_destroy(struct lws_context *context)
368 struct lws_context_per_thread *pt = &context->pt[0];
369 int n = context->count_threads;
373 WSACloseEvent(pt->events[0]);
374 lws_free(pt->events);
381 lws_plat_context_late_destroy(struct lws_context *context)
385 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
386 if (context->fd_hashtable[n].wsi)
387 lws_free(context->fd_hashtable[n].wsi);
393 LWS_VISIBLE LWS_EXTERN int
394 lws_interface_to_sa(int ipv6,
395 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
397 long long address = inet_addr(ifname);
399 if (address == INADDR_NONE) {
400 struct hostent *entry = gethostbyname(ifname);
402 address = ((struct in_addr *)entry->h_addr_list[0])->s_addr;
405 if (address == INADDR_NONE)
408 addr->sin_addr.s_addr = (unsigned long)address;
414 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
416 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
418 pt->fds[pt->fds_count++].revents = 0;
419 pt->events[pt->fds_count] = pt->events[0];
420 WSAEventSelect(wsi->sock, pt->events[0],
421 LWS_POLLIN | LWS_POLLHUP | FD_CONNECT);
425 lws_plat_delete_socket_from_fds(struct lws_context *context,
426 struct lws *wsi, int m)
428 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
430 pt->events[m + 1] = pt->events[pt->fds_count--];
434 lws_plat_service_periodic(struct lws_context *context)
439 lws_plat_check_connection_error(struct lws *wsi)
442 int optLen = sizeof(int);
444 if (getsockopt(wsi->sock, SOL_SOCKET, SO_ERROR,
445 (char*)&optVal, &optLen) != SOCKET_ERROR && optVal &&
446 optVal != LWS_EALREADY && optVal != LWS_EINPROGRESS &&
447 optVal != LWS_EWOULDBLOCK && optVal != WSAEINVAL) {
448 lwsl_debug("Connect failed SO_ERROR=%d\n", optVal);
456 lws_plat_change_pollfd(struct lws_context *context,
457 struct lws *wsi, struct lws_pollfd *pfd)
459 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
460 long networkevents = LWS_POLLHUP | FD_CONNECT;
462 if ((pfd->events & LWS_POLLIN))
463 networkevents |= LWS_POLLIN;
465 if ((pfd->events & LWS_POLLOUT))
466 networkevents |= LWS_POLLOUT;
468 if (WSAEventSelect(wsi->sock,
470 networkevents) != SOCKET_ERROR)
473 lwsl_err("WSAEventSelect() failed with error %d\n", LWS_ERRNO);
478 LWS_VISIBLE const char *
479 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
482 DWORD bufferlen = cnt;
485 buffer = lws_malloc(bufferlen * 2);
487 lwsl_err("Out of memory\n");
492 struct sockaddr_in srcaddr;
493 bzero(&srcaddr, sizeof(srcaddr));
494 srcaddr.sin_family = AF_INET;
495 memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
497 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
500 } else if (af == AF_INET6) {
501 struct sockaddr_in6 srcaddr;
502 bzero(&srcaddr, sizeof(srcaddr));
503 srcaddr.sin6_family = AF_INET6;
504 memcpy(&(srcaddr.sin6_addr), src, sizeof(srcaddr.sin6_addr));
506 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
510 lwsl_err("Unsupported type\n");
513 int rv = WSAGetLastError();
514 lwsl_err("WSAAddressToString() : %d\n", rv);
516 if (WideCharToMultiByte(CP_ACP, 0, buffer, bufferlen, dst, cnt, 0, NULL) <= 0)
521 return ok ? dst : NULL;
524 static lws_filefd_type
525 _lws_plat_file_open(struct lws *wsi, const char *filename,
526 unsigned long *filelen, int *flags)
532 MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, ARRAY_SIZE(buf));
533 if (((*flags) & 7) == _O_RDONLY) {
534 ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
535 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
537 lwsl_err("%s: open for write not implemented\n", __func__);
539 return LWS_INVALID_FILE;
542 if (ret != LWS_INVALID_FILE)
543 *filelen = GetFileSize(ret, NULL);
549 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
553 CloseHandle((HANDLE)fd);
559 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
563 return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT);
567 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
568 unsigned char* buf, unsigned long len)
572 if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) {
578 *amount = (unsigned long)_amount;
584 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
585 unsigned char* buf, unsigned long len)
593 lwsl_err("%s: not implemented yet on this platform\n", __func__);
599 lws_plat_init(struct lws_context *context,
600 struct lws_context_creation_info *info)
602 struct lws_context_per_thread *pt = &context->pt[0];
603 int i, n = context->count_threads;
605 for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
606 context->fd_hashtable[i].wsi =
607 lws_zalloc(sizeof(struct lws*) * context->max_fds);
609 if (!context->fd_hashtable[i].wsi)
614 pt->events = lws_malloc(sizeof(WSAEVENT) *
615 (context->fd_limit_per_thread + 1));
616 if (pt->events == NULL) {
617 lwsl_err("Unable to allocate events array for %d connections\n",
618 context->fd_limit_per_thread + 1);
623 pt->events[0] = WSACreateEvent();
628 context->fd_random = 0;
630 context->fops.open = _lws_plat_file_open;
631 context->fops.close = _lws_plat_file_close;
632 context->fops.seek_cur = _lws_plat_file_seek_cur;
633 context->fops.read = _lws_plat_file_read;
634 context->fops.write = _lws_plat_file_write;
636 #ifdef LWS_WITH_PLUGINS
637 if (info->plugin_dirs)
638 lws_plat_plugins_init(context, info->plugin_dirs);
645 int kill(int pid, int sig)
647 lwsl_err("Sorry Windows doesn't support kill().");
653 lwsl_err("Sorry Windows doesn't support fork().");