summaryrefslogtreecommitdiffstats
path: root/contrib/tcpdump/makemib
blob: 3ab2d7d0d5c4652b34600b52b780f7c76a87096c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/sh
#
# Copyright (c) 1990, 1996, by John Robert LoVerso.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that the above copyright notice and this paragraph are
# duplicated in all such forms and that any documentation,
# advertising materials, and other materials related to such
# distribution and use acknowledge that the software was developed
# by John Robert LoVerso.
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# @(#) $Id: makemib,v 2.1 90/07/10 23:51:54 loverso Exp Locker: loverso $ (jlv
)

#
# This script will read either ASN.1-style MIB files or the ".defs" files
# created by the ISODE "mosy" program on such files.
#
# The output of this script is the "mib.h" file used by tcpdumps' ASN.1/SNMP
# decoding code.
#
# This script needs to be run by "gawk" (GNU awk).  "nawk" will work, but
# dump will get a recursion error if you process LARGE mibs.  While it would
# by farily easy to rewrite this not to use recursion (and also easy to
# eliminate use of gsub and functions to use classic "awk"), you have to 
# order the structure declarations in defined-first order for the compiler
# not to barf; too bad tsort doesn't take arguments.
#

cat << EOF
/*
 * This file was generated by tcpdump/makemib on `date`
 * You probably don't want to edit this by hand!
 *
 * struct mib somename = { desc, oid-octet, type, child-pointer, next-pointer 
};
 */

EOF

# use sed to make the ASN.1 easier to parse
# I should really just use a recursive descent parser in awk, but...
sed \
	-e 's/--\*.*\*--//' \
	-e 's/--.*//' \
	-e 's/\([{}]\)/ \1 /g' \
	$@ \
| gawk '
BEGIN {
	# for sanity, we prep the namespace with objects from RFC-1155
	# (we manually establish the root)
	oid["iso"]=1
	oidadd("org", "iso", 3)
	oidadd("dod", "org", 6)
	oidadd("internet", "dod", 1)
	oidadd("directory", "internet", 1)
	oidadd("mgmt", "internet", 2)
	oidadd("mib", "mgmt", 1)
	oidadd("experimental", "internet", 3)
	oidadd("private", "internet", 4)
	oidadd("enterprises", "private", 1)

	holddesc="none"
}

#
# Read mosy "*.defs" file.  mosy does all the parsing work; we just read
# its simple and straightforward output.  It would not be too hard to make
# tcpdump directly read mosy output, but...
#

NF > 1 && index($2,".")>0 {
	# currently ignore items of the form "{ iso.3.6.1 }"
	if (split($2, p, ".") == 2)
		oidadd($1, p[1], p[2])
	next
}

#
# this next section is simple and naive, but does the job 100%
#

$2$3$4 == "OBJECTIDENTIFIER::=" {
	holddesc="none"
	if (NF == 8)
		oidadd($1, $6, $7)
}
$2 == "OBJECT-TYPE" {
	holddesc=$1
}
$1 == "::=" && holddesc != "none" && NF == 5 {
	oidadd(holddesc, $3, $4)
	holddesc="none"
}

# 
# End of the road - output the data.
#

END {
	print "struct obj"
	dump("iso")
	print "*mibroot = &_iso_obj;"
}

#
# add a new object to the tree
#
#		new OBJECT IDENTIFIER ::= { parent value }
#

function oidadd(new, parent, value) {
	# use safe C identifiers
	gsub(/[-&\/]/,"",new)
	gsub(/[-&\/]/,"",parent)
	# check if parent missing
	if (oid[parent] == 0) {
		printf "/* parse problem: no parent for %s.%s(%d) */\n", \
			parent, new, value
		return
	}
	# check if parent.value already exists
	if (oid[new] > 0 && oid[new] != value) {
		printf "/* parse problem: dup %s.%s(%d) != old (%d) */\n", \
			parent, new, value, oid[new]
		return
	}
	# check for new name for parent.value
	if (child[parent] != "") {
		for (sib = child[parent]; sib != ""; sib = sibling[sib])
			if (oid[sib] == value) {
				printf "/* parse problem: new name \"%s\"" \
					" for %s.%s(%d) ignored */\n", \
						new, parent, sib, value
				return
			}
	}

	oid[new]=value
	if (child[parent] == "") {
		child[parent] = new
	} else {
		sibling[new] = child[parent]
		child[parent] = new
	}
}

#
# old(?) routine to recurse down the tree (in postfix order for convenience)
#

function dump(item, c, s) {
#	newitem=sofar"."item"("oid[item]")"
#	printf "/* %s c=%s s=%s */\n", newitem, child[item], sibling[item]
	c="NULL"
	if (child[item] != "") {
		dump(child[item])
		c = "&_"child[item]"_obj"
	}
	s="NULL"
	if (sibling[item] != "") {
		dump(sibling[item])
		s = "&_"sibling[item]"_obj"
	}
	printf "_%s_obj = {\n\t\"%s\", %d, 0,\n\t%s, %s\n},\n", \
		item, item, oid[item], c, s
}
'
exit 0
OpenPOWER on IntegriCloud