stream: add backlog option to stream services, forward to listen()
[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 <networking/streams/stream.h>
27
28 /**
29 * Constructor function prototype for stream_service_t.
30 *
31 * @param uri URI to create a stream for
32 * @param backlog size of the backlog queue, as passed to listen()
33 * @return stream instance, NULL on error
34 */
35 typedef stream_service_t*(*stream_service_constructor_t)(char *uri, int backlog);
36
37 /**
38 * Service callback routine for accepting client connections.
39 *
40 * The passed stream_service gets closed/destroyed by the callback caller.
41 *
42 * @param data user data, as passed during registration
43 * @param stream accept()ed client connection
44 */
45 typedef void (*stream_service_cb_t)(void *data, stream_t *stream);
46
47 /**
48 * A service accepting client connection streams.
49 */
50 struct stream_service_t {
51
52 /**
53 * Start accepting client connections on this stream service.
54 *
55 * To stop accepting connections, pass a NULL callback function.
56 *
57 * @param cb callback function to call for accepted client streams
58 * @param data data to pass to callback function
59 */
60 void (*on_accept)(stream_service_t *this,
61 stream_service_cb_t cb, void *data);
62
63 /**
64 * Destroy a stream_service_t.
65 */
66 void (*destroy)(stream_service_t *this);
67 };
68
69 /**
70 * Create a service from a file descriptor.
71 *
72 * The file descriptor MUST be a socket.
73 *
74 * @param fd file descriptor to wrap into a stream_service_t
75 * @return stream_service instance
76 */
77 stream_service_t *stream_service_create_from_fd(int fd);
78
79 /**
80 * Create a service instance for UNIX sockets.
81 *
82 * @param uri UNIX socket specific URI, must start with "unix://"
83 * @param backlog size of the backlog queue, as passed to listen()
84 * @return stream_service instance, NULL on failure
85 */
86 stream_service_t *stream_service_create_unix(char *uri, int backlog);
87
88 /**
89 * Create a service instance for TCP sockets.
90 *
91 * @param uri TCP socket specific URI, must start with "tcp://"
92 * @param backlog size of the backlog queue, as passed to listen()
93 * @return stream_service instance, NULL on failure
94 */
95 stream_service_t *stream_service_create_tcp(char *uri, int backlog);
96
97 #endif /** STREAM_SERVICE_H_ @}*/