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