| 1 | /*
|
|---|
| 2 | API for reading packets using fixed and dynamic buffer
|
|---|
| 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 | #ifndef __CTDB_PKT_READ_H__
|
|---|
| 21 | #define __CTDB_PKT_READ_H__
|
|---|
| 22 |
|
|---|
| 23 | #include <talloc.h>
|
|---|
| 24 | #include <tevent.h>
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * @file pkt_read.h
|
|---|
| 28 | *
|
|---|
| 29 | * @brief Read a packet using fixed size buffer or allocated memory.
|
|---|
| 30 | *
|
|---|
| 31 | * CTDB communication uses lots of small packets. This abstraction avoids the
|
|---|
| 32 | * need to allocate memory for small packets. Only if the received packet is
|
|---|
| 33 | * larger than the fixed memory buffer, use talloc to allocate memory.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * @brief Start async computation to read a packet
|
|---|
| 38 | *
|
|---|
| 39 | * This returns a tevent request to read a packet from given fd. The fd
|
|---|
| 40 | * should be nonblocking. Freeing this request will free all the memory
|
|---|
| 41 | * associated with the request.
|
|---|
| 42 | *
|
|---|
| 43 | * @param[in] mem_ctx Talloc memory context
|
|---|
| 44 | * @param[in] ev Tevent context
|
|---|
| 45 | * @param[in] fd The non-blocking file/socket descriptor to read from
|
|---|
| 46 | * @param[in] initial Initial amount of data to read
|
|---|
| 47 | * @param[in] buf The static buffer to read data in
|
|---|
| 48 | * @param[in] buflen The size of the static buffer
|
|---|
| 49 | * @param[in] more The function to check if the bytes read forms a packet
|
|---|
| 50 | * @param[in] private_data Private data to pass to more function
|
|---|
| 51 | * @return new tevent request or NULL on failure
|
|---|
| 52 | */
|
|---|
| 53 | struct tevent_req *pkt_read_send(TALLOC_CTX *mem_ctx,
|
|---|
| 54 | struct tevent_context *ev,
|
|---|
| 55 | int fd, size_t initial,
|
|---|
| 56 | uint8_t *buf, size_t buflen,
|
|---|
| 57 | ssize_t (*more)(uint8_t *buf,
|
|---|
| 58 | size_t buflen,
|
|---|
| 59 | void *private_data),
|
|---|
| 60 | void *private_data);
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * @brief Function to actually read data from the socket
|
|---|
| 64 | *
|
|---|
| 65 | * This function should be called, when tevent fd event is triggered. This
|
|---|
| 66 | * function has the syntax of tevent_fd_handler_t. The private_data for this
|
|---|
| 67 | * function is the tevent request created by pkt_read_send function.
|
|---|
| 68 | *
|
|---|
| 69 | * @param[in] ev Tevent context
|
|---|
| 70 | * @param[in] fde Tevent fd context
|
|---|
| 71 | * @param[in] flags Tevent fd flags
|
|---|
| 72 | * @param[in] req The active tevent request
|
|---|
| 73 | */
|
|---|
| 74 | void pkt_read_handler(struct tevent_context *ev, struct tevent_fd *fde,
|
|---|
| 75 | uint16_t flags, struct tevent_req *req);
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * @brief Retrieve a packet
|
|---|
| 79 | *
|
|---|
| 80 | * This function returns the pkt read from fd.
|
|---|
| 81 | *
|
|---|
| 82 | * @param[in] req Tevent request
|
|---|
| 83 | * @param[in] mem_ctx Talloc memory context
|
|---|
| 84 | * @param[out] pbuf The pointer to the buffer
|
|---|
| 85 | * @param[out] free_buf Boolean to indicate that caller should free buffer
|
|---|
| 86 | * @param[out] perrno errno in case of failure
|
|---|
| 87 | * @return the size of the pkt, or -1 on failure
|
|---|
| 88 | *
|
|---|
| 89 | * If the pkt data is dynamically allocated, then it is moved under the
|
|---|
| 90 | * specified talloc memory context and free_buf is set to true. It is the
|
|---|
| 91 | * responsibility of the caller to the free the memory returned.
|
|---|
| 92 | *
|
|---|
| 93 | * If the pkt data is stored in the fixed buffer, then free_buf is set to false.
|
|---|
| 94 | */
|
|---|
| 95 | ssize_t pkt_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
|
|---|
| 96 | uint8_t **pbuf, bool *free_buf, int *perrno);
|
|---|
| 97 |
|
|---|
| 98 | #endif /* __CTDB_PKT_READ_H__ */
|
|---|