socket.h 8.9 KB

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