📄 tools.c
字号:
/* * tools.c */#include <net-snmp/net-snmp-config.h>#include <ctype.h>#include <stdio.h>#include <sys/types.h>#if TIME_WITH_SYS_TIME# ifdef WIN32# include <sys/timeb.h># else# include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#ifdef HAVE_SYS_SOCKET_H#include <sys/socket.h>#endif#if HAVE_WINSOCK_H#include <winsock.h>#endif#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#ifdef HAVE_NETINET_IN_H#include <netinet/in.h>#endif#ifdef HAVE_ARPA_INET_H#include <arpa/inet.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include <net-snmp/types.h>#include <net-snmp/output_api.h>#include <net-snmp/utilities.h>#include <net-snmp/library/tools.h> /* for "internal" definitions */#include <net-snmp/library/snmp_api.h>#include <net-snmp/library/mib.h>#include <net-snmp/library/scapi.h>/* * snmp_realloc: * * Parameters: * * buf pointer to a buffer pointer * buf_len pointer to current size of buffer in bytes * * This function increase the size of the buffer pointed at by *buf, which is * initially of size *buf_len. Contents are preserved **AT THE BOTTOM END OF * THE BUFFER**. If memory can be (re-)allocated then it returns 1, else it * returns 0. * */intsnmp_realloc(u_char ** buf, size_t * buf_len){ u_char *new_buf = NULL; size_t new_buf_len = 0; if (buf == NULL) { return 0; } /* * The current re-allocation algorithm is to increase the buffer size by * whichever is the greater of 256 bytes or the current buffer size, up to * a maximum increase of 8192 bytes. */ if (*buf_len <= 255) { new_buf_len = *buf_len + 256; } else if (*buf_len > 255 && *buf_len <= 8191) { new_buf_len = *buf_len * 2; } else if (*buf_len > 8191) { new_buf_len = *buf_len + 8192; } if (*buf == NULL) { new_buf = (u_char *) malloc(new_buf_len); } else { new_buf = (u_char *) realloc(*buf, new_buf_len); } if (new_buf != NULL) { *buf = new_buf; *buf_len = new_buf_len; return 1; } else { return 0; }}intsnmp_strcat(u_char ** buf, size_t * buf_len, size_t * out_len, int allow_realloc, const u_char * s){ if (buf == NULL || buf_len == NULL || out_len == NULL) { return 0; } if (s == NULL) { /* * Appending a NULL string always succeeds since it is a NOP. */ return 1; } while ((*out_len + strlen((const char *) s) + 1) >= *buf_len) { if (!(allow_realloc && snmp_realloc(buf, buf_len))) { return 0; } } strcpy((char *) (*buf + *out_len), (const char *) s); *out_len += strlen((char *) (*buf + *out_len)); return 1;}/*******************************************************************-o-****** * free_zero * * Parameters: * *buf Pointer at bytes to free. * size Number of bytes in buf. */voidfree_zero(void *buf, size_t size){ if (buf) { memset(buf, 0, size); free(buf); }} /* end free_zero() *//*******************************************************************-o-****** * malloc_random * * Parameters: * size Number of bytes to malloc() and fill with random bytes. * * Returns pointer to allocaed & set buffer on success, size contains * number of random bytes filled. * * buf is NULL and *size set to KMT error value upon failure. * */u_char *malloc_random(size_t * size){ int rval = SNMPERR_SUCCESS; u_char *buf = (u_char *) calloc(1, *size); if (buf) { rval = sc_random(buf, size); if (rval < 0) { free_zero(buf, *size); buf = NULL; } else { *size = rval; } } return buf;} /* end malloc_random() *//*******************************************************************-o-****** * memdup * * Parameters: * to Pointer to allocate and copy memory to. * from Pointer to copy memory from. * size Size of the data to be copied. * * Returns * SNMPERR_SUCCESS On success. * SNMPERR_GENERR On failure. */intmemdup(u_char ** to, const u_char * from, size_t size){ if (to == NULL) return SNMPERR_GENERR; if (from == NULL) { *to = NULL; return SNMPERR_SUCCESS; } if ((*to = (u_char *) malloc(size)) == NULL) return SNMPERR_GENERR; memcpy(*to, from, size); return SNMPERR_SUCCESS;} /* end memdup() *//** copies a (possible) unterminated string of a given length into a * new buffer and null terminates it as well (new buffer MAY be one * byte longer to account for this */char *netsnmp_strdup_and_null(const u_char * from, size_t from_len){ u_char *ret; if (from_len == 0 || from[from_len - 1] != '\0') { ret = malloc(from_len + 1); if (!ret) return NULL; ret[from_len] = '\0'; } else { ret = malloc(from_len); if (!ret) return NULL; ret[from_len - 1] = '\0'; } memcpy(ret, from, from_len); return ret;}/*******************************************************************-o-****** * binary_to_hex * * Parameters: * *input Binary data. * len Length of binary data. * **output NULL terminated string equivalent in hex. * * Returns: * olen Length of output string not including NULL terminator. * * FIX Is there already one of these in the UCD SNMP codebase? * The old one should be used, or this one should be moved to * snmplib/snmp_api.c. */u_intbinary_to_hex(const u_char * input, size_t len, char **output){ u_int olen = (len * 2) + 1; char *s = (char *) calloc(1, olen), *op = s; const u_char *ip = input; while (ip - input < (int) len) { *op++ = VAL2HEX((*ip >> 4) & 0xf); *op++ = VAL2HEX(*ip & 0xf); ip++; } *op = '\0'; *output = s; return olen;} /* end binary_to_hex() *//*******************************************************************-o-****** * hex_to_binary2 * * Parameters: * *input Printable data in base16. * len Length in bytes of data. * **output Binary data equivalent to input. * * Returns: * SNMPERR_GENERR Failure. * <len> Otherwise, Length of allocated string. * * * Input of an odd length is right aligned. * * FIX Another version of "hex-to-binary" which takes odd length input * strings. It also allocates the memory to hold the binary data. * Should be integrated with the official hex_to_binary() function. */inthex_to_binary2(const u_char * input, size_t len, char **output){ u_int olen = (len / 2) + (len % 2); char *s = (char *) calloc(1, (olen) ? olen : 1), *op = s; const u_char *ip = input; *output = NULL; *op = 0; if (len % 2) { if (!isxdigit(*ip)) goto hex_to_binary2_quit; *op++ = HEX2VAL(*ip); ip++; } while (ip - input < (int) len) { if (!isxdigit(*ip)) goto hex_to_binary2_quit; *op = HEX2VAL(*ip) << 4; ip++; if (!isxdigit(*ip)) goto hex_to_binary2_quit; *op++ += HEX2VAL(*ip); ip++; } *output = s; return olen; hex_to_binary2_quit: free_zero(s, olen); return -1;} /* end hex_to_binary2() */intsnmp_decimal_to_binary(u_char ** buf, size_t * buf_len, size_t * out_len, int allow_realloc, const char *decimal){ int subid = 0; const char *cp = decimal; if (buf == NULL || buf_len == NULL || out_len == NULL || decimal == NULL) { return 0; } while (*cp != '\0') { if (isspace((int) *cp) || *cp == '.') { cp++; continue; } if (!isdigit((int) *cp)) { return 0; } if ((subid = atoi(cp)) > 255) { return 0; } if ((*out_len >= *buf_len) && !(allow_realloc && snmp_realloc(buf, buf_len))) { return 0; } *(*buf + *out_len) = (u_char) subid; (*out_len)++; while (isdigit((int) *cp)) { cp++; } } return 1;}intsnmp_hex_to_binary(u_char ** buf, size_t * buf_len, size_t * out_len, int allow_realloc, const char *hex){ int subid = 0; const char *cp = hex; if (buf == NULL || buf_len == NULL || out_len == NULL || hex == NULL) { return 0; } if ((*cp == '0') && ((*(cp + 1) == 'x') || (*(cp + 1) == 'X'))) { cp += 2; } while (*cp != '\0') { if (isspace((int) *cp)) { cp++; continue; } if (!isxdigit((int) *cp)) { return 0; } if (sscanf(cp, "%2x", &subid) == 0) { return 0; } if ((*out_len >= *buf_len) && !(allow_realloc && snmp_realloc(buf, buf_len))) { return 0; } *(*buf + *out_len) = (u_char) subid; (*out_len)++; if (*++cp == '\0') { /* * Odd number of hex digits is an error. */ return 0; } else { cp++; } } return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -