2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
10 * Author: Harti Brandt <harti@freebsd.org>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $Begemot: bsnmp/gensnmptree/gensnmptree.c,v 1.42 2005/04/26 16:26:19 brandt_h Exp $
35 * Generate OID table from table description.
39 * file := tree | tree file
41 * tree := head elements ')'
43 * entry := head ':' index STRING elements ')'
45 * leaf := head TYPE STRING ACCESS ')'
47 * column := head TYPE ACCESS ')'
49 * head := '(' INT STRING
51 * elements := EMPTY | elements element
53 * element := tree | leaf | column
55 * index := TYPE | index TYPE
58 #include <sys/types.h>
59 #include <sys/param.h>
71 #include <sys/queue.h>
75 #include "snmpagent.h"
78 * Constant prefix for all OIDs
80 static const asn_subid_t prefix[] = { 1, 3, 6 };
81 #define PREFIX_LEN (sizeof(prefix) / sizeof(prefix[0]))
84 static const char *file_prefix = "";
87 /* if true generate local include paths */
88 static int localincs = 0;
90 static const char usgtxt[] = "\
91 Generate SNMP tables. Copyright (c) 2001-2002 Fraunhofer Institute for\n\
92 Open Communication Systems (FhG Fokus). All rights reserved.\n\
93 usage: gensnmptree [-hel] [-p prefix] [name]...\n\
96 -e extrace the named oids\n\
97 -l generate local include directives\n\
98 -p prefix prepend prefix to file and variable names\n\
102 * A node in the OID tree
117 TAILQ_HEAD(node_list, node);
121 asn_subid_t id; /* last element of OID */
122 char *name; /* name of node */
123 TAILQ_ENTRY(node) link;
124 u_int lno; /* starting line number */
125 u_int flags; /* allowed operations */
129 struct node_list subs;
133 uint32_t index; /* index for table entry */
134 char *func; /* function for tables */
135 struct node_list subs;
139 enum snmp_syntax syntax; /* syntax for this leaf */
140 char *func; /* function name */
144 enum snmp_syntax syntax; /* syntax for this column */
151 LIST_ENTRY(func) link;
154 static LIST_HEAD(, func) funcs = LIST_HEAD_INITIALIZER(funcs);
156 /************************************************************
158 * Allocate memory and panic just in the case...
165 if ((ptr = malloc(size)) == NULL)
166 err(1, "allocing %zu bytes", size);
171 /************************************************************
176 TOK_EOF = 0200, /* end-of-file seen */
177 TOK_NUM, /* number */
178 TOK_STR, /* string */
179 TOK_ACCESS, /* access operator */
180 TOK_TYPE, /* type operator */
183 static const struct {
188 { "GET", TOK_ACCESS, FL_GET },
189 { "SET", TOK_ACCESS, FL_SET },
190 { "NULL", TOK_TYPE, SNMP_SYNTAX_NULL },
191 { "INTEGER", TOK_TYPE, SNMP_SYNTAX_INTEGER },
192 { "INTEGER32", TOK_TYPE, SNMP_SYNTAX_INTEGER },
193 { "UNSIGNED32", TOK_TYPE, SNMP_SYNTAX_GAUGE },
194 { "OCTETSTRING", TOK_TYPE, SNMP_SYNTAX_OCTETSTRING },
195 { "IPADDRESS", TOK_TYPE, SNMP_SYNTAX_IPADDRESS },
196 { "OID", TOK_TYPE, SNMP_SYNTAX_OID },
197 { "TIMETICKS", TOK_TYPE, SNMP_SYNTAX_TIMETICKS },
198 { "COUNTER", TOK_TYPE, SNMP_SYNTAX_COUNTER },
199 { "GAUGE", TOK_TYPE, SNMP_SYNTAX_GAUGE },
200 { "COUNTER64", TOK_TYPE, SNMP_SYNTAX_COUNTER64 },
204 /* arbitrary upper limit on node names and function names */
207 u_long val; /* integer values */
208 u_int lno = 1; /* current line number */
209 int all_cond; /* all conditions are true */
211 static void report(const char *, ...) __dead2 __printflike(1, 2);
212 static void report_node(const struct node *, const char *, ...)
213 __dead2 __printflike(2, 3);
216 * Report an error and exit.
219 report(const char *fmt, ...)
225 fprintf(stderr, "line %u: ", lno);
226 vfprintf(stderr, fmt, ap);
227 fprintf(stderr, "\n");
228 fprintf(stderr, "context: \"");
229 while ((c = getchar()) != EOF && c != '\n')
230 fprintf(stderr, "%c", c);
231 fprintf(stderr, "\n");
236 report_node(const struct node *np, const char *fmt, ...)
241 fprintf(stderr, "line %u, node %s: ", np->lno, np->name);
242 vfprintf(stderr, fmt, ap);
243 fprintf(stderr, "\n");
249 * Return a fresh copy of the string constituting the current token.
254 return (strcpy(xalloc(strlen(str)+1), str));
258 * Get the next token from input.
267 * Skip any whitespace before the next token
269 while ((c = getchar()) != EOF) {
278 report("unexpected character %#2x", (u_int)c);
284 while ((c = getchar()) != EOF) {
290 report("unexpected EOF in comment");
294 * Single character tokens
296 if (c == ')' || c == '(' || c == ':')
309 * So that has to be a string.
311 if (isalpha(c) || c == '_') {
314 while ((c = getchar()) != EOF) {
315 if (!isalnum(c) && c != '_') {
319 if (n == sizeof(str) - 1) {
321 report("string too long '%s...'", str);
330 for (c = 0; keywords[c].str != NULL; c++)
331 if (strcmp(keywords[c].str, str) == 0) {
332 val = keywords[c].val;
333 return (keywords[c].tok);
339 errx(1, "%u: unexpected character '%c'", lno, c);
341 errx(1, "%u: unexpected character 0x%02x", lno, (u_int)c);
345 * Parse the next node (complete with all subnodes)
354 node = xalloc(sizeof(struct node));
358 report("'(' expected at begin of node");
359 if (gettoken() != TOK_NUM)
360 report("node id expected after opening '('");
362 report("subid too large '%lu'", val);
363 node->id = (asn_subid_t)val;
364 if (gettoken() != TOK_STR)
365 report("node name expected after '(' ID");
366 node->name = savetok();
368 if ((tok = gettoken()) == TOK_TYPE) {
372 if ((tok = gettoken()) == TOK_STR) {
374 node->type = NODE_LEAF;
375 node->u.leaf.func = savetok();
376 node->u.leaf.syntax = syntax;
380 node->type = NODE_COLUMN;
381 node->u.column.syntax = syntax;
385 if (tok != TOK_ACCESS)
386 report("access keyword or ')' expected");
387 node->flags |= (u_int)val;
391 } else if (tok == ':') {
393 node->type = NODE_ENTRY;
394 TAILQ_INIT(&node->u.entry.subs);
397 node->u.entry.index = 0;
398 while ((tok = gettoken()) == TOK_TYPE) {
399 if (index_count++ == SNMP_INDEXES_MAX)
400 report("too many table indexes");
401 node->u.entry.index |=
402 val << (SNMP_INDEX_SHIFT * index_count);
404 node->u.entry.index |= index_count;
405 if (index_count == 0)
406 report("need at least one index");
409 report("function name expected");
411 node->u.entry.func = savetok();
417 TAILQ_INSERT_TAIL(&node->u.entry.subs, sub, link);
423 node->type = NODE_TREE;
424 TAILQ_INIT(&node->u.tree.subs);
428 TAILQ_INSERT_TAIL(&node->u.tree.subs, sub, link);
436 * Generate the C-code table part for one node.
439 gen_node(struct node *np, struct asn_oid *oid, u_int idx, const char *func)
445 if (oid->len == ASN_MAXOIDLEN)
446 report_node(np, "OID too long");
447 oid->subs[oid->len++] = np->id;
449 if (np->type == NODE_TREE) {
450 TAILQ_FOREACH(sub, &np->u.tree.subs, link)
451 gen_node(sub, oid, 0, NULL);
455 if (np->type == NODE_ENTRY) {
456 TAILQ_FOREACH(sub, &np->u.entry.subs, link)
457 gen_node(sub, oid, np->u.entry.index, np->u.entry.func);
463 if ((np->flags & (FL_GET|FL_SET)) == 0) {
468 fprintf(fp, " {{ %u, {", oid->len);
469 for (n = 0; n < oid->len; n++)
470 fprintf(fp, " %u,", oid->subs[n]);
471 fprintf(fp, " }}, \"%s\", ", np->name);
473 if (np->type == NODE_COLUMN) {
474 syntax = np->u.column.syntax;
475 fprintf(fp, "SNMP_NODE_COLUMN, ");
477 syntax = np->u.leaf.syntax;
478 fprintf(fp, "SNMP_NODE_LEAF, ");
483 case SNMP_SYNTAX_NULL:
484 fprintf(fp, "SNMP_SYNTAX_NULL, ");
487 case SNMP_SYNTAX_INTEGER:
488 fprintf(fp, "SNMP_SYNTAX_INTEGER, ");
491 case SNMP_SYNTAX_OCTETSTRING:
492 fprintf(fp, "SNMP_SYNTAX_OCTETSTRING, ");
495 case SNMP_SYNTAX_IPADDRESS:
496 fprintf(fp, "SNMP_SYNTAX_IPADDRESS, ");
499 case SNMP_SYNTAX_OID:
500 fprintf(fp, "SNMP_SYNTAX_OID, ");
503 case SNMP_SYNTAX_TIMETICKS:
504 fprintf(fp, "SNMP_SYNTAX_TIMETICKS, ");
507 case SNMP_SYNTAX_COUNTER:
508 fprintf(fp, "SNMP_SYNTAX_COUNTER, ");
511 case SNMP_SYNTAX_GAUGE:
512 fprintf(fp, "SNMP_SYNTAX_GAUGE, ");
515 case SNMP_SYNTAX_COUNTER64:
516 fprintf(fp, "SNMP_SYNTAX_COUNTER64, ");
519 case SNMP_SYNTAX_NOSUCHOBJECT:
520 case SNMP_SYNTAX_NOSUCHINSTANCE:
521 case SNMP_SYNTAX_ENDOFMIBVIEW:
525 if (np->type == NODE_COLUMN)
526 fprintf(fp, "%s, ", func);
528 fprintf(fp, "%s, ", np->u.leaf.func);
531 if (np->flags & FL_SET)
532 fprintf(fp, "|SNMP_NODE_CANSET");
533 fprintf(fp, ", %#x, NULL, NULL },\n", idx);
539 * Generate the header file with the function declarations.
542 gen_header(struct node *np, u_int oidlen, const char *func)
549 if (np->type == NODE_TREE) {
550 TAILQ_FOREACH(sub, &np->u.tree.subs, link)
551 gen_header(sub, oidlen, NULL);
554 if (np->type == NODE_ENTRY) {
555 TAILQ_FOREACH(sub, &np->u.entry.subs, link)
556 gen_header(sub, oidlen, np->u.entry.func);
560 if((np->flags & (FL_GET|FL_SET)) == 0)
563 if (np->type == NODE_COLUMN) {
565 errx(1, "column without function (%s) - probably "
566 "outside of a table", np->name);
567 sprintf(f, "%s", func);
569 sprintf(f, "%s", np->u.leaf.func);
571 LIST_FOREACH(ptr, &funcs, link)
572 if (strcmp(ptr->name, f) == 0)
576 ptr = xalloc(sizeof(*ptr));
577 ptr->name = strcpy(xalloc(strlen(f)+1), f);
578 LIST_INSERT_HEAD(&funcs, ptr, link);
580 fprintf(fp, "int %s(struct snmp_context *, "
581 "struct snmp_value *, u_int, u_int, "
582 "enum snmp_op);\n", f);
585 fprintf(fp, "# define LEAF_%s %u\n", np->name, np->id);
589 * Generate the OID table.
592 gen_table(struct node *node)
596 fprintf(fp, "#include <sys/types.h>\n");
597 fprintf(fp, "#include <stdio.h>\n");
598 fprintf(fp, "#include <stdint.h>\n");
600 fprintf(fp, "#include \"asn1.h\"\n");
601 fprintf(fp, "#include \"snmp.h\"\n");
602 fprintf(fp, "#include \"snmpagent.h\"\n");
604 fprintf(fp, "#include <bsnmp/asn1.h>\n");
605 fprintf(fp, "#include <bsnmp/snmp.h>\n");
606 fprintf(fp, "#include <bsnmp/snmpagent.h>\n");
608 fprintf(fp, "#include \"%stree.h\"\n", file_prefix);
611 fprintf(fp, "const struct snmp_node %sctree[] = {\n", file_prefix);
613 oid.len = PREFIX_LEN;
614 memcpy(oid.subs, prefix, sizeof(prefix));
615 gen_node(node, &oid, 0, NULL);
617 fprintf(fp, "};\n\n");
621 print_syntax(u_int syntax)
625 for (i = 0; keywords[i].str != NULL; i++)
626 if (keywords[i].tok == TOK_TYPE &&
627 keywords[i].val == syntax) {
628 printf(" %s", keywords[i].str);
635 * Generate a tree definition file
638 gen_tree(const struct node *np, int level)
640 const struct node *sp;
643 printf("%*s(%u %s", 2 * level, "", np->id, np->name);
648 print_syntax(np->u.leaf.syntax);
649 printf(" %s%s%s)\n", np->u.leaf.func,
650 (np->flags & FL_GET) ? " GET" : "",
651 (np->flags & FL_SET) ? " SET" : "");
655 if (TAILQ_EMPTY(&np->u.tree.subs)) {
659 TAILQ_FOREACH(sp, &np->u.tree.subs, link)
660 gen_tree(sp, level + 1);
661 printf("%*s)\n", 2 * level, "");
668 for (i = 0; i < SNMP_INDEX_COUNT(np->u.entry.index); i++)
669 print_syntax(SNMP_INDEX(np->u.entry.index, i));
670 printf(" %s\n", np->u.entry.func);
671 TAILQ_FOREACH(sp, &np->u.entry.subs, link)
672 gen_tree(sp, level + 1);
673 printf("%*s)\n", 2 * level, "");
677 print_syntax(np->u.column.syntax);
678 printf("%s%s)\n", (np->flags & FL_GET) ? " GET" : "",
679 (np->flags & FL_SET) ? " SET" : "");
686 extract(const struct node *np, struct asn_oid *oid, const char *obj,
687 const struct asn_oid *idx, const char *iname)
692 if (oid->len == ASN_MAXOIDLEN)
693 report_node(np, "OID too long");
694 oid->subs[oid->len++] = np->id;
696 if (strcmp(obj, np->name) == 0) {
697 if (oid->len + idx->len >= ASN_MAXOIDLEN)
698 report_node(np, "OID too long");
699 fprintf(fp, "#define OID_%s%s\t%u\n", np->name,
700 iname ? iname : "", np->id);
701 fprintf(fp, "#define OIDLEN_%s%s\t%u\n", np->name,
702 iname ? iname : "", oid->len + idx->len);
703 fprintf(fp, "#define OIDX_%s%s\t{ %u, {", np->name,
704 iname ? iname : "", oid->len + idx->len);
705 for (n = 0; n < oid->len; n++)
706 fprintf(fp, " %u,", oid->subs[n]);
707 for (n = 0; n < idx->len; n++)
708 fprintf(fp, " %u,", idx->subs[n]);
709 fprintf(fp, " } }\n");
713 if (np->type == NODE_TREE) {
714 TAILQ_FOREACH(sub, &np->u.tree.subs, link)
715 if (!extract(sub, oid, obj, idx, iname))
717 } else if (np->type == NODE_ENTRY) {
718 TAILQ_FOREACH(sub, &np->u.entry.subs, link)
719 if (!extract(sub, oid, obj, idx, iname))
727 gen_extract(const struct node *root, char *object)
731 char *s, *e, *end, *iname;
735 /* look whether the object to extract has an index part */
738 s = strchr(object, '.');
740 iname = malloc(strlen(s) + 1);
742 err(1, "cannot allocated index");
745 for (e = iname; *e != '\0'; e++)
752 errx(1, "bad index syntax");
753 if ((e = strchr(s, '.')) != NULL)
757 ul = strtoul(s, &end, 0);
759 errx(1, "bad index syntax '%s'", end);
761 err(1, "bad index syntax");
763 if (idx.len == ASN_MAXOIDLEN)
764 errx(1, "index oid too large");
765 idx.subs[idx.len++] = ul;
771 oid.len = PREFIX_LEN;
772 memcpy(oid.subs, prefix, sizeof(prefix));
773 ret = extract(root, &oid, object, &idx, iname);
782 check_sub_order(const struct node *np, const struct node_list *subs)
785 const struct node *sub;
786 asn_subid_t maxid = 0;
788 /* ensure, that subids are ordered */
790 TAILQ_FOREACH(sub, subs, link) {
791 if (!first && sub->id <= maxid)
792 report_node(np, "subids not ordered at %s", sub->name);
799 * Do some sanity checks on the tree definition and do some computations.
802 check_tree(struct node *np)
806 if (np->type == NODE_LEAF || np->type == NODE_COLUMN) {
807 if ((np->flags & (FL_GET|FL_SET)) != 0)
812 if (np->type == NODE_ENTRY) {
813 check_sub_order(np, &np->u.entry.subs);
815 /* ensure all subnodes are columns */
816 TAILQ_FOREACH(sub, &np->u.entry.subs, link) {
817 if (sub->type != NODE_COLUMN)
818 report_node(np, "entry subnode '%s' is not "
819 "a column", sub->name);
823 check_sub_order(np, &np->u.tree.subs);
825 TAILQ_FOREACH(sub, &np->u.tree.subs, link)
831 merge_subs(struct node_list *s1, struct node_list *s2)
833 struct node *n1, *n2;
835 while (!TAILQ_EMPTY(s2)) {
836 n2 = TAILQ_FIRST(s2);
837 TAILQ_REMOVE(s2, n2, link);
839 TAILQ_FOREACH(n1, s1, link)
840 if (n1->id >= n2->id)
843 TAILQ_INSERT_TAIL(s1, n2, link);
844 else if (n1->id > n2->id)
845 TAILQ_INSERT_BEFORE(n1, n2, link);
847 if (n1->type == NODE_TREE && n2->type == NODE_TREE) {
848 if (strcmp(n1->name, n2->name) != 0)
849 errx(1, "trees to merge must have "
850 "same name '%s' '%s'", n1->name,
852 merge_subs(&n1->u.tree.subs, &n2->u.tree.subs);
854 } else if (n1->type == NODE_ENTRY &&
855 n2->type == NODE_ENTRY) {
856 if (strcmp(n1->name, n2->name) != 0)
857 errx(1, "entries to merge must have "
858 "same name '%s' '%s'", n1->name,
860 if (n1->u.entry.index != n2->u.entry.index)
861 errx(1, "entries to merge must have "
862 "same index '%s'", n1->name);
863 if (strcmp(n1->u.entry.func,
864 n2->u.entry.func) != 0)
865 errx(1, "entries to merge must have "
866 "same op '%s'", n1->name);
867 merge_subs(&n1->u.entry.subs,
871 errx(1, "entities to merge must be both "
872 "trees or both entries: %s, %s",
879 merge(struct node *root, struct node *t)
882 /* both must be trees */
883 if (root->type != NODE_TREE)
884 errx(1, "root is not a tree");
885 if (t->type != NODE_TREE)
886 errx(1, "can merge only with tree");
887 if (root->id != t->id)
888 errx(1, "trees to merge must have same id");
890 merge_subs(&root->u.tree.subs, &t->u.tree.subs);
894 main(int argc, char *argv[])
900 char fname[MAXPATHLEN + 1];
903 while ((opt = getopt(argc, argv, "help:t")) != EOF)
907 fprintf(stderr, "%s", usgtxt);
919 file_prefix = optarg;
920 if (strlen(file_prefix) + strlen("tree.c") >
922 errx(1, "prefix too long");
930 if (do_extract && do_tree)
931 errx(1, "conflicting options -e and -t");
932 if (!do_extract && argc != optind)
933 errx(1, "no arguments allowed");
934 if (do_extract && argc == optind)
935 errx(1, "no objects specified");
937 root = parse(gettoken());
938 while ((tok = gettoken()) != TOK_EOF)
939 merge(root, parse(tok));
945 while (optind < argc) {
946 if (gen_extract(root, argv[optind]))
947 errx(1, "object not found: %s", argv[optind]);
957 sprintf(fname, "%stree.h", file_prefix);
958 if ((fp = fopen(fname, "w")) == NULL)
959 err(1, "%s: ", fname);
960 gen_header(root, PREFIX_LEN, NULL);
962 fprintf(fp, "#define %sCTREE_SIZE %u\n", file_prefix, tree_size);
963 fprintf(fp, "extern const struct snmp_node %sctree[];\n", file_prefix);
967 sprintf(fname, "%stree.c", file_prefix);
968 if ((fp = fopen(fname, "w")) == NULL)
969 err(1, "%s: ", fname);