sal_winsock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-05-26 xiangxistu first version
  9. */
  10. /* Do NOT include sys/time.h in this file */
  11. #include <WinSock2.h>
  12. #include <WS2tcpip.h>
  13. #include <rtthread.h>
  14. #include <stdint.h>
  15. #define DBG_TAG "sal.winsock"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. #include "rtt_winsock.h"
  19. static struct sal_type_hostent sal_hostent;
  20. static sal_type_ip_addr_t hostent_addr = { 0 };
  21. static sal_type_ip_addr_t* hostent_addr_p = RT_NULL;
  22. /* Important, must reserve */
  23. #pragma comment( lib, "ws2_32.lib" )
  24. /*
  25. * reserve for select / poll
  26. *
  27. * #define WINDOWS_STACK_SIZE 2048
  28. * #define WINDOWS_PRIORITY 2048
  29. * #define WINDOWS_TICK 10
  30. *
  31. * struct poll_thread_list
  32. * {
  33. * int socket;
  34. * rt_slist_t slist;
  35. * };
  36. * static rt_slist_t poll_pending_list = RT_SLIST_OBJECT_INIT(poll_pending_list);
  37. *
  38. **/
  39. #define WINDOWS_PERVIOS_COMAMND \
  40. do \
  41. { \
  42. int iResult; \
  43. WSADATA wsa_data; \
  44. \
  45. iResult = WSAStartup(MAKEWORD(2, 2), &wsa_data); \
  46. if (iResult != 0) \
  47. { \
  48. LOG_E("WSAStartup failed: %d\n", iResult); \
  49. return -RT_ERROR; \
  50. } \
  51. }while(0)
  52. static void data_sync_sockaddr(struct sal_type_sockaddr* sal, struct sockaddr** window)
  53. {
  54. /*
  55. * ws2def.h
  56. * typedef struct sockaddr {
  57. *
  58. * #if (_WIN32_WINNT < 0x0600)
  59. * u_short sa_family;
  60. * #else
  61. * ADDRESS_FAMILY sa_family; // Address family.
  62. * #endif //(_WIN32_WINNT < 0x0600)
  63. *
  64. * CHAR sa_data[14]; // Up to 14 bytes of direct address.
  65. * } SOCKADDR, *PSOCKADDR, FAR *LPSOCKADDR;
  66. *
  67. ***********************************
  68. * sal_socket.h
  69. * struct sockaddr
  70. * {
  71. * uint8_t sa_len;
  72. * sa_family_t sa_family;
  73. * char sa_data[14];
  74. * };
  75. */
  76. ADDRESS_FAMILY family = 0;
  77. family = sal->sa_family;
  78. *window = sal;
  79. (* window)->sa_family = family;
  80. }
  81. int win_socket(int domain, int type, int protocol)
  82. {
  83. WINDOWS_PERVIOS_COMAMND;
  84. return (int)socket(domain, type, protocol);
  85. }
  86. int win_closesocket(int s)
  87. {
  88. return (int)closesocket(s);
  89. }
  90. int win_bind(int s, const struct sal_type_sockaddr *name, sal_type_socklen_t namelen)
  91. {
  92. return (int)bind(s, name, namelen);
  93. }
  94. int win_listen(int s, int backlog)
  95. {
  96. return (int)listen(s, backlog);
  97. }
  98. int win_connect(int s, const struct sal_type_sockaddr *name, sal_type_socklen_t namelen)
  99. {
  100. struct sockaddr *windows_addr = RT_NULL;
  101. data_sync_sockaddr(name, &windows_addr);
  102. return (int)connect(s, windows_addr, namelen);
  103. }
  104. int win_accept(int s, struct sal_type_sockaddr *addr, sal_type_socklen_t *addrlen)
  105. {
  106. WINDOWS_PERVIOS_COMAMND;
  107. return (int)accept(s, addr, addrlen);
  108. }
  109. int win_sendto(int s, const void *data, size_t size, int flags, const struct sal_type_sockaddr *to, sal_type_socklen_t tolen)
  110. {
  111. return (int)sendto(s, data, size, flags, to, tolen);
  112. }
  113. int win_recvfrom(int s, void *mem, size_t len, int flags, struct sal_type_sockaddr *from, sal_type_socklen_t *fromlen)
  114. {
  115. return (int)recvfrom(s, mem, len, flags, from, fromlen);
  116. }
  117. int win_getsockopt(int s, int level, int optname, void *optval, sal_type_socklen_t *optlen)
  118. {
  119. return (int)getsockopt(s, level, optname, optval, optlen);
  120. }
  121. int win_setsockopt(int s, int level, int optname, const void *optval, sal_type_socklen_t optlen)
  122. {
  123. return (int)setsockopt(s, level, optname, optval, optlen);
  124. }
  125. int win_shutdown(int s, int how)
  126. {
  127. return (int)shutdown(s, how);
  128. }
  129. int win_getpeername(int s, struct sal_type_sockaddr *name, sal_type_socklen_t *namelen)
  130. {
  131. return (int)getpeername(s, name, namelen);
  132. }
  133. int win_getsockname(int s, struct sal_type_sockaddr *name, sal_type_socklen_t *namelen)
  134. {
  135. return (int)getsockname(s, name, namelen);
  136. }
  137. int win_ioctlsocket(int s, long cmd, void *arg)
  138. {
  139. return (int)ioctlsocket(s, cmd, arg);
  140. }
  141. #ifdef SAL_USING_POSIX
  142. /* use windows poll, but not wait */
  143. int inet_poll(struct dfs_fd* file, struct rt_pollreq* req)
  144. {
  145. /*WSAPOLLFD winpoll;
  146. struct sal_socket* sal_sock;
  147. int mask = 0;
  148. int poll_event = 0;
  149. sal_sock = sal_get_socket((int)file->data);
  150. if (!sal_sock)
  151. {
  152. return -1;
  153. }
  154. winpoll.fd = sal_sock->socket;
  155. poll_event &= POLLERR | POLLHUP;
  156. winpoll.events = req->_key & (~poll_event);
  157. poll_event = 0;
  158. mask = WSAPoll(&winpoll, 1, 0);
  159. if (mask == 0)
  160. return 0;
  161. return winpoll.revents;*/
  162. }
  163. #endif /* SAL_USING_POSIX */
  164. struct sal_type_hostent* win_gethostbyname(const char* name)
  165. {
  166. struct hostent *windows_hostent;
  167. char** h_addr_list;
  168. WINDOWS_PERVIOS_COMAMND;
  169. windows_hostent = gethostbyname(name);
  170. sal_hostent.h_name = windows_hostent->h_name;
  171. sal_hostent.h_aliases = windows_hostent->h_aliases;
  172. sal_hostent.h_addrtype = windows_hostent->h_addrtype;
  173. sal_hostent.h_length = windows_hostent->h_length;
  174. sal_hostent.h_addr_list = windows_hostent->h_addr_list;
  175. hostent_addr.addr = ((struct in_addr*)windows_hostent->h_addr)->s_addr;
  176. hostent_addr_p = &hostent_addr;
  177. sal_hostent.h_addr_list = &hostent_addr_p;
  178. return &sal_hostent;
  179. }
  180. int win_getaddrinfo(const char* nodename, const char* servname, const struct sal_type_addrinfo* hints, struct sal_type_addrinfo** res)
  181. {
  182. int port_nr = 0;
  183. sal_type_ip_addr_t addr = { 0 };
  184. struct sal_type_addrinfo* ai;
  185. struct sal_type_sockaddr_storage* sa;
  186. size_t total_size = 0;
  187. size_t namelen = 0;
  188. char* change_point;
  189. if (res == RT_NULL)
  190. return EAI_FAIL;
  191. *res = RT_NULL;
  192. if ((nodename == RT_NULL) && (servname == RT_NULL))
  193. return EAI_NONAME;
  194. if ((hints != RT_NULL)
  195. && (hints->ai_family != AF_INET && hints->ai_family != AF_UNSPEC))
  196. return EAI_FAMILY;
  197. if (servname != RT_NULL)
  198. {
  199. /* service name specified: convert to port number */
  200. port_nr = atoi(servname);
  201. if ((port_nr <= 0) || (port_nr > 0xffff))
  202. {
  203. return EAI_SERVICE;
  204. }
  205. }
  206. if (nodename != RT_NULL)
  207. {
  208. WINDOWS_PERVIOS_COMAMND;
  209. struct hostent* hostent = gethostbyname(nodename);
  210. if (hostent == RT_NULL)
  211. return EAI_FAIL;
  212. addr.addr = ((struct in_addr*)hostent->h_addr)->s_addr;
  213. }
  214. total_size = sizeof(struct sal_type_addrinfo) + sizeof(struct sal_type_sockaddr_storage);
  215. if (nodename != RT_NULL)
  216. {
  217. namelen = strlen(nodename);
  218. if (namelen > DNS_MAX_NAME_LENGTH)
  219. {
  220. /* invalid name length */
  221. return EAI_FAIL;
  222. }
  223. RT_ASSERT(total_size + namelen + 1 > total_size);
  224. total_size += namelen + 1;
  225. }
  226. RT_ASSERT(total_size <= sizeof(struct sal_type_addrinfo) + sizeof(struct sal_type_sockaddr_storage) + DNS_MAX_NAME_LENGTH + 1);
  227. ai = (struct sal_type_addrinfo*)rt_malloc(total_size);
  228. if (ai == RT_NULL)
  229. {
  230. return EAI_MEMORY;
  231. }
  232. rt_memset(ai, 0, total_size);
  233. /* cast through void* to get rid of alignment warnings */
  234. sa = (struct sal_type_sockaddr_storage*)(void*)((uint8_t*)ai + sizeof(struct sal_type_addrinfo));
  235. struct sal_type_sockaddr_in* sa4 = (struct sal_type_sockaddr_in*)sa;
  236. sa4->sin_family = AF_INET;
  237. sa4->sin_addr.sal_type_s_addr = addr.addr;
  238. sa4->sin_port = htons((uint16_t)port_nr);
  239. /* set up addrinfo */
  240. if (hints != RT_NULL)
  241. {
  242. /* copy socktype & protocol from hints if specified */
  243. ai->ai_socktype = hints->ai_socktype;
  244. ai->ai_protocol = hints->ai_protocol;
  245. }
  246. if (nodename != RT_NULL)
  247. {
  248. /* copy nodename to canonname if specified */
  249. ai->ai_canonname = ((char*)ai + sizeof(struct sal_type_addrinfo) + sizeof(struct sal_type_sockaddr_storage));
  250. rt_memcpy(ai->ai_canonname, nodename, namelen);
  251. ai->ai_canonname[namelen] = 0;
  252. }
  253. ai->ai_addrlen = sizeof(struct sal_type_sockaddr_storage);
  254. ai->ai_addr = (struct sal_type_sockaddr*)sa;
  255. ai->ai_family = sa4->sin_family;
  256. *res = ai;
  257. return 0;
  258. }
  259. void win_freeaddrinfo(struct sal_type_addrinfo* ai)
  260. {
  261. struct sal_type_addrinfo* next;
  262. while (ai != NULL)
  263. {
  264. next = ai->ai_next;
  265. rt_free(ai);
  266. ai = next;
  267. }
  268. }