| 1 |
/************************************************************** |
| 2 |
* Copyright (C) 2001 Alex Rozin, Optical Access |
| 3 |
* |
| 4 |
* All Rights Reserved |
| 5 |
* |
| 6 |
* Permission to use, copy, modify and distribute this software and its |
| 7 |
* documentation for any purpose and without fee is hereby granted, |
| 8 |
* provided that the above copyright notice appear in all copies and that |
| 9 |
* both that copyright notice and this permission notice appear in |
| 10 |
* supporting documentation. |
| 11 |
* |
| 12 |
* ALEX ROZIN DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING |
| 13 |
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL |
| 14 |
* ALEX ROZIN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR |
| 15 |
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 16 |
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
| 17 |
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 18 |
* SOFTWARE. |
| 19 |
******************************************************************/ |
| 20 |
|
| 21 |
#include <net-snmp/net-snmp-config.h> |
| 22 |
#include <net-snmp/net-snmp-includes.h> |
| 23 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 24 |
|
| 25 |
#include "agutil.h" |
| 26 |
#include "agutil_api.h" |
| 27 |
|
| 28 |
#ifndef OPTICALL_ACESS /* in OpticalAccess case : find them in ~agent/mibgroup/nbase directory */ |
| 29 |
void |
| 30 |
ag_trace(const char *format, ...) |
| 31 |
{ |
| 32 |
#define AG_MAX_MSG_LEN 120 |
| 33 |
char msg[AG_MAX_MSG_LEN]; |
| 34 |
va_list args; |
| 35 |
|
| 36 |
/* |
| 37 |
* create msg |
| 38 |
*/ |
| 39 |
va_start(args, format); |
| 40 |
vsnprintf(msg, AG_MAX_MSG_LEN - 1, format, args); |
| 41 |
|
| 42 |
snmp_log(LOG_INFO, "%s\n", msg); |
| 43 |
va_end(args); |
| 44 |
} |
| 45 |
|
| 46 |
int |
| 47 |
AGUTIL_advance_index_name(struct variable *vp, oid * name, |
| 48 |
size_t * length, int exact) |
| 49 |
{ |
| 50 |
int result; |
| 51 |
|
| 52 |
if (exact) |
| 53 |
return 0; |
| 54 |
|
| 55 |
if (*length <= vp->namelen) { |
| 56 |
result = |
| 57 |
snmp_oid_compare(name, *length, vp->name, (int) vp->namelen); |
| 58 |
memcpy((char *) name, (char *) vp->name, |
| 59 |
((int) vp->namelen) * sizeof(oid)); |
| 60 |
*length = vp->namelen; |
| 61 |
} else { |
| 62 |
/* |
| 63 |
* If the name is given with indexes - compare only the oids. |
| 64 |
*/ |
| 65 |
result = |
| 66 |
snmp_oid_compare(name, (int) vp->namelen, vp->name, |
| 67 |
(int) vp->namelen); |
| 68 |
/* |
| 69 |
* If it's not the same oid - change name to the new oid |
| 70 |
*/ |
| 71 |
if (result < 0) { |
| 72 |
memcpy((char *) name, (char *) vp->name, |
| 73 |
((int) vp->namelen) * sizeof(oid)); |
| 74 |
*length = vp->namelen; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if (result > 0) { |
| 79 |
ag_trace("*length=%d result=%d !!!", (int) *length, (int) result); |
| 80 |
return -1; |
| 81 |
} |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
/********************************************************************* |
| 86 |
* Function: AGUTIL_get_int_value |
| 87 |
* |
| 88 |
* Description: |
| 89 |
* Check/Get long value from the PDU.. |
| 90 |
* The parameters 'min_value' & 'max_value' allow to check the diaposon |
| 91 |
* of the value. If (max_value <= min_value) we avoid this checking. |
| 92 |
* |
| 93 |
* Returns: |
| 94 |
* SNMP_ERR_NOERROR |
| 95 |
* SNMP_ERR_WRONGTYPE |
| 96 |
* SNMP_ERR_WRONGLENGTH |
| 97 |
* SNMP_ERR_WRONGENCODING |
| 98 |
* SNMP_ERR_BADVALUE |
| 99 |
*********************************************************************/ |
| 100 |
int |
| 101 |
AGUTIL_get_int_value(u_char * var_val, u_char var_val_type, |
| 102 |
size_t var_val_len, long min_value, long max_value, |
| 103 |
long *long_tmp) |
| 104 |
{ |
| 105 |
if (var_val_type != ASN_INTEGER && var_val_type != ASN_TIMETICKS) { |
| 106 |
ag_trace("not ASN_INTEGER 0x%lx", (long) var_val_type); |
| 107 |
return SNMP_ERR_WRONGTYPE; |
| 108 |
} |
| 109 |
|
| 110 |
if (var_val_len > sizeof(long)) { |
| 111 |
ag_trace("wrong len=%d", (int) var_val_len); |
| 112 |
return SNMP_ERR_WRONGLENGTH; |
| 113 |
} |
| 114 |
|
| 115 |
*long_tmp = *((long *) var_val); |
| 116 |
|
| 117 |
if (max_value > min_value) { |
| 118 |
if (*long_tmp < min_value) { |
| 119 |
ag_trace("%ld=long_tmp < min=%ld", (long) *long_tmp, |
| 120 |
(long) min_value); |
| 121 |
return SNMP_ERR_BADVALUE; |
| 122 |
} |
| 123 |
|
| 124 |
if (*long_tmp > max_value) { |
| 125 |
ag_trace("%ld=long_tmp > max=%ld", (long) *long_tmp, |
| 126 |
(long) max_value); |
| 127 |
return SNMP_ERR_BADVALUE; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return SNMP_ERR_NOERROR; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
/********************************************************************* |
| 136 |
* Function: AGUTIL_get_string_value |
| 137 |
* |
| 138 |
* Description: |
| 139 |
* Check/Get 'DisplayString' value from the PDU.. |
| 140 |
* |
| 141 |
* Returns: |
| 142 |
* SNMP_ERR_NOERROR |
| 143 |
* SNMP_ERR_WRONGTYPE |
| 144 |
* SNMP_ERR_WRONGLENGTH |
| 145 |
* SNMP_ERR_WRONGENCODING |
| 146 |
* SNMP_ERR_BADVALUE |
| 147 |
*********************************************************************/ |
| 148 |
int |
| 149 |
AGUTIL_get_string_value(u_char * var_val, u_char var_val_type, |
| 150 |
size_t var_val_len, size_t buffer_max_size, |
| 151 |
u_char should_zero_limited, |
| 152 |
size_t * buffer_actual_size, char *buffer) |
| 153 |
{ |
| 154 |
if (var_val_type != ASN_OCTET_STR) { |
| 155 |
ag_trace("not ASN_OCTET_STR 0x%lx", (long) var_val_type); |
| 156 |
return SNMP_ERR_WRONGTYPE; |
| 157 |
} |
| 158 |
|
| 159 |
if (var_val_len > buffer_max_size) { |
| 160 |
ag_trace("wrong len=%d > %d", (int) var_val_len, buffer_max_size); |
| 161 |
return SNMP_ERR_WRONGLENGTH; |
| 162 |
} |
| 163 |
|
| 164 |
if (buffer_actual_size) |
| 165 |
*buffer_actual_size = var_val_len; |
| 166 |
|
| 167 |
memcpy(buffer, var_val, var_val_len); |
| 168 |
if (should_zero_limited) { |
| 169 |
buffer[var_val_len] = 0; |
| 170 |
if (buffer_actual_size) |
| 171 |
*buffer_actual_size += 1; |
| 172 |
} |
| 173 |
|
| 174 |
return SNMP_ERR_NOERROR; |
| 175 |
} |
| 176 |
|
| 177 |
#endif |
| 178 |
|
| 179 |
int |
| 180 |
AGUTIL_get_oid_value(u_char * var_val, u_char var_val_type, |
| 181 |
size_t var_val_len, VAR_OID_T * data_source_ptr) |
| 182 |
{ |
| 183 |
register int iii; |
| 184 |
register oid *oid_var; |
| 185 |
|
| 186 |
if (var_val_len > MAX_OID_LEN) { |
| 187 |
ag_trace("wrong len=%d > %d", (int) var_val_len, MAX_OID_LEN); |
| 188 |
return SNMP_ERR_WRONGLENGTH; |
| 189 |
} |
| 190 |
|
| 191 |
var_val_len /= sizeof(oid); |
| 192 |
data_source_ptr->length = var_val_len; |
| 193 |
oid_var = (oid *) var_val; |
| 194 |
for (iii = 0; iii < (int)data_source_ptr->length; iii++) |
| 195 |
data_source_ptr->objid[iii] = oid_var[iii]; |
| 196 |
|
| 197 |
return SNMP_ERR_NOERROR; |
| 198 |
} |
| 199 |
|
| 200 |
u_long |
| 201 |
AGUTIL_sys_up_time(void) |
| 202 |
{ |
| 203 |
return netsnmp_get_agent_runtime(); |
| 204 |
} |
| 205 |
|
| 206 |
/* |
| 207 |
* NOTE: this function is a template for system dependent |
| 208 |
* implementation. Actually it (in debug purposes) returns |
| 209 |
* random (but likely) data */ |
| 210 |
void |
| 211 |
SYSTEM_get_eth_statistics(VAR_OID_T * data_source, ETH_STATS_T * where) |
| 212 |
{ |
| 213 |
#ifdef OPTICALL_ACESS |
| 214 |
where->ifIndex = data_source->objid[data_source->length - 1]; |
| 215 |
agent_get_Rmon_ethernet_statistics(where->ifIndex, 1, /* exact */ |
| 216 |
where); |
| 217 |
#else /* OPTICALL_ACESS */ |
| 218 |
static ETH_STATS_T prev = { -1, -1 }; |
| 219 |
static time_t ifLastRead = 0; |
| 220 |
time_t curr_time; |
| 221 |
u_char need_to_read; |
| 222 |
u_long rc; |
| 223 |
|
| 224 |
where->ifIndex = data_source->objid[data_source->length - 1]; |
| 225 |
need_to_read = (where->ifIndex != prev.ifIndex); |
| 226 |
if (!need_to_read) { |
| 227 |
curr_time = time(NULL); |
| 228 |
need_to_read = (curr_time - ifLastRead > 1); |
| 229 |
} |
| 230 |
|
| 231 |
if (need_to_read) { |
| 232 |
rc = (u_long) (1.0 + |
| 233 |
((double) rand() / (double) RAND_MAX) * 100.0); |
| 234 |
ifLastRead = time(NULL); |
| 235 |
prev.ifIndex = where->ifIndex; |
| 236 |
} else |
| 237 |
rc = 0; |
| 238 |
|
| 239 |
memcpy(where, &prev, sizeof(ETH_STATS_T)); |
| 240 |
where->octets += rc * 100 * 200; |
| 241 |
where->packets += rc * 100; |
| 242 |
where->bcast_pkts += rc * 2; |
| 243 |
where->mcast_pkts += rc * 3; |
| 244 |
where->crc_align += rc; |
| 245 |
where->undersize += 0; |
| 246 |
where->oversize += 0; |
| 247 |
where->fragments += rc / 2; |
| 248 |
where->jabbers += 0; |
| 249 |
where->collisions += rc / 4; |
| 250 |
|
| 251 |
where->pkts_64 += rc * 10; |
| 252 |
where->pkts_65_127 += rc * 50; |
| 253 |
where->pkts_128_255 += rc * 20; |
| 254 |
where->pkts_256_511 += rc * 10; |
| 255 |
where->pkts_512_1023 += rc * 15; |
| 256 |
where->pkts_1024_1518 += rc * 5; |
| 257 |
|
| 258 |
need_to_read = prev.ifIndex; |
| 259 |
memcpy(&prev, where, sizeof(ETH_STATS_T)); |
| 260 |
prev.ifIndex = need_to_read; |
| 261 |
#endif /* OPTICALL_ACESS */ |
| 262 |
} |
| 263 |
|
| 264 |
#if 0 /* for memory debug */ |
| 265 |
static u_long dbg_mem_cnt = 0; |
| 266 |
|
| 267 |
void * |
| 268 |
dbg_f_AGMALLOC(size_t size) |
| 269 |
{ |
| 270 |
dbg_mem_cnt++; |
| 271 |
return malloc(size); |
| 272 |
} |
| 273 |
|
| 274 |
void |
| 275 |
dbg_f_AGFREE(void *ptr) |
| 276 |
{ |
| 277 |
dbg_mem_cnt--; |
| 278 |
free(ptr); |
| 279 |
} |
| 280 |
|
| 281 |
char * |
| 282 |
dbg_f_AGSTRDUP(const char *s) |
| 283 |
{ |
| 284 |
dbg_mem_cnt++; |
| 285 |
return strdup(s); |
| 286 |
} |
| 287 |
|
| 288 |
void |
| 289 |
dbg_f_AG_MEM_REPORT(void) |
| 290 |
{ |
| 291 |
ag_trace("dbg_mem_cnt=%ld", (long) dbg_mem_cnt); |
| 292 |
} |
| 293 |
|
| 294 |
#endif |
| 295 |
|
| 296 |
void |
| 297 |
init_agutil(void) |
| 298 |
{ |
| 299 |
} |