stream: add support for TCP streams
[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 * printf() convenience function for this stream.
105 *
106 * @param format printf format string
107 * @param ... argument list for format string
108 * @return number of characters written, negative on error
109 */
110 int (*print)(stream_t *this, char *format, ...);
111
112 /**
113 * vprintf() convenience function for this stream.
114 *
115 * @param format printf format string
116 * @param ap argument list for format string
117 * @return number of characters written, negative on error
118 */
119 int (*vprint)(stream_t *this, char *format, va_list ap);
120
121 /**
122 * Destroy a stream_t.
123 */
124 void (*destroy)(stream_t *this);
125 };
126
127 /**
128 * Create a stream for UNIX sockets.
129 *
130 * UNIX URIs start with unix://, followed by the socket path. For absolute
131 * paths, an URI looks something like:
132 *
133 * unix:///path/to/socket
134 *
135 * @param uri UNIX socket specific URI, must start with "unix://"
136 * @return stream instance, NULL on failure
137 */
138 stream_t *stream_create_unix(char *uri);
139
140 /**
141 * Helper function to parse a unix:// URI to a sockaddr
142 *
143 * @param uri URI
144 * @param addr sockaddr
145 * @return length of sockaddr, -1 on error
146 */
147 int stream_parse_uri_unix(char *uri, struct sockaddr_un *addr);
148
149 /**
150 * Create a stream for TCP sockets.
151 *
152 * TCP URIs start with tcp://, followed by a hostname (FQDN or IP), followed
153 * by a colon separated port. A full TCP uri looks something like:
154 *
155 * tcp://srv.example.com:5555
156 * tcp://0.0.0.0:1234
157 * tcp://[fec2::1]:7654
158 *
159 * There is no default port, so a colon after tcp:// is mandatory.
160 *
161 * @param uri TCP socket specific URI, must start with "tcp://"
162 * @return stream instance, NULL on failure
163 */
164 stream_t *stream_create_tcp(char *uri);
165
166 /**
167 * Helper function to parse a tcp:// URI to a sockaddr
168 *
169 * @param uri URI
170 * @param addr sockaddr, large enough for URI
171 * @return length of sockaddr, -1 on error
172 */
173 int stream_parse_uri_tcp(char *uri, struct sockaddr *addr);
174
175 /**
176 * Create a stream from a file descriptor.
177 *
178 * The file descriptor MUST be a socket for non-blocking operation.
179 *
180 * @param fd file descriptor to wrap into a stream_t
181 * @return stream instance
182 */
183 stream_t *stream_create_from_fd(int fd);
184
185 #endif /** STREAM_H_ @}*/