1275970Scy/* 2275970Scy * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 3275970Scy * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 4275970Scy * 5275970Scy * Redistribution and use in source and binary forms, with or without 6275970Scy * modification, are permitted provided that the following conditions 7275970Scy * are met: 8275970Scy * 1. Redistributions of source code must retain the above copyright 9275970Scy * notice, this list of conditions and the following disclaimer. 10275970Scy * 2. Redistributions in binary form must reproduce the above copyright 11275970Scy * notice, this list of conditions and the following disclaimer in the 12275970Scy * documentation and/or other materials provided with the distribution. 13275970Scy * 3. The name of the author may not be used to endorse or promote products 14275970Scy * derived from this software without specific prior written permission. 15275970Scy * 16275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26275970Scy */ 27275970Scy#ifndef EVENT2_LISTENER_H_INCLUDED_ 28275970Scy#define EVENT2_LISTENER_H_INCLUDED_ 29275970Scy 30275970Scy#include <event2/visibility.h> 31275970Scy 32275970Scy#ifdef __cplusplus 33275970Scyextern "C" { 34275970Scy#endif 35275970Scy 36275970Scy#include <event2/event.h> 37275970Scy 38275970Scystruct sockaddr; 39275970Scystruct evconnlistener; 40275970Scy 41275970Scy/** 42275970Scy A callback that we invoke when a listener has a new connection. 43275970Scy 44275970Scy @param listener The evconnlistener 45275970Scy @param fd The new file descriptor 46275970Scy @param addr The source address of the connection 47275970Scy @param socklen The length of addr 48275970Scy @param user_arg the pointer passed to evconnlistener_new() 49275970Scy */ 50275970Scytypedef void (*evconnlistener_cb)(struct evconnlistener *, evutil_socket_t, struct sockaddr *, int socklen, void *); 51275970Scy 52275970Scy/** 53275970Scy A callback that we invoke when a listener encounters a non-retriable error. 54275970Scy 55275970Scy @param listener The evconnlistener 56275970Scy @param user_arg the pointer passed to evconnlistener_new() 57275970Scy */ 58275970Scytypedef void (*evconnlistener_errorcb)(struct evconnlistener *, void *); 59275970Scy 60275970Scy/** Flag: Indicates that we should not make incoming sockets nonblocking 61275970Scy * before passing them to the callback. */ 62275970Scy#define LEV_OPT_LEAVE_SOCKETS_BLOCKING (1u<<0) 63275970Scy/** Flag: Indicates that freeing the listener should close the underlying 64275970Scy * socket. */ 65275970Scy#define LEV_OPT_CLOSE_ON_FREE (1u<<1) 66275970Scy/** Flag: Indicates that we should set the close-on-exec flag, if possible */ 67275970Scy#define LEV_OPT_CLOSE_ON_EXEC (1u<<2) 68275970Scy/** Flag: Indicates that we should disable the timeout (if any) between when 69275970Scy * this socket is closed and when we can listen again on the same port. */ 70275970Scy#define LEV_OPT_REUSEABLE (1u<<3) 71275970Scy/** Flag: Indicates that the listener should be locked so it's safe to use 72275970Scy * from multiple threadcs at once. */ 73275970Scy#define LEV_OPT_THREADSAFE (1u<<4) 74275970Scy/** Flag: Indicates that the listener should be created in disabled 75275970Scy * state. Use evconnlistener_enable() to enable it later. */ 76275970Scy#define LEV_OPT_DISABLED (1u<<5) 77275970Scy/** Flag: Indicates that the listener should defer accept() until data is 78275970Scy * available, if possible. Ignored on platforms that do not support this. 79275970Scy * 80275970Scy * This option can help performance for protocols where the client transmits 81275970Scy * immediately after connecting. Do not use this option if your protocol 82275970Scy * _doesn't_ start out with the client transmitting data, since in that case 83275970Scy * this option will sometimes cause the kernel to never tell you about the 84275970Scy * connection. 85275970Scy * 86275970Scy * This option is only supported by evconnlistener_new_bind(): it can't 87275970Scy * work with evconnlistener_new_fd(), since the listener needs to be told 88275970Scy * to use the option before it is actually bound. 89275970Scy */ 90275970Scy#define LEV_OPT_DEFERRED_ACCEPT (1u<<6) 91285612Sdelphij/** Flag: Indicates that we ask to allow multiple servers (processes or 92285612Sdelphij * threads) to bind to the same port if they each set the option. 93285612Sdelphij * 94285612Sdelphij * SO_REUSEPORT is what most people would expect SO_REUSEADDR to be, however 95285612Sdelphij * SO_REUSEPORT does not imply SO_REUSEADDR. 96285612Sdelphij * 97285612Sdelphij * This is only available on Linux and kernel 3.9+ 98285612Sdelphij */ 99285612Sdelphij#define LEV_OPT_REUSEABLE_PORT (1u<<7) 100275970Scy 101275970Scy/** 102275970Scy Allocate a new evconnlistener object to listen for incoming TCP connections 103275970Scy on a given file descriptor. 104275970Scy 105275970Scy @param base The event base to associate the listener with. 106275970Scy @param cb A callback to be invoked when a new connection arrives. If the 107275970Scy callback is NULL, the listener will be treated as disabled until the 108275970Scy callback is set. 109275970Scy @param ptr A user-supplied pointer to give to the callback. 110275970Scy @param flags Any number of LEV_OPT_* flags 111275970Scy @param backlog Passed to the listen() call to determine the length of the 112275970Scy acceptable connection backlog. Set to -1 for a reasonable default. 113275970Scy Set to 0 if the socket is already listening. 114275970Scy @param fd The file descriptor to listen on. It must be a nonblocking 115275970Scy file descriptor, and it should already be bound to an appropriate 116275970Scy port and address. 117275970Scy*/ 118275970ScyEVENT2_EXPORT_SYMBOL 119275970Scystruct evconnlistener *evconnlistener_new(struct event_base *base, 120275970Scy evconnlistener_cb cb, void *ptr, unsigned flags, int backlog, 121275970Scy evutil_socket_t fd); 122275970Scy/** 123275970Scy Allocate a new evconnlistener object to listen for incoming TCP connections 124275970Scy on a given address. 125275970Scy 126275970Scy @param base The event base to associate the listener with. 127275970Scy @param cb A callback to be invoked when a new connection arrives. If the 128275970Scy callback is NULL, the listener will be treated as disabled until the 129275970Scy callback is set. 130275970Scy @param ptr A user-supplied pointer to give to the callback. 131275970Scy @param flags Any number of LEV_OPT_* flags 132275970Scy @param backlog Passed to the listen() call to determine the length of the 133275970Scy acceptable connection backlog. Set to -1 for a reasonable default. 134275970Scy @param addr The address to listen for connections on. 135275970Scy @param socklen The length of the address. 136275970Scy */ 137275970ScyEVENT2_EXPORT_SYMBOL 138275970Scystruct evconnlistener *evconnlistener_new_bind(struct event_base *base, 139275970Scy evconnlistener_cb cb, void *ptr, unsigned flags, int backlog, 140275970Scy const struct sockaddr *sa, int socklen); 141275970Scy/** 142275970Scy Disable and deallocate an evconnlistener. 143275970Scy */ 144275970ScyEVENT2_EXPORT_SYMBOL 145275970Scyvoid evconnlistener_free(struct evconnlistener *lev); 146275970Scy/** 147275970Scy Re-enable an evconnlistener that has been disabled. 148275970Scy */ 149275970ScyEVENT2_EXPORT_SYMBOL 150275970Scyint evconnlistener_enable(struct evconnlistener *lev); 151275970Scy/** 152275970Scy Stop listening for connections on an evconnlistener. 153275970Scy */ 154275970ScyEVENT2_EXPORT_SYMBOL 155275970Scyint evconnlistener_disable(struct evconnlistener *lev); 156275970Scy 157275970Scy/** Return an evconnlistener's associated event_base. */ 158275970ScyEVENT2_EXPORT_SYMBOL 159275970Scystruct event_base *evconnlistener_get_base(struct evconnlistener *lev); 160275970Scy 161275970Scy/** Return the socket that an evconnlistner is listening on. */ 162275970ScyEVENT2_EXPORT_SYMBOL 163275970Scyevutil_socket_t evconnlistener_get_fd(struct evconnlistener *lev); 164275970Scy 165275970Scy/** Change the callback on the listener to cb and its user_data to arg. 166275970Scy */ 167275970ScyEVENT2_EXPORT_SYMBOL 168275970Scyvoid evconnlistener_set_cb(struct evconnlistener *lev, 169275970Scy evconnlistener_cb cb, void *arg); 170275970Scy 171275970Scy/** Set an evconnlistener's error callback. */ 172275970ScyEVENT2_EXPORT_SYMBOL 173275970Scyvoid evconnlistener_set_error_cb(struct evconnlistener *lev, 174275970Scy evconnlistener_errorcb errorcb); 175275970Scy 176275970Scy#ifdef __cplusplus 177275970Scy} 178275970Scy#endif 179275970Scy 180275970Scy#endif 181