stream: replace print/vprint() convenience functions by a FILE* getter
[strongswan.git] / src / libstrongswan / networking / streams / stream.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 stream
18 * @{ @ingroup streams
19 */
20
21 #ifndef STREAM_H_
22 #define STREAM_H_
23
24 typedef struct stream_t stream_t;
25
26 #include <library.h>
27
28 #include <sys/un.h>
29 #include <sys/socket.h>
30
31 /**
32 * Constructor function prototype for stream_t.
33 *
34 * @param uri URI to create a stream for
35 * @return stream instance, NULL on error
36 */
37 typedef stream_t*(*stream_constructor_t)(char *uri);
38
39 /**
40 * Callback function prototype, called when stream is ready.
41 *
42 * It is not allowed to destroy the stream during the callback, this would
43 * deadlock. Instead, return FALSE to destroy the stream. It is not allowed
44 * to call on_read()/on_write() during this callback.
45 *
46 * As select() may return even if a read()/write() would actually block, it is
47 * recommended to use the non-blocking calls and handle return values
48 * appropriately.
49 *
50 * @param data data passed during callback registration
51 * @param stream associated stream
52 * @return FALSE to destroy the stream
53 */
54 typedef bool (*stream_cb_t)(void *data, stream_t *stream);
55
56 /**
57 * Abstraction of a Berkley socket using stream semantics.
58 */
59 struct stream_t {
60
61 /**
62 * Read data from the stream.
63 *
64 * If "block" is FALSE and no data is available, the function returns -1
65 * and sets errno to EWOULDBLOCK.
66 *
67 * @param buf data buffer to read into
68 * @param len number of bytes to read
69 * @param block TRUE to use a blocking read
70 * @return number of bytes read, -1 on error
71 */
72 ssize_t (*read)(stream_t *this, void *buf, size_t len, bool block);
73
74 /**
75 * Register a callback to invoke when stream has data to read.
76 *
77 * @param cb callback function, NULL to unregister
78 * @param data data to pass to callback
79 */
80 void (*on_read)(stream_t *this, stream_cb_t cb, void *data);
81
82 /**
83 * Write data to the stream.
84 *
85 * If "block" is FALSE and the write would block, the function returns -1
86 * and sets errno to EWOULDBLOCK.
87 *
88 * @param buf data buffer to write
89 * @param len number of bytes to write
90 * @param block TRUE to use a blocking write
91 * @return number of bytes written, -1 on error
92 */
93 ssize_t (*write)(stream_t *this, void *buf, size_t len, bool block);
94
95 /**
96 * Register a callback to invoke when a write would not block.
97 *
98 * @param cb callback function, NULL to unregister
99 * @param data data to pass to callback
100 */
101 void (*on_write)(stream_t *this, stream_cb_t cb, void *data);
102
103 /**
104 * Get a FILE reference for this stream.
105 *
106 * @return FILE*, must be fclose()d, NULL on error
107 */
108 FILE* (*get_file)(stream_t *this);
109
110 /**
111 * Destroy a stream_t.
112 */
113 void (*destroy)(stream_t *this);
114 };
115
116 /**
117 * Create a stream for UNIX sockets.
118 *
119 * UNIX URIs start with unix://, followed by the socket path. For absolute
120 * paths, an URI looks something like:
121 *
122 * unix:///path/to/socket
123 *
124 * @param uri UNIX socket specific URI, must start with "unix://"
125 * @return stream instance, NULL on failure
126 */
127 stream_t *stream_create_unix(char *uri);
128
129 /**
130 * Helper function to parse a unix:// URI to a sockaddr
131 *
132 * @param uri URI
133 * @param addr sockaddr
134 * @return length of sockaddr, -1 on error
135 */
136 int stream_parse_uri_unix(char *uri, struct sockaddr_un *addr);
137
138 /**
139 * Create a stream for TCP sockets.
140 *
141 * TCP URIs start with tcp://, followed by a hostname (FQDN or IP), followed
142 * by a colon separated port. A full TCP uri looks something like:
143 *
144 * tcp://srv.example.com:5555
145 * tcp://0.0.0.0:1234
146 * tcp://[fec2::1]:7654
147 *
148 * There is no default port, so a colon after tcp:// is mandatory.
149 *
150 * @param uri TCP socket specific URI, must start with "tcp://"
151 * @return stream instance, NULL on failure
152 */
153 stream_t *stream_create_tcp(char *uri);
154
155 /**
156 * Helper function to parse a tcp:// URI to a sockaddr
157 *
158 * @param uri URI
159 * @param addr sockaddr, large enough for URI
160 * @return length of sockaddr, -1 on error
161 */
162 int stream_parse_uri_tcp(char *uri, struct sockaddr *addr);
163
164 /**
165 * Create a stream from a file descriptor.
166 *
167 * The file descriptor MUST be a socket for non-blocking operation.
168 *
169 * @param fd file descriptor to wrap into a stream_t
170 * @return stream instance
171 */
172 stream_t *stream_create_from_fd(int fd);
173
174 #endif /** STREAM_H_ @}*/