1 #include "private-libwebsockets.h"
6 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
8 ULARGE_INTEGER datetime;
11 GetCurrentFT(&filetime);
13 GetSystemTimeAsFileTime(&filetime);
17 * As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
18 * ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
19 * prevent alignment faults on 64-bit Windows).
21 memcpy(&datetime, &filetime, sizeof(datetime));
23 /* Windows file times are in 100s of nanoseconds. */
24 return (datetime.QuadPart - DELTA_EPOCH_IN_MICROSECS) / 10;
28 time_t time(time_t *t)
30 time_t ret = time_in_microseconds() / 1000000;
36 /* file descriptor hash management */
39 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd)
41 int h = LWS_FD_HASH(fd);
44 for (n = 0; n < context->fd_hashtable[h].length; n++)
45 if (context->fd_hashtable[h].wsi[n]->sock == fd)
46 return context->fd_hashtable[h].wsi[n];
52 insert_wsi(struct lws_context *context, struct lws *wsi)
54 int h = LWS_FD_HASH(wsi->sock);
56 if (context->fd_hashtable[h].length == (getdtablesize() - 1)) {
57 lwsl_err("hash table overflow\n");
61 context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi;
67 delete_from_fd(struct lws_context *context, lws_sockfd_type fd)
69 int h = LWS_FD_HASH(fd);
72 for (n = 0; n < context->fd_hashtable[h].length; n++)
73 if (context->fd_hashtable[h].wsi[n]->sock == fd) {
74 while (n < context->fd_hashtable[h].length) {
75 context->fd_hashtable[h].wsi[n] =
76 context->fd_hashtable[h].wsi[n + 1];
79 context->fd_hashtable[h].length--;
84 lwsl_err("Failed to find fd %d requested for "
85 "delete in hashtable\n", fd);
89 LWS_VISIBLE int lws_get_random(struct lws_context *context,
93 char *p = (char *)buf;
95 for (n = 0; n < len; n++)
96 p[n] = (unsigned char)rand();
101 LWS_VISIBLE int lws_send_pipe_choked(struct lws *wsi)
103 return wsi->sock_send_blocking;
106 LWS_VISIBLE int lws_poll_listen_fd(struct lws_pollfd *fd)
109 struct timeval tv = { 0, 0 };
111 assert(fd->events == LWS_POLLIN);
114 FD_SET(fd->fd, &readfds);
116 return select(fd->fd + 1, &readfds, NULL, NULL, &tv);
120 * lws_cancel_service() - Cancel servicing of pending websocket activity
121 * @context: Websocket context
123 * This function let a call to lws_service() waiting for a timeout
124 * immediately return.
127 lws_cancel_service(struct lws_context *context)
129 WSASetEvent(context->events[0]);
132 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
134 lwsl_emit_stderr(level, line);
138 lws_plat_service(struct lws_context *context, int timeout_ms)
143 WSANETWORKEVENTS networkevents;
144 struct lws_pollfd *pfd;
147 /* stay dead once we are dead */
152 context->service_tid = context->protocols[0].callback(NULL,
153 LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
155 for (i = 0; i < context->fds_count; ++i) {
156 pfd = &context->fds[i];
157 if (pfd->fd == context->listen_service_fd)
160 if (pfd->events & LWS_POLLOUT) {
161 wsi = wsi_from_fd(context, pfd->fd);
162 if (!wsi || wsi->sock_send_blocking)
164 pfd->revents = LWS_POLLOUT;
165 n = lws_service_fd(context, pfd);
168 /* if something closed, retry this slot */
174 ev = WSAWaitForMultipleEvents(context->fds_count + 1,
175 context->events, FALSE, timeout_ms, FALSE);
176 context->service_tid = 0;
178 if (ev == WSA_WAIT_TIMEOUT) {
179 lws_service_fd(context, NULL);
183 if (ev == WSA_WAIT_EVENT_0) {
184 WSAResetEvent(context->events[0]);
188 if (ev < WSA_WAIT_EVENT_0 || ev > WSA_WAIT_EVENT_0 + context->fds_count)
191 pfd = &context->fds[ev - WSA_WAIT_EVENT_0 - 1];
193 if (WSAEnumNetworkEvents(pfd->fd,
194 context->events[ev - WSA_WAIT_EVENT_0],
195 &networkevents) == SOCKET_ERROR) {
196 lwsl_err("WSAEnumNetworkEvents() failed with error %d\n",
201 pfd->revents = (short)networkevents.lNetworkEvents;
203 if (pfd->revents & LWS_POLLOUT) {
204 wsi = wsi_from_fd(context, pfd->fd);
206 wsi->sock_send_blocking = FALSE;
209 return lws_service_fd(context, pfd);
213 lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd)
216 int optlen = sizeof(optval);
219 struct tcp_keepalive alive;
220 struct protoent *tcp_proto;
222 if (context->ka_time) {
223 /* enable keepalive on this socket */
225 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
226 (const char *)&optval, optlen) < 0)
230 alive.keepalivetime = context->ka_time;
231 alive.keepaliveinterval = context->ka_interval;
233 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
234 NULL, 0, &dwBytesRet, NULL, NULL))
240 tcp_proto = getprotobyname("TCP");
242 lwsl_err("getprotobyname() failed with error %d\n", LWS_ERRNO);
246 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, (const char *)&optval, optlen);
248 /* We are nonblocking... */
249 ioctlsocket(fd, FIONBIO, &optl);
255 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
260 lws_plat_context_early_init(void)
262 WORD wVersionRequested;
266 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
267 wVersionRequested = MAKEWORD(2, 2);
269 err = WSAStartup(wVersionRequested, &wsaData);
273 * Tell the user that we could not find a usable
276 lwsl_err("WSAStartup failed with error: %d\n", err);
282 lws_plat_context_early_destroy(struct lws_context *context)
284 if (context->events) {
285 WSACloseEvent(context->events[0]);
286 lws_free(context->events);
291 lws_plat_context_late_destroy(struct lws_context *context)
295 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
296 if (context->fd_hashtable[n].wsi)
297 lws_free(context->fd_hashtable[n].wsi);
304 interface_to_sa(struct lws_context *context,
305 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
307 long long address = inet_addr(ifname);
309 if (address == INADDR_NONE) {
310 struct hostent *entry = gethostbyname(ifname);
312 address = ((struct in_addr *)entry->h_addr_list[0])->s_addr;
315 if (address == INADDR_NONE)
318 addr->sin_addr.s_addr = (unsigned long)address;
324 lws_plat_insert_socket_into_fds(struct lws_context *context,
327 context->fds[context->fds_count++].revents = 0;
328 context->events[context->fds_count] = WSACreateEvent();
329 WSAEventSelect(wsi->sock, context->events[context->fds_count], LWS_POLLIN);
333 lws_plat_delete_socket_from_fds(struct lws_context *context,
334 struct lws *wsi, int m)
336 WSACloseEvent(context->events[m + 1]);
337 context->events[m + 1] = context->events[context->fds_count + 1];
341 lws_plat_service_periodic(struct lws_context *context)
346 lws_plat_change_pollfd(struct lws_context *context,
347 struct lws *wsi, struct lws_pollfd *pfd)
349 long networkevents = LWS_POLLHUP;
351 if ((pfd->events & LWS_POLLIN))
352 networkevents |= LWS_POLLIN;
354 if ((pfd->events & LWS_POLLOUT))
355 networkevents |= LWS_POLLOUT;
357 if (WSAEventSelect(wsi->sock,
358 context->events[wsi->position_in_fds_table + 1],
359 networkevents) != SOCKET_ERROR)
362 lwsl_err("WSAEventSelect() failed with error %d\n", LWS_ERRNO);
367 LWS_VISIBLE const char *
368 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
371 DWORD bufferlen = cnt;
374 buffer = lws_malloc(bufferlen);
376 lwsl_err("Out of memory\n");
381 struct sockaddr_in srcaddr;
382 bzero(&srcaddr, sizeof(srcaddr));
383 srcaddr.sin_family = AF_INET;
384 memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
386 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
389 } else if (af == AF_INET6) {
390 struct sockaddr_in6 srcaddr;
391 bzero(&srcaddr, sizeof(srcaddr));
392 srcaddr.sin6_family = AF_INET6;
393 memcpy(&(srcaddr.sin6_addr), src, sizeof(srcaddr.sin6_addr));
395 if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
399 lwsl_err("Unsupported type\n");
402 int rv = WSAGetLastError();
403 lwsl_err("WSAAddressToString() : %d\n", rv);
405 if (WideCharToMultiByte(CP_ACP, 0, buffer, bufferlen, dst, cnt, 0, NULL) <= 0)
410 return ok ? dst : NULL;
413 static lws_filefd_type
414 _lws_plat_file_open(struct lws *wsi, const char *filename,
415 unsigned long *filelen, int flags)
421 MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, ARRAY_SIZE(buf));
422 if ((flags & 7) == _O_RDONLY) {
423 ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
424 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
426 lwsl_err("%s: open for write not implemented\n", __func__);
428 return LWS_INVALID_FILE;
431 if (ret != LWS_INVALID_FILE)
432 *filelen = GetFileSize(ret, NULL);
438 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
442 CloseHandle((HANDLE)fd);
448 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
452 return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT);
456 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
457 unsigned char* buf, unsigned long len)
462 if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) {
468 *amount = (unsigned long)_amount;
474 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
475 unsigned char* buf, unsigned long len)
483 lwsl_err("%s: not implemented yet on this platform\n", __func__);
489 lws_plat_init(struct lws_context *context,
490 struct lws_context_creation_info *info)
494 for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
495 context->fd_hashtable[i].wsi =
496 lws_zalloc(sizeof(struct lws*) * context->max_fds);
498 if (!context->fd_hashtable[i].wsi)
502 context->events = lws_malloc(sizeof(WSAEVENT) * (context->max_fds + 1));
503 if (context->events == NULL) {
504 lwsl_err("Unable to allocate events array for %d connections\n",
509 context->fds_count = 0;
510 context->events[0] = WSACreateEvent();
512 context->fd_random = 0;
514 context->fops.open = _lws_plat_file_open;
515 context->fops.close = _lws_plat_file_close;
516 context->fops.seek_cur = _lws_plat_file_seek_cur;
517 context->fops.read = _lws_plat_file_read;
518 context->fops.write = _lws_plat_file_write;