stream: add read/write_all() methods to stream
[strongswan.git] / src / libstrongswan / networking / streams / stream.c
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 #include <library.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include <limits.h>
20
21 typedef struct private_stream_t private_stream_t;
22
23 /**
24 * Private data of an stream_t object.
25 */
26 struct private_stream_t {
27
28 /**
29 * Public stream_t interface.
30 */
31 stream_t public;
32
33 /**
34 * Underlying socket
35 */
36 int fd;
37
38 /**
39 * Callback if data is ready to read
40 */
41 stream_cb_t read_cb;
42
43 /**
44 * Data for read-ready callback
45 */
46 void *read_data;
47
48 /**
49 * Callback if write is non-blocking
50 */
51 stream_cb_t write_cb;
52
53 /**
54 * Data for write-ready callback
55 */
56 void *write_data;
57 };
58
59 METHOD(stream_t, read_, ssize_t,
60 private_stream_t *this, void *buf, size_t len, bool block)
61 {
62 while (TRUE)
63 {
64 ssize_t ret;
65
66 if (block)
67 {
68 ret = read(this->fd, buf, len);
69 }
70 else
71 {
72 ret = recv(this->fd, buf, len, MSG_DONTWAIT);
73 if (ret == -1 && errno == EAGAIN)
74 {
75 /* unify EGAIN and EWOULDBLOCK */
76 errno = EWOULDBLOCK;
77 }
78 }
79 if (ret == -1 && errno == EINTR)
80 { /* interrupted, try again */
81 continue;
82 }
83 return ret;
84 }
85 }
86
87 METHOD(stream_t, read_all, bool,
88 private_stream_t *this, void *buf, size_t len)
89 {
90 ssize_t ret;
91
92 while (len)
93 {
94 ret = read_(this, buf, len, TRUE);
95 if (ret < 0)
96 {
97 return FALSE;
98 }
99 if (ret == 0)
100 {
101 errno = ECONNRESET;
102 return FALSE;
103 }
104 len -= ret;
105 buf += ret;
106 }
107 return TRUE;
108 }
109
110 METHOD(stream_t, write_, ssize_t,
111 private_stream_t *this, void *buf, size_t len, bool block)
112 {
113 ssize_t ret;
114
115 while (TRUE)
116 {
117 if (block)
118 {
119 ret = write(this->fd, buf, len);
120 }
121 else
122 {
123 ret = send(this->fd, buf, len, MSG_DONTWAIT);
124 if (ret == -1 && errno == EAGAIN)
125 {
126 /* unify EGAIN and EWOULDBLOCK */
127 errno = EWOULDBLOCK;
128 }
129 }
130 if (ret == -1 && errno == EINTR)
131 { /* interrupted, try again */
132 continue;
133 }
134 return ret;
135 }
136 }
137
138 METHOD(stream_t, write_all, bool,
139 private_stream_t *this, void *buf, size_t len)
140 {
141 ssize_t ret;
142
143 while (len)
144 {
145 ret = write_(this, buf, len, TRUE);
146 if (ret < 0)
147 {
148 return FALSE;
149 }
150 if (ret == 0)
151 {
152 errno = ECONNRESET;
153 return FALSE;
154 }
155 len -= ret;
156 buf += ret;
157 }
158 return TRUE;
159 }
160
161 /**
162 * Remove a registered watcher
163 */
164 static void remove_watcher(private_stream_t *this)
165 {
166 if (this->read_cb || this->write_cb)
167 {
168 lib->watcher->remove(lib->watcher, this->fd);
169 }
170 }
171
172 /**
173 * Watcher callback
174 */
175 static bool watch(private_stream_t *this, int fd, watcher_event_t event)
176 {
177 bool keep = FALSE;
178
179 switch (event)
180 {
181 case WATCHER_READ:
182 keep = this->read_cb(this->read_data, &this->public);
183 if (!keep)
184 {
185 this->read_cb = NULL;
186 }
187 break;
188 case WATCHER_WRITE:
189 keep = this->write_cb(this->write_data, &this->public);
190 if (!keep)
191 {
192 this->write_cb = NULL;
193 }
194 break;
195 case WATCHER_EXCEPT:
196 break;
197 }
198 return keep;
199 }
200
201 /**
202 * Register watcher for stream callbacks
203 */
204 static void add_watcher(private_stream_t *this)
205 {
206 watcher_event_t events = 0;
207
208 if (this->read_cb)
209 {
210 events |= WATCHER_READ;
211 }
212 if (this->write_cb)
213 {
214 events |= WATCHER_WRITE;
215 }
216 if (events)
217 {
218 lib->watcher->add(lib->watcher, this->fd, events,
219 (watcher_cb_t)watch, this);
220 }
221 }
222
223 METHOD(stream_t, on_read, void,
224 private_stream_t *this, stream_cb_t cb, void *data)
225 {
226 remove_watcher(this);
227
228 this->read_cb = cb;
229 this->read_data = data;
230
231 add_watcher(this);
232 }
233
234 METHOD(stream_t, on_write, void,
235 private_stream_t *this, stream_cb_t cb, void *data)
236 {
237 remove_watcher(this);
238
239 this->write_cb = cb;
240 this->write_data = data;
241
242 add_watcher(this);
243 }
244
245 METHOD(stream_t, get_file, FILE*,
246 private_stream_t *this)
247 {
248 FILE *file;
249 int fd;
250
251 /* fclose() closes the FD passed to fdopen(), so dup() it */
252 fd = dup(this->fd);
253 if (fd == -1)
254 {
255 return NULL;
256 }
257 file = fdopen(fd, "w+");
258 if (!file)
259 {
260 close(fd);
261 }
262 return file;
263 }
264
265 METHOD(stream_t, destroy, void,
266 private_stream_t *this)
267 {
268 remove_watcher(this);
269 close(this->fd);
270 free(this);
271 }
272
273 /**
274 * See header
275 */
276 stream_t *stream_create_from_fd(int fd)
277 {
278 private_stream_t *this;
279
280 INIT(this,
281 .public = {
282 .read = _read_,
283 .read_all = _read_all,
284 .on_read = _on_read,
285 .write = _write_,
286 .write_all = _write_all,
287 .on_write = _on_write,
288 .get_file = _get_file,
289 .destroy = _destroy,
290 },
291 .fd = fd,
292 );
293
294 return &this->public;
295 }
296
297 /**
298 * See header
299 */
300 int stream_parse_uri_unix(char *uri, struct sockaddr_un *addr)
301 {
302 if (!strpfx(uri, "unix://"))
303 {
304 return -1;
305 }
306 uri += strlen("unix://");
307
308 memset(addr, 0, sizeof(*addr));
309 addr->sun_family = AF_UNIX;
310 strncpy(addr->sun_path, uri, sizeof(addr->sun_path));
311
312 return offsetof(struct sockaddr_un, sun_path) + strlen(addr->sun_path);
313 }
314
315 /**
316 * See header
317 */
318 stream_t *stream_create_unix(char *uri)
319 {
320 struct sockaddr_un addr;
321 int len, fd;
322
323 len = stream_parse_uri_unix(uri, &addr);
324 if (len == -1)
325 {
326 DBG1(DBG_NET, "invalid stream URI: '%s'", uri);
327 return NULL;
328 }
329 fd = socket(AF_UNIX, SOCK_STREAM, 0);
330 if (fd < 0)
331 {
332 DBG1(DBG_NET, "opening socket '%s' failed: %s", uri, strerror(errno));
333 return NULL;
334 }
335 if (connect(fd, (struct sockaddr*)&addr, len) < 0)
336 {
337 DBG1(DBG_NET, "connecting to '%s' failed: %s", uri, strerror(errno));
338 close(fd);
339 return NULL;
340 }
341 return stream_create_from_fd(fd);
342 }
343
344 /**
345 * See header.
346 */
347 int stream_parse_uri_tcp(char *uri, struct sockaddr *addr)
348 {
349 char *pos, buf[128];
350 host_t *host;
351 u_long port;
352 int len;
353
354 if (!strpfx(uri, "tcp://"))
355 {
356 return -1;
357 }
358 uri += strlen("tcp://");
359 pos = strrchr(uri, ':');
360 if (!pos)
361 {
362 return -1;
363 }
364 if (*uri == '[' && pos > uri && *(pos - 1) == ']')
365 {
366 /* IPv6 URI */
367 snprintf(buf, sizeof(buf), "%.*s", (int)(pos - uri - 2), uri + 1);
368 }
369 else
370 {
371 snprintf(buf, sizeof(buf), "%.*s", (int)(pos - uri), uri);
372 }
373 port = strtoul(pos + 1, &pos, 10);
374 if (port == ULONG_MAX || *pos || port > 65535)
375 {
376 return -1;
377 }
378 host = host_create_from_dns(buf, AF_UNSPEC, port);
379 if (!host)
380 {
381 return -1;
382 }
383 len = *host->get_sockaddr_len(host);
384 memcpy(addr, host->get_sockaddr(host), len);
385 host->destroy(host);
386 return len;
387 }
388
389 /**
390 * See header
391 */
392 stream_t *stream_create_tcp(char *uri)
393 {
394 union {
395 struct sockaddr_in in;
396 struct sockaddr_in6 in6;
397 struct sockaddr sa;
398 } addr;
399 int fd, len;
400
401 len = stream_parse_uri_tcp(uri, &addr.sa);
402 if (len == -1)
403 {
404 DBG1(DBG_NET, "invalid stream URI: '%s'", uri);
405 return NULL;
406 }
407 fd = socket(addr.sa.sa_family, SOCK_STREAM, 0);
408 if (fd < 0)
409 {
410 DBG1(DBG_NET, "opening socket '%s' failed: %s", uri, strerror(errno));
411 return NULL;
412 }
413 if (connect(fd, &addr.sa, len))
414 {
415 DBG1(DBG_NET, "connecting to '%s' failed: %s", uri, strerror(errno));
416 close(fd);
417 return NULL;
418 }
419 return stream_create_from_fd(fd);
420 }