1 | /*
|
---|
2 | Write a packet
|
---|
3 |
|
---|
4 | Copyright (C) Amitay Isaacs 2015
|
---|
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 "replace.h"
|
---|
21 | #include "system/network.h"
|
---|
22 |
|
---|
23 | #include <talloc.h>
|
---|
24 | #include <tevent.h>
|
---|
25 |
|
---|
26 | #include "lib/util/tevent_unix.h"
|
---|
27 |
|
---|
28 | #include "pkt_write.h"
|
---|
29 |
|
---|
30 | /*
|
---|
31 | * Write a packet
|
---|
32 | */
|
---|
33 |
|
---|
34 | struct pkt_write_state {
|
---|
35 | int fd;
|
---|
36 | uint8_t *buf;
|
---|
37 | size_t buflen, offset;
|
---|
38 | };
|
---|
39 |
|
---|
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)
|
---|
43 | {
|
---|
44 | struct tevent_req *req;
|
---|
45 | struct pkt_write_state *state;
|
---|
46 |
|
---|
47 | req = tevent_req_create(mem_ctx, &state, struct pkt_write_state);
|
---|
48 | if (req == NULL) {
|
---|
49 | return NULL;
|
---|
50 | }
|
---|
51 |
|
---|
52 | state->fd = fd;
|
---|
53 | state->buf = buf;
|
---|
54 | state->buflen = buflen;
|
---|
55 | state->offset = 0;
|
---|
56 |
|
---|
57 | return req;
|
---|
58 | }
|
---|
59 |
|
---|
60 | void pkt_write_handler(struct tevent_context *ev, struct tevent_fd *fde,
|
---|
61 | uint16_t flags, struct tevent_req *req)
|
---|
62 | {
|
---|
63 | struct pkt_write_state *state = tevent_req_data(
|
---|
64 | req, struct pkt_write_state);
|
---|
65 | ssize_t nwritten;
|
---|
66 |
|
---|
67 | nwritten = write(state->fd, state->buf + state->offset,
|
---|
68 | state->buflen - state->offset);
|
---|
69 | if ((nwritten == -1) && (errno == EINTR)) {
|
---|
70 | /* retry */
|
---|
71 | return;
|
---|
72 | }
|
---|
73 | if (nwritten == -1) {
|
---|
74 | tevent_req_error(req, errno);
|
---|
75 | return;
|
---|
76 | }
|
---|
77 | if (nwritten == 0) {
|
---|
78 | /* retry */
|
---|
79 | return;
|
---|
80 | }
|
---|
81 |
|
---|
82 | state->offset += nwritten;
|
---|
83 | if (state->offset < state->buflen) {
|
---|
84 | /* come back later */
|
---|
85 | return;
|
---|
86 | }
|
---|
87 |
|
---|
88 | tevent_req_done(req);
|
---|
89 | }
|
---|
90 |
|
---|
91 | ssize_t pkt_write_recv(struct tevent_req *req, int *perrno)
|
---|
92 | {
|
---|
93 | struct pkt_write_state *state = tevent_req_data(
|
---|
94 | req, struct pkt_write_state);
|
---|
95 |
|
---|
96 | if (tevent_req_is_unix_error(req, perrno)) {
|
---|
97 | return -1;
|
---|
98 | }
|
---|
99 |
|
---|
100 | return state->offset;
|
---|
101 | }
|
---|