net-snmp  5.4.1
tools.h
Go to the documentation of this file.
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 
00091 #ifndef FALSE
00092 #define FALSE 0
00093 #endif
00094 #ifndef TRUE
00095 #define TRUE  1
00096 #endif
00097 
00098     /*
00099      * QUIT the FUNction:
00100      *      e       Error code variable
00101      *      l       Label to goto to cleanup and get out of the function.
00102      *
00103      * XXX  It would be nice if the label could be constructed by the
00104      *      preprocessor in context.  Limited to a single error return value.
00105      *      Temporary hack at best.
00106      */
00107 #define QUITFUN(e, l)                   \
00108         if ( (e) != SNMPERR_SUCCESS) {  \
00109                 rval = SNMPERR_GENERR;  \
00110                 goto l ;                \
00111         }
00112 
00113     /*
00114      * DIFFTIMEVAL
00115      *      Set <diff> to the difference between <now> (current) and <then> (past).
00116      *
00117      * ASSUMES that all inputs are (struct timeval)'s.
00118      * Cf. system.c:calculate_time_diff().
00119      */
00120 #define DIFFTIMEVAL(now, then, diff)                    \
00121 {                                                       \
00122         now.tv_sec--;                                   \
00123         now.tv_usec += 1000000L;                        \
00124         diff.tv_sec  = now.tv_sec  - then.tv_sec;       \
00125         diff.tv_usec = now.tv_usec - then.tv_usec;      \
00126         if (diff.tv_usec > 1000000L){                   \
00127                 diff.tv_usec -= 1000000L;               \
00128                 diff.tv_sec++;                          \
00129         }                                               \
00130 }
00131 
00132 
00133     /*
00134      * ISTRANSFORM
00135      * ASSUMES the minimum length for ttype and toid.
00136      */
00137 #define USM_LENGTH_OID_TRANSFORM        10
00138 
00139 #define ISTRANSFORM(ttype, toid)                                        \
00140         !snmp_oid_compare(ttype, USM_LENGTH_OID_TRANSFORM,              \
00141                 usm ## toid ## Protocol, USM_LENGTH_OID_TRANSFORM)
00142 
00143 #define ENGINETIME_MAX  2147483647      /* ((2^31)-1) */
00144 #define ENGINEBOOT_MAX  2147483647      /* ((2^31)-1) */
00145 
00146 
00147 
00148 
00149     /*
00150      * Prototypes.
00151      */
00152 
00153     int             snmp_realloc(u_char ** buf, size_t * buf_len);
00154 
00155     void            free_zero(void *buf, size_t size);
00156 
00157     u_char         *malloc_random(size_t * size);
00158     u_char         *malloc_zero(size_t size);
00159     int             memdup(u_char ** to, const u_char * from, size_t size);
00160 
00161     u_int           binary_to_hex(const u_char * input, size_t len,
00162                                   char **output);
00163                     /* preferred */
00164     int             netsnmp_hex_to_binary(u_char ** buf, size_t * buf_len,
00165                                          size_t * offset, int allow_realloc,
00166                                          const char *hex, const char *delim);
00167                     /* calls netsnmp_hex_to_binary w/delim of " " */
00168     int             snmp_hex_to_binary(u_char ** buf, size_t * buf_len,
00169                                        size_t * offset, int allow_realloc,
00170                                        const char *hex);
00171                     /* handles odd lengths */
00172     int             hex_to_binary2(const u_char * input, size_t len,
00173                                    char **output);
00174 
00175     int             snmp_decimal_to_binary(u_char ** buf, size_t * buf_len,
00176                                            size_t * out_len,
00177                                            int allow_realloc,
00178                                            const char *decimal);
00179 #define snmp_cstrcat(b,l,o,a,s) snmp_strcat(b,l,o,a,(const u_char *)s)
00180     int             snmp_strcat(u_char ** buf, size_t * buf_len,
00181                                 size_t * out_len, int allow_realloc,
00182                                 const u_char * s);
00183     char           *netsnmp_strdup_and_null(const u_char * from,
00184                                             size_t from_len);
00185 
00186     void            dump_chunk(const char *debugtoken, const char *title,
00187                                const u_char * buf, int size);
00188     char           *dump_snmpEngineID(const u_char * buf, size_t * buflen);
00189 
00190     typedef void   *marker_t;
00191     marker_t        atime_newMarker(void);
00192     void            atime_setMarker(marker_t pm);
00193     long            atime_diff(marker_t first, marker_t second);
00194     u_long          uatime_diff(marker_t first, marker_t second);       /* 1/1000th sec */
00195     u_long          uatime_hdiff(marker_t first, marker_t second);      /* 1/100th sec */
00196     int             atime_ready(marker_t pm, int deltaT);
00197     int             uatime_ready(marker_t pm, unsigned int deltaT);
00198 
00199     int             marker_tticks(marker_t pm);
00200     int             timeval_tticks(struct timeval *tv);
00201     char            *netsnmp_getenv(const char *name);
00202 
00203     int             netsnmp_addrstr_hton(char *ptr, size_t len);
00204 
00205 #ifdef __cplusplus
00206 }
00207 #endif
00208 #endif                          /* _TOOLS_H */
00209 /* @} */