1/*
2 * Copyright (C) 2021 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 NSTACKX_SOCKET_H
17#define NSTACKX_SOCKET_H
18
19#include "sys_common_header.h"
20#include "nstackx_common_header.h"
21
22typedef enum SocketProtocol {
23    NSTACKX_PROTOCOL_TCP = 0,
24    NSTACKX_PROTOCOL_UDP,
25    NSTACKX_PROTOCOL_D2D
26} SocketProtocol;
27
28typedef struct Socket {
29    SocketProtocol protocol;
30    uint8_t isServer;
31    SocketDesc sockfd;
32    struct sockaddr_in dstAddr;
33    struct sockaddr_in srcAddr;
34} Socket;
35
36int32_t SocketModuleInit(void);
37void SocketModuleClean(void);
38int32_t SetSocketNonBlock(SocketDesc fd);
39int32_t SocketOpInProgress(void);
40int32_t SocketOpWouldBlock(void);
41int32_t CheckSocketError(void);
42Socket *ClientSocket(SocketProtocol protocol, const struct sockaddr_in *sockAddr);
43Socket *ServerSocket(SocketProtocol protocol, const struct sockaddr_in *sockAddr);
44void CloseSocket(Socket *socket);
45Socket *AcceptSocket(Socket *serverSocket);
46int32_t SocketSend(const Socket *socket, const uint8_t *buffer, size_t length);
47int32_t SocketSendEx(const Socket *socket, uint16_t mss, const struct iovec *iov, uint32_t cnt);
48void CheckGSOSupport(void);
49int32_t SupportGSO(void);
50int32_t SocketRecv(Socket *socket, uint8_t *buffer, size_t length, struct sockaddr_in *srcAddr,
51                   const socklen_t *addrLen);
52Socket *ClientSocketWithTargetDev(SocketProtocol protocol, const struct sockaddr_in *sockAddr,
53                                  const char *localInterface);
54
55#endif  /* NSTACKX_SOCKET_H */
56