lws-plat-win.c 14 KB

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