windows-sockets.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  25. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  26. #endif
  27. #include "private-lib-core.h"
  28. #if defined(LWS_WITH_MBEDTLS)
  29. #if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
  30. #include "mbedtls/net_sockets.h"
  31. #else
  32. #include "mbedtls/net.h"
  33. #endif
  34. #endif
  35. int
  36. lws_send_pipe_choked(struct lws *wsi)
  37. { struct lws *wsi_eff;
  38. #if defined(LWS_WITH_HTTP2)
  39. wsi_eff = lws_get_network_wsi(wsi);
  40. #else
  41. wsi_eff = wsi;
  42. #endif
  43. /* the fact we checked implies we avoided back-to-back writes */
  44. wsi_eff->could_have_pending = 0;
  45. /* treat the fact we got a truncated send pending as if we're choked */
  46. if (lws_has_buffered_out(wsi_eff)
  47. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  48. ||wsi->http.comp_ctx.buflist_comp ||
  49. wsi->http.comp_ctx.may_have_more
  50. #endif
  51. )
  52. return 1;
  53. return (int)wsi_eff->sock_send_blocking;
  54. }
  55. int
  56. lws_poll_listen_fd(struct lws_pollfd *fd)
  57. {
  58. fd_set readfds;
  59. struct timeval tv = { 0, 0 };
  60. assert((fd->events & LWS_POLLIN) == LWS_POLLIN);
  61. FD_ZERO(&readfds);
  62. FD_SET(fd->fd, &readfds);
  63. return select(((int)fd->fd) + 1, &readfds, NULL, NULL, &tv);
  64. }
  65. int
  66. lws_plat_set_nonblocking(lws_sockfd_type fd)
  67. {
  68. u_long optl = 1;
  69. int result = !!ioctlsocket(fd, FIONBIO, &optl);
  70. if (result)
  71. {
  72. int error = LWS_ERRNO;
  73. lwsl_err("ioctlsocket FIONBIO 1 failed with error %d\n", error);
  74. }
  75. return result;
  76. }
  77. int
  78. lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd,
  79. int unix_skt)
  80. {
  81. int optval = 1;
  82. int optlen = sizeof(optval);
  83. DWORD dwBytesRet;
  84. struct tcp_keepalive alive;
  85. int protonbr;
  86. #ifndef _WIN32_WCE
  87. struct protoent *tcp_proto;
  88. #endif
  89. if (vhost->ka_time) {
  90. /* enable keepalive on this socket */
  91. optval = 1;
  92. if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
  93. (const char *)&optval, optlen) < 0) {
  94. int error = LWS_ERRNO;
  95. lwsl_err("setsockopt SO_KEEPALIVE 1 failed with error %d\n", error);
  96. return 1;
  97. }
  98. alive.onoff = TRUE;
  99. alive.keepalivetime = vhost->ka_time * 1000;
  100. alive.keepaliveinterval = vhost->ka_interval * 1000;
  101. if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
  102. NULL, 0, &dwBytesRet, NULL, NULL)) {
  103. int error = LWS_ERRNO;
  104. lwsl_err("WSAIoctl SIO_KEEPALIVE_VALS 1 %lu %lu failed with error %d\n", alive.keepalivetime, alive.keepaliveinterval, error);
  105. return 1;
  106. }
  107. }
  108. /* Disable Nagle */
  109. optval = 1;
  110. #ifndef _WIN32_WCE
  111. tcp_proto = getprotobyname("TCP");
  112. if (!tcp_proto) {
  113. int error = LWS_ERRNO;
  114. lwsl_warn("getprotobyname(\"TCP\") failed with error, falling back to 6 %d\n", error);
  115. protonbr = 6; /* IPPROTO_TCP */
  116. } else
  117. protonbr = tcp_proto->p_proto;
  118. #else
  119. protonbr = 6;
  120. #endif
  121. if (setsockopt(fd, protonbr, TCP_NODELAY, (const char *)&optval, optlen) ) {
  122. int error = LWS_ERRNO;
  123. lwsl_warn("setsockopt TCP_NODELAY 1 failed with error %d\n", error);
  124. }
  125. return lws_plat_set_nonblocking(fd);
  126. }
  127. int
  128. lws_interface_to_sa(int ipv6,
  129. const char *ifname, struct sockaddr_in *addr, size_t addrlen)
  130. {
  131. long long address;
  132. #ifdef LWS_WITH_IPV6
  133. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
  134. if (ipv6) {
  135. if (lws_plat_inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1) {
  136. return LWS_ITOSA_USABLE;
  137. }
  138. }
  139. #endif
  140. address = inet_addr(ifname);
  141. if (address == INADDR_NONE) {
  142. struct hostent *entry = gethostbyname(ifname);
  143. if (entry)
  144. address = ((struct in_addr *)entry->h_addr_list[0])->s_addr;
  145. }
  146. if (address == INADDR_NONE)
  147. return LWS_ITOSA_NOT_EXIST;
  148. addr->sin_addr.s_addr = (unsigned long)(lws_intptr_t)address;
  149. return LWS_ITOSA_USABLE;
  150. }
  151. void
  152. lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
  153. {
  154. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  155. if (wsi->udp) {
  156. lwsl_info("%s: UDP\n", __func__);
  157. pt->fds[pt->fds_count].events |= LWS_POLLIN;
  158. }
  159. pt->fds[pt->fds_count++].revents = 0;
  160. lws_plat_change_pollfd(context, wsi, &pt->fds[pt->fds_count - 1]);
  161. }
  162. void
  163. lws_plat_delete_socket_from_fds(struct lws_context *context,
  164. struct lws *wsi, int m)
  165. {
  166. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  167. pt->fds_count--;
  168. }
  169. int
  170. lws_plat_check_connection_error(struct lws *wsi)
  171. {
  172. int optVal;
  173. int optLen = sizeof(int);
  174. if (getsockopt(wsi->desc.sockfd, SOL_SOCKET, SO_ERROR,
  175. (char*)&optVal, &optLen) != SOCKET_ERROR && optVal &&
  176. optVal != LWS_EALREADY && optVal != LWS_EINPROGRESS &&
  177. optVal != LWS_EWOULDBLOCK && optVal != WSAEINVAL) {
  178. lwsl_debug("Connect failed SO_ERROR=%d\n", optVal);
  179. return 1;
  180. }
  181. return 0;
  182. }
  183. int
  184. lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
  185. struct lws_pollfd *pfd)
  186. {
  187. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  188. long e = LWS_POLLHUP | FD_CONNECT | FD_ACCEPT | FD_CLOSE | FD_WRITE;
  189. /*
  190. * On windows, FD_WRITE is only coming to indicate that we are writable
  191. * again after being choked. So we must always listen for it.
  192. */
  193. if (pfd->events & LWS_POLLIN)
  194. e |= FD_READ;
  195. if (WSAEventSelect(wsi->desc.sockfd, pt->events[(pfd - pt->fds) + 1], e)) {
  196. lwsl_err("WSAEventSelect() failed with error %d\n", LWS_ERRNO);
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. const char *
  202. lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
  203. {
  204. WCHAR *buffer;
  205. size_t bufferlen = (size_t)cnt;
  206. BOOL ok = FALSE;
  207. buffer = lws_malloc(bufferlen * 2, "inet_ntop");
  208. if (!buffer) {
  209. lwsl_err("Out of memory\n");
  210. return NULL;
  211. }
  212. if (af == AF_INET) {
  213. struct sockaddr_in srcaddr;
  214. memset(&srcaddr, 0, sizeof(srcaddr));
  215. srcaddr.sin_family = AF_INET;
  216. memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
  217. if (!WSAAddressToStringW((struct sockaddr*)&srcaddr,
  218. sizeof(srcaddr), 0, buffer,
  219. (LPDWORD)&bufferlen))
  220. ok = TRUE;
  221. #ifdef LWS_WITH_IPV6
  222. } else if (af == AF_INET6) {
  223. struct sockaddr_in6 srcaddr;
  224. memset(&srcaddr, 0, sizeof(srcaddr));
  225. srcaddr.sin6_family = AF_INET6;
  226. memcpy(&(srcaddr.sin6_addr), src, sizeof(srcaddr.sin6_addr));
  227. if (!WSAAddressToStringW((struct sockaddr*)&srcaddr,
  228. sizeof(srcaddr), 0, buffer,
  229. (LPDWORD)&bufferlen))
  230. ok = TRUE;
  231. #endif
  232. } else
  233. lwsl_err("Unsupported type\n");
  234. if (!ok) {
  235. int rv = WSAGetLastError();
  236. lwsl_err("WSAAddressToString() : %d\n", rv);
  237. } else {
  238. if (WideCharToMultiByte(CP_ACP, 0, buffer, (int)bufferlen, dst,
  239. cnt, 0, NULL) <= 0)
  240. ok = FALSE;
  241. }
  242. lws_free(buffer);
  243. return ok ? dst : NULL;
  244. }
  245. int
  246. lws_plat_inet_pton(int af, const char *src, void *dst)
  247. {
  248. WCHAR *buffer;
  249. size_t bufferlen = strlen(src) + 1;
  250. BOOL ok = FALSE;
  251. buffer = lws_malloc(bufferlen * 2, "inet_pton");
  252. if (!buffer) {
  253. lwsl_err("Out of memory\n");
  254. return -1;
  255. }
  256. if (MultiByteToWideChar(CP_ACP, 0, src, (int)bufferlen, buffer,
  257. (int)bufferlen) <= 0) {
  258. lwsl_err("Failed to convert multi byte to wide char\n");
  259. lws_free(buffer);
  260. return -1;
  261. }
  262. if (af == AF_INET) {
  263. struct sockaddr_in dstaddr;
  264. int dstaddrlen = sizeof(dstaddr);
  265. memset(&dstaddr, 0, sizeof(dstaddr));
  266. dstaddr.sin_family = AF_INET;
  267. if (!WSAStringToAddressW(buffer, af, 0, (struct sockaddr *) &dstaddr, &dstaddrlen)) {
  268. ok = TRUE;
  269. memcpy(dst, &dstaddr.sin_addr, sizeof(dstaddr.sin_addr));
  270. }
  271. #ifdef LWS_WITH_IPV6
  272. } else if (af == AF_INET6) {
  273. struct sockaddr_in6 dstaddr;
  274. int dstaddrlen = sizeof(dstaddr);
  275. memset(&dstaddr, 0, sizeof(dstaddr));
  276. dstaddr.sin6_family = AF_INET6;
  277. if (!WSAStringToAddressW(buffer, af, 0, (struct sockaddr *) &dstaddr, &dstaddrlen)) {
  278. ok = TRUE;
  279. memcpy(dst, &dstaddr.sin6_addr, sizeof(dstaddr.sin6_addr));
  280. }
  281. #endif
  282. } else
  283. lwsl_err("Unsupported type\n");
  284. if (!ok) {
  285. int rv = WSAGetLastError();
  286. lwsl_err("WSAAddressToString() : %d\n", rv);
  287. }
  288. lws_free(buffer);
  289. return ok ? 1 : -1;
  290. }
  291. int
  292. lws_plat_ifname_to_hwaddr(int fd, const char *ifname, uint8_t *hwaddr, int len)
  293. {
  294. lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
  295. return -1;
  296. }
  297. int
  298. lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, int canned_len,
  299. int n, int fd, const char *iface)
  300. {
  301. lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
  302. return -1;
  303. }
  304. int
  305. lws_plat_if_up(const char *ifname, int fd, int up)
  306. {
  307. lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
  308. return -1;
  309. }
  310. int
  311. lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname)
  312. {
  313. lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
  314. return -1;
  315. }
  316. int
  317. lws_plat_ifconfig_ip(const char *ifname, int fd, uint8_t *ip, uint8_t *mask_ip,
  318. uint8_t *gateway_ip)
  319. {
  320. lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
  321. return -1;
  322. }
  323. #if defined(LWS_WITH_MBEDTLS)
  324. int
  325. lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
  326. {
  327. int fd = ((mbedtls_net_context *) ctx)->fd;
  328. int ret;
  329. if (fd < 0)
  330. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  331. ret = write(fd, buf, len);
  332. if (ret >= 0)
  333. return ret;
  334. if (errno == EAGAIN || errno == EWOULDBLOCK)
  335. return MBEDTLS_ERR_SSL_WANT_WRITE;
  336. if (WSAGetLastError() == WSAECONNRESET )
  337. return( MBEDTLS_ERR_NET_CONN_RESET );
  338. return MBEDTLS_ERR_NET_SEND_FAILED;
  339. }
  340. int
  341. lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
  342. {
  343. int fd = ((mbedtls_net_context *) ctx)->fd;
  344. int ret;
  345. if (fd < 0)
  346. return MBEDTLS_ERR_NET_INVALID_CONTEXT;
  347. ret = (int)read(fd, buf, len);
  348. if (ret >= 0)
  349. return ret;
  350. if (errno == EAGAIN || errno == EWOULDBLOCK)
  351. return MBEDTLS_ERR_SSL_WANT_READ;
  352. if (WSAGetLastError() == WSAECONNRESET)
  353. return MBEDTLS_ERR_NET_CONN_RESET;
  354. return MBEDTLS_ERR_NET_RECV_FAILED;
  355. }
  356. #endif