util_funcs.c
来自「ucd-snmp源代码」· C语言 代码 · 共 957 行 · 第 1/2 页
C
957 行
size_t name_len){ long tmp=0; if (var_val_type != ASN_INTEGER) { snmp_log(LOG_NOTICE, "Wrong type != int\n"); return SNMP_ERR_WRONGTYPE; } tmp = *((long *) var_val); if (tmp == 1 && action == COMMIT) {#ifdef SIGALRM signal(SIGALRM,restart_doit);#endif alarm(RESTARTSLEEP); } return SNMP_ERR_NOERROR;}voidprint_mib_oid(oid name[], size_t len){ char *buffer; buffer=(char*)malloc(11*len); /* maximum digit lengths for int32 + a '.' */ if (!buffer) { snmp_log(LOG_ERR, "Malloc failed - out of memory?"); return; } sprint_mib_oid(buffer, name, len); snmp_log(LOG_NOTICE, "Mib: %s\n", buffer); free(buffer);}voidsprint_mib_oid(char *buf, oid name[], size_t len){ int i; for(i=0; i < (int)len; i++) { sprintf(buf,".%d",(int) name[i]); while (*buf != 0) buf++; }}/*******************************************************************-o-****** * header_simple_table * * Parameters: * *vp Variable data. * *name Fully instantiated OID name. * *length Length of name. * exact TRUE if an exact match is desired. * *var_len Hook for size of returned data type. * (**write_method) Hook for write method (UNUSED). * max * * Returns: * 0 If name matches vp->name (accounting for 'exact') and is * not greater in length than 'max'. * 1 Otherwise. * * * Compare 'name' to vp->name for the best match or an exact match (if * requested). Also check that 'name' is not longer than 'max' if * max is greater-than/equal 0. * Store a successful match in 'name', and increment the OID instance if * the match was not exact. * * 'name' and 'length' are undefined upon failure. * */int header_simple_table(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method, int max){ int i, rtest; /* Set to: -1 If name < vp->name, * 1 If name > vp->name, * 0 Otherwise. */ oid newname[MAX_OID_LEN]; for(i=0,rtest=0; i < (int) vp->namelen && i < (int)(*length) && !rtest; i++) { if (name[i] != vp->name[i]) { if (name[i] < vp->name[i]) rtest = -1; else rtest = 1; } } if (rtest > 0 || (exact == 1 && (rtest || (int)*length != (int)(vp->namelen+1)))) { if (var_len) *var_len = 0; return MATCH_FAILED; } memset(newname, 0, sizeof(newname)); if (((int) *length) <= (int) vp->namelen || rtest == -1) { memmove(newname, vp->name, (int)vp->namelen * sizeof (oid)); newname[vp->namelen] = 1; *length = vp->namelen+1; } else if (((int)*length) > (int)vp->namelen+1) { /* exact case checked earlier */ *length = vp->namelen+1; memmove(newname, name, (*length) * sizeof(oid)); newname[*length-1] = name[*length-1] + 1; } else { *length = vp->namelen+1; memmove(newname, name, (*length) * sizeof(oid)); if (!exact) newname[*length-1] = name[*length-1] + 1; else newname[*length-1] = name[*length-1]; } if ((max >= 0 && (newname[*length-1] > max)) || (0 == newname[*length-1] )) { if(var_len) *var_len = 0; return MATCH_FAILED; } memmove(name, newname, (*length) * sizeof(oid)); if (write_method) *write_method = 0; if (var_len) *var_len = sizeof(long); /* default */ return(MATCH_SUCCEEDED);}/* header_generic(... Arguments: vp IN - pointer to variable entry that points here name IN/OUT - IN/name requested, OUT/name found length IN/OUT - length of IN/OUT oid's exact IN - TRUE if an exact match was requested var_len OUT - length of variable or 0 if function returned write_method *//*******************************************************************-o-****** * generic_header * * Parameters: * *vp (I) Pointer to variable entry that points here. * *name (I/O) Input name requested, output name found. * *length (I/O) Length of input and output oid's. * exact (I) TRUE if an exact match was requested. * *var_len (O) Length of variable or 0 if function returned. * (**write_method) Hook to name a write method (UNUSED). * * Returns: * MATCH_SUCCEEDED If vp->name matches name (accounting for exact bit). * MATCH_FAILED Otherwise, * * * Check whether variable (vp) matches name. */intheader_generic(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ oid newname[MAX_OID_LEN]; int result; DEBUGMSGTL(("util_funcs", "header_generic: ")); DEBUGMSGOID(("util_funcs", name, *length)); DEBUGMSG(("util_funcs"," exact=%d\n", exact)); memcpy((char *)newname, (char *)vp->name, (int)vp->namelen * sizeof(oid)); newname[vp->namelen] = 0; result = snmp_oid_compare(name, *length, newname, vp->namelen + 1); DEBUGMSGTL(("util_funcs", " result: %d\n", result)); if ((exact && (result != 0)) || (!exact && (result >= 0))) return(MATCH_FAILED); memcpy( (char *)name,(char *)newname, ((int)vp->namelen + 1) * sizeof(oid)); *length = vp->namelen + 1; *write_method = 0; *var_len = sizeof(long); /* default to 'long' results */ return(MATCH_SUCCEEDED);}/* checkmib(): provided for backwards compatibility, do not use: */int checkmib(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method, int max) { /* checkmib used to be header_simple_table, with reveresed boolean return output. header_simple_table() was created to match header_generic(). */ return (!header_simple_table(vp, name, length, exact, var_len, write_method, max));}char *find_field(char *ptr, int field){ int i; char *init=ptr; if (field == LASTFIELD) { /* skip to end */ while (*ptr++); ptr = ptr - 2; /* rewind a field length */ while (*ptr != 0 && isspace(*ptr) && init <= ptr) ptr--; while (*ptr != 0 && !isspace(*ptr) && init <= ptr) ptr--; if (isspace(*ptr)) ptr++; /* past space */ if (ptr < init) ptr = init; if (!isspace(*ptr) && *ptr != 0) return(ptr); } else { if ((ptr = skip_white(ptr)) == NULL) return(NULL); for (i=1; *ptr != 0 && i != field; i++) { if ((ptr = skip_not_white(ptr)) == NULL) return (NULL); if ((ptr = skip_white(ptr)) == NULL) return (NULL); } if (*ptr != 0 && i == field) return(ptr); return (NULL); } return(NULL);}int parse_miboid(const char *buf, oid *oidout){ int i; if (!buf) return 0; if (*buf == '.') buf++; for(i=0;isdigit(*buf);i++) { oidout[i] = atoi(buf); while(isdigit(*buf++)); if (*buf == '.') buf++; } /* oidout[i] = -1; hmmm */ return i;}voidstring_append_int (char *s, int val){ char textVal[16]; if (val < 10) { *s++ = '0' + val; *s = '\0'; return; } sprintf (textVal, "%d", val); strcpy(s, textVal); return;}struct internal_mib_table { int max_size; /* Size of the current data table */ int next_index; /* Index of the next free entry */ int current_index; /* Index of the 'current' entry */ int cache_timeout; marker_t cache_marker; RELOAD* reload; /* Routine to read in the data */ COMPARE* compare; /* Routine to compare two entries */ int data_size; /* Size of an individual entry */ void* data; /* The table itself */};mib_table_tInitialise_Table( int size, int timeout, RELOAD reload, COMPARE compare){ struct internal_mib_table *t; t = (struct internal_mib_table*)malloc(sizeof(struct internal_mib_table)); if ( t == NULL ) return NULL; t->max_size = 0; t->next_index = 1; /* Don't use index 0 */ t->current_index = 1; t->cache_timeout = timeout; t->cache_marker = NULL; t->reload = reload; t->compare = compare; t->data_size = size; t->data = NULL; return (mib_table_t) t ;}#define TABLE_ADD( x, y ) ((void*)((char*)(x) + y))#define TABLE_INDEX(t, i) (TABLE_ADD(t->data, i * t->data_size))#define TABLE_START(t) (TABLE_INDEX(t, 1))#define TABLE_NEXT(t) (TABLE_INDEX(t, t->next_index))#define TABLE_CURRENT(t) (TABLE_INDEX(t, t->current_index))int check_and_reload_table( struct internal_mib_table *table ){ /* * If the saved data is fairly recent, * we don't need to reload it */ if ( table->cache_marker && !(atime_ready( table->cache_marker, table->cache_timeout*1000))) return 1; /* * Call the routine provided to read in the data * * N.B: Update the cache marker *before* calling * this routine, to avoid problems with recursion */ if ( !table->cache_marker ) table->cache_marker = atime_newMarker(); else atime_setMarker( table->cache_marker ); table->next_index = 1; if ( table->reload( (mib_table_t)table ) < 0 ) { free( table->cache_marker ); table->cache_marker = NULL; return 0; } table->current_index = 1; if ( table->compare != NULL ) /* Sort the table */ qsort( TABLE_START(table), table->next_index, table->data_size, table->compare ); return 1;}intSearch_Table( mib_table_t t, void* entry, int exact){ struct internal_mib_table *table = (struct internal_mib_table *)t; void *entry2; int res; if (!check_and_reload_table( table )) return -1; if ( table->compare == NULL ) { /* XXX - not sure this is right ? */ memcpy(entry, table->data, table->data_size); return 0; } if ( table->next_index == table->current_index ) table->current_index = 1; entry2 = TABLE_CURRENT(table); res = table->compare( entry, entry2 ); if (( res < 0 ) && (table->current_index != 1)) { table->current_index = 1; entry2 = TABLE_CURRENT(table); res = table->compare( entry, entry2 ); } while ( res > 0 ) { table->current_index++; if ( table->next_index == table->current_index ) return -1; entry2 = TABLE_CURRENT(table); res = table->compare( entry, entry2 ); } if ( exact && res != 0 ) return -1; if ( !exact && res == 0 ) { table->current_index++; if ( table->next_index == table->current_index ) return -1; entry2 = TABLE_CURRENT(table); } memcpy(entry, entry2, table->data_size); return 0;}int Add_Entry( mib_table_t t, void* entry){ struct internal_mib_table *table = (struct internal_mib_table *)t; int new_max; void *new_data; /* Used for * a) extending the data table * b) the next entry to use */ if ( table->max_size <= table->next_index ) { /* * Table is full, so extend it to double the size */ new_max = 2*table->max_size; if ( new_max == 0 ) new_max = 10; /* Start with 10 entries */ new_data = (void*)malloc( new_max * table->data_size ); if ( new_data == NULL ) return -1; if ( table->data ) { memcpy(new_data, table->data, table->max_size * table->data_size); free( table->data ); } table->data = new_data; table->max_size = new_max; } /* * Insert the new entry into the data array */ new_data = TABLE_NEXT(table); memcpy( new_data, entry, table->data_size ); table->next_index++; return 0;}void *Retrieve_Table_Data( mib_table_t t, int* max_idx){ struct internal_mib_table *table = (struct internal_mib_table *)t; if (!check_and_reload_table( table )) return NULL; *max_idx = table->next_index-1; return table->data;} /* * Time-related utility functions */extern struct timeval starttime; /* Return the value of 'sysUpTime' at the given marker */u_longmarker_uptime( marker_t pm ){ u_long res; marker_t start = (marker_t)&starttime; res = atime_diff( start, pm ); return res/10; /* atime_diff works in msec, not csec */} /* Return the number of timeTicks since the given marker */u_longmarker_tticks( marker_t pm ){ u_long res; marker_t now = atime_newMarker(); res = atime_diff( pm, now ); free( now ); return res/10; /* atime_diff works in msec, not csec */} /* struct timeval equivalents of these */u_long timeval_uptime( struct timeval *tv ){ return marker_uptime((marker_t)tv);}u_long timeval_tticks( struct timeval *tv ){ return marker_tticks((marker_t)tv);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?