ap_listen.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file ap_listen.h
  18. * @brief Apache Listeners Library
  19. *
  20. * @defgroup APACHE_CORE_LISTEN Apache Listeners Library
  21. * @ingroup APACHE_CORE
  22. * @{
  23. */
  24. #ifndef AP_LISTEN_H
  25. #define AP_LISTEN_H
  26. #include "apr_network_io.h"
  27. #include "httpd.h"
  28. #include "http_config.h"
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. typedef struct ap_slave_t ap_slave_t;
  33. typedef struct ap_listen_rec ap_listen_rec;
  34. typedef apr_status_t (*accept_function)(void **csd, ap_listen_rec *lr, apr_pool_t *ptrans);
  35. /**
  36. * @brief Apache's listeners record.
  37. *
  38. * These are used in the Multi-Processing Modules
  39. * to setup all of the sockets for the MPM to listen to and accept on.
  40. */
  41. struct ap_listen_rec {
  42. /**
  43. * The next listener in the list
  44. */
  45. ap_listen_rec *next;
  46. /**
  47. * The actual socket
  48. */
  49. apr_socket_t *sd;
  50. /**
  51. * The sockaddr the socket should bind to
  52. */
  53. apr_sockaddr_t *bind_addr;
  54. /**
  55. * The accept function for this socket
  56. */
  57. accept_function accept_func;
  58. /**
  59. * Is this socket currently active
  60. */
  61. int active;
  62. /**
  63. * The default protocol for this listening socket.
  64. */
  65. const char* protocol;
  66. ap_slave_t *slave;
  67. };
  68. /**
  69. * The global list of ap_listen_rec structures
  70. */
  71. AP_DECLARE_DATA extern ap_listen_rec *ap_listeners;
  72. AP_DECLARE_DATA extern int ap_num_listen_buckets;
  73. AP_DECLARE_DATA extern int ap_have_so_reuseport;
  74. /**
  75. * Setup all of the defaults for the listener list
  76. */
  77. AP_DECLARE(void) ap_listen_pre_config(void);
  78. /**
  79. * Loop through the global ap_listen_rec list and create all of the required
  80. * sockets. This executes the listen and bind on the sockets.
  81. * @param s The global server_rec
  82. * @return The number of open sockets.
  83. */
  84. AP_DECLARE(int) ap_setup_listeners(server_rec *s);
  85. /**
  86. * This function duplicates ap_listeners into multiple buckets when configured
  87. * to (see ListenCoresBucketsRatio) and the platform supports it (eg. number of
  88. * online CPU cores and SO_REUSEPORT available).
  89. * @param p The config pool
  90. * @param s The global server_rec
  91. * @param buckets The array of listeners buckets.
  92. * @param num_buckets The total number of listeners buckets (array size).
  93. * @remark If the given *num_buckets is 0 (input), it will be computed
  94. * according to the platform capacities, otherwise (positive) it
  95. * will be preserved. The number of listeners duplicated will
  96. * always match *num_buckets, be it computed or given.
  97. */
  98. AP_DECLARE(apr_status_t) ap_duplicate_listeners(apr_pool_t *p, server_rec *s,
  99. ap_listen_rec ***buckets,
  100. int *num_buckets);
  101. /**
  102. * Loop through the global ap_listen_rec list and close each of the sockets.
  103. */
  104. AP_DECLARE_NONSTD(void) ap_close_listeners(void);
  105. /**
  106. * Loop through the given ap_listen_rec list and close each of the sockets.
  107. * @param listeners The listener to close.
  108. */
  109. AP_DECLARE_NONSTD(void) ap_close_listeners_ex(ap_listen_rec *listeners);
  110. /**
  111. * FIXMEDOC
  112. */
  113. AP_DECLARE_NONSTD(int) ap_close_selected_listeners(ap_slave_t *);
  114. /* Although these functions are exported from libmain, they are not really
  115. * public functions. These functions are actually called while parsing the
  116. * config file, when one of the LISTEN_COMMANDS directives is read. These
  117. * should not ever be called by external modules. ALL MPMs should include
  118. * LISTEN_COMMANDS in their command_rec table so that these functions are
  119. * called.
  120. */
  121. AP_DECLARE_NONSTD(const char *) ap_set_listenbacklog(cmd_parms *cmd, void *dummy, const char *arg);
  122. AP_DECLARE_NONSTD(const char *) ap_set_listencbratio(cmd_parms *cmd, void *dummy, const char *arg);
  123. AP_DECLARE_NONSTD(const char *) ap_set_listener(cmd_parms *cmd, void *dummy,
  124. int argc, char *const argv[]);
  125. AP_DECLARE_NONSTD(const char *) ap_set_send_buffer_size(cmd_parms *cmd, void *dummy,
  126. const char *arg);
  127. AP_DECLARE_NONSTD(const char *) ap_set_receive_buffer_size(cmd_parms *cmd,
  128. void *dummy,
  129. const char *arg);
  130. #define LISTEN_COMMANDS \
  131. AP_INIT_TAKE1("ListenBacklog", ap_set_listenbacklog, NULL, RSRC_CONF, \
  132. "Maximum length of the queue of pending connections, as used by listen(2)"), \
  133. AP_INIT_TAKE1("ListenCoresBucketsRatio", ap_set_listencbratio, NULL, RSRC_CONF, \
  134. "Ratio between the number of CPU cores (online) and the number of listeners buckets"), \
  135. AP_INIT_TAKE_ARGV("Listen", ap_set_listener, NULL, RSRC_CONF, \
  136. "A port number or a numeric IP address and a port number, and an optional protocol"), \
  137. AP_INIT_TAKE1("SendBufferSize", ap_set_send_buffer_size, NULL, RSRC_CONF, \
  138. "Send buffer size in bytes"), \
  139. AP_INIT_TAKE1("ReceiveBufferSize", ap_set_receive_buffer_size, NULL, \
  140. RSRC_CONF, "Receive buffer size in bytes")
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #endif
  145. /** @} */