wscript: separate embedded_heimdal from system_heimdal
[Samba.git] / ctdb / common / pkt_write.c
blobb1c17305c6d564ee57a3d1986980aec27aed6f74
1 /*
2 Write a packet
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/>.
20 #include "replace.h"
21 #include "system/network.h"
23 #include <talloc.h>
24 #include <tevent.h>
26 #include "lib/util/tevent_unix.h"
28 #include "pkt_write.h"
31 * Write a packet
34 struct pkt_write_state {
35 int fd;
36 uint8_t *buf;
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);
48 if (req == NULL) {
49 return NULL;
52 state->fd = fd;
53 state->buf = buf;
54 state->buflen = buflen;
55 state->offset = 0;
57 return req;
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);
65 ssize_t nwritten;
67 nwritten = write(state->fd, state->buf + state->offset,
68 state->buflen - state->offset);
69 if ((nwritten == -1) && (errno == EINTR)) {
70 /* retry */
71 return;
73 if (nwritten == -1) {
74 tevent_req_error(req, errno);
75 return;
77 if (nwritten == 0) {
78 /* retry */
79 return;
82 state->offset += nwritten;
83 if (state->offset < state->buflen) {
84 /* come back later */
85 return;
88 tevent_req_done(req);
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)) {
97 return -1;
100 return state->offset;