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

taosdata / TDengine / #4908

30 Dec 2025 10:52AM UTC coverage: 65.386% (-0.2%) from 65.541%
#4908

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

1330 existing lines in 113 files now uncovered.

193461 of 295877 relevant lines covered (65.39%)

115765274.47 hits per line

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

56.87
/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
#elif !defined(TD_ASTRA)
44
#include <sys/epoll.h>
45
#endif
46
#endif
47

48
#ifdef TD_ASTRA
49
#ifndef __BYTE_ORDER
50
#define __BYTE_ORDER _BYTE_ORDER
51
#endif
52

53
#ifndef __BIG_ENDIAN
54
#define __BIG_ENDIAN _BIG_ENDIAN
55
#endif
56

57
#ifndef __LITTLE_ENDIAN
58
#define __LITTLE_ENDIAN _LITTLE_ENDIAN
59
#endif
60
#endif // TD_ASTRA
61

62
#ifndef INVALID_SOCKET
63
#define INVALID_SOCKET -1
64
#endif
65

66
typedef struct TdSocket {
67
#if SOCKET_WITH_LOCK
68
  TdThreadRwlock rwlock;
69
#endif
70
  int      refId;
71
  SocketFd fd;
72
} * TdSocketPtr, TdSocket;
73

74
typedef struct TdSocketServer {
75
#if SOCKET_WITH_LOCK
76
  TdThreadRwlock rwlock;
77
#endif
78
  int      refId;
79
  SocketFd fd;
80
} * TdSocketServerPtr, TdSocketServer;
81

82
typedef struct TdEpoll {
83
#if SOCKET_WITH_LOCK
84
  TdThreadRwlock rwlock;
85
#endif
86
  int     refId;
87
  EpollFd fd;
88
} * TdEpollPtr, TdEpoll;
89

90
int32_t taosCloseSocketNoCheck1(SocketFd fd) {
553,429✔
91
#ifdef WINDOWS
92
  int ret = closesocket(fd);
93
  if (ret == SOCKET_ERROR) {
94
    int errorCode = WSAGetLastError();
95
    return terrno = TAOS_SYSTEM_WINSOCKET_ERROR(errorCode);
96
  }
97
  return 0;
98
#else
99
  int32_t code = close(fd);
553,429✔
100
  if (-1 == code) {
553,429✔
101
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
102
    return terrno;
×
103
  }
104
  return code;
553,429✔
105
#endif
106
}
107

108
int32_t taosCloseSocket(TdSocketPtr *ppSocket) {
553,429✔
109
  int32_t code;
110
  if (ppSocket == NULL || *ppSocket == NULL || (*ppSocket)->fd < 0) {
553,429✔
111
    terrno = TSDB_CODE_INVALID_PARA;
×
112
    return terrno;
×
113
  }
114
  code = taosCloseSocketNoCheck1((*ppSocket)->fd);
553,429✔
115
  (*ppSocket)->fd = -1;
553,429✔
116
  taosMemoryFree(*ppSocket);
553,429✔
117

118
  return code;
553,429✔
119
}
120

121
int32_t taosSetSockOpt(TdSocketPtr pSocket, int32_t level, int32_t optname, void *optval, int32_t optlen) {
553,429✔
122
  if (pSocket == NULL || pSocket->fd < 0) {
553,429✔
123
    terrno = TSDB_CODE_INVALID_PARA;
×
124
    return terrno;
×
125
  }
126

127
#ifdef WINDOWS
128
#ifdef TCP_KEEPCNT
129
  if (level == SOL_SOCKET && optname == TCP_KEEPCNT) {
130
    return 0;
131
  }
132
#endif
133

134
#ifdef TCP_KEEPIDLE
135
  if (level == SOL_TCP && optname == TCP_KEEPIDLE) {
136
    return 0;
137
  }
138
#endif
139

140
#ifdef TCP_KEEPINTVL
141
  if (level == SOL_TCP && optname == TCP_KEEPINTVL) {
142
    return 0;
143
  }
144
#endif
145

146
#ifdef TCP_KEEPCNT
147
  if (level == SOL_TCP && optname == TCP_KEEPCNT) {
148
    return 0;
149
  }
150
#endif
151

152
  int ret = setsockopt(pSocket->fd, level, optname, optval, optlen);
153
  if (ret == SOCKET_ERROR) {
154
    int errorCode = WSAGetLastError();
155
    return terrno = TAOS_SYSTEM_WINSOCKET_ERROR(errorCode);
156
  }
157
#else
158
  int32_t code = setsockopt(pSocket->fd, level, optname, optval, (int)optlen);
553,429✔
159
  if (-1 == code) {
553,429✔
160
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
161
    return terrno;
×
162
  }
163
  return 0;
553,429✔
164
#endif
165
}
166

167
const char *taosInetNtop(struct in_addr ipInt, char *dstStr, int32_t len) {
×
168
  const char *r = inet_ntop(AF_INET, &ipInt, dstStr, len);
×
169
  if (NULL == r) {
×
170
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
171
  }
172

173
  return r;
×
174
}
175

176
#ifndef SIGPIPE
177
#define SIGPIPE EPIPE
178
#endif
179

180
#define TCP_CONN_TIMEOUT 3000  // conn timeout
181

182
int8_t taosValidIpAndPort(uint32_t ip, uint16_t port) {
553,429✔
183
  struct sockaddr_in serverAdd;
553,171✔
184
  SocketFd           fd;
185
  int32_t            reuse;
553,171✔
186
  int32_t            code = 0;
553,429✔
187

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

190
  bzero((char *)&serverAdd, sizeof(serverAdd));
553,429✔
191
  serverAdd.sin_family = AF_INET;
553,429✔
192
#ifdef WINDOWS
193
  serverAdd.sin_addr.s_addr = INADDR_ANY;
194
#else
195
  serverAdd.sin_addr.s_addr = ip;
553,429✔
196
#endif
197
  serverAdd.sin_port = (uint16_t)htons(port);
553,429✔
198

199
  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
553,429✔
200
  if (-1 == fd) {  // exception
553,429✔
201
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
202
    return 0;
×
203
  }
204

205
  TdSocketPtr pSocket = (TdSocketPtr)taosMemoryMalloc(sizeof(TdSocket));
553,429✔
206
  if (pSocket == NULL) {
553,429✔
207
    TAOS_SKIP_ERROR(taosCloseSocketNoCheck1(fd));
×
208
    return 0;
×
209
  }
210
  pSocket->refId = 0;
553,429✔
211
  pSocket->fd = fd;
553,429✔
212

213
  /* set REUSEADDR option, so the portnumber can be re-used */
214
  reuse = 1;
553,429✔
215
  if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) {
553,429✔
216
    TAOS_SKIP_ERROR(taosCloseSocket(&pSocket));
×
217
    return 0;
×
218
  }
219

220
  /* bind socket to server address */
221
  if (-1 == bind(pSocket->fd, (struct sockaddr *)&serverAdd, sizeof(serverAdd))) {
553,429✔
222
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
29✔
223
    TAOS_SKIP_ERROR(taosCloseSocket(&pSocket));
29✔
224
    return 0;
29✔
225
  }
226

227
  TAOS_SKIP_ERROR(taosCloseSocket(&pSocket));
553,400✔
228

229
  return 1;
553,400✔
230
}
231

232
int32_t taosBlockSIGPIPE() {
202,351,479✔
233
#ifdef WINDOWS
234
  return 0;
235
#else
236
  sigset_t signal_mask;
201,483,262✔
237
  (void)sigemptyset(&signal_mask);
202,367,587✔
238
  (void)sigaddset(&signal_mask, SIGPIPE);
202,363,778✔
239
  int32_t rc = pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
202,355,822✔
240
  if (rc != 0) {
202,354,481✔
241
    terrno = TAOS_SYSTEM_ERROR(rc);
×
242
    return terrno;
×
243
  }
244

245
  return 0;
202,354,481✔
246
#endif
247
}
248

249
int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t *ip) {
238✔
250
  int32_t code = 0;
238✔
251
  OS_PARAM_CHECK(fqdn);
238✔
252
  OS_PARAM_CHECK(ip);
238✔
253
  int64_t limitMs = 1000;
238✔
254
  int64_t st = taosGetTimestampMs(), cost = 0;
238✔
255
#ifdef WINDOWS
256
  // Initialize Winsock
257
  WSADATA wsaData;
258
  int     iResult;
259
  iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
260
  if (iResult != 0) {
261
    code = TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
262
    goto _err;
263
  }
264
#endif
265

266
#if defined(LINUX)
267
  struct addrinfo hints = {0};
238✔
268
  hints.ai_family = AF_INET;
238✔
269
  hints.ai_socktype = SOCK_STREAM;
238✔
270

271
  struct addrinfo *result = NULL;
238✔
272
  bool             inRetry = false;
238✔
273

274
  while (true) {
×
275
    int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
238✔
276
    if (ret) {
238✔
277
      if (EAI_AGAIN == ret && !inRetry) {
×
278
        inRetry = true;
×
279
        continue;
×
280
      } else if (EAI_SYSTEM == ret) {
×
281
        code = TAOS_SYSTEM_ERROR(ERRNO);
×
282
        goto _err;
×
283
      }
284

285
      code = TAOS_SYSTEM_ERROR(ERRNO);
×
286
      goto _err;
×
287
    }
288

289
    struct sockaddr    *sa = result->ai_addr;
238✔
290
    struct sockaddr_in *si = (struct sockaddr_in *)sa;
238✔
291
    struct in_addr      ia = si->sin_addr;
238✔
292

293
    *ip = ia.s_addr;
238✔
294

295
    freeaddrinfo(result);
238✔
296
    goto _err;
238✔
297
  }
298
#else
299
  struct addrinfo hints = {0};
300
  hints.ai_family = AF_INET;
301
  hints.ai_socktype = SOCK_STREAM;
302

303
  struct addrinfo *result = NULL;
304

305
  int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
306
  if (result) {
307
    struct sockaddr    *sa = result->ai_addr;
308
    struct sockaddr_in *si = (struct sockaddr_in *)sa;
309
    struct in_addr      ia = si->sin_addr;
310
    *ip = ia.s_addr;
311
    freeaddrinfo(result);
312
    goto _err;
313
  } else {
314
#ifdef EAI_SYSTEM
315
    if (ret == EAI_SYSTEM) {
316
      // printf("failed to get the ip address, fqdn:%s, errno:%d, since:%s", fqdn, ERRNO, strerror(ERRNO));
317
    } else {
318
      // printf("failed to get the ip address, fqdn:%s, ret:%d, since:%s", fqdn, ret, gai_strerror(ret));
319
    }
320
#else
321
    // printf("failed to get the ip address, fqdn:%s, ret:%d, since:%s", fqdn, ret, gai_strerror(ret));
322
#endif
323

324
    *ip = 0xFFFFFFFF;
325
    code = TSDB_CODE_RPC_FQDN_ERROR;
326
    goto _err;
327
  }
328
#endif
329
_err:
238✔
330
  cost = taosGetTimestampMs() - st;
238✔
331
  if (cost >= limitMs) {
238✔
332
    uWarn("get ip from fqdn:%s, cost:%" PRId64 "ms", fqdn, cost);
×
333
  }
334
  return code;
238✔
335
}
336
int32_t taosGetIpv6FromFqdn(const char *fqdn, SIpAddr *pAddr) {
×
337
  int32_t code = 0;
×
338
  OS_PARAM_CHECK(fqdn);
×
339
  int64_t limitMs = 1000;
×
340
  int64_t st = taosGetTimestampMs(), cost = 0;
×
341
#ifdef WINDOWS
342
  // Initialize Winsock
343
  WSADATA wsaData;
344
  int     iResult;
345
  iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
346
  if (iResult != 0) {
347
    code = TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
348
    goto _err;
349
  }
350
#endif
351

352
#if defined(LINUX)
353
  struct addrinfo hints = {0};
×
354
  hints.ai_family = AF_UNSPEC;
×
355
  hints.ai_socktype = SOCK_STREAM;
×
356

357
  struct addrinfo *result = NULL;
×
358
  bool             inRetry = false;
×
359

360
  char ipStr[INET6_ADDRSTRLEN];
361
  while (true) {
×
362
    int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
×
363
    if (ret) {
×
364
      if (EAI_AGAIN == ret && !inRetry) {
×
365
        inRetry = true;
×
366
        continue;
×
367
      } else if (EAI_SYSTEM == ret) {
×
368
        code = TAOS_SYSTEM_ERROR(ERRNO);
×
369
        goto _err;
×
370
      }
371

372
      code = TAOS_SYSTEM_ERROR(ERRNO);
×
373
      goto _err;
×
374
    }
375

376
    if (result->ai_family == AF_INET6) {
×
377
      struct sockaddr_in6 *p6 = (struct sockaddr_in6 *)result->ai_addr;
×
378

379
      const char *t = inet_ntop(AF_INET6, &p6->sin6_addr, pAddr->ipv6, sizeof(pAddr->ipv6));
×
380
      TAOS_UNUSED(t);
381

382
      pAddr->type = 1;
×
383
    } else if (result->ai_family == AF_INET) {
×
384
      struct sockaddr_in *p4 = (struct sockaddr_in *)result->ai_addr;
×
385

386
      const char *t = inet_ntop(AF_INET, &p4->sin_addr, pAddr->ipv4, sizeof(pAddr->ipv4));
×
387
      TAOS_UNUSED(t);
388

389
      pAddr->type = 0;
×
390
    } else {
391
      code = TSDB_CODE_RPC_FQDN_ERROR;
×
392
      goto _err;
×
393
    }
394

395
    freeaddrinfo(result);
×
396
    goto _err;
×
397
  }
398
#else
399
  struct addrinfo hints = {0};
400
  hints.ai_family = AF_UNSPEC;
401
  hints.ai_socktype = SOCK_STREAM;
402

403
  struct addrinfo *result = NULL;
404

405
  int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
406
  if (result) {
407
    if (result->ai_family == AF_INET6) {
408
      struct sockaddr_in6 *p6 = (struct sockaddr_in6 *)result->ai_addr;
409
      inet_ntop(AF_INET6, &p6->sin6_addr, pAddr->ipv6, sizeof(pAddr->ipv6));
410
      pAddr->type = 1;
411
    } else if (result->ai_family == AF_INET) {
412
      struct sockaddr_in *p4 = (struct sockaddr_in *)result->ai_addr;
413
      inet_ntop(AF_INET, &p4->sin_addr, pAddr->ipv4, sizeof(pAddr->ipv4));
414
      pAddr->type = 0;
415
    } else {
416
      code = TSDB_CODE_RPC_FQDN_ERROR;
417
      goto _err;
418
    }
419
    freeaddrinfo(result);
420
    goto _err;
421
  } else {
422
#ifdef EAI_SYSTEM
423
    if (ret == EAI_SYSTEM) {
424
      // printf("failed to get the ip address, fqdn:%s, errno:%d, since:%s", fqdn, ERRNO, strerror(ERRNO));
425
    } else {
426
      // printf("failed to get the ip address, fqdn:%s, ret:%d, since:%s", fqdn, ret, gai_strerror(ret));
427
    }
428
#else
429
    // printf("failed to get the ip address, fqdn:%s, ret:%d, since:%s", fqdn, ret, gai_strerror(ret));
430
#endif
431
  }
432
#endif
433
_err:
×
434
  cost = taosGetTimestampMs() - st;
×
435
  if (cost >= limitMs) {
×
436
    uWarn("get ip from fqdn:%s, cost:%" PRId64 "ms", fqdn, cost);
×
437
  }
438

439
#ifdef WINDOWS
440
  WSACleanup();
441
#endif
442
  return code;
×
443
}
444

445
int32_t taosGetIp4FromFqdn(const char *fqdn, SIpAddr *pAddr) {
45,290,925✔
446
  int32_t code = 0;
45,290,925✔
447
  OS_PARAM_CHECK(fqdn);
45,290,925✔
448
  int64_t limitMs = 1000;
45,290,925✔
449
  int64_t st = taosGetTimestampMs(), cost = 0;
45,292,507✔
450
#ifdef WINDOWS
451
  // Initialize Winsock
452
  WSADATA wsaData;
453
  int     iResult;
454
  iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
455
  if (iResult != 0) {
456
    code = TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
457
    goto _err;
458
  }
459
#endif
460

461
#if defined(LINUX)
462
  struct addrinfo hints = {0};
45,292,507✔
463
  hints.ai_family = AF_INET;
45,292,063✔
464
  hints.ai_socktype = SOCK_STREAM;
45,292,063✔
465

466
  struct addrinfo *result = NULL;
45,292,063✔
467
  bool             inRetry = false;
45,283,608✔
468

469
  char ipStr[INET6_ADDRSTRLEN];
470
  while (true) {
×
471
    int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
45,283,608✔
472
    if (ret) {
45,294,210✔
473
      if (EAI_AGAIN == ret && !inRetry) {
1,551✔
474
        inRetry = true;
×
475
        continue;
×
476
      } else if (EAI_SYSTEM == ret) {
1,551✔
477
        code = TAOS_SYSTEM_ERROR(ERRNO);
×
478
        goto _err;
×
479
      }
480

481
      code = TAOS_SYSTEM_ERROR(ERRNO);
1,551✔
482
      goto _err;
1,551✔
483
    }
484

485
    if (result->ai_family == AF_INET) {
45,292,659✔
486
      struct sockaddr_in *p4 = (struct sockaddr_in *)result->ai_addr;
45,291,574✔
487
      const char         *t = inet_ntop(AF_INET, &p4->sin_addr, pAddr->ipv4, sizeof(pAddr->ipv4));
45,293,101✔
488
      TAOS_UNUSED(t);
489

490
      pAddr->type = 0;
45,292,717✔
491
    } else {
492
      code = TSDB_CODE_RPC_FQDN_ERROR;
×
493
      goto _err;
×
494
    }
495

496
    freeaddrinfo(result);
45,292,155✔
497
    goto _err;
45,292,371✔
498
  }
499
#else
500
  struct addrinfo hints = {0};
501
  hints.ai_family = AF_INET;
502
  hints.ai_socktype = SOCK_STREAM;
503

504
  struct addrinfo *result = NULL;
505

506
  int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
507
  if (result) {
508
    struct sockaddr_in *p4 = (struct sockaddr_in *)result->ai_addr;
509
    inet_ntop(AF_INET, &p4->sin_addr, pAddr->ipv4, sizeof(pAddr->ipv4));
510
    freeaddrinfo(result);
511
    pAddr->type = 0;
512
    goto _err;
513
  } else {
514
#ifdef EAI_SYSTEM
515
    if (ret == EAI_SYSTEM) {
516
      // printf("failed to get the ip address, fqdn:%s, errno:%d, since:%s", fqdn, ERRNO, strerror(ERRNO));
517
    } else {
518
      // printf("failed to get the ip address, fqdn:%s, ret:%d, since:%s", fqdn, ret, gai_strerror(ret));
519
    }
520
    code = TSDB_CODE_RPC_FQDN_ERROR;
521
#else
522
    // printf("failed to get the ip address, fqdn:%s, ret:%d, since:%s", fqdn, ret, gai_strerror(ret));
523
#endif
524
  }
525
#endif
526

527
_err:
45,293,818✔
528
  cost = taosGetTimestampMs() - st;
45,294,606✔
529
  if (cost >= limitMs) {
45,294,606✔
UNCOV
530
    uWarn("get ip from fqdn:%s, cost:%" PRId64 "ms", fqdn, cost);
×
531
  }
532
#ifdef WINDOWS
533
  WSACleanup();
534
#endif
535
  return code;
45,293,404✔
536
}
537
int32_t taosGetIpFromFqdn(int8_t enableIpv6, const char *fqdn, SIpAddr *addr) {
45,291,670✔
538
  int32_t  code = 0;
45,291,670✔
539
  if (enableIpv6) {
45,291,670✔
540
    code = taosGetIpv6FromFqdn(fqdn, addr);
×
541
  } else {
542
    code = taosGetIp4FromFqdn(fqdn, addr);
45,291,670✔
543
  }
544
  return code;
45,293,470✔
545
}
546

547
int8_t taosIpAddrIsEqual(SIpAddr *ip1, SIpAddr *ip2) {
2,147,483,647✔
548
  if (ip1->type == ip2->type) {
2,147,483,647✔
549
    if (ip1->type == 0) {
2,147,483,647✔
550
      return (strcmp(ip1->ipv4, ip2->ipv4) == 0 ? 1 : 0);
2,147,483,647✔
551
    } else {
552
      return (strcmp(ip1->ipv6, ip2->ipv6) == 0 ? 1 : 0);
7,688✔
553
    }
554
  }
555
  return 0;
×
556
}
557

558
int32_t taosGetFqdnWithTimeCost(char *fqdn, int64_t *timeoutMs) {
2,293,347✔
559
  int32_t code = 0;
2,293,347✔
560
  int64_t startMs = taosGetTimestampMs();
2,293,347✔
561

562
  code = taosGetFqdn(fqdn);
2,293,347✔
563
  int64_t cost = taosGetTimestampMs() - startMs;
2,293,347✔
564
  if (timeoutMs != NULL) {
2,293,347✔
565
    *timeoutMs = cost;
2,293,347✔
566
  }
567
  return code;
2,293,347✔
568
}
569
int32_t taosGetFqdn(char *fqdn) {
15,847,599✔
570
  OS_PARAM_CHECK(fqdn);
15,847,599✔
571
#ifdef WINDOWS
572
  // Initialize Winsock
573
  WSADATA wsaData;
574
  int     iResult;
575
  iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
576
  if (iResult != 0) {
577
    // printf("WSAStartup failed: %d\n", iResult);
578
    return TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
579
  }
580
#endif
581
  char hostname[1024];
15,730,599✔
582
  hostname[1023] = '\0';
15,847,599✔
583
  int32_t code = taosGetlocalhostname(hostname, 1023);
15,847,599✔
584
  if (code) {
15,847,599✔
585
    return code;
×
586
  }
587

588
#ifdef __APPLE__
589
  // on macosx, hostname -f has the form of xxx.local
590
  // which will block getaddrinfo for a few seconds if AI_CANONNAME is set
591
  // thus, we choose AF_INET (ipv4 for the moment) to make getaddrinfo return
592
  // immediately
593
  // hints.ai_family = AF_INET;
594
  tstrncpy(fqdn, hostname, TD_FQDN_LEN);
595
  tstrncpy(fqdn + strlen(hostname), ".local", TD_FQDN_LEN - strlen(hostname));
596
#else  // linux
597

598
#endif  // linux
599
#if defined(TD_ASTRA)
600
  tstrncpy(fqdn, hostname, TD_FQDN_LEN);  
601
#elif defined(LINUX)
602

603
  struct addrinfo  hints = {0};
15,847,599✔
604
  struct addrinfo *result = NULL;
15,847,599✔
605
  hints.ai_flags = AI_CANONNAME;
15,847,599✔
606

607
  while (true) {
×
608
    int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
15,847,599✔
609
    if (ret) {
15,847,599✔
610
      if (EAI_AGAIN == ret) {
×
611
        continue;
×
612
      } else if (EAI_SYSTEM == ret) {
×
613
        terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
614
        return terrno;
×
615
      }
616

617
      terrno = TAOS_SYSTEM_ERROR(ret);
×
618
      return terrno;
×
619
    }
620

621
    break;
15,847,599✔
622
  }
623

624
  tstrncpy(fqdn, result->ai_canonname, TD_FQDN_LEN);
15,847,599✔
625

626
  freeaddrinfo(result);
15,847,599✔
627

628
#elif WINDOWS
629
  struct addrinfo  hints = {0};
630
  struct addrinfo *result = NULL;
631
  hints.ai_flags = AI_CANONNAME;
632

633
  int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
634
  if (!result) {
635
    // fprintf(stderr, "failed to get fqdn, code:%d, hostname:%s, reason:%s\n", ret, hostname, gai_strerror(ret));
636
    return TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
637
  }
638
  tstrncpy(fqdn, result->ai_canonname, TD_FQDN_LEN);
639
  freeaddrinfo(result);
640

641
#endif
642

643
  return 0;
15,847,599✔
644
}
645

646
void taosInetNtoa(char *ipstr, uint32_t ip) {
238✔
647
  if (ipstr == NULL) {
238✔
648
    return;
×
649
  }
650
  unsigned char *bytes = (unsigned char *) &ip;
238✔
651
  (void)snprintf(ipstr, TD_IP_LEN, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
238✔
652
}
653

654
uint32_t taosInetAddr(const char *ipstr){
×
655
  if (ipstr == NULL) {
×
656
    return 0;
×
657
  }
658
  return inet_addr(ipstr);
×
659
}
660

661
int32_t taosIgnSIGPIPE() {
563,046✔
662
  sighandler_t h = signal(SIGPIPE, SIG_IGN);
563,046✔
663
  if (SIG_ERR == h) {
563,046✔
664
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
665
    return terrno;
×
666
  }
667
  return 0;
563,046✔
668
}
669

670
/*
671
 * Set TCP connection timeout per-socket level.
672
 * ref [https://github.com/libuv/help/issues/54]
673
 */
674
int32_t taosCreateSocketWithTimeout(uint32_t timeout, int8_t t) {
41,693,634✔
675
#if defined(WINDOWS)
676
  SOCKET fd;
677
#else
678
  int fd;
679
#endif
680
  int32_t type = t == 0 ? AF_INET : AF_INET6;
41,693,634✔
681

682
  if ((fd = socket(type, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
41,693,634✔
683
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
684
    return terrno;
×
685
  }
686

687
#if defined(WINDOWS)
688
  if (0 != setsockopt(fd, IPPROTO_TCP, TCP_MAXRT, (char *)&timeout, sizeof(timeout))) {
689
    taosCloseSocketNoCheck1(fd);
690
    return TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
691
  }
692
#elif defined(_TD_DARWIN_64)
693
  // invalid config
694
  // uint32_t conn_timeout_ms = timeout * 1000;
695
  // if (0 != setsockopt(fd, IPPROTO_TCP, TCP_CONNECTIONTIMEOUT, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms))) {
696
  //  taosCloseSocketNoCheck1(fd);
697
  //  return -1;
698
  //}
699
#elif defined(TD_ASTRA) // TD_ASTRA_TODO
700
  uint32_t conn_timeout_ms = timeout;
701
  if (0 != setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms)) || 
702
      0 != setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms))) {
703
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
704
    TAOS_SKIP_ERROR(taosCloseSocketNoCheck1(fd));
705
    return terrno;
706
  }
707
#else  // Linux like systems
708
  uint32_t conn_timeout_ms = timeout;
41,701,109✔
709
  if (-1 == setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms))) {
41,697,251✔
710
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
20✔
711
    TAOS_SKIP_ERROR(taosCloseSocketNoCheck1(fd));
×
712
    return terrno;
×
713
  }
714
#endif
715

716
  return (int)fd;
41,690,820✔
717
}
718

719
int32_t taosWinSocketInit() {
×
720
#ifdef WINDOWS
721
  static int8_t flag = 0;
722
  if (atomic_val_compare_exchange_8(&flag, 0, 1) == 0) {
723
    WORD    wVersionRequested;
724
    WSADATA wsaData;
725
    wVersionRequested = MAKEWORD(1, 1);
726
    if (WSAStartup(wVersionRequested, &wsaData) != 0) {
727
      atomic_store_8(&flag, 0);
728
      int errorCode = WSAGetLastError();
729
      return terrno = TAOS_SYSTEM_WINSOCKET_ERROR(errorCode);
730
    }
731
  }
732
  return 0;
733
#else
734
#endif
735
  return 0;
×
736
}
737

738
uint64_t taosHton64(uint64_t val) {
2,147,483,647✔
739
#if defined(WINDOWS) || defined(DARWIN)
740
  return ((val & 0x00000000000000ff) << 7 * 8) | ((val & 0x000000000000ff00) << 5 * 8) |
741
         ((val & 0x0000000000ff0000) << 3 * 8) | ((val & 0x00000000ff000000) << 1 * 8) |
742
         ((val & 0x000000ff00000000) >> 1 * 8) | ((val & 0x0000ff0000000000) >> 3 * 8) |
743
         ((val & 0x00ff000000000000) >> 5 * 8) | ((val & 0xff00000000000000) >> 7 * 8);
744
#else
745
  if (__BYTE_ORDER == __LITTLE_ENDIAN) {
746
    return (((uint64_t)htonl((int)((val << 32) >> 32))) << 32) | (unsigned int)htonl((int)(val >> 32));
2,147,483,647✔
747
  } else if (__BYTE_ORDER == __BIG_ENDIAN) {
748
    return val;
749
  }
750
#endif
751
}
752

753
uint64_t taosNtoh64(uint64_t val) {
2,147,483,647✔
754
#if defined(WINDOWS) || defined(DARWIN)
755
  return taosHton64(val);
756
#else
757
  if (__BYTE_ORDER == __LITTLE_ENDIAN) {
758
    return (((uint64_t)htonl((int)((val << 32) >> 32))) << 32) | (unsigned int)htonl((int)(val >> 32));
2,147,483,647✔
759
  } else if (__BYTE_ORDER == __BIG_ENDIAN) {
760
    return val;
761
  }
762
#endif
763
}
764

765
int32_t taosSetSockOpt2(int32_t fd) {
2,147,483,647✔
766
#if defined(WINDOWS) || defined(DARWIN) || defined(TD_ASTRA)
767
  return 0;
768
#else
769
  int32_t ret = setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){1}, sizeof(int));
2,147,483,647✔
770
  if (ret < 0) {
2,147,483,647✔
UNCOV
771
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
772
    return terrno;
×
773
  } else {
774
    return 0;
2,147,483,647✔
775
  }
776
#endif
777
  return 0;
778
}
779

780
int32_t taosValidFqdn(int8_t enableIpv6, char *fqdn) {
×
781
  int32_t code = 0;
×
782
  SIpAddr addr = {0};
×
783
  code = taosGetIpFromFqdn(enableIpv6, fqdn, &addr);
×
784
  if (code != 0) {
×
785
    return code;
×
786
  }
787

788
  if (enableIpv6) {
×
789
    // if ipv6 is enabled, but the fqdn resolves to ipv4 address
790
    // then return error
791
    if (addr.type == 0) return TSDB_CODE_RPC_FQDN_ERROR;
×
792
  } else {
793
    if (addr.type == 1) return TSDB_CODE_RPC_FQDN_ERROR;
×
794
  }
795

796
  return code;
×
797
}
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