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 FILLP_NET_H
17 #define FILLP_NET_H
18 
19 #include "fillp_os.h"
20 #include "pcb.h"
21 #include "spunge_mem.h"
22 #include "fillp.h"
23 #include "sysio.h"
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 enum FtConnState {
29     CONN_STATE_IDLE = 0,       /* Alloced but not do connect */
30     CONN_STATE_LISTENING = 1,  /* Listen socket */
31     CONN_STATE_CONNECTING = 2, /* Do connecting, four handshake not finished yet */
32     CONN_STATE_CONNECTED = 3,  /* netConn already connected */
33     CONN_STATE_CLOSING = 4,    /* send disconn request out */
34     CONN_STATE_CLOSED = 5,     /* connection already closed */
35     CONN_STATE_BUTT = 0xff
36 };
37 
38 struct SockOsSocket {
39     /* osListNode is used to get the ft socket index based on address indexing
40     so this must be first member of the structure. if need to be changed please
41     handle SockOsListEntry first */
42     struct HlistNode osListNode; /* This has to be the First member of the structure */
43     SysIoSock *ioSock;
44     FILLP_INT reference;
45     FILLP_INT addrType;
46 };
47 
48 #define OS_SOCK_OPS_FUNC_VALID(_osSock, _ops_func) \
49     (((_osSock) != FILLP_NULL_PTR) && \
50     ((_osSock)->ioSock != FILLP_NULL_PTR) && \
51     ((_osSock)->ioSock->ops != FILLP_NULL_PTR) && \
52     ((_osSock)->ioSock->ops->_ops_func != FILLP_NULL_PTR))
53 
54 #ifndef UDP_MAX_SEG
55 #define UDP_MAX_SEG 44u
56 #endif
57 
58 struct FtNetconn {
59     struct SpungePcb *pcb;
60     void *sock;
61     struct SockOsSocket *osSocket[MAX_SPUNGEINSTANCE_NUM];
62 
63     FILLP_LLONG connTimeout;
64 
65     FILLP_UINT32 closeSet : 1;      /* Application calls close() function */
66     FILLP_UINT32 shutdownRdSet : 1; /* Application called shutdown(sock, RD) */
67     FILLP_UINT32 shutdownWrSet : 1; /* Application called shutdown(sock, WR) */
68     FILLP_UINT32 peerRdSet : 1;     /* Peer notify that it won't read anything */
69     FILLP_UINT32 peerWrSet : 1;     /* Peer notify that it won't send anything */
70     FILLP_UINT32 sendBufRunOut : 1; /* Send buffer has run out */
71     FILLP_UINT32 flagsReverse : 26;
72     FILLP_UINT8 state;
73     FILLP_UINT8 clientFourHandshakeState;
74     FILLP_UINT8 peerFcAlgs; /* bit0: alg1, bit1: alg2 ... */
75     FILLP_UINT8 padd[1];
76     FILLP_UINT32 peerCharacters;
77     FILLP_INT lastErr;
78     FILLP_ULLONG calcRttDuringConnect;
79     FILLP_UINT32 peerPktSize;
80 #ifdef FILLP_LINUX
81     size_t iovCount;
82     struct iovec sendIov[UDP_MAX_SEG];
83 #endif
84 
85 #ifdef FILLP_MGT_MSG_LOG
86     FILLP_BOOL extParameterExisted[FILLP_PKT_EXT_BUTT];
87 #endif
88 };
89 
90 #define NETCONN_GET_OSSOCK(_conn, _instIdx) ((_conn)->osSocket[(_instIdx)])
91 
92 FILLP_INT FillpErrToErrno(FILLP_INT err);
93 void FillpNetconnSetSafeErr(struct FtNetconn *conn, FILLP_INT err);
94 
95 void FillpNetconnSetState(struct FtNetconn *conn, FILLP_UINT8 state);
96 
97 #define NETCONN_GET_STATE(_conn) ((_conn)->state)
98 
99 struct FtNetconn *FillpNetconnAlloc(FILLP_UINT16 domain, struct SpungeInstance *inst);
100 
101 void NetconnSetSendCacheSize(struct FtNetconn *conn, FILLP_UINT32 cacheSize);
102 void NetconnSetRecvCacheSize(struct FtNetconn *conn, FILLP_UINT32 cacheSize);
103 void NetconnSetPktSize(struct FtNetconn *conn, FILLP_UINT32 pktSize);
104 void NetconnSetOpersiteRate(struct FtNetconn *conn, FILLP_UINT32 rate);
105 void NetconnSetSlowStart(struct FtNetconn *conn, FILLP_BOOL slowStart);
106 void NetconnSetPackInterval(struct FtNetconn *conn, FILLP_UINT32 interval);
107 void NetconnSetLocalPort(struct FtNetconn *conn, FILLP_INT port);
108 void NetconnSetAddrType(struct FtNetconn *conn, FILLP_UINT16 addrType);
109 void NetconnSetDirectlySend(struct FtNetconn *conn, FILLP_INT directlySend);
110 void NetconnSetConnectTimeout(struct FtNetconn *conn, FILLP_LLONG timeoutUs);
111 
112 FILLP_BOOL NetconnIsConnectTimeout(struct FtNetconn *conn);
113 
114 void NetconnSetSock(struct FtSocket *sock, struct FtNetconn *conn);
115 void FillpNetconnDestroy(struct FtNetconn *conn);
116 
117 void FillpHandleConnConfirmAckInput(struct FtSocket *sock, struct FtNetconn *conn,
118     struct FillpPcb *pcb, FILLP_CONST struct NetBuf *p);
119 
120 void FillpInitNewconnBySock(struct FtNetconn *conn, FILLP_CONST struct FtSocket *sock);
121 void FillpConnConfirmInput(struct FillpPcb *pcb, FILLP_CONST struct NetBuf *p, struct SpungeInstance *inst);
122 
123 
124 #ifdef __cplusplus
125 }
126 #endif
127 
128 #endif /* FILLP_NET_H */
129