1060ff233Sopenharmony_ci/*
2060ff233Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License.
5060ff233Sopenharmony_ci * You may obtain a copy of the License at
6060ff233Sopenharmony_ci *
7060ff233Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8060ff233Sopenharmony_ci *
9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and
13060ff233Sopenharmony_ci * limitations under the License.
14060ff233Sopenharmony_ci */
15060ff233Sopenharmony_ci
16060ff233Sopenharmony_ci#ifndef SOFTBUS_ADAPTER_SOCKET_H
17060ff233Sopenharmony_ci#define SOFTBUS_ADAPTER_SOCKET_H
18060ff233Sopenharmony_ci
19060ff233Sopenharmony_ci#include <stdint.h>
20060ff233Sopenharmony_ci#include "softbus_adapter_define.h"
21060ff233Sopenharmony_ci#include "softbus_adapter_timer.h"
22060ff233Sopenharmony_ci#ifdef __cplusplus
23060ff233Sopenharmony_ci#if __cplusplus
24060ff233Sopenharmony_ciextern "C" {
25060ff233Sopenharmony_ci#endif
26060ff233Sopenharmony_ci#endif
27060ff233Sopenharmony_ci
28060ff233Sopenharmony_ci#define SA_DATA_SIZE (26)
29060ff233Sopenharmony_ci#define ADDR_IN6_8_SIZE (16)
30060ff233Sopenharmony_ci#define ADDR_IN6_16_SIZE (8)
31060ff233Sopenharmony_ci#define ADDR_IN6_32_SIZE (4)
32060ff233Sopenharmony_ci#define IF_NAME_SIZE (16)
33060ff233Sopenharmony_ci
34060ff233Sopenharmony_ci#if defined(__aarch64__) || defined(__x86_64__) || (defined(__riscv) && (__riscv_xlen == 64))
35060ff233Sopenharmony_ci#define ADDR_IN_RESER_SIZE (4)
36060ff233Sopenharmony_ci#else
37060ff233Sopenharmony_ci#define ADDR_IN_RESER_SIZE (8)
38060ff233Sopenharmony_ci#endif
39060ff233Sopenharmony_ci
40060ff233Sopenharmony_ci/* sys/socket.h */
41060ff233Sopenharmony_ci#define SOFTBUS_PF_UNSPEC SOFTBUS_PF_UNSPEC_
42060ff233Sopenharmony_ci#define SOFTBUS_AF_UNSPEC SOFTBUS_AF_UNSPEC_
43060ff233Sopenharmony_ci
44060ff233Sopenharmony_ci#define SOFTBUS_PF_INET SOFTBUS_PF_INET_
45060ff233Sopenharmony_ci#define SOFTBUS_AF_INET SOFTBUS_AF_INET_
46060ff233Sopenharmony_ci
47060ff233Sopenharmony_ci#define SOFTBUS_PF_INET6 SOFTBUS_PF_INET6_
48060ff233Sopenharmony_ci#define SOFTBUS_AF_INET6 SOFTBUS_AF_INET6_
49060ff233Sopenharmony_ci
50060ff233Sopenharmony_ci#define SOFTBUS_PF_NETLINK SOFTBUS_PF_NETLINK_
51060ff233Sopenharmony_ci#define SOFTBUS_AF_NETLINK SOFTBUS_AF_NETLINK_
52060ff233Sopenharmony_ci
53060ff233Sopenharmony_ci#define SOFTBUS_SOCK_STREAM SOFTBUS_SOCK_STREAM_
54060ff233Sopenharmony_ci#define SOFTBUS_SOCK_DGRAM SOFTBUS_SOCK_DGRAM_
55060ff233Sopenharmony_ci#define SOFTBUS_SOCK_RAW SOFTBUS_SOCK_RAW_
56060ff233Sopenharmony_ci
57060ff233Sopenharmony_ci#define SOFTBUS_SOCK_CLOEXEC SOFTBUS_SOCK_CLOEXEC_
58060ff233Sopenharmony_ci#define SOFTBUS_SOCK_NONBLOCK SOFTBUS_SOCK_NONBLOCK_
59060ff233Sopenharmony_ci
60060ff233Sopenharmony_ci#define SOFTBUS_SOL_SOCKET SOFTBUS_SOL_SOCKET_
61060ff233Sopenharmony_ci
62060ff233Sopenharmony_ci#define SOFTBUS_SO_REUSEADDR SOFTBUS_SO_REUSEADDR_
63060ff233Sopenharmony_ci#define SOFTBUS_SO_RCVBUF SOFTBUS_SO_RCVBUF_
64060ff233Sopenharmony_ci#define SOFTBUS_SO_SNDBUF SOFTBUS_SO_SNDBUF_
65060ff233Sopenharmony_ci#define SOFTBUS_SO_KEEPALIVE SOFTBUS_SO_KEEPALIVE_
66060ff233Sopenharmony_ci#define SOFTBUS_SO_REUSEPORT SOFTBUS_SO_REUSEPORT_
67060ff233Sopenharmony_ci#define SOFTBUS_SO_RCVBUFFORCE SOFTBUS_SO_RCVBUFFORCE_
68060ff233Sopenharmony_ci#define SOFTBUS_SO_BINDTODEVICE SOFTBUS_SO_BINDTODEVICE_
69060ff233Sopenharmony_ci
70060ff233Sopenharmony_ci#define SOFTBUS_TCP_KEEPIDLE SOFTBUS_TCP_KEEPIDLE_
71060ff233Sopenharmony_ci#define SOFTBUS_TCP_KEEPINTVL SOFTBUS_TCP_KEEPINTVL_
72060ff233Sopenharmony_ci#define SOFTBUS_TCP_KEEPCNT SOFTBUS_TCP_KEEPCNT_
73060ff233Sopenharmony_ci#define SOFTBUS_TCP_USER_TIMEOUT SOFTBUS_TCP_USER_TIMEOUT_
74060ff233Sopenharmony_ci
75060ff233Sopenharmony_ci#define SOFTBUS_SHUT_RD SOFTBUS_SHUT_RD_
76060ff233Sopenharmony_ci#define SOFTBUS_SHUT_WR SOFTBUS_SHUT_WR_
77060ff233Sopenharmony_ci#define SOFTBUS_SHUT_RDWR SOFTBUS_SHUT_RDWR_
78060ff233Sopenharmony_ci
79060ff233Sopenharmony_ci/* netinet/in.h */
80060ff233Sopenharmony_ci#define SOFTBUS_IPPROTO_IP SOFTBUS_IPPROTO_IP_
81060ff233Sopenharmony_ci#define SOFTBUS_IPPROTO_TCP SOFTBUS_IPPROTO_TCP_
82060ff233Sopenharmony_ci
83060ff233Sopenharmony_ci#define SOFTBUS_IP_TOS SOFTBUS_IP_TOS_
84060ff233Sopenharmony_ci
85060ff233Sopenharmony_ci/* netinet/tcp.h */
86060ff233Sopenharmony_ci#define SOFTBUS_TCP_NODELAY SOFTBUS_TCP_NODELAY_
87060ff233Sopenharmony_ci
88060ff233Sopenharmony_ci/* fcntl.h */
89060ff233Sopenharmony_ci#define SOFTBUS_F_GETFL SOFTBUS_F_GETFL_
90060ff233Sopenharmony_ci#define SOFTBUS_F_SETFL SOFTBUS_F_SETFL_
91060ff233Sopenharmony_ci
92060ff233Sopenharmony_ci#define SOFTBUS_O_NONBLOCK SOFTBUS_O_NONBLOCK_
93060ff233Sopenharmony_ci
94060ff233Sopenharmony_ci/* select.h */
95060ff233Sopenharmony_ci/* linux support 1024, liteos support 640 */
96060ff233Sopenharmony_ci#define SOFTBUS_FD_SETSIZE SOFTBUS_FD_SETSIZE_
97060ff233Sopenharmony_ci
98060ff233Sopenharmony_citypedef SoftBusSysTime SoftBusSockTimeOut;
99060ff233Sopenharmony_ci/* netinet/in.h */
100060ff233Sopenharmony_citypedef struct {
101060ff233Sopenharmony_ci    uint16_t saFamily; /* address family */
102060ff233Sopenharmony_ci    char saData[SA_DATA_SIZE];
103060ff233Sopenharmony_ci} SoftBusSockAddr;
104060ff233Sopenharmony_ci
105060ff233Sopenharmony_ci#pragma pack (1)
106060ff233Sopenharmony_citypedef struct {
107060ff233Sopenharmony_ci    unsigned long sAddr;
108060ff233Sopenharmony_ci} SoftBusInAddr;
109060ff233Sopenharmony_ci
110060ff233Sopenharmony_citypedef struct {
111060ff233Sopenharmony_ci    uint16_t sinFamily; /* address family */
112060ff233Sopenharmony_ci    uint16_t sinPort; /* Port number */
113060ff233Sopenharmony_ci    SoftBusInAddr sinAddr; /* Internet address */
114060ff233Sopenharmony_ci    unsigned char sinZero[ADDR_IN_RESER_SIZE]; /* Same size as struct sockaddr */
115060ff233Sopenharmony_ci} SoftBusSockAddrIn;
116060ff233Sopenharmony_ci
117060ff233Sopenharmony_citypedef struct {
118060ff233Sopenharmony_ci    union {
119060ff233Sopenharmony_ci        uint8_t sA6ddr8[ADDR_IN6_8_SIZE];
120060ff233Sopenharmony_ci        uint8_t sA6ddr16[ADDR_IN6_16_SIZE];
121060ff233Sopenharmony_ci        uint8_t sA6ddr32[ADDR_IN6_32_SIZE];
122060ff233Sopenharmony_ci    } sA6ddr;
123060ff233Sopenharmony_ci} SoftBusIn6Addr;
124060ff233Sopenharmony_ci
125060ff233Sopenharmony_citypedef struct {
126060ff233Sopenharmony_ci    uint16_t sin6Family; /* Ipv6 address family */
127060ff233Sopenharmony_ci    uint16_t sin6Port; /* Ipv6 Port number */
128060ff233Sopenharmony_ci    uint32_t sin6FlowInfo; /* Ipv6 flow info */
129060ff233Sopenharmony_ci    SoftBusIn6Addr sin6Addr; /* Ipv6 address */
130060ff233Sopenharmony_ci    uint32_t sin6ScopeId; /* Ipv6 scope id */
131060ff233Sopenharmony_ci} SoftBusSockAddrIn6;
132060ff233Sopenharmony_ci#pragma pack ()
133060ff233Sopenharmony_ci
134060ff233Sopenharmony_citypedef struct {
135060ff233Sopenharmony_ci    uint32_t fdsCount;
136060ff233Sopenharmony_ci    unsigned long fdsBits[SOFTBUS_FD_SETSIZE / 8 / sizeof(long)];
137060ff233Sopenharmony_ci} SoftBusFdSet;
138060ff233Sopenharmony_ci
139060ff233Sopenharmony_ciint32_t SoftBusSocketCreate(int32_t domain, int32_t type, int32_t protocol, int32_t *socketFd);
140060ff233Sopenharmony_ciint32_t SoftBusSocketSetOpt(int32_t socketFd, int32_t level, int32_t optName,  const void *optVal, int32_t optLen);
141060ff233Sopenharmony_ciint32_t SoftBusSocketGetOpt(int32_t socketFd, int32_t level, int32_t optName,  void *optVal, int32_t *optLen);
142060ff233Sopenharmony_ciint32_t SoftBusSocketGetLocalName(int32_t socketFd, SoftBusSockAddr *addr);
143060ff233Sopenharmony_ciint32_t SoftBusSocketGetPeerName(int32_t socketFd, SoftBusSockAddr *addr);
144060ff233Sopenharmony_ci
145060ff233Sopenharmony_ciint32_t SoftBusSocketBind(int32_t socketFd, SoftBusSockAddr *addr, int32_t addrLen);
146060ff233Sopenharmony_ciint32_t SoftBusSocketListen(int32_t socketFd, int32_t backLog);
147060ff233Sopenharmony_ciint32_t SoftBusSocketAccept(int32_t socketFd, SoftBusSockAddr *addr, int32_t *acceptFd);
148060ff233Sopenharmony_ciint32_t SoftBusSocketConnect(int32_t socketFd, const SoftBusSockAddr *addr, int32_t addrLen);
149060ff233Sopenharmony_ci
150060ff233Sopenharmony_civoid SoftBusSocketFdZero(SoftBusFdSet *set);
151060ff233Sopenharmony_civoid SoftBusSocketFdSet(int32_t socketFd, SoftBusFdSet *set);
152060ff233Sopenharmony_civoid SoftBusSocketFdClr(int32_t socketFd, SoftBusFdSet *set);
153060ff233Sopenharmony_ciint32_t SoftBusSocketFdIsset(int32_t socketFd, SoftBusFdSet *set);
154060ff233Sopenharmony_ci
155060ff233Sopenharmony_ciint32_t SoftBusSocketSelect(int32_t nfds, SoftBusFdSet *readFds, SoftBusFdSet *writeFds, SoftBusFdSet *exceptFds,
156060ff233Sopenharmony_ci    SoftBusSockTimeOut *timeOut);
157060ff233Sopenharmony_ciint32_t SoftBusSocketIoctl(int32_t socketFd, long cmd, void *argp);
158060ff233Sopenharmony_ciint32_t SoftBusSocketFcntl(int32_t socketFd, long cmd, long flag);
159060ff233Sopenharmony_ci
160060ff233Sopenharmony_ciint32_t SoftBusSocketSend(int32_t socketFd, const void *buf, uint32_t len, uint32_t flags);
161060ff233Sopenharmony_ciint32_t SoftBusSocketSendTo(int32_t socketFd, const void *buf, uint32_t len, int32_t flags,
162060ff233Sopenharmony_ci    const SoftBusSockAddr *toAddr, int32_t toAddrLen);
163060ff233Sopenharmony_ci
164060ff233Sopenharmony_ciint32_t SoftBusSocketRecv(int32_t socketFd, void *buf, uint32_t len, int32_t flags);
165060ff233Sopenharmony_ciint32_t SoftBusSocketRecvFrom(int32_t socketFd, void *buf, uint32_t len, int32_t flags, SoftBusSockAddr *fromAddr,
166060ff233Sopenharmony_ci    int32_t *fromAddrLen);
167060ff233Sopenharmony_ci
168060ff233Sopenharmony_ci
169060ff233Sopenharmony_ciint32_t SoftBusSocketShutDown(int32_t socketFd, int32_t how);
170060ff233Sopenharmony_ciint32_t SoftBusSocketClose(int32_t socketFd);
171060ff233Sopenharmony_ci
172060ff233Sopenharmony_ciint32_t SoftBusInetPtoN(int32_t af, const char *src, void *dst);
173060ff233Sopenharmony_ciconst char *SoftBusInetNtoP(int32_t af, const void *src, char *dst, int32_t size);
174060ff233Sopenharmony_ci
175060ff233Sopenharmony_ciuint32_t SoftBusHtoNl(uint32_t hostlong);
176060ff233Sopenharmony_ciuint16_t SoftBusHtoNs(uint16_t hostshort);
177060ff233Sopenharmony_ciuint32_t SoftBusNtoHl(uint32_t netlong);
178060ff233Sopenharmony_ciuint16_t SoftBusNtoHs(uint16_t netshort);
179060ff233Sopenharmony_ci
180060ff233Sopenharmony_ci/* host to little-endian */
181060ff233Sopenharmony_ciuint16_t SoftBusHtoLs(uint16_t value);
182060ff233Sopenharmony_ciuint32_t SoftBusHtoLl(uint32_t value);
183060ff233Sopenharmony_ciuint64_t SoftBusHtoLll(uint64_t value);
184060ff233Sopenharmony_ci/* little-endian to host */
185060ff233Sopenharmony_ciuint16_t SoftBusLtoHs(uint16_t value);
186060ff233Sopenharmony_ciuint32_t SoftBusLtoHl(uint32_t value);
187060ff233Sopenharmony_ciuint64_t SoftBusLtoHll(uint64_t value);
188060ff233Sopenharmony_ciuint16_t SoftBusLEtoBEs(uint16_t value);
189060ff233Sopenharmony_ciuint16_t SoftBusBEtoLEs(uint16_t value);
190060ff233Sopenharmony_ci
191060ff233Sopenharmony_ciuint32_t SoftBusInetAddr(const char *cp);
192060ff233Sopenharmony_ciuint32_t SoftBusIfNameToIndex(const char *name);
193060ff233Sopenharmony_ciint32_t SoftBusIndexToIfName(int32_t index, char *ifname, uint32_t nameLen);
194060ff233Sopenharmony_ci
195060ff233Sopenharmony_ciint32_t SoftBusSocketGetError(int32_t socketFd);
196060ff233Sopenharmony_ci
197060ff233Sopenharmony_ci#ifdef __cplusplus
198060ff233Sopenharmony_ci#if __cplusplus
199060ff233Sopenharmony_ci}
200060ff233Sopenharmony_ci#endif /* __cplusplus */
201060ff233Sopenharmony_ci#endif /* __cplusplus */
202060ff233Sopenharmony_ci
203060ff233Sopenharmony_ci#endif // SOFTBUS_ADAPTER_SOCKET_H
204