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