socket.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. } LISTEN_SOCKETS;
  21. extern char *strdup_client_description(int family, const char *protocol, const char *ip, uint16_t port);
  22. extern int listen_sockets_setup(LISTEN_SOCKETS *sockets);
  23. extern void listen_sockets_close(LISTEN_SOCKETS *sockets);
  24. extern int connect_to_this(const char *definition, int default_port, struct timeval *timeout);
  25. 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);
  26. extern ssize_t recv_timeout(int sockfd, void *buf, size_t len, int flags, int timeout);
  27. extern ssize_t send_timeout(int sockfd, void *buf, size_t len, int flags, int timeout);
  28. extern int sock_setnonblock(int fd);
  29. extern int sock_delnonblock(int fd);
  30. extern int sock_setreuse(int fd, int reuse);
  31. extern int sock_setreuse_port(int fd, int reuse);
  32. extern int sock_enlarge_in(int fd);
  33. extern int sock_enlarge_out(int fd);
  34. extern int accept_socket(int fd, int flags, char *client_ip, size_t ipsize, char *client_port, size_t portsize, SIMPLE_PATTERN *access_list);
  35. #ifndef HAVE_ACCEPT4
  36. extern int accept4(int sock, struct sockaddr *addr, socklen_t *addrlen, int flags);
  37. #ifndef SOCK_NONBLOCK
  38. #define SOCK_NONBLOCK 00004000
  39. #endif /* #ifndef SOCK_NONBLOCK */
  40. #ifndef SOCK_CLOEXEC
  41. #define SOCK_CLOEXEC 02000000
  42. #endif /* #ifndef SOCK_CLOEXEC */
  43. #endif /* #ifndef HAVE_ACCEPT4 */
  44. // ----------------------------------------------------------------------------
  45. // poll() based listener
  46. #define POLLINFO_FLAG_SERVER_SOCKET 0x00000001
  47. #define POLLINFO_FLAG_CLIENT_SOCKET 0x00000002
  48. #define POLLINFO_FLAG_DONT_CLOSE 0x00000004
  49. typedef struct poll POLLJOB;
  50. typedef struct pollinfo {
  51. POLLJOB *p; // the parent
  52. size_t slot; // the slot id
  53. int fd; // the file descriptor
  54. int socktype; // the client socket type
  55. char *client_ip; // the connected client IP
  56. char *client_port; // the connected client port
  57. time_t connected_t; // the time the socket connected
  58. time_t last_received_t; // the time the socket last received data
  59. time_t last_sent_t; // the time the socket last sent data
  60. size_t recv_count; // the number of times the socket was ready for inbound traffic
  61. size_t send_count; // the number of times the socket was ready for outbound traffic
  62. uint32_t flags; // internal flags
  63. // callbacks for this socket
  64. void (*del_callback)(struct pollinfo *pi);
  65. int (*rcv_callback)(struct pollinfo *pi, short int *events);
  66. int (*snd_callback)(struct pollinfo *pi, short int *events);
  67. // the user data
  68. void *data;
  69. // linking of free pollinfo structures
  70. // for quickly finding the next available
  71. // this is like a stack, it grows and shrinks
  72. // (with gaps - lower empty slots are preferred)
  73. struct pollinfo *next;
  74. } POLLINFO;
  75. struct poll {
  76. size_t slots;
  77. size_t used;
  78. size_t min;
  79. size_t max;
  80. size_t limit;
  81. time_t complete_request_timeout;
  82. time_t idle_timeout;
  83. time_t checks_every;
  84. time_t timer_milliseconds;
  85. void *timer_data;
  86. struct pollfd *fds;
  87. struct pollinfo *inf;
  88. struct pollinfo *first_free;
  89. SIMPLE_PATTERN *access_list;
  90. void *(*add_callback)(POLLINFO *pi, short int *events, void *data);
  91. void (*del_callback)(POLLINFO *pi);
  92. int (*rcv_callback)(POLLINFO *pi, short int *events);
  93. int (*snd_callback)(POLLINFO *pi, short int *events);
  94. void (*tmr_callback)(void *timer_data);
  95. };
  96. #define pollinfo_from_slot(p, slot) (&((p)->inf[(slot)]))
  97. extern int poll_default_snd_callback(POLLINFO *pi, short int *events);
  98. extern int poll_default_rcv_callback(POLLINFO *pi, short int *events);
  99. extern void poll_default_del_callback(POLLINFO *pi);
  100. extern void *poll_default_add_callback(POLLINFO *pi, short int *events, void *data);
  101. extern POLLINFO *poll_add_fd(POLLJOB *p
  102. , int fd
  103. , int socktype
  104. , uint32_t flags
  105. , const char *client_ip
  106. , const char *client_port
  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 *data
  112. );
  113. extern void poll_close_fd(POLLINFO *pi);
  114. extern void poll_events(LISTEN_SOCKETS *sockets
  115. , void *(*add_callback)(POLLINFO *pi, short int *events, void *data)
  116. , void (*del_callback)(POLLINFO *pi)
  117. , int (*rcv_callback)(POLLINFO *pi, short int *events)
  118. , int (*snd_callback)(POLLINFO *pi, short int *events)
  119. , void (*tmr_callback)(void *timer_data)
  120. , SIMPLE_PATTERN *access_list
  121. , void *data
  122. , time_t tcp_request_timeout_seconds
  123. , time_t tcp_idle_timeout_seconds
  124. , time_t timer_milliseconds
  125. , void *timer_data
  126. , size_t max_tcp_sockets
  127. );
  128. #endif //NETDATA_SOCKET_H