summaryrefslogtreecommitdiffstats
path: root/ctdb/common/pkt_write.c
blob: b1c17305c6d564ee57a3d1986980aec27aed6f74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
   Write a packet

   Copyright (C) Amitay Isaacs 2015

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/network.h"

#include <talloc.h>
#include <tevent.h>

#include "lib/util/tevent_unix.h"

#include "pkt_write.h"

/*
 * Write a packet
 */

struct pkt_write_state {
	int fd;
	uint8_t *buf;
	size_t buflen, offset;
};

struct tevent_req *pkt_write_send(TALLOC_CTX *mem_ctx,
				  struct tevent_context *ev,
				  int fd, uint8_t *buf, size_t buflen)
{
	struct tevent_req *req;
	struct pkt_write_state *state;

	req = tevent_req_create(mem_ctx, &state, struct pkt_write_state);
	if (req == NULL) {
		return NULL;
	}

	state->fd = fd;
	state->buf = buf;
	state->buflen = buflen;
	state->offset = 0;

	return req;
}

void pkt_write_handler(struct tevent_context *ev, struct tevent_fd *fde,
		       uint16_t flags, struct tevent_req *req)
{
	struct pkt_write_state *state = tevent_req_data(
		req, struct pkt_write_state);
	ssize_t nwritten;

	nwritten = write(state->fd, state->buf + state->offset,
			 state->buflen - state->offset);
	if ((nwritten == -1) && (errno == EINTR)) {
		/* retry */
		return;
	}
	if (nwritten == -1) {
		tevent_req_error(req, errno);
		return;
	}
	if (nwritten == 0) {
		/* retry */
		return;
	}

	state->offset += nwritten;
	if (state->offset < state->buflen) {
		/* come back later */
		return;
	}

	tevent_req_done(req);
}

ssize_t pkt_write_recv(struct tevent_req *req, int *perrno)
{
	struct pkt_write_state *state = tevent_req_data(
		req, struct pkt_write_state);

	if (tevent_req_is_unix_error(req, perrno)) {
		return -1;
	}

	return state->offset;
}