Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:12

0001 /*
0002  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
0003  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions
0007  * are met:
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  * 3. The name of the author may not be used to endorse or promote products
0014  *    derived from this software without specific prior written permission.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 #ifndef EVENT2_LISTENER_H_INCLUDED_
0028 #define EVENT2_LISTENER_H_INCLUDED_
0029 
0030 #include <event2/visibility.h>
0031 
0032 #ifdef __cplusplus
0033 extern "C" {
0034 #endif
0035 
0036 #include <event2/event.h>
0037 
0038 struct sockaddr;
0039 struct evconnlistener;
0040 
0041 /**
0042    A callback that we invoke when a listener has a new connection.
0043 
0044    @param listener The evconnlistener
0045    @param fd The new file descriptor
0046    @param addr The source address of the connection
0047    @param socklen The length of addr
0048    @param user_arg the pointer passed to evconnlistener_new()
0049  */
0050 typedef void (*evconnlistener_cb)(struct evconnlistener *, evutil_socket_t, struct sockaddr *, int socklen, void *);
0051 
0052 /**
0053    A callback that we invoke when a listener encounters a non-retriable error.
0054 
0055    @param listener The evconnlistener
0056    @param user_arg the pointer passed to evconnlistener_new()
0057  */
0058 typedef void (*evconnlistener_errorcb)(struct evconnlistener *, void *);
0059 
0060 /** Flag: Indicates that we should not make incoming sockets nonblocking
0061  * before passing them to the callback. */
0062 #define LEV_OPT_LEAVE_SOCKETS_BLOCKING  (1u<<0)
0063 /** Flag: Indicates that freeing the listener should close the underlying
0064  * socket. */
0065 #define LEV_OPT_CLOSE_ON_FREE       (1u<<1)
0066 /** Flag: Indicates that we should set the close-on-exec flag, if possible */
0067 #define LEV_OPT_CLOSE_ON_EXEC       (1u<<2)
0068 /** Flag: Indicates that we should disable the timeout (if any) between when
0069  * this socket is closed and when we can listen again on the same port. */
0070 #define LEV_OPT_REUSEABLE       (1u<<3)
0071 /** Flag: Indicates that the listener should be locked so it's safe to use
0072  * from multiple threadcs at once. */
0073 #define LEV_OPT_THREADSAFE      (1u<<4)
0074 /** Flag: Indicates that the listener should be created in disabled
0075  * state. Use evconnlistener_enable() to enable it later. */
0076 #define LEV_OPT_DISABLED        (1u<<5)
0077 /** Flag: Indicates that the listener should defer accept() until data is
0078  * available, if possible.  Ignored on platforms that do not support this.
0079  *
0080  * This option can help performance for protocols where the client transmits
0081  * immediately after connecting.  Do not use this option if your protocol
0082  * _doesn't_ start out with the client transmitting data, since in that case
0083  * this option will sometimes cause the kernel to never tell you about the
0084  * connection.
0085  *
0086  * This option is only supported by evconnlistener_new_bind(): it can't
0087  * work with evconnlistener_new_fd(), since the listener needs to be told
0088  * to use the option before it is actually bound.
0089  */
0090 #define LEV_OPT_DEFERRED_ACCEPT     (1u<<6)
0091 /** Flag: Indicates that we ask to allow multiple servers (processes or
0092  * threads) to bind to the same port if they each set the option. 
0093  * 
0094  * SO_REUSEPORT is what most people would expect SO_REUSEADDR to be, however
0095  * SO_REUSEPORT does not imply SO_REUSEADDR.
0096  *
0097  * This is only available on Linux and kernel 3.9+
0098  */
0099 #define LEV_OPT_REUSEABLE_PORT      (1u<<7)
0100 /** Flag: Indicates that the listener wants to work only in IPv6 socket.
0101  *
0102  * According to RFC3493 and most Linux distributions, default value is to
0103  * work in IPv4-mapped mode. If there is a requirement to bind same port
0104  * on same ip addresses but different handlers for both IPv4 and IPv6,
0105  * it is required to set IPV6_V6ONLY socket option to be sure that the
0106  * code works as expected without affected by bindv6only sysctl setting in
0107  * system.
0108  *
0109  * This socket option also supported by Windows.
0110  */
0111 #define LEV_OPT_BIND_IPV6ONLY       (1u<<8)
0112 
0113 /**
0114    Allocate a new evconnlistener object to listen for incoming TCP connections
0115    on a given file descriptor.
0116 
0117    @param base The event base to associate the listener with.
0118    @param cb A callback to be invoked when a new connection arrives.  If the
0119       callback is NULL, the listener will be treated as disabled until the
0120       callback is set.
0121    @param ptr A user-supplied pointer to give to the callback.
0122    @param flags Any number of LEV_OPT_* flags
0123    @param backlog Passed to the listen() call to determine the length of the
0124       acceptable connection backlog.  Set to -1 for a reasonable default.
0125       Set to 0 if the socket is already listening.
0126    @param fd The file descriptor to listen on.  It must be a nonblocking
0127       file descriptor, and it should already be bound to an appropriate
0128       port and address.
0129 */
0130 EVENT2_EXPORT_SYMBOL
0131 struct evconnlistener *evconnlistener_new(struct event_base *base,
0132     evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
0133     evutil_socket_t fd);
0134 /**
0135    Allocate a new evconnlistener object to listen for incoming TCP connections
0136    on a given address.
0137 
0138    @param base The event base to associate the listener with.
0139    @param cb A callback to be invoked when a new connection arrives. If the
0140       callback is NULL, the listener will be treated as disabled until the
0141       callback is set.
0142    @param ptr A user-supplied pointer to give to the callback.
0143    @param flags Any number of LEV_OPT_* flags
0144    @param backlog Passed to the listen() call to determine the length of the
0145       acceptable connection backlog.  Set to -1 for a reasonable default.
0146    @param addr The address to listen for connections on.
0147    @param socklen The length of the address.
0148  */
0149 EVENT2_EXPORT_SYMBOL
0150 struct evconnlistener *evconnlistener_new_bind(struct event_base *base,
0151     evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
0152     const struct sockaddr *sa, int socklen);
0153 /**
0154    Disable and deallocate an evconnlistener.
0155  */
0156 EVENT2_EXPORT_SYMBOL
0157 void evconnlistener_free(struct evconnlistener *lev);
0158 /**
0159    Re-enable an evconnlistener that has been disabled.
0160  */
0161 EVENT2_EXPORT_SYMBOL
0162 int evconnlistener_enable(struct evconnlistener *lev);
0163 /**
0164    Stop listening for connections on an evconnlistener.
0165  */
0166 EVENT2_EXPORT_SYMBOL
0167 int evconnlistener_disable(struct evconnlistener *lev);
0168 
0169 /** Return an evconnlistener's associated event_base. */
0170 EVENT2_EXPORT_SYMBOL
0171 struct event_base *evconnlistener_get_base(struct evconnlistener *lev);
0172 
0173 /** Return the socket that an evconnlistner is listening on. */
0174 EVENT2_EXPORT_SYMBOL
0175 evutil_socket_t evconnlistener_get_fd(struct evconnlistener *lev);
0176 
0177 /** Change the callback on the listener to cb and its user_data to arg.
0178  */
0179 EVENT2_EXPORT_SYMBOL
0180 void evconnlistener_set_cb(struct evconnlistener *lev,
0181     evconnlistener_cb cb, void *arg);
0182 
0183 /** Set an evconnlistener's error callback. */
0184 EVENT2_EXPORT_SYMBOL
0185 void evconnlistener_set_error_cb(struct evconnlistener *lev,
0186     evconnlistener_errorcb errorcb);
0187 
0188 #ifdef __cplusplus
0189 }
0190 #endif
0191 
0192 #endif