osSocket.h 6.5 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16 17
#ifndef _TD_OS_SOCKET_H_
#define _TD_OS_SOCKET_H_
S
Shengliang Guan 已提交
18

19
// If the error is in a third-party library, place this header file under the third-party library header file.
wafwerar's avatar
wafwerar 已提交
20
// When you want to use this feature, you should find or add the same function in the following section.
21
#ifndef ALLOW_FORBID_FUNC
dengyihao's avatar
dengyihao 已提交
22 23 24 25 26 27 28 29 30
#define socket       SOCKET_FUNC_TAOS_FORBID
#define bind         BIND_FUNC_TAOS_FORBID
#define listen       LISTEN_FUNC_TAOS_FORBID
#define accept       ACCEPT_FUNC_TAOS_FORBID
#define epoll_create EPOLL_CREATE_FUNC_TAOS_FORBID
#define epoll_ctl    EPOLL_CTL_FUNC_TAOS_FORBID
#define epoll_wait   EPOLL_WAIT_FUNC_TAOS_FORBID
#define inet_addr    INET_ADDR_FUNC_TAOS_FORBID
#define inet_ntoa    INET_NTOA_FUNC_TAOS_FORBID
31 32
#endif

33
#if defined(WINDOWS)
dengyihao's avatar
dengyihao 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#if BYTE_ORDER == LITTLE_ENDIAN
#include <stdlib.h>
#define htobe16(x) _byteswap_ushort(x)
#define htole16(x) (x)
#define be16toh(x) _byteswap_ushort(x)
#define le16toh(x) (x)

#define htobe32(x) _byteswap_ulong(x)
#define htole32(x) (x)
#define be32toh(x) _byteswap_ulong(x)
#define le32toh(x) (x)

#define htobe64(x) _byteswap_uint64(x)
#define htole64(x) (x)
#define be64toh(x) _byteswap_uint64(x)
#define le64toh(x) (x)
#else
#error byte order not supported
#endif

#define __BYTE_ORDER    BYTE_ORDER
#define __BIG_ENDIAN    BIG_ENDIAN
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#define __PDP_ENDIAN    PDP_ENDIAN
wafwerar's avatar
wafwerar 已提交
58

S
Shengliang Guan 已提交
59
#else
dengyihao's avatar
dengyihao 已提交
60 61 62 63 64 65 66 67 68
#include <netinet/in.h>
#include <sys/socket.h>

#if defined(_TD_DARWIN_64)
#include <osEok.h>
#else
#include <netinet/in.h>
#include <sys/epoll.h>
#endif
S
Shengliang Guan 已提交
69 70
#endif

S
os  
Shengliang Guan 已提交
71 72 73 74
#ifdef __cplusplus
extern "C" {
#endif

75
#if defined(WINDOWS)
dengyihao's avatar
dengyihao 已提交
76 77 78 79 80 81 82 83
typedef int socklen_t;
#define TAOS_EPOLL_WAIT_TIME 100
typedef SOCKET eventfd_t;
#define eventfd(a, b)      -1
#define EpollClose(pollFd) epoll_close(pollFd)
#ifndef EPOLLWAKEUP
#define EPOLLWAKEUP (1u << 29)
#endif
84
#elif defined(_TD_DARWIN_64)
dengyihao's avatar
dengyihao 已提交
85 86 87 88
#define TAOS_EPOLL_WAIT_TIME 500
typedef int32_t SOCKET;
typedef SOCKET  EpollFd;
#define EpollClose(pollFd)   epoll_close(pollFd)
89
#else
dengyihao's avatar
dengyihao 已提交
90 91 92 93
#define TAOS_EPOLL_WAIT_TIME 500
typedef int32_t SOCKET;
typedef SOCKET  EpollFd;
#define EpollClose(pollFd)   taosCloseSocket(pollFd)
94 95
#endif

S
TD-4088  
Shengliang Guan 已提交
96
#if defined(_TD_DARWIN_64)
97 98
//  #define htobe64 htonll

wafwerar's avatar
wafwerar 已提交
99
#include <libkern/OSByteOrder.h>
100

wafwerar's avatar
wafwerar 已提交
101 102 103 104
#define htobe16(x) OSSwapHostToBigInt16(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#define be16toh(x) OSSwapBigToHostInt16(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
105

wafwerar's avatar
wafwerar 已提交
106 107 108 109
#define htobe32(x) OSSwapHostToBigInt32(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define be32toh(x) OSSwapBigToHostInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
110

wafwerar's avatar
wafwerar 已提交
111 112 113 114
#define htobe64(x) OSSwapHostToBigInt64(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define be64toh(x) OSSwapBigToHostInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
115

wafwerar's avatar
wafwerar 已提交
116 117 118 119
#define __BYTE_ORDER    BYTE_ORDER
#define __BIG_ENDIAN    BIG_ENDIAN
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#define __PDP_ENDIAN    PDP_ENDIAN
S
TD-4088  
Shengliang Guan 已提交
120 121
#endif

dengyihao's avatar
dengyihao 已提交
122 123
typedef int32_t  SocketFd;
typedef SocketFd EpollFd;
124 125 126 127 128 129 130

typedef struct TdSocket {
#if SOCKET_WITH_LOCK
  TdThreadRwlock rwlock;
#endif
  int      refId;
  SocketFd fd;
dengyihao's avatar
dengyihao 已提交
131
} * TdSocketPtr, TdSocket;
132

wafwerar's avatar
wafwerar 已提交
133
typedef struct TdSocketServer *TdSocketServerPtr;
dengyihao's avatar
dengyihao 已提交
134 135
typedef struct TdSocket *      TdSocketPtr;
typedef struct TdEpoll *       TdEpollPtr;
wafwerar's avatar
wafwerar 已提交
136

dengyihao's avatar
dengyihao 已提交
137
int32_t taosSendto(TdSocketPtr pSocket, void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen);
wafwerar's avatar
wafwerar 已提交
138 139
int32_t taosWriteSocket(TdSocketPtr pSocket, void *msg, int len);
int32_t taosReadSocket(TdSocketPtr pSocket, void *msg, int len);
dengyihao's avatar
dengyihao 已提交
140 141
int32_t taosReadFromSocket(TdSocketPtr pSocket, void *buf, int32_t len, int32_t flags, struct sockaddr *destAddr,
                           int *addrLen);
142
int32_t taosCloseSocketNoCheck1(SocketFd fd);
wafwerar's avatar
wafwerar 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
int32_t taosCloseSocket(TdSocketPtr *ppSocket);
int32_t taosCloseSocketServer(TdSocketServerPtr *ppSocketServer);
int32_t taosShutDownSocketRD(TdSocketPtr pSocket);
int32_t taosShutDownSocketServerRD(TdSocketServerPtr pSocketServer);
int32_t taosShutDownSocketWR(TdSocketPtr pSocket);
int32_t taosShutDownSocketServerWR(TdSocketServerPtr pSocketServer);
int32_t taosShutDownSocketRDWR(TdSocketPtr pSocket);
int32_t taosShutDownSocketServerRDWR(TdSocketServerPtr pSocketServer);
int32_t taosSetNonblocking(TdSocketPtr pSocket, int32_t on);
int32_t taosSetSockOpt(TdSocketPtr pSocket, int32_t level, int32_t optname, void *optval, int32_t optlen);
int32_t taosGetSockOpt(TdSocketPtr pSocket, int32_t level, int32_t optname, void *optval, int32_t *optlen);
int32_t taosWriteMsg(TdSocketPtr pSocket, void *ptr, int32_t nbytes);
int32_t taosReadMsg(TdSocketPtr pSocket, void *ptr, int32_t nbytes);
int32_t taosNonblockwrite(TdSocketPtr pSocket, char *ptr, int32_t nbytes);
int64_t taosCopyFds(TdSocketPtr pSrcSocket, TdSocketPtr pDestSocket, int64_t len);
dengyihao's avatar
dengyihao 已提交
158 159 160
void    taosWinSocketInit();

int taosCreateSocketWithTimeOutOpt(uint32_t conn_timeout_sec);
161

dengyihao's avatar
dengyihao 已提交
162 163
TdSocketPtr       taosOpenUdpSocket(uint32_t localIp, uint16_t localPort);
TdSocketPtr       taosOpenTcpClientSocket(uint32_t ip, uint16_t port, uint32_t localIp);
wafwerar's avatar
wafwerar 已提交
164
TdSocketServerPtr taosOpenTcpServerSocket(uint32_t ip, uint16_t port);
dengyihao's avatar
dengyihao 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177
int32_t           taosKeepTcpAlive(TdSocketPtr pSocket);
TdSocketPtr       taosAcceptTcpConnectSocket(TdSocketServerPtr pServerSocket, struct sockaddr *destAddr, int *addrLen);

int32_t taosGetSocketName(TdSocketPtr pSocket, struct sockaddr *destAddr, int *addrLen);

void        taosBlockSIGPIPE();
uint32_t    taosGetIpv4FromFqdn(const char *);
int32_t     taosGetFqdn(char *);
void        tinet_ntoa(char *ipstr, uint32_t ip);
uint32_t    ip2uint(const char *const ip_addr);
void        taosIgnSIGPIPE();
void        taosSetMaskSIGPIPE();
uint32_t    taosInetAddr(const char *ipAddr);
wafwerar's avatar
wafwerar 已提交
178 179 180
const char *taosInetNtoa(struct in_addr ipInt);

TdEpollPtr taosCreateEpoll(int32_t size);
dengyihao's avatar
dengyihao 已提交
181 182 183
int32_t    taosCtlEpoll(TdEpollPtr pEpoll, int32_t epollOperate, TdSocketPtr pSocket, struct epoll_event *event);
int32_t    taosWaitEpoll(TdEpollPtr pEpoll, struct epoll_event *event, int32_t maxEvents, int32_t timeout);
int32_t    taosCloseEpoll(TdEpollPtr *ppEpoll);
184

S
Shengliang Guan 已提交
185 186 187 188
#ifdef __cplusplus
}
#endif

S
Shengliang Guan 已提交
189
#endif /*_TD_OS_SOCKET_H_*/