1 /* $NetBSD: listener.h,v 1.1.1.4 2021/04/07 02:43:14 christos Exp $ */ 2 /* 3 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 4 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 #ifndef EVENT2_LISTENER_H_INCLUDED_ 29 #define EVENT2_LISTENER_H_INCLUDED_ 30 31 #include <event2/visibility.h> 32 33 #ifdef __cplusplus 34 extern "C" { 35 #endif 36 37 #include <event2/event.h> 38 39 struct sockaddr; 40 struct evconnlistener; 41 42 /** 43 A callback that we invoke when a listener has a new connection. 44 45 @param listener The evconnlistener 46 @param fd The new file descriptor 47 @param addr The source address of the connection 48 @param socklen The length of addr 49 @param user_arg the pointer passed to evconnlistener_new() 50 */ 51 typedef void (*evconnlistener_cb)(struct evconnlistener *, evutil_socket_t, struct sockaddr *, int socklen, void *); 52 53 /** 54 A callback that we invoke when a listener encounters a non-retriable error. 55 56 @param listener The evconnlistener 57 @param user_arg the pointer passed to evconnlistener_new() 58 */ 59 typedef void (*evconnlistener_errorcb)(struct evconnlistener *, void *); 60 61 /** Flag: Indicates that we should not make incoming sockets nonblocking 62 * before passing them to the callback. */ 63 #define LEV_OPT_LEAVE_SOCKETS_BLOCKING (1u<<0) 64 /** Flag: Indicates that freeing the listener should close the underlying 65 * socket. */ 66 #define LEV_OPT_CLOSE_ON_FREE (1u<<1) 67 /** Flag: Indicates that we should set the close-on-exec flag, if possible */ 68 #define LEV_OPT_CLOSE_ON_EXEC (1u<<2) 69 /** Flag: Indicates that we should disable the timeout (if any) between when 70 * this socket is closed and when we can listen again on the same port. */ 71 #define LEV_OPT_REUSEABLE (1u<<3) 72 /** Flag: Indicates that the listener should be locked so it's safe to use 73 * from multiple threadcs at once. */ 74 #define LEV_OPT_THREADSAFE (1u<<4) 75 /** Flag: Indicates that the listener should be created in disabled 76 * state. Use evconnlistener_enable() to enable it later. */ 77 #define LEV_OPT_DISABLED (1u<<5) 78 /** Flag: Indicates that the listener should defer accept() until data is 79 * available, if possible. Ignored on platforms that do not support this. 80 * 81 * This option can help performance for protocols where the client transmits 82 * immediately after connecting. Do not use this option if your protocol 83 * _doesn't_ start out with the client transmitting data, since in that case 84 * this option will sometimes cause the kernel to never tell you about the 85 * connection. 86 * 87 * This option is only supported by evconnlistener_new_bind(): it can't 88 * work with evconnlistener_new_fd(), since the listener needs to be told 89 * to use the option before it is actually bound. 90 */ 91 #define LEV_OPT_DEFERRED_ACCEPT (1u<<6) 92 /** Flag: Indicates that we ask to allow multiple servers (processes or 93 * threads) to bind to the same port if they each set the option. 94 * 95 * SO_REUSEPORT is what most people would expect SO_REUSEADDR to be, however 96 * SO_REUSEPORT does not imply SO_REUSEADDR. 97 * 98 * This is only available on Linux and kernel 3.9+ 99 */ 100 #define LEV_OPT_REUSEABLE_PORT (1u<<7) 101 /** Flag: Indicates that the listener wants to work only in IPv6 socket. 102 * 103 * According to RFC3493 and most Linux distributions, default value is to 104 * work in IPv4-mapped mode. If there is a requirement to bind same port 105 * on same ip addresses but different handlers for both IPv4 and IPv6, 106 * it is required to set IPV6_V6ONLY socket option to be sure that the 107 * code works as expected without affected by bindv6only sysctl setting in 108 * system. 109 * 110 * This socket option also supported by Windows. 111 */ 112 #define LEV_OPT_BIND_IPV6ONLY (1u<<8) 113 114 /** 115 Allocate a new evconnlistener object to listen for incoming TCP connections 116 on a given file descriptor. 117 118 @param base The event base to associate the listener with. 119 @param cb A callback to be invoked when a new connection arrives. If the 120 callback is NULL, the listener will be treated as disabled until the 121 callback is set. 122 @param ptr A user-supplied pointer to give to the callback. 123 @param flags Any number of LEV_OPT_* flags 124 @param backlog Passed to the listen() call to determine the length of the 125 acceptable connection backlog. Set to -1 for a reasonable default. 126 Set to 0 if the socket is already listening. 127 @param fd The file descriptor to listen on. It must be a nonblocking 128 file descriptor, and it should already be bound to an appropriate 129 port and address. 130 */ 131 EVENT2_EXPORT_SYMBOL 132 struct evconnlistener *evconnlistener_new(struct event_base *base, 133 evconnlistener_cb cb, void *ptr, unsigned flags, int backlog, 134 evutil_socket_t fd); 135 /** 136 Allocate a new evconnlistener object to listen for incoming TCP connections 137 on a given address. 138 139 @param base The event base to associate the listener with. 140 @param cb A callback to be invoked when a new connection arrives. If the 141 callback is NULL, the listener will be treated as disabled until the 142 callback is set. 143 @param ptr A user-supplied pointer to give to the callback. 144 @param flags Any number of LEV_OPT_* flags 145 @param backlog Passed to the listen() call to determine the length of the 146 acceptable connection backlog. Set to -1 for a reasonable default. 147 @param addr The address to listen for connections on. 148 @param socklen The length of the address. 149 */ 150 EVENT2_EXPORT_SYMBOL 151 struct evconnlistener *evconnlistener_new_bind(struct event_base *base, 152 evconnlistener_cb cb, void *ptr, unsigned flags, int backlog, 153 const struct sockaddr *sa, int socklen); 154 /** 155 Disable and deallocate an evconnlistener. 156 */ 157 EVENT2_EXPORT_SYMBOL 158 void evconnlistener_free(struct evconnlistener *lev); 159 /** 160 Re-enable an evconnlistener that has been disabled. 161 */ 162 EVENT2_EXPORT_SYMBOL 163 int evconnlistener_enable(struct evconnlistener *lev); 164 /** 165 Stop listening for connections on an evconnlistener. 166 */ 167 EVENT2_EXPORT_SYMBOL 168 int evconnlistener_disable(struct evconnlistener *lev); 169 170 /** Return an evconnlistener's associated event_base. */ 171 EVENT2_EXPORT_SYMBOL 172 struct event_base *evconnlistener_get_base(struct evconnlistener *lev); 173 174 /** Return the socket that an evconnlistner is listening on. */ 175 EVENT2_EXPORT_SYMBOL 176 evutil_socket_t evconnlistener_get_fd(struct evconnlistener *lev); 177 178 /** Change the callback on the listener to cb and its user_data to arg. 179 */ 180 EVENT2_EXPORT_SYMBOL 181 void evconnlistener_set_cb(struct evconnlistener *lev, 182 evconnlistener_cb cb, void *arg); 183 184 /** Set an evconnlistener's error callback. */ 185 EVENT2_EXPORT_SYMBOL 186 void evconnlistener_set_error_cb(struct evconnlistener *lev, 187 evconnlistener_errorcb errorcb); 188 189 #ifdef __cplusplus 190 } 191 #endif 192 193 #endif 194