810514da9642f8bb6e1f63bc56af2788e7e51a09
[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 allowed to destroy the stream during the callback, but only if it has
43 * no other active on_read()/on_write() callback and returns FALSE. It is not
44 * allowed to to call on_read()/on_write/() during the 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 unregisters the invoked callback, TRUE keeps it
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 * Read data from the stream, avoiding short reads.
76 *
77 * This call is always blocking, and reads until len has been read
78 * completely. If the connection is closed before enough bytes could be
79 * returned, errno is set to ECONNRESET.
80 *
81 * @param buf data buffer to read into
82 * @param len number of bytes to read
83 * @return TRUE if len bytes read, FALSE on error
84 */
85 bool (*read_all)(stream_t *this, void *buf, size_t len);
86
87 /**
88 * Register a callback to invoke when stream has data to read.
89 *
90 * @param cb callback function, NULL to unregister
91 * @param data data to pass to callback
92 */
93 void (*on_read)(stream_t *this, stream_cb_t cb, void *data);
94
95 /**
96 * Write data to the stream.
97 *
98 * If "block" is FALSE and the write would block, the function returns -1
99 * and sets errno to EWOULDBLOCK.
100 *
101 * @param buf data buffer to write
102 * @param len number of bytes to write
103 * @param block TRUE to use a blocking write
104 * @return number of bytes written, -1 on error
105 */
106 ssize_t (*write)(stream_t *this, void *buf, size_t len, bool block);
107
108 /**
109 * Write data to the stream, avoiding short writes.
110 *
111 * This call is always blocking, and writes until len bytes has been
112 * written.
113 *
114 * @param buf data buffer to write
115 * @param len number of bytes to write
116 * @return TRUE if len bytes written, FALSE on error
117 */
118 bool (*write_all)(stream_t *this, void *buf, size_t len);
119
120 /**
121 * Register a callback to invoke when a write would not block.
122 *
123 * @param cb callback function, NULL to unregister
124 * @param data data to pass to callback
125 */
126 void (*on_write)(stream_t *this, stream_cb_t cb, void *data);
127
128 /**
129 * Get a FILE reference for this stream.
130 *
131 * @return FILE*, must be fclose()d, NULL on error
132 */
133 FILE* (*get_file)(stream_t *this);
134
135 /**
136 * Destroy a stream_t.
137 */
138 void (*destroy)(stream_t *this);
139 };
140
141 /**
142 * Create a stream for UNIX sockets.
143 *
144 * UNIX URIs start with unix://, followed by the socket path. For absolute
145 * paths, an URI looks something like:
146 *
147 * unix:///path/to/socket
148 *
149 * @param uri UNIX socket specific URI, must start with "unix://"
150 * @return stream instance, NULL on failure
151 */
152 stream_t *stream_create_unix(char *uri);
153
154 /**
155 * Helper function to parse a unix:// URI to a sockaddr
156 *
157 * @param uri URI
158 * @param addr sockaddr
159 * @return length of sockaddr, -1 on error
160 */
161 int stream_parse_uri_unix(char *uri, struct sockaddr_un *addr);
162
163 /**
164 * Create a stream for TCP sockets.
165 *
166 * TCP URIs start with tcp://, followed by a hostname (FQDN or IP), followed
167 * by a colon separated port. A full TCP uri looks something like:
168 *
169 * tcp://srv.example.com:5555
170 * tcp://0.0.0.0:1234
171 * tcp://[fec2::1]:7654
172 *
173 * There is no default port, so a colon after tcp:// is mandatory.
174 *
175 * @param uri TCP socket specific URI, must start with "tcp://"
176 * @return stream instance, NULL on failure
177 */
178 stream_t *stream_create_tcp(char *uri);
179
180 /**
181 * Helper function to parse a tcp:// URI to a sockaddr
182 *
183 * @param uri URI
184 * @param addr sockaddr, large enough for URI
185 * @return length of sockaddr, -1 on error
186 */
187 int stream_parse_uri_tcp(char *uri, struct sockaddr *addr);
188
189 /**
190 * Create a stream from a file descriptor.
191 *
192 * The file descriptor MUST be a socket for non-blocking operation.
193 *
194 * @param fd file descriptor to wrap into a stream_t
195 * @return stream instance
196 */
197 stream_t *stream_create_from_fd(int fd);
198
199 #endif /** STREAM_H_ @}*/