7 #include "types/types.h"
9 #include <talloc/talloc.h>
11 #include "device-handler.h"
13 #include "parser-utils.h"
17 static struct parser **parsers;
19 static const int max_file_size = 1024 * 1024;
21 static int read_file(struct discover_context *ctx,
22 const char *filename, char **bufp, int *lenp)
28 fd = open(filename, O_RDONLY);
32 rc = fstat(fd, &statbuf);
36 len = statbuf.st_size;
37 if (len > max_file_size)
40 buf = talloc_array(ctx, char, len + 1);
44 for (i = 0; i < len; i += rc) {
45 rc = read(fd, buf + i, len - i);
47 /* unexpected EOF: trim and return */
72 static char *local_path(struct discover_context *ctx,
75 return join_paths(ctx, ctx->device->mount_path, filename);
78 static int download_config(struct discover_context *ctx, char **buf, int *len)
84 file = load_url(ctx, ctx->conf_url, &tempfile);
88 rc = read_file(ctx, file, buf, len);
100 static void iterate_parser_files(struct discover_context *ctx,
101 const struct parser *parser)
103 const char * const *filename;
106 if (!parser->filenames)
109 for (filename = parser->filenames; *filename; filename++) {
113 path = local_path(ctx, *filename);
117 rc = read_file(ctx, path, &buf, &len);
119 parser->parse(ctx, buf, len);
125 void iterate_parsers(struct discover_context *ctx, enum conf_method method)
130 pb_log("trying parsers for %s\n", ctx->device->device->id);
133 case CONF_METHOD_LOCAL_FILE:
134 for (i = 0; i < n_parsers; i++) {
135 if (parsers[i]->method != CONF_METHOD_LOCAL_FILE)
138 pb_log("\ttrying parser '%s'\n", parsers[i]->name);
139 ctx->parser = parsers[i];
140 iterate_parser_files(ctx, ctx->parser);
145 case CONF_METHOD_DHCP:
146 rc = download_config(ctx, &buf, &len);
148 pb_log("\tdownload failed, aborting\n");
152 for (i = 0; i < n_parsers; i++) {
153 if (parsers[i]->method != method)
156 pb_log("\ttrying parser '%s'\n", parsers[i]->name);
157 ctx->parser = parsers[i];
158 parsers[i]->parse(ctx, buf, len);
163 case CONF_METHOD_UNKNOWN:
169 void __register_parser(struct parser *parser)
171 parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1);
172 parsers[n_parsers] = parser;
176 void parser_init(void)