1 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
2 #define _WINSOCK_DEPRECATED_NO_WARNINGS
4 #include "private-libwebsockets.h"
9 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
11 ULARGE_INTEGER datetime;
14 GetCurrentFT(&filetime);
16 GetSystemTimeAsFileTime(&filetime);
20 * As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
21 * ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
22 * prevent alignment faults on 64-bit Windows).
24 memcpy(&datetime, &filetime, sizeof(datetime));
26 /* Windows file times are in 100s of nanoseconds. */
27 return (datetime.QuadPart - DELTA_EPOCH_IN_MICROSECS) / 10;
31 time_t time(time_t *t)
33 time_t ret = time_in_microseconds() / 1000000;
42 /* file descriptor hash management */
45 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd)
47 int h = LWS_FD_HASH(fd);
50 for (n = 0; n < context->fd_hashtable[h].length; n++)
51 if (context->fd_hashtable[h].wsi[n]->sock == fd)
52 return context->fd_hashtable[h].wsi[n];
58 insert_wsi(struct lws_context *context, struct lws *wsi)
60 int h = LWS_FD_HASH(wsi->sock);
62 if (context->fd_hashtable[h].length == (getdtablesize() - 1)) {
63 lwsl_err("hash table overflow\n");
67 context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi;
73 delete_from_fd(struct lws_context *context, lws_sockfd_type fd)
75 int h = LWS_FD_HASH(fd);
78 for (n = 0; n < context->fd_hashtable[h].length; n++)
79 if (context->fd_hashtable[h].wsi[n]->sock == fd) {
80 while (n < context->fd_hashtable[h].length) {
81 context->fd_hashtable[h].wsi[n] =
82 context->fd_hashtable[h].wsi[n + 1];
85 context->fd_hashtable[h].length--;
90 lwsl_err("Failed to find fd %d requested for "
91 "delete in hashtable\n", fd);
95 LWS_VISIBLE int lws_get_random(struct lws_context *context,
99 char *p = (char *)buf;
101 for (n = 0; n < len; n++)
102 p[n] = (unsigned char)rand();
107 LWS_VISIBLE int lws_send_pipe_choked(struct lws *wsi)
109 return (int)wsi->sock_send_blocking;
112 LWS_VISIBLE int lws_poll_listen_fd(struct lws_pollfd *fd)
115 struct timeval tv = { 0, 0 };
117 assert((fd->events & LWS_POLLIN) == LWS_POLLIN);
120 FD_SET(fd->fd, &readfds);
122 return select(fd->fd + 1, &readfds, NULL, NULL, &tv);
126 lws_cancel_service(struct lws_context *context)
128 struct lws_context_per_thread *pt = &context->pt[0];
129 int n = context->count_threads;
132 WSASetEvent(pt->events[0]);
138 lws_cancel_service_pt(struct lws *wsi)
140 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
141 WSASetEvent(pt->events[0]);
144 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
146 lwsl_emit_stderr(level, line);
149 LWS_VISIBLE LWS_EXTERN int
150 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
152 struct lws_context_per_thread *pt = &context->pt[tsi];
153 WSANETWORKEVENTS networkevents;
154 struct lws_pollfd *pfd;
160 /* stay dead once we are dead */
164 if (!context->service_tid_detected) {
167 memset(&_lws, 0, sizeof(_lws));
168 _lws.context = context;
170 context->service_tid_detected = context->vhost_list->
171 protocols[0].callback(&_lws, LWS_CALLBACK_GET_THREAD_ID,
174 context->service_tid = context->service_tid_detected;
178 if (lws_service_flag_pending(context, tsi)) {
179 /* any socket with events to service? */
180 for (n = 0; n < (int)pt->fds_count; n++) {
181 if (!pt->fds[n].revents)
184 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
187 /* if something closed, retry this slot */
195 for (i = 0; i < pt->fds_count; ++i) {
198 if (!(pfd->events & LWS_POLLOUT))
201 wsi = wsi_from_fd(context, pfd->fd);
204 if (!wsi || wsi->sock_send_blocking)
206 pfd->revents = LWS_POLLOUT;
207 n = lws_service_fd(context, pfd);
210 /* if something closed, retry this slot */
216 * is there anybody with pending stuff that needs service forcing?
218 if (!lws_service_adjust_timeout(context, 1, tsi)) {
219 /* -1 timeout means just do forced service */
220 _lws_plat_service_tsi(context, -1, pt->tid);
221 /* still somebody left who wants forced service? */
222 if (!lws_service_adjust_timeout(context, 1, pt->tid))
223 /* yes... come back again quickly */
227 ev = WSAWaitForMultipleEvents( 1, pt->events , FALSE, timeout_ms, FALSE);
228 if (ev == WSA_WAIT_EVENT_0) {
230 WSAResetEvent(pt->events[0]);
232 for(unsigned int eIdx = 0; eIdx < pt->fds_count; ++eIdx) {
233 if (WSAEnumNetworkEvents(pt->fds[eIdx].fd, 0, &networkevents) == SOCKET_ERROR) {
234 lwsl_err("WSAEnumNetworkEvents() failed with error %d\n", LWS_ERRNO);
238 pfd = &pt->fds[eIdx];
239 pfd->revents = (short)networkevents.lNetworkEvents;
241 if ((networkevents.lNetworkEvents & FD_CONNECT) &&
242 networkevents.iErrorCode[FD_CONNECT_BIT] &&
243 networkevents.iErrorCode[FD_CONNECT_BIT] != LWS_EALREADY &&
244 networkevents.iErrorCode[FD_CONNECT_BIT] != LWS_EINPROGRESS &&
245 networkevents.iErrorCode[FD_CONNECT_BIT] != LWS_EWOULDBLOCK &&
246 networkevents.iErrorCode[FD_CONNECT_BIT] != WSAEINVAL) {
247 lwsl_debug("Unable to connect errno=%d\n",
248 networkevents.iErrorCode[FD_CONNECT_BIT]);
249 pfd->revents = LWS_POLLHUP;
251 pfd->revents = (short)networkevents.lNetworkEvents;
253 if (pfd->revents & LWS_POLLOUT) {
254 wsi = wsi_from_fd(context, pfd->fd);
256 wsi->sock_send_blocking = 0;
258 /* if something closed, retry this slot */
259 if (pfd->revents & LWS_POLLHUP)
262 if( pfd->revents != 0 ) {
263 lws_service_fd_tsi(context, pfd, tsi);
269 context->service_tid = 0;
271 if (ev == WSA_WAIT_TIMEOUT) {
272 lws_service_fd(context, NULL);
278 lws_plat_service(struct lws_context *context, int timeout_ms)
280 return _lws_plat_service_tsi(context, timeout_ms, 0);
284 lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd)
287 int optlen = sizeof(optval);
290 struct tcp_keepalive alive;
293 struct protoent *tcp_proto;
296 if (vhost->ka_time) {
297 /* enable keepalive on this socket */
299 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
300 (const char *)&optval, optlen) < 0)
304 alive.keepalivetime = vhost->ka_time;
305 alive.keepaliveinterval = vhost->ka_interval;
307 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
308 NULL, 0, &dwBytesRet, NULL, NULL))
315 tcp_proto = getprotobyname("TCP");
317 lwsl_err("getprotobyname() failed with error %d\n", LWS_ERRNO);
320 protonbr = tcp_proto->p_proto;
325 setsockopt(fd, protonbr, TCP_NODELAY, (const char *)&optval, optlen);
327 /* We are nonblocking... */
328 ioctlsocket(fd, FIONBIO, &optl);
334 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
339 lws_plat_context_early_init(void)
341 WORD wVersionRequested;
345 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
346 wVersionRequested = MAKEWORD(2, 2);
348 err = WSAStartup(wVersionRequested, &wsaData);
352 * Tell the user that we could not find a usable
355 lwsl_err("WSAStartup failed with error: %d\n", err);
361 lws_plat_context_early_destroy(struct lws_context *context)
363 struct lws_context_per_thread *pt = &context->pt[0];
364 int n = context->count_threads;
368 WSACloseEvent(pt->events[0]);
369 lws_free(pt->events);
376 lws_plat_context_late_destroy(struct lws_context *context)
380 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
381 if (context->fd_hashtable[n].wsi)
382 lws_free(context->fd_hashtable[n].wsi);
388 LWS_VISIBLE LWS_EXTERN int
389 lws_interface_to_sa(int ipv6,
390 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
392 long long address = inet_addr(ifname);
394 if (address == INADDR_NONE) {
395 struct hostent *entry = gethostbyname(ifname);
397 address = ((struct in_addr *)entry->h_addr_list[0])->s_addr;
400 if (address == INADDR_NONE)
403 addr->sin_addr.s_addr = (unsigned long)address;
409 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
411 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
413 pt->fds[pt->fds_count++].revents = 0;
414 pt->events[pt->fds_count] = pt->events[0];
415 WSAEventSelect(wsi->sock, pt->events[0],
416 LWS_POLLIN | LWS_POLLHUP | FD_CONNECT);
420 lws_plat_delete_socket_from_fds(struct lws_context *context,
421 struct lws *wsi, int m)
423 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
425 pt->events[m + 1] = pt->events[pt->fds_count--];
429 lws_plat_service_periodic(struct lws_context *context)
434 lws_plat_check_connection_error(struct lws *wsi)
437 int optLen = sizeof(int);
439 if (getsockopt(wsi->sock, SOL_SOCKET, SO_ERROR,
440 (char*)&optVal, &optLen) != SOCKET_ERROR && optVal &&
441 optVal != LWS_EALREADY && optVal != LWS_EINPROGRESS &&
442 optVal != LWS_EWOULDBLOCK && optVal != WSAEINVAL) {
443 lwsl_debug("Connect failed SO_ERROR=%d\n", optVal);
451 lws_plat_change_pollfd(struct lws_context *context,
452 struct lws *wsi, struct lws_pollfd *pfd)
454 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
455 long networkevents = LWS_POLLHUP | FD_CONNECT;
457 if ((pfd->events & LWS_POLLIN))
458 networkevents |= LWS_POLLIN;
460 if ((pfd->events & LWS_POLLOUT))
461 networkevents |= LWS_POLLOUT;
463 if (WSAEventSelect(wsi->sock,
465 networkevents) != SOCKET_ERROR)
468 lwsl_err("WSAEventSelect() failed with error %d\n", LWS_ERRNO);
473 LWS_VISIBLE const char *
474 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
477 DWORD bufferlen = cnt;
480 buffer = lws_malloc(bufferlen * 2);
482 lwsl_err("Out of memory\n");
487 struct sockaddr_in srcaddr;
488 bzero(&srcaddr, sizeof(srcaddr));
489 srcaddr.sin_family = AF_INET;
490 memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
492 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
495 } else if (af == AF_INET6) {
496 struct sockaddr_in6 srcaddr;
497 bzero(&srcaddr, sizeof(srcaddr));
498 srcaddr.sin6_family = AF_INET6;
499 memcpy(&(srcaddr.sin6_addr), src, sizeof(srcaddr.sin6_addr));
501 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
505 lwsl_err("Unsupported type\n");
508 int rv = WSAGetLastError();
509 lwsl_err("WSAAddressToString() : %d\n", rv);
511 if (WideCharToMultiByte(CP_ACP, 0, buffer, bufferlen, dst, cnt, 0, NULL) <= 0)
516 return ok ? dst : NULL;
519 static lws_filefd_type
520 _lws_plat_file_open(struct lws *wsi, const char *filename,
521 unsigned long *filelen, int flags)
527 MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, ARRAY_SIZE(buf));
528 if ((flags & 7) == _O_RDONLY) {
529 ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
530 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
532 lwsl_err("%s: open for write not implemented\n", __func__);
534 return LWS_INVALID_FILE;
537 if (ret != LWS_INVALID_FILE)
538 *filelen = GetFileSize(ret, NULL);
544 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
548 CloseHandle((HANDLE)fd);
554 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
558 return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT);
562 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
563 unsigned char* buf, unsigned long len)
567 if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) {
573 *amount = (unsigned long)_amount;
579 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
580 unsigned char* buf, unsigned long len)
588 lwsl_err("%s: not implemented yet on this platform\n", __func__);
594 lws_plat_init(struct lws_context *context,
595 struct lws_context_creation_info *info)
597 struct lws_context_per_thread *pt = &context->pt[0];
598 int i, n = context->count_threads;
600 for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
601 context->fd_hashtable[i].wsi =
602 lws_zalloc(sizeof(struct lws*) * context->max_fds);
604 if (!context->fd_hashtable[i].wsi)
609 pt->events = lws_malloc(sizeof(WSAEVENT) *
610 (context->fd_limit_per_thread + 1));
611 if (pt->events == NULL) {
612 lwsl_err("Unable to allocate events array for %d connections\n",
613 context->fd_limit_per_thread + 1);
618 pt->events[0] = WSACreateEvent();
623 context->fd_random = 0;
625 context->fops.open = _lws_plat_file_open;
626 context->fops.close = _lws_plat_file_close;
627 context->fops.seek_cur = _lws_plat_file_seek_cur;
628 context->fops.read = _lws_plat_file_read;
629 context->fops.write = _lws_plat_file_write;
631 #ifdef LWS_WITH_PLUGINS
632 if (info->plugin_dirs)
633 lws_plat_plugins_init(context, info->plugin_dirs);