luat_libtcpip_win32.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "luat_base.h"
  2. #include "string.h"
  3. // #define LUAT_USE_LIBTCPIP_WIN32
  4. #ifdef LUAT_USE_LIBTCPIP_WIN32
  5. #include "time.h"
  6. #include "luat_libtcpip.h"
  7. #include <unistd.h>
  8. #include "windows.h"
  9. #include <WinSock2.h>
  10. #define LUAT_LOG_TAG "win32"
  11. #include "luat_log.h"
  12. static int luat_libtcpip_socket_posix(int domain, int type, int protocol) {
  13. LLOGD("create socket %d %d %d", domain, type, protocol);
  14. return socket(domain, type, protocol);
  15. }
  16. static int luat_libtcpip_send_posix(int s, const void *data, size_t size, int flags) {
  17. return send(s, data, size, flags);
  18. }
  19. static int luat_libtcpip_recv_posix(int s, void *mem, size_t len, int flags) {
  20. return recv(s, mem, len, flags);
  21. }
  22. static int luat_libtcpip_recv_timeout_posix(int s, void *mem, size_t len, int flags, int timeout) {
  23. int ret;
  24. struct timeval tv;
  25. fd_set read_fds;
  26. int fd = s;
  27. FD_ZERO( &read_fds );
  28. FD_SET( fd, &read_fds );
  29. tv.tv_sec = timeout / 1000;
  30. tv.tv_usec = ( timeout % 1000 ) * 1000;
  31. ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv );
  32. /* Zero fds ready means we timed out */
  33. if( ret == 0 )
  34. return -1;
  35. if( ret < 0 )
  36. {
  37. return ret;
  38. }
  39. /* This call will not block */
  40. return recv( fd, mem, len, flags);
  41. }
  42. static int luat_libtcpip_select_posix(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  43. struct timeval *timeout) {
  44. return select(maxfdp1, readset, writeset, exceptset, timeout);
  45. }
  46. static int luat_libtcpip_close_posix(int s) {
  47. // return close(s);
  48. return closesocket(s);
  49. }
  50. static int luat_libtcpip_connect_posix(int s, const char *hostname, uint16_t port) {
  51. struct sockaddr_in socket_address;
  52. struct hostent *hp;
  53. char tmp[32];
  54. hp = gethostbyname(hostname);
  55. if (hp == NULL )
  56. {
  57. LLOGW("DNS Query Fail %s", hostname);
  58. return -2;
  59. }
  60. else {
  61. inet_ntop(hp->h_addrtype, hp->h_addr_list[0], tmp, sizeof(tmp));
  62. LLOGW("DNS Query OK %s %s", hostname, tmp);
  63. }
  64. memset(&socket_address, 0, sizeof(struct sockaddr_in));
  65. socket_address.sin_family = AF_INET;
  66. socket_address.sin_port = htons(port);
  67. memcpy(&(socket_address.sin_addr), hp->h_addr, hp->h_length);
  68. LLOGD("socket fd %d", s);
  69. LLOGD("connect %s %s %d", hostname, tmp, port);
  70. int ret = connect(s, &socket_address, sizeof(socket_address));
  71. if (ret == -1) {
  72. LLOGD("connect errno %d", errno);
  73. }
  74. //LLOGD("connect %s %s %d ret %d", hostname, tmp, port, ret);
  75. return ret;
  76. }
  77. static struct hostent* luat_libtcpip_gethostbyname_posix(const char* name) {
  78. return gethostbyname(name);
  79. }
  80. static int luat_libtcpip_setsockopt_posix(int s, int level, int optname, const void *optval, uint32_t optlen) {
  81. return setsockopt(s, level, optname, optval, (socklen_t)optlen);
  82. }
  83. luat_libtcpip_opts_t luat_libtcpip_posix = {
  84. ._socket = luat_libtcpip_socket_posix,
  85. ._close = luat_libtcpip_close_posix,
  86. ._connect = luat_libtcpip_connect_posix,
  87. ._gethostbyname = luat_libtcpip_gethostbyname_posix,
  88. ._recv = luat_libtcpip_recv_posix,
  89. ._recv_timeout = luat_libtcpip_recv_timeout_posix,
  90. // ._select = luat_libtcpip_select_posix,
  91. ._send = luat_libtcpip_send_posix,
  92. ._setsockopt = luat_libtcpip_setsockopt_posix
  93. };
  94. #endif