summaryrefslogtreecommitdiff
path: root/bsp/simulator/drivers/winsock/sal_winsock.c
blob: 715ac4e250c6329c2a53299c916389209dea4dbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-05-26     xiangxistu   first version
 */

/* Do NOT include sys/time.h in this file */
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <rtthread.h>
#include <stdint.h>

#define DBG_TAG    "sal.winsock"
#define DBG_LVL    DBG_INFO
#include <rtdbg.h>

#include "rtt_winsock.h"

static struct sal_type_hostent sal_hostent;
static sal_type_ip_addr_t hostent_addr = { 0 };
static sal_type_ip_addr_t* hostent_addr_p = RT_NULL;

/* Important, must reserve */
#pragma comment( lib, "ws2_32.lib" )

/*
 * reserve for select / poll
 *
 * #define WINDOWS_STACK_SIZE 2048
 * #define WINDOWS_PRIORITY   2048
 * #define WINDOWS_TICK       10
 *
 * struct poll_thread_list
 * {
 *     int socket;
 *     rt_slist_t slist;
 * };
 * static rt_slist_t poll_pending_list = RT_SLIST_OBJECT_INIT(poll_pending_list);
 *
 **/

#define WINDOWS_PERVIOS_COMAMND                                 \
    do                                                          \
    {                                                           \
        int iResult;                                            \
        WSADATA wsa_data;                                       \
                                                                \
        iResult = WSAStartup(MAKEWORD(2, 2), &wsa_data);        \
        if (iResult != 0)                                       \
        {                                                       \
            LOG_E("WSAStartup failed: %d\n", iResult);          \
            return -RT_ERROR;                                   \
        }                                                       \
    }while(0)

static void data_sync_sockaddr(struct sal_type_sockaddr* sal, struct sockaddr** window)
{
    /*
     *       ws2def.h
     * typedef struct sockaddr {
     *
     * #if (_WIN32_WINNT < 0x0600)
     *     u_short sa_family;
     * #else
     *     ADDRESS_FAMILY sa_family;           // Address family.
     * #endif //(_WIN32_WINNT < 0x0600)
     *
     *     CHAR sa_data[14];                   // Up to 14 bytes of direct address.
     * } SOCKADDR, *PSOCKADDR, FAR *LPSOCKADDR;
     *
     ***********************************
     *      sal_socket.h
     * struct sockaddr
     * {
     *     uint8_t        sa_len;
     *     sa_family_t    sa_family;
     *     char           sa_data[14];
     * };
     */
    ADDRESS_FAMILY family = 0;
    family = sal->sa_family;

    *window = sal;
    (* window)->sa_family = family;
}

int win_socket(int domain, int type, int protocol)
{
    WINDOWS_PERVIOS_COMAMND;

     return (int)socket(domain, type, protocol);
}

int win_closesocket(int s)
{
    return (int)closesocket(s);
}

int win_bind(int s, const struct sal_type_sockaddr *name, sal_type_socklen_t namelen)
{
    return (int)bind(s, name, namelen);
}

int win_listen(int s, int backlog)
{
    return (int)listen(s, backlog);
}

int win_connect(int s, const struct sal_type_sockaddr *name, sal_type_socklen_t namelen)
{
    struct sockaddr *windows_addr = RT_NULL;
    data_sync_sockaddr(name, &windows_addr);

    return (int)connect(s, windows_addr, namelen);
}

int win_accept(int s, struct sal_type_sockaddr *addr, sal_type_socklen_t *addrlen)
{
    WINDOWS_PERVIOS_COMAMND;

    return (int)accept(s, addr, addrlen);
}

int win_sendto(int s, const void *data, size_t size, int flags, const struct sal_type_sockaddr *to, sal_type_socklen_t tolen)
{
    return (int)sendto(s, data, size, flags, to, tolen);
}

int win_recvfrom(int s, void *mem, size_t len, int flags, struct sal_type_sockaddr *from, sal_type_socklen_t *fromlen)
{
    return (int)recvfrom(s, mem, len, flags, from, fromlen);
}

int win_getsockopt(int s, int level, int optname, void *optval, sal_type_socklen_t *optlen)
{
    return (int)getsockopt(s, level, optname, optval, optlen);
}

int win_setsockopt(int s, int level, int optname, const void *optval, sal_type_socklen_t optlen)
{
    return (int)setsockopt(s, level, optname, optval, optlen);
}

int win_shutdown(int s, int how)
{
    return (int)shutdown(s, how);
}

int win_getpeername(int s, struct sal_type_sockaddr *name, sal_type_socklen_t *namelen)
{
    return (int)getpeername(s, name, namelen);
}

int win_getsockname(int s, struct sal_type_sockaddr *name, sal_type_socklen_t *namelen)
{
    return (int)getsockname(s, name, namelen);
}

int win_ioctlsocket(int s, long cmd, void *arg)
{
    return (int)ioctlsocket(s, cmd, arg);
}

#ifdef SAL_USING_POSIX
/* use windows poll, but not wait */
int inet_poll(struct dfs_file* file, struct rt_pollreq* req)
{
    /*
    WSAPOLLFD winpoll;
    struct sal_socket* sal_sock;
    int mask = 0;
    int poll_event = 0;

    sal_sock = sal_get_socket((int)file->data);
    if (!sal_sock)
    {
        return -1;
    }

    winpoll.fd = sal_sock->socket;
    poll_event &= POLLERR | POLLHUP;
    winpoll.events = req->_key & (~poll_event);
    poll_event = 0;

    mask = WSAPoll(&winpoll, 1, 0);
    if (mask == 0)
        return 0;

    return winpoll.revents;
    */

   return 0;
}
#endif /* SAL_USING_POSIX */

struct sal_type_hostent* win_gethostbyname(const char* name)
{
    struct hostent *windows_hostent;
    char** h_addr_list;

    WINDOWS_PERVIOS_COMAMND;

    windows_hostent = gethostbyname(name);

    sal_hostent.h_name = windows_hostent->h_name;
    sal_hostent.h_aliases = windows_hostent->h_aliases;
    sal_hostent.h_addrtype = windows_hostent->h_addrtype;
    sal_hostent.h_length = windows_hostent->h_length;
    sal_hostent.h_addr_list = windows_hostent->h_addr_list;
    hostent_addr.addr = ((struct in_addr*)windows_hostent->h_addr)->s_addr;
    hostent_addr_p = &hostent_addr;
    sal_hostent.h_addr_list = &hostent_addr_p;

    return &sal_hostent;
}

int win_getaddrinfo(const char* nodename, const char* servname, const struct sal_type_addrinfo* hints, struct sal_type_addrinfo** res)
{
    int port_nr = 0;
    sal_type_ip_addr_t addr = { 0 };
    struct sal_type_addrinfo* ai;
    struct sal_type_sockaddr_storage* sa;
    size_t total_size = 0;
    size_t namelen = 0;
    char* change_point;

    if (res == RT_NULL)
        return EAI_FAIL;
    *res = RT_NULL;

    if ((nodename == RT_NULL) && (servname == RT_NULL))
        return EAI_NONAME;

    if ((hints != RT_NULL)
        && (hints->ai_family != AF_INET && hints->ai_family != AF_UNSPEC))
        return EAI_FAMILY;

    if (servname != RT_NULL)
    {
        /* service name specified: convert to port number */
        port_nr = atoi(servname);
        if ((port_nr <= 0) || (port_nr > 0xffff))
        {
            return EAI_SERVICE;
        }
    }

    if (nodename != RT_NULL)
    {
        WINDOWS_PERVIOS_COMAMND;
        struct hostent* hostent = gethostbyname(nodename);
        if (hostent == RT_NULL)
            return EAI_FAIL;
        addr.addr = ((struct in_addr*)hostent->h_addr)->s_addr;
    }

    total_size = sizeof(struct sal_type_addrinfo) + sizeof(struct sal_type_sockaddr_storage);
    if (nodename != RT_NULL)
    {
        namelen = strlen(nodename);
        if (namelen > DNS_MAX_NAME_LENGTH)
        {
            /* invalid name length */
            return EAI_FAIL;
        }
        RT_ASSERT(total_size + namelen + 1 > total_size);
        total_size += namelen + 1;
    }

    RT_ASSERT(total_size <= sizeof(struct sal_type_addrinfo) + sizeof(struct sal_type_sockaddr_storage) + DNS_MAX_NAME_LENGTH + 1);
    ai = (struct sal_type_addrinfo*)rt_malloc(total_size);
    if (ai == RT_NULL)
    {
        return EAI_MEMORY;
    }
    rt_memset(ai, 0, total_size);

    /* cast through void* to get rid of alignment warnings */
    sa = (struct sal_type_sockaddr_storage*)(void*)((uint8_t*)ai + sizeof(struct sal_type_addrinfo));
    struct sal_type_sockaddr_in* sa4 = (struct sal_type_sockaddr_in*)sa;

    sa4->sin_family = AF_INET;
    sa4->sin_addr.sal_type_s_addr = addr.addr;
    sa4->sin_port = htons((uint16_t)port_nr);

    /* set up addrinfo */
    if (hints != RT_NULL)
    {
        /* copy socktype & protocol from hints if specified */
        ai->ai_socktype = hints->ai_socktype;
        ai->ai_protocol = hints->ai_protocol;
    }
    if (nodename != RT_NULL)
    {
        /* copy nodename to canonname if specified */
        ai->ai_canonname = ((char*)ai + sizeof(struct sal_type_addrinfo) + sizeof(struct sal_type_sockaddr_storage));
        rt_memcpy(ai->ai_canonname, nodename, namelen);
        ai->ai_canonname[namelen] = 0;
    }

    ai->ai_addrlen = sizeof(struct sal_type_sockaddr_storage);
    ai->ai_addr = (struct sal_type_sockaddr*)sa;
    ai->ai_family = sa4->sin_family;

    *res = ai;

    return 0;
}

void win_freeaddrinfo(struct sal_type_addrinfo* ai)
{
    struct sal_type_addrinfo* next;

    while (ai != NULL)
    {
        next = ai->ai_next;
        rt_free(ai);
        ai = next;
    }
}