Deleted Added
full compact
makemib (17680) makemib (56893)
1#!/bin/sh
2#
3# Copyright (c) 1990, 1996, by John Robert LoVerso.
4# All rights reserved.
1#!/bin/sh
2#
3# Copyright (c) 1990, 1996, by John Robert LoVerso.
4# All rights reserved.
5# SMIv2 parsing copyright (c) 1999 by William C. Fenner.
5#
6# Redistribution and use in source and binary forms are permitted
7# provided that the above copyright notice and this paragraph are
8# duplicated in all such forms and that any documentation,
9# advertising materials, and other materials related to such
10# distribution and use acknowledge that the software was developed
11# by John Robert LoVerso.
12# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
13# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
14# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15#
6#
7# Redistribution and use in source and binary forms are permitted
8# provided that the above copyright notice and this paragraph are
9# duplicated in all such forms and that any documentation,
10# advertising materials, and other materials related to such
11# distribution and use acknowledge that the software was developed
12# by John Robert LoVerso.
13# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16#
16# @(#) $Id: makemib,v 2.1 90/07/10 23:51:54 loverso Exp Locker: loverso $ (jlv
17)
17# @(#) $Id: makemib,v 1.2 1999/11/21 17:24:15 fenner Exp $ (jlv)
18
19#
20# This script will read either ASN.1-style MIB files or the ".defs" files
21# created by the ISODE "mosy" program on such files.
22#
23# The output of this script is the "mib.h" file used by tcpdumps' ASN.1/SNMP
24# decoding code.
25#

--- 11 unchanged lines hidden (view full) ---

37 * You probably don't want to edit this by hand!
38 *
39 * struct mib somename = { desc, oid-octet, type, child-pointer, next-pointer
40};
41 */
42
43EOF
44
18
19#
20# This script will read either ASN.1-style MIB files or the ".defs" files
21# created by the ISODE "mosy" program on such files.
22#
23# The output of this script is the "mib.h" file used by tcpdumps' ASN.1/SNMP
24# decoding code.
25#

--- 11 unchanged lines hidden (view full) ---

37 * You probably don't want to edit this by hand!
38 *
39 * struct mib somename = { desc, oid-octet, type, child-pointer, next-pointer
40};
41 */
42
43EOF
44
45# use sed to make the ASN.1 easier to parse
46# I should really just use a recursive descent parser in awk, but...
47sed \
48 -e 's/--\*.*\*--//' \
49 -e 's/--.*//' \
50 -e 's/\([{}]\)/ \1 /g' \
51 $@ \
52| gawk '
45awk '
53BEGIN {
46BEGIN {
47 debug=0;
54 # for sanity, we prep the namespace with objects from RFC-1155
55 # (we manually establish the root)
56 oid["iso"]=1
57 oidadd("org", "iso", 3)
58 oidadd("dod", "org", 6)
59 oidadd("internet", "dod", 1)
60 oidadd("directory", "internet", 1)
61 oidadd("mgmt", "internet", 2)
48 # for sanity, we prep the namespace with objects from RFC-1155
49 # (we manually establish the root)
50 oid["iso"]=1
51 oidadd("org", "iso", 3)
52 oidadd("dod", "org", 6)
53 oidadd("internet", "dod", 1)
54 oidadd("directory", "internet", 1)
55 oidadd("mgmt", "internet", 2)
62 oidadd("mib", "mgmt", 1)
56#XXX oidadd("mib", "mgmt", 1)
57 oidadd("mib-2", "mgmt", 1)
63 oidadd("experimental", "internet", 3)
64 oidadd("private", "internet", 4)
65 oidadd("enterprises", "private", 1)
58 oidadd("experimental", "internet", 3)
59 oidadd("private", "internet", 4)
60 oidadd("enterprises", "private", 1)
61 oidadd("ip", "mib-2", 4)
62 oidadd("transmission", "mib-2", 10)
66
67 holddesc="none"
68}
69
70#
71# Read mosy "*.defs" file. mosy does all the parsing work; we just read
72# its simple and straightforward output. It would not be too hard to make
73# tcpdump directly read mosy output, but...
74#
63
64 holddesc="none"
65}
66
67#
68# Read mosy "*.defs" file. mosy does all the parsing work; we just read
69# its simple and straightforward output. It would not be too hard to make
70# tcpdump directly read mosy output, but...
71#
72# Ignore these unless the current file is called something.defs; false
73# positives are too common in DESCRIPTIONs.
75
74
76NF > 1 && index($2,".")>0 {
75NF > 1 && index($2,".")>0 && FILENAME ~ /\.defs/ {
77 # currently ignore items of the form "{ iso.3.6.1 }"
76 # currently ignore items of the form "{ iso.3.6.1 }"
78 if (split($2, p, ".") == 2)
77 if (split($2, p, ".") == 2) {
79 oidadd($1, p[1], p[2])
78 oidadd($1, p[1], p[2])
79 }
80 next
81}
82
83#
80 next
81}
82
83#
84# this next section is simple and naive, but does the job 100%
84# Must be a MIB file
85# Make it easier to parse - used to be done by sed
86{ sub(/--\*.*\*--/, ""); sub(/--.*/, ""); gsub(/[{}]/, " & "); }
87
85#
88#
89# this next section is simple and naive, but does the job ok
90#
86
91
92# foo OBJECT IDENTIFIER ::= { baz 17 }
93# or
94# foo OBJECT IDENTIFIER ::=
95# { baz 17 }
87$2$3$4 == "OBJECTIDENTIFIER::=" {
88 holddesc="none"
89 if (NF == 8)
90 oidadd($1, $6, $7)
96$2$3$4 == "OBJECTIDENTIFIER::=" {
97 holddesc="none"
98 if (NF == 8)
99 oidadd($1, $6, $7)
100 if (NF == 4)
101 holddesc=$1
102 next
91}
103}
92$2 == "OBJECT-TYPE" {
104$1 == "{" && holddesc != "none" && NF == 4 {
105 oidadd(holddesc, $2, $3)
106 holddesc="none"
107}
108#
109# foo OBJECT IDENTIFIER
110# ::= { bar 1 }
111$2$3 == "OBJECTIDENTIFIER" && $1 != "SYNTAX" && NF == 3 {
93 holddesc=$1
94}
112 holddesc=$1
113}
114#
115# foo
116# OBJECT IDENTIFIER ::= { bar 1 }
117# a couple of heuristics to exclude single words in e.g. long
118# DESCRIPTION clauses
119NF == 1 && $1 ~ "[a-z][a-z]*[A-Z]" && $1 !~ /[(){}.,]/ && holddesc == "none" {
120 holddesc=$1
121}
122$1$2$3 == "OBJECTIDENTIFIER::=" && holddesc != "none" {
123 oidadd(holddesc, $5, $6)
124 holddesc="none"
125}
126#
127# "normal" style
128# foo OBJECT-TYPE ...
129# ...
130# ::= { baz 5 }
131$2 == "MODULE-IDENTITY" || $2 == "MODULE-COMPLIANCE" ||
132 $2 == "OBJECT-IDENTITY" || $2 == "OBJECT-TYPE" ||
133 $2 == "OBJECT-GROUP" ||
134 $2 == "NOTIFICATION-TYPE" || $2 == "NOTIFICATION-GROUP" {
135 holddesc=$1
136}
95$1 == "::=" && holddesc != "none" && NF == 5 {
96 oidadd(holddesc, $3, $4)
97 holddesc="none"
98}
137$1 == "::=" && holddesc != "none" && NF == 5 {
138 oidadd(holddesc, $3, $4)
139 holddesc="none"
140}
141#
142# foo ::= { baz 17 }
143$2$3 == "::={" {
144 oidadd($1,$4,$5)
145 holddesc="none"
146}
99
147
148
100#
101# End of the road - output the data.
102#
103
104END {
105 print "struct obj"
106 dump("iso")
107 print "*mibroot = &_iso_obj;"
108}
109
149#
150# End of the road - output the data.
151#
152
153END {
154 print "struct obj"
155 dump("iso")
156 print "*mibroot = &_iso_obj;"
157}
158
159function inn(file) {
160 if (file == "" || file == "-")
161 return ""
162 return " in " file
163}
164
110#
111# add a new object to the tree
112#
113# new OBJECT IDENTIFIER ::= { parent value }
114#
115
116function oidadd(new, parent, value) {
165#
166# add a new object to the tree
167#
168# new OBJECT IDENTIFIER ::= { parent value }
169#
170
171function oidadd(new, parent, value) {
172 # Ignore 0.0
173 if (parent == "0" && value == 0)
174 return
175 if (debug)
176 print "/* oidadd" inn(FILENAME) ":", new, "in", parent, "as", value, "line", $0, "*/"
117 # use safe C identifiers
118 gsub(/[-&\/]/,"",new)
119 gsub(/[-&\/]/,"",parent)
120 # check if parent missing
177 # use safe C identifiers
178 gsub(/[-&\/]/,"",new)
179 gsub(/[-&\/]/,"",parent)
180 # check if parent missing
121 if (oid[parent] == 0) {
122 printf "/* parse problem: no parent for %s.%s(%d) */\n", \
123 parent, new, value
181 if (oid[parent] == "") {
182 printf "/* parse problem%s: no parent for %s.%s(%d) */\n", \
183 inn(FILENAME), parent, new, value
124 return
125 }
126 # check if parent.value already exists
127 if (oid[new] > 0 && oid[new] != value) {
184 return
185 }
186 # check if parent.value already exists
187 if (oid[new] > 0 && oid[new] != value) {
128 printf "/* parse problem: dup %s.%s(%d) != old (%d) */\n", \
129 parent, new, value, oid[new]
188 printf "/* parse problem%s: dup %s.%s(%d) != old (%d) */\n", \
189 inn(FILENAME), parent, new, value, oid[new]
130 return
131 }
132 # check for new name for parent.value
133 if (child[parent] != "") {
134 for (sib = child[parent]; sib != ""; sib = sibling[sib])
135 if (oid[sib] == value) {
190 return
191 }
192 # check for new name for parent.value
193 if (child[parent] != "") {
194 for (sib = child[parent]; sib != ""; sib = sibling[sib])
195 if (oid[sib] == value) {
136 printf "/* parse problem: new name \"%s\"" \
137 " for %s.%s(%d) ignored */\n", \
138 new, parent, sib, value
196 if (new != sib)
197 printf "/* parse problem%s: new name" \
198 " \"%s\"" \
199 " for %s.%s(%d) ignored */\n", \
200 inn(FILENAME), new, parent, \
201 sib, value
139 return
140 }
141 }
142
143 oid[new]=value
144 if (child[parent] == "") {
145 child[parent] = new
146 } else {

--- 17 unchanged lines hidden (view full) ---

164 s="NULL"
165 if (sibling[item] != "") {
166 dump(sibling[item])
167 s = "&_"sibling[item]"_obj"
168 }
169 printf "_%s_obj = {\n\t\"%s\", %d, 0,\n\t%s, %s\n},\n", \
170 item, item, oid[item], c, s
171}
202 return
203 }
204 }
205
206 oid[new]=value
207 if (child[parent] == "") {
208 child[parent] = new
209 } else {

--- 17 unchanged lines hidden (view full) ---

227 s="NULL"
228 if (sibling[item] != "") {
229 dump(sibling[item])
230 s = "&_"sibling[item]"_obj"
231 }
232 printf "_%s_obj = {\n\t\"%s\", %d, 0,\n\t%s, %s\n},\n", \
233 item, item, oid[item], c, s
234}
172'
235' $@
173exit 0
236exit 0