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