snmp_mib.c
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
00044
00045
00046
00047
00048
00049
00050
00051
00052 typedef struct _SUBTREE {
00054 struct _SUBTREE *sub_next;
00056 int sub_numvars;
00058 SNMPVAR *sub_vars;
00060 size_t sub_namelen;
00062 OID sub_name[MAX_OID_LEN];
00063 } SUBTREE;
00064
00065 static SUBTREE *mibtree;
00066
00070 int SnmpMibRegister(OID basename[], size_t baselen, SNMPVAR * vars, int num)
00071 {
00072 SUBTREE **tpp;
00073 SUBTREE *branch;
00074
00075
00076 if ((branch = malloc(sizeof(SUBTREE))) == NULL) {
00077 return -1;
00078 }
00079 branch->sub_numvars = num;
00080 branch->sub_vars = vars;
00081 branch->sub_namelen = baselen;
00082 memcpy(branch->sub_name, basename, baselen * sizeof(OID));
00083
00084
00085 for (tpp = &mibtree; *tpp; tpp = &(*tpp)->sub_next) {
00086 if (SnmpOidCmp((*tpp)->sub_name, (*tpp)->sub_namelen, branch->sub_name, branch->sub_namelen) > 0) {
00087 break;
00088 }
00089 }
00090
00091 branch->sub_next = *tpp;
00092 *tpp = branch;
00093
00094 return 0;
00095 }
00096
00109 uint8_t *SnmpMibFind(OID * name, size_t * namelen, uint8_t * type, size_t * len, uint16_t * acl, int exact, WMETHOD ** wmethod,
00110 int *no_obj)
00111 {
00112 SUBTREE *tp;
00113 SNMPVAR *vp = 0;
00114 int i;
00115 uint8_t *access = NULL;
00116 int rc;
00117 OID *suffix;
00118 size_t sufflen;
00119 OID *ori_oid = NULL;
00120 size_t ori_len = 0;
00121 int found = 0;
00122
00123
00124
00125
00126 if (!exact) {
00127 if ((ori_oid = malloc(*namelen * sizeof(OID))) == NULL) {
00128 return NULL;
00129 }
00130 memcpy(ori_oid, name, *namelen * sizeof(OID));
00131 ori_len = *namelen;
00132 }
00133 *wmethod = NULL;
00134
00135
00136
00137
00138 for (tp = mibtree; tp; tp = tp->sub_next) {
00139
00140
00141
00142
00143 rc = SnmpOidTreeCmp(name, *namelen, tp->sub_name, tp->sub_namelen);
00144 if (rc == 0 || (rc < 0 && !exact)) {
00145 sufflen = *namelen - tp->sub_namelen;
00146 suffix = name + tp->sub_namelen;
00147
00148 for (i = 0, vp = tp->sub_vars; i < tp->sub_numvars; i++, vp++) {
00149 if (vp->var_namelen && (exact || rc >= 0)) {
00150 rc = SnmpOidTreeCmp(suffix, sufflen, vp->var_name, vp->var_namelen);
00151 }
00152
00153 if ((exact && rc == 0) || (!exact && rc <= 0) || vp->var_namelen == 0) {
00154 access = (*(vp->var_get)) (vp, name, namelen, exact, len, wmethod);
00155 if (wmethod) {
00156 *acl = vp->var_acl;
00157 }
00158 if (exact) {
00159 found = 1;
00160 }
00161 if (access) {
00162 break;
00163 }
00164 }
00165 if (exact && rc <= 0) {
00166 *type = vp->var_type;
00167 *acl = vp->var_acl;
00168 *no_obj = !found;
00169 return NULL;
00170 }
00171 }
00172 if (access) {
00173 break;
00174 }
00175 }
00176 }
00177 if (tp == NULL) {
00178 if (!access && !exact) {
00179 memcpy(name, ori_oid, ori_len * sizeof(OID));
00180 *namelen = ori_len;
00181 free(ori_oid);
00182 }
00183 *no_obj = !found;
00184 return NULL;
00185 }
00186 if (ori_oid) {
00187 free(ori_oid);
00188 }
00189
00190
00191
00192
00193 *type = vp->var_type;
00194 *acl = vp->var_acl;
00195
00196 return access;
00197 }