home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume40 / nocol / part08 < prev    next >
Encoding:
Text File  |  1993-11-22  |  75.0 KB  |  2,469 lines

  1. Newsgroups: comp.sources.misc
  2. From: vikas@jvnc.net (Vikas Aggarwal)
  3. Subject: v40i138:  nocol - Network Monitoring System, Part08/26
  4. Message-ID: <1993Nov23.034816.5769@sparky.sterling.com>
  5. X-Md4-Signature: 6f081f53a48fd4176c41b3a52ea5f816
  6. Sender: kent@sparky.sterling.com (Kent Landfield)
  7. Organization: Sterling Software
  8. Date: Tue, 23 Nov 1993 03:48:16 GMT
  9. Approved: kent@sparky.sterling.com
  10.  
  11. Submitted-by: vikas@jvnc.net (Vikas Aggarwal)
  12. Posting-number: Volume 40, Issue 138
  13. Archive-name: nocol/part08
  14. Environment: INET, UNIX
  15.  
  16. #! /bin/sh
  17. # This is a shell archive.  Remove anything before this line, then feed it
  18. # into a shell via "sh file" or similar.  To overwrite existing files,
  19. # type "sh file -c".
  20. # Contents:  nocol-3.0/src/cmu-snmp/apps/snmp_vars.c
  21. #   nocol-3.0/src/cmu-snmp/include/parse.c
  22. #   nocol-3.0/src/cmu-snmp/snmplib/snmp.h
  23. # Wrapped by kent@sparky on Tue Nov  9 22:22:16 1993
  24. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin:$PATH ; export PATH
  25. echo If this archive is complete, you will see the following message:
  26. echo '          "shar: End of archive 8 (of 26)."'
  27. if test -f 'nocol-3.0/src/cmu-snmp/apps/snmp_vars.c' -a "${1}" != "-c" ; then 
  28.   echo shar: Will not clobber existing file \"'nocol-3.0/src/cmu-snmp/apps/snmp_vars.c'\"
  29. else
  30.   echo shar: Extracting \"'nocol-3.0/src/cmu-snmp/apps/snmp_vars.c'\" \(46704 characters\)
  31.   sed "s/^X//" >'nocol-3.0/src/cmu-snmp/apps/snmp_vars.c' <<'END_OF_FILE'
  32. X/*
  33. X * snmp_vars.c - return a pointer to the named variable.
  34. X *
  35. X *
  36. X */
  37. X/***********************************************************
  38. X    Copyright 1988, 1989, 1990 by Carnegie Mellon University
  39. X    Copyright 1989    TGV, Incorporated
  40. X
  41. X              All Rights Reserved
  42. X
  43. XPermission to use, copy, modify, and distribute this software and its
  44. Xdocumentation for any purpose and without fee is hereby granted,
  45. Xprovided that the above copyright notice appear in all copies and that
  46. Xboth that copyright notice and this permission notice appear in
  47. Xsupporting documentation, and that the name of CMU and TGV not be used
  48. Xin advertising or publicity pertaining to distribution of the software
  49. Xwithout specific, written prior permission.
  50. X
  51. XCMU AND TGV DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  52. XINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  53. XEVENT SHALL CMU OR TGV BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  54. XCONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  55. XUSE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  56. XOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  57. XPERFORMANCE OF THIS SOFTWARE.
  58. X******************************************************************/
  59. X
  60. X#define USE_NAME_AS_DESCRIPTION /*"se0" instead of text */
  61. X#define GATEWAY            /* MultiNet is always configured this way! */
  62. X#include <sys/types.h>
  63. X#include <sys/socket.h>
  64. X/* #include <sys/time.h> */
  65. X#include <sys/param.h>
  66. X#include <sys/dir.h>
  67. X#include <sys/user.h>
  68. X#include <sys/proc.h>
  69. X#include <machine/pte.h>
  70. X#include <sys/vm.h>
  71. X#include <netinet/in.h>
  72. X#include <syslog.h>
  73. X#include <sys/ioctl.h>
  74. X#include <net/if.h>
  75. X#include <net/route.h>
  76. X#include <netinet/in_pcb.h>
  77. X#include <netinet/if_ether.h>
  78. X#include <netinet/in_systm.h>
  79. X#include <netinet/in_var.h>
  80. X#include <netinet/ip.h>
  81. X#include <netinet/ip_var.h>
  82. X#include <netinet/tcp.h>
  83. X#include <netinet/tcp_timer.h>
  84. X#include <netinet/tcp_var.h>
  85. X#include <netinet/tcp_fsm.h>
  86. X#include <netinet/udp.h>
  87. X#include <netinet/udp_var.h>
  88. X#include <netinet/ip_icmp.h>
  89. X#include <netinet/icmp_var.h>
  90. X#include <nlist.h>
  91. X#include <sys/protosw.h>
  92. X#ifndef NULL
  93. X#define NULL 0
  94. X#endif
  95. X
  96. X#include "asn1.h"
  97. X#include "snmp.h"
  98. X#include "snmp_impl.h"
  99. X#include "mib.h"
  100. X#include "snmp_vars.h"
  101. X
  102. X
  103. X
  104. X#ifdef vax11c
  105. X#define ioctl socket_ioctl
  106. X#define perror socket_perror
  107. X#endif vax11c
  108. X
  109. Xextern char *Lookup_Device_Annotation();
  110. X
  111. Xstatic struct nlist nl[] = {
  112. X#define N_IPSTAT    0
  113. X    { "_ipstat" },
  114. X#define N_IPFORWARDING    1
  115. X    { "_ipforwarding" },
  116. X#define N_TCP_TTL    2
  117. X    { "_tcp_ttl" },
  118. X#define N_UDPSTAT    3
  119. X    { "_udpstat" },
  120. X#define N_IN_INTERFACES 4
  121. X    { "_in_interfaces" },
  122. X#define N_ICMPSTAT    5
  123. X    { "_icmpstat" },
  124. X#define N_IFNET        6
  125. X    { "_ifnet" },
  126. X#define N_TCPSTAT    7
  127. X    { "_tcpstat" },
  128. X#define N_TCB        8
  129. X    { "_tcb" },
  130. X#define N_ARPTAB_SIZE    9
  131. X    { "_arptab_size" },
  132. X#define N_ARPTAB       10
  133. X    { "_arptab" },
  134. X#define N_IN_IFADDR    11
  135. X    { "_in_ifaddr" },
  136. X#define N_BOOTTIME    12
  137. X    { "_boottime" },
  138. X#ifdef ibm032
  139. X#define N_PROC        13
  140. X    { "_proc" },
  141. X#define N_NPROC        14
  142. X    { "_nproc" },
  143. X#define N_DMMIN        15
  144. X    { "_dmmin" },
  145. X#define N_DMMAX        16
  146. X    { "_dmmax" },
  147. X#define N_NSWAP        17
  148. X    { "_nswap" },
  149. X#define N_USRPTMAP    18
  150. X    { "_Usrptmap" },
  151. X#define N_USRPT        19
  152. X    { "_usrpt" },
  153. X#endif
  154. X#ifdef ibm032
  155. X#define N_USERSIZE    20
  156. X    { "_userSIZE" },
  157. X#endif
  158. X    0,
  159. X};
  160. X
  161. X/*
  162. X *    Each variable name is placed in the variable table, without the terminating
  163. X * substring that determines the instance of the variable.  When a string is found that
  164. X * is lexicographicly preceded by the input string, the function for that entry is
  165. X * called to find the method of access of the instance of the named variable.  If
  166. X * that variable is not found, NULL is returned, and the search through the table
  167. X * continues (it should stop at the next entry).  If it is found, the function returns
  168. X * a character pointer and a length or a function pointer.  The former is the address
  169. X * of the operand, the latter is a write routine for the variable.
  170. X *
  171. X * u_char *
  172. X * findVar(name, length, exact, var_len, write_method)
  173. X * oid        *name;        IN/OUT - input name requested, output name found
  174. X * int        length;        IN/OUT - number of sub-ids in the in and out oid's
  175. X * int        exact;        IN - TRUE if an exact match was requested.
  176. X * int        len;        OUT - length of variable or 0 if function returned.
  177. X * int        write_method;   OUT - 1 if function, 0 if char pointer.
  178. X *
  179. X * writeVar(doSet, var_val, var_val_type, var_val_len, statP)
  180. X * int        doSet;        IN - 0 if only check of validity of operation
  181. X * u_char   *var_val;        IN - input or output buffer space
  182. X * u_char   var_val_type;   IN - type of input buffer
  183. X * int        var_val_len;    IN - input and output buffer len
  184. X * u_char   *statP;        IN - pointer to local statistic
  185. X */
  186. X
  187. Xlong        long_return;
  188. Xu_char        return_buf[256]; /* nee 64 */
  189. X
  190. Xinit_snmp()
  191. X{
  192. X    nlist("/vmunix",nl);
  193. X    init_kmem("/dev/kmem");
  194. X    init_routes();
  195. X
  196. X}
  197. X
  198. Xstruct variable     variables[] = {
  199. X    /* these must be lexicographly ordered by the name field */
  200. X    {{MIB, 1, 1, 0},        9, STRING,  VERSION_DESCR, RWRITE, var_system },
  201. X    {{MIB, 1, 2, 0},        9, OBJID,   VERSION_ID, RONLY, var_system },
  202. X    {{MIB, 1, 3, 0},        9, TIMETICKS, UPTIME, RONLY, var_system },
  203. X    {{MIB, 2, 1, 0},        9, INTEGER, IFNUMBER, RONLY, var_system },
  204. X    {{MIB, 2, 2, 1, 1, 0xFF},  11, INTEGER, IFINDEX, RONLY, var_ifEntry },
  205. X    {{MIB, 2, 2, 1, 2, 0xFF},  11, STRING,  IFDESCR, RONLY, var_ifEntry },
  206. X    {{MIB, 2, 2, 1, 3, 0xFF},  11, INTEGER, IFTYPE, RONLY, var_ifEntry },
  207. X    {{MIB, 2, 2, 1, 4, 0xFF},  11, INTEGER, IFMTU, RONLY, var_ifEntry },
  208. X    {{MIB, 2, 2, 1, 5, 0xFF},  11, GAUGE,   IFSPEED, RONLY, var_ifEntry },
  209. X    {{MIB, 2, 2, 1, 6, 0xFF},  11, STRING,  IFPHYSADDRESS, RONLY, var_ifEntry },
  210. X    {{MIB, 2, 2, 1, 7, 0xFF},  11, INTEGER, IFADMINSTATUS, RWRITE, var_ifEntry },
  211. X    {{MIB, 2, 2, 1, 8, 0xFF},  11, INTEGER, IFOPERSTATUS, RONLY, var_ifEntry },
  212. X    {{MIB, 2, 2, 1, 9, 0xFF},  11, TIMETICKS, IFLASTCHANGE, RONLY, var_ifEntry },
  213. X    {{MIB, 2, 2, 1, 10, 0xFF}, 11, COUNTER, IFINOCTETS, RONLY, var_ifEntry },
  214. X    {{MIB, 2, 2, 1, 11, 0xFF}, 11, COUNTER, IFINUCASTPKTS, RONLY, var_ifEntry },
  215. X    {{MIB, 2, 2, 1, 12, 0xFF}, 11, COUNTER, IFINNUCASTPKTS, RONLY, var_ifEntry },
  216. X    {{MIB, 2, 2, 1, 13, 0xFF}, 11, COUNTER, IFINDISCARDS, RONLY, var_ifEntry },
  217. X    {{MIB, 2, 2, 1, 14, 0xFF}, 11, COUNTER, IFINERRORS, RONLY, var_ifEntry },
  218. X    {{MIB, 2, 2, 1, 15, 0xFF}, 11, COUNTER, IFINUNKNOWNPROTOS, RONLY, var_ifEntry },
  219. X    {{MIB, 2, 2, 1, 16, 0xFF}, 11, COUNTER, IFOUTOCTETS, RONLY, var_ifEntry },
  220. X    {{MIB, 2, 2, 1, 17, 0xFF}, 11, COUNTER, IFOUTUCASTPKTS, RONLY, var_ifEntry },
  221. X    {{MIB, 2, 2, 1, 18, 0xFF}, 11, COUNTER, IFOUTNUCASTPKTS, RONLY, var_ifEntry },
  222. X    {{MIB, 2, 2, 1, 19, 0xFF}, 11, COUNTER, IFOUTDISCARDS, RONLY, var_ifEntry },
  223. X    {{MIB, 2, 2, 1, 20, 0xFF}, 11, COUNTER, IFOUTERRORS, RONLY, var_ifEntry },
  224. X    {{MIB, 2, 2, 1, 21, 0xFF}, 11, GAUGE,   IFOUTQLEN, RONLY, var_ifEntry },
  225. X    {{MIB, 3, 1, 1, 1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 16, INTEGER,    ATIFINDEX, RONLY, var_atEntry },
  226. X    {{MIB, 3, 1, 1, 2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 16, STRING,     ATPHYSADDRESS, RONLY, var_atEntry },
  227. X    {{MIB, 3, 1, 1, 3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 16, IPADDRESS,  ATNETADDRESS, RONLY, var_atEntry },
  228. X    {{MIB, 4, 1, 0},        9, INTEGER, IPFORWARDING, RONLY, var_ip },
  229. X    {{MIB, 4, 2, 0},        9, INTEGER, IPDEFAULTTTL, RONLY, var_ip },
  230. X    {{MIB, 4, 3, 0},        9, COUNTER, IPINRECEIVES, RONLY, var_ip },
  231. X    {{MIB, 4, 4, 0},        9, COUNTER, IPINHDRERRORS, RONLY, var_ip },
  232. X    {{MIB, 4, 5, 0},        9, COUNTER, IPINADDRERRORS, RONLY, var_ip },
  233. X    {{MIB, 4, 6, 0},        9, COUNTER, IPFORWDATAGRAMS, RONLY, var_ip },
  234. X    {{MIB, 4, 7, 0},        9, COUNTER, IPINUNKNOWNPROTOS, RONLY, var_ip },
  235. X    {{MIB, 4, 8, 0},        9, COUNTER, IPINDISCARDS, RONLY, var_ip },
  236. X    {{MIB, 4, 9, 0},        9, COUNTER, IPINDELIVERS, RONLY, var_ip },
  237. X    {{MIB, 4, 10, 0},        9, COUNTER, IPOUTREQUESTS, RONLY, var_ip },
  238. X    {{MIB, 4, 11, 0},        9, COUNTER, IPOUTDISCARDS, RONLY, var_ip },
  239. X    {{MIB, 4, 12, 0},        9, COUNTER, IPOUTNOROUTES, RONLY, var_ip },
  240. X    {{MIB, 4, 13, 0},        9, INTEGER, IPREASMTIMEOUT, RONLY, var_ip },
  241. X    {{MIB, 4, 14, 0},        9, COUNTER, IPREASMREQDS, RONLY, var_ip },
  242. X    {{MIB, 4, 15, 0},        9, COUNTER, IPREASMOKS, RONLY, var_ip },
  243. X    {{MIB, 4, 16, 0},        9, COUNTER, IPREASMFAILS, RONLY, var_ip },
  244. X    {{MIB, 4, 17, 0},        9, COUNTER, IPFRAGOKS, RONLY, var_ip },
  245. X    {{MIB, 4, 18, 0},        9, COUNTER, IPFRAGFAILS, RONLY, var_ip },
  246. X    {{MIB, 4, 19, 0},        9, COUNTER, IPFRAGCREATES, RONLY, var_ip },
  247. X    {{MIB, 4, 20, 1, 1, 0xFF, 0xFF, 0xFF, 0xFF}, 14, IPADDRESS, IPADADDR, RONLY, var_ipAddrEntry },
  248. X    {{MIB, 4, 20, 1, 2, 0xFF, 0xFF, 0xFF, 0xFF}, 14, INTEGER,    IPADIFINDEX, RONLY, var_ipAddrEntry },
  249. X    {{MIB, 4, 20, 1, 3, 0xFF, 0xFF, 0xFF, 0xFF}, 14, IPADDRESS, IPADNETMASK, RONLY, var_ipAddrEntry },
  250. X    {{MIB, 4, 20, 1, 4, 0xFF, 0xFF, 0xFF, 0xFF}, 14, INTEGER,    IPADBCASTADDR, RONLY, var_ipAddrEntry },
  251. X    {{MIB, 4, 21, 1, 1, 0xFF, 0xFF, 0xFF, 0xFF}, 14, IPADDRESS, IPROUTEDEST, RONLY, var_ipRouteEntry },
  252. X    {{MIB, 4, 21, 1, 2, 0xFF, 0xFF, 0xFF, 0xFF}, 14, INTEGER,    IPROUTEIFINDEX, RONLY, var_ipRouteEntry },
  253. X    {{MIB, 4, 21, 1, 3, 0xFF, 0xFF, 0xFF, 0xFF}, 14, INTEGER,    IPROUTEMETRIC1, RONLY, var_ipRouteEntry },
  254. X    {{MIB, 4, 21, 1, 4, 0xFF, 0xFF, 0xFF, 0xFF}, 14, INTEGER,    IPROUTEMETRIC2, RONLY, var_ipRouteEntry },
  255. X    {{MIB, 4, 21, 1, 5, 0xFF, 0xFF, 0xFF, 0xFF}, 14, INTEGER,    IPROUTEMETRIC3, RONLY, var_ipRouteEntry },
  256. X    {{MIB, 4, 21, 1, 6, 0xFF, 0xFF, 0xFF, 0xFF}, 14, INTEGER,    IPROUTEMETRIC4, RONLY, var_ipRouteEntry },
  257. X    {{MIB, 4, 21, 1, 7, 0xFF, 0xFF, 0xFF, 0xFF}, 14, IPADDRESS, IPROUTENEXTHOP, RONLY, var_ipRouteEntry },
  258. X    {{MIB, 4, 21, 1, 8, 0xFF, 0xFF, 0xFF, 0xFF}, 14, INTEGER,    IPROUTETYPE, RONLY, var_ipRouteEntry },
  259. X    {{MIB, 4, 21, 1, 9, 0xFF, 0xFF, 0xFF, 0xFF}, 14, INTEGER,    IPROUTEPROTO, RONLY, var_ipRouteEntry },
  260. X    {{MIB, 4, 21, 1, 10, 0xFF, 0xFF, 0xFF, 0xFF}, 14, INTEGER,    IPROUTEAGE, RONLY, var_ipRouteEntry },
  261. X    {{MIB, 5, 1, 0},        9, COUNTER, ICMPINMSGS, RONLY, var_icmp },
  262. X    {{MIB, 5, 2, 0},        9, COUNTER, ICMPINERRORS, RONLY, var_icmp },
  263. X    {{MIB, 5, 3, 0},        9, COUNTER, ICMPINDESTUNREACHS, RONLY, var_icmp },
  264. X    {{MIB, 5, 4, 0},        9, COUNTER, ICMPINTIMEEXCDS, RONLY, var_icmp },
  265. X    {{MIB, 5, 5, 0},        9, COUNTER, ICMPINPARMPROBS, RONLY, var_icmp },
  266. X    {{MIB, 5, 6, 0},        9, COUNTER, ICMPINSRCQUENCHS, RONLY, var_icmp },
  267. X    {{MIB, 5, 7, 0},        9, COUNTER, ICMPINREDIRECTS, RONLY, var_icmp },
  268. X    {{MIB, 5, 8, 0},        9, COUNTER, ICMPINECHOS, RONLY, var_icmp },
  269. X    {{MIB, 5, 9, 0},        9, COUNTER, ICMPINECHOREPS, RONLY, var_icmp },
  270. X    {{MIB, 5, 10, 0},        9, COUNTER, ICMPINTIMESTAMPS, RONLY, var_icmp },
  271. X    {{MIB, 5, 11, 0},        9, COUNTER, ICMPINTIMESTAMPREPS, RONLY, var_icmp },
  272. X    {{MIB, 5, 12, 0},        9, COUNTER, ICMPINADDRMASKS, RONLY, var_icmp },
  273. X    {{MIB, 5, 13, 0},        9, COUNTER, ICMPINADDRMASKREPS, RONLY, var_icmp },
  274. X    {{MIB, 5, 14, 0},        9, COUNTER, ICMPOUTMSGS, RONLY, var_icmp },
  275. X    {{MIB, 5, 15, 0},        9, COUNTER, ICMPOUTERRORS, RONLY, var_icmp },
  276. X    {{MIB, 5, 16, 0},        9, COUNTER, ICMPOUTDESTUNREACHS, RONLY, var_icmp },
  277. X    {{MIB, 5, 17, 0},        9, COUNTER, ICMPOUTTIMEEXCDS, RONLY, var_icmp },
  278. X    {{MIB, 5, 18, 0},        9, COUNTER, ICMPOUTPARMPROBS, RONLY, var_icmp },
  279. X    {{MIB, 5, 19, 0},        9, COUNTER, ICMPOUTSRCQUENCHS, RONLY, var_icmp },
  280. X    {{MIB, 5, 20, 0},        9, COUNTER, ICMPOUTREDIRECTS, RONLY, var_icmp },
  281. X    {{MIB, 5, 21, 0},        9, COUNTER, ICMPOUTECHOS, RONLY, var_icmp },
  282. X    {{MIB, 5, 22, 0},        9, COUNTER, ICMPOUTECHOREPS, RONLY, var_icmp },
  283. X    {{MIB, 5, 23, 0},        9, COUNTER, ICMPOUTTIMESTAMPS, RONLY, var_icmp },
  284. X    {{MIB, 5, 24, 0},        9, COUNTER, ICMPOUTTIMESTAMPREPS, RONLY, var_icmp },
  285. X    {{MIB, 5, 25, 0},        9, COUNTER, ICMPOUTADDRMASKS, RONLY, var_icmp },
  286. X    {{MIB, 5, 26, 0},        9, COUNTER, ICMPOUTADDRMASKREPS, RONLY, var_icmp },
  287. X    {{MIB, 6, 1, 0},        9, INTEGER, TCPRTOALGORITHM, RONLY, var_tcp },
  288. X    {{MIB, 6, 2, 0},        9, INTEGER, TCPRTOMIN, RONLY, var_tcp },
  289. X    {{MIB, 6, 3, 0},        9, INTEGER, TCPRTOMAX, RONLY, var_tcp },
  290. X    {{MIB, 6, 4, 0},        9, INTEGER, TCPMAXCONN, RONLY, var_tcp },
  291. X    {{MIB, 6, 5, 0},        9, COUNTER, TCPACTIVEOPENS, RONLY, var_tcp },
  292. X    {{MIB, 6, 6, 0},        9, COUNTER, TCPPASSIVEOPENS, RONLY, var_tcp },
  293. X    {{MIB, 6, 7, 0},        9, COUNTER, TCPATTEMPTFAILS, RONLY, var_tcp },
  294. X    {{MIB, 6, 8, 0},        9, COUNTER, TCPESTABRESETS, RONLY, var_tcp },
  295. X    {{MIB, 6, 9, 0},        9, GAUGE,    TCPCURRESTAB, RONLY, var_tcp },
  296. X    {{MIB, 6,10, 0},        9, COUNTER, TCPINSEGS, RONLY, var_tcp },
  297. X    {{MIB, 6,11, 0},        9, COUNTER, TCPOUTSEGS, RONLY, var_tcp },
  298. X    {{MIB, 6,12, 0},        9, COUNTER, TCPRETRANSSEGS, RONLY, var_tcp },
  299. X    {{MIB, 6,13, 1, 1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},  20, INTEGER, TCPCONNSTATE, RONLY, var_tcp },
  300. X    {{MIB, 6,13, 1, 2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},  20, IPADDRESS, TCPCONNLOCALADDRESS, RONLY, var_tcp },
  301. X    {{MIB, 6,13, 1, 3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},  20, INTEGER, TCPCONNLOCALPORT, RONLY, var_tcp },
  302. X    {{MIB, 6,13, 1, 4, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},  20, IPADDRESS, TCPCONNREMADDRESS, RONLY, var_tcp },
  303. X    {{MIB, 6,13, 1, 5, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},  20, INTEGER, TCPCONNREMPORT, RONLY, var_tcp },
  304. X    {{MIB, 7, 1, 0},        9, COUNTER, UDPINDATAGRAMS, RONLY, var_udp },
  305. X    {{MIB, 7, 2, 0},        9, COUNTER, UDPNOPORTS, RONLY, var_udp },
  306. X    {{MIB, 7, 3, 0},        9, COUNTER, UDPINERRORS, RONLY, var_udp },
  307. X    {{MIB, 7, 4, 0},        9, COUNTER, UDPOUTDATAGRAMS, RONLY, var_udp }
  308. X};
  309. X
  310. X
  311. X
  312. X
  313. X/*
  314. X * getStatPtr - return a pointer to the named variable, as well as it's
  315. X * type, length, and access control list.
  316. X *
  317. X * If an exact match for the variable name exists, it is returned.  If not,
  318. X * and exact is false, the next variable lexicographically after the
  319. X * requested one is returned.
  320. X *
  321. X * If no appropriate variable can be found, NULL is returned.
  322. X */
  323. Xu_char    *
  324. XgetStatPtr(name, namelen, type, len, acl, exact, access_method)
  325. X    oid        *name;        /* IN - name of var, OUT - name matched */
  326. X    int        *namelen;   /* IN -number of sub-ids in name, OUT - subid-is in matched name */
  327. X    u_char    *type;        /* OUT - type of matched variable */
  328. X    int        *len;        /* OUT - length of matched variable */
  329. X    u_short    *acl;        /* OUT - access control list */
  330. X    int        exact;        /* IN - TRUE if exact match wanted */
  331. X    int        *access_method; /* OUT - 1 if function, 0 if char * */
  332. X{
  333. X
  334. X    register struct variable    *vp;
  335. X
  336. X    register int    x;
  337. X    register u_char    *access;
  338. X    int            result;
  339. X    register int    minlen;
  340. X    register oid    *name1, *name2;
  341. X
  342. X    for(x = 0, vp = variables; x < sizeof(variables)/sizeof(struct variable); vp++, x++){
  343. X    if (*namelen < (int)vp->namelen)
  344. X        minlen = *namelen;
  345. X    else
  346. X        minlen = (int)vp->namelen;
  347. X    name1 = name; name2 = vp->name;
  348. X    result = 0;
  349. X    while(minlen-- > 0){
  350. X        if (*name1 < *name2){
  351. X        result = -1;
  352. X        break;
  353. X        }
  354. X        if (*name2++ < *name1++){
  355. X        result = 1;
  356. X        break;
  357. X        }
  358. X    }
  359. X    if (result == 0){
  360. X        if (*namelen < (int)vp->namelen)
  361. X        result = -1;    /* name1 shorter so it is "less" */
  362. X        else if ((int)vp->namelen < *namelen)
  363. X        result = 1;
  364. X        else
  365. X        result = 0;
  366. X    }
  367. X/*    result = compare(name, *namelen, vp->name, (int)vp->namelen); */
  368. X    if ((result < 0) || (exact && (result == 0))){
  369. X        access = (*(vp->findVar))(vp, name, namelen, exact, len, access_method);
  370. X        if (access != NULL)
  371. X        break;
  372. X    }
  373. X    }
  374. X    if (x == sizeof(variables)/sizeof(struct variable))
  375. X    return NULL;
  376. X
  377. X    /* vp now points to the approprate struct */
  378. X    *type = vp->type;
  379. X    *acl = vp->acl;
  380. X    return access;
  381. X}
  382. X
  383. X
  384. X
  385. Xint
  386. Xcompare(name1, len1, name2, len2)
  387. X    register oid        *name1, *name2;
  388. X    register int        len1, len2;
  389. X{
  390. X    register int    len;
  391. X
  392. X    /* len = minimum of len1 and len2 */
  393. X    if (len1 < len2)
  394. X    len = len1;
  395. X    else
  396. X    len = len2;
  397. X    /* find first non-matching byte */
  398. X    while(len-- > 0){
  399. X    if (*name1 < *name2)
  400. X        return -1;
  401. X    if (*name2++ < *name1++)
  402. X        return 1;
  403. X    }
  404. X    /* bytes match up to length of shorter string */
  405. X    if (len1 < len2)
  406. X    return -1;  /* name1 shorter, so it is "less" */
  407. X    if (len2 < len1)
  408. X    return 1;
  409. X    return 0;    /* both strings are equal */
  410. X}
  411. X
  412. Xchar version_descr[32] = "Unix 4.3BSD";
  413. Xoid version_id[] = {1, 3, 6, 1, 4, 1, 3, 1, 1};
  414. X
  415. Xu_char *
  416. Xvar_system(vp, name, length, exact, var_len, write_method)
  417. X    register struct variable *vp;   /* IN - pointer to variable entry that points here */
  418. X    register oid    *name;        /* IN/OUT - input name requested, output name found */
  419. X    register int    *length;    /* IN/OUT - length of input and output oid's */
  420. X    int            exact;        /* IN - TRUE if an exact match was requested. */
  421. X    int            *var_len;   /* OUT - length of variable or 0 if function returned. */
  422. X    int            (**write_method)(); /* OUT - 1 if function, 0 if char pointer. */
  423. X{
  424. X    struct timeval now, boottime;
  425. X    extern int writeVersion();
  426. X
  427. X    if (exact && (compare(name, *length, vp->name, (int)vp->namelen) != 0))
  428. X    return NULL;
  429. X    bcopy((char *)vp->name, (char *)name, (int)vp->namelen * sizeof(oid));
  430. X    *length = vp->namelen;
  431. X    *write_method = 0;
  432. X    *var_len = sizeof(long);    /* default length */
  433. X    switch (vp->magic){
  434. X    case VERSION_DESCR:
  435. X        *var_len = strlen(version_descr);
  436. X        *write_method = writeVersion;
  437. X        return (u_char *)version_descr;
  438. X    case VERSION_ID:
  439. X        *var_len = sizeof(version_id);
  440. X        return (u_char *)version_id;
  441. X    case UPTIME:
  442. X        klseek(nl[N_BOOTTIME].n_value);
  443. X        klread((char *)&boottime, sizeof(boottime));
  444. X        gettimeofday(&now, (struct timezone *)0);
  445. X        long_return = (now.tv_sec - boottime.tv_sec) * 100
  446. X            + (now.tv_usec - boottime.tv_usec) / 10000;
  447. X        return (u_char *) &long_return;
  448. X    case IFNUMBER:
  449. X        long_return = Interface_Scan_Get_Count();
  450. X        return (u_char *) &long_return;
  451. X    default:
  452. X        ERROR("");
  453. X    }
  454. X    return NULL;
  455. X}
  456. X
  457. X#include <ctype.h>
  458. Xint
  459. XwriteVersion(doSet, var_val, var_val_type, var_val_len, statP)
  460. X   int      doSet;
  461. X   u_char   *var_val;
  462. X   u_char   var_val_type;
  463. X   int      var_val_len;
  464. X   u_char   *statP;
  465. X{
  466. X    int bigsize = 1000;
  467. X    u_char buf[sizeof(version_descr)], *cp;
  468. X    int count, size;
  469. X
  470. X    if (var_val_type != STRING){
  471. X    printf("not string\n");
  472. X    return FALSE;
  473. X    }
  474. X    if (var_val_len > sizeof(version_descr)-1){
  475. X    printf("bad length\n");
  476. X    return FALSE;
  477. X    }
  478. X    size = sizeof(buf);
  479. X    asn_parse_string(var_val, &bigsize, &var_val_type, (long *)buf, &size);
  480. X    for(cp = buf, count = 0; count < size; count++, cp++){
  481. X    if (!isprint(*cp)){
  482. X        printf("not print %x\n", *cp);
  483. X        return FALSE;
  484. X    }
  485. X    }
  486. X    buf[size] = 0;
  487. X    if (doSet){
  488. X    strcpy(version_descr, buf);
  489. X    
  490. X    }
  491. X    return TRUE;
  492. X}
  493. X
  494. X
  495. X
  496. Xu_char *
  497. Xvar_ifEntry(vp, name, length, exact, var_len, write_method)
  498. X    register struct variable *vp;   /* IN - pointer to variable entry that points here */
  499. X    register oid    *name;        /* IN/OUT - input name requested, output name found */
  500. X    register int    *length;    /* IN/OUT - length of input and output oid's */
  501. X    int            exact;        /* IN - TRUE if an exact match was requested. */
  502. X    int            *var_len;   /* OUT - length of variable or 0 if function returned. */
  503. X    int            (**write_method)(); /* OUT - 1 if function, 0 if char pointer. */
  504. X{
  505. X    oid            newname[MAX_NAME_LEN];
  506. X    register int    interface;
  507. X    int result, count;
  508. X    static struct ifnet ifnet;
  509. X    static struct in_ifaddr in_ifaddr;
  510. X    static char Name[16];
  511. X    register char *cp;
  512. X
  513. X    bcopy((char *)vp->name, (char *)newname, (int)vp->namelen * sizeof(oid));
  514. X    /* find "next" interface */
  515. X    count = Interface_Scan_Get_Count();
  516. X    for(interface = 1; interface <= count; interface++){
  517. X    newname[10] = (oid)interface;
  518. X    result = compare(name, *length, newname, (int)vp->namelen);
  519. X    if ((exact && (result == 0)) || (!exact && (result < 0)))
  520. X        break;
  521. X    }
  522. X    if (interface > count)
  523. X    return NULL;
  524. X
  525. X    bcopy((char *)newname, (char *)name, (int)vp->namelen * sizeof(oid));
  526. X    *length = vp->namelen;
  527. X    *write_method = 0;
  528. X    *var_len = sizeof(long);
  529. X
  530. X    Interface_Scan_By_Index(interface, Name, &ifnet, &in_ifaddr);
  531. X    switch (vp->magic){
  532. X    case IFINDEX:
  533. X        long_return = interface;
  534. X        return (u_char *) &long_return;
  535. X    case IFDESCR:
  536. X#define USE_NAME_AS_DESCRIPTION
  537. X#ifdef USE_NAME_AS_DESCRIPTION
  538. X        cp = Name;
  539. X#else  USE_NAME_AS_DESCRIPTION
  540. X        cp = Lookup_Device_Annotation(Name, "snmp-descr");
  541. X        if (!cp)
  542. X        cp = Lookup_Device_Annotation(Name, 0);
  543. X        if (!cp) cp = Name;
  544. X#endif USE_NAME_AS_DESCRIPTION
  545. X        *var_len = strlen(cp);
  546. X        return (u_char *)cp;
  547. X    case IFTYPE:
  548. X#if 0
  549. X        cp = Lookup_Device_Annotation(Name, "snmp-type");
  550. X        if (cp) long_return = atoi(cp);
  551. X        else
  552. X#endif
  553. X        long_return = 1;    /* OTHER */
  554. X        return (u_char *) &long_return;
  555. X    case IFMTU: {
  556. X        long_return = (long) ifnet.if_mtu;
  557. X        return (u_char *) &long_return;
  558. X    }
  559. X    case IFSPEED:
  560. X#if 0
  561. X        cp = Lookup_Device_Annotation(Name, "snmp-speed");
  562. X        if (cp) long_return = atoi(cp);
  563. X        else
  564. X#endif
  565. X        long_return = 1;    /* OTHER */
  566. X        return (u_char *) &long_return;
  567. X    case IFPHYSADDRESS:
  568. X#if 0
  569. X        if (Lookup_Device_Annotation(Name, "ethernet-device")) {
  570. X        Interface_Get_Ether_By_Index(interface, return_buf);
  571. X        *var_len = 6;
  572. X        return(u_char *) return_buf;
  573. X        } else {
  574. X        long_return = 0;
  575. X        return (u_char *) long_return;
  576. X        }
  577. X#endif
  578. X        *var_len = 6;
  579. X        return (u_char *)return_buf;
  580. X    case IFADMINSTATUS:
  581. X        long_return = ifnet.if_flags & IFF_RUNNING ? 1 : 2;
  582. X        return (u_char *) &long_return;
  583. X    case IFOPERSTATUS:
  584. X        long_return = ifnet.if_flags & IFF_UP ? 1 : 2;
  585. X        return (u_char *) &long_return;
  586. X    case IFLASTCHANGE:
  587. X        long_return = 0; /* XXX */
  588. X        return (u_char *) &long_return;
  589. X    case IFINOCTETS:
  590. X        long_return = ifnet.if_ipackets * (ifnet.if_mtu / 2); /* XXX */
  591. X        return (u_char *) &long_return;
  592. X    case IFINUCASTPKTS:
  593. X        long_return = ifnet.if_ipackets;
  594. X        return (u_char *) &long_return;
  595. X    case IFINNUCASTPKTS:
  596. X        long_return = 0; /* XXX */
  597. X        return (u_char *) &long_return;
  598. X    case IFINDISCARDS:
  599. X        long_return = 0; /* XXX */
  600. X        return (u_char *) &long_return;
  601. X    case IFINERRORS:
  602. X        return (u_char *) &ifnet.if_ierrors;
  603. X    case IFINUNKNOWNPROTOS:
  604. X        long_return = 0; /* XXX */
  605. X        return (u_char *) &long_return;
  606. X    case IFOUTOCTETS:
  607. X        long_return = ifnet.if_opackets * (ifnet.if_mtu / 2); /* XXX */
  608. X        return (u_char *) &long_return;
  609. X    case IFOUTUCASTPKTS:
  610. X        long_return = ifnet.if_opackets;
  611. X        return (u_char *) &long_return;
  612. X    case IFOUTNUCASTPKTS:
  613. X        long_return = 0; /* XXX */
  614. X        return (u_char *) &long_return;
  615. X    case IFOUTDISCARDS:
  616. X        return (u_char *) &ifnet.if_snd.ifq_drops;
  617. X    case IFOUTERRORS:
  618. X        return (u_char *) &ifnet.if_oerrors;
  619. X    case IFOUTQLEN:
  620. X        return (u_char *) &ifnet.if_snd.ifq_len;
  621. X    default:
  622. X        ERROR("");
  623. X    }
  624. X    return NULL;
  625. X}
  626. X
  627. X/*
  628. X * Read the ARP table
  629. X */
  630. X
  631. Xu_char *
  632. Xvar_atEntry(vp, name, length, exact, var_len, write_method)
  633. X    register struct variable *vp;    /* IN - pointer to variable entry that points here */
  634. X    register oid        *name;    /* IN/OUT - input name requested, output name found */
  635. X    register int        *length;    /* IN/OUT - length of input and output oid's */
  636. X    int                exact;    /* IN - TRUE if an exact match was requested. */
  637. X    int                *var_len;    /* OUT - length of variable or 0 if function returned. */
  638. X    int                (**write_method)(); /* OUT - 1 if function, 0 if char pointer. */
  639. X{
  640. X    /*
  641. X     * object identifier is of form:
  642. X     * 1.3.6.1.2.1.3.1.1.1.interface.1.A.B.C.D,  where A.B.C.D is IP address.
  643. X     * Interface is at offset 10,
  644. X     * IPADDR starts at offset 12.
  645. X     */
  646. X    u_char            *cp;
  647. X    oid                *op;
  648. X    oid                lowest[16];
  649. X    oid                current[16];
  650. X    static char            PhysAddr[6], LowPhysAddr[6];
  651. X    u_long            Addr, LowAddr;
  652. X
  653. X    /* fill in object part of name for current (less sizeof instance part) */
  654. X
  655. X    bcopy((char *)vp->name, (char *)current, (int)(vp->namelen - 6) * sizeof(oid));
  656. X
  657. X    LowAddr = -1;      /* Don't have one yet */
  658. X    ARP_Scan_Init();
  659. X    for (;;) {
  660. X    if (ARP_Scan_Next(&Addr, PhysAddr) == 0) break;
  661. X    current[10] = 1;    /* IfIndex == 1 (ethernet???) XXX */
  662. X    current[11] = 1;
  663. X    cp = (u_char *)&Addr;
  664. X    op = current + 12;
  665. X    *op++ = *cp++;
  666. X    *op++ = *cp++;
  667. X    *op++ = *cp++;
  668. X    *op++ = *cp++;
  669. X
  670. X    if (exact){
  671. X        if (compare(current, 16, name, *length) == 0){
  672. X        bcopy((char *)current, (char *)lowest, 16 * sizeof(oid));
  673. X        LowAddr = Addr;
  674. X        bcopy(PhysAddr, LowPhysAddr, sizeof(PhysAddr));
  675. X        break;    /* no need to search further */
  676. X        }
  677. X    } else {
  678. X        if ((compare(current, 16, name, *length) > 0) &&
  679. X         ((LowAddr == -1) || (compare(current, 16, lowest, 16) < 0))){
  680. X        /*
  681. X         * if new one is greater than input and closer to input than
  682. X         * previous lowest, save this one as the "next" one.
  683. X         */
  684. X        bcopy((char *)current, (char *)lowest, 16 * sizeof(oid));
  685. X        LowAddr = Addr;
  686. X        bcopy(PhysAddr, LowPhysAddr, sizeof(PhysAddr));
  687. X        }
  688. X    }
  689. X    }
  690. X    if (LowAddr == -1) return(NULL);
  691. X
  692. X    bcopy((char *)lowest, (char *)name, 16 * sizeof(oid));
  693. X    *length = 16;
  694. X    *write_method = 0;
  695. X    switch(vp->magic){
  696. X    case ATIFINDEX:
  697. X        *var_len = sizeof long_return;
  698. X        long_return = 1; /* XXX */
  699. X        return (u_char *)&long_return;
  700. X    case ATPHYSADDRESS:
  701. X        *var_len = sizeof(LowPhysAddr);
  702. X        return (u_char *)LowPhysAddr;
  703. X    case ATNETADDRESS:
  704. X        *var_len = sizeof long_return;
  705. X        long_return = LowAddr;
  706. X        return (u_char *)&long_return;
  707. X    default:
  708. X        ERROR("");
  709. X   }
  710. X   return NULL;
  711. X}
  712. X
  713. Xu_char *
  714. Xvar_ip(vp, name, length, exact, var_len, write_method)
  715. X    register struct variable *vp;   /* IN - pointer to variable entry that points here */
  716. X    oid     *name;        /* IN/OUT - input name requested, output name found */
  717. X    int     *length;        /* IN/OUT - length of input and output oid's */
  718. X    int     exact;        /* IN - TRUE if an exact match was requested. */
  719. X    int     *var_len;        /* OUT - length of variable or 0 if function returned. */
  720. X    int     (**write_method)(); /* OUT - 1 if function, 0 if char pointer. */
  721. X{
  722. X    int i;
  723. X    static struct ipstat ipstat;
  724. X
  725. X    if (exact && (compare(name, *length, vp->name, (int)vp->namelen) != 0))
  726. X    return NULL;
  727. X    bcopy((char *)vp->name, (char *)name, (int)vp->namelen * sizeof(oid));
  728. X
  729. X    *length = vp->namelen;
  730. X    *write_method = 0;
  731. X    *var_len = sizeof(long);    /* default length */
  732. X    /*
  733. X     *    Get the IP statistics from the kernel...
  734. X     */
  735. X
  736. X    klseek(nl[N_IPSTAT].n_value);
  737. X    klread((char *)&ipstat, sizeof (ipstat));
  738. X
  739. X    switch (vp->magic){
  740. X    case IPFORWARDING:
  741. X        klseek(nl[N_IPFORWARDING].n_value);
  742. X        klread((char *) &i, sizeof(i));
  743. X        if (i) {
  744. X        klseek(nl[N_IN_INTERFACES].n_value);
  745. X        klread((char *) &i, sizeof(i));
  746. X        if (i > 1)
  747. X            long_return = 1;        /* GATEWAY */
  748. X        else
  749. X            long_return = 2;        /* GATEWAY configured as HOST */
  750. X        } else {
  751. X        long_return = 2;        /* HOST    */
  752. X        }
  753. X        return (u_char *) &long_return;
  754. X    case IPDEFAULTTTL:
  755. X        /*
  756. X         *    Allow for a kernel w/o TCP.
  757. X         */
  758. X        if (nl[N_TCP_TTL].n_value) {
  759. X        klseek(nl[N_TCP_TTL].n_value);
  760. X        klread((char *) &long_return, sizeof(long_return));
  761. X        } else long_return = 60;        /* XXX */
  762. X        return (u_char *) &long_return;
  763. X    case IPINRECEIVES:
  764. X        return (u_char *) &ipstat.ips_total;
  765. X    case IPINHDRERRORS:
  766. X        long_return = ipstat.ips_badsum + ipstat.ips_tooshort +
  767. X              ipstat.ips_toosmall + ipstat.ips_badhlen +
  768. X              ipstat.ips_badlen;
  769. X        return (u_char *) &long_return;
  770. X    case IPINADDRERRORS:
  771. X        return (u_char *) &ipstat.ips_cantforward;
  772. X    case IPFORWDATAGRAMS:
  773. X        return (u_char *) &ipstat.ips_forward;
  774. X    case IPINUNKNOWNPROTOS:
  775. X        long_return = 0;
  776. X        return (u_char *) &long_return;
  777. X    case IPINDISCARDS:
  778. X        long_return = 0;
  779. X        return (u_char *) &long_return;
  780. X    case IPINDELIVERS:
  781. X        long_return = ipstat.ips_total -
  782. X             (ipstat.ips_badsum + ipstat.ips_tooshort +
  783. X              ipstat.ips_toosmall + ipstat.ips_badhlen +
  784. X              ipstat.ips_badlen);
  785. X        return (u_char *) &long_return;
  786. X    case IPOUTREQUESTS:
  787. X        long_return = 0;
  788. X        return (u_char *) &long_return;
  789. X    case IPOUTDISCARDS:
  790. X        long_return = 0;
  791. X        return (u_char *) &long_return;
  792. X    case IPOUTNOROUTES:
  793. X        return (u_char *) &ipstat.ips_cantforward;
  794. X    case IPREASMTIMEOUT:
  795. X        long_return = IPFRAGTTL;
  796. X        return (u_char *) &long_return;
  797. X    case IPREASMREQDS:
  798. X        return (u_char *) &ipstat.ips_fragments;
  799. X    case IPREASMOKS:
  800. X        return (u_char *) &ipstat.ips_fragments;
  801. X    case IPREASMFAILS:
  802. X        long_return = ipstat.ips_fragdropped + ipstat.ips_fragtimeout;
  803. X        return (u_char *) &long_return;
  804. X    case IPFRAGOKS:
  805. X        long_return = 0;
  806. X        return (u_char *) &long_return;
  807. X    case IPFRAGFAILS:
  808. X        long_return = 0;
  809. X        return (u_char *) &long_return;
  810. X    case IPFRAGCREATES:
  811. X        long_return = 0;
  812. X        return (u_char *) &long_return;
  813. X    default:
  814. X        ERROR("");
  815. X    }
  816. X    return NULL;
  817. X}
  818. X
  819. Xu_char *
  820. Xvar_ipAddrEntry(vp, name, length, exact, var_len, write_method)
  821. X    register struct variable *vp;    /* IN - pointer to variable entry that points here */
  822. X    register oid    *name;        /* IN/OUT - input name requested, output name found */
  823. X    register int    *length;    /* IN/OUT - length of input and output oid's */
  824. X    int            exact;        /* IN - TRUE if an exact match was requested. */
  825. X    int            *var_len;   /* OUT - length of variable or 0 if function returned. */
  826. X    int            (**write_method)(); /* OUT - 1 if function, 0 if char pointer. */
  827. X{
  828. X    /*
  829. X     * object identifier is of form:
  830. X     * 1.3.6.1.2.1.4.20.1.?.A.B.C.D,  where A.B.C.D is IP address.
  831. X     * IPADDR starts at offset 10.
  832. X     */
  833. X    oid                lowest[14];
  834. X    oid                current[14], *op;
  835. X    u_char            *cp;
  836. X    int                interface, lowinterface=0;
  837. X    static struct ifnet ifnet;
  838. X    static struct in_ifaddr in_ifaddr, lowin_ifaddr;
  839. X
  840. X    /* fill in object part of name for current (less sizeof instance part) */
  841. X
  842. X    bcopy((char *)vp->name, (char *)current, (int)(vp->namelen - 4) * sizeof(oid));
  843. X
  844. X    Interface_Scan_Init();
  845. X    for (;;) {
  846. X    if (Interface_Scan_Next(&interface, (char *)0, &ifnet, &in_ifaddr) == 0) break;
  847. X
  848. X    cp = (u_char *)&(((struct sockaddr_in *) &(in_ifaddr.ia_addr))->sin_addr.s_addr);
  849. X    op = current + 10;
  850. X    *op++ = *cp++;
  851. X    *op++ = *cp++;
  852. X    *op++ = *cp++;
  853. X    *op++ = *cp++;
  854. X
  855. X    if (exact){
  856. X        if (compare(current, 14, name, *length) == 0){
  857. X        bcopy((char *)current, (char *)lowest, 14 * sizeof(oid));
  858. X        lowinterface = interface;
  859. X        lowin_ifaddr = in_ifaddr;
  860. X        break;    /* no need to search further */
  861. X        }
  862. X    } else {
  863. X        if ((compare(current, 14, name, *length) > 0) &&
  864. X         (!lowinterface || (compare(current, 14, lowest, 14) < 0))){
  865. X        /*
  866. X         * if new one is greater than input and closer to input than
  867. X         * previous lowest, save this one as the "next" one.
  868. X         */
  869. X        lowinterface = interface;
  870. X        lowin_ifaddr = in_ifaddr;
  871. X        bcopy((char *)current, (char *)lowest, 14 * sizeof(oid));
  872. X        }
  873. X    }
  874. X    }
  875. X    if (!lowinterface) return(NULL);
  876. X    bcopy((char *)lowest, (char *)name, 14 * sizeof(oid));
  877. X    *length = 14;
  878. X    *write_method = 0;
  879. X    *var_len = sizeof(long_return);
  880. X    switch(vp->magic){
  881. X    case IPADADDR:
  882. X        return(u_char *) &((struct sockaddr_in *) &lowin_ifaddr.ia_addr)->sin_addr.s_addr;
  883. X    case IPADIFINDEX:
  884. X        long_return = lowinterface;
  885. X        return(u_char *) &long_return;
  886. X    case IPADNETMASK:
  887. X        long_return = ntohl(lowin_ifaddr.ia_subnetmask);
  888. X        return(u_char *) &long_return;
  889. X    case IPADBCASTADDR:
  890. X        long_return = ntohl(((struct sockaddr_in *) &lowin_ifaddr.ia_addr)->sin_addr.s_addr) & 1;
  891. X        return(u_char *) &long_return;
  892. X    default:
  893. X        ERROR("");
  894. X    }
  895. X    return NULL;
  896. X}
  897. X
  898. X
  899. Xu_char *
  900. Xvar_icmp(vp, name, length, exact, var_len, write_method)
  901. X    register struct variable *vp;    /* IN - pointer to variable entry that points here */
  902. X    oid     *name;        /* IN/OUT - input name requested, output name found */
  903. X    int     *length;        /* IN/OUT - length of input and output oid's */
  904. X    int     exact;        /* IN - TRUE if an exact match was requested. */
  905. X    int     *var_len;        /* OUT - length of variable or 0 if function returned. */
  906. X    int     (**write_method)(); /* OUT - 1 if function, 0 if char pointer. */
  907. X{
  908. X    register int i;
  909. X    static struct icmpstat icmpstat;
  910. X
  911. X    if (exact && (compare(name, *length, vp->name, (int)vp->namelen) != 0))
  912. X    return NULL;
  913. X    bcopy((char *)vp->name, (char *)name, (int)vp->namelen * sizeof(oid));
  914. X    *length = vp->namelen;
  915. X    *write_method = 0;
  916. X    *var_len = sizeof(long); /* all following variables are sizeof long */
  917. X
  918. X    /*
  919. X     *    Get the UDP statistics from the kernel...
  920. X     */
  921. X
  922. X    klseek(nl[N_ICMPSTAT].n_value);
  923. X    klread((char *)&icmpstat, sizeof (icmpstat));
  924. X
  925. X    switch (vp->magic){
  926. X    case ICMPINMSGS:
  927. X        long_return = icmpstat.icps_badcode + icmpstat.icps_tooshort +
  928. X              icmpstat.icps_checksum + icmpstat.icps_badlen;
  929. X        for (i=0; i <= ICMP_MAXTYPE; i++)
  930. X        long_return += icmpstat.icps_inhist[i];
  931. X        return (u_char *)&long_return;
  932. X    case ICMPINERRORS:
  933. X        long_return = icmpstat.icps_badcode + icmpstat.icps_tooshort +
  934. X              icmpstat.icps_checksum + icmpstat.icps_badlen;
  935. X        return (u_char *)&long_return;
  936. X    case ICMPINDESTUNREACHS:
  937. X        return (u_char *) &icmpstat.icps_inhist[ICMP_UNREACH];
  938. X    case ICMPINTIMEEXCDS:
  939. X        return (u_char *) &icmpstat.icps_inhist[ICMP_TIMXCEED];
  940. X    case ICMPINPARMPROBS:
  941. X        return (u_char *) &icmpstat.icps_inhist[ICMP_PARAMPROB];
  942. X    case ICMPINSRCQUENCHS:
  943. X        return (u_char *) &icmpstat.icps_inhist[ICMP_SOURCEQUENCH];
  944. X    case ICMPINREDIRECTS:
  945. X        return (u_char *) &icmpstat.icps_inhist[ICMP_REDIRECT];
  946. X    case ICMPINECHOS:
  947. X        return (u_char *) &icmpstat.icps_inhist[ICMP_ECHO];
  948. X    case ICMPINECHOREPS:
  949. X        return (u_char *) &icmpstat.icps_inhist[ICMP_ECHOREPLY];
  950. X    case ICMPINTIMESTAMPS:
  951. X        return (u_char *) &icmpstat.icps_inhist[ICMP_TSTAMP];
  952. X    case ICMPINTIMESTAMPREPS:
  953. X        return (u_char *) &icmpstat.icps_inhist[ICMP_TSTAMPREPLY];
  954. X    case ICMPINADDRMASKS:
  955. X        return (u_char *) &icmpstat.icps_inhist[ICMP_MASKREQ];
  956. X    case ICMPINADDRMASKREPS:
  957. X        return (u_char *) &icmpstat.icps_inhist[ICMP_MASKREPLY];
  958. X    case ICMPOUTMSGS:
  959. X        long_return = icmpstat.icps_oldshort + icmpstat.icps_oldicmp;
  960. X        for (i=0; i <= ICMP_MAXTYPE; i++)
  961. X        long_return += icmpstat.icps_outhist[i];
  962. X        return (u_char *)&long_return;
  963. X    case ICMPOUTERRORS:
  964. X        long_return = icmpstat.icps_oldshort + icmpstat.icps_oldicmp;
  965. X        return (u_char *)&long_return;
  966. X    case ICMPOUTDESTUNREACHS:
  967. X        return (u_char *) &icmpstat.icps_outhist[ICMP_UNREACH];
  968. X    case ICMPOUTTIMEEXCDS:
  969. X        return (u_char *) &icmpstat.icps_outhist[ICMP_TIMXCEED];
  970. X    case ICMPOUTPARMPROBS:
  971. X        return (u_char *) &icmpstat.icps_outhist[ICMP_PARAMPROB];
  972. X    case ICMPOUTSRCQUENCHS:
  973. X        return (u_char *) &icmpstat.icps_outhist[ICMP_SOURCEQUENCH];
  974. X    case ICMPOUTREDIRECTS:
  975. X        return (u_char *) &icmpstat.icps_outhist[ICMP_REDIRECT];
  976. X    case ICMPOUTECHOS:
  977. X        return (u_char *) &icmpstat.icps_outhist[ICMP_ECHO];
  978. X    case ICMPOUTECHOREPS:
  979. X        return (u_char *) &icmpstat.icps_outhist[ICMP_ECHOREPLY];
  980. X    case ICMPOUTTIMESTAMPS:
  981. X        return (u_char *) &icmpstat.icps_outhist[ICMP_TSTAMP];
  982. X    case ICMPOUTTIMESTAMPREPS:
  983. X        return (u_char *) &icmpstat.icps_outhist[ICMP_TSTAMPREPLY];
  984. X    case ICMPOUTADDRMASKS:
  985. X        return (u_char *) &icmpstat.icps_outhist[ICMP_MASKREQ];
  986. X    case ICMPOUTADDRMASKREPS:
  987. X        return (u_char *) &icmpstat.icps_outhist[ICMP_MASKREPLY];
  988. X    default:
  989. X        ERROR("");
  990. X    }
  991. X    return NULL;
  992. X}
  993. X
  994. X
  995. Xu_char *
  996. Xvar_udp(vp, name, length, exact, var_len, write_method)
  997. X    register struct variable *vp;    /* IN - pointer to variable entry that points here */
  998. X    oid     *name;        /* IN/OUT - input name requested, output name found */
  999. X    int     *length;        /* IN/OUT - length of input and output oid's */
  1000. X    int     exact;        /* IN - TRUE if an exact match was requested. */
  1001. X    int     *var_len;        /* OUT - length of variable or 0 if function returned. */
  1002. X    int     (**write_method)(); /* OUT - 1 if function, 0 if char pointer. */
  1003. X{
  1004. X    static struct udpstat udpstat;
  1005. X
  1006. X    if (exact && (compare(name, *length, vp->name, (int)vp->namelen) != 0))
  1007. X    return NULL;
  1008. X    bcopy((char *)vp->name, (char *)name, (int)vp->namelen * sizeof(oid));
  1009. X
  1010. X    *length = vp->namelen;
  1011. X    *write_method = 0;
  1012. X    *var_len = sizeof(long);    /* default length */
  1013. X    /*
  1014. X     *    Get the IP statistics from the kernel...
  1015. X     */
  1016. X
  1017. X    klseek(nl[N_UDPSTAT].n_value);
  1018. X    klread((char *)&udpstat, sizeof (udpstat));
  1019. X
  1020. X    switch (vp->magic){
  1021. X    case UDPINDATAGRAMS:
  1022. X    case UDPNOPORTS:
  1023. X    case UDPOUTDATAGRAMS:
  1024. X        long_return = 0;
  1025. X        return (u_char *) &long_return;
  1026. X    case UDPINERRORS:
  1027. X        long_return = udpstat.udps_hdrops + udpstat.udps_badsum +
  1028. X              udpstat.udps_badlen;
  1029. X        return (u_char *) &long_return;
  1030. X    default:
  1031. X        ERROR("");
  1032. X    }
  1033. X    return NULL;
  1034. X}
  1035. X
  1036. Xu_char *
  1037. Xvar_tcp(vp, name, length, exact, var_len, write_method)
  1038. X    register struct variable *vp;    /* IN - pointer to variable entry that points here */
  1039. X    oid     *name;        /* IN/OUT - input name requested, output name found */
  1040. X    int     *length;        /* IN/OUT - length of input and output oid's */
  1041. X    int     exact;        /* IN - TRUE if an exact match was requested. */
  1042. X    int     *var_len;        /* OUT - length of variable or 0 if function returned. */
  1043. X    int     (**write_method)(); /* OUT - 1 if function, 0 if char pointer. */
  1044. X{
  1045. X    int i;
  1046. X    static struct tcpstat tcpstat;
  1047. X    oid newname[MAX_NAME_LEN], lowest[MAX_NAME_LEN], *op;
  1048. X    u_char *cp;
  1049. X    int State, LowState;
  1050. X    static struct inpcb inpcb, Lowinpcb;
  1051. X
  1052. X    /*
  1053. X     *    Allow for a kernel w/o TCP
  1054. X     */
  1055. X
  1056. X    if (nl[N_TCPSTAT].n_value == 0) return(NULL);
  1057. X
  1058. X    if (vp->magic < TCPCONNSTATE) {
  1059. X    if (exact && (compare(name, *length, vp->name, (int)vp->namelen) != 0))
  1060. X        return NULL;
  1061. X    bcopy((char *)vp->name, (char *)name, (int)vp->namelen * sizeof(oid));
  1062. X
  1063. X    *length = vp->namelen;
  1064. X    *write_method = 0;
  1065. X    *var_len = sizeof(long);    /* default length */
  1066. X    /*
  1067. X     *  Get the TCP statistics from the kernel...
  1068. X     */
  1069. X
  1070. X    klseek(nl[N_TCPSTAT].n_value);
  1071. X    klread((char *)&tcpstat, sizeof (tcpstat));
  1072. X
  1073. X    switch (vp->magic){
  1074. X        case TCPRTOALGORITHM:
  1075. X        long_return = 4;    /* Van Jacobsen's algorithm */    /* XXX */
  1076. X        return (u_char *) &long_return;
  1077. X        case TCPRTOMIN:
  1078. X        long_return = TCPTV_MIN / PR_SLOWHZ * 1000;
  1079. X        return (u_char *) &long_return;
  1080. X        case TCPRTOMAX:
  1081. X        long_return = TCPTV_REXMTMAX / PR_SLOWHZ * 1000;
  1082. X        return (u_char *) &long_return;
  1083. X        case TCPMAXCONN:
  1084. X        long_return = -1;
  1085. X        return (u_char *) &long_return;
  1086. X        case TCPACTIVEOPENS:
  1087. X        return (u_char *) &tcpstat.tcps_connattempt;
  1088. X        case TCPPASSIVEOPENS:
  1089. X        return (u_char *) &tcpstat.tcps_accepts;
  1090. X        case TCPATTEMPTFAILS:
  1091. X        return (u_char *) &tcpstat.tcps_conndrops;
  1092. X        case TCPESTABRESETS:
  1093. X        return (u_char *) &tcpstat.tcps_drops;
  1094. X        case TCPCURRESTAB:
  1095. X        long_return = TCP_Count_Connections();
  1096. X        return (u_char *) &long_return;
  1097. X        case TCPINSEGS:
  1098. X        return (u_char *) &tcpstat.tcps_rcvtotal;
  1099. X        case TCPOUTSEGS:
  1100. X        return (u_char *) &tcpstat.tcps_sndtotal;
  1101. X        case TCPRETRANSSEGS:
  1102. X        return (u_char *) &tcpstat.tcps_sndrexmitpack;
  1103. X        default:
  1104. X        ERROR("");
  1105. X    }
  1106. X    } else {    /* Info about a particular connection */
  1107. X    bcopy((char *)vp->name, (char *)newname, (int)vp->namelen * sizeof(oid));
  1108. X    /* find "next" connection */
  1109. XAgain:
  1110. XLowState = -1;        /* Don't have one yet */
  1111. X    TCP_Scan_Init();
  1112. X    for (;;) {
  1113. X        if ((i = TCP_Scan_Next(&State, &inpcb)) < 0) goto Again;
  1114. X        if (i == 0) break;        /* Done */
  1115. X        cp = (u_char *)&inpcb.inp_laddr.s_addr;
  1116. X        op = newname + 10;
  1117. X        *op++ = *cp++;
  1118. X        *op++ = *cp++;
  1119. X        *op++ = *cp++;
  1120. X        *op++ = *cp++;
  1121. X        
  1122. X        newname[14] = ntohs(inpcb.inp_lport);
  1123. X
  1124. X        cp = (u_char *)&inpcb.inp_faddr.s_addr;
  1125. X        op = newname + 15;
  1126. X        *op++ = *cp++;
  1127. X        *op++ = *cp++;
  1128. X        *op++ = *cp++;
  1129. X        *op++ = *cp++;
  1130. X        
  1131. X        newname[19] = ntohs(inpcb.inp_fport);
  1132. X
  1133. X        if (exact){
  1134. X        if (compare(newname, 20, name, *length) == 0){
  1135. X            bcopy((char *)newname, (char *)lowest, 20 * sizeof(oid));
  1136. X            LowState = State;
  1137. X            Lowinpcb = inpcb;
  1138. X            break;  /* no need to search further */
  1139. X        }
  1140. X        } else {
  1141. X        if ((compare(newname, 20, name, *length) > 0) &&
  1142. X             ((LowState < 0) || (compare(newname, 20, lowest, 20) < 0))){
  1143. X            /*
  1144. X             * if new one is greater than input and closer to input than
  1145. X             * previous lowest, save this one as the "next" one.
  1146. X             */
  1147. X            bcopy((char *)newname, (char *)lowest, 20 * sizeof(oid));
  1148. X            LowState = State;
  1149. X            Lowinpcb = inpcb;
  1150. X        }
  1151. X        }
  1152. X    }
  1153. X    if (LowState < 0) return(NULL);
  1154. X    bcopy((char *)lowest, (char *)name, (int)vp->namelen * sizeof(oid));
  1155. X    *length = vp->namelen;
  1156. X    *write_method = 0;
  1157. X    *var_len = sizeof(long);
  1158. X    switch (vp->magic) {
  1159. X        case TCPCONNSTATE: {
  1160. X        static int StateMap[]={1, 2, 3, 4, 5, 8, 6, 10, 9, 7, 11};
  1161. X        return (u_char *) &StateMap[LowState];
  1162. X        }
  1163. X        case TCPCONNLOCALADDRESS:
  1164. X        return (u_char *) &Lowinpcb.inp_laddr.s_addr;
  1165. X        case TCPCONNLOCALPORT:
  1166. X        long_return = ntohs(Lowinpcb.inp_lport);
  1167. X        return (u_char *) &long_return;
  1168. X        case TCPCONNREMADDRESS:
  1169. X        return (u_char *) &Lowinpcb.inp_faddr.s_addr;
  1170. X        case TCPCONNREMPORT:
  1171. X        long_return = ntohs(Lowinpcb.inp_fport);
  1172. X        return (u_char *) &long_return;
  1173. X    }
  1174. X    }
  1175. X    return NULL;
  1176. X}
  1177. X
  1178. X/*
  1179. X *    Print INTERNET connections
  1180. X */
  1181. X
  1182. Xstatic int TCP_Count_Connections()
  1183. X{
  1184. X    int Established;
  1185. X    struct inpcb cb;
  1186. X    register struct inpcb *prev, *next;
  1187. X    struct inpcb inpcb;
  1188. X    struct tcpcb tcpcb;
  1189. X
  1190. XAgain:    /*
  1191. X     *    Prepare to scan the control blocks
  1192. X     */
  1193. X    Established = 0;
  1194. X    klseek(nl[N_TCB].n_value);
  1195. X    klread((char *)&cb, sizeof(struct inpcb));
  1196. X    inpcb = cb;
  1197. X    prev = (struct inpcb *) nl[N_TCB].n_value;
  1198. X    /*
  1199. X     *    Scan the control blocks
  1200. X     */
  1201. X    while (inpcb.inp_next != (struct inpcb *) nl[N_TCB].n_value) {
  1202. X        next = inpcb.inp_next;
  1203. X        klseek((caddr_t)next);
  1204. X        klread((char *)&inpcb, sizeof (inpcb));
  1205. X        if (inpcb.inp_prev != prev) {        /* ??? */
  1206. X            sleep(1);
  1207. X            goto Again;
  1208. X        }
  1209. X        if (inet_lnaof(inpcb.inp_laddr) == INADDR_ANY) {
  1210. X            prev = next;
  1211. X            continue;
  1212. X        }
  1213. X        klseek((caddr_t)inpcb.inp_ppcb);
  1214. X        klread((char *)&tcpcb, sizeof (tcpcb));
  1215. X        if ((tcpcb.t_state == TCPS_ESTABLISHED) ||
  1216. X            (tcpcb.t_state == TCPS_CLOSE_WAIT))
  1217. X            Established++;
  1218. X        prev = next;
  1219. X    }
  1220. X    return(Established);
  1221. X}
  1222. X
  1223. X
  1224. Xstatic struct inpcb inpcb, *prev;
  1225. X
  1226. Xstatic TCP_Scan_Init()
  1227. X{
  1228. X    klseek(nl[N_TCB].n_value);
  1229. X    klread((char *)&inpcb, sizeof(inpcb));
  1230. X    prev = (struct inpcb *) nl[N_TCB].n_value;
  1231. X}
  1232. X
  1233. Xstatic int TCP_Scan_Next(State, RetInPcb)
  1234. Xint *State;
  1235. Xstruct inpcb *RetInPcb;
  1236. X{
  1237. X    register struct inpcb *next;
  1238. X    struct tcpcb tcpcb;
  1239. X
  1240. X    if (inpcb.inp_next == (struct inpcb *) nl[N_TCB].n_value) {
  1241. X        return(0);        /* "EOF" */
  1242. X    }
  1243. X
  1244. X    next = inpcb.inp_next;
  1245. X    klseek((caddr_t)next);
  1246. X    klread((char *)&inpcb, sizeof (inpcb));
  1247. X    if (inpcb.inp_prev != prev)       /* ??? */
  1248. X        return(-1); /* "FAILURE" */
  1249. X    klseek((caddr_t)inpcb.inp_ppcb);
  1250. X    klread((char *)&tcpcb, sizeof (tcpcb));
  1251. X    *State = tcpcb.t_state;
  1252. X    *RetInPcb = inpcb;
  1253. X    prev = next;
  1254. X    return(1);    /* "OK" */
  1255. X}
  1256. X
  1257. Xstatic int arptab_size, arptab_current;
  1258. Xstatic struct arptab *at=0;
  1259. Xstatic ARP_Scan_Init()
  1260. X{
  1261. X    if (!at) {
  1262. X        klseek(nl[N_ARPTAB_SIZE].n_value);
  1263. X        klread((char *)&arptab_size, sizeof arptab_size);
  1264. X
  1265. X        at = (struct arptab *) malloc(arptab_size * sizeof(struct arptab));
  1266. X    }
  1267. X
  1268. X    klseek(nl[N_ARPTAB].n_value);
  1269. X    klread((char *)at, arptab_size * sizeof(struct arptab));
  1270. X    arptab_current = 0;
  1271. X}
  1272. X
  1273. Xstatic int ARP_Scan_Next(IPAddr, PhysAddr)
  1274. Xu_long *IPAddr;
  1275. Xchar *PhysAddr;
  1276. X{
  1277. X    register struct arptab *atab;
  1278. X
  1279. X    while (arptab_current < arptab_size) {
  1280. X        atab = &at[arptab_current++];
  1281. X        if (!(atab->at_flags & ATF_COM)) continue;
  1282. X        *IPAddr = atab->at_iaddr.s_addr;
  1283. X        bcopy((char *)&atab->at_enaddr, PhysAddr, sizeof(atab->at_enaddr));
  1284. X        return(1);
  1285. X    }
  1286. X    return(0);        /* "EOF" */
  1287. X}
  1288. X
  1289. X
  1290. Xstatic struct ifnet *ifnetaddr, saveifnet, *saveifnetaddr;
  1291. Xstatic struct in_ifaddr savein_ifaddr;
  1292. Xstatic int saveIndex=0;
  1293. Xstatic char saveName[16];
  1294. X
  1295. XInterface_Scan_Init()
  1296. X{
  1297. X    klseek(nl[N_IFNET].n_value);
  1298. X    klread((char *)&ifnetaddr, sizeof ifnetaddr);
  1299. X    saveIndex=0;
  1300. X}
  1301. X
  1302. Xint Interface_Scan_Next(Index, Name, Retifnet, Retin_ifaddr)
  1303. Xint *Index;
  1304. Xchar *Name;
  1305. Xstruct ifnet *Retifnet;
  1306. Xstruct in_ifaddr *Retin_ifaddr;
  1307. X{
  1308. X    struct ifnet ifnet;
  1309. X    struct in_ifaddr *ia, in_ifaddr;
  1310. X    register char *cp;
  1311. X    extern char *index();
  1312. X
  1313. X    while (ifnetaddr) {
  1314. X        /*
  1315. X         *        Get the "ifnet" structure and extract the device name
  1316. X         */
  1317. X        klseek(ifnetaddr);
  1318. X        klread((char *)&ifnet, sizeof ifnet);
  1319. X        klseek((caddr_t)ifnet.if_name);
  1320. X        klread(saveName, 16);
  1321. X        saveName[15] = '\0';
  1322. X        cp = index(saveName, '\0');
  1323. X        *cp++ = ifnet.if_unit + '0';
  1324. X        *cp = '\0';
  1325. X        if (1 || strcmp(saveName,"lo0") != 0) {  /* XXX */
  1326. X        /*
  1327. X         *  Try to find an addres for this interface
  1328. X         */
  1329. X        klseek(nl[N_IN_IFADDR].n_value);
  1330. X        klread((char *) &ia, sizeof(ia));
  1331. X        while (ia) {
  1332. X            klseek(ia);
  1333. X            klread((char *) &in_ifaddr, sizeof(in_ifaddr));
  1334. X            if (in_ifaddr.ia_ifp == ifnetaddr) break;
  1335. X            ia = in_ifaddr.ia_next;
  1336. X        }
  1337. X
  1338. X        ifnet.if_addrlist = (struct ifaddr *)ia;     /* WRONG DATA TYPE; ONLY A FLAG */
  1339. X/*        ifnet.if_addrlist = (struct ifaddr *)&ia->ia_ifa;   */  /* WRONG DATA TYPE; ONLY A FLAG */
  1340. X        if (Index)
  1341. X            *Index = ++saveIndex;
  1342. X        if (Retifnet)
  1343. X            *Retifnet = ifnet;
  1344. X        if (Retin_ifaddr)
  1345. X            *Retin_ifaddr = in_ifaddr;
  1346. X        if (Name)
  1347. X            strcpy(Name, saveName);
  1348. X        saveifnet = ifnet;
  1349. X        saveifnetaddr = ifnetaddr;
  1350. X        savein_ifaddr = in_ifaddr;
  1351. X        ifnetaddr = ifnet.if_next;
  1352. X        return(1);    /* DONE */
  1353. X        }
  1354. X        ifnetaddr = ifnet.if_next;
  1355. X    }
  1356. X    return(0);        /* EOF */
  1357. X}
  1358. X
  1359. Xstatic int Interface_Scan_By_Index(Index, Name, Retifnet, Retin_ifaddr)
  1360. Xint Index;
  1361. Xchar *Name;
  1362. Xstruct ifnet *Retifnet;
  1363. Xstruct in_ifaddr *Retin_ifaddr;
  1364. X{
  1365. X    int i;
  1366. X
  1367. X    if (saveIndex != Index) {    /* Optimization! */
  1368. X        Interface_Scan_Init();
  1369. X        while (Interface_Scan_Next(&i, Name, Retifnet, Retin_ifaddr)) {
  1370. X        if (i == Index) break;
  1371. X        }
  1372. X        if (i != Index) return(-1);     /* Error, doesn't exist */
  1373. X    } else {
  1374. X        if (Retifnet)
  1375. X        *Retifnet = saveifnet;
  1376. X        if (Retin_ifaddr)
  1377. X        *Retin_ifaddr = savein_ifaddr;
  1378. X        if (Name)
  1379. X        strcpy(Name, saveName);
  1380. X    }
  1381. X    return(0);    /* DONE */
  1382. X}
  1383. X
  1384. Xstatic int Interface_Count=0;
  1385. X
  1386. Xstatic int Interface_Scan_Get_Count()
  1387. X{
  1388. X    if (!Interface_Count) {
  1389. X        Interface_Scan_Init();
  1390. X        while (Interface_Scan_Next((int *)0, (char *)0, (struct ifnet *)0, (struct in_ifaddr *)0) != 0)
  1391. X        Interface_Count++;
  1392. X    }
  1393. X    return(Interface_Count);
  1394. X}
  1395. X
  1396. Xstatic int Interface_Get_Ether_By_Index(Index, EtherAddr)
  1397. Xint Index;
  1398. Xchar *EtherAddr;
  1399. X{
  1400. X    int i;
  1401. X    struct arpcom arpcom;
  1402. X
  1403. X    if (saveIndex != Index) {    /* Optimization! */
  1404. X        Interface_Scan_Init();
  1405. X        while (Interface_Scan_Next(&i, (char *)0, (struct ifnet *)0, (struct in_ifaddr *)0)) {
  1406. X        if (i == Index) break;
  1407. X        }
  1408. X        if (i != Index) return(-1);     /* Error, doesn't exist */
  1409. X    }
  1410. X
  1411. X    /*
  1412. X     *  the arpcom structure is an extended ifnet structure which
  1413. X     *  contains the ethernet address.
  1414. X     */
  1415. X    klseek(saveifnetaddr);
  1416. X    klread((char *)&arpcom, sizeof arpcom);
  1417. X    bcopy((char *)&arpcom.ac_enaddr, EtherAddr, sizeof(arpcom.ac_enaddr));
  1418. X    return(0);    /* DONE */
  1419. X}
  1420. X
  1421. END_OF_FILE
  1422.   if test 46704 -ne `wc -c <'nocol-3.0/src/cmu-snmp/apps/snmp_vars.c'`; then
  1423.     echo shar: \"'nocol-3.0/src/cmu-snmp/apps/snmp_vars.c'\" unpacked with wrong size!
  1424.   fi
  1425.   # end of 'nocol-3.0/src/cmu-snmp/apps/snmp_vars.c'
  1426. fi
  1427. if test -f 'nocol-3.0/src/cmu-snmp/include/parse.c' -a "${1}" != "-c" ; then 
  1428.   echo shar: Will not clobber existing file \"'nocol-3.0/src/cmu-snmp/include/parse.c'\"
  1429. else
  1430.   echo shar: Extracting \"'nocol-3.0/src/cmu-snmp/include/parse.c'\" \(22322 characters\)
  1431.   sed "s/^X//" >'nocol-3.0/src/cmu-snmp/include/parse.c' <<'END_OF_FILE'
  1432. X/***********************************************************
  1433. X    Copyright 1989 by Carnegie Mellon University
  1434. X
  1435. X                      All Rights Reserved
  1436. X
  1437. XPermission to use, copy, modify, and distribute this software and its 
  1438. Xdocumentation for any purpose and without fee is hereby granted, 
  1439. Xprovided that the above copyright notice appear in all copies and that
  1440. Xboth that copyright notice and this permission notice appear in 
  1441. Xsupporting documentation, and that the name of CMU not be
  1442. Xused in advertising or publicity pertaining to distribution of the
  1443. Xsoftware without specific, written prior permission.  
  1444. X
  1445. XCMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  1446. XALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  1447. XCMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  1448. XANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  1449. XWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  1450. XARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  1451. XSOFTWARE.
  1452. X******************************************************************/
  1453. X/*
  1454. X * parse.c
  1455. X */
  1456. X#include <stdio.h>
  1457. X#include <ctype.h>
  1458. X#include <sys/types.h>
  1459. X#include "parse.h"
  1460. X
  1461. X/*
  1462. X * This is one element of an object identifier with either an integer subidentifier,
  1463. X * or a textual string label, or both.
  1464. X * The subid is -1 if not present, and label is NULL if not present.
  1465. X */
  1466. Xstruct subid {
  1467. X    int subid;
  1468. X    char *label;
  1469. X};
  1470. X
  1471. Xint Line = 1;
  1472. X
  1473. X/* types of tokens */
  1474. X#define    CONTINUE    -1
  1475. X#define LABEL        1
  1476. X#define SUBTREE        2
  1477. X#define SYNTAX        3
  1478. X#define OBJID        4
  1479. X#define OCTETSTR    5
  1480. X#define INTEGER        6
  1481. X#define NETADDR        7
  1482. X#define    IPADDR        8
  1483. X#define COUNTER        9
  1484. X#define GAUGE        10
  1485. X#define TIMETICKS   11
  1486. X#define OPAQUE        12
  1487. X#define NUL        13
  1488. X#define SEQUENCE    14
  1489. X#define OF        15    /* SEQUENCE OF */
  1490. X#define OBJTYPE        16
  1491. X#define ACCESS        17
  1492. X#define READONLY    18
  1493. X#define READWRITE   19
  1494. X#define    WRITEONLY   20
  1495. X#define NOACCESS    21
  1496. X#define STATUS        22
  1497. X#define MANDATORY   23
  1498. X#define OPTIONAL    24
  1499. X#define OBSOLETE    25
  1500. X#define RECOMMENDED 26
  1501. X#define PUNCT        27
  1502. X#define EQUALS        28
  1503. X
  1504. Xstruct tok {
  1505. X    char *name;            /* token name */
  1506. X    int len;            /* length not counting nul */
  1507. X    int token;            /* value */
  1508. X    int hash;            /* hash of name */
  1509. X    struct tok *next;        /* pointer to next in hash table */
  1510. X};
  1511. X
  1512. X
  1513. Xstruct tok tokens[] = {
  1514. X    { "obsolete", sizeof ("obsolete")-1, OBSOLETE },
  1515. X    { "Opaque", sizeof ("Opaque")-1, OPAQUE },
  1516. X    { "recommended", sizeof("recommended")-1, RECOMMENDED }, 
  1517. X    { "optional", sizeof ("optional")-1, OPTIONAL },
  1518. X    { "mandatory", sizeof ("mandatory")-1, MANDATORY },
  1519. X    { "not-accessible", sizeof ("not-accessible")-1, NOACCESS },
  1520. X    { "write-only", sizeof ("write-only")-1, WRITEONLY },
  1521. X    { "read-write", sizeof ("read-write")-1, READWRITE },
  1522. X    { "TimeTicks", sizeof ("TimeTicks")-1, TIMETICKS },
  1523. X    { "OBJECTIDENTIFIER", sizeof ("OBJECTIDENTIFIER")-1, OBJID },
  1524. X    /*
  1525. X     * This CONTINUE appends the next word onto OBJECT,
  1526. X     * hopefully matching OBJECTIDENTIFIER above.
  1527. X     */
  1528. X    { "OBJECT", sizeof ("OBJECT")-1, CONTINUE },
  1529. X    { "NetworkAddress", sizeof ("NetworkAddress")-1, NETADDR },
  1530. X    { "Gauge", sizeof ("Gauge")-1, GAUGE },
  1531. X    { "OCTETSTRING", sizeof ("OCTETSTRING")-1, OCTETSTR },
  1532. X    { "OCTET", sizeof ("OCTET")-1, -1 },
  1533. X    { "OF", sizeof ("OF")-1, OF },
  1534. X    { "SEQUENCE", sizeof ("SEQUENCE")-1, SEQUENCE },
  1535. X    { "NULL", sizeof ("NULL")-1, NUL },
  1536. X    { "IpAddress", sizeof ("IpAddress")-1, IPADDR },
  1537. X    { "INTEGER", sizeof ("INTEGER")-1, INTEGER },
  1538. X    { "Counter", sizeof ("Counter")-1, COUNTER },
  1539. X    { "read-only", sizeof ("read-only")-1, READONLY },
  1540. X    { "ACCESS", sizeof ("ACCESS")-1, ACCESS },
  1541. X    { "STATUS", sizeof ("STATUS")-1, STATUS },
  1542. X    { "SYNTAX", sizeof ("SYNTAX")-1, SYNTAX },
  1543. X    { "OBJECT-TYPE", sizeof ("OBJECT-TYPE")-1, OBJTYPE },
  1544. X    { "{", sizeof ("{")-1, PUNCT },
  1545. X    { "}", sizeof ("}")-1, PUNCT },
  1546. X    { "::=", sizeof ("::=")-1, EQUALS },
  1547. X    { NULL }
  1548. X};
  1549. X
  1550. X#define    HASHSIZE    32
  1551. X#define    BUCKET(x)    (x & 0x01F)
  1552. X
  1553. Xstruct tok    *buckets[HASHSIZE];
  1554. X
  1555. Xstatic
  1556. Xhash_init()
  1557. X{
  1558. X    register struct tok    *tp;
  1559. X    register char    *cp;
  1560. X    register int    h;
  1561. X    register int    b;
  1562. X
  1563. X    for (tp = tokens; tp->name; tp++) {
  1564. X        for (h = 0, cp = tp->name; *cp; cp++)
  1565. X            h += *cp;
  1566. X        tp->hash = h;
  1567. X        b = BUCKET(h);
  1568. X        if (buckets[b])
  1569. X            tp->next = buckets[b];
  1570. X        buckets[b] = tp;
  1571. X    }
  1572. X}
  1573. X
  1574. X
  1575. Xstatic char *
  1576. XMalloc(num)
  1577. X    unsigned num;
  1578. X{
  1579. X    char *cp;
  1580. X    char *malloc();
  1581. X    
  1582. X    /* this is to fix (what seems to be) a problem with the IBM RT C library malloc */
  1583. X    if (num < 16)
  1584. X    num = 16;
  1585. X    cp = malloc(num);
  1586. X    return cp;
  1587. X}
  1588. X
  1589. Xstatic
  1590. Xprint_error(string, token)
  1591. X    char *string;
  1592. X    char *token;
  1593. X{
  1594. X    if (token)
  1595. X    fprintf(stderr, "%s(%s): On or around line %d\n", string, token, Line);
  1596. X    else
  1597. X    fprintf(stderr, "%s: On or around line %d\n", string, Line);
  1598. X}
  1599. X
  1600. X#ifdef TEST
  1601. Xprint_subtree(tree, count)
  1602. X    struct tree *tree;
  1603. X    int count;
  1604. X{
  1605. X    struct tree *tp;
  1606. X    int i;
  1607. X
  1608. X    for(i = 0; i < count; i++)
  1609. X    printf("  ");
  1610. X    printf("Children of %s:\n", tree->label);
  1611. X    count++;
  1612. X    for(tp = tree->child_list; tp; tp = tp->next_peer){
  1613. X    for(i = 0; i < count; i++)
  1614. X        printf("  ");
  1615. X    printf("%s\n", tp->label);
  1616. X    }
  1617. X    for(tp = tree->child_list; tp; tp = tp->next_peer){
  1618. X    print_subtree(tp, count);
  1619. X    }
  1620. X}
  1621. X#endif /* TEST */
  1622. X
  1623. X
  1624. Xstatic struct tree *
  1625. Xbuild_tree(nodes)
  1626. X    struct node *nodes;
  1627. X{
  1628. X    struct node *np;
  1629. X    struct tree *tp;
  1630. X    
  1631. X    /* build root node */
  1632. X    tp = (struct tree *)Malloc(sizeof(struct tree));
  1633. X    tp->parent = NULL;
  1634. X    tp->next_peer = NULL;
  1635. X    tp->child_list = NULL;
  1636. X    tp->enums = NULL;
  1637. X    strcpy(tp->label, "iso");
  1638. X    tp->subid = 1;
  1639. X    tp->type = 0;
  1640. X    /* grow tree from this root node */
  1641. X    do_subtree(tp, &nodes);
  1642. X#ifdef TEST
  1643. X    print_subtree(tp, 0);
  1644. X#endif /* TEST */
  1645. X    /* If any nodes are left, the tree is probably inconsistent */
  1646. X    if (nodes){
  1647. X    fprintf(stderr, "The mib description doesn't seem to be consistent.\n");
  1648. X    fprintf(stderr, "Some nodes couldn't be linked under the \"iso\" tree.\n");
  1649. X    fprintf(stderr, "these nodes are left:\n");
  1650. X    for(np = nodes; np; np = np->next)
  1651. X        fprintf(stderr, "%s ::= { %s %d } (%d)\n", np->label, np->parent, np->subid,
  1652. X            np->type);
  1653. X    }
  1654. X    return tp;
  1655. X}
  1656. X
  1657. X
  1658. X/*
  1659. X * Find all the children of root in the list of nodes.  Link them into the
  1660. X * tree and out of the nodes list.
  1661. X */
  1662. Xstatic
  1663. Xdo_subtree(root, nodes)
  1664. X    struct tree *root;
  1665. X    struct node **nodes;
  1666. X{
  1667. X    register struct tree *tp;
  1668. X    struct tree *peer = NULL;
  1669. X    register struct node *np;
  1670. X    struct node *oldnp = NULL, *child_list = NULL, *childp = NULL;
  1671. X    
  1672. X    tp = root;
  1673. X    /*
  1674. X     * Search each of the nodes for one whose parent is root, and
  1675. X     * move each into a separate list.
  1676. X     */
  1677. X    for(np = *nodes; np; np = np->next){
  1678. X    if ((tp->label[0] == np->parent[0]) && !strcmp(tp->label, np->parent)){
  1679. X        if (child_list == NULL){
  1680. X        child_list = childp = np;   /* first entry in child list */
  1681. X        } else {
  1682. X        childp->next = np;
  1683. X        childp = np;
  1684. X        }
  1685. X        /* take this node out of the node list */
  1686. X        if (oldnp == NULL){
  1687. X        *nodes = np->next;  /* fix root of node list */
  1688. X        } else {
  1689. X        oldnp->next = np->next;    /* link around this node */
  1690. X        }
  1691. X    } else {
  1692. X        oldnp = np;
  1693. X    }
  1694. X    }
  1695. X    if (childp)
  1696. X    childp->next = 0;    /* re-terminate list */
  1697. X    /*
  1698. X     * Take each element in the child list and place it into the tree.
  1699. X     */
  1700. X    for(np = child_list; np; np = np->next){
  1701. X    tp = (struct tree *)Malloc(sizeof(struct tree));
  1702. X    tp->parent = root;
  1703. X    tp->next_peer = NULL;
  1704. X    tp->child_list = NULL;
  1705. X    strcpy(tp->label, np->label);
  1706. X    tp->subid = np->subid;
  1707. X    switch(np->type){
  1708. X        case OBJID:
  1709. X        tp->type = TYPE_OBJID;
  1710. X        break;
  1711. X        case OCTETSTR:
  1712. X        tp->type = TYPE_OCTETSTR;
  1713. X        break;
  1714. X        case INTEGER:
  1715. X        tp->type = TYPE_INTEGER;
  1716. X        break;
  1717. X        case NETADDR:
  1718. X        tp->type = TYPE_IPADDR;
  1719. X        break;
  1720. X        case IPADDR:
  1721. X        tp->type = TYPE_IPADDR;
  1722. X        break;
  1723. X        case COUNTER:
  1724. X        tp->type = TYPE_COUNTER;
  1725. X        break;
  1726. X        case GAUGE:
  1727. X        tp->type = TYPE_GAUGE;
  1728. X        break;
  1729. X        case TIMETICKS:
  1730. X        tp->type = TYPE_TIMETICKS;
  1731. X        break;
  1732. X        case OPAQUE:
  1733. X        tp->type = TYPE_OPAQUE;
  1734. X        break;
  1735. X        case NUL:
  1736. X        tp->type = TYPE_NULL;
  1737. X        break;
  1738. X        default:
  1739. X        tp->type = TYPE_OTHER;
  1740. X        break;
  1741. X    }
  1742. X    tp->enums = np->enums;
  1743. X    np->enums = NULL;    /* so we don't free them later */
  1744. X    if (root->child_list == NULL){
  1745. X        root->child_list = tp;
  1746. X    } else {
  1747. X        peer->next_peer = tp;
  1748. X    }
  1749. X    peer = tp;
  1750. X    do_subtree(tp, nodes);    /* recurse on this child */
  1751. X    }
  1752. X    /* free all nodes that were copied into tree */
  1753. X    for(np = child_list; np;){
  1754. X    oldnp = np;
  1755. X    np = np->next;
  1756. X    free_node(oldnp);
  1757. X    }
  1758. X}
  1759. X
  1760. X
  1761. X/*
  1762. X * Takes a list of the form:
  1763. X * { iso org(3) dod(6) 1 }
  1764. X * and creates several nodes, one for each parent-child pair.
  1765. X * Returns NULL on error.
  1766. X */
  1767. Xstatic int
  1768. Xgetoid(fp, oid,  length)
  1769. X    register FILE *fp;
  1770. X    register struct subid *oid;    /* an array of subids */
  1771. X    int length;        /* the length of the array */
  1772. X{
  1773. X    register int count;
  1774. X    int type;
  1775. X    char token[64], label[32];
  1776. X    register char *cp, *tp;
  1777. X
  1778. X    if ((type = get_token(fp, token)) != PUNCT){
  1779. X    if (type == -1)
  1780. X        print_error("Unexpected EOF", (char *)NULL);
  1781. X    else
  1782. X        print_error("Unexpected", token);
  1783. X    return NULL;
  1784. X    }
  1785. X    if (*token != '{'){
  1786. X    print_error("Unexpected", token);
  1787. X    return NULL;
  1788. X    }
  1789. X    for(count = 0; count < length; count++, oid++){
  1790. X    oid->label = 0;
  1791. X    oid->subid = -1;
  1792. X    if ((type = get_token(fp, token)) != LABEL){
  1793. X        if (type == -1){
  1794. X        print_error("Unexpected EOF", (char *)NULL);
  1795. X        return NULL;
  1796. X        }
  1797. X        else if (type == PUNCT && *token == '}'){
  1798. X        return count;
  1799. X        } else {
  1800. X        print_error("Unexpected", token);
  1801. X        return NULL;
  1802. X        }
  1803. X    }
  1804. X    tp = token;
  1805. X    if (!isdigit(*tp)){
  1806. X        /* this entry has a label */
  1807. X        cp = label;
  1808. X        while(*tp && *tp != '(')
  1809. X        *cp++ = *tp++;
  1810. X        *cp = 0;
  1811. X        cp = (char *)Malloc((unsigned)strlen(label));
  1812. X        strcpy(cp, label);
  1813. X        oid->label = cp;
  1814. X        if (*tp == '('){
  1815. X        /* this entry has a label-integer pair in the form label(integer). */
  1816. X        cp = ++tp;
  1817. X        while(*cp && *cp != ')')
  1818. X            cp++;
  1819. X        if (*cp == ')')
  1820. X            *cp = 0;
  1821. X        else {
  1822. X            print_error("No terminating parenthesis", (char *)NULL);
  1823. X            return NULL;
  1824. X        }
  1825. X        oid->subid = atoi(tp);
  1826. X        }
  1827. X    } else {
  1828. X        /* this entry  has just an integer sub-identifier */
  1829. X        oid->subid = atoi(tp);
  1830. X    }
  1831. X    }
  1832. X    return count;
  1833. X
  1834. X
  1835. X}
  1836. X
  1837. Xstatic
  1838. Xfree_node(np)
  1839. X    struct node *np;
  1840. X{
  1841. X    struct enum_list *ep, *tep;
  1842. X
  1843. X    ep = np->enums;
  1844. X    while(ep){
  1845. X    tep = ep;
  1846. X    ep = ep->next;
  1847. X    free((char *)tep);
  1848. X    }
  1849. X    free((char *)np);
  1850. X}
  1851. X
  1852. X/*
  1853. X * Parse an entry of the form:
  1854. X * label OBJECT IDENTIFIER ::= { parent 2 }
  1855. X * The "label OBJECT IDENTIFIER" portion has already been parsed.
  1856. X * Returns 0 on error.
  1857. X */
  1858. Xstatic struct node *
  1859. Xparse_objectid(fp, name)
  1860. X    FILE *fp;
  1861. X    char *name;
  1862. X{
  1863. X    int type;
  1864. X    char token[64];
  1865. X    register int count;
  1866. X    register struct subid *op, *nop;
  1867. X    int length;
  1868. X    struct subid oid[16];
  1869. X    struct node *np, *root, *oldnp = NULL;
  1870. X
  1871. X    type = get_token(fp, token);
  1872. X    if (type != EQUALS){
  1873. X    print_error("Bad format", token);
  1874. X    return 0;
  1875. X    }
  1876. X    if (length = getoid(fp, oid, 16)){
  1877. X    np = root = (struct node *)Malloc(sizeof(struct node));
  1878. X    /*
  1879. X     * For each parent-child subid pair in the subid array,
  1880. X     * create a node and link it into the node list.
  1881. X     */
  1882. X    for(count = 0, op = oid, nop=oid+1; count < (length - 2); count++,
  1883. X        op++, nop++){
  1884. X        /* every node must have parent's name and child's name or number */
  1885. X        if (op->label && (nop->label || (nop->subid != -1))){
  1886. X        strcpy(np->parent, op->label);
  1887. X        if (nop->label)
  1888. X            strcpy(np->label, nop->label);
  1889. X        if (nop->subid != -1)
  1890. X            np->subid = nop->subid;
  1891. X        np ->type = 0;
  1892. X        np->enums = 0;
  1893. X        /* set up next entry */
  1894. X        np->next = (struct node *)Malloc(sizeof(*np->next));
  1895. X        oldnp = np;
  1896. X        np = np->next;
  1897. X        }
  1898. X    }
  1899. X    np->next = (struct node *)NULL;
  1900. X    /*
  1901. X     * The above loop took care of all but the last pair.  This pair is taken
  1902. X     * care of here.  The name for this node is taken from the label for this
  1903. X     * entry.
  1904. X     * np still points to an unused entry.
  1905. X     */
  1906. X    if (count == (length - 2)){
  1907. X        if (op->label){
  1908. X        strcpy(np->parent, op->label);
  1909. X        strcpy(np->label, name);
  1910. X        if (nop->subid != -1)
  1911. X            np->subid = nop->subid;
  1912. X        else
  1913. X            print_error("Warning: This entry is pretty silly", token);
  1914. X        } else {
  1915. X        free_node(np);
  1916. X        if (oldnp)
  1917. X            oldnp->next = NULL;
  1918. X        else
  1919. X            return NULL;
  1920. X        }
  1921. X    } else {
  1922. X        print_error("Missing end of oid", (char *)NULL);
  1923. X        free_node(np);   /* the last node allocated wasn't used */
  1924. X        if (oldnp)
  1925. X        oldnp->next = NULL;
  1926. X        return NULL;
  1927. X    }
  1928. X    /* free the oid array */
  1929. X    for(count = 0, op = oid; count < length; count++, op++){
  1930. X        if (op->label)
  1931. X        free(oid->label);
  1932. X        op->label = 0;
  1933. X    }
  1934. X    return root;
  1935. X    } else {
  1936. X    print_error("Bad object identifier", (char *)NULL);
  1937. X    return 0;
  1938. X    }
  1939. X}
  1940. X
  1941. X/*
  1942. X * Parses an asn type.  This structure is ignored by this parser.
  1943. X * Returns NULL on error.
  1944. X */
  1945. Xstatic int
  1946. Xparse_asntype(fp)
  1947. X    FILE *fp;
  1948. X{
  1949. X    int type;
  1950. X    char token[64];
  1951. X
  1952. X    type = get_token(fp, token);
  1953. X    if (type != SEQUENCE){
  1954. X    print_error("Not a sequence", (char *)NULL); /* should we handle this */
  1955. X    return NULL;
  1956. X    }
  1957. X    while((type = get_token(fp, token)) != NULL){
  1958. X    if (type == -1)
  1959. X        return NULL;
  1960. X    if (type == PUNCT && (token[0] == '}' && token[1] == '\0'))
  1961. X        return -1;
  1962. X    }
  1963. X    print_error("Premature end of file", (char *)NULL);
  1964. X    return NULL;
  1965. X}
  1966. X
  1967. X/*
  1968. X * Parses an OBJECT TYPE macro.
  1969. X * Returns 0 on error.
  1970. X */
  1971. Xstatic struct node *
  1972. Xparse_objecttype(fp, name)
  1973. X    register FILE *fp;
  1974. X    char *name;
  1975. X{
  1976. X    register int type;
  1977. X    char token[64];
  1978. X    int count, length;
  1979. X    struct subid oid[16];
  1980. X    char syntax[32];
  1981. X    int nexttype;
  1982. X    char nexttoken[64];
  1983. X    register struct node *np;
  1984. X    register struct enum_list *ep;
  1985. X    register char *cp;
  1986. X    register char *tp;
  1987. X
  1988. X    type = get_token(fp, token);
  1989. X    if (type != SYNTAX){
  1990. X    print_error("Bad format for OBJECT TYPE", token);
  1991. X    return 0;
  1992. X    }
  1993. X    np = (struct node *)Malloc(sizeof(struct node));
  1994. X    np->next = 0;
  1995. X    np->enums = 0;
  1996. X    type = get_token(fp, token);
  1997. X    nexttype = get_token(fp, nexttoken);
  1998. X    np->type = type;
  1999. X    switch(type){
  2000. X    case SEQUENCE:
  2001. X        strcpy(syntax, token);
  2002. X        if (nexttype == OF){
  2003. X        strcat(syntax, " ");
  2004. X        strcat(syntax, nexttoken);
  2005. X        nexttype = get_token(fp, nexttoken);
  2006. X        strcat(syntax, " ");
  2007. X        strcat(syntax, nexttoken);
  2008. X        nexttype = get_token(fp, nexttoken);
  2009. X        }
  2010. X        break;
  2011. X    case INTEGER:
  2012. X        strcpy(syntax, token);
  2013. X        if (nexttype == PUNCT &&
  2014. X        (nexttoken[0] == '{' && nexttoken[1] == '\0')) {
  2015. X        /* if there is an enumeration list, parse it */
  2016. X        while((type = get_token(fp, token)) != NULL){
  2017. X            if (type == -1){
  2018. X            free_node(np);
  2019. X            return 0;
  2020. X            }
  2021. X            if (type == PUNCT &&
  2022. X            (token[0] == '}' && token[1] == '\0'))
  2023. X            break;
  2024. X            if (type == 1){
  2025. X            /* this is an enumerated label */
  2026. X            if (np->enums == 0){
  2027. X                ep = np->enums = (struct enum_list *)
  2028. X                    Malloc(sizeof(struct enum_list));
  2029. X            } else {
  2030. X                ep->next = (struct enum_list *)
  2031. X                    Malloc(sizeof(struct enum_list));
  2032. X                ep = ep->next;
  2033. X            }
  2034. X            ep->next = 0;
  2035. X            /* a reasonable approximation for the length */
  2036. X            ep->label = (char *)Malloc((unsigned)strlen(token));
  2037. X            cp = ep->label;
  2038. X            tp = token;
  2039. X            while(*tp != '(' && *tp != 0)
  2040. X                *cp++ = *tp++;
  2041. X            *cp = 0;
  2042. X            if (*tp == 0){
  2043. X                type = get_token(fp, token);
  2044. X                if (type != LABEL){
  2045. X                print_error("Expected \"(\"", (char *)NULL);
  2046. X                free_node(np);
  2047. X                return 0;
  2048. X                }
  2049. X                tp = token;
  2050. X            }
  2051. X            if (*tp == '('){
  2052. X                tp++;
  2053. X            } else {
  2054. X                print_error("Expected \"(\"", token);
  2055. X                free_node(np);
  2056. X                return 0;
  2057. X            }
  2058. X            if (*tp == 0){
  2059. X                type = get_token(fp, token);
  2060. X                if (type != LABEL){
  2061. X                print_error("Expected integer", token);
  2062. X                free_node(np);
  2063. X                return 0;
  2064. X                }
  2065. X                tp = token;
  2066. X            }
  2067. X
  2068. X            cp = tp;
  2069. X            if (!isdigit(*cp)){
  2070. X                print_error("Expected integer", token);
  2071. X                free_node(np);
  2072. X                return 0;
  2073. X            }
  2074. X            while(isdigit(*tp) && *tp != 0)
  2075. X                tp++;
  2076. X            if (*tp == ')')
  2077. X                *tp = '\0';    /* terminate number */
  2078. X            else if (*tp == 0){
  2079. X                type = get_token(fp, token);
  2080. X                if (type != LABEL || *token != ')'){
  2081. X                print_error("Expected \")\"", token);
  2082. X                free_node(np);
  2083. X                return 0;
  2084. X                }
  2085. X            } else {
  2086. X                print_error("Expected \")\"", token);
  2087. X                free_node(np);
  2088. X                return 0;
  2089. X            }
  2090. X            ep->value = atoi(cp);
  2091. X            }
  2092. X        }
  2093. X        if (type == NULL){
  2094. X            print_error("Premature end of file", (char *)NULL);
  2095. X            free_node(np);
  2096. X            return 0;
  2097. X        }
  2098. X        nexttype = get_token(fp, nexttoken);
  2099. X        } else if (nexttype == LABEL && *nexttoken == '('){
  2100. X        /* ignore the "constrained integer" for now */
  2101. X        nexttype = get_token(fp, nexttoken);
  2102. X        }
  2103. X        break;
  2104. X    case OBJID:
  2105. X    case OCTETSTR:
  2106. X    case NETADDR:
  2107. X    case IPADDR:
  2108. X    case COUNTER:
  2109. X    case GAUGE:
  2110. X    case TIMETICKS:
  2111. X    case OPAQUE:
  2112. X    case NUL:
  2113. X    case LABEL:
  2114. X        strcpy(syntax, token);
  2115. X        break;
  2116. X    default:
  2117. X        print_error("Bad syntax", token);
  2118. X        free_node(np);
  2119. X        return 0;
  2120. X    }
  2121. X    if (nexttype != ACCESS){
  2122. X    print_error("Should be ACCESS", nexttoken);
  2123. X    free_node(np);
  2124. X    return 0;
  2125. X    }
  2126. X    type = get_token(fp, token);
  2127. X    if (type != READONLY && type != READWRITE && type != WRITEONLY
  2128. X    && type != NOACCESS){
  2129. X    print_error("Bad access type", nexttoken);
  2130. X    free_node(np);
  2131. X    return 0;
  2132. X    }
  2133. X    type = get_token(fp, token);
  2134. X    if (type != STATUS){
  2135. X    print_error("Should be STATUS", token);
  2136. X    free_node(np);
  2137. X    return 0;
  2138. X    }
  2139. X    type = get_token(fp, token);
  2140. X    if (type != MANDATORY && type != OPTIONAL && type != OBSOLETE && type != RECOMMENDED){
  2141. X    print_error("Bad status", token);
  2142. X    free_node(np);
  2143. X    return 0;
  2144. X    }
  2145. X    type = get_token(fp, token);
  2146. X    if (type != EQUALS){
  2147. X    print_error("Bad format", token);
  2148. X    free_node(np);
  2149. X    return 0;
  2150. X    }
  2151. X    length = getoid(fp, oid, 16);
  2152. X    if (length > 1 && length <= 16){
  2153. X    /* just take the last pair in the oid list */
  2154. X    if (oid[length - 2].label)
  2155. X        strncpy(np->parent, oid[length - 2].label, 32);
  2156. X    strcpy(np->label, name);
  2157. X    if (oid[length - 1].subid != -1)
  2158. X        np->subid = oid[length - 1].subid;
  2159. X    else
  2160. X        print_error("Warning: This entry is pretty silly", (char *)NULL);
  2161. X    } else {
  2162. X    print_error("No end to oid", (char *)NULL);
  2163. X    free_node(np);
  2164. X    np = 0;
  2165. X    }
  2166. X    /* free oid array */
  2167. X    for(count = 0; count < length; count++){
  2168. X    if (oid[count].label)
  2169. X        free(oid[count].label);
  2170. X    oid[count].label = 0;
  2171. X    }
  2172. X    return np;
  2173. X}
  2174. X
  2175. X
  2176. X/*
  2177. X * Parses a mib file and returns a linked list of nodes found in the file.
  2178. X * Returns NULL on error.
  2179. X */
  2180. Xstatic struct node *
  2181. Xparse(fp)
  2182. X    FILE *fp;
  2183. X{
  2184. X    char token[64];
  2185. X    char name[32];
  2186. X    int    type = 1;
  2187. X    struct node *np, *root = NULL;
  2188. X
  2189. X    hash_init();
  2190. X
  2191. X    while(type != NULL){
  2192. X    type = get_token(fp, token);
  2193. X    if (type != LABEL){
  2194. X        if (type == NULL){
  2195. X        return root;
  2196. X        }
  2197. X        print_error(token, "is a reserved word");
  2198. X        return NULL;
  2199. X    }
  2200. X    strncpy(name, token, 32);
  2201. X    type = get_token(fp, token);
  2202. X    if (type == OBJTYPE){
  2203. X        if (root == NULL){
  2204. X        /* first link in chain */
  2205. X        np = root = parse_objecttype(fp, name);
  2206. X        if (np == NULL){
  2207. X            print_error("Bad parse of object type", (char *)NULL);
  2208. X            return NULL;
  2209. X        }
  2210. X        } else {
  2211. X        np->next = parse_objecttype(fp, name);
  2212. X        if (np->next == NULL){
  2213. X            print_error("Bad parse of objecttype", (char *)NULL);
  2214. X            return NULL;
  2215. X        }
  2216. X        }
  2217. X        /* now find end of chain */
  2218. X        while(np->next)
  2219. X        np = np->next;
  2220. X    } else if (type == OBJID){
  2221. X        if (root == NULL){
  2222. X        /* first link in chain */
  2223. X        np = root = parse_objectid(fp, name);
  2224. X        if (np == NULL){
  2225. X            print_error("Bad parse of object id", (char *)NULL);
  2226. X            return NULL;
  2227. X        }
  2228. X        } else {
  2229. X        np->next = parse_objectid(fp, name);
  2230. X        if (np->next == NULL){
  2231. X            print_error("Bad parse of object type", (char *)NULL);
  2232. X            return NULL;
  2233. X        }
  2234. X        }
  2235. X        /* now find end of chain */
  2236. X        while(np->next)
  2237. X        np = np->next;
  2238. X    } else if (type == EQUALS){
  2239. X        type = parse_asntype(fp);
  2240. X    } else if (type == NULL){
  2241. X        break;
  2242. X    } else {
  2243. X        print_error("Bad operator", (char *)NULL);
  2244. X        return NULL;
  2245. X    }
  2246. X    }
  2247. X#ifdef TEST
  2248. X{
  2249. X    struct enum_list *ep;
  2250. X    
  2251. X    for(np = root; np; np = np->next){
  2252. X    printf("%s ::= { %s %d } (%d)\n", np->label, np->parent, np->subid,
  2253. X        np->type);
  2254. X    if (np->enums){
  2255. X        printf("Enums: \n");
  2256. X        for(ep = np->enums; ep; ep = ep->next){
  2257. X        printf("%s(%d)\n", ep->label, ep->value);
  2258. X        }
  2259. X    }
  2260. X    }
  2261. X}
  2262. X#endif /* TEST */
  2263. X    return root;
  2264. X}
  2265. X
  2266. X/*
  2267. X * Parses a token from the file.  The type of the token parsed is returned,
  2268. X * and the text is placed in the string pointed to by token.
  2269. X */
  2270. Xstatic int
  2271. Xget_token(fp, token)
  2272. X    register FILE *fp;
  2273. X    register char *token;
  2274. X{
  2275. X    static char last = ' ';
  2276. X    register int ch;
  2277. X    register char *cp = token;
  2278. X    register int hash = 0;
  2279. X    register struct tok *tp;
  2280. X
  2281. X    *cp = 0;
  2282. X    ch = last;
  2283. X    /* skip all white space */
  2284. X    while(isspace(ch) && ch != -1){
  2285. X    ch = getc(fp);
  2286. X    if (ch == '\n')
  2287. X        Line++;
  2288. X    }
  2289. X    if (ch == -1)
  2290. X    return NULL;
  2291. X
  2292. X    /*
  2293. X     * Accumulate characters until white space is found.  Then attempt to match this
  2294. X     * token as a reserved word.  If a match is found, return the type.  Else it is
  2295. X     * a label.
  2296. X     */
  2297. X    do {
  2298. X    if (!isspace(ch)){
  2299. X        hash += ch;
  2300. X        *cp++ = ch;
  2301. X        if (ch == '\n')
  2302. X        Line++;
  2303. X    } else {
  2304. X        last = ch;
  2305. X        *cp = '\0';
  2306. X
  2307. X        for (tp = buckets[BUCKET(hash)]; tp; tp = tp->next) {
  2308. X        if ((tp->hash == hash) && (strcmp(tp->name, token) == 0))
  2309. X            break;
  2310. X        }
  2311. X        if (tp){
  2312. X        if (tp->token == CONTINUE)
  2313. X            continue;
  2314. X        return (tp->token);
  2315. X        }
  2316. X
  2317. X        if (token[0] == '-' && token[1] == '-'){
  2318. X        /* strip comment */
  2319. X        while ((ch = getc(fp)) != -1)
  2320. X            if (ch == '\n'){
  2321. X            Line++;
  2322. X            break;
  2323. X            }
  2324. X        if (ch == -1)
  2325. X            return NULL;
  2326. X        last = ch;
  2327. X        return get_token(fp, token);        
  2328. X        }
  2329. X        return LABEL;
  2330. X    }
  2331. X    
  2332. X    } while ((ch = getc(fp)) != -1);
  2333. X    return NULL;
  2334. X}
  2335. X
  2336. Xstruct tree *
  2337. Xread_mib(filename)
  2338. X    char *filename;
  2339. X{
  2340. X    FILE *fp;
  2341. X    struct node *nodes;
  2342. X    struct tree *tree;
  2343. X    struct node *parse();
  2344. X
  2345. X    fp = fopen(filename, "r");
  2346. X    if (fp == NULL)
  2347. X    return NULL;
  2348. X    nodes = parse(fp);
  2349. X    if (!nodes){
  2350. X    fprintf(stderr, "Mib table is bad.  Exiting\n");
  2351. X    exit(1);
  2352. X    }
  2353. X    tree = build_tree(nodes);
  2354. X    fclose(fp);
  2355. X    return tree;
  2356. X}
  2357. X
  2358. X
  2359. X#ifdef TEST
  2360. Xmain(argc, argv)
  2361. X    int argc;
  2362. X    char *argv[];
  2363. X{
  2364. X    FILE *fp;
  2365. X    struct node *nodes;
  2366. X    struct tree *tp;
  2367. X
  2368. X    fp = fopen("mib.txt", "r");
  2369. X    if (fp == NULL){
  2370. X    fprintf(stderr, "open failed\n");
  2371. X    return 1;
  2372. X    }
  2373. X    nodes = parse(fp);
  2374. X    tp = build_tree(nodes);
  2375. X    print_subtree(tp, 0);
  2376. X    fclose(fp);
  2377. X}
  2378. X
  2379. X#endif /* TEST */
  2380. END_OF_FILE
  2381.   if test 22322 -ne `wc -c <'nocol-3.0/src/cmu-snmp/include/parse.c'`; then
  2382.     echo shar: \"'nocol-3.0/src/cmu-snmp/include/parse.c'\" unpacked with wrong size!
  2383.   fi
  2384.   # end of 'nocol-3.0/src/cmu-snmp/include/parse.c'
  2385. fi
  2386. if test -f 'nocol-3.0/src/cmu-snmp/snmplib/snmp.h' -a "${1}" != "-c" ; then 
  2387.   echo shar: Will not clobber existing file \"'nocol-3.0/src/cmu-snmp/snmplib/snmp.h'\"
  2388. else
  2389.   echo shar: Extracting \"'nocol-3.0/src/cmu-snmp/snmplib/snmp.h'\" \(2082 characters\)
  2390.   sed "s/^X//" >'nocol-3.0/src/cmu-snmp/snmplib/snmp.h' <<'END_OF_FILE'
  2391. X/*
  2392. X * Definitions for the Simple Network Management Protocol (RFC 1067).
  2393. X *
  2394. X *
  2395. X */
  2396. X/***********************************************************
  2397. X    Copyright 1988, 1989 by Carnegie Mellon University
  2398. X
  2399. X                      All Rights Reserved
  2400. X
  2401. XPermission to use, copy, modify, and distribute this software and its 
  2402. Xdocumentation for any purpose and without fee is hereby granted, 
  2403. Xprovided that the above copyright notice appear in all copies and that
  2404. Xboth that copyright notice and this permission notice appear in 
  2405. Xsupporting documentation, and that the name of CMU not be
  2406. Xused in advertising or publicity pertaining to distribution of the
  2407. Xsoftware without specific, written prior permission.  
  2408. X
  2409. XCMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  2410. XALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  2411. XCMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  2412. XANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  2413. XWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  2414. XARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  2415. XSOFTWARE.
  2416. X******************************************************************/
  2417. X
  2418. X#define SNMP_PORT        161
  2419. X#define SNMP_TRAP_PORT        162
  2420. X
  2421. X#define SNMP_MAX_LEN        484
  2422. X
  2423. X#define SNMP_VERSION_1        0
  2424. X
  2425. X#define GET_REQ_MSG        (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x0)
  2426. X#define GETNEXT_REQ_MSG        (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x1)
  2427. X#define GET_RSP_MSG        (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x2)
  2428. X#define SET_REQ_MSG        (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x3)
  2429. X#define TRP_REQ_MSG        (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x4)
  2430. X
  2431. X#define SNMP_ERR_NOERROR    (0x0)
  2432. X#define SNMP_ERR_TOOBIG        (0x1)
  2433. X#define SNMP_ERR_NOSUCHNAME (0x2)
  2434. X#define SNMP_ERR_BADVALUE   (0x3)
  2435. X#define SNMP_ERR_READONLY   (0x4)
  2436. X#define SNMP_ERR_GENERR        (0x5)
  2437. X
  2438. X#define SNMP_TRAP_COLDSTART        (0x0)
  2439. X#define SNMP_TRAP_WARMSTART        (0x1)
  2440. X#define SNMP_TRAP_LINKDOWN        (0x2)
  2441. X#define SNMP_TRAP_LINKUP        (0x3)
  2442. X#define SNMP_TRAP_AUTHFAIL        (0x4)
  2443. X#define SNMP_TRAP_EGPNEIGHBORLOSS    (0x5)
  2444. X#define SNMP_TRAP_ENTERPRISESPECIFIC    (0x6)
  2445. X
  2446. END_OF_FILE
  2447.   if test 2082 -ne `wc -c <'nocol-3.0/src/cmu-snmp/snmplib/snmp.h'`; then
  2448.     echo shar: \"'nocol-3.0/src/cmu-snmp/snmplib/snmp.h'\" unpacked with wrong size!
  2449.   fi
  2450.   # end of 'nocol-3.0/src/cmu-snmp/snmplib/snmp.h'
  2451. fi
  2452. echo shar: End of archive 8 \(of 26\).
  2453. cp /dev/null ark8isdone
  2454. MISSING=""
  2455. for I in 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 ; do
  2456.     if test ! -f ark${I}isdone ; then
  2457.     MISSING="${MISSING} ${I}"
  2458.     fi
  2459. done
  2460. if test "${MISSING}" = "" ; then
  2461.     echo You have unpacked all 26 archives.
  2462.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2463. else
  2464.     echo You still must unpack the following archives:
  2465.     echo "        " ${MISSING}
  2466. fi
  2467. exit 0
  2468. exit 0 # Just in case...
  2469.