socket.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_SOCKET_H
  3. #define NETDATA_SOCKET_H
  4. #include "../libnetdata.h"
  5. #ifndef MAX_LISTEN_FDS
  6. #define MAX_LISTEN_FDS 50
  7. #endif
  8. typedef enum web_client_acl {
  9. WEB_CLIENT_ACL_NONE = (0),
  10. WEB_CLIENT_ACL_NOCHECK = (1 << 0), // Don't check anything - this should work on all channels
  11. WEB_CLIENT_ACL_DASHBOARD = (1 << 1),
  12. WEB_CLIENT_ACL_REGISTRY = (1 << 2),
  13. WEB_CLIENT_ACL_BADGE = (1 << 3),
  14. WEB_CLIENT_ACL_MGMT = (1 << 4),
  15. WEB_CLIENT_ACL_STREAMING = (1 << 5),
  16. WEB_CLIENT_ACL_NETDATACONF = (1 << 6),
  17. WEB_CLIENT_ACL_SSL_OPTIONAL = (1 << 7),
  18. WEB_CLIENT_ACL_SSL_FORCE = (1 << 8),
  19. WEB_CLIENT_ACL_SSL_DEFAULT = (1 << 9),
  20. WEB_CLIENT_ACL_ACLK = (1 << 10),
  21. WEB_CLIENT_ACL_WEBRTC = (1 << 11),
  22. WEB_CLIENT_ACL_BEARER_OPTIONAL = (1 << 12), // allow unprotected access if bearer is not enabled in netdata
  23. WEB_CLIENT_ACL_BEARER_REQUIRED = (1 << 13), // allow access only if a valid bearer is used
  24. } WEB_CLIENT_ACL;
  25. #define WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC (WEB_CLIENT_ACL_DASHBOARD | WEB_CLIENT_ACL_ACLK | WEB_CLIENT_ACL_WEBRTC | WEB_CLIENT_ACL_BEARER_OPTIONAL)
  26. #define WEB_CLIENT_ACL_ACLK_WEBRTC_DASHBOARD_WITH_BEARER (WEB_CLIENT_ACL_DASHBOARD | WEB_CLIENT_ACL_ACLK | WEB_CLIENT_ACL_WEBRTC | WEB_CLIENT_ACL_BEARER_REQUIRED)
  27. #ifdef NETDATA_DEV_MODE
  28. #define ACL_DEV_OPEN_ACCESS WEB_CLIENT_ACL_NOCHECK
  29. #else
  30. #define ACL_DEV_OPEN_ACCESS 0
  31. #endif
  32. #define WEB_CLIENT_ACL_ALL 0xFFFF
  33. #define web_client_can_access_dashboard(w) ((w)->acl & WEB_CLIENT_ACL_DASHBOARD)
  34. #define web_client_can_access_registry(w) ((w)->acl & WEB_CLIENT_ACL_REGISTRY)
  35. #define web_client_can_access_badges(w) ((w)->acl & WEB_CLIENT_ACL_BADGE)
  36. #define web_client_can_access_mgmt(w) ((w)->acl & WEB_CLIENT_ACL_MGMT)
  37. #define web_client_can_access_stream(w) ((w)->acl & WEB_CLIENT_ACL_STREAMING)
  38. #define web_client_can_access_netdataconf(w) ((w)->acl & WEB_CLIENT_ACL_NETDATACONF)
  39. #define web_client_is_using_ssl_optional(w) ((w)->port_acl & WEB_CLIENT_ACL_SSL_OPTIONAL)
  40. #define web_client_is_using_ssl_force(w) ((w)->port_acl & WEB_CLIENT_ACL_SSL_FORCE)
  41. #define web_client_is_using_ssl_default(w) ((w)->port_acl & WEB_CLIENT_ACL_SSL_DEFAULT)
  42. typedef struct listen_sockets {
  43. struct config *config; // the config file to use
  44. const char *config_section; // the netdata configuration section to read settings from
  45. const char *default_bind_to; // the default bind to configuration string
  46. uint16_t default_port; // the default port to use
  47. int backlog; // the default listen backlog to use
  48. size_t opened; // the number of sockets opened
  49. size_t failed; // the number of sockets attempted to open, but failed
  50. int fds[MAX_LISTEN_FDS]; // the open sockets
  51. char *fds_names[MAX_LISTEN_FDS]; // descriptions for the open sockets
  52. int fds_types[MAX_LISTEN_FDS]; // the socktype for the open sockets (SOCK_STREAM, SOCK_DGRAM)
  53. int fds_families[MAX_LISTEN_FDS]; // the family of the open sockets (AF_UNIX, AF_INET, AF_INET6)
  54. WEB_CLIENT_ACL fds_acl_flags[MAX_LISTEN_FDS]; // the acl to apply to the open sockets (dashboard, badges, streaming, netdata.conf, management)
  55. } LISTEN_SOCKETS;
  56. char *strdup_client_description(int family, const char *protocol, const char *ip, uint16_t port);
  57. int listen_sockets_setup(LISTEN_SOCKETS *sockets);
  58. void listen_sockets_close(LISTEN_SOCKETS *sockets);
  59. void foreach_entry_in_connection_string(const char *destination, bool (*callback)(char *entry, void *data), void *data);
  60. int connect_to_this_ip46(int protocol, int socktype, const char *host, uint32_t scope_id, const char *service, struct timeval *timeout);
  61. int connect_to_this(const char *definition, int default_port, struct timeval *timeout);
  62. int connect_to_one_of(const char *destination, int default_port, struct timeval *timeout, size_t *reconnects_counter, char *connected_to, size_t connected_to_size);
  63. int connect_to_one_of_urls(const char *destination, int default_port, struct timeval *timeout, size_t *reconnects_counter, char *connected_to, size_t connected_to_size);
  64. #ifdef ENABLE_HTTPS
  65. ssize_t recv_timeout(NETDATA_SSL *ssl,int sockfd, void *buf, size_t len, int flags, int timeout);
  66. ssize_t send_timeout(NETDATA_SSL *ssl,int sockfd, void *buf, size_t len, int flags, int timeout);
  67. #else
  68. ssize_t recv_timeout(int sockfd, void *buf, size_t len, int flags, int timeout);
  69. ssize_t send_timeout(int sockfd, void *buf, size_t len, int flags, int timeout);
  70. #endif
  71. bool fd_is_socket(int fd);
  72. bool sock_has_output_error(int fd);
  73. int sock_setnonblock(int fd);
  74. int sock_delnonblock(int fd);
  75. int sock_setreuse(int fd, int reuse);
  76. int sock_setreuse_port(int fd, int reuse);
  77. int sock_enlarge_in(int fd);
  78. int sock_enlarge_out(int fd);
  79. int connection_allowed(int fd, char *client_ip, char *client_host, size_t hostsize,
  80. SIMPLE_PATTERN *access_list, const char *patname, int allow_dns);
  81. int accept_socket(int fd, int flags, char *client_ip, size_t ipsize, char *client_port, size_t portsize,
  82. char *client_host, size_t hostsize, SIMPLE_PATTERN *access_list, int allow_dns);
  83. #ifndef HAVE_ACCEPT4
  84. int accept4(int sock, struct sockaddr *addr, socklen_t *addrlen, int flags);
  85. #ifndef SOCK_NONBLOCK
  86. #define SOCK_NONBLOCK 00004000
  87. #endif /* #ifndef SOCK_NONBLOCK */
  88. #ifndef SOCK_CLOEXEC
  89. #define SOCK_CLOEXEC 02000000
  90. #endif /* #ifndef SOCK_CLOEXEC */
  91. #endif /* #ifndef HAVE_ACCEPT4 */
  92. // ----------------------------------------------------------------------------
  93. // poll() based listener
  94. #define POLLINFO_FLAG_SERVER_SOCKET 0x00000001
  95. #define POLLINFO_FLAG_CLIENT_SOCKET 0x00000002
  96. #define POLLINFO_FLAG_DONT_CLOSE 0x00000004
  97. typedef struct poll POLLJOB;
  98. typedef struct pollinfo {
  99. POLLJOB *p; // the parent
  100. size_t slot; // the slot id
  101. int fd; // the file descriptor
  102. int socktype; // the client socket type
  103. WEB_CLIENT_ACL port_acl; // the access lists permitted on this web server port (it's -1 for client sockets)
  104. char *client_ip; // Max INET6_ADDRSTRLEN bytes
  105. char *client_port; // Max NI_MAXSERV bytes
  106. char *client_host; // Max NI_MAXHOST bytes
  107. time_t connected_t; // the time the socket connected
  108. time_t last_received_t; // the time the socket last received data
  109. time_t last_sent_t; // the time the socket last sent data
  110. size_t recv_count; // the number of times the socket was ready for inbound traffic
  111. size_t send_count; // the number of times the socket was ready for outbound traffic
  112. uint32_t flags; // internal flags
  113. // callbacks for this socket
  114. void (*del_callback)(struct pollinfo *pi);
  115. int (*rcv_callback)(struct pollinfo *pi, short int *events);
  116. int (*snd_callback)(struct pollinfo *pi, short int *events);
  117. // the user data
  118. void *data;
  119. // linking of free pollinfo structures
  120. // for quickly finding the next available
  121. // this is like a stack, it grows and shrinks
  122. // (with gaps - lower empty slots are preferred)
  123. struct pollinfo *next;
  124. } POLLINFO;
  125. struct poll {
  126. size_t slots;
  127. size_t used;
  128. size_t min;
  129. size_t max;
  130. size_t limit;
  131. time_t complete_request_timeout;
  132. time_t idle_timeout;
  133. time_t checks_every;
  134. time_t timer_milliseconds;
  135. void *timer_data;
  136. struct pollfd *fds;
  137. struct pollinfo *inf;
  138. struct pollinfo *first_free;
  139. SIMPLE_PATTERN *access_list;
  140. int allow_dns;
  141. void *(*add_callback)(POLLINFO *pi, short int *events, void *data);
  142. void (*del_callback)(POLLINFO *pi);
  143. int (*rcv_callback)(POLLINFO *pi, short int *events);
  144. int (*snd_callback)(POLLINFO *pi, short int *events);
  145. void (*tmr_callback)(void *timer_data);
  146. };
  147. #define pollinfo_from_slot(p, slot) (&((p)->inf[(slot)]))
  148. int poll_default_snd_callback(POLLINFO *pi, short int *events);
  149. int poll_default_rcv_callback(POLLINFO *pi, short int *events);
  150. void poll_default_del_callback(POLLINFO *pi);
  151. void *poll_default_add_callback(POLLINFO *pi, short int *events, void *data);
  152. POLLINFO *poll_add_fd(POLLJOB *p
  153. , int fd
  154. , int socktype
  155. , WEB_CLIENT_ACL port_acl
  156. , uint32_t flags
  157. , const char *client_ip
  158. , const char *client_port
  159. , const char *client_host
  160. , void *(*add_callback)(POLLINFO *pi, short int *events, void *data)
  161. , void (*del_callback)(POLLINFO *pi)
  162. , int (*rcv_callback)(POLLINFO *pi, short int *events)
  163. , int (*snd_callback)(POLLINFO *pi, short int *events)
  164. , void *data
  165. );
  166. void poll_close_fd(POLLINFO *pi);
  167. void poll_events(LISTEN_SOCKETS *sockets
  168. , void *(*add_callback)(POLLINFO *pi, short int *events, void *data)
  169. , void (*del_callback)(POLLINFO *pi)
  170. , int (*rcv_callback)(POLLINFO *pi, short int *events)
  171. , int (*snd_callback)(POLLINFO *pi, short int *events)
  172. , void (*tmr_callback)(void *timer_data)
  173. , bool (*check_to_stop_callback)(void)
  174. , SIMPLE_PATTERN *access_list
  175. , int allow_dns
  176. , void *data
  177. , time_t tcp_request_timeout_seconds
  178. , time_t tcp_idle_timeout_seconds
  179. , time_t timer_milliseconds
  180. , void *timer_data
  181. , size_t max_tcp_sockets
  182. );
  183. #ifndef INET6_ADDRSTRLEN
  184. #define INET6_ADDRSTRLEN 46
  185. #endif
  186. typedef struct socket_peers {
  187. struct {
  188. char ip[INET6_ADDRSTRLEN];
  189. int port;
  190. } local;
  191. struct {
  192. char ip[INET6_ADDRSTRLEN];
  193. int port;
  194. } peer;
  195. } SOCKET_PEERS;
  196. SOCKET_PEERS socket_peers(int sock_fd);
  197. bool ip_to_hostname(const char *ip, char *dst, size_t dst_len);
  198. #endif //NETDATA_SOCKET_H