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