sal_winsock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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_file* file, struct rt_pollreq* req)
  144. {
  145. /*
  146. WSAPOLLFD winpoll;
  147. struct sal_socket* sal_sock;
  148. int mask = 0;
  149. int poll_event = 0;
  150. sal_sock = sal_get_socket((int)file->data);
  151. if (!sal_sock)
  152. {
  153. return -1;
  154. }
  155. winpoll.fd = sal_sock->socket;
  156. poll_event &= POLLERR | POLLHUP;
  157. winpoll.events = req->_key & (~poll_event);
  158. poll_event = 0;
  159. mask = WSAPoll(&winpoll, 1, 0);
  160. if (mask == 0)
  161. return 0;
  162. return winpoll.revents;
  163. */
  164. return 0;
  165. }
  166. #endif /* SAL_USING_POSIX */
  167. struct sal_type_hostent* win_gethostbyname(const char* name)
  168. {
  169. struct hostent *windows_hostent;
  170. char** h_addr_list;
  171. WINDOWS_PERVIOS_COMAMND;
  172. windows_hostent = gethostbyname(name);
  173. sal_hostent.h_name = windows_hostent->h_name;
  174. sal_hostent.h_aliases = windows_hostent->h_aliases;
  175. sal_hostent.h_addrtype = windows_hostent->h_addrtype;
  176. sal_hostent.h_length = windows_hostent->h_length;
  177. sal_hostent.h_addr_list = windows_hostent->h_addr_list;
  178. hostent_addr.addr = ((struct in_addr*)windows_hostent->h_addr)->s_addr;
  179. hostent_addr_p = &hostent_addr;
  180. sal_hostent.h_addr_list = &hostent_addr_p;
  181. return &sal_hostent;
  182. }
  183. int win_getaddrinfo(const char* nodename, const char* servname, const struct sal_type_addrinfo* hints, struct sal_type_addrinfo** res)
  184. {
  185. int port_nr = 0;
  186. sal_type_ip_addr_t addr = { 0 };
  187. struct sal_type_addrinfo* ai;
  188. struct sal_type_sockaddr_storage* sa;
  189. size_t total_size = 0;
  190. size_t namelen = 0;
  191. char* change_point;
  192. if (res == RT_NULL)
  193. return EAI_FAIL;
  194. *res = RT_NULL;
  195. if ((nodename == RT_NULL) && (servname == RT_NULL))
  196. return EAI_NONAME;
  197. if ((hints != RT_NULL)
  198. && (hints->ai_family != AF_INET && hints->ai_family != AF_UNSPEC))
  199. return EAI_FAMILY;
  200. if (servname != RT_NULL)
  201. {
  202. /* service name specified: convert to port number */
  203. port_nr = atoi(servname);
  204. if ((port_nr <= 0) || (port_nr > 0xffff))
  205. {
  206. return EAI_SERVICE;
  207. }
  208. }
  209. if (nodename != RT_NULL)
  210. {
  211. WINDOWS_PERVIOS_COMAMND;
  212. struct hostent* hostent = gethostbyname(nodename);
  213. if (hostent == RT_NULL)
  214. return EAI_FAIL;
  215. addr.addr = ((struct in_addr*)hostent->h_addr)->s_addr;
  216. }
  217. total_size = sizeof(struct sal_type_addrinfo) + sizeof(struct sal_type_sockaddr_storage);
  218. if (nodename != RT_NULL)
  219. {
  220. namelen = strlen(nodename);
  221. if (namelen > DNS_MAX_NAME_LENGTH)
  222. {
  223. /* invalid name length */
  224. return EAI_FAIL;
  225. }
  226. RT_ASSERT(total_size + namelen + 1 > total_size);
  227. total_size += namelen + 1;
  228. }
  229. RT_ASSERT(total_size <= sizeof(struct sal_type_addrinfo) + sizeof(struct sal_type_sockaddr_storage) + DNS_MAX_NAME_LENGTH + 1);
  230. ai = (struct sal_type_addrinfo*)rt_malloc(total_size);
  231. if (ai == RT_NULL)
  232. {
  233. return EAI_MEMORY;
  234. }
  235. rt_memset(ai, 0, total_size);
  236. /* cast through void* to get rid of alignment warnings */
  237. sa = (struct sal_type_sockaddr_storage*)(void*)((uint8_t*)ai + sizeof(struct sal_type_addrinfo));
  238. struct sal_type_sockaddr_in* sa4 = (struct sal_type_sockaddr_in*)sa;
  239. sa4->sin_family = AF_INET;
  240. sa4->sin_addr.sal_type_s_addr = addr.addr;
  241. sa4->sin_port = htons((uint16_t)port_nr);
  242. /* set up addrinfo */
  243. if (hints != RT_NULL)
  244. {
  245. /* copy socktype & protocol from hints if specified */
  246. ai->ai_socktype = hints->ai_socktype;
  247. ai->ai_protocol = hints->ai_protocol;
  248. }
  249. if (nodename != RT_NULL)
  250. {
  251. /* copy nodename to canonname if specified */
  252. ai->ai_canonname = ((char*)ai + sizeof(struct sal_type_addrinfo) + sizeof(struct sal_type_sockaddr_storage));
  253. rt_memcpy(ai->ai_canonname, nodename, namelen);
  254. ai->ai_canonname[namelen] = 0;
  255. }
  256. ai->ai_addrlen = sizeof(struct sal_type_sockaddr_storage);
  257. ai->ai_addr = (struct sal_type_sockaddr*)sa;
  258. ai->ai_family = sa4->sin_family;
  259. *res = ai;
  260. return 0;
  261. }
  262. void win_freeaddrinfo(struct sal_type_addrinfo* ai)
  263. {
  264. struct sal_type_addrinfo* next;
  265. while (ai != NULL)
  266. {
  267. next = ai->ai_next;
  268. rt_free(ai);
  269. ai = next;
  270. }
  271. }