socket.h 7.8 KB

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