xref: /kernel/liteos_a/compat/posix/src/socket.c (revision 0d163575)
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 *    conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 *    of conditions and the following disclaimer in the documentation and/or other materials
13 *    provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 *    to endorse or promote products derived from this software without specific prior written
17 *    permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <los_config.h>
33#include <arpa/inet.h>
34#include <sys/socket.h>
35
36#ifdef LOSCFG_NET_LWIP_SACK
37#include <lwip/sockets.h>
38
39#if !LWIP_COMPAT_SOCKETS
40
41#define CHECK_NULL_PTR(ptr) do { if (ptr == NULL) { set_errno(EFAULT); return -1; } } while (0)
42
43int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
44{
45    return lwip_accept(s, addr, addrlen);
46}
47
48int bind(int s, const struct sockaddr *name, socklen_t namelen)
49{
50    CHECK_NULL_PTR(name);
51    if (namelen < sizeof(*name)) {
52        set_errno(EINVAL);
53        return -1;
54    }
55    return lwip_bind(s, name, namelen);
56}
57
58int shutdown(int s, int how)
59{
60    return lwip_shutdown(s, how);
61}
62
63int getpeername(int s, struct sockaddr *name, socklen_t *namelen)
64{
65    CHECK_NULL_PTR(name);
66    CHECK_NULL_PTR(namelen);
67    return lwip_getpeername(s, name, namelen);
68}
69
70int getsockname(int s, struct sockaddr *name, socklen_t *namelen)
71{
72    CHECK_NULL_PTR(name);
73    CHECK_NULL_PTR(namelen);
74    return lwip_getsockname(s, name, namelen);
75}
76
77int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
78{
79    return lwip_getsockopt(s, level, optname, optval, optlen);
80}
81
82int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
83{
84    return lwip_setsockopt(s, level, optname, optval, optlen);
85}
86
87int closesocket(int s)
88{
89    return lwip_close(s);
90}
91
92int connect(int s, const struct sockaddr *name, socklen_t namelen)
93{
94    CHECK_NULL_PTR(name);
95    if (namelen < sizeof(*name)) {
96        set_errno(EINVAL);
97        return -1;
98    }
99    return lwip_connect(s, name, namelen);
100}
101
102int listen(int s, int backlog)
103{
104    return lwip_listen(s, backlog);
105}
106
107ssize_t recv(int s, void *mem, size_t len, int flags)
108{
109    CHECK_NULL_PTR(mem);
110    return lwip_recv(s, mem, len, flags);
111}
112
113ssize_t recvfrom(int s, void *mem, size_t len, int flags,
114                 struct sockaddr *from, socklen_t *fromlen)
115{
116    CHECK_NULL_PTR(mem);
117    return lwip_recvfrom(s, mem, len, flags, from, fromlen);
118}
119
120ssize_t recvmsg(int s, struct msghdr *message, int flags)
121{
122    CHECK_NULL_PTR(message);
123    if (message->msg_iovlen) {
124        CHECK_NULL_PTR(message->msg_iov);
125    }
126    return lwip_recvmsg(s, message, flags);
127}
128
129ssize_t send(int s, const void *dataptr, size_t size, int flags)
130{
131    CHECK_NULL_PTR(dataptr);
132    return  lwip_send(s, dataptr, size, flags);
133}
134
135ssize_t sendmsg(int s, const struct msghdr *message, int flags)
136{
137    return lwip_sendmsg(s, message, flags);
138}
139
140ssize_t sendto(int s, const void *dataptr, size_t size, int flags,
141               const struct sockaddr *to, socklen_t tolen)
142{
143    CHECK_NULL_PTR(dataptr);
144    if (to && tolen < sizeof(*to)) {
145        set_errno(EINVAL);
146        return -1;
147    }
148    return lwip_sendto(s, dataptr, size, flags, to, tolen);
149}
150
151int socket(int domain, int type, int protocol)
152{
153    return lwip_socket(domain, type, protocol);
154}
155
156const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
157{
158    return lwip_inet_ntop(af, src, dst, size);
159}
160
161int inet_pton(int af, const char *src, void *dst)
162{
163    return lwip_inet_pton(af, src, dst);
164}
165
166#ifndef LWIP_INET_ADDR_FUNC
167in_addr_t inet_addr(const char* cp)
168{
169    return ipaddr_addr(cp);
170}
171#endif
172
173#ifndef LWIP_INET_ATON_FUNC
174int inet_aton(const char* cp, struct in_addr* inp)
175{
176    return ip4addr_aton(cp, (ip4_addr_t*)inp);
177}
178#endif
179
180#ifndef LWIP_INET_NTOA_FUNC
181char* inet_ntoa(struct in_addr in)
182{
183    return ip4addr_ntoa((const ip4_addr_t*)&(in));
184}
185#endif
186
187#endif /* !LWIP_COMPAT_SOCKETS */
188#endif /* LOSCFG_NET_LWIP_SACK */