The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
proto_cron.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /**
18  * $Id: 5b1561ca7537bc8b2e95c36ab723c2d7d5f5e9d8 $
19  * @file proto_cron.c
20  * @brief Load master protocol handler.
21  *
22  * @copyright 2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23  * @copyright 2016 Alan DeKok (aland@freeradius.org)
24  */
25 #include <freeradius-devel/io/application.h>
26 #include <freeradius-devel/io/listen.h>
27 #include <freeradius-devel/io/schedule.h>
28 #include <freeradius-devel/radius/radius.h>
29 
30 #include "proto_cron.h"
31 
32 extern fr_app_t proto_cron;
33 static int type_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
34 static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
35 
36 /** How to parse a Load listen section
37  *
38  */
39 static conf_parser_t const proto_cron_config[] = {
41  type), .func = type_parse },
42  { FR_CONF_OFFSET_TYPE_FLAGS("transport", FR_TYPE_VOID, 0, proto_cron_t, io.submodule),
43  .func = transport_parse, .dflt = "crontab" },
44 
45  /*
46  * Add this as a synonym so normal humans can understand it.
47  */
48  { FR_CONF_OFFSET("max_entry_size", proto_cron_t, max_packet_size) } ,
49 
50  /*
51  * For performance tweaking. NOT for normal humans.
52  */
53  { FR_CONF_OFFSET("max_packet_size", proto_cron_t, max_packet_size) } ,
54  { FR_CONF_OFFSET("num_messages", proto_cron_t, num_messages) } ,
55 
56  { FR_CONF_OFFSET("priority", proto_cron_t, priority) },
57 
59 };
60 
61 static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
62 {
63  proto_cron_t *inst = talloc_get_type_abort(parent, proto_cron_t);
65 
66  if (unlikely(virtual_server_listen_transport_parse(ctx, out, parent, ci, rule) < 0)) {
67  return -1;
68  }
69 
70  mi = talloc_get_type_abort(*(void **)out, module_instance_t);
71  inst->io.app_io = (fr_app_io_t const *)mi->exported;
72  inst->io.app_io_instance = mi->data;
73  inst->io.app_io_conf = mi->conf;
74 
75  return 0;
76 }
77 
78 /** Translates the packet-type into a submodule name
79  *
80  * @param[in] ctx to allocate data in (instance of proto_cron).
81  * @param[out] out Where to write a module_instance_t containing the module handle and instance.
82  * @param[in] parent Base structure address.
83  * @param[in] ci #CONF_PAIR specifying the name of the type module.
84  * @param[in] rule unused.
85  * @return
86  * - 0 on success.
87  * - -1 on failure.
88  */
89 static int type_parse(UNUSED TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
90 {
91  proto_cron_t *inst = talloc_get_type_abort(parent, proto_cron_t);
92  fr_dict_enum_value_t const *type_enum;
93  CONF_PAIR *cp = cf_item_to_pair(ci);
94  char const *value = cf_pair_value(cp);
95 
96  *((char const **) out) = value;
97 
99  if (!inst->dict) {
100  cf_log_err(ci, "Please define 'namespace' in this virtual server");
101  return -1;
102  }
103 
104  inst->attr_packet_type = fr_dict_attr_by_name(NULL, fr_dict_root(inst->dict), "Packet-Type");
105  if (!inst->attr_packet_type) {
106  cf_log_err(ci, "Failed to find 'Packet-Type' attribute");
107  return -1;
108  }
109 
110  if (!value) {
111  cf_log_err(ci, "No value given for 'type'");
112  return -1;
113  }
114 
115  type_enum = fr_dict_enum_by_name(inst->attr_packet_type, value, -1);
116  if (!type_enum) {
117  cf_log_err(ci, "Invalid type \"%s\"", value);
118  return -1;
119  }
120 
121  inst->code = type_enum->value->vb_uint32;
122  return 0;
123 }
124 
125 /** Decode the packet, and set the request->process function
126  *
127  */
128 static int mod_decode(void const *instance, request_t *request, uint8_t *const data, size_t data_len)
129 {
131 
132  request->dict = inst->dict;
133  request->packet->code = inst->code;
134 
135  /*
136  * Set default addresses
137  */
138  request->packet->socket.fd = -1;
139  request->packet->socket.inet.src_ipaddr.af = AF_INET;
140  request->packet->socket.inet.src_ipaddr.addr.v4.s_addr = htonl(INADDR_NONE);
141  request->packet->socket.inet.dst_ipaddr = request->packet->socket.inet.src_ipaddr;
142 
143  request->reply->socket.inet.src_ipaddr = request->packet->socket.inet.src_ipaddr;
144  request->reply->socket.inet.dst_ipaddr = request->packet->socket.inet.src_ipaddr;
145 
146  /*
147  * The app_io is responsible for decoding all of the data.
148  */
149  return inst->io.app_io->decode(inst->io.app_io_instance, request, data, data_len);
150 }
151 
152 /*
153  * We don't need to encode any of the replies. We just go "yeah, it's fine".
154  */
155 static ssize_t mod_encode(UNUSED void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
156 {
157  if (buffer_len < 1) return -1;
158 
159  *buffer = request->reply->code;
160  return 1;
161 }
162 
163 /** Open listen sockets/connect to external event source
164  *
165  * @param[in] instance Ctx data for this application.
166  * @param[in] sc to add our file descriptor to.
167  * @param[in] conf Listen section parsed to give us instance.
168  * @return
169  * - 0 on success.
170  * - -1 on failure.
171  */
172 static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
173 {
174  proto_cron_t *inst = talloc_get_type_abort(instance, proto_cron_t);
175 
176  inst->io.app = &proto_cron;
177  inst->io.app_instance = instance;
178 
179  /*
180  * io.app_io should already be set
181  */
182  return fr_master_io_listen(&inst->io, sc,
183  inst->max_packet_size, inst->num_messages);
184 }
185 
186 
187 /** Instantiate the application
188  *
189  * Instantiate I/O and type submodules.
190  *
191  * @return
192  * - 0 on success.
193  * - -1 on failure.
194  */
195 static int mod_instantiate(module_inst_ctx_t const *mctx)
196 {
197  proto_cron_t *inst = talloc_get_type_abort(mctx->mi->data, proto_cron_t);
198  CONF_SECTION *conf = mctx->mi->conf;
199 
200  /*
201  * Ensure that the server CONF_SECTION is always set.
202  */
203  inst->io.server_cs = cf_item_to_section(cf_parent(conf));
204 
205  /*
206  * No IO module, it's an empty listener.
207  */
208  if (!inst->io.submodule) {
209  cf_log_err(conf, "The load generator MUST have a 'transport = ...' set");
210  return -1;
211  }
212 
213  /*
214  * Tell the master handler about the main protocol instance.
215  */
216  inst->io.app = &proto_cron;
217  inst->io.app_instance = inst;
218 
219  /*
220  * The listener is inside of a virtual server.
221  */
222  inst->server_cs = cf_item_to_section(cf_parent(conf));
223  inst->cs = conf;
224  inst->self = &proto_cron;
225 
226  /*
227  * We will need this for dynamic clients and connected sockets.
228  */
229  inst->io.mi = mctx->mi;
230 
231  /*
232  * These configuration items are not printed by default,
233  * because normal people shouldn't be touching them.
234  */
235  if (!inst->max_packet_size && inst->io.app_io) inst->max_packet_size = inst->io.app_io->default_message_size;
236 
237  if (!inst->num_messages) inst->num_messages = 256;
238 
239  FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, >=, 32);
240  FR_INTEGER_BOUND_CHECK("num_messages", inst->num_messages, <=, 65535);
241 
242  FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, >=, 1024);
243  FR_INTEGER_BOUND_CHECK("max_packet_size", inst->max_packet_size, <=, 65535);
244 
245  /*
246  * Instantiate the transport module before calling the
247  * common instantiation function.
248  */
249  if (module_instantiate(inst->io.submodule) < 0) return -1;
250 
251  /*
252  * Instantiate the master io submodule
253  */
255 }
256 
258  .common = {
259  .magic = MODULE_MAGIC_INIT,
260  .name = "cron",
261  .config = proto_cron_config,
262  .inst_size = sizeof(proto_cron_t),
264  },
265  .open = mod_open,
266  .decode = mod_decode,
267  .encode = mod_encode,
268 };
static int const char char buffer[256]
Definition: acutest.h:574
module_t common
Common fields to all loadable modules.
Definition: app_io.h:34
Public structure describing an I/O path for a protocol.
Definition: app_io.h:33
module_t common
Common fields provided by all modules.
Definition: application.h:72
Describes a new application (protocol)
Definition: application.h:71
#define unlikely(_x)
Definition: build.h:379
#define UNUSED
Definition: build.h:313
#define CONF_PARSER_TERMINATOR
Definition: cf_parse.h:627
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition: cf_parse.h:487
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition: cf_parse.h:268
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition: cf_parse.h:405
@ CONF_FLAG_NOT_EMPTY
CONF_PAIR is required to have a non zero length value.
Definition: cf_parse.h:420
#define FR_CONF_OFFSET_TYPE_FLAGS(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition: cf_parse.h:241
Defines a CONF_PAIR to C data type mapping.
Definition: cf_parse.h:564
Common header for all CONF_* types.
Definition: cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition: cf_priv.h:70
A section grouping multiple CONF_PAIR.
Definition: cf_priv.h:101
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition: cf_util.c:664
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition: cf_util.c:1594
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition: cf_util.c:684
#define cf_log_err(_cf, _fmt,...)
Definition: cf_util.h:289
#define cf_parent(_cf)
Definition: cf_util.h:101
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition: dict_util.c:3263
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition: dict_util.c:2400
fr_value_box_t const * value
Enum value (what name maps to).
Definition: dict.h:230
fr_dict_enum_value_t * fr_dict_enum_by_name(fr_dict_attr_t const *da, char const *name, ssize_t len)
Definition: dict_util.c:3395
Value of an enumerated attribute.
Definition: dict.h:226
Test enumeration values.
Definition: dict_test.h:92
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition: dl_module.h:63
fr_app_io_t fr_master_app_io
Definition: master.c:3139
int fr_master_io_listen(fr_io_instance_t *inst, fr_schedule_t *sc, size_t default_message_size, size_t num_messages)
Definition: master.c:2931
@ FR_TYPE_VOID
User data.
Definition: merged_model.c:127
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
#define MODULE_INST_CTX(_mi)
Wrapper to create a module_inst_ctx_t as a compound literal.
Definition: module_ctx.h:158
module_instance_t * mi
Instance of the module being instantiated.
Definition: module_ctx.h:51
Temporary structure to hold arguments for instantiation calls.
Definition: module_ctx.h:50
static ssize_t mod_encode(UNUSED void const *instance, request_t *request, uint8_t *buffer, size_t buffer_len)
Definition: proto_cron.c:155
fr_app_t proto_cron
Definition: proto_cron.c:257
static int type_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
static int mod_decode(void const *instance, request_t *request, uint8_t *const data, size_t data_len)
Decode the packet, and set the request->process function.
Definition: proto_cron.c:128
static conf_parser_t const proto_cron_config[]
How to parse a Load listen section.
Definition: proto_cron.c:39
static int transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Definition: proto_cron.c:61
static int mod_instantiate(module_inst_ctx_t const *mctx)
Instantiate the application.
Definition: proto_cron.c:195
static int mod_open(void *instance, fr_schedule_t *sc, UNUSED CONF_SECTION *conf)
Open listen sockets/connect to external event source.
Definition: proto_cron.c:172
Cron master protocol handler.
static rs_t * conf
Definition: radsniff.c:53
static int instantiate(module_inst_ctx_t const *mctx)
Definition: rlm_rest.c:1302
The scheduler.
Definition: schedule.c:125
CONF_SECTION * conf
Module's instance configuration.
Definition: module.h:329
void * data
Module's instance data.
Definition: module.h:271
module_instantiate_t instantiate
Callback to allow the module to register any per-instance resources like sockets and file handles.
Definition: module.h:218
module_t * exported
Public module structure.
Definition: module.h:276
Module instance data.
Definition: module.h:265
static const uchar sc[16]
Definition: smbdes.c:115
int module_instantiate(module_instance_t *instance)
Manually complete module setup by calling its instantiate function.
Definition: module.c:1195
eap_aka_sim_process_conf_t * inst
fr_aka_sim_id_type_t type
#define talloc_get_type_abort_const
Definition: talloc.h:282
static fr_slen_t parent
Definition: pair.h:851
static fr_slen_t data
Definition: value.h:1265
static size_t char ** out
Definition: value.h:997
fr_dict_t const * virtual_server_dict_by_child_ci(CONF_ITEM const *ci)
Return the namespace for a given virtual server specified by a CONF_ITEM within the virtual server.
int virtual_server_listen_transport_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic conf_parser_t func for loading drivers.