rtt_winsock.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-05-30 xiangxistu first version
  9. */
  10. #define DNS_MAX_NAME_LENGTH 256
  11. /*
  12. * 1. Define the structure to avoid conflict with winsock's structure.
  13. * 2. And the same time, in the "af_inet_winsock.c" shouldn't include header files aboult "sal",
  14. * if include header files aboult "sal", the complier in the vs2012 will give me "structure redefined error".
  15. *
  16. * So, i define the same structure with "sal" but not include header files aboult "sal".
  17. * The same sturcture means the same memory in the system. I can offer wonderful compatibility with "sal" ,"winsock", "lwip" and so on.
  18. *
  19. *
  20. * Aross the way, "WinSock2.h" only be included in the "af_inet_winsock.c", the more software packages aboult network can
  21. * work that useless modification is required for "winsock".
  22. *
  23. */
  24. typedef uint32_t sal_type_socklen_t;
  25. typedef uint16_t in_port_t;
  26. typedef uint8_t sa_family_t;
  27. typedef uint32_t in_addr_t;
  28. typedef struct sal_type_ip4_addr
  29. {
  30. uint32_t addr;
  31. } sal_type_ip4_addr_t;
  32. typedef sal_type_ip4_addr_t sal_type_ip_addr_t;
  33. struct sal_type_sockaddr
  34. {
  35. uint8_t sa_len;
  36. sa_family_t sa_family;
  37. char sa_data[14];
  38. };
  39. struct sal_type_in_addr
  40. {
  41. in_addr_t sal_type_s_addr;
  42. };
  43. struct sal_type_sockaddr_in
  44. {
  45. uint8_t sin_len;
  46. sa_family_t sin_family;
  47. in_port_t sin_port;
  48. struct sal_type_in_addr sin_addr;
  49. #define SIN_ZERO_LEN 8
  50. char sin_zero[SIN_ZERO_LEN];
  51. };
  52. struct sal_type_sockaddr_storage
  53. {
  54. uint8_t s2_len;
  55. sa_family_t ss_family;
  56. char s2_data1[2];
  57. uint32_t s2_data2[3];
  58. #if NETDEV_IPV6
  59. uint32_t s2_data3[3];
  60. #endif /* NETDEV_IPV6 */
  61. };
  62. struct sal_type_addrinfo {
  63. int ai_flags; /* Input flags. */
  64. int ai_family; /* Address family of socket. */
  65. int ai_socktype; /* Socket type. */
  66. int ai_protocol; /* Protocol of socket. */
  67. socklen_t ai_addrlen; /* Length of socket address. */
  68. struct sal_type_sockaddr* ai_addr; /* Socket address of socket. */
  69. char* ai_canonname; /* Canonical name of service location. */
  70. struct sal_type_addrinfo* ai_next; /* Pointer to next in list. */
  71. };
  72. struct sal_type_hostent {
  73. char* h_name; /* Official name of the host. */
  74. char** h_aliases; /* A pointer to an array of pointers to alternative host names,
  75. terminated by a null pointer. */
  76. int h_addrtype; /* Address type. */
  77. int h_length; /* The length, in bytes, of the address. */
  78. char** h_addr_list; /* A pointer to an array of pointers to network addresses (in
  79. network byte order) for the host, terminated by a null pointer. */
  80. #define h_addr h_addr_list[0] /* for backward compatibility */
  81. };
  82. /* sal_socket_ops */
  83. int win_socket(int domain, int type, int protocol);
  84. int win_closesocket(int s);
  85. int win_bind(int s, const struct sal_type_sockaddr* name, sal_type_socklen_t namelen);
  86. int win_listen(int s, int backlog);
  87. int win_connect(int s, const struct sal_type_sockaddr* name, sal_type_socklen_t namelen);
  88. int win_accept(int s, struct sal_type_sockaddr* addr, sal_type_socklen_t* addrlen);
  89. int win_sendto(int s, const void* data, size_t size, int flags, const struct sal_type_sockaddr* to, sal_type_socklen_t tolen);
  90. int win_recvfrom(int s, void* mem, size_t len, int flags, struct sal_type_sockaddr* from, sal_type_socklen_t* fromlen);
  91. int win_getsockopt(int s, int level, int optname, void* optval, sal_type_socklen_t* optlen);
  92. int win_setsockopt(int s, int level, int optname, const void* optval, sal_type_socklen_t optlen);
  93. int win_shutdown(int s, int how);
  94. int win_getpeername(int s, struct sal_type_sockaddr* name, sal_type_socklen_t* namelen);
  95. int win_getsockname(int s, struct sal_type_sockaddr* name, sal_type_socklen_t* namelen);
  96. int win_ioctlsocket(int s, long cmd, void* arg);
  97. #ifdef SAL_USING_POSIX
  98. int inet_poll(struct dfs_file* file, struct rt_pollreq* req);
  99. #endif /* SAL_USING_POSIX */
  100. /* sal_netdb_ops */
  101. struct sal_type_hostent* win_gethostbyname(const char* name);
  102. int win_getaddrinfo(const char* nodename, const char* servname, const struct sal_type_addrinfo* hints, struct sal_type_addrinfo** res);
  103. void win_freeaddrinfo(struct sal_type_addrinfo* ai);