s4: make libintl a build dependency of heimdal_krb5 so all dependent binaries are...
[tprouty/samba.git] / lib / async_req / async_sock.c
1 /*
2    Unix SMB/CIFS implementation.
3    async socket syscalls
4    Copyright (C) Volker Lendecke 2008
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/talloc/talloc.h"
22 #include "lib/tevent/tevent.h"
23 #include "lib/async_req/async_req.h"
24 #include "lib/async_req/async_sock.h"
25 #include "lib/util/tevent_unix.h"
26 #include <fcntl.h>
27
28 #ifndef TALLOC_FREE
29 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
30 #endif
31
32 /**
33  * @brief Map async_req states to unix-style errnos
34  * @param[in]  req      The async req to get the state from
35  * @param[out] err      Pointer to take the unix-style errno
36  *
37  * @return true if the async_req is in an error state, false otherwise
38  */
39
40 bool async_req_is_errno(struct async_req *req, int *err)
41 {
42         enum async_req_state state;
43         uint64_t error;
44
45         if (!async_req_is_error(req, &state, &error)) {
46                 return false;
47         }
48
49         switch (state) {
50         case ASYNC_REQ_USER_ERROR:
51                 *err = (int)error;
52                 break;
53         case ASYNC_REQ_TIMED_OUT:
54 #ifdef ETIMEDOUT
55                 *err = ETIMEDOUT;
56 #else
57                 *err = EAGAIN;
58 #endif
59                 break;
60         case ASYNC_REQ_NO_MEMORY:
61                 *err = ENOMEM;
62                 break;
63         default:
64                 *err = EIO;
65                 break;
66         }
67         return true;
68 }
69
70 int async_req_simple_recv_errno(struct async_req *req)
71 {
72         int err;
73
74         if (async_req_is_errno(req, &err)) {
75                 return err;
76         }
77
78         return 0;
79 }
80
81 struct async_send_state {
82         int fd;
83         const void *buf;
84         size_t len;
85         int flags;
86         ssize_t sent;
87 };
88
89 static void async_send_handler(struct tevent_context *ev,
90                                struct tevent_fd *fde,
91                                uint16_t flags, void *private_data);
92
93 struct tevent_req *async_send_send(TALLOC_CTX *mem_ctx,
94                                    struct tevent_context *ev,
95                                    int fd, const void *buf, size_t len,
96                                    int flags)
97 {
98         struct tevent_req *result;
99         struct async_send_state *state;
100         struct tevent_fd *fde;
101
102         result = tevent_req_create(mem_ctx, &state, struct async_send_state);
103         if (result == NULL) {
104                 return result;
105         }
106         state->fd = fd;
107         state->buf = buf;
108         state->len = len;
109         state->flags = flags;
110
111         fde = tevent_add_fd(ev, state, fd, TEVENT_FD_WRITE, async_send_handler,
112                             result);
113         if (fde == NULL) {
114                 TALLOC_FREE(result);
115                 return NULL;
116         }
117         return result;
118 }
119
120 static void async_send_handler(struct tevent_context *ev,
121                                struct tevent_fd *fde,
122                                uint16_t flags, void *private_data)
123 {
124         struct tevent_req *req = talloc_get_type_abort(
125                 private_data, struct tevent_req);
126         struct async_send_state *state = talloc_get_type_abort(
127                 req->private_state, struct async_send_state);
128
129         state->sent = send(state->fd, state->buf, state->len, state->flags);
130         if (state->sent == -1) {
131                 tevent_req_error(req, errno);
132                 return;
133         }
134         tevent_req_done(req);
135 }
136
137 ssize_t async_send_recv(struct tevent_req *req, int *perrno)
138 {
139         struct async_send_state *state = talloc_get_type_abort(
140                 req->private_state, struct async_send_state);
141
142         if (tevent_req_is_unix_error(req, perrno)) {
143                 return -1;
144         }
145         return state->sent;
146 }
147
148 struct async_recv_state {
149         int fd;
150         void *buf;
151         size_t len;
152         int flags;
153         ssize_t received;
154 };
155
156 static void async_recv_handler(struct tevent_context *ev,
157                                struct tevent_fd *fde,
158                                uint16_t flags, void *private_data);
159
160 struct tevent_req *async_recv_send(TALLOC_CTX *mem_ctx,
161                                    struct tevent_context *ev,
162                                    int fd, void *buf, size_t len, int flags)
163 {
164         struct tevent_req *result;
165         struct async_recv_state *state;
166         struct tevent_fd *fde;
167
168         result = tevent_req_create(mem_ctx, &state, struct async_recv_state);
169         if (result == NULL) {
170                 return result;
171         }
172         state->fd = fd;
173         state->buf = buf;
174         state->len = len;
175         state->flags = flags;
176
177         fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ, async_recv_handler,
178                             result);
179         if (fde == NULL) {
180                 TALLOC_FREE(result);
181                 return NULL;
182         }
183         return result;
184 }
185
186 static void async_recv_handler(struct tevent_context *ev,
187                                struct tevent_fd *fde,
188                                uint16_t flags, void *private_data)
189 {
190         struct tevent_req *req = talloc_get_type_abort(
191                 private_data, struct tevent_req);
192         struct async_recv_state *state = talloc_get_type_abort(
193                 req->private_state, struct async_recv_state);
194
195         state->received = recv(state->fd, state->buf, state->len,
196                                state->flags);
197         if (state->received == -1) {
198                 tevent_req_error(req, errno);
199                 return;
200         }
201         tevent_req_done(req);
202 }
203
204 ssize_t async_recv_recv(struct tevent_req *req, int *perrno)
205 {
206         struct async_recv_state *state = talloc_get_type_abort(
207                 req->private_state, struct async_recv_state);
208
209         if (tevent_req_is_unix_error(req, perrno)) {
210                 return -1;
211         }
212         return state->received;
213 }
214
215 struct async_connect_state {
216         int fd;
217         int result;
218         int sys_errno;
219         long old_sockflags;
220 };
221
222 static void async_connect_connected(struct tevent_context *ev,
223                                     struct tevent_fd *fde, uint16_t flags,
224                                     void *priv);
225
226 /**
227  * @brief async version of connect(2)
228  * @param[in] mem_ctx   The memory context to hang the result off
229  * @param[in] ev        The event context to work from
230  * @param[in] fd        The socket to recv from
231  * @param[in] address   Where to connect?
232  * @param[in] address_len Length of *address
233  * @retval The async request
234  *
235  * This function sets the socket into non-blocking state to be able to call
236  * connect in an async state. This will be reset when the request is finished.
237  */
238
239 struct tevent_req *async_connect_send(TALLOC_CTX *mem_ctx,
240                                       struct tevent_context *ev,
241                                       int fd, const struct sockaddr *address,
242                                       socklen_t address_len)
243 {
244         struct tevent_req *result;
245         struct async_connect_state *state;
246         struct tevent_fd *fde;
247
248         result = tevent_req_create(
249                 mem_ctx, &state, struct async_connect_state);
250         if (result == NULL) {
251                 return NULL;
252         }
253
254         /**
255          * We have to set the socket to nonblocking for async connect(2). Keep
256          * the old sockflags around.
257          */
258
259         state->fd = fd;
260         state->sys_errno = 0;
261
262         state->old_sockflags = fcntl(fd, F_GETFL, 0);
263         if (state->old_sockflags == -1) {
264                 goto post_errno;
265         }
266
267         set_blocking(fd, false);
268
269         state->result = connect(fd, address, address_len);
270         if (state->result == 0) {
271                 errno = 0;
272                 goto post_errno;
273         }
274
275         /**
276          * A number of error messages show that something good is progressing
277          * and that we have to wait for readability.
278          *
279          * If none of them are present, bail out.
280          */
281
282         if (!(errno == EINPROGRESS || errno == EALREADY ||
283 #ifdef EISCONN
284               errno == EISCONN ||
285 #endif
286               errno == EAGAIN || errno == EINTR)) {
287                 goto post_errno;
288         }
289
290         fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ | TEVENT_FD_WRITE,
291                            async_connect_connected, result);
292         if (fde == NULL) {
293                 errno = ENOMEM;
294                 goto post_errno;
295         }
296         return result;
297
298  post_errno:
299         state->sys_errno = errno;
300         fcntl(fd, F_SETFL, state->old_sockflags);
301         if (state->sys_errno == 0) {
302                 tevent_req_done(result);
303         } else {
304                 tevent_req_error(result, state->sys_errno);
305         }
306         return tevent_req_post(result, ev);
307 }
308
309 /**
310  * fde event handler for connect(2)
311  * @param[in] ev        The event context that sent us here
312  * @param[in] fde       The file descriptor event associated with the connect
313  * @param[in] flags     Indicate read/writeability of the socket
314  * @param[in] priv      private data, "struct async_req *" in this case
315  */
316
317 static void async_connect_connected(struct tevent_context *ev,
318                                     struct tevent_fd *fde, uint16_t flags,
319                                     void *priv)
320 {
321         struct tevent_req *req = talloc_get_type_abort(
322                 priv, struct tevent_req);
323         struct async_connect_state *state = talloc_get_type_abort(
324                 req->private_state, struct async_connect_state);
325
326         TALLOC_FREE(fde);
327
328         /*
329          * Stevens, Network Programming says that if there's a
330          * successful connect, the socket is only writable. Upon an
331          * error, it's both readable and writable.
332          */
333         if ((flags & (TEVENT_FD_READ|TEVENT_FD_WRITE))
334             == (TEVENT_FD_READ|TEVENT_FD_WRITE)) {
335                 int sockerr;
336                 socklen_t err_len = sizeof(sockerr);
337
338                 if (getsockopt(state->fd, SOL_SOCKET, SO_ERROR,
339                                (void *)&sockerr, &err_len) == 0) {
340                         errno = sockerr;
341                 }
342
343                 state->sys_errno = errno;
344
345                 DEBUG(10, ("connect returned %s\n", strerror(errno)));
346
347                 fcntl(state->fd, F_SETFL, state->old_sockflags);
348                 tevent_req_error(req, state->sys_errno);
349                 return;
350         }
351
352         state->sys_errno = 0;
353         tevent_req_done(req);
354 }
355
356 int async_connect_recv(struct tevent_req *req, int *perrno)
357 {
358         struct async_connect_state *state = talloc_get_type_abort(
359                 req->private_state, struct async_connect_state);
360         int err;
361
362         fcntl(state->fd, F_SETFL, state->old_sockflags);
363
364         if (tevent_req_is_unix_error(req, &err)) {
365                 *perrno = err;
366                 return -1;
367         }
368
369         if (state->sys_errno == 0) {
370                 return 0;
371         }
372
373         *perrno = state->sys_errno;
374         return -1;
375 }
376
377 struct writev_state {
378         struct tevent_context *ev;
379         int fd;
380         struct iovec *iov;
381         int count;
382         size_t total_size;
383 };
384
385 static void writev_handler(struct tevent_context *ev, struct tevent_fd *fde,
386                            uint16_t flags, void *private_data);
387
388 struct tevent_req *writev_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
389                                int fd, struct iovec *iov, int count)
390 {
391         struct tevent_req *result;
392         struct writev_state *state;
393         struct tevent_fd *fde;
394
395         result = tevent_req_create(mem_ctx, &state, struct writev_state);
396         if (result == NULL) {
397                 return NULL;
398         }
399         state->ev = ev;
400         state->fd = fd;
401         state->total_size = 0;
402         state->count = count;
403         state->iov = (struct iovec *)talloc_memdup(
404                 state, iov, sizeof(struct iovec) * count);
405         if (state->iov == NULL) {
406                 goto fail;
407         }
408
409         fde = tevent_add_fd(ev, state, fd, TEVENT_FD_WRITE, writev_handler,
410                             result);
411         if (fde == NULL) {
412                 goto fail;
413         }
414         return result;
415
416  fail:
417         TALLOC_FREE(result);
418         return NULL;
419 }
420
421 static void writev_handler(struct tevent_context *ev, struct tevent_fd *fde,
422                            uint16_t flags, void *private_data)
423 {
424         struct tevent_req *req = talloc_get_type_abort(
425                 private_data, struct tevent_req);
426         struct writev_state *state = talloc_get_type_abort(
427                 req->private_state, struct writev_state);
428         size_t to_write, written;
429         int i;
430
431         to_write = 0;
432
433         for (i=0; i<state->count; i++) {
434                 to_write += state->iov[i].iov_len;
435         }
436
437         written = sys_writev(state->fd, state->iov, state->count);
438         if (written == -1) {
439                 tevent_req_error(req, errno);
440                 return;
441         }
442         if (written == 0) {
443                 tevent_req_error(req, EPIPE);
444                 return;
445         }
446         state->total_size += written;
447
448         if (written == to_write) {
449                 tevent_req_done(req);
450                 return;
451         }
452
453         /*
454          * We've written less than we were asked to, drop stuff from
455          * state->iov.
456          */
457
458         while (written > 0) {
459                 if (written < state->iov[0].iov_len) {
460                         state->iov[0].iov_base =
461                                 (char *)state->iov[0].iov_base + written;
462                         state->iov[0].iov_len -= written;
463                         break;
464                 }
465                 written = state->iov[0].iov_len;
466                 state->iov += 1;
467                 state->count -= 1;
468         }
469 }
470
471 ssize_t writev_recv(struct tevent_req *req, int *perrno)
472 {
473         struct writev_state *state = talloc_get_type_abort(
474                 req->private_state, struct writev_state);
475
476         if (tevent_req_is_unix_error(req, perrno)) {
477                 return -1;
478         }
479         return state->total_size;
480 }
481
482 struct read_packet_state {
483         int fd;
484         uint8_t *buf;
485         size_t nread;
486         ssize_t (*more)(uint8_t *buf, size_t buflen, void *private_data);
487         void *private_data;
488 };
489
490 static void read_packet_handler(struct tevent_context *ev,
491                                 struct tevent_fd *fde,
492                                 uint16_t flags, void *private_data);
493
494 struct tevent_req *read_packet_send(TALLOC_CTX *mem_ctx,
495                                     struct tevent_context *ev,
496                                     int fd, size_t initial,
497                                     ssize_t (*more)(uint8_t *buf,
498                                                     size_t buflen,
499                                                     void *private_data),
500                                     void *private_data)
501 {
502         struct tevent_req *result;
503         struct read_packet_state *state;
504         struct tevent_fd *fde;
505
506         result = tevent_req_create(mem_ctx, &state, struct read_packet_state);
507         if (result == NULL) {
508                 return NULL;
509         }
510         state->fd = fd;
511         state->nread = 0;
512         state->more = more;
513         state->private_data = private_data;
514
515         state->buf = talloc_array(state, uint8_t, initial);
516         if (state->buf == NULL) {
517                 goto fail;
518         }
519
520         fde = tevent_add_fd(ev, state, fd, TEVENT_FD_READ, read_packet_handler,
521                             result);
522         if (fde == NULL) {
523                 goto fail;
524         }
525         return result;
526  fail:
527         TALLOC_FREE(result);
528         return NULL;
529 }
530
531 static void read_packet_handler(struct tevent_context *ev,
532                                 struct tevent_fd *fde,
533                                 uint16_t flags, void *private_data)
534 {
535         struct tevent_req *req = talloc_get_type_abort(
536                 private_data, struct tevent_req);
537         struct read_packet_state *state = talloc_get_type_abort(
538                 req->private_state, struct read_packet_state);
539         size_t total = talloc_get_size(state->buf);
540         ssize_t nread, more;
541         uint8_t *tmp;
542
543         nread = read(state->fd, state->buf+state->nread, total-state->nread);
544         if (nread == -1) {
545                 tevent_req_error(req, errno);
546                 return;
547         }
548         if (nread == 0) {
549                 tevent_req_error(req, EPIPE);
550                 return;
551         }
552
553         state->nread += nread;
554         if (state->nread < total) {
555                 /* Come back later */
556                 return;
557         }
558
559         /*
560          * We got what was initially requested. See if "more" asks for -- more.
561          */
562         if (state->more == NULL) {
563                 /* Nobody to ask, this is a async read_data */
564                 tevent_req_done(req);
565                 return;
566         }
567
568         more = state->more(state->buf, total, state->private_data);
569         if (more == -1) {
570                 /* We got an invalid packet, tell the caller */
571                 tevent_req_error(req, EIO);
572                 return;
573         }
574         if (more == 0) {
575                 /* We're done, full packet received */
576                 tevent_req_done(req);
577                 return;
578         }
579
580         tmp = TALLOC_REALLOC_ARRAY(state, state->buf, uint8_t, total+more);
581         if (tevent_req_nomem(tmp, req)) {
582                 return;
583         }
584         state->buf = tmp;
585 }
586
587 ssize_t read_packet_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
588                          uint8_t **pbuf, int *perrno)
589 {
590         struct read_packet_state *state = talloc_get_type_abort(
591                 req->private_state, struct read_packet_state);
592
593         if (tevent_req_is_unix_error(req, perrno)) {
594                 return -1;
595         }
596         *pbuf = talloc_move(mem_ctx, &state->buf);
597         return talloc_get_size(*pbuf);
598 }