socket.h 8.5 KB

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