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 SOFTBUS_SOCKET_H 17 #define SOFTBUS_SOCKET_H 18 19 #include <sys/types.h> 20 #include <sys/uio.h> 21 22 #include "softbus_conn_interface.h" 23 #include "softbus_protocol_def.h" 24 25 #ifdef __cplusplus 26 #if __cplusplus 27 extern "C" { 28 #endif 29 #endif 30 31 #ifndef SOFTBUS_TEMP_FAILURE_RETRY 32 #define SOFTBUS_TEMP_FAILURE_RETRY(expression) \ 33 ( \ 34 __extension__ \ 35 ( \ 36 { \ 37 long int __result; \ 38 do { \ 39 __result = (long int) (expression); \ 40 } \ 41 while (__result == SOFTBUS_ADAPTER_SOCKET_EINTR); \ 42 __result; \ 43 } \ 44 ) \ 45 ) 46 #endif 47 48 enum { 49 SOFTBUS_SOCKET_OUT, // writable 50 SOFTBUS_SOCKET_IN, // readable 51 SOFTBUS_SOCKET_EXCEPTION, // exception 52 }; 53 54 typedef struct { 55 char addr[MAX_SOCKET_ADDR_LEN]; 56 int32_t port; 57 } SocketAddr; 58 59 typedef struct SocketInterface { 60 const char* name; 61 const ProtocolType type; 62 int32_t (*GetSockPort)(int32_t fd); 63 int32_t (*OpenServerSocket)(const LocalListenerInfo *option); 64 int32_t (*OpenClientSocket)(const ConnectOption *option, const char* bindAddr, bool isNonBlock); 65 int32_t (*AcceptClient)(int32_t fd, ConnectOption *clientAddr, int32_t *cfd); 66 } SocketInterface; 67 68 int32_t ConnInitSockets(void); 69 void ConnDeinitSockets(void); 70 71 const SocketInterface* GetSocketInterface(ProtocolType protocolType); 72 73 int32_t RegistSocketProtocol(const SocketInterface* interface); 74 75 int32_t ConnOpenClientSocket(const ConnectOption *option, const char* bindAddr, bool isNonBlock); 76 77 ssize_t ConnSendSocketData(int32_t fd, const char *buf, size_t len, int32_t timeout); 78 ssize_t ConnRecvSocketData(int32_t fd, char *buf, size_t len, int32_t timeout); 79 void ConnCloseSocket(int32_t fd); 80 void ConnShutdownSocket(int32_t fd); 81 int32_t ConnSetTcpKeepAlive(int32_t fd, int32_t seconds); 82 int32_t ConnSetTcpUserTimeOut(int32_t fd, uint32_t millSec); 83 84 int32_t ConnToggleNonBlockMode(int32_t fd, bool isNonBlock); 85 int32_t ConnGetSocketError(int32_t fd); 86 int32_t ConnGetLocalSocketPort(int32_t fd); 87 int32_t ConnGetPeerSocketAddr(int32_t fd, SocketAddr *socketAddr); 88 89 #ifdef __cplusplus 90 #if __cplusplus 91 } 92 #endif /* __cplusplus */ 93 #endif /* __cplusplus */ 94 95 #endif 96