• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3535

23 Nov 2024 02:07AM UTC coverage: 60.85% (+0.03%) from 60.825%
#3535

push

travis-ci

web-flow
Merge pull request #28893 from taosdata/doc/internal

refact: rename taos lib name

120252 of 252737 branches covered (47.58%)

Branch coverage included in aggregate %.

201187 of 275508 relevant lines covered (73.02%)

15886166.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#define _DEFAULT_SOURCE
17
#define ALLOW_FORBID_FUNC
18
#include "os.h"
19

20
#if defined(WINDOWS)
21
#include <IPHlpApi.h>
22
#include <WS2tcpip.h>
23
#include <Winsock2.h>
24
#include <stdio.h>
25
#include <string.h>
26
#include <tchar.h>
27
#include <winbase.h>
28
#else
29
#include <arpa/inet.h>
30
#include <fcntl.h>
31
#include <net/if.h>
32
#include <netdb.h>
33
#include <netinet/in.h>
34
#include <netinet/ip.h>
35
#include <netinet/tcp.h>
36
#include <netinet/udp.h>
37
#include <sys/socket.h>
38
#include <unistd.h>
39

40
#if defined(DARWIN)
41
#include <dispatch/dispatch.h>
42
#include "osEok.h"
43
#else
44
#include <sys/epoll.h>
45
#endif
46
#endif
47

48
#ifndef INVALID_SOCKET
49
#define INVALID_SOCKET -1
50
#endif
51

52
typedef struct TdSocket {
53
#if SOCKET_WITH_LOCK
54
  TdThreadRwlock rwlock;
55
#endif
56
  int      refId;
57
  SocketFd fd;
58
} * TdSocketPtr, TdSocket;
59

60
typedef struct TdSocketServer {
61
#if SOCKET_WITH_LOCK
62
  TdThreadRwlock rwlock;
63
#endif
64
  int      refId;
65
  SocketFd fd;
66
} * TdSocketServerPtr, TdSocketServer;
67

68
typedef struct TdEpoll {
69
#if SOCKET_WITH_LOCK
70
  TdThreadRwlock rwlock;
71
#endif
72
  int     refId;
73
  EpollFd fd;
74
} * TdEpollPtr, TdEpoll;
75

76
int32_t taosCloseSocketNoCheck1(SocketFd fd) {
2,434✔
77
#ifdef WINDOWS
78
  int ret = closesocket(fd);
79
  if (ret == SOCKET_ERROR) {
80
    int errorCode = WSAGetLastError();
81
    return terrno = TAOS_SYSTEM_WINSOCKET_ERROR(errorCode);
82
  }
83
  return 0;
84
#else
85
  int32_t code = close(fd);
2,434✔
86
  if (-1 == code) {
2,434!
87
    terrno = TAOS_SYSTEM_ERROR(errno);
×
88
    return terrno;
×
89
  }
90
  return code;
2,434✔
91
#endif
92
}
93

94
int32_t taosCloseSocket(TdSocketPtr *ppSocket) {
2,434✔
95
  int32_t code;
96
  if (ppSocket == NULL || *ppSocket == NULL || (*ppSocket)->fd < 0) {
2,434!
97
    terrno = TSDB_CODE_INVALID_PARA;
×
98
    return terrno;
×
99
  }
100
  code = taosCloseSocketNoCheck1((*ppSocket)->fd);
2,434✔
101
  (*ppSocket)->fd = -1;
2,434✔
102
  taosMemoryFree(*ppSocket);
2,434✔
103

104
  return code;
2,434✔
105
}
106

107
int32_t taosSetSockOpt(TdSocketPtr pSocket, int32_t level, int32_t optname, void *optval, int32_t optlen) {
2,434✔
108
  if (pSocket == NULL || pSocket->fd < 0) {
2,434!
109
    terrno = TSDB_CODE_INVALID_PARA;
×
110
    return terrno;
×
111
  }
112

113
#ifdef WINDOWS
114
#ifdef TCP_KEEPCNT
115
  if (level == SOL_SOCKET && optname == TCP_KEEPCNT) {
116
    return 0;
117
  }
118
#endif
119

120
#ifdef TCP_KEEPIDLE
121
  if (level == SOL_TCP && optname == TCP_KEEPIDLE) {
122
    return 0;
123
  }
124
#endif
125

126
#ifdef TCP_KEEPINTVL
127
  if (level == SOL_TCP && optname == TCP_KEEPINTVL) {
128
    return 0;
129
  }
130
#endif
131

132
#ifdef TCP_KEEPCNT
133
  if (level == SOL_TCP && optname == TCP_KEEPCNT) {
134
    return 0;
135
  }
136
#endif
137

138
  int ret = setsockopt(pSocket->fd, level, optname, optval, optlen);
139
  if (ret == SOCKET_ERROR) {
140
    int errorCode = WSAGetLastError();
141
    return terrno = TAOS_SYSTEM_WINSOCKET_ERROR(errorCode);
142
  }
143
#else
144
  int32_t code = setsockopt(pSocket->fd, level, optname, optval, (int)optlen);
2,434✔
145
  if (-1 == code) {
2,434!
146
    terrno = TAOS_SYSTEM_ERROR(errno);
×
147
    return terrno;
×
148
  }
149
  return 0;
2,434✔
150
#endif
151
}
152

153
const char *taosInetNtoa(struct in_addr ipInt, char *dstStr, int32_t len) {
×
154
  const char *r = inet_ntop(AF_INET, &ipInt, dstStr, len);
×
155
  if (NULL == r) {
×
156
    terrno = TAOS_SYSTEM_ERROR(errno);
×
157
  }
158

159
  return r;
×
160
}
161

162
#ifndef SIGPIPE
163
#define SIGPIPE EPIPE
164
#endif
165

166
#define TCP_CONN_TIMEOUT 3000  // conn timeout
167

168
bool taosValidIpAndPort(uint32_t ip, uint16_t port) {
2,434✔
169
  struct sockaddr_in serverAdd;
170
  SocketFd           fd;
171
  int32_t            reuse;
172
  int32_t            code = 0;
2,434✔
173

174
  // printf("open tcp server socket:0x%x:%hu", ip, port);
175

176
  bzero((char *)&serverAdd, sizeof(serverAdd));
2,434✔
177
  serverAdd.sin_family = AF_INET;
2,434✔
178
#ifdef WINDOWS
179
  serverAdd.sin_addr.s_addr = INADDR_ANY;
180
#else
181
  serverAdd.sin_addr.s_addr = ip;
2,434✔
182
#endif
183
  serverAdd.sin_port = (uint16_t)htons(port);
2,434✔
184

185
  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2,434✔
186
  if (-1 == fd) {  // exception
2,434!
187
    terrno = TAOS_SYSTEM_ERROR(errno);
×
188
    return false;
×
189
  }
190

191
  TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket));
2,434✔
192
  if (pSocket == NULL) {
2,434!
193
    TAOS_SKIP_ERROR(taosCloseSocketNoCheck1(fd));
×
194
    return false;
×
195
  }
196
  pSocket->refId = 0;
2,434✔
197
  pSocket->fd = fd;
2,434✔
198

199
  /* set REUSEADDR option, so the portnumber can be re-used */
200
  reuse = 1;
2,434✔
201
  if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) {
2,434!
202
    TAOS_SKIP_ERROR(taosCloseSocket(&pSocket));
×
203
    return false;
×
204
  }
205

206
  /* bind socket to server address */
207
  if (-1 == bind(pSocket->fd, (struct sockaddr *)&serverAdd, sizeof(serverAdd))) {
2,434!
208
    terrno = TAOS_SYSTEM_ERROR(errno);
×
209
    TAOS_SKIP_ERROR(taosCloseSocket(&pSocket));
×
210
    return false;
×
211
  }
212

213
  TAOS_SKIP_ERROR(taosCloseSocket(&pSocket));
2,434✔
214

215
  return true;
2,434✔
216
}
217

218
int32_t taosBlockSIGPIPE() {
771,975✔
219
#ifdef WINDOWS
220
  return 0;
221
#else
222
  sigset_t signal_mask;
223
  (void)sigemptyset(&signal_mask);
771,975✔
224
  (void)sigaddset(&signal_mask, SIGPIPE);
771,982✔
225
  int32_t rc = pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
772,007✔
226
  if (rc != 0) {
772,249!
227
    terrno = TAOS_SYSTEM_ERROR(rc);
×
228
    return terrno;
×
229
  }
230

231
  return 0;
772,249✔
232
#endif
233
}
234

235
int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t *ip) {
197,550✔
236
  OS_PARAM_CHECK(fqdn);
197,550!
237
  OS_PARAM_CHECK(ip);
197,550!
238
#ifdef WINDOWS
239
  // Initialize Winsock
240
  WSADATA wsaData;
241
  int     iResult;
242
  iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
243
  if (iResult != 0) {
244
    return TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
245
  }
246
#endif
247

248
#if defined(LINUX)
249
  struct addrinfo hints = {0};
197,550✔
250
  hints.ai_family = AF_INET;
197,550✔
251
  hints.ai_socktype = SOCK_STREAM;
197,550✔
252

253
  struct addrinfo *result = NULL;
197,550✔
254
  bool             inRetry = false;
197,550✔
255

256
  while (true) {
×
257
    int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
197,550✔
258
    if (ret) {
197,608✔
259
      if (EAI_AGAIN == ret && !inRetry) {
5!
260
        inRetry = true;
×
261
        continue;
×
262
      } else if (EAI_SYSTEM == ret) {
5!
263
        terrno = TAOS_SYSTEM_ERROR(errno);
×
264
        return terrno;
197,618✔
265
      }
266

267
      terrno = TAOS_SYSTEM_ERROR(errno);
5✔
268
      return terrno;
5✔
269
    }
270

271
    struct sockaddr    *sa = result->ai_addr;
197,603✔
272
    struct sockaddr_in *si = (struct sockaddr_in *)sa;
197,603✔
273
    struct in_addr      ia = si->sin_addr;
197,603✔
274

275
    *ip = ia.s_addr;
197,603✔
276

277
    freeaddrinfo(result);
197,603✔
278

279
    return 0;
197,606✔
280
  }
281
#else
282
  struct addrinfo hints = {0};
283
  hints.ai_family = AF_INET;
284
  hints.ai_socktype = SOCK_STREAM;
285

286
  struct addrinfo *result = NULL;
287

288
  int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
289
  if (result) {
290
    struct sockaddr    *sa = result->ai_addr;
291
    struct sockaddr_in *si = (struct sockaddr_in *)sa;
292
    struct in_addr      ia = si->sin_addr;
293
    *ip = ia.s_addr;
294
    freeaddrinfo(result);
295
    return 0;
296
  } else {
297
#ifdef EAI_SYSTEM
298
    if (ret == EAI_SYSTEM) {
299
      // printf("failed to get the ip address, fqdn:%s, errno:%d, since:%s", fqdn, errno, strerror(errno));
300
    } else {
301
      // printf("failed to get the ip address, fqdn:%s, ret:%d, since:%s", fqdn, ret, gai_strerror(ret));
302
    }
303
#else
304
    // printf("failed to get the ip address, fqdn:%s, ret:%d, since:%s", fqdn, ret, gai_strerror(ret));
305
#endif
306

307
    *ip = 0xFFFFFFFF;
308
    return TSDB_CODE_RPC_FQDN_ERROR;
309
  }
310
#endif
311
}
312

313
int32_t taosGetFqdn(char *fqdn) {
39,683✔
314
  OS_PARAM_CHECK(fqdn);
39,683!
315
#ifdef WINDOWS
316
  // Initialize Winsock
317
  WSADATA wsaData;
318
  int     iResult;
319
  iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
320
  if (iResult != 0) {
321
    // printf("WSAStartup failed: %d\n", iResult);
322
    return TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
323
  }
324
#endif
325
  char hostname[1024];
326
  hostname[1023] = '\0';
39,683✔
327
  int32_t code = taosGetlocalhostname(hostname, 1023);
39,683✔
328
  if (code) {
39,683!
329
    return code;
×
330
  }
331

332
#ifdef __APPLE__
333
  // on macosx, hostname -f has the form of xxx.local
334
  // which will block getaddrinfo for a few seconds if AI_CANONNAME is set
335
  // thus, we choose AF_INET (ipv4 for the moment) to make getaddrinfo return
336
  // immediately
337
  // hints.ai_family = AF_INET;
338
  tstrncpy(fqdn, hostname, TD_FQDN_LEN);
339
  tstrncpy(fqdn + strlen(hostname), ".local", TD_FQDN_LEN - strlen(hostname));
340
#else  // linux
341

342
#endif  // linux
343

344
#if defined(LINUX)
345

346
  struct addrinfo  hints = {0};
39,683✔
347
  struct addrinfo *result = NULL;
39,683✔
348
  hints.ai_flags = AI_CANONNAME;
39,683✔
349

350
  while (true) {
×
351
    int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
39,683✔
352
    if (ret) {
39,681!
353
      if (EAI_AGAIN == ret) {
×
354
        continue;
×
355
      } else if (EAI_SYSTEM == ret) {
×
356
        terrno = TAOS_SYSTEM_ERROR(errno);
×
357
        return terrno;
×
358
      }
359

360
      terrno = TAOS_SYSTEM_ERROR(ret);
×
361
      return terrno;
×
362
    }
363

364
    break;
39,681✔
365
  }
366

367
  tstrncpy(fqdn, result->ai_canonname, TD_FQDN_LEN);
39,681✔
368

369
  freeaddrinfo(result);
39,681✔
370

371
#elif WINDOWS
372
  struct addrinfo  hints = {0};
373
  struct addrinfo *result = NULL;
374
  hints.ai_flags = AI_CANONNAME;
375

376
  int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
377
  if (!result) {
378
    // fprintf(stderr, "failed to get fqdn, code:%d, hostname:%s, reason:%s\n", ret, hostname, gai_strerror(ret));
379
    return TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
380
  }
381
  tstrncpy(fqdn, result->ai_canonname, TD_FQDN_LEN);
382
  freeaddrinfo(result);
383

384
#endif
385

386
  return 0;
39,681✔
387
}
388

389
void tinet_ntoa(char *ipstr, uint32_t ip) {
24,248✔
390
  if (ipstr == NULL) {
24,248!
391
    return;
×
392
  }
393
  (void)snprintf(ipstr, TD_IP_LEN, "%d.%d.%d.%d", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24);
24,248✔
394
}
395

396
int32_t taosIgnSIGPIPE() {
2,436✔
397
  sighandler_t h = signal(SIGPIPE, SIG_IGN);
2,436✔
398
  if (SIG_ERR == h) {
2,436!
399
    terrno = TAOS_SYSTEM_ERROR(errno);
×
400
    return terrno;
×
401
  }
402

403
  return 0;
2,436✔
404
}
405

406
/*
407
 * Set TCP connection timeout per-socket level.
408
 * ref [https://github.com/libuv/help/issues/54]
409
 */
410
int32_t taosCreateSocketWithTimeout(uint32_t timeout) {
168,111✔
411
#if defined(WINDOWS)
412
  SOCKET fd;
413
#else
414
  int fd;
415
#endif
416

417
  if ((fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
168,111!
418
    terrno = TAOS_SYSTEM_ERROR(errno);
×
419
    return terrno;
×
420
  }
421

422
#if defined(WINDOWS)
423
  if (0 != setsockopt(fd, IPPROTO_TCP, TCP_MAXRT, (char *)&timeout, sizeof(timeout))) {
424
    taosCloseSocketNoCheck1(fd);
425
    return TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
426
  }
427
#elif defined(_TD_DARWIN_64)
428
  // invalid config
429
  // uint32_t conn_timeout_ms = timeout * 1000;
430
  // if (0 != setsockopt(fd, IPPROTO_TCP, TCP_CONNECTIONTIMEOUT, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms))) {
431
  //  taosCloseSocketNoCheck1(fd);
432
  //  return -1;
433
  //}
434
#else  // Linux like systems
435
  uint32_t conn_timeout_ms = timeout;
168,071✔
436
  if (-1 == setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms))) {
168,071!
437
    terrno = TAOS_SYSTEM_ERROR(errno);
×
438
    TAOS_SKIP_ERROR(taosCloseSocketNoCheck1(fd));
×
439
    return terrno;
×
440
  }
441
#endif
442

443
  return (int)fd;
168,061✔
444
}
445

446
int32_t taosWinSocketInit() {
×
447
#ifdef WINDOWS
448
  static int8_t flag = 0;
449
  if (atomic_val_compare_exchange_8(&flag, 0, 1) == 0) {
450
    WORD    wVersionRequested;
451
    WSADATA wsaData;
452
    wVersionRequested = MAKEWORD(1, 1);
453
    if (WSAStartup(wVersionRequested, &wsaData) != 0) {
454
      atomic_store_8(&flag, 0);
455
      int errorCode = WSAGetLastError();
456
      return terrno = TAOS_SYSTEM_WINSOCKET_ERROR(errorCode);
457
    }
458
  }
459
  return 0;
460
#else
461
#endif
462
  return 0;
×
463
}
464

465
uint64_t taosHton64(uint64_t val) {
398,257,437✔
466
#if defined(WINDOWS) || defined(DARWIN)
467
  return ((val & 0x00000000000000ff) << 7 * 8) | ((val & 0x000000000000ff00) << 5 * 8) |
468
         ((val & 0x0000000000ff0000) << 3 * 8) | ((val & 0x00000000ff000000) << 1 * 8) |
469
         ((val & 0x000000ff00000000) >> 1 * 8) | ((val & 0x0000ff0000000000) >> 3 * 8) |
470
         ((val & 0x00ff000000000000) >> 5 * 8) | ((val & 0xff00000000000000) >> 7 * 8);
471
#else
472
  if (__BYTE_ORDER == __LITTLE_ENDIAN) {
473
    return (((uint64_t)htonl((int)((val << 32) >> 32))) << 32) | (unsigned int)htonl((int)(val >> 32));
398,257,437✔
474
  } else if (__BYTE_ORDER == __BIG_ENDIAN) {
475
    return val;
476
  }
477
#endif
478
}
479

480
uint64_t taosNtoh64(uint64_t val) {
7,740,583✔
481
#if defined(WINDOWS) || defined(DARWIN)
482
  return taosHton64(val);
483
#else
484
  if (__BYTE_ORDER == __LITTLE_ENDIAN) {
485
    return (((uint64_t)htonl((int)((val << 32) >> 32))) << 32) | (unsigned int)htonl((int)(val >> 32));
7,740,583✔
486
  } else if (__BYTE_ORDER == __BIG_ENDIAN) {
487
    return val;
488
  }
489
#endif
490
}
491

492
int32_t taosSetSockOpt2(int32_t fd) {
78,109,109✔
493
#if defined(WINDOWS) || defined(DARWIN)
494
  return 0;
495
#else
496
  int32_t ret = setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){1}, sizeof(int));
78,109,109✔
497
  if (ret < 0) {
78,222,697!
498
    terrno = TAOS_SYSTEM_ERROR(errno);
×
499
    return terrno;
×
500
  } else {
501
    return 0;
78,241,297✔
502
  }
503
#endif
504
  return 0;
505
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc