lws-plat-win.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  2. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  3. #endif
  4. #include "private-libwebsockets.h"
  5. unsigned long long
  6. time_in_microseconds()
  7. {
  8. #ifndef DELTA_EPOCH_IN_MICROSECS
  9. #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
  10. #endif
  11. FILETIME filetime;
  12. ULARGE_INTEGER datetime;
  13. #ifdef _WIN32_WCE
  14. GetCurrentFT(&filetime);
  15. #else
  16. GetSystemTimeAsFileTime(&filetime);
  17. #endif
  18. /*
  19. * As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
  20. * ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
  21. * prevent alignment faults on 64-bit Windows).
  22. */
  23. memcpy(&datetime, &filetime, sizeof(datetime));
  24. /* Windows file times are in 100s of nanoseconds. */
  25. return (datetime.QuadPart - DELTA_EPOCH_IN_MICROSECS) / 10;
  26. }
  27. #ifdef _WIN32_WCE
  28. time_t time(time_t *t)
  29. {
  30. time_t ret = time_in_microseconds() / 1000000;
  31. if(t != NULL)
  32. *t = ret;
  33. return ret;
  34. }
  35. #endif
  36. /* file descriptor hash management */
  37. struct lws *
  38. wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd)
  39. {
  40. int h = LWS_FD_HASH(fd);
  41. int n = 0;
  42. for (n = 0; n < context->fd_hashtable[h].length; n++)
  43. if (context->fd_hashtable[h].wsi[n]->desc.sockfd == fd)
  44. return context->fd_hashtable[h].wsi[n];
  45. return NULL;
  46. }
  47. int
  48. insert_wsi(struct lws_context *context, struct lws *wsi)
  49. {
  50. int h = LWS_FD_HASH(wsi->desc.sockfd);
  51. if (context->fd_hashtable[h].length == (getdtablesize() - 1)) {
  52. lwsl_err("hash table overflow\n");
  53. return 1;
  54. }
  55. context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi;
  56. return 0;
  57. }
  58. int
  59. delete_from_fd(struct lws_context *context, lws_sockfd_type fd)
  60. {
  61. int h = LWS_FD_HASH(fd);
  62. int n = 0;
  63. for (n = 0; n < context->fd_hashtable[h].length; n++)
  64. if (context->fd_hashtable[h].wsi[n]->desc.sockfd == fd) {
  65. while (n < context->fd_hashtable[h].length) {
  66. context->fd_hashtable[h].wsi[n] =
  67. context->fd_hashtable[h].wsi[n + 1];
  68. n++;
  69. }
  70. context->fd_hashtable[h].length--;
  71. return 0;
  72. }
  73. lwsl_err("Failed to find fd %d requested for "
  74. "delete in hashtable\n", fd);
  75. return 1;
  76. }
  77. LWS_VISIBLE int lws_get_random(struct lws_context *context,
  78. void *buf, int len)
  79. {
  80. int n;
  81. char *p = (char *)buf;
  82. for (n = 0; n < len; n++)
  83. p[n] = (unsigned char)rand();
  84. return n;
  85. }
  86. LWS_VISIBLE int lws_send_pipe_choked(struct lws *wsi)
  87. {
  88. /* treat the fact we got a truncated send pending as if we're choked */
  89. if (wsi->trunc_len)
  90. return 1;
  91. return (int)wsi->sock_send_blocking;
  92. }
  93. LWS_VISIBLE int lws_poll_listen_fd(struct lws_pollfd *fd)
  94. {
  95. fd_set readfds;
  96. struct timeval tv = { 0, 0 };
  97. assert((fd->events & LWS_POLLIN) == LWS_POLLIN);
  98. FD_ZERO(&readfds);
  99. FD_SET(fd->fd, &readfds);
  100. return select(fd->fd + 1, &readfds, NULL, NULL, &tv);
  101. }
  102. LWS_VISIBLE void
  103. lws_cancel_service(struct lws_context *context)
  104. {
  105. struct lws_context_per_thread *pt = &context->pt[0];
  106. int n = context->count_threads;
  107. while (n--) {
  108. WSASetEvent(pt->events[0]);
  109. pt++;
  110. }
  111. }
  112. LWS_VISIBLE void
  113. lws_cancel_service_pt(struct lws *wsi)
  114. {
  115. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  116. WSASetEvent(pt->events[0]);
  117. }
  118. LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
  119. {
  120. lwsl_emit_stderr(level, line);
  121. }
  122. LWS_VISIBLE LWS_EXTERN int
  123. _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
  124. {
  125. struct lws_context_per_thread *pt;
  126. WSANETWORKEVENTS networkevents;
  127. struct lws_pollfd *pfd;
  128. struct lws *wsi;
  129. unsigned int i;
  130. DWORD ev;
  131. int n, m;
  132. /* stay dead once we are dead */
  133. if (context == NULL)
  134. return 1;
  135. pt = &context->pt[tsi];
  136. if (!context->service_tid_detected) {
  137. struct lws _lws;
  138. memset(&_lws, 0, sizeof(_lws));
  139. _lws.context = context;
  140. context->service_tid_detected = context->vhost_list->
  141. protocols[0].callback(&_lws, LWS_CALLBACK_GET_THREAD_ID,
  142. NULL, NULL, 0);
  143. }
  144. context->service_tid = context->service_tid_detected;
  145. if (timeout_ms < 0)
  146. {
  147. if (lws_service_flag_pending(context, tsi)) {
  148. /* any socket with events to service? */
  149. for (n = 0; n < (int)pt->fds_count; n++) {
  150. if (!pt->fds[n].revents)
  151. continue;
  152. m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
  153. if (m < 0)
  154. return -1;
  155. /* if something closed, retry this slot */
  156. if (m)
  157. n--;
  158. }
  159. }
  160. return 0;
  161. }
  162. for (i = 0; i < pt->fds_count; ++i) {
  163. pfd = &pt->fds[i];
  164. if (!(pfd->events & LWS_POLLOUT))
  165. continue;
  166. wsi = wsi_from_fd(context, pfd->fd);
  167. if (wsi->listener)
  168. continue;
  169. if (!wsi || wsi->sock_send_blocking)
  170. continue;
  171. pfd->revents = LWS_POLLOUT;
  172. n = lws_service_fd(context, pfd);
  173. if (n < 0)
  174. return -1;
  175. /* if something closed, retry this slot */
  176. if (n)
  177. i--;
  178. }
  179. /*
  180. * is there anybody with pending stuff that needs service forcing?
  181. */
  182. if (!lws_service_adjust_timeout(context, 1, tsi)) {
  183. /* -1 timeout means just do forced service */
  184. _lws_plat_service_tsi(context, -1, pt->tid);
  185. /* still somebody left who wants forced service? */
  186. if (!lws_service_adjust_timeout(context, 1, pt->tid))
  187. /* yes... come back again quickly */
  188. timeout_ms = 0;
  189. }
  190. ev = WSAWaitForMultipleEvents( 1, pt->events , FALSE, timeout_ms, FALSE);
  191. if (ev == WSA_WAIT_EVENT_0) {
  192. unsigned int eIdx;
  193. WSAResetEvent(pt->events[0]);
  194. for (eIdx = 0; eIdx < pt->fds_count; ++eIdx) {
  195. if (WSAEnumNetworkEvents(pt->fds[eIdx].fd, 0, &networkevents) == SOCKET_ERROR) {
  196. lwsl_err("WSAEnumNetworkEvents() failed with error %d\n", LWS_ERRNO);
  197. return -1;
  198. }
  199. pfd = &pt->fds[eIdx];
  200. pfd->revents = (short)networkevents.lNetworkEvents;
  201. if ((networkevents.lNetworkEvents & FD_CONNECT) &&
  202. networkevents.iErrorCode[FD_CONNECT_BIT] &&
  203. networkevents.iErrorCode[FD_CONNECT_BIT] != LWS_EALREADY &&
  204. networkevents.iErrorCode[FD_CONNECT_BIT] != LWS_EINPROGRESS &&
  205. networkevents.iErrorCode[FD_CONNECT_BIT] != LWS_EWOULDBLOCK &&
  206. networkevents.iErrorCode[FD_CONNECT_BIT] != WSAEINVAL) {
  207. lwsl_debug("Unable to connect errno=%d\n",
  208. networkevents.iErrorCode[FD_CONNECT_BIT]);
  209. pfd->revents = LWS_POLLHUP;
  210. } else
  211. pfd->revents = (short)networkevents.lNetworkEvents;
  212. if (pfd->revents & LWS_POLLOUT) {
  213. wsi = wsi_from_fd(context, pfd->fd);
  214. if (wsi)
  215. wsi->sock_send_blocking = 0;
  216. }
  217. /* if something closed, retry this slot */
  218. if (pfd->revents & LWS_POLLHUP)
  219. --eIdx;
  220. if( pfd->revents != 0 ) {
  221. lws_service_fd_tsi(context, pfd, tsi);
  222. }
  223. }
  224. }
  225. context->service_tid = 0;
  226. if (ev == WSA_WAIT_TIMEOUT) {
  227. lws_service_fd(context, NULL);
  228. }
  229. return 0;;
  230. }
  231. LWS_VISIBLE int
  232. lws_plat_service(struct lws_context *context, int timeout_ms)
  233. {
  234. return _lws_plat_service_tsi(context, timeout_ms, 0);
  235. }
  236. LWS_VISIBLE int
  237. lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd)
  238. {
  239. int optval = 1;
  240. int optlen = sizeof(optval);
  241. u_long optl = 1;
  242. DWORD dwBytesRet;
  243. struct tcp_keepalive alive;
  244. int protonbr;
  245. #ifndef _WIN32_WCE
  246. struct protoent *tcp_proto;
  247. #endif
  248. if (vhost->ka_time) {
  249. /* enable keepalive on this socket */
  250. optval = 1;
  251. if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
  252. (const char *)&optval, optlen) < 0)
  253. return 1;
  254. alive.onoff = TRUE;
  255. alive.keepalivetime = vhost->ka_time;
  256. alive.keepaliveinterval = vhost->ka_interval;
  257. if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
  258. NULL, 0, &dwBytesRet, NULL, NULL))
  259. return 1;
  260. }
  261. /* Disable Nagle */
  262. optval = 1;
  263. #ifndef _WIN32_WCE
  264. tcp_proto = getprotobyname("TCP");
  265. if (!tcp_proto) {
  266. lwsl_err("getprotobyname() failed with error %d\n", LWS_ERRNO);
  267. return 1;
  268. }
  269. protonbr = tcp_proto->p_proto;
  270. #else
  271. protonbr = 6;
  272. #endif
  273. setsockopt(fd, protonbr, TCP_NODELAY, (const char *)&optval, optlen);
  274. /* We are nonblocking... */
  275. ioctlsocket(fd, FIONBIO, &optl);
  276. return 0;
  277. }
  278. LWS_VISIBLE void
  279. lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
  280. {
  281. }
  282. LWS_VISIBLE int
  283. lws_plat_context_early_init(void)
  284. {
  285. WORD wVersionRequested;
  286. WSADATA wsaData;
  287. int err;
  288. /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
  289. wVersionRequested = MAKEWORD(2, 2);
  290. err = WSAStartup(wVersionRequested, &wsaData);
  291. if (!err)
  292. return 0;
  293. /*
  294. * Tell the user that we could not find a usable
  295. * Winsock DLL
  296. */
  297. lwsl_err("WSAStartup failed with error: %d\n", err);
  298. return 1;
  299. }
  300. LWS_VISIBLE void
  301. lws_plat_context_early_destroy(struct lws_context *context)
  302. {
  303. struct lws_context_per_thread *pt = &context->pt[0];
  304. int n = context->count_threads;
  305. while (n--) {
  306. if (pt->events) {
  307. WSACloseEvent(pt->events[0]);
  308. lws_free(pt->events);
  309. }
  310. pt++;
  311. }
  312. }
  313. LWS_VISIBLE void
  314. lws_plat_context_late_destroy(struct lws_context *context)
  315. {
  316. int n;
  317. for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
  318. if (context->fd_hashtable[n].wsi)
  319. lws_free(context->fd_hashtable[n].wsi);
  320. }
  321. WSACleanup();
  322. }
  323. LWS_VISIBLE LWS_EXTERN int
  324. lws_interface_to_sa(int ipv6,
  325. const char *ifname, struct sockaddr_in *addr, size_t addrlen)
  326. {
  327. long long address = inet_addr(ifname);
  328. if (address == INADDR_NONE) {
  329. struct hostent *entry = gethostbyname(ifname);
  330. if (entry)
  331. address = ((struct in_addr *)entry->h_addr_list[0])->s_addr;
  332. }
  333. if (address == INADDR_NONE)
  334. return -1;
  335. addr->sin_addr.s_addr = (unsigned long)address;
  336. return 0;
  337. }
  338. LWS_VISIBLE void
  339. lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
  340. {
  341. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  342. pt->fds[pt->fds_count++].revents = 0;
  343. pt->events[pt->fds_count] = pt->events[0];
  344. WSAEventSelect(wsi->desc.sockfd, pt->events[0],
  345. LWS_POLLIN | LWS_POLLHUP | FD_CONNECT);
  346. }
  347. LWS_VISIBLE void
  348. lws_plat_delete_socket_from_fds(struct lws_context *context,
  349. struct lws *wsi, int m)
  350. {
  351. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  352. pt->events[m + 1] = pt->events[pt->fds_count--];
  353. }
  354. LWS_VISIBLE void
  355. lws_plat_service_periodic(struct lws_context *context)
  356. {
  357. }
  358. LWS_VISIBLE int
  359. lws_plat_check_connection_error(struct lws *wsi)
  360. {
  361. int optVal;
  362. int optLen = sizeof(int);
  363. if (getsockopt(wsi->desc.sockfd, SOL_SOCKET, SO_ERROR,
  364. (char*)&optVal, &optLen) != SOCKET_ERROR && optVal &&
  365. optVal != LWS_EALREADY && optVal != LWS_EINPROGRESS &&
  366. optVal != LWS_EWOULDBLOCK && optVal != WSAEINVAL) {
  367. lwsl_debug("Connect failed SO_ERROR=%d\n", optVal);
  368. return 1;
  369. }
  370. return 0;
  371. }
  372. LWS_VISIBLE int
  373. lws_plat_change_pollfd(struct lws_context *context,
  374. struct lws *wsi, struct lws_pollfd *pfd)
  375. {
  376. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  377. long networkevents = LWS_POLLHUP | FD_CONNECT;
  378. if ((pfd->events & LWS_POLLIN))
  379. networkevents |= LWS_POLLIN;
  380. if ((pfd->events & LWS_POLLOUT))
  381. networkevents |= LWS_POLLOUT;
  382. if (WSAEventSelect(wsi->desc.sockfd,
  383. pt->events[0],
  384. networkevents) != SOCKET_ERROR)
  385. return 0;
  386. lwsl_err("WSAEventSelect() failed with error %d\n", LWS_ERRNO);
  387. return 1;
  388. }
  389. LWS_VISIBLE const char *
  390. lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
  391. {
  392. WCHAR *buffer;
  393. DWORD bufferlen = cnt;
  394. BOOL ok = FALSE;
  395. buffer = lws_malloc(bufferlen * 2);
  396. if (!buffer) {
  397. lwsl_err("Out of memory\n");
  398. return NULL;
  399. }
  400. if (af == AF_INET) {
  401. struct sockaddr_in srcaddr;
  402. bzero(&srcaddr, sizeof(srcaddr));
  403. srcaddr.sin_family = AF_INET;
  404. memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
  405. if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
  406. ok = TRUE;
  407. #ifdef LWS_USE_IPV6
  408. } else if (af == AF_INET6) {
  409. struct sockaddr_in6 srcaddr;
  410. bzero(&srcaddr, sizeof(srcaddr));
  411. srcaddr.sin6_family = AF_INET6;
  412. memcpy(&(srcaddr.sin6_addr), src, sizeof(srcaddr.sin6_addr));
  413. if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
  414. ok = TRUE;
  415. #endif
  416. } else
  417. lwsl_err("Unsupported type\n");
  418. if (!ok) {
  419. int rv = WSAGetLastError();
  420. lwsl_err("WSAAddressToString() : %d\n", rv);
  421. } else {
  422. if (WideCharToMultiByte(CP_ACP, 0, buffer, bufferlen, dst, cnt, 0, NULL) <= 0)
  423. ok = FALSE;
  424. }
  425. lws_free(buffer);
  426. return ok ? dst : NULL;
  427. }
  428. LWS_VISIBLE lws_fop_fd_t
  429. _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
  430. const char *vpath, lws_fop_flags_t *flags)
  431. {
  432. HANDLE ret;
  433. WCHAR buf[MAX_PATH];
  434. lws_fop_fd_t fop_fd;
  435. MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, ARRAY_SIZE(buf));
  436. if (((*flags) & 7) == _O_RDONLY) {
  437. ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
  438. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  439. } else {
  440. lwsl_err("%s: open for write not implemented\n", __func__);
  441. goto bail;
  442. }
  443. if (ret == LWS_INVALID_FILE)
  444. goto bail;
  445. fop_fd = malloc(sizeof(*fop_fd));
  446. if (!fop_fd)
  447. goto bail;
  448. fop_fd->fops = fops;
  449. fop_fd->fd = ret;
  450. fop_fd->filesystem_priv = NULL; /* we don't use it */
  451. fop_fd->flags = *flags;
  452. fop_fd->len = GetFileSize(ret, NULL);
  453. fop_fd->pos = 0;
  454. return fop_fd;
  455. bail:
  456. return NULL;
  457. }
  458. LWS_VISIBLE int
  459. _lws_plat_file_close(lws_fop_fd_t *fop_fd)
  460. {
  461. HANDLE fd = (*fop_fd)->fd;
  462. free(*fop_fd);
  463. *fop_fd = NULL;
  464. CloseHandle((HANDLE)fd);
  465. return 0;
  466. }
  467. LWS_VISIBLE lws_fileofs_t
  468. _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  469. {
  470. return SetFilePointer((HANDLE)fop_fd->fd, offset, NULL, FILE_CURRENT);
  471. }
  472. LWS_VISIBLE int
  473. _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  474. uint8_t *buf, lws_filepos_t len)
  475. {
  476. DWORD _amount;
  477. if (!ReadFile((HANDLE)fop_fd->fd, buf, (DWORD)len, &_amount, NULL)) {
  478. *amount = 0;
  479. return 1;
  480. }
  481. fop_fd->pos += _amount;
  482. *amount = (unsigned long)_amount;
  483. return 0;
  484. }
  485. LWS_VISIBLE int
  486. _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  487. uint8_t* buf, lws_filepos_t len)
  488. {
  489. (void)fop_fd;
  490. (void)amount;
  491. (void)buf;
  492. (void)len;
  493. fop_fd->pos += len;
  494. lwsl_err("%s: not implemented yet on this platform\n", __func__);
  495. return -1;
  496. }
  497. LWS_VISIBLE int
  498. lws_plat_init(struct lws_context *context,
  499. struct lws_context_creation_info *info)
  500. {
  501. struct lws_context_per_thread *pt = &context->pt[0];
  502. int i, n = context->count_threads;
  503. for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
  504. context->fd_hashtable[i].wsi =
  505. lws_zalloc(sizeof(struct lws*) * context->max_fds);
  506. if (!context->fd_hashtable[i].wsi)
  507. return -1;
  508. }
  509. while (n--) {
  510. pt->events = lws_malloc(sizeof(WSAEVENT) *
  511. (context->fd_limit_per_thread + 1));
  512. if (pt->events == NULL) {
  513. lwsl_err("Unable to allocate events array for %d connections\n",
  514. context->fd_limit_per_thread + 1);
  515. return 1;
  516. }
  517. pt->fds_count = 0;
  518. pt->events[0] = WSACreateEvent();
  519. pt++;
  520. }
  521. context->fd_random = 0;
  522. #ifdef LWS_WITH_PLUGINS
  523. if (info->plugin_dirs)
  524. lws_plat_plugins_init(context, info->plugin_dirs);
  525. #endif
  526. return 0;
  527. }
  528. int kill(int pid, int sig)
  529. {
  530. lwsl_err("Sorry Windows doesn't support kill().");
  531. exit(0);
  532. }
  533. int fork(void)
  534. {
  535. lwsl_err("Sorry Windows doesn't support fork().");
  536. exit(0);
  537. }