1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef SOCKET_APP_H
17 #define SOCKET_APP_H
18 
19 #include "fillpinc.h"
20 #include "sockets.h"
21 #include "socket_common.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define IPV6_ADDR_IS_NULL(addr) \
28     (((addr)->sin6_addr.s6_addr[0] == 0) && ((addr)->sin6_addr.s6_addr[1] == 0) && \
29     ((addr)->sin6_addr.s6_addr[2] == 0) && ((addr)->sin6_addr.s6_addr[3] == 0) && \
30     ((addr)->sin6_addr.s6_addr[4] == 0) && ((addr)->sin6_addr.s6_addr[5] == 0) && \
31     ((addr)->sin6_addr.s6_addr[6] == 0) && ((addr)->sin6_addr.s6_addr[7] == 0) && \
32     ((addr)->sin6_addr.s6_addr[8] == 0) && ((addr)->sin6_addr.s6_addr[9] == 0) && \
33     ((addr)->sin6_addr.s6_addr[10] == 0) && ((addr)->sin6_addr.s6_addr[11] == 0) && \
34     ((addr)->sin6_addr.s6_addr[12] == 0) && ((addr)->sin6_addr.s6_addr[13] == 0) && \
35     ((addr)->sin6_addr.s6_addr[14] == 0) && ((addr)->sin6_addr.s6_addr[15] == 0))
36 
37 FILLP_INT SockSocket(FILLP_INT domain, FILLP_INT type, FILLP_INT protocol);
38 FillpErrorType SockConnect(FILLP_INT sockIndex, FILLP_CONST struct sockaddr *name, socklen_t nameLen);
39 FILLP_ULLONG SockGetRtt(FILLP_INT sockFd);
40 FillpErrorType SockBind(FILLP_INT sockIndex, FILLP_CONST struct sockaddr *name, FILLP_UINT32 nameLen);
41 FILLP_INT SockRecv(FILLP_INT fd, void *mem, FILLP_SIZE_T len, FILLP_INT flags);
42 FILLP_INT SockSend(FILLP_INT sockIndex, FILLP_CONST void *data, FILLP_SIZE_T size, FILLP_INT flags);
43 FILLP_INT SockSendFrame(FILLP_INT sockIndex, FILLP_CONST void *data, FILLP_SIZE_T size, FILLP_INT flags,
44     FILLP_CONST struct FrameInfo *frame);
45 
46 FillpErrorType SockReadv(FILLP_INT sockIndex, const struct iovec *iov, FILLP_INT iovCount);
47 FillpErrorType SockWritev(FILLP_INT sockIndex, const struct iovec *iov, FILLP_INT iovCount);
48 FILLP_INT SockRecvmsg(FILLP_INT sockIndex, struct msghdr *msg, FILLP_INT flags);
49 FILLP_INT SockSendmsg(FILLP_INT sockIndex, struct msghdr *msg, FILLP_INT flags);
50 FillpErrorType SockListen(FILLP_INT sockIndex, FILLP_INT backLog);
51 FillpErrorType SockAccept(FILLP_INT fd, struct sockaddr *addr, socklen_t *addrLen);
52 
53 FillpErrorType SockClose(FILLP_INT sockIndex);
54 FillpErrorType SockShutdown(FILLP_INT sockIndex, FILLP_INT how);
55 
56 
57 #ifdef FILLP_LINUX
58 FILLP_INT SockFcntl(FILLP_INT fd, FILLP_INT cmd, FILLP_INT val);
59 #endif
60 
61 FILLP_INT SockIoctlsocket(FILLP_INT fd, FILLP_SLONG cmd, FILLP_CONST FILLP_INT *val);
62 
63 FILLP_INT SockGetsockname(FILLP_INT sockIndex, struct sockaddr *name, socklen_t *nameLen);
64 
65 FILLP_INT SockGetpeername(FILLP_INT sockIndex, struct sockaddr *name, socklen_t *nameLen);
66 
67 FILLP_INT SockGetSockEvt(FILLP_INT fd);
68 
69 #ifdef FILLP_WIN32
70 #ifndef MSGHDR_IOVEC_DEFINED
71 struct iovec {
72     void *iov_base;
73     size_t iov_len;
74 };
75 
76 struct msghdr {
77     void *msg_name;
78     socklen_t msg_namelen;
79     struct iovec *msg_iov;
80     size_t msg_iovlen;
81     void *msg_control;
82     size_t msg_controllen;
83     int msg_flags;
84 };
85 
86 #define MSGHDR_IOVEC_DEFINED 1
87 #endif
88 
89 #ifndef MSG_DONTWAIT
90 #define MSG_DONTWAIT 0x40
91 #endif
92 #endif /* FILLP_WIN32 */
93 
94 FILLP_INT SockEventInfoGet(int s, FtEventCbkInfo *info);
95 
96 #define SOCK_DESTROY_CONN(lock, conn, sock, err) \
97 do { \
98     (void)SYS_ARCH_RWSEM_RDPOST(lock); \
99     FillpNetconnDestroy(conn); \
100     FILLP_SOCK_SET_ERR(sock, err); \
101     SET_ERRNO(err); \
102 } while (0)
103 
104 #define SOCK_SENDMSG_DATA_MOD_LEN(iovIter, iovRemainLen, itemIter, itemRemainLen, sendLen, dataLen, cpylen) \
105 do { \
106     (iovIter) += (cpylen); \
107     (iovRemainLen) -= (cpylen); \
108     (itemIter) += (cpylen); \
109     (itemRemainLen) -= (cpylen); \
110     (sendLen) += (FILLP_INT)(cpylen); \
111     (dataLen) += (FILLP_UINT16)(cpylen); \
112 } while (0)
113 
114 #define SOCK_SENDMSG_DATA_MOD_IOV(iovRemainLen, iovIter, msg, index) \
115 do { \
116     if ((iovRemainLen) == 0) { \
117         (iovIter) = (msg)->msg_iov[index].iov_base; \
118         (iovRemainLen) = (FILLP_UINT32)(msg)->msg_iov[index].iov_len; \
119         (index)++; \
120     } \
121 } while (0)
122 
123 #ifdef __cplusplus
124 }
125 #endif
126 
127 #endif /* SOCKET_APP_H */
128