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