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