lookip: Disconnect asynchronously to avoid dead-locking watcher unregistration
[strongswan.git] / src / libstrongswan / networking / streams / stream_service.h
1 /*
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 /**
17 * @defgroup stream_service stream_service
18 * @{ @ingroup streams
19 */
20
21 #ifndef STREAM_SERVICE_H_
22 #define STREAM_SERVICE_H_
23
24 typedef struct stream_service_t stream_service_t;
25
26 #include <library.h>
27 #include <processing/jobs/job.h>
28 #include <networking/streams/stream.h>
29
30 /**
31 * Constructor function prototype for stream_service_t.
32 *
33 * @param uri URI to create a stream for
34 * @param backlog size of the backlog queue, as passed to listen()
35 * @return stream instance, NULL on error
36 */
37 typedef stream_service_t*(*stream_service_constructor_t)(char *uri, int backlog);
38
39 /**
40 * Service callback routine for accepting client connections.
41 *
42 * The passed stream gets closed/destroyed by the callback caller, unless
43 * TRUE is returned.
44 *
45 * @param data user data, as passed during registration
46 * @param stream accept()ed client connection
47 * @return TRUE to keep stream alive, FALSE to destroy it
48 */
49 typedef bool (*stream_service_cb_t)(void *data, stream_t *stream);
50
51 /**
52 * A service accepting client connection streams.
53 */
54 struct stream_service_t {
55
56 /**
57 * Start accepting client connections on this stream service.
58 *
59 * To stop accepting connections, pass a NULL callback function.
60 *
61 * @param cb callback function to call for accepted client streams
62 * @param data data to pass to callback function
63 * @param prio job priority to run callback with
64 * @param cncrncy maximum number of parallel callback invocations
65 */
66 void (*on_accept)(stream_service_t *this,
67 stream_service_cb_t cb, void *data,
68 job_priority_t prio, u_int cncrncy);
69
70 /**
71 * Destroy a stream_service_t.
72 */
73 void (*destroy)(stream_service_t *this);
74 };
75
76 /**
77 * Create a service from a file descriptor.
78 *
79 * The file descriptor MUST be a socket.
80 *
81 * @param fd file descriptor to wrap into a stream_service_t
82 * @return stream_service instance
83 */
84 stream_service_t *stream_service_create_from_fd(int fd);
85
86 /**
87 * Create a service instance for UNIX sockets.
88 *
89 * @param uri UNIX socket specific URI, must start with "unix://"
90 * @param backlog size of the backlog queue, as passed to listen()
91 * @return stream_service instance, NULL on failure
92 */
93 stream_service_t *stream_service_create_unix(char *uri, int backlog);
94
95 /**
96 * Create a service instance for TCP sockets.
97 *
98 * @param uri TCP socket specific URI, must start with "tcp://"
99 * @param backlog size of the backlog queue, as passed to listen()
100 * @return stream_service instance, NULL on failure
101 */
102 stream_service_t *stream_service_create_tcp(char *uri, int backlog);
103
104 #endif /** STREAM_SERVICE_H_ @}*/