2 * This file is part of secnet.
3 * See README for full list of copyright holders.
5 * secnet is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * secnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * version 3 along with secnet; if not, see
17 * https://www.gnu.org/licenses/gpl.html.
35 static random_fn random_generate;
36 static void random_generate(void *data, int32_t bytes, uint8_t *buff)
38 struct rgen_data *st=data;
41 r= read(st->fd,buff,bytes);
44 /* This is totally crap error checking, but callers of
45 * this function do not check the return value and dealing
46 * with failure of this everywhere would be very inconvenient.
50 static list_t *random_apply(closure_t *self, struct cloc loc,
51 dict_t *context, list_t *args)
55 string_t filename=NULL;
59 st->cl.description="randomsource";
60 st->cl.type=CL_RANDOMSRC;
62 st->cl.interface=&st->ops;
64 st->ops.blocking=False;
65 st->ops.generate=random_generate;
68 arg1=list_elem(args,0);
69 arg2=list_elem(args,1);
72 cfgfatal(loc,"randomsource","requires a filename\n");
74 if (arg1->type != t_string) {
75 cfgfatal(arg1->loc,"randomsource",
76 "filename (arg1) must be a string\n");
78 filename=arg1->data.string;
81 if (arg2->type != t_bool) {
82 cfgfatal(arg2->loc,"randomsource",
83 "blocking parameter (arg2) must be bool\n");
85 st->ops.blocking=arg2->data.bool;
89 cfgfatal(loc,"randomsource","requires a filename\n");
91 st->fd=open(filename,O_RDONLY);
93 fatal_perror("randomsource (%s:%d): cannot open %s",arg1->loc.file,
94 arg1->loc.line,filename);
96 return new_closure(&st->cl);
99 void random_module(dict_t *d)
101 add_closure(d,"randomfile",random_apply);