da_util.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef DA_UTIL_H_
  17. #define DA_UTIL_H_
  18. #include <netdb.h>
  19. #include <netinet/in.h>
  20. #include <netinet/tcp.h>
  21. #include <stdbool.h>
  22. #include <stddef.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/socket.h>
  27. #include <sys/time.h>
  28. #include <sys/types.h>
  29. #include <sys/un.h>
  30. struct string;
  31. #define LF (uint8_t)10
  32. #define CR (uint8_t)13
  33. #define CRLF "\x0d\x0a"
  34. #define NELEMS(a) ((sizeof(a)) / sizeof((a)[0]))
  35. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  36. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  37. #define DA_MAXHOSTNAMELEN 256
  38. #define DA_UNIX_ADDRSTRLEN \
  39. (sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path))
  40. /*
  41. * Length of 1 byte, 2 bytes, 4 bytes, 8 bytes and largest integral
  42. * type (uintmax_t) in ascii, including the null terminator '\0'
  43. *
  44. * From stdint.h, we have:
  45. * # define UINT8_MAX (255)
  46. * # define UINT16_MAX (65535)
  47. * # define UINT32_MAX (4294967295U)
  48. * # define UINT64_MAX (__UINT64_C(18446744073709551615))
  49. */
  50. #define DA_UINT8_MAXLEN (3 + 1)
  51. #define DA_UINT16_MAXLEN (5 + 1)
  52. #define DA_UINT32_MAXLEN (10 + 1)
  53. #define DA_UINT64_MAXLEN (20 + 1)
  54. #define DA_UINTMAX_MAXLEN DA_UINT64_MAXLEN
  55. #define da_sendn(_s, _b, _n) _da_sendn(_s, _b, (size_t)(_n))
  56. #define da_recvn(_s, _b, _n) _da_recvn(_s, _b, (size_t)(_n))
  57. #define da_read(_d, _b, _n) read(_d, _b, (size_t)(_n))
  58. #define da_readv(_d, _b, _n) readv(_d, _b, (int)(_n))
  59. #define da_write(_d, _b, _n) write(_d, _b, (size_t)(_n))
  60. #define da_writev(_d, _b, _n) writev(_d, _b, (int)(_n))
  61. ssize_t _da_sendn(int sd, const void *vptr, size_t n);
  62. ssize_t _da_recvn(int sd, void *vptr, size_t n);
  63. #define da_gethostname(_name, _len) gethostname((char *)_name, (size_t)(_len))
  64. #define da_atoi(_line, _n) _da_atoi((uint8_t *)_line, (size_t)(_n))
  65. int _da_atoi(uint8_t *line, size_t n);
  66. #ifdef DA_ASSERT_PANIC
  67. #define ASSERT(_x) \
  68. do { \
  69. if (!(_x)) { \
  70. da_assert(#_x, __FILE__, __LINE__, 1); \
  71. } \
  72. } while (0)
  73. #elif DA_ASSERT_LOG
  74. #define ASSERT(_x) \
  75. do { \
  76. if (!(_x)) { \
  77. da_assert(#_x, __FILE__, __LINE__, 0); \
  78. } \
  79. } while (0)
  80. #else
  81. #define ASSERT(_x)
  82. #endif
  83. void da_assert(const char *cond, const char *file, int line, int panic);
  84. void da_stacktrace(int skip_count);
  85. void da_stacktrace_fd(int fd);
  86. int da_strlcpy(char *dst, const char *src, int size);
  87. int set_reuseaddr(int fd);
  88. int set_nonblocking(int fd);
  89. int set_tcpnodelay(int fd);
  90. int set_tcpquickack(int fd);
  91. int get_soerror(int sd);
  92. bool nc_valid_port(int n);
  93. char upper(char c);
  94. char lower(char c);
  95. int _scnprintf(char *buf, size_t size, const char *fmt, ...);
  96. int _vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
  97. bool da_valid_port(int n);
  98. /*
  99. * Address resolution for internet (ipv4 and ipv6) and unix domain
  100. * socket address.
  101. */
  102. struct sockinfo {
  103. int family; /* socket address family */
  104. socklen_t addrlen; /* socket address length */
  105. union {
  106. struct sockaddr_in in; /* ipv4 socket address */
  107. struct sockaddr_in6 in6; /* ipv6 socket address */
  108. struct sockaddr_un un; /* unix domain address */
  109. } addr;
  110. };
  111. int da_resolve(struct string *name, int port, struct sockinfo *si);
  112. char *da_unresolve_addr(struct sockaddr *addr, socklen_t addrlen);
  113. char *da_unresolve_peer_desc(int sd);
  114. char *da_unresolve_desc(int sd);
  115. int64_t nc_usec_now(void);
  116. #endif /* DA_UTIL_H_ */