1290001Sglebius/*
2290001Sglebius * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3290001Sglebius * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4290001Sglebius *
5290001Sglebius * Redistribution and use in source and binary forms, with or without
6290001Sglebius * modification, are permitted provided that the following conditions
7290001Sglebius * are met:
8290001Sglebius * 1. Redistributions of source code must retain the above copyright
9290001Sglebius *    notice, this list of conditions and the following disclaimer.
10290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright
11290001Sglebius *    notice, this list of conditions and the following disclaimer in the
12290001Sglebius *    documentation and/or other materials provided with the distribution.
13290001Sglebius * 3. The name of the author may not be used to endorse or promote products
14290001Sglebius *    derived from this software without specific prior written permission.
15290001Sglebius *
16290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17290001Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18290001Sglebius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19290001Sglebius * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20290001Sglebius * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21290001Sglebius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22290001Sglebius * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23290001Sglebius * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24290001Sglebius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25290001Sglebius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26290001Sglebius */
27290001Sglebius#ifndef EVENT2_LISTENER_H_INCLUDED_
28290001Sglebius#define EVENT2_LISTENER_H_INCLUDED_
29290001Sglebius
30290001Sglebius#include <event2/visibility.h>
31290001Sglebius
32290001Sglebius#ifdef __cplusplus
33290001Sglebiusextern "C" {
34290001Sglebius#endif
35290001Sglebius
36290001Sglebius#include <event2/event.h>
37290001Sglebius
38290001Sglebiusstruct sockaddr;
39290001Sglebiusstruct evconnlistener;
40290001Sglebius
41290001Sglebius/**
42290001Sglebius   A callback that we invoke when a listener has a new connection.
43290001Sglebius
44290001Sglebius   @param listener The evconnlistener
45290001Sglebius   @param fd The new file descriptor
46290001Sglebius   @param addr The source address of the connection
47290001Sglebius   @param socklen The length of addr
48290001Sglebius   @param user_arg the pointer passed to evconnlistener_new()
49290001Sglebius */
50290001Sglebiustypedef void (*evconnlistener_cb)(struct evconnlistener *, evutil_socket_t, struct sockaddr *, int socklen, void *);
51290001Sglebius
52290001Sglebius/**
53290001Sglebius   A callback that we invoke when a listener encounters a non-retriable error.
54290001Sglebius
55290001Sglebius   @param listener The evconnlistener
56290001Sglebius   @param user_arg the pointer passed to evconnlistener_new()
57290001Sglebius */
58290001Sglebiustypedef void (*evconnlistener_errorcb)(struct evconnlistener *, void *);
59290001Sglebius
60290001Sglebius/** Flag: Indicates that we should not make incoming sockets nonblocking
61290001Sglebius * before passing them to the callback. */
62290001Sglebius#define LEV_OPT_LEAVE_SOCKETS_BLOCKING	(1u<<0)
63290001Sglebius/** Flag: Indicates that freeing the listener should close the underlying
64290001Sglebius * socket. */
65290001Sglebius#define LEV_OPT_CLOSE_ON_FREE		(1u<<1)
66290001Sglebius/** Flag: Indicates that we should set the close-on-exec flag, if possible */
67290001Sglebius#define LEV_OPT_CLOSE_ON_EXEC		(1u<<2)
68290001Sglebius/** Flag: Indicates that we should disable the timeout (if any) between when
69290001Sglebius * this socket is closed and when we can listen again on the same port. */
70290001Sglebius#define LEV_OPT_REUSEABLE		(1u<<3)
71290001Sglebius/** Flag: Indicates that the listener should be locked so it's safe to use
72290001Sglebius * from multiple threadcs at once. */
73290001Sglebius#define LEV_OPT_THREADSAFE		(1u<<4)
74290001Sglebius/** Flag: Indicates that the listener should be created in disabled
75290001Sglebius * state. Use evconnlistener_enable() to enable it later. */
76290001Sglebius#define LEV_OPT_DISABLED		(1u<<5)
77290001Sglebius/** Flag: Indicates that the listener should defer accept() until data is
78290001Sglebius * available, if possible.  Ignored on platforms that do not support this.
79290001Sglebius *
80290001Sglebius * This option can help performance for protocols where the client transmits
81290001Sglebius * immediately after connecting.  Do not use this option if your protocol
82290001Sglebius * _doesn't_ start out with the client transmitting data, since in that case
83290001Sglebius * this option will sometimes cause the kernel to never tell you about the
84290001Sglebius * connection.
85290001Sglebius *
86290001Sglebius * This option is only supported by evconnlistener_new_bind(): it can't
87290001Sglebius * work with evconnlistener_new_fd(), since the listener needs to be told
88290001Sglebius * to use the option before it is actually bound.
89290001Sglebius */
90290001Sglebius#define LEV_OPT_DEFERRED_ACCEPT		(1u<<6)
91290001Sglebius/** Flag: Indicates that we ask to allow multiple servers (processes or
92290001Sglebius * threads) to bind to the same port if they each set the option.
93290001Sglebius *
94290001Sglebius * SO_REUSEPORT is what most people would expect SO_REUSEADDR to be, however
95290001Sglebius * SO_REUSEPORT does not imply SO_REUSEADDR.
96290001Sglebius *
97290001Sglebius * This is only available on Linux and kernel 3.9+
98290001Sglebius */
99290001Sglebius#define LEV_OPT_REUSEABLE_PORT		(1u<<7)
100290001Sglebius
101290001Sglebius/**
102290001Sglebius   Allocate a new evconnlistener object to listen for incoming TCP connections
103290001Sglebius   on a given file descriptor.
104290001Sglebius
105290001Sglebius   @param base The event base to associate the listener with.
106290001Sglebius   @param cb A callback to be invoked when a new connection arrives.  If the
107290001Sglebius      callback is NULL, the listener will be treated as disabled until the
108290001Sglebius      callback is set.
109290001Sglebius   @param ptr A user-supplied pointer to give to the callback.
110290001Sglebius   @param flags Any number of LEV_OPT_* flags
111290001Sglebius   @param backlog Passed to the listen() call to determine the length of the
112290001Sglebius      acceptable connection backlog.  Set to -1 for a reasonable default.
113290001Sglebius      Set to 0 if the socket is already listening.
114290001Sglebius   @param fd The file descriptor to listen on.  It must be a nonblocking
115290001Sglebius      file descriptor, and it should already be bound to an appropriate
116290001Sglebius      port and address.
117290001Sglebius*/
118290001SglebiusEVENT2_EXPORT_SYMBOL
119290001Sglebiusstruct evconnlistener *evconnlistener_new(struct event_base *base,
120290001Sglebius    evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
121290001Sglebius    evutil_socket_t fd);
122290001Sglebius/**
123290001Sglebius   Allocate a new evconnlistener object to listen for incoming TCP connections
124290001Sglebius   on a given address.
125290001Sglebius
126290001Sglebius   @param base The event base to associate the listener with.
127290001Sglebius   @param cb A callback to be invoked when a new connection arrives. If the
128290001Sglebius      callback is NULL, the listener will be treated as disabled until the
129290001Sglebius      callback is set.
130290001Sglebius   @param ptr A user-supplied pointer to give to the callback.
131290001Sglebius   @param flags Any number of LEV_OPT_* flags
132290001Sglebius   @param backlog Passed to the listen() call to determine the length of the
133290001Sglebius      acceptable connection backlog.  Set to -1 for a reasonable default.
134290001Sglebius   @param addr The address to listen for connections on.
135290001Sglebius   @param socklen The length of the address.
136290001Sglebius */
137290001SglebiusEVENT2_EXPORT_SYMBOL
138290001Sglebiusstruct evconnlistener *evconnlistener_new_bind(struct event_base *base,
139290001Sglebius    evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
140290001Sglebius    const struct sockaddr *sa, int socklen);
141290001Sglebius/**
142290001Sglebius   Disable and deallocate an evconnlistener.
143290001Sglebius */
144290001SglebiusEVENT2_EXPORT_SYMBOL
145290001Sglebiusvoid evconnlistener_free(struct evconnlistener *lev);
146290001Sglebius/**
147290001Sglebius   Re-enable an evconnlistener that has been disabled.
148290001Sglebius */
149290001SglebiusEVENT2_EXPORT_SYMBOL
150290001Sglebiusint evconnlistener_enable(struct evconnlistener *lev);
151290001Sglebius/**
152290001Sglebius   Stop listening for connections on an evconnlistener.
153290001Sglebius */
154290001SglebiusEVENT2_EXPORT_SYMBOL
155290001Sglebiusint evconnlistener_disable(struct evconnlistener *lev);
156290001Sglebius
157290001Sglebius/** Return an evconnlistener's associated event_base. */
158290001SglebiusEVENT2_EXPORT_SYMBOL
159290001Sglebiusstruct event_base *evconnlistener_get_base(struct evconnlistener *lev);
160290001Sglebius
161290001Sglebius/** Return the socket that an evconnlistner is listening on. */
162290001SglebiusEVENT2_EXPORT_SYMBOL
163290001Sglebiusevutil_socket_t evconnlistener_get_fd(struct evconnlistener *lev);
164290001Sglebius
165290001Sglebius/** Change the callback on the listener to cb and its user_data to arg.
166290001Sglebius */
167290001SglebiusEVENT2_EXPORT_SYMBOL
168290001Sglebiusvoid evconnlistener_set_cb(struct evconnlistener *lev,
169290001Sglebius    evconnlistener_cb cb, void *arg);
170290001Sglebius
171290001Sglebius/** Set an evconnlistener's error callback. */
172290001SglebiusEVENT2_EXPORT_SYMBOL
173290001Sglebiusvoid evconnlistener_set_error_cb(struct evconnlistener *lev,
174290001Sglebius    evconnlistener_errorcb errorcb);
175290001Sglebius
176290001Sglebius#ifdef __cplusplus
177290001Sglebius}
178290001Sglebius#endif
179290001Sglebius
180290001Sglebius#endif
181