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() - Cancel servicing of pending websocket activity
127 * @context: Websocket context
129 * This function let a call to lws_service() waiting for a timeout
130 * immediately return.
133 lws_cancel_service(struct lws_context *context)
135 struct lws_context_per_thread *pt = &context->pt[0];
136 int n = context->count_threads;
139 WSASetEvent(pt->events[0]);
145 lws_cancel_service_pt(struct lws *wsi)
147 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
148 WSASetEvent(pt->events[0]);
151 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
153 lwsl_emit_stderr(level, line);
157 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
159 struct lws_context_per_thread *pt = &context->pt[tsi];
160 WSANETWORKEVENTS networkevents;
161 struct lws_pollfd *pfd;
167 /* stay dead once we are dead */
171 if (!context->service_tid_detected) {
174 memset(&_lws, 0, sizeof(_lws));
175 _lws.context = context;
177 context->service_tid_detected = context->vhost_list->
178 protocols[0].callback(&_lws, LWS_CALLBACK_GET_THREAD_ID,
181 context->service_tid = context->service_tid_detected;
186 for (i = 0; i < pt->fds_count; ++i) {
189 if (!(pfd->events & LWS_POLLOUT))
192 wsi = wsi_from_fd(context, pfd->fd);
195 if (!wsi || wsi->sock_send_blocking)
197 pfd->revents = LWS_POLLOUT;
198 n = lws_service_fd(context, pfd);
201 /* if something closed, retry this slot */
206 /* if we know something needs service already, don't wait in poll */
207 timeout_ms = lws_service_adjust_timeout(context, timeout_ms, tsi);
209 ev = WSAWaitForMultipleEvents(pt->fds_count + 1, pt->events,
210 FALSE, timeout_ms, FALSE);
211 context->service_tid = 0;
213 if (ev == WSA_WAIT_TIMEOUT) {
214 lws_service_fd(context, NULL);
218 if (ev == WSA_WAIT_EVENT_0) {
219 WSAResetEvent(pt->events[0]);
223 if (ev < WSA_WAIT_EVENT_0 || ev > WSA_WAIT_EVENT_0 + pt->fds_count)
226 pfd = &pt->fds[ev - WSA_WAIT_EVENT_0 - 1];
228 /* eh... is one event at a time the best windows can do? */
230 if (WSAEnumNetworkEvents(pfd->fd, pt->events[ev - WSA_WAIT_EVENT_0],
231 &networkevents) == SOCKET_ERROR) {
232 lwsl_err("WSAEnumNetworkEvents() failed with error %d\n",
237 pfd->revents = (short)networkevents.lNetworkEvents;
239 if (pfd->revents & LWS_POLLOUT) {
240 wsi = wsi_from_fd(context, pfd->fd);
242 wsi->sock_send_blocking = 0;
247 /* if someone faked their LWS_POLLIN, then go through all active fds */
249 if (lws_service_flag_pending(context, tsi)) {
250 /* any socket with events to service? */
251 for (n = 0; n < (int)pt->fds_count; n++) {
252 if (!pt->fds[n].revents)
255 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
258 /* if something closed, retry this slot */
268 /* otherwise just do the one... must be a way to improve that... */
270 return lws_service_fd_tsi(context, pfd, tsi);
274 lws_plat_service(struct lws_context *context, int timeout_ms)
276 return lws_plat_service_tsi(context, timeout_ms, 0);
280 lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd)
283 int optlen = sizeof(optval);
286 struct tcp_keepalive alive;
289 struct protoent *tcp_proto;
292 if (vhost->ka_time) {
293 /* enable keepalive on this socket */
295 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
296 (const char *)&optval, optlen) < 0)
300 alive.keepalivetime = vhost->ka_time;
301 alive.keepaliveinterval = vhost->ka_interval;
303 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
304 NULL, 0, &dwBytesRet, NULL, NULL))
311 tcp_proto = getprotobyname("TCP");
313 lwsl_err("getprotobyname() failed with error %d\n", LWS_ERRNO);
316 protonbr = tcp_proto->p_proto;
321 setsockopt(fd, protonbr, TCP_NODELAY, (const char *)&optval, optlen);
323 /* We are nonblocking... */
324 ioctlsocket(fd, FIONBIO, &optl);
330 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
335 lws_plat_context_early_init(void)
337 WORD wVersionRequested;
341 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
342 wVersionRequested = MAKEWORD(2, 2);
344 err = WSAStartup(wVersionRequested, &wsaData);
348 * Tell the user that we could not find a usable
351 lwsl_err("WSAStartup failed with error: %d\n", err);
357 lws_plat_context_early_destroy(struct lws_context *context)
359 struct lws_context_per_thread *pt = &context->pt[0];
360 int n = context->count_threads;
364 WSACloseEvent(pt->events[0]);
365 lws_free(pt->events);
372 lws_plat_context_late_destroy(struct lws_context *context)
376 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
377 if (context->fd_hashtable[n].wsi)
378 lws_free(context->fd_hashtable[n].wsi);
384 LWS_VISIBLE LWS_EXTERN int
385 lws_interface_to_sa(int ipv6,
386 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
388 long long address = inet_addr(ifname);
390 if (address == INADDR_NONE) {
391 struct hostent *entry = gethostbyname(ifname);
393 address = ((struct in_addr *)entry->h_addr_list[0])->s_addr;
396 if (address == INADDR_NONE)
399 addr->sin_addr.s_addr = (unsigned long)address;
405 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
407 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
409 pt->fds[pt->fds_count++].revents = 0;
410 pt->events[pt->fds_count] = WSACreateEvent();
411 WSAEventSelect(wsi->sock, pt->events[pt->fds_count],
412 LWS_POLLIN | LWS_POLLHUP);
416 lws_plat_delete_socket_from_fds(struct lws_context *context,
417 struct lws *wsi, int m)
419 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
421 WSACloseEvent(pt->events[m + 1]);
422 pt->events[m + 1] = pt->events[pt->fds_count--];
426 lws_plat_service_periodic(struct lws_context *context)
431 lws_plat_change_pollfd(struct lws_context *context,
432 struct lws *wsi, struct lws_pollfd *pfd)
434 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
435 long networkevents = LWS_POLLHUP;
437 if ((pfd->events & LWS_POLLIN))
438 networkevents |= LWS_POLLIN;
440 if ((pfd->events & LWS_POLLOUT))
441 networkevents |= LWS_POLLOUT;
443 if (WSAEventSelect(wsi->sock,
444 pt->events[wsi->position_in_fds_table + 1],
445 networkevents) != SOCKET_ERROR)
448 lwsl_err("WSAEventSelect() failed with error %d\n", LWS_ERRNO);
453 LWS_VISIBLE const char *
454 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
457 DWORD bufferlen = cnt;
460 buffer = lws_malloc(bufferlen);
462 lwsl_err("Out of memory\n");
467 struct sockaddr_in srcaddr;
468 bzero(&srcaddr, sizeof(srcaddr));
469 srcaddr.sin_family = AF_INET;
470 memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
472 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
475 } else if (af == AF_INET6) {
476 struct sockaddr_in6 srcaddr;
477 bzero(&srcaddr, sizeof(srcaddr));
478 srcaddr.sin6_family = AF_INET6;
479 memcpy(&(srcaddr.sin6_addr), src, sizeof(srcaddr.sin6_addr));
481 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
485 lwsl_err("Unsupported type\n");
488 int rv = WSAGetLastError();
489 lwsl_err("WSAAddressToString() : %d\n", rv);
491 if (WideCharToMultiByte(CP_ACP, 0, buffer, bufferlen, dst, cnt, 0, NULL) <= 0)
496 return ok ? dst : NULL;
499 static lws_filefd_type
500 _lws_plat_file_open(struct lws *wsi, const char *filename,
501 unsigned long *filelen, int flags)
507 MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, ARRAY_SIZE(buf));
508 if ((flags & 7) == _O_RDONLY) {
509 ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
510 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
512 lwsl_err("%s: open for write not implemented\n", __func__);
514 return LWS_INVALID_FILE;
517 if (ret != LWS_INVALID_FILE)
518 *filelen = GetFileSize(ret, NULL);
524 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
528 CloseHandle((HANDLE)fd);
534 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
538 return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT);
542 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
543 unsigned char* buf, unsigned long len)
547 if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) {
553 *amount = (unsigned long)_amount;
559 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
560 unsigned char* buf, unsigned long len)
568 lwsl_err("%s: not implemented yet on this platform\n", __func__);
574 lws_plat_init(struct lws_context *context,
575 struct lws_context_creation_info *info)
577 struct lws_context_per_thread *pt = &context->pt[0];
578 int i, n = context->count_threads;
580 for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
581 context->fd_hashtable[i].wsi =
582 lws_zalloc(sizeof(struct lws*) * context->max_fds);
584 if (!context->fd_hashtable[i].wsi)
589 pt->events = lws_malloc(sizeof(WSAEVENT) *
590 (context->fd_limit_per_thread + 1));
591 if (pt->events == NULL) {
592 lwsl_err("Unable to allocate events array for %d connections\n",
593 context->fd_limit_per_thread + 1);
598 pt->events[0] = WSACreateEvent();
603 context->fd_random = 0;
605 context->fops.open = _lws_plat_file_open;
606 context->fops.close = _lws_plat_file_close;
607 context->fops.seek_cur = _lws_plat_file_seek_cur;
608 context->fops.read = _lws_plat_file_read;
609 context->fops.write = _lws_plat_file_write;