1 /*
2    packet read tests
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/filesys.h"
22 
23 #include <assert.h>
24 
25 #include "lib/util/blocking.h"
26 
27 #include "common/pkt_read.c"
28 
writer(int fd)29 static void writer(int fd)
30 {
31 	uint8_t buf[1024*1024];
32 	size_t buflen;
33 	size_t pkt_size[4] = { 100, 500, 1024, 1024*1024 };
34 	int i, j;
35 	int ret;
36 
37 	for (i=0; i<1024*1024; i++) {
38 		buf[i] = i%256;
39 	}
40 
41 	for (i=0; i<1000; i++) {
42 		for (j=0; j<4; j++) {
43 			buflen = pkt_size[j];
44 			memcpy(buf, &buflen, sizeof(buflen));
45 
46 			ret = write(fd, buf, buflen);
47 			if (ret < 0) {
48 				printf("write error: %s\n", strerror(errno));
49 				assert(ret > 0);
50 			}
51 		}
52 	}
53 
54 	close(fd);
55 }
56 
57 struct reader_state {
58 	struct tevent_context *ev;
59 	int fd;
60 	uint8_t *buf;
61 	size_t buflen;
62 	struct tevent_req *subreq;
63 };
64 
65 static ssize_t reader_more(uint8_t *buf, size_t buflen, void *private_data);
66 static void reader_done(struct tevent_req *subreq);
67 
reader_send(TALLOC_CTX * mem_ctx,struct tevent_context * ev,int fd,uint8_t * buf,size_t buflen)68 static struct tevent_req *reader_send(TALLOC_CTX *mem_ctx,
69 				      struct tevent_context *ev,
70 				      int fd, uint8_t *buf,
71 				      size_t buflen)
72 {
73 	struct tevent_req *req, *subreq;
74 	struct reader_state *state;
75 
76 	req = tevent_req_create(mem_ctx, &state, struct reader_state);
77 	if (req == NULL) {
78 		return NULL;
79 	}
80 
81 	state->ev = ev;
82 	state->fd = fd;
83 	state->buf = buf;
84 	state->buflen = buflen;
85 
86 	subreq = pkt_read_send(state, state->ev, state->fd, 4,
87 			       state->buf, state->buflen, reader_more, NULL);
88 	if (tevent_req_nomem(subreq, req)) {
89 		tevent_req_post(req, ev);
90 	}
91 
92 	state->subreq = subreq;
93 	tevent_req_set_callback(subreq, reader_done, req);
94 	return req;
95 }
96 
reader_more(uint8_t * buf,size_t buflen,void * private_data)97 static ssize_t reader_more(uint8_t *buf, size_t buflen, void *private_data)
98 {
99 	uint32_t pkt_len;
100 
101 	if (buflen < sizeof(pkt_len)) {
102 		return sizeof(pkt_len) - buflen;
103 	}
104 
105 	pkt_len = *(uint32_t *)buf;
106 	return pkt_len - buflen;
107 }
108 
reader_done(struct tevent_req * subreq)109 static void reader_done(struct tevent_req *subreq)
110 {
111 	struct tevent_req *req = tevent_req_callback_data(
112 		subreq, struct tevent_req);
113 	struct reader_state *state = tevent_req_data(
114 		req, struct reader_state);
115 	ssize_t nread;
116 	uint8_t *buf;
117 	bool free_buf;
118 	int err;
119 
120 	nread = pkt_read_recv(subreq, state, &buf, &free_buf, &err);
121 	TALLOC_FREE(subreq);
122 	state->subreq = NULL;
123 	if (nread == -1) {
124 		if (err == EPIPE) {
125 			tevent_req_done(req);
126 		} else {
127 			tevent_req_error(req, err);
128 		}
129 		return;
130 	}
131 
132 	if (free_buf) {
133 		talloc_free(buf);
134 	}
135 
136 	subreq = pkt_read_send(state, state->ev, state->fd, 4,
137 			       state->buf, state->buflen, reader_more, NULL);
138 	if (tevent_req_nomem(subreq, req)) {
139 		return;
140 	}
141 
142 	state->subreq = subreq;
143 	tevent_req_set_callback(subreq, reader_done, req);
144 }
145 
reader_recv(struct tevent_req * req,int * perr)146 static void reader_recv(struct tevent_req *req, int *perr)
147 {
148 	struct reader_state *state = tevent_req_data(
149 		req, struct reader_state);
150 	int err = 0;
151 
152 	if (state->subreq != NULL) {
153 		*perr = -1;
154 	}
155 
156 	if (tevent_req_is_unix_error(req, &err)) {
157 		*perr = err;
158 		return;
159 	}
160 
161 	*perr = 0;
162 }
163 
reader_handler(struct tevent_context * ev,struct tevent_fd * fde,uint16_t flags,void * private_data)164 static void reader_handler(struct tevent_context *ev, struct tevent_fd *fde,
165 			   uint16_t flags, void *private_data)
166 {
167 	struct tevent_req *req = talloc_get_type_abort(
168 		private_data, struct tevent_req);
169 	struct reader_state *state = tevent_req_data(
170 		req, struct reader_state);
171 
172 	assert(state->subreq != NULL);
173 	pkt_read_handler(ev, fde, flags, state->subreq);
174 }
175 
reader(int fd,bool fixed)176 static void reader(int fd, bool fixed)
177 {
178 	TALLOC_CTX *mem_ctx;
179 	struct tevent_context *ev;
180 	struct tevent_fd *fde;
181 	struct tevent_req *req;
182 	int err;
183 	uint8_t *buf = NULL;
184 	size_t buflen = 0;
185 
186 	mem_ctx = talloc_new(NULL);
187 	assert(mem_ctx != NULL);
188 
189 	ev = tevent_context_init(mem_ctx);
190 	assert(ev != NULL);
191 
192 	if (fixed) {
193 		buflen = 1024;
194 		buf = talloc_size(mem_ctx, buflen);
195 		assert(buf != NULL);
196 	}
197 
198 	req = reader_send(mem_ctx, ev, fd, buf, buflen);
199 	assert(req != NULL);
200 
201 	fde = tevent_add_fd(ev, mem_ctx, fd, TEVENT_FD_READ,
202 			    reader_handler, req);
203 	assert(fde != NULL);
204 
205 	tevent_req_poll(req, ev);
206 
207 	reader_recv(req, &err);
208 	assert(err == 0);
209 
210 	close(fd);
211 
212 	talloc_free(mem_ctx);
213 }
214 
reader_test(bool fixed)215 static void reader_test(bool fixed)
216 {
217 	int fd[2];
218 	int ret;
219 	pid_t pid;
220 
221 	ret = pipe(fd);
222 	assert(ret == 0);
223 
224 	pid = fork();
225 	assert(pid != -1);
226 
227 	if (pid == 0) {
228 		/* Child process */
229 		close(fd[0]);
230 		writer(fd[1]);
231 		exit(0);
232 	}
233 
234 	close(fd[1]);
235 	ret = set_blocking(fd[0], false);
236 	if (ret == -1) {
237 		exit(1);
238 	}
239 
240 	reader(fd[0], fixed);
241 }
242 
main(void)243 int main(void)
244 {
245 	reader_test(true);
246 	reader_test(false);
247 
248 	return 0;
249 }
250