listener.h revision 1.1.1.2
1/* $NetBSD: listener.h,v 1.1.1.2 2015/01/29 06:38:25 spz Exp $ */ 2/* $NetBSD: listener.h,v 1.1.1.2 2015/01/29 06:38:25 spz Exp $ */ 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_ 30#define _EVENT2_LISTENER_H_ 31 32#ifdef __cplusplus 33extern "C" { 34#endif 35 36#include <event2/event.h> 37 38struct sockaddr; 39struct 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 */ 50typedef 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 */ 58typedef 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 75/** 76 Allocate a new evconnlistener object to listen for incoming TCP connections 77 on a given file descriptor. 78 79 @param base The event base to associate the listener with. 80 @param cb A callback to be invoked when a new connection arrives. If the 81 callback is NULL, the listener will be treated as disabled until the 82 callback is set. 83 @param ptr A user-supplied pointer to give to the callback. 84 @param flags Any number of LEV_OPT_* flags 85 @param backlog Passed to the listen() call to determine the length of the 86 acceptable connection backlog. Set to -1 for a reasonable default. 87 Set to 0 if the socket is already listening. 88 @param fd The file descriptor to listen on. It must be a nonblocking 89 file descriptor, and it should already be bound to an appropriate 90 port and address. 91*/ 92struct evconnlistener *evconnlistener_new(struct event_base *base, 93 evconnlistener_cb cb, void *ptr, unsigned flags, int backlog, 94 evutil_socket_t fd); 95/** 96 Allocate a new evconnlistener object to listen for incoming TCP connections 97 on a given address. 98 99 @param base The event base to associate the listener with. 100 @param cb A callback to be invoked when a new connection arrives. If the 101 callback is NULL, the listener will be treated as disabled until the 102 callback is set. 103 @param ptr A user-supplied pointer to give to the callback. 104 @param flags Any number of LEV_OPT_* flags 105 @param backlog Passed to the listen() call to determine the length of the 106 acceptable connection backlog. Set to -1 for a reasonable default. 107 @param addr The address to listen for connections on. 108 @param socklen The length of the address. 109 */ 110struct evconnlistener *evconnlistener_new_bind(struct event_base *base, 111 evconnlistener_cb cb, void *ptr, unsigned flags, int backlog, 112 const struct sockaddr *sa, int socklen); 113/** 114 Disable and deallocate an evconnlistener. 115 */ 116void evconnlistener_free(struct evconnlistener *lev); 117/** 118 Re-enable an evconnlistener that has been disabled. 119 */ 120int evconnlistener_enable(struct evconnlistener *lev); 121/** 122 Stop listening for connections on an evconnlistener. 123 */ 124int evconnlistener_disable(struct evconnlistener *lev); 125 126/** Return an evconnlistener's associated event_base. */ 127struct event_base *evconnlistener_get_base(struct evconnlistener *lev); 128 129/** Return the socket that an evconnlistner is listening on. */ 130evutil_socket_t evconnlistener_get_fd(struct evconnlistener *lev); 131 132/** Change the callback on the listener to cb and its user_data to arg. 133 */ 134void evconnlistener_set_cb(struct evconnlistener *lev, 135 evconnlistener_cb cb, void *arg); 136 137/** Set an evconnlistener's error callback. */ 138void evconnlistener_set_error_cb(struct evconnlistener *lev, 139 evconnlistener_errorcb errorcb); 140 141#ifdef __cplusplus 142} 143#endif 144 145#endif 146