summaryrefslogtreecommitdiffstats
path: root/src/libknot/control/control.h
blob: 1d3dcd164e8c61554b23388634c491580fbad19f (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*  Copyright (C) 2022 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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 <https://www.gnu.org/licenses/>.
 */

/*!
 * \file
 *
 * \brief A server control interface.
 *
 * \addtogroup ctl
 * @{
 */

#pragma once

/*! Control data item indexes. */
typedef enum {
	KNOT_CTL_IDX_CMD = 0, /*!< Control command name. */
	KNOT_CTL_IDX_FLAGS,   /*!< Control command flags. */
	KNOT_CTL_IDX_ERROR,   /*!< Error message. */
	KNOT_CTL_IDX_SECTION, /*!< Configuration section name. */
	KNOT_CTL_IDX_ITEM,    /*!< Configuration item name. */
	KNOT_CTL_IDX_ID,      /*!< Configuration item identifier. */
	KNOT_CTL_IDX_ZONE,    /*!< Zone name. */
	KNOT_CTL_IDX_OWNER,   /*!< Zone record owner */
	KNOT_CTL_IDX_TTL,     /*!< Zone record TTL. */
	KNOT_CTL_IDX_TYPE,    /*!< Zone record type name. */
	KNOT_CTL_IDX_DATA,    /*!< Configuration item/zone record data. */
	KNOT_CTL_IDX_FILTER,  /*!< An option or a filter for output data processing. */
	KNOT_CTL_IDX__COUNT,  /*!< The number of data items. */
} knot_ctl_idx_t;

/*! Control unit types. */
typedef enum {
	KNOT_CTL_TYPE_END,   /*!< End of message, cache flushed. */
	KNOT_CTL_TYPE_DATA,  /*!< Data unit, cached. */
	KNOT_CTL_TYPE_EXTRA, /*!< Extra value data unit, cached. */
	KNOT_CTL_TYPE_BLOCK, /*!< End of data block, cache flushed. */
} knot_ctl_type_t;

/*! Control input/output string data. */
typedef const char* knot_ctl_data_t[KNOT_CTL_IDX__COUNT];

/*! A control context. */
struct knot_ctl;
typedef struct knot_ctl knot_ctl_t;

/*!
 * Allocates a control context.
 *
 * \return Control context.
 */
knot_ctl_t* knot_ctl_alloc(void);

/*!
 * Deallocates a control context.
 *
 * \param[in] ctx  Control context.
 */
void knot_ctl_free(knot_ctl_t *ctx);

/*!
 * Sets the timeout for socket operations.
 *
 * Default value is 30 seconds.
 *
 * \param[in] ctx         Control context.
 * \param[in] timeout_ms  Timeout in milliseconds (0 for infinity).
 */
void knot_ctl_set_timeout(knot_ctl_t *ctx, int timeout_ms);

/*!
 * Binds a specified UNIX socket path.
 *
 * \note Server operation.
 *
 * \param[in] ctx   Control context.
 * \param[in] path  Control UNIX socket path.
 *
 * \return Error code, KNOT_EOK if successful.
 */
int knot_ctl_bind(knot_ctl_t *ctx, const char *path);

/*!
 * Unbinds a control socket.
 *
 * \note Server operation.
 *
 * \param[in] ctx  Control context.
 */
void knot_ctl_unbind(knot_ctl_t *ctx);

/*!
 * Connects to a specified UNIX socket path.
 *
 * \note Client operation.
 *
 * \param[in] ctx   Control context.
 * \param[in] path  Control UNIX socket path.
 *
 * \return Error code, KNOT_EOK if successful.
 */
int knot_ctl_connect(knot_ctl_t *ctx, const char *path);

/*!
 * Waits for an incoming connection.
 *
 * \note Server operation.
 *
 * \param[in] ctx  Control context.
 *
 * \retval KNOT_EOK       if successful.
 * \retval KNOT_ETIMEOUT  if no connection accepted during ACCEPT_TIMEOUT interval.
 * \retval KNOT_E*        if error.
 */
int knot_ctl_accept(knot_ctl_t *ctx);

/*!
 * Closes the remote connections.
 *
 * \note Applies to both server and client.
 *
 * \param[in] ctx  Control context.
 */
void knot_ctl_close(knot_ctl_t *ctx);

/*!
 * Sends one control unit.
 *
 * \param[in] ctx   Control context.
 * \param[in] type  Unit type to send.
 * \param[in] data  Data unit to send (optional, ignored if non-data type).
 *
 * \return Error code, KNOT_EOK if successful.
 */
int knot_ctl_send(knot_ctl_t *ctx, knot_ctl_type_t type, knot_ctl_data_t *data);

/*!
 * Receives one control unit.
 *
 * \param[in] ctx    Control context.
 * \param[out] type  Received unit type.
 * \param[out] data  Received data unit (optional).
 *
 * \return Error code, KNOT_EOK if successful.
 */
int knot_ctl_receive(knot_ctl_t *ctx, knot_ctl_type_t *type, knot_ctl_data_t *data);

/*! @} */