4 Copyright (C) Amitay Isaacs 2015
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.
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.
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/>.
21 #include "system/network.h"
26 #include "lib/util/tevent_unix.h"
28 #include "pkt_write.h"
34 struct pkt_write_state {
37 size_t buflen, offset;
40 struct tevent_req *pkt_write_send(TALLOC_CTX *mem_ctx,
41 struct tevent_context *ev,
42 int fd, uint8_t *buf, size_t buflen)
44 struct tevent_req *req;
45 struct pkt_write_state *state;
47 req = tevent_req_create(mem_ctx, &state, struct pkt_write_state);
54 state->buflen = buflen;
60 void pkt_write_handler(struct tevent_context *ev, struct tevent_fd *fde,
61 uint16_t flags, struct tevent_req *req)
63 struct pkt_write_state *state = tevent_req_data(
64 req, struct pkt_write_state);
67 nwritten = write(state->fd, state->buf + state->offset,
68 state->buflen - state->offset);
69 if ((nwritten == -1) && (errno == EINTR)) {
74 tevent_req_error(req, errno);
82 state->offset += nwritten;
83 if (state->offset < state->buflen) {
91 ssize_t pkt_write_recv(struct tevent_req *req, int *perrno)
93 struct pkt_write_state *state = tevent_req_data(
94 req, struct pkt_write_state);
96 if (tevent_req_is_unix_error(req, perrno)) {
100 return state->offset;