tsocket: return EINVAL when tdgram_sendto_send() is used with len == 0
[Samba/bb.git] / lib / tsocket / tsocket.c
blob11ae6ce9ed194ec863ec0cf161fe8536fa889102
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2009
6 ** NOTE! The following LGPL license applies to the tevent
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "replace.h"
25 #include "system/network.h"
26 #include "tsocket.h"
27 #include "tsocket_internal.h"
29 struct tsocket_address *_tsocket_address_create(TALLOC_CTX *mem_ctx,
30 const struct tsocket_address_ops *ops,
31 void *pstate,
32 size_t psize,
33 const char *type,
34 const char *location)
36 void **ppstate = (void **)pstate;
37 struct tsocket_address *addr;
39 addr = talloc_zero(mem_ctx, struct tsocket_address);
40 if (!addr) {
41 return NULL;
43 addr->ops = ops;
44 addr->location = location;
45 addr->private_data = talloc_size(addr, psize);
46 if (!addr->private_data) {
47 talloc_free(addr);
48 return NULL;
50 talloc_set_name_const(addr->private_data, type);
52 *ppstate = addr->private_data;
53 return addr;
56 char *tsocket_address_string(const struct tsocket_address *addr,
57 TALLOC_CTX *mem_ctx)
59 if (!addr) {
60 return talloc_strdup(mem_ctx, "NULL");
62 return addr->ops->string(addr, mem_ctx);
65 struct tsocket_address *_tsocket_address_copy(const struct tsocket_address *addr,
66 TALLOC_CTX *mem_ctx,
67 const char *location)
69 return addr->ops->copy(addr, mem_ctx, location);
72 struct tdgram_context {
73 const char *location;
74 const struct tdgram_context_ops *ops;
75 void *private_data;
78 struct tdgram_context *_tdgram_context_create(TALLOC_CTX *mem_ctx,
79 const struct tdgram_context_ops *ops,
80 void *pstate,
81 size_t psize,
82 const char *type,
83 const char *location)
85 struct tdgram_context *dgram;
86 void **ppstate = (void **)pstate;
87 void *state;
89 dgram = talloc(mem_ctx, struct tdgram_context);
90 if (dgram == NULL) {
91 return NULL;
93 dgram->location = location;
94 dgram->ops = ops;
96 state = talloc_size(dgram, psize);
97 if (state == NULL) {
98 talloc_free(dgram);
99 return NULL;
101 talloc_set_name_const(state, type);
103 dgram->private_data = state;
105 *ppstate = state;
106 return dgram;
109 void *_tdgram_context_data(struct tdgram_context *dgram)
111 return dgram->private_data;
114 struct tdgram_recvfrom_state {
115 const struct tdgram_context_ops *ops;
116 uint8_t *buf;
117 size_t len;
118 struct tsocket_address *src;
121 static void tdgram_recvfrom_done(struct tevent_req *subreq);
123 struct tevent_req *tdgram_recvfrom_send(TALLOC_CTX *mem_ctx,
124 struct tevent_context *ev,
125 struct tdgram_context *dgram)
127 struct tevent_req *req;
128 struct tdgram_recvfrom_state *state;
129 struct tevent_req *subreq;
131 req = tevent_req_create(mem_ctx, &state,
132 struct tdgram_recvfrom_state);
133 if (req == NULL) {
134 return NULL;
137 state->ops = dgram->ops;
139 subreq = state->ops->recvfrom_send(state, ev, dgram);
140 if (tevent_req_nomem(subreq, req)) {
141 goto post;
143 tevent_req_set_callback(subreq, tdgram_recvfrom_done, req);
145 return req;
147 post:
148 tevent_req_post(req, ev);
149 return req;
152 static void tdgram_recvfrom_done(struct tevent_req *subreq)
154 struct tevent_req *req = tevent_req_callback_data(subreq,
155 struct tevent_req);
156 struct tdgram_recvfrom_state *state = tevent_req_data(req,
157 struct tdgram_recvfrom_state);
158 ssize_t ret;
159 int sys_errno;
161 ret = state->ops->recvfrom_recv(subreq, &sys_errno, state,
162 &state->buf, &state->src);
163 if (ret == -1) {
164 tevent_req_error(req, sys_errno);
165 return;
168 state->len = ret;
170 tevent_req_done(req);
173 ssize_t tdgram_recvfrom_recv(struct tevent_req *req,
174 int *perrno,
175 TALLOC_CTX *mem_ctx,
176 uint8_t **buf,
177 struct tsocket_address **src)
179 struct tdgram_recvfrom_state *state = tevent_req_data(req,
180 struct tdgram_recvfrom_state);
181 ssize_t ret;
183 ret = tsocket_simple_int_recv(req, perrno);
184 if (ret == 0) {
185 *buf = talloc_move(mem_ctx, &state->buf);
186 ret = state->len;
187 if (src) {
188 *src = talloc_move(mem_ctx, &state->src);
192 tevent_req_received(req);
193 return ret;
196 struct tdgram_sendto_state {
197 const struct tdgram_context_ops *ops;
198 ssize_t ret;
201 static void tdgram_sendto_done(struct tevent_req *subreq);
203 struct tevent_req *tdgram_sendto_send(TALLOC_CTX *mem_ctx,
204 struct tevent_context *ev,
205 struct tdgram_context *dgram,
206 const uint8_t *buf, size_t len,
207 const struct tsocket_address *dst)
209 struct tevent_req *req;
210 struct tdgram_sendto_state *state;
211 struct tevent_req *subreq;
213 req = tevent_req_create(mem_ctx, &state,
214 struct tdgram_sendto_state);
215 if (req == NULL) {
216 return NULL;
219 state->ops = dgram->ops;
220 state->ret = -1;
222 if (len == 0) {
223 tevent_req_error(req, EINVAL);
224 goto post;
227 subreq = state->ops->sendto_send(state, ev, dgram,
228 buf, len, dst);
229 if (tevent_req_nomem(subreq, req)) {
230 goto post;
232 tevent_req_set_callback(subreq, tdgram_sendto_done, req);
234 return req;
236 post:
237 tevent_req_post(req, ev);
238 return req;
241 static void tdgram_sendto_done(struct tevent_req *subreq)
243 struct tevent_req *req = tevent_req_callback_data(subreq,
244 struct tevent_req);
245 struct tdgram_sendto_state *state = tevent_req_data(req,
246 struct tdgram_sendto_state);
247 ssize_t ret;
248 int sys_errno;
250 ret = state->ops->sendto_recv(subreq, &sys_errno);
251 if (ret == -1) {
252 tevent_req_error(req, sys_errno);
253 return;
256 state->ret = ret;
258 tevent_req_done(req);
261 ssize_t tdgram_sendto_recv(struct tevent_req *req,
262 int *perrno)
264 struct tdgram_sendto_state *state = tevent_req_data(req,
265 struct tdgram_sendto_state);
266 ssize_t ret;
268 ret = tsocket_simple_int_recv(req, perrno);
269 if (ret == 0) {
270 ret = state->ret;
273 tevent_req_received(req);
274 return ret;
277 struct tdgram_disconnect_state {
278 const struct tdgram_context_ops *ops;
281 static void tdgram_disconnect_done(struct tevent_req *subreq);
283 struct tevent_req *tdgram_disconnect_send(TALLOC_CTX *mem_ctx,
284 struct tevent_context *ev,
285 struct tdgram_context *dgram)
287 struct tevent_req *req;
288 struct tdgram_disconnect_state *state;
289 struct tevent_req *subreq;
291 req = tevent_req_create(mem_ctx, &state,
292 struct tdgram_disconnect_state);
293 if (req == NULL) {
294 return NULL;
297 state->ops = dgram->ops;
299 subreq = state->ops->disconnect_send(state, ev, dgram);
300 if (tevent_req_nomem(subreq, req)) {
301 goto post;
303 tevent_req_set_callback(subreq, tdgram_disconnect_done, req);
305 return req;
307 post:
308 tevent_req_post(req, ev);
309 return req;
312 static void tdgram_disconnect_done(struct tevent_req *subreq)
314 struct tevent_req *req = tevent_req_callback_data(subreq,
315 struct tevent_req);
316 struct tdgram_disconnect_state *state = tevent_req_data(req,
317 struct tdgram_disconnect_state);
318 int ret;
319 int sys_errno;
321 ret = state->ops->disconnect_recv(subreq, &sys_errno);
322 if (ret == -1) {
323 tevent_req_error(req, sys_errno);
324 return;
327 tevent_req_done(req);
330 int tdgram_disconnect_recv(struct tevent_req *req,
331 int *perrno)
333 int ret;
335 ret = tsocket_simple_int_recv(req, perrno);
337 tevent_req_received(req);
338 return ret;
341 struct tdgram_sendto_queue_state {
342 /* this structs are owned by the caller */
343 struct {
344 struct tevent_context *ev;
345 struct tdgram_context *dgram;
346 const uint8_t *buf;
347 size_t len;
348 const struct tsocket_address *dst;
349 } caller;
350 ssize_t ret;
353 static void tdgram_sendto_queue_trigger(struct tevent_req *req,
354 void *private_data);
355 static void tdgram_sendto_queue_done(struct tevent_req *subreq);
358 * @brief Queue a dgram blob for sending through the socket
359 * @param[in] mem_ctx The memory context for the result
360 * @param[in] ev The event context the operation should work on
361 * @param[in] dgram The tdgram_context to send the message buffer
362 * @param[in] queue The existing dgram queue
363 * @param[in] buf The message buffer
364 * @param[in] len The message length
365 * @param[in] dst The destination socket address
366 * @retval The async request handle
368 * This function queues a blob for sending to destination through an existing
369 * dgram socket. The async callback is triggered when the whole blob is
370 * delivered to the underlying system socket.
372 * The caller needs to make sure that all non-scalar input parameters hang
373 * arround for the whole lifetime of the request.
375 struct tevent_req *tdgram_sendto_queue_send(TALLOC_CTX *mem_ctx,
376 struct tevent_context *ev,
377 struct tdgram_context *dgram,
378 struct tevent_queue *queue,
379 const uint8_t *buf,
380 size_t len,
381 struct tsocket_address *dst)
383 struct tevent_req *req;
384 struct tdgram_sendto_queue_state *state;
385 bool ok;
387 req = tevent_req_create(mem_ctx, &state,
388 struct tdgram_sendto_queue_state);
389 if (!req) {
390 return NULL;
393 state->caller.ev = ev;
394 state->caller.dgram = dgram;
395 state->caller.buf = buf;
396 state->caller.len = len;
397 state->caller.dst = dst;
398 state->ret = -1;
400 ok = tevent_queue_add(queue,
402 req,
403 tdgram_sendto_queue_trigger,
404 NULL);
405 if (!ok) {
406 tevent_req_nomem(NULL, req);
407 goto post;
410 return req;
412 post:
413 tevent_req_post(req, ev);
414 return req;
417 static void tdgram_sendto_queue_trigger(struct tevent_req *req,
418 void *private_data)
420 struct tdgram_sendto_queue_state *state = tevent_req_data(req,
421 struct tdgram_sendto_queue_state);
422 struct tevent_req *subreq;
424 subreq = tdgram_sendto_send(state,
425 state->caller.ev,
426 state->caller.dgram,
427 state->caller.buf,
428 state->caller.len,
429 state->caller.dst);
430 if (tevent_req_nomem(subreq, req)) {
431 return;
433 tevent_req_set_callback(subreq, tdgram_sendto_queue_done, req);
436 static void tdgram_sendto_queue_done(struct tevent_req *subreq)
438 struct tevent_req *req = tevent_req_callback_data(subreq,
439 struct tevent_req);
440 struct tdgram_sendto_queue_state *state = tevent_req_data(req,
441 struct tdgram_sendto_queue_state);
442 ssize_t ret;
443 int sys_errno;
445 ret = tdgram_sendto_recv(subreq, &sys_errno);
446 talloc_free(subreq);
447 if (ret == -1) {
448 tevent_req_error(req, sys_errno);
449 return;
451 state->ret = ret;
453 tevent_req_done(req);
456 ssize_t tdgram_sendto_queue_recv(struct tevent_req *req, int *perrno)
458 struct tdgram_sendto_queue_state *state = tevent_req_data(req,
459 struct tdgram_sendto_queue_state);
460 ssize_t ret;
462 ret = tsocket_simple_int_recv(req, perrno);
463 if (ret == 0) {
464 ret = state->ret;
467 tevent_req_received(req);
468 return ret;