socket.h 9.1 KB

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