| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
#include <net-snmp/net-snmp-includes.h> |
| 3 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 4 |
|
| 5 |
#include "header_simple_table.h" |
| 6 |
|
| 7 |
#include <limits.h> |
| 8 |
|
| 9 |
/*******************************************************************-o-****** |
| 10 |
* header_simple_table |
| 11 |
* |
| 12 |
* Parameters: |
| 13 |
* *vp Variable data. |
| 14 |
* *name Fully instantiated OID name. |
| 15 |
* *length Length of name. |
| 16 |
* exact TRUE if an exact match is desired. |
| 17 |
* *var_len Hook for size of returned data type. |
| 18 |
* (**write_method) Hook for write method (UNUSED). |
| 19 |
* max |
| 20 |
* |
| 21 |
* Returns: |
| 22 |
* 0 If name matches vp->name (accounting for 'exact') and is |
| 23 |
* not greater in length than 'max'. |
| 24 |
* 1 Otherwise. |
| 25 |
* |
| 26 |
* |
| 27 |
* Compare 'name' to vp->name for the best match or an exact match (if |
| 28 |
* requested). Also check that 'name' is not longer than 'max' if |
| 29 |
* max is greater-than/equal 0. |
| 30 |
* Store a successful match in 'name', and increment the OID instance if |
| 31 |
* the match was not exact. |
| 32 |
* |
| 33 |
* 'name' and 'length' are undefined upon failure. |
| 34 |
* |
| 35 |
*/ |
| 36 |
int |
| 37 |
header_simple_table(struct variable *vp, oid *name, size_t *length, |
| 38 |
int exact, size_t *var_len, WriteMethod **write_method, |
| 39 |
int max) |
| 40 |
{ |
| 41 |
int i, rtest; /* Set to: -1 If name < vp->name, |
| 42 |
* 1 If name > vp->name, |
| 43 |
* 0 Otherwise. |
| 44 |
*/ |
| 45 |
oid newname[MAX_OID_LEN]; |
| 46 |
|
| 47 |
for (i = 0, rtest = 0; i < vp->namelen && i < *length && !rtest; i++) { |
| 48 |
if (name[i] != vp->name[i]) { |
| 49 |
if (name[i] < vp->name[i]) |
| 50 |
rtest = -1; |
| 51 |
else |
| 52 |
rtest = 1; |
| 53 |
} |
| 54 |
} |
| 55 |
if (rtest > 0 || (exact == 1 && (rtest || *length != vp->namelen + 1))) { |
| 56 |
if (var_len) |
| 57 |
*var_len = 0; |
| 58 |
return MATCH_FAILED; |
| 59 |
} |
| 60 |
|
| 61 |
memset(newname, 0, sizeof(newname)); |
| 62 |
|
| 63 |
if (*length <= vp->namelen || rtest == -1) { |
| 64 |
memmove(newname, vp->name, vp->namelen * sizeof(oid)); |
| 65 |
newname[vp->namelen] = 1; |
| 66 |
*length = vp->namelen + 1; |
| 67 |
} else if (*length > vp->namelen + 1) { /* exact case checked earlier */ |
| 68 |
*length = vp->namelen + 1; |
| 69 |
memmove(newname, name, *length * sizeof(oid)); |
| 70 |
if (name[*length - 1] < MAX_SUBID) { |
| 71 |
newname[*length - 1] = name[*length - 1] + 1; |
| 72 |
} else { |
| 73 |
/* |
| 74 |
* Careful not to overflow... |
| 75 |
*/ |
| 76 |
newname[*length - 1] = name[*length - 1]; |
| 77 |
} |
| 78 |
} else { |
| 79 |
*length = vp->namelen + 1; |
| 80 |
memmove(newname, name, *length * sizeof(oid)); |
| 81 |
if (!exact) { |
| 82 |
if (name[*length - 1] < MAX_SUBID) { |
| 83 |
newname[*length - 1] = name[*length - 1] + 1; |
| 84 |
} else { |
| 85 |
/* |
| 86 |
* Careful not to overflow... |
| 87 |
*/ |
| 88 |
newname[*length - 1] = name[*length - 1]; |
| 89 |
} |
| 90 |
} else { |
| 91 |
newname[*length - 1] = name[*length - 1]; |
| 92 |
} |
| 93 |
} |
| 94 |
if (*length && ((max >= 0 && newname[*length - 1] > (unsigned int)max) || |
| 95 |
newname[*length - 1] == 0)) { |
| 96 |
if (var_len) |
| 97 |
*var_len = 0; |
| 98 |
return MATCH_FAILED; |
| 99 |
} |
| 100 |
|
| 101 |
memmove(name, newname, *length * sizeof(oid)); |
| 102 |
if (write_method) |
| 103 |
*write_method = NULL; |
| 104 |
if (var_len) |
| 105 |
*var_len = sizeof(long); /* default */ |
| 106 |
return (MATCH_SUCCEEDED); |
| 107 |
} |