Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <sys/types.h>
00035
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include <memdebug.h>
00039
00040 #include <pro/snmp.h>
00041 #include <pro/snmp_api.h>
00042 #include <pro/snmp_mib.h>
00043
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 typedef struct _SUBTREE {
00059 struct _SUBTREE *sub_next;
00061 int sub_numvars;
00063 SNMPVAR *sub_vars;
00065 size_t sub_namelen;
00067 OID sub_name[MAX_OID_LEN];
00068 } SUBTREE;
00069
00070 static SUBTREE *mibtree;
00071
00075 int SnmpMibRegister(OID basename[], size_t baselen, SNMPVAR * vars, int num)
00076 {
00077 SUBTREE **tpp;
00078 SUBTREE *branch;
00079
00080
00081 if ((branch = malloc(sizeof(SUBTREE))) == NULL) {
00082 return -1;
00083 }
00084 branch->sub_numvars = num;
00085 branch->sub_vars = vars;
00086 branch->sub_namelen = baselen;
00087 memcpy(branch->sub_name, basename, baselen * sizeof(OID));
00088
00089
00090 for (tpp = &mibtree; *tpp; tpp = &(*tpp)->sub_next) {
00091 if (SnmpOidCmp((*tpp)->sub_name, (*tpp)->sub_namelen, branch->sub_name, branch->sub_namelen) > 0) {
00092 break;
00093 }
00094 }
00095
00096 branch->sub_next = *tpp;
00097 *tpp = branch;
00098
00099 return 0;
00100 }
00101
00114 uint8_t *SnmpMibFind(OID * name, size_t * namelen, uint8_t * type, size_t * len, uint16_t * acl, int exact, WMETHOD ** wmethod,
00115 int *no_obj)
00116 {
00117 SUBTREE *tp;
00118 SNMPVAR *vp = 0;
00119 int i;
00120 uint8_t *access = NULL;
00121 int rc;
00122 OID *suffix;
00123 size_t sufflen;
00124 OID *ori_oid = NULL;
00125 size_t ori_len = 0;
00126 int found = 0;
00127
00128
00129
00130
00131 if (!exact) {
00132 if ((ori_oid = malloc(*namelen * sizeof(OID))) == NULL) {
00133 return NULL;
00134 }
00135 memcpy(ori_oid, name, *namelen * sizeof(OID));
00136 ori_len = *namelen;
00137 }
00138 *wmethod = NULL;
00139
00140
00141
00142
00143 for (tp = mibtree; tp; tp = tp->sub_next) {
00144
00145
00146
00147
00148 rc = SnmpOidTreeCmp(name, *namelen, tp->sub_name, tp->sub_namelen);
00149 if (rc == 0 || (rc < 0 && !exact)) {
00150 sufflen = *namelen - tp->sub_namelen;
00151 suffix = name + tp->sub_namelen;
00152
00153 for (i = 0, vp = tp->sub_vars; i < tp->sub_numvars; i++, vp++) {
00154 if (vp->var_namelen && (exact || rc >= 0)) {
00155 rc = SnmpOidTreeCmp(suffix, sufflen, vp->var_name, vp->var_namelen);
00156 }
00157
00158 if ((exact && rc == 0) || (!exact && rc <= 0) || vp->var_namelen == 0) {
00159 access = (*(vp->var_get)) (vp, name, namelen, exact, len, wmethod);
00160 if (wmethod) {
00161 *acl = vp->var_acl;
00162 }
00163 if (exact) {
00164 found = 1;
00165 }
00166 if (access) {
00167 break;
00168 }
00169 }
00170 if (exact && rc <= 0) {
00171 *type = vp->var_type;
00172 *acl = vp->var_acl;
00173 *no_obj = !found;
00174 return NULL;
00175 }
00176 }
00177 if (access) {
00178 break;
00179 }
00180 }
00181 }
00182 if (tp == NULL) {
00183 if (!access && !exact) {
00184 memcpy(name, ori_oid, ori_len * sizeof(OID));
00185 *namelen = ori_len;
00186 free(ori_oid);
00187 }
00188 *no_obj = !found;
00189 return NULL;
00190 }
00191 if (ori_oid) {
00192 free(ori_oid);
00193 }
00194
00195
00196
00197
00198 *type = vp->var_type;
00199 *acl = vp->var_acl;
00200
00201 return access;
00202 }
00203