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