summaryrefslogtreecommitdiff
path: root/ctdb/common/pkt_read.h
blob: 25d4a51edc1fda276aed5537fee20c3d164e89e2 (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
/*
   API for reading packets using fixed and dynamic buffer

   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/>.
*/

#ifndef __CTDB_PKT_READ_H__
#define __CTDB_PKT_READ_H__

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

/**
 * @file pkt_read.h
 *
 * @brief Read a packet using fixed size buffer or allocated memory.
 *
 * CTDB communication uses lots of small packets.  This abstraction avoids the
 * need to allocate memory for small packets.  Only if the received packet is
 * larger than the fixed memory buffer, use talloc to allocate memory.
 */

/**
 * @brief Start async computation to read a packet
 *
 * This returns a tevent request to read a packet from given fd.  The fd
 * should be nonblocking. Freeing this request will free all the memory
 * associated with the request.
 *
 * @param[in] mem_ctx Talloc memory context
 * @param[in] ev Tevent context
 * @param[in] fd The non-blocking file/socket descriptor to read from
 * @param[in] initial Initial amount of data to read
 * @param[in] buf The static buffer to read data in
 * @param[in] buflen The size of the static buffer
 * @param[in] more The function to check if the bytes read forms a packet
 * @param[in] private_data Private data to pass to more function
 * @return new tevent request or NULL on failure
 */
struct tevent_req *pkt_read_send(TALLOC_CTX *mem_ctx,
				 struct tevent_context *ev,
				 int fd, size_t initial,
				 uint8_t *buf, size_t buflen,
				 ssize_t (*more)(uint8_t *buf,
						 size_t buflen,
						 void *private_data),
				 void *private_data);

/**
 * @brief Function to actually read data from the socket
 *
 * This function should be called, when tevent fd event is triggered.  This
 * function has the syntax of tevent_fd_handler_t.  The private_data for this
 * function is the tevent request created by pkt_read_send function.
 *
 * @param[in] ev Tevent context
 * @param[in] fde Tevent fd context
 * @param[in] flags Tevent fd flags
 * @param[in] req The active tevent request
 */
void pkt_read_handler(struct tevent_context *ev, struct tevent_fd *fde,
		      uint16_t flags, struct tevent_req *req);

/**
 * @brief Retrieve a packet
 *
 * This function returns the pkt read from fd.
 *
 * @param[in] req Tevent request
 * @param[in] mem_ctx Talloc memory context
 * @param[out] pbuf The pointer to the buffer
 * @param[out] free_buf Boolean to indicate that caller should free buffer
 * @param[out] perrno errno in case of failure
 * @return the size of the pkt, or -1 on failure
 *
 * If the pkt data is dynamically allocated, then it is moved under the
 * specified talloc memory context and free_buf is set to true.  It is the
 * responsibility of the caller to the free the memory returned.
 *
 * If the pkt data is stored in the fixed buffer, then free_buf is set to false.
 */
ssize_t pkt_read_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
		      uint8_t **pbuf, bool *free_buf, int *perrno);

#endif /* __CTDB_PKT_READ_H__ */