socket.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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(NETDATA_SSL *ssl,int sockfd, void *buf, size_t len, int flags, int timeout);
  58. ssize_t send_timeout(NETDATA_SSL *ssl,int sockfd, void *buf, size_t len, int flags, int timeout);
  59. #else
  60. ssize_t recv_timeout(int sockfd, void *buf, size_t len, int flags, int timeout);
  61. ssize_t send_timeout(int sockfd, void *buf, size_t len, int flags, int timeout);
  62. #endif
  63. bool fd_is_socket(int fd);
  64. bool sock_has_output_error(int fd);
  65. int sock_setnonblock(int fd);
  66. int sock_delnonblock(int fd);
  67. int sock_setreuse(int fd, int reuse);
  68. int sock_setreuse_port(int fd, int reuse);
  69. int sock_enlarge_in(int fd);
  70. int sock_enlarge_out(int fd);
  71. int connection_allowed(int fd, char *client_ip, char *client_host, size_t hostsize,
  72. SIMPLE_PATTERN *access_list, const char *patname, int allow_dns);
  73. int accept_socket(int fd, int flags, char *client_ip, size_t ipsize, char *client_port, size_t portsize,
  74. char *client_host, size_t hostsize, SIMPLE_PATTERN *access_list, int allow_dns);
  75. #ifndef HAVE_ACCEPT4
  76. int accept4(int sock, struct sockaddr *addr, socklen_t *addrlen, int flags);
  77. #ifndef SOCK_NONBLOCK
  78. #define SOCK_NONBLOCK 00004000
  79. #endif /* #ifndef SOCK_NONBLOCK */
  80. #ifndef SOCK_CLOEXEC
  81. #define SOCK_CLOEXEC 02000000
  82. #endif /* #ifndef SOCK_CLOEXEC */
  83. #endif /* #ifndef HAVE_ACCEPT4 */
  84. // ----------------------------------------------------------------------------
  85. // poll() based listener
  86. #define POLLINFO_FLAG_SERVER_SOCKET 0x00000001
  87. #define POLLINFO_FLAG_CLIENT_SOCKET 0x00000002
  88. #define POLLINFO_FLAG_DONT_CLOSE 0x00000004
  89. typedef struct poll POLLJOB;
  90. typedef struct pollinfo {
  91. POLLJOB *p; // the parent
  92. size_t slot; // the slot id
  93. int fd; // the file descriptor
  94. int socktype; // the client socket type
  95. WEB_CLIENT_ACL port_acl; // the access lists permitted on this web server port (it's -1 for client sockets)
  96. char *client_ip; // Max INET6_ADDRSTRLEN bytes
  97. char *client_port; // Max NI_MAXSERV bytes
  98. char *client_host; // Max NI_MAXHOST bytes
  99. time_t connected_t; // the time the socket connected
  100. time_t last_received_t; // the time the socket last received data
  101. time_t last_sent_t; // the time the socket last sent data
  102. size_t recv_count; // the number of times the socket was ready for inbound traffic
  103. size_t send_count; // the number of times the socket was ready for outbound traffic
  104. uint32_t flags; // internal flags
  105. // callbacks for this socket
  106. void (*del_callback)(struct pollinfo *pi);
  107. int (*rcv_callback)(struct pollinfo *pi, short int *events);
  108. int (*snd_callback)(struct pollinfo *pi, short int *events);
  109. // the user data
  110. void *data;
  111. // linking of free pollinfo structures
  112. // for quickly finding the next available
  113. // this is like a stack, it grows and shrinks
  114. // (with gaps - lower empty slots are preferred)
  115. struct pollinfo *next;
  116. } POLLINFO;
  117. struct poll {
  118. size_t slots;
  119. size_t used;
  120. size_t min;
  121. size_t max;
  122. size_t limit;
  123. time_t complete_request_timeout;
  124. time_t idle_timeout;
  125. time_t checks_every;
  126. time_t timer_milliseconds;
  127. void *timer_data;
  128. struct pollfd *fds;
  129. struct pollinfo *inf;
  130. struct pollinfo *first_free;
  131. SIMPLE_PATTERN *access_list;
  132. int allow_dns;
  133. void *(*add_callback)(POLLINFO *pi, short int *events, void *data);
  134. void (*del_callback)(POLLINFO *pi);
  135. int (*rcv_callback)(POLLINFO *pi, short int *events);
  136. int (*snd_callback)(POLLINFO *pi, short int *events);
  137. void (*tmr_callback)(void *timer_data);
  138. };
  139. #define pollinfo_from_slot(p, slot) (&((p)->inf[(slot)]))
  140. int poll_default_snd_callback(POLLINFO *pi, short int *events);
  141. int poll_default_rcv_callback(POLLINFO *pi, short int *events);
  142. void poll_default_del_callback(POLLINFO *pi);
  143. void *poll_default_add_callback(POLLINFO *pi, short int *events, void *data);
  144. POLLINFO *poll_add_fd(POLLJOB *p
  145. , int fd
  146. , int socktype
  147. , WEB_CLIENT_ACL port_acl
  148. , uint32_t flags
  149. , const char *client_ip
  150. , const char *client_port
  151. , const char *client_host
  152. , void *(*add_callback)(POLLINFO *pi, short int *events, void *data)
  153. , void (*del_callback)(POLLINFO *pi)
  154. , int (*rcv_callback)(POLLINFO *pi, short int *events)
  155. , int (*snd_callback)(POLLINFO *pi, short int *events)
  156. , void *data
  157. );
  158. void poll_close_fd(POLLINFO *pi);
  159. void poll_events(LISTEN_SOCKETS *sockets
  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 (*tmr_callback)(void *timer_data)
  165. , bool (*check_to_stop_callback)(void)
  166. , SIMPLE_PATTERN *access_list
  167. , int allow_dns
  168. , void *data
  169. , time_t tcp_request_timeout_seconds
  170. , time_t tcp_idle_timeout_seconds
  171. , time_t timer_milliseconds
  172. , void *timer_data
  173. , size_t max_tcp_sockets
  174. );
  175. #ifndef INET6_ADDRSTRLEN
  176. #define INET6_ADDRSTRLEN 46
  177. #endif
  178. typedef struct socket_peers {
  179. struct {
  180. char ip[INET6_ADDRSTRLEN];
  181. int port;
  182. } local;
  183. struct {
  184. char ip[INET6_ADDRSTRLEN];
  185. int port;
  186. } peer;
  187. } SOCKET_PEERS;
  188. SOCKET_PEERS socket_peers(int sock_fd);
  189. #endif //NETDATA_SOCKET_H