00001 00008 #ifndef _TOOLS_H 00009 #define _TOOLS_H 00010 00011 #ifdef __cplusplus 00012 extern "C" { 00013 #endif 00014 00015 00016 00017 /* 00018 * General acros and constants. 00019 */ 00020 #ifdef WIN32 00021 # define SNMP_MAXPATH MAX_PATH 00022 #else 00023 # ifdef PATH_MAX 00024 # define SNMP_MAXPATH PATH_MAX 00025 # else 00026 # ifdef MAXPATHLEN 00027 # define SNMP_MAXPATH MAXPATHLEN 00028 # else 00029 # define SNMP_MAXPATH 1024 /* Should be safe enough */ 00030 # endif 00031 # endif 00032 #endif 00033 00034 #define SNMP_MAXBUF (1024 * 4) 00035 #define SNMP_MAXBUF_MEDIUM 1024 00036 #define SNMP_MAXBUF_SMALL 512 00037 00038 #define SNMP_MAXBUF_MESSAGE 1500 00039 00040 #define SNMP_MAXOID 64 00041 #define SNMP_MAX_CMDLINE_OIDS 128 00042 00043 #define SNMP_FILEMODE_CLOSED 0600 00044 #define SNMP_FILEMODE_OPEN 0644 00045 00046 #define BYTESIZE(bitsize) ((bitsize + 7) >> 3) 00047 #define ROUNDUP8(x) ( ( (x+7) >> 3 ) * 8 ) 00048 00049 #define SNMP_STRORNULL(x) ( x ? x : "(null)") 00050 00053 #define SNMP_FREE(s) do { if (s) { free((void *)s); s=NULL; } } while(0) 00054 00057 #define SNMP_SWIPE_MEM(n,s) do { if (n) free((void *)n); n = s; s=NULL; } while(0) 00058 00059 /* 00060 * XXX Not optimal everywhere. 00061 */ 00064 #define SNMP_MALLOC_STRUCT(s) (struct s *) calloc(1, sizeof(struct s)) 00065 00068 #define SNMP_MALLOC_TYPEDEF(td) (td *) calloc(1, sizeof(td)) 00069 00072 #define SNMP_ZERO(s,l) do { if (s) memset(s, 0, l); } while(0) 00073 00074 00075 #define TOUPPER(c) (c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c) 00076 #define TOLOWER(c) (c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c) 00077 00078 #define HEX2VAL(s) \ 00079 ((isalpha(s) ? (TOLOWER(s)-'a'+10) : (TOLOWER(s)-'0')) & 0xf) 00080 #define VAL2HEX(s) ( (s) + (((s) >= 10) ? ('a'-10) : '0') ) 00081 00082 00085 #define SNMP_MAX(a,b) ((a) > (b) ? (a) : (b)) 00086 00089 #define SNMP_MIN(a,b) ((a) > (b) ? (b) : (a)) 00090 00099 #define SNMP_MACRO_VAL_TO_STR(s) SNMP_MACRO_VAL_TO_STR_PRIV(s) 00100 #define SNMP_MACRO_VAL_TO_STR_PRIV(s) #s 00101 00102 #ifndef FALSE 00103 #define FALSE 0 00104 #endif 00105 #ifndef TRUE 00106 #define TRUE 1 00107 #endif 00108 00109 /* 00110 * QUIT the FUNction: 00111 * e Error code variable 00112 * l Label to goto to cleanup and get out of the function. 00113 * 00114 * XXX It would be nice if the label could be constructed by the 00115 * preprocessor in context. Limited to a single error return value. 00116 * Temporary hack at best. 00117 */ 00118 #define QUITFUN(e, l) \ 00119 if ( (e) != SNMPERR_SUCCESS) { \ 00120 rval = SNMPERR_GENERR; \ 00121 goto l ; \ 00122 } 00123 00124 /* 00125 * DIFFTIMEVAL 00126 * Set <diff> to the difference between <now> (current) and <then> (past). 00127 * 00128 * ASSUMES that all inputs are (struct timeval)'s. 00129 * Cf. system.c:calculate_time_diff(). 00130 */ 00131 #define DIFFTIMEVAL(now, then, diff) \ 00132 { \ 00133 now.tv_sec--; \ 00134 now.tv_usec += 1000000L; \ 00135 diff.tv_sec = now.tv_sec - then.tv_sec; \ 00136 diff.tv_usec = now.tv_usec - then.tv_usec; \ 00137 if (diff.tv_usec > 1000000L){ \ 00138 diff.tv_usec -= 1000000L; \ 00139 diff.tv_sec++; \ 00140 } \ 00141 } 00142 00143 00144 /* 00145 * ISTRANSFORM 00146 * ASSUMES the minimum length for ttype and toid. 00147 */ 00148 #define USM_LENGTH_OID_TRANSFORM 10 00149 00150 #define ISTRANSFORM(ttype, toid) \ 00151 !snmp_oid_compare(ttype, USM_LENGTH_OID_TRANSFORM, \ 00152 usm ## toid ## Protocol, USM_LENGTH_OID_TRANSFORM) 00153 00154 #define ENGINETIME_MAX 2147483647 /* ((2^31)-1) */ 00155 #define ENGINEBOOT_MAX 2147483647 /* ((2^31)-1) */ 00156 00157 00158 00159 00160 /* 00161 * Prototypes. 00162 */ 00163 00164 int snmp_realloc(u_char ** buf, size_t * buf_len); 00165 00166 void free_zero(void *buf, size_t size); 00167 00168 u_char *malloc_random(size_t * size); 00169 u_char *malloc_zero(size_t size); 00170 int memdup(u_char ** to, const void * from, size_t size); 00171 00172 u_int binary_to_hex(const u_char * input, size_t len, 00173 char **output); 00174 /* preferred */ 00175 int netsnmp_hex_to_binary(u_char ** buf, size_t * buf_len, 00176 size_t * offset, int allow_realloc, 00177 const char *hex, const char *delim); 00178 /* calls netsnmp_hex_to_binary w/delim of " " */ 00179 int snmp_hex_to_binary(u_char ** buf, size_t * buf_len, 00180 size_t * offset, int allow_realloc, 00181 const char *hex); 00182 /* handles odd lengths */ 00183 int hex_to_binary2(const u_char * input, size_t len, 00184 char **output); 00185 00186 int snmp_decimal_to_binary(u_char ** buf, size_t * buf_len, 00187 size_t * out_len, 00188 int allow_realloc, 00189 const char *decimal); 00190 #define snmp_cstrcat(b,l,o,a,s) snmp_strcat(b,l,o,a,(const u_char *)s) 00191 int snmp_strcat(u_char ** buf, size_t * buf_len, 00192 size_t * out_len, int allow_realloc, 00193 const u_char * s); 00194 char *netsnmp_strdup_and_null(const u_char * from, 00195 size_t from_len); 00196 00197 void dump_chunk(const char *debugtoken, const char *title, 00198 const u_char * buf, int size); 00199 char *dump_snmpEngineID(const u_char * buf, size_t * buflen); 00200 00201 typedef void *marker_t; 00202 marker_t atime_newMarker(void); 00203 void atime_setMarker(marker_t pm); 00204 long atime_diff(marker_t first, marker_t second); 00205 u_long uatime_diff(marker_t first, marker_t second); /* 1/1000th sec */ 00206 u_long uatime_hdiff(marker_t first, marker_t second); /* 1/100th sec */ 00207 int atime_ready(marker_t pm, int deltaT); 00208 int uatime_ready(marker_t pm, unsigned int deltaT); 00209 00210 int marker_tticks(marker_t pm); 00211 int timeval_tticks(struct timeval *tv); 00212 char *netsnmp_getenv(const char *name); 00213 00214 int netsnmp_addrstr_hton(char *ptr, size_t len); 00215 00216 #ifdef __cplusplus 00217 } 00218 #endif 00219 #endif /* _TOOLS_H */ 00220 /* @} */
1.5.7.1
Last modified: Tuesday, 23-Dec-2025 17:22:04 UTC
For questions regarding web content and site functionality, please write to the net-snmp-users mail list.