Commit | Line | Data |
---|---|---|
8689b3a9 | 1 | #include "secnet.h" |
2fe58dfd SE |
2 | #include <stdio.h> |
3 | #include <fcntl.h> | |
4 | #include <string.h> | |
2fe58dfd SE |
5 | #include <sys/stat.h> |
6 | #include <unistd.h> | |
7 | ||
2fe58dfd SE |
8 | struct rgen_data { |
9 | closure_t cl; | |
10 | struct random_if ops; | |
11 | struct cloc loc; | |
12 | int fd; | |
13 | }; | |
14 | ||
15 | static random_fn random_generate; | |
16 | static bool_t random_generate(void *data, uint32_t bytes, uint8_t *buff) | |
17 | { | |
18 | struct rgen_data *st=data; | |
19 | ||
20 | /* XXX XXX error checking */ | |
21 | read(st->fd,buff,bytes); | |
22 | ||
23 | return True; | |
24 | } | |
25 | ||
26 | static list_t *random_apply(closure_t *self, struct cloc loc, | |
27 | dict_t *context, list_t *args) | |
28 | { | |
29 | struct rgen_data *st; | |
30 | item_t *arg1, *arg2; | |
31 | string_t filename=NULL; | |
32 | ||
33 | st=safe_malloc(sizeof(*st),"random_apply"); | |
34 | ||
35 | st->cl.description="randomsource"; | |
36 | st->cl.type=CL_RANDOMSRC; | |
37 | st->cl.apply=NULL; | |
38 | st->cl.interface=&st->ops; | |
39 | st->ops.st=st; | |
40 | st->ops.blocking=False; | |
41 | st->ops.generate=random_generate; | |
42 | st->loc=loc; | |
43 | ||
44 | arg1=list_elem(args,0); | |
45 | arg2=list_elem(args,1); | |
46 | ||
47 | if (!arg1) { | |
48 | fatal("randomsource: requires a filename\n"); | |
49 | } | |
50 | if (arg1->type != t_string) { | |
51 | cfgfatal(arg1->loc,"randomsource", | |
52 | "filename (arg1) must be a string\n"); | |
53 | } | |
54 | filename=arg1->data.string; | |
55 | ||
56 | if (arg2) { | |
57 | if (arg2->type != t_bool) { | |
58 | cfgfatal(arg2->loc,"randomsource", | |
59 | "blocking parameter (arg2) must be bool\n"); | |
60 | } | |
61 | st->ops.blocking=arg2->data.bool; | |
62 | } | |
63 | ||
64 | if (!filename) { | |
65 | fatal("randomsource requires a filename"); | |
66 | } | |
67 | st->fd=open(filename,O_RDONLY); | |
68 | if (st->fd<0) { | |
69 | fatal_perror("randomsource (%s:%d): cannot open %s",arg1->loc.file, | |
70 | arg1->loc.line,filename); | |
71 | } | |
72 | return new_closure(&st->cl); | |
73 | } | |
74 | ||
75 | void random_module(dict_t *d) | |
76 | { | |
77 | add_closure(d,"randomfile",random_apply); | |
78 | } |