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;
39 /* file descriptor hash management */
42 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd)
44 int h = LWS_FD_HASH(fd);
47 for (n = 0; n < context->fd_hashtable[h].length; n++)
48 if (context->fd_hashtable[h].wsi[n]->sock == fd)
49 return context->fd_hashtable[h].wsi[n];
55 insert_wsi(struct lws_context *context, struct lws *wsi)
57 int h = LWS_FD_HASH(wsi->sock);
59 if (context->fd_hashtable[h].length == (getdtablesize() - 1)) {
60 lwsl_err("hash table overflow\n");
64 context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi;
70 delete_from_fd(struct lws_context *context, lws_sockfd_type fd)
72 int h = LWS_FD_HASH(fd);
75 for (n = 0; n < context->fd_hashtable[h].length; n++)
76 if (context->fd_hashtable[h].wsi[n]->sock == fd) {
77 while (n < context->fd_hashtable[h].length) {
78 context->fd_hashtable[h].wsi[n] =
79 context->fd_hashtable[h].wsi[n + 1];
82 context->fd_hashtable[h].length--;
87 lwsl_err("Failed to find fd %d requested for "
88 "delete in hashtable\n", fd);
92 LWS_VISIBLE int lws_get_random(struct lws_context *context,
96 char *p = (char *)buf;
98 for (n = 0; n < len; n++)
99 p[n] = (unsigned char)rand();
104 LWS_VISIBLE int lws_send_pipe_choked(struct lws *wsi)
106 return (int)wsi->sock_send_blocking;
109 LWS_VISIBLE int lws_poll_listen_fd(struct lws_pollfd *fd)
112 struct timeval tv = { 0, 0 };
114 assert((fd->events & LWS_POLLIN) == LWS_POLLIN);
117 FD_SET(fd->fd, &readfds);
119 return select(fd->fd + 1, &readfds, NULL, NULL, &tv);
123 * lws_cancel_service() - Cancel servicing of pending websocket activity
124 * @context: Websocket context
126 * This function let a call to lws_service() waiting for a timeout
127 * immediately return.
130 lws_cancel_service(struct lws_context *context)
132 struct lws_context_per_thread *pt = &context->pt[0];
133 int n = context->count_threads;
136 WSASetEvent(pt->events[0]);
142 lws_cancel_service_pt(struct lws *wsi)
144 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
145 WSASetEvent(pt->events[0]);
148 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
150 lwsl_emit_stderr(level, line);
154 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
156 struct lws_context_per_thread *pt = &context->pt[tsi];
157 WSANETWORKEVENTS networkevents;
158 struct lws_pollfd *pfd;
164 /* stay dead once we are dead */
168 if (!context->service_tid_detected) {
171 memset(&_lws, 0, sizeof(_lws));
172 _lws.context = context;
174 context->service_tid_detected = context->protocols[0].callback(
175 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
177 context->service_tid = context->service_tid_detected;
179 for (i = 0; i < pt->fds_count; ++i) {
181 if (pfd->fd == pt->lserv_fd)
184 if (pfd->events & LWS_POLLOUT) {
185 wsi = wsi_from_fd(context, pfd->fd);
186 if (!wsi || wsi->sock_send_blocking)
188 pfd->revents = LWS_POLLOUT;
189 n = lws_service_fd(context, pfd);
192 /* if something closed, retry this slot */
198 /* if we know something needs service already, don't wait in poll */
199 timeout_ms = lws_service_adjust_timeout(context, timeout_ms, tsi);
201 ev = WSAWaitForMultipleEvents(pt->fds_count + 1, pt->events,
202 FALSE, timeout_ms, FALSE);
203 context->service_tid = 0;
205 if (ev == WSA_WAIT_TIMEOUT) {
206 lws_service_fd(context, NULL);
210 if (ev == WSA_WAIT_EVENT_0) {
211 WSAResetEvent(pt->events[0]);
215 if (ev < WSA_WAIT_EVENT_0 || ev > WSA_WAIT_EVENT_0 + pt->fds_count)
218 pfd = &pt->fds[ev - WSA_WAIT_EVENT_0 - 1];
220 /* eh... is one event at a time the best windows can do? */
222 if (WSAEnumNetworkEvents(pfd->fd, pt->events[ev - WSA_WAIT_EVENT_0],
223 &networkevents) == SOCKET_ERROR) {
224 lwsl_err("WSAEnumNetworkEvents() failed with error %d\n",
229 pfd->revents = (short)networkevents.lNetworkEvents;
231 if (pfd->revents & LWS_POLLOUT) {
232 wsi = wsi_from_fd(context, pfd->fd);
234 wsi->sock_send_blocking = 0;
237 /* if someone faked their LWS_POLLIN, then go through all active fds */
239 if (lws_service_flag_pending(context, tsi)) {
240 /* any socket with events to service? */
241 for (n = 0; n < (int)pt->fds_count; n++) {
242 if (!pt->fds[n].revents)
245 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
248 /* if something closed, retry this slot */
255 /* otherwise just do the one... must be a way to improve that... */
257 return lws_service_fd_tsi(context, pfd, tsi);
261 lws_plat_service(struct lws_context *context, int timeout_ms)
263 return lws_plat_service_tsi(context, timeout_ms, 0);
267 lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd)
270 int optlen = sizeof(optval);
273 struct tcp_keepalive alive;
276 struct protoent *tcp_proto;
279 if (context->ka_time) {
280 /* enable keepalive on this socket */
282 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
283 (const char *)&optval, optlen) < 0)
287 alive.keepalivetime = context->ka_time;
288 alive.keepaliveinterval = context->ka_interval;
290 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
291 NULL, 0, &dwBytesRet, NULL, NULL))
298 tcp_proto = getprotobyname("TCP");
300 lwsl_err("getprotobyname() failed with error %d\n", LWS_ERRNO);
303 protonbr = tcp_proto->p_proto;
308 setsockopt(fd, protonbr, TCP_NODELAY, (const char *)&optval, optlen);
310 /* We are nonblocking... */
311 ioctlsocket(fd, FIONBIO, &optl);
317 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
322 lws_plat_context_early_init(void)
324 WORD wVersionRequested;
328 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
329 wVersionRequested = MAKEWORD(2, 2);
331 err = WSAStartup(wVersionRequested, &wsaData);
335 * Tell the user that we could not find a usable
338 lwsl_err("WSAStartup failed with error: %d\n", err);
344 lws_plat_context_early_destroy(struct lws_context *context)
346 struct lws_context_per_thread *pt = &context->pt[0];
347 int n = context->count_threads;
351 WSACloseEvent(pt->events[0]);
352 lws_free(pt->events);
359 lws_plat_context_late_destroy(struct lws_context *context)
363 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
364 if (context->fd_hashtable[n].wsi)
365 lws_free(context->fd_hashtable[n].wsi);
371 LWS_VISIBLE LWS_EXTERN int
372 lws_interface_to_sa(int ipv6,
373 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
375 long long address = inet_addr(ifname);
377 if (address == INADDR_NONE) {
378 struct hostent *entry = gethostbyname(ifname);
380 address = ((struct in_addr *)entry->h_addr_list[0])->s_addr;
383 if (address == INADDR_NONE)
386 addr->sin_addr.s_addr = (unsigned long)address;
392 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
394 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
396 pt->fds[pt->fds_count++].revents = 0;
397 pt->events[pt->fds_count] = WSACreateEvent();
398 WSAEventSelect(wsi->sock, pt->events[pt->fds_count],
399 LWS_POLLIN | LWS_POLLHUP);
403 lws_plat_delete_socket_from_fds(struct lws_context *context,
404 struct lws *wsi, int m)
406 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
408 WSACloseEvent(pt->events[m + 1]);
409 pt->events[m + 1] = pt->events[pt->fds_count--];
413 lws_plat_service_periodic(struct lws_context *context)
418 lws_plat_change_pollfd(struct lws_context *context,
419 struct lws *wsi, struct lws_pollfd *pfd)
421 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
422 long networkevents = LWS_POLLHUP;
424 if ((pfd->events & LWS_POLLIN))
425 networkevents |= LWS_POLLIN;
427 if ((pfd->events & LWS_POLLOUT))
428 networkevents |= LWS_POLLOUT;
430 if (WSAEventSelect(wsi->sock,
431 pt->events[wsi->position_in_fds_table + 1],
432 networkevents) != SOCKET_ERROR)
435 lwsl_err("WSAEventSelect() failed with error %d\n", LWS_ERRNO);
440 LWS_VISIBLE const char *
441 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
444 DWORD bufferlen = cnt;
447 buffer = lws_malloc(bufferlen);
449 lwsl_err("Out of memory\n");
454 struct sockaddr_in srcaddr;
455 bzero(&srcaddr, sizeof(srcaddr));
456 srcaddr.sin_family = AF_INET;
457 memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
459 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
462 } else if (af == AF_INET6) {
463 struct sockaddr_in6 srcaddr;
464 bzero(&srcaddr, sizeof(srcaddr));
465 srcaddr.sin6_family = AF_INET6;
466 memcpy(&(srcaddr.sin6_addr), src, sizeof(srcaddr.sin6_addr));
468 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
472 lwsl_err("Unsupported type\n");
475 int rv = WSAGetLastError();
476 lwsl_err("WSAAddressToString() : %d\n", rv);
478 if (WideCharToMultiByte(CP_ACP, 0, buffer, bufferlen, dst, cnt, 0, NULL) <= 0)
483 return ok ? dst : NULL;
486 static lws_filefd_type
487 _lws_plat_file_open(struct lws *wsi, const char *filename,
488 unsigned long *filelen, int flags)
494 MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, ARRAY_SIZE(buf));
495 if ((flags & 7) == _O_RDONLY) {
496 ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
497 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
499 lwsl_err("%s: open for write not implemented\n", __func__);
501 return LWS_INVALID_FILE;
504 if (ret != LWS_INVALID_FILE)
505 *filelen = GetFileSize(ret, NULL);
511 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
515 CloseHandle((HANDLE)fd);
521 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
525 return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT);
529 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
530 unsigned char* buf, unsigned long len)
535 if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) {
541 *amount = (unsigned long)_amount;
547 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
548 unsigned char* buf, unsigned long len)
556 lwsl_err("%s: not implemented yet on this platform\n", __func__);
562 lws_plat_init(struct lws_context *context,
563 struct lws_context_creation_info *info)
565 struct lws_context_per_thread *pt = &context->pt[0];
566 int i, n = context->count_threads;
568 for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
569 context->fd_hashtable[i].wsi =
570 lws_zalloc(sizeof(struct lws*) * context->max_fds);
572 if (!context->fd_hashtable[i].wsi)
577 pt->events = lws_malloc(sizeof(WSAEVENT) *
578 (context->fd_limit_per_thread + 1));
579 if (pt->events == NULL) {
580 lwsl_err("Unable to allocate events array for %d connections\n",
581 context->fd_limit_per_thread + 1);
586 pt->events[0] = WSACreateEvent();
591 context->fd_random = 0;
593 context->fops.open = _lws_plat_file_open;
594 context->fops.close = _lws_plat_file_close;
595 context->fops.seek_cur = _lws_plat_file_seek_cur;
596 context->fops.read = _lws_plat_file_read;
597 context->fops.write = _lws_plat_file_write;