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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

53.97
/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) {
2,389✔
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);
2,389✔
100
  if (-1 == code) {
2,389!
101
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
102
    return terrno;
×
103
  }
104
  return code;
2,389✔
105
#endif
106
}
107

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

118
  return code;
2,389✔
119
}
120

121
int32_t taosSetSockOpt(TdSocketPtr pSocket, int32_t level, int32_t optname, void *optval, int32_t optlen) {
2,392✔
122
  if (pSocket == NULL || pSocket->fd < 0) {
2,392!
123
    terrno = TSDB_CODE_INVALID_PARA;
3✔
124
    return terrno;
3✔
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);
2,389✔
159
  if (-1 == code) {
2,389!
160
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
161
    return terrno;
×
162
  }
163
  return 0;
2,389✔
164
#endif
165
}
166

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

173
  return r;
3✔
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) {
2,389✔
183
  struct sockaddr_in serverAdd;
184
  SocketFd           fd;
185
  int32_t            reuse;
186
  int32_t            code = 0;
2,389✔
187

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

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

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

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

213
  /* set REUSEADDR option, so the portnumber can be re-used */
214
  reuse = 1;
2,389✔
215
  if (taosSetSockOpt(pSocket, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(reuse)) < 0) {
2,389!
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))) {
2,389!
222
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
223
    TAOS_SKIP_ERROR(taosCloseSocket(&pSocket));
×
224
    return 0;
×
225
  }
226

227
  TAOS_SKIP_ERROR(taosCloseSocket(&pSocket));
2,389✔
228

229
  return 1;
2,389✔
230
}
231

232
int32_t taosBlockSIGPIPE() {
632,339✔
233
#ifdef WINDOWS
234
  return 0;
235
#else
236
  sigset_t signal_mask;
237
  (void)sigemptyset(&signal_mask);
632,339✔
238
  (void)sigaddset(&signal_mask, SIGPIPE);
632,351✔
239
  int32_t rc = pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
632,362✔
240
  if (rc != 0) {
632,379!
241
    terrno = TAOS_SYSTEM_ERROR(rc);
×
242
    return terrno;
×
243
  }
244

245
  return 0;
632,379✔
246
#endif
247
}
248

249
int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t *ip) {
35✔
250
  int32_t code = 0;
35✔
251
  OS_PARAM_CHECK(fqdn);
35✔
252
  OS_PARAM_CHECK(ip);
32✔
253
  int64_t limitMs = 1000;
29✔
254
  int64_t st = taosGetTimestampMs(), cost = 0;
29✔
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};
29✔
268
  hints.ai_family = AF_INET;
29✔
269
  hints.ai_socktype = SOCK_STREAM;
29✔
270

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

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

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

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

293
    *ip = ia.s_addr;
25✔
294

295
    freeaddrinfo(result);
25✔
296
    goto _err;
25✔
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:
29✔
330
  cost = taosGetTimestampMs() - st;
29✔
331
  if (cost >= limitMs) {
29!
332
    uWarn("get ip from fqdn:%s, cost:%" PRId64 "ms", fqdn, cost);
×
333
  }
334
  return code;
29✔
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
      inet_ntop(AF_INET6, &p6->sin6_addr, pAddr->ipv6, sizeof(pAddr->ipv6));
×
379
      pAddr->type = 1;
×
380
    } else if (result->ai_family == AF_INET) {
×
381
      struct sockaddr_in *p4 = (struct sockaddr_in *)result->ai_addr;
×
382
      inet_ntop(AF_INET, &p4->sin_addr, pAddr->ipv4, sizeof(pAddr->ipv4));
×
383
      pAddr->type = 0;
×
384
    } else {
385
      code = TSDB_CODE_RPC_FQDN_ERROR;
×
386
      goto _err;
×
387
    }
388

389
    freeaddrinfo(result);
×
390
    goto _err;
×
391
  }
392
#else
393
  struct addrinfo hints = {0};
394
  hints.ai_family = AF_UNSPEC;
395
  hints.ai_socktype = SOCK_STREAM;
396

397
  struct addrinfo *result = NULL;
398

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

433
#ifdef WINDOWS
434
  WSACleanup();
435
#endif
436
  return code;
×
437
}
438

439
int32_t taosGetIp4FromFqdn(const char *fqdn, SIpAddr *pAddr) {
121,157✔
440
  int32_t code = 0;
121,157✔
441
  OS_PARAM_CHECK(fqdn);
121,157!
442
  int64_t limitMs = 1000;
121,157✔
443
  int64_t st = taosGetTimestampMs(), cost = 0;
121,196✔
444
#ifdef WINDOWS
445
  // Initialize Winsock
446
  WSADATA wsaData;
447
  int     iResult;
448
  iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
449
  if (iResult != 0) {
450
    code = TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
451
    goto _err;
452
  }
453
#endif
454

455
#if defined(LINUX)
456
  struct addrinfo hints = {0};
121,196✔
457
  hints.ai_family = AF_INET;
121,196✔
458
  hints.ai_socktype = SOCK_STREAM;
121,196✔
459

460
  struct addrinfo *result = NULL;
121,196✔
461
  bool             inRetry = false;
121,196✔
462

463
  char ipStr[INET6_ADDRSTRLEN];
464
  while (true) {
×
465
    int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
121,196✔
466
    if (ret) {
121,216✔
467
      if (EAI_AGAIN == ret && !inRetry) {
9!
468
        inRetry = true;
×
469
        continue;
×
470
      } else if (EAI_SYSTEM == ret) {
9!
471
        code = TAOS_SYSTEM_ERROR(ERRNO);
×
472
        goto _err;
×
473
      }
474

475
      code = TAOS_SYSTEM_ERROR(ERRNO);
9✔
476
      goto _err;
9✔
477
    }
478

479
    if (result->ai_family == AF_INET) {
121,207!
480
      struct sockaddr_in *p4 = (struct sockaddr_in *)result->ai_addr;
121,208✔
481
      inet_ntop(AF_INET, &p4->sin_addr, pAddr->ipv4, sizeof(pAddr->ipv4));
121,208✔
482
      pAddr->type = 0;
121,210✔
483
    } else {
484
      code = TSDB_CODE_RPC_FQDN_ERROR;
×
485
      goto _err;
×
486
    }
487

488
    freeaddrinfo(result);
121,210✔
489
    goto _err;
121,211✔
490
  }
491
#else
492
  struct addrinfo hints = {0};
493
  hints.ai_family = AF_INET;
494
  hints.ai_socktype = SOCK_STREAM;
495

496
  struct addrinfo *result = NULL;
497

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

519
_err:
121,219✔
520
  cost = taosGetTimestampMs() - st;
121,221✔
521
  if (cost >= limitMs) {
121,221!
522
    uWarn("get ip from fqdn:%s, cost:%" PRId64 "ms", fqdn, cost);
×
523
  }
524
#ifdef WINDOWS
525
  WSACleanup();
526
#endif
527
  return code;
121,220✔
528
}
529
int32_t taosGetIpFromFqdn(int8_t enableIpv6, const char *fqdn, SIpAddr *addr) {
121,176✔
530
  int32_t  code = 0;
121,176✔
531
  if (enableIpv6) {
121,176!
532
    code = taosGetIpv6FromFqdn(fqdn, addr);
×
533
  } else {
534
    code = taosGetIp4FromFqdn(fqdn, addr);
121,176✔
535
  }
536
  return code;
121,219✔
537
}
538

539
int8_t taosIpAddrIsEqual(SIpAddr *ip1, SIpAddr *ip2) {
10,457,539✔
540
  if (ip1->type == ip2->type) {
10,457,539!
541
    if (ip1->type == 0) {
10,459,137!
542
      return (strcmp(ip1->ipv4, ip2->ipv4) == 0 ? 1 : 0);
10,460,085✔
543
    } else {
544
      return (strcmp(ip1->ipv6, ip2->ipv6) == 0 ? 1 : 0);
×
545
    }
546
  }
547
  return 0;
×
548
}
549

550
int32_t taosGetFqdn(char *fqdn) {
9,738✔
551
  OS_PARAM_CHECK(fqdn);
9,738!
552
#ifdef WINDOWS
553
  // Initialize Winsock
554
  WSADATA wsaData;
555
  int     iResult;
556
  iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
557
  if (iResult != 0) {
558
    // printf("WSAStartup failed: %d\n", iResult);
559
    return TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
560
  }
561
#endif
562
  char hostname[1024];
563
  hostname[1023] = '\0';
9,738✔
564
  int32_t code = taosGetlocalhostname(hostname, 1023);
9,738✔
565
  if (code) {
9,738!
566
    return code;
×
567
  }
568

569
#ifdef __APPLE__
570
  // on macosx, hostname -f has the form of xxx.local
571
  // which will block getaddrinfo for a few seconds if AI_CANONNAME is set
572
  // thus, we choose AF_INET (ipv4 for the moment) to make getaddrinfo return
573
  // immediately
574
  // hints.ai_family = AF_INET;
575
  tstrncpy(fqdn, hostname, TD_FQDN_LEN);
576
  tstrncpy(fqdn + strlen(hostname), ".local", TD_FQDN_LEN - strlen(hostname));
577
#else  // linux
578

579
#endif  // linux
580
#if defined(TD_ASTRA)
581
  tstrncpy(fqdn, hostname, TD_FQDN_LEN);  
582
#elif defined(LINUX)
583

584
  struct addrinfo  hints = {0};
9,738✔
585
  struct addrinfo *result = NULL;
9,738✔
586
  hints.ai_flags = AI_CANONNAME;
9,738✔
587

588
  while (true) {
×
589
    int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
9,738✔
590
    if (ret) {
9,738!
591
      if (EAI_AGAIN == ret) {
×
592
        continue;
×
593
      } else if (EAI_SYSTEM == ret) {
×
594
        terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
595
        return terrno;
×
596
      }
597

598
      terrno = TAOS_SYSTEM_ERROR(ret);
×
599
      return terrno;
×
600
    }
601

602
    break;
9,738✔
603
  }
604

605
  tstrncpy(fqdn, result->ai_canonname, TD_FQDN_LEN);
9,738✔
606

607
  freeaddrinfo(result);
9,738✔
608

609
#elif WINDOWS
610
  struct addrinfo  hints = {0};
611
  struct addrinfo *result = NULL;
612
  hints.ai_flags = AI_CANONNAME;
613

614
  int32_t ret = getaddrinfo(hostname, NULL, &hints, &result);
615
  if (!result) {
616
    // fprintf(stderr, "failed to get fqdn, code:%d, hostname:%s, reason:%s\n", ret, hostname, gai_strerror(ret));
617
    return TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
618
  }
619
  tstrncpy(fqdn, result->ai_canonname, TD_FQDN_LEN);
620
  freeaddrinfo(result);
621

622
#endif
623

624
  return 0;
9,738✔
625
}
626

627
void taosInetNtoa(char *ipstr, uint32_t ip) {
33✔
628
  if (ipstr == NULL) {
33✔
629
    return;
3✔
630
  }
631
  unsigned char *bytes = (unsigned char *) &ip;
30✔
632
  (void)snprintf(ipstr, TD_IP_LEN, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
30✔
633
}
634

635
uint32_t taosInetAddr(const char *ipstr){
13✔
636
  if (ipstr == NULL) {
13✔
637
    return 0;
3✔
638
  }
639
  return inet_addr(ipstr);
10✔
640
}
641

642
int32_t taosIgnSIGPIPE() {
2,390✔
643
  sighandler_t h = signal(SIGPIPE, SIG_IGN);
2,390✔
644
  if (SIG_ERR == h) {
2,390!
645
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
646
    return terrno;
×
647
  }
648
  return 0;
2,390✔
649
}
650

651
/*
652
 * Set TCP connection timeout per-socket level.
653
 * ref [https://github.com/libuv/help/issues/54]
654
 */
655
int32_t taosCreateSocketWithTimeout(uint32_t timeout, int8_t t) {
87,560✔
656
#if defined(WINDOWS)
657
  SOCKET fd;
658
#else
659
  int fd;
660
#endif
661
  int32_t type = t == 0 ? AF_INET : AF_INET6;
87,560!
662

663
  if ((fd = socket(type, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
87,560!
664
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
665
    return terrno;
×
666
  }
667

668
#if defined(WINDOWS)
669
  if (0 != setsockopt(fd, IPPROTO_TCP, TCP_MAXRT, (char *)&timeout, sizeof(timeout))) {
670
    taosCloseSocketNoCheck1(fd);
671
    return TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError());
672
  }
673
#elif defined(_TD_DARWIN_64)
674
  // invalid config
675
  // uint32_t conn_timeout_ms = timeout * 1000;
676
  // if (0 != setsockopt(fd, IPPROTO_TCP, TCP_CONNECTIONTIMEOUT, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms))) {
677
  //  taosCloseSocketNoCheck1(fd);
678
  //  return -1;
679
  //}
680
#elif defined(TD_ASTRA) // TD_ASTRA_TODO
681
  uint32_t conn_timeout_ms = timeout;
682
  if (0 != setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms)) || 
683
      0 != setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms))) {
684
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
685
    TAOS_SKIP_ERROR(taosCloseSocketNoCheck1(fd));
686
    return terrno;
687
  }
688
#else  // Linux like systems
689
  uint32_t conn_timeout_ms = timeout;
87,571✔
690
  if (-1 == setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, (char *)&conn_timeout_ms, sizeof(conn_timeout_ms))) {
87,571✔
691
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
1✔
692
    TAOS_SKIP_ERROR(taosCloseSocketNoCheck1(fd));
×
693
    return terrno;
×
694
  }
695
#endif
696

697
  return (int)fd;
87,558✔
698
}
699

700
int32_t taosWinSocketInit() {
3✔
701
#ifdef WINDOWS
702
  static int8_t flag = 0;
703
  if (atomic_val_compare_exchange_8(&flag, 0, 1) == 0) {
704
    WORD    wVersionRequested;
705
    WSADATA wsaData;
706
    wVersionRequested = MAKEWORD(1, 1);
707
    if (WSAStartup(wVersionRequested, &wsaData) != 0) {
708
      atomic_store_8(&flag, 0);
709
      int errorCode = WSAGetLastError();
710
      return terrno = TAOS_SYSTEM_WINSOCKET_ERROR(errorCode);
711
    }
712
  }
713
  return 0;
714
#else
715
#endif
716
  return 0;
3✔
717
}
718

719
uint64_t taosHton64(uint64_t val) {
66,317,877✔
720
#if defined(WINDOWS) || defined(DARWIN)
721
  return ((val & 0x00000000000000ff) << 7 * 8) | ((val & 0x000000000000ff00) << 5 * 8) |
722
         ((val & 0x0000000000ff0000) << 3 * 8) | ((val & 0x00000000ff000000) << 1 * 8) |
723
         ((val & 0x000000ff00000000) >> 1 * 8) | ((val & 0x0000ff0000000000) >> 3 * 8) |
724
         ((val & 0x00ff000000000000) >> 5 * 8) | ((val & 0xff00000000000000) >> 7 * 8);
725
#else
726
  if (__BYTE_ORDER == __LITTLE_ENDIAN) {
727
    return (((uint64_t)htonl((int)((val << 32) >> 32))) << 32) | (unsigned int)htonl((int)(val >> 32));
66,317,877✔
728
  } else if (__BYTE_ORDER == __BIG_ENDIAN) {
729
    return val;
730
  }
731
#endif
732
}
733

734
uint64_t taosNtoh64(uint64_t val) {
9,194,750✔
735
#if defined(WINDOWS) || defined(DARWIN)
736
  return taosHton64(val);
737
#else
738
  if (__BYTE_ORDER == __LITTLE_ENDIAN) {
739
    return (((uint64_t)htonl((int)((val << 32) >> 32))) << 32) | (unsigned int)htonl((int)(val >> 32));
9,194,750✔
740
  } else if (__BYTE_ORDER == __BIG_ENDIAN) {
741
    return val;
742
  }
743
#endif
744
}
745

746
int32_t taosSetSockOpt2(int32_t fd) {
13,201,956✔
747
#if defined(WINDOWS) || defined(DARWIN) || defined(TD_ASTRA)
748
  return 0;
749
#else
750
  int32_t ret = setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (int[]){1}, sizeof(int));
13,201,956✔
751
  if (ret < 0) {
13,208,894!
752
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
753
    return terrno;
×
754
  } else {
755
    return 0;
13,211,965✔
756
  }
757
#endif
758
  return 0;
759
}
760

761
int32_t taosValidFqdn(int8_t enableIpv6, char *fqdn) {
×
762
  int32_t code = 0;
×
763
  SIpAddr addr = {0};
×
764
  code = taosGetIpFromFqdn(enableIpv6, fqdn, &addr);
×
765
  if (code != 0) {
×
766
    return code;
×
767
  }
768

769
  if (enableIpv6) {
×
770
    // if ipv6 is enabled, but the fqdn resolves to ipv4 address
771
    // then return error
772
    if (addr.type == 0) return TSDB_CODE_RPC_FQDN_ERROR;
×
773
  } else {
774
    if (addr.type == 1) return TSDB_CODE_RPC_FQDN_ERROR;
×
775
  }
776

777
  return code;
×
778
}
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

© 2025 Coveralls, Inc