Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / cmu_compat.c
1
2 #ifdef CMU_COMPATIBLE
3
4 #include <net-snmp/mib_api.h>
5 #include <net-snmp/pdu_api.h>
6 #include <net-snmp/session_api.h>
7
8 int
9 mib_TxtToOid(char *Buf, oid ** OidP, size_t * LenP)
10 {
11     return read_objid(Buf, *OidP, LenP);
12 }
13
14 int
15 mib_OidToTxt(oid * O, size_t OidLen, char *Buf, size_t BufLen)
16 {
17     _sprint_objid(Buf, O, OidLen);
18     return 1;
19 }
20
21
22
23 char           *
24 snmp_pdu_type(netsnmp_pdu *PDU)
25 {
26     switch (PDU->command) {
27     case SNMP_MSG_GET:
28         return ("GET");
29         break;
30     case SNMP_MSG_GETNEXT:
31         return ("GETNEXT");
32         break;
33     case SNMP_MSG_RESPONSE:
34         return ("RESPONSE");
35         break;
36     case SNMP_MSG_SET:
37         return ("SET");
38         break;
39     case SNMP_MSG_GETBULK:
40         return ("GETBULK");
41         break;
42     case SNMP_MSG_INFORM:
43         return ("INFORM");
44         break;
45     case SNMP_MSG_TRAP2:
46         return ("V2TRAP");
47         break;
48     case SNMP_MSG_REPORT:
49         return ("REPORT");
50         break;
51
52     case SNMP_MSG_TRAP:
53         return ("V1TRAP");
54         break;
55     default:
56         return ("Unknown");
57         break;
58     }
59 }
60
61 /*
62  * cmu_snmp_parse - emulate CMU library's snmp_parse.
63  *
64  * Parse packet, storing results into PDU.
65  * Returns community string if success, NULL if fail.
66  * WARNING: may return a zero length community string.
67  *
68  * Note:
69  * Some CMU-aware apps call init_mib(), but do not
70  * initialize a session.
71  * Check Reqid to make sure that this module is initialized.
72  */
73
74 u_char         *
75 cmu_snmp_parse(netsnmp_session * session,
76                netsnmp_pdu *pdu, u_char * data, size_t length)
77 {
78     u_char         *bufp = NULL;
79
80     snmp_sess_init(session);    /* gimme a break! */
81
82     switch (pdu->version) {
83     case SNMP_VERSION_1:
84     case SNMP_VERSION_2c:
85     case SNMP_DEFAULT_VERSION:
86         break;
87     default:
88         return NULL;
89     }
90 #ifndef NO_INTERNAL_VARLIST
91     if (snmp_parse(0, session, pdu, data, length) != SNMP_ERR_NOERROR) {
92         return NULL;
93     }
94 #else
95     /*
96      * while there are two versions of variable_list:
97      * use an internal variable list for snmp_parse;
98      * clone the result.
99      */
100     if (1) {
101         netsnmp_pdu    *snmp_clone_pdu(netsnmp_pdu *);
102         netsnmp_pdu    *snmp_2clone_pdu(netsnmp_pdu *from_pdu,
103                                         netsnmp_pdu *to_pdu);
104
105         netsnmp_pdu    *ipdu;
106         ipdu = snmp_clone_pdu(pdu);
107         if (snmp_parse(0, session, ipdu, data, length) != SNMP_ERR_NOERROR) {
108             snmp_free_internal_pdu(ipdu);
109             return NULL;
110         }
111         pdu = snmp_2clone_pdu(ipdu, pdu);
112         snmp_free_internal_pdu(ipdu);
113     }
114 #endif                          /* NO_INTERNAL_VAR_LIST */
115
116     /*
117      * Add a null to meet the caller's expectations. 
118      */
119
120     bufp = (u_char *) malloc(1 + pdu->community_len);
121     if (bufp && pdu->community_len) {
122         memcpy(bufp, pdu->community, pdu->community_len);
123         bufp[pdu->community_len] = '\0';
124     }
125     return (bufp);
126 }
127
128
129 #endif                          /* CMU_COMPATIBLE */